chiark / gitweb /
Check for pre-commit hook existence.
[topgit.git] / tg.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6
7 ## Auxiliary functions
8
9 info()
10 {
11         echo "${TG_RECURSIVE}tg: $*"
12 }
13
14 die()
15 {
16         info "fatal: $*"
17         exit 1
18 }
19
20 # setup_hook NAME
21 setup_hook()
22 {
23         hook_call="\"\$(tg --hooks-path)\"/$1 \"\$@\""
24         if [ -x "$git_dir/hooks/$1" ]; then
25                 if fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
26                         # Another job well done!
27                         return
28                 fi
29         fi
30         # Prepare incanation
31         if [ -x "$git_dir/hooks/$1" ]; then
32                 hook_call="$hook_call"' || exit $?'
33         else
34                 hook_call="exec $hook_call"
35         fi
36         # Insert call into the hook
37         {
38                 echo "#!/bin/sh"
39                 echo "$hook_call"
40                 [ -x "$git_dir/hooks/$1" ] && cat "$git_dir/hooks/$1"
41         } >"$git_dir/hooks/$1+"
42         chmod a+x "$git_dir/hooks/$1+"
43         mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
44 }
45
46 # setup_ours (no arguments)
47 setup_ours()
48 {
49         if [ ! -s "$git_dir/info/gitattributes" ] || ! grep -q topmsg "$git_dir/info/gitattributes"; then
50                 {
51                         echo -e ".topmsg\tmerge=ours"
52                         echo -e ".topdeps\tmerge=ours"
53                 } >>"$git_dir/info/gitattributes"
54         fi
55         if ! git config merge.ours.driver >/dev/null; then
56                 git config merge.ours.name '"always keep ours" merge driver'
57                 git config merge.ours.driver 'touch %A'
58         fi
59 }
60
61 # measure_branch NAME [BASE]
62 measure_branch()
63 {
64         _name="$1"; _base="$2"
65         [ -n "$_base" ] || _base="refs/top-bases/$_name"
66         # The caller should've verified $name is valid
67         _commits="$(git rev-list "$_name" ^"$_base" | wc -l)"
68         _nmcommits="$(git rev-list --no-merges "$_name" ^"$_base" | wc -l)"
69         if [ $_commits -gt 1 ]; then
70                 _suffix="commits"
71         else
72                 _suffix="commit"
73         fi
74         echo "$_commits/$_nmcommits $_suffix"
75 }
76
77 # branch_contains B1 B2
78 # Whether B1 is a superset of B2.
79 branch_contains()
80 {
81         [ -z "$(git rev-list ^"$1" "$2")" ]
82 }
83
84 # needs_update NAME [BRANCHPATH...]
85 # This function is recursive; it outputs reverse path from NAME
86 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
87 # inner paths first. Innermost name can be ':' if the head is
88 # not in sync with the base.
89 needs_update()
90 {
91         {
92         git cat-file blob "$1:.topdeps" 2>/dev/null |
93                 while read _dep; do
94                         _dep_is_tgish=1
95                         git rev-parse --verify "refs/top-bases/$_dep" >/dev/null 2>&1 ||
96                                 _dep_is_tgish=
97
98                         # Shoo shoo, keep our environment alone!
99                         [ -z "$_dep_is_tgish" ] || (needs_update "$_dep" "$@")
100
101                         _dep_base_uptodate=1
102                         if [ -n "$_dep_is_tgish" ]; then
103                                 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
104                         fi
105
106                         if [ -z "$_dep_base_uptodate" ]; then
107                                 # _dep needs to be synced with its base
108                                 echo ": $_dep $*"
109                         elif ! branch_contains "refs/top-bases/$1" "$_dep"; then
110                                 # Some new commits in _dep
111                                 echo "$_dep $*"
112                         fi
113                 done
114         } || : # $1 is not tracked by TopGit anymore
115 }
116
117 # branch_empty NAME
118 branch_empty()
119 {
120         [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v "     .top")" ]
121 }
122
123 # switch_to_base NAME [SEED]
124 switch_to_base()
125 {
126         _base="refs/top-bases/$1"; _seed="$2"
127         # We have to do all the hard work ourselves :/
128         # This is like git checkout -b "$_base" "$_seed"
129         # (or just git checkout "$_base"),
130         # but does not create a detached HEAD.
131         git read-tree -u -m HEAD "${_seed:-$_base}"
132         [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
133         git symbolic-ref HEAD "$_base"
134 }
135
136
137 ## Initial setup
138
139 set -e
140 git_dir="$(git rev-parse --git-dir)"
141 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
142 # make sure merging the .top* files will always behave sanely
143 setup_ours
144 setup_hook "pre-commit"
145
146
147 ## Dispatch
148
149 # We were sourced from another script for our utility functions;
150 # this is set by hooks.
151 [ -z "$tg__include" ] || return 0
152
153 cmd="$1"
154 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
155 shift
156
157 case "$cmd" in
158 help)
159         echo "TopGit v0.1 - A different patch queue manager"
160         echo "Usage: tg (create|delete|info|patch|summary|update|help) ..."
161         exit 1;;
162 create|delete|info|patch|summary|update)
163         . "@cmddir@"/tg-$cmd;;
164 --hooks-path)
165         # Internal command
166         echo "@hooksdir@";;
167 *)
168         echo "Unknown subcommand: $cmd" >&2
169         exit 1;;
170 esac