-
Notifications
You must be signed in to change notification settings - Fork 46
/
install_hook.sh
executable file
·85 lines (67 loc) · 2.44 KB
/
install_hook.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env bash
# Copyright 2017-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included in
# the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
# file, in accordance with the Business Source License, use of this software
# will be governed by the Apache License, Version 2.0, included in the file
# licenses/APL2.txt.
usage () {
cat << EOF
Usage: install_hook.sh project [project ...]
Installs a pre-commit hook for the given projects to run
git clang-format
before each commit.
Once installed, the behaviour of the hook can be customised with
git config couchbase.clangformat <BEHAVIOUR>
Where the valid options are:
"warn" : Block the commit if there are style issues but do not make
changes to files (default)
"fix" : Block the commit, Fix style issues but do not stage the
resulting changes. Changes can be reviewed with \`git diff\`.
"stage" : Block, fix issues and immediately stage the changes
ready to commit.
"commit" : Transparently fix style issues and allow the commit.
In all cases except "warn", if clang-format refuses to run
(e.g., if it would modify files with unstaged changes) files
will not be changed and the commit will be refused. Stash any
unstaged changes you do not wish to commit in this case.
EOF
}
install_gcf_hook () {
proj=$1
base_dir=$2
if [ -e $base_dir/.git/hooks/pre-commit ] ; then
echo "Project $proj already has a pre-commit hook, not overwriting it!"
return 1
else
echo "Installing pre-commit hook in $base_dir/.git/hooks/pre-commit"
cp $(repo list -fpr tlm)/clang-format-pre-commit-hook $base_dir/.git/hooks/pre-commit
fi
}
# Show usage if "-h" or no projects given
if [[ ! "${@#-h}" = "$@" ]] || [[ $# = 0 ]] ; then
usage
exit
fi
# Very basic check whether prereqs are present
for tool in clang-format git-clang-format ; do
if [ -z $(which $tool) ] ; then
echo "$tool not found, please install it, e.g. \`brew install $tool\` and try again."
fi
done
any_failed=0
for project in $@ ; do
# Attempt to find the project dir from repo
project_dir=`repo list -fpr $project`
if [ -z $project_dir ] ; then
echo "Project '$project' not found"
continue
else
install_gcf_hook $project $project_dir
if [ ! $? = 0 ] ; then
any_failed=1
fi
fi
done
exit $any_failed