hooks/pre-commit: check for cycles in dependencies
[topgit.git] / tg.sh
blob8264a3b074f1d6bbded09e8110f1c07be475ffdd
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 TG_VERSION=0.8
8 ## Auxiliary functions
10 info()
12 echo "${TG_RECURSIVE}tg: $*"
15 die()
17 info "fatal: $*" >&2
18 exit 1
21 # cat_file "topic:file"
22 # Like `git cat-file blob $1`, but topics '(i)' and '(w)' means index and worktree
23 cat_file()
25 arg="$1"
26 case "$arg" in
27 '(w):'*)
28 cat "${arg#(w):}"
30 '(i):'*)
31 # ':file' means cat from index
32 git cat-file blob "${arg#(i)}"
35 git cat-file blob "$arg"
37 esac
40 # setup_hook NAME
41 setup_hook()
43 hook_call="\"\$($tg --hooks-path)\"/$1 \"\$@\""
44 if [ -f "$git_dir/hooks/$1" ] &&
45 fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
46 # Another job well done!
47 return
49 # Prepare incantation
50 if [ -x "$git_dir/hooks/$1" ]; then
51 hook_call="$hook_call"' || exit $?'
52 else
53 hook_call="exec $hook_call"
55 # Don't call hook if tg is not installed
56 hook_call="if which \"$tg\" > /dev/null; then $hook_call; fi"
57 # Insert call into the hook
59 echo "#!/bin/sh"
60 echo "$hook_call"
61 [ ! -s "$git_dir/hooks/$1" ] || cat "$git_dir/hooks/$1"
62 } >"$git_dir/hooks/$1+"
63 chmod a+x "$git_dir/hooks/$1+"
64 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
67 # setup_ours (no arguments)
68 setup_ours()
70 if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
72 echo ".topmsg merge=ours"
73 echo ".topdeps merge=ours"
74 } >>"$git_dir/info/attributes"
76 if ! git config merge.ours.driver >/dev/null; then
77 git config merge.ours.name '"always keep ours" merge driver'
78 git config merge.ours.driver 'touch %A'
82 # measure_branch NAME [BASE]
83 measure_branch()
85 _bname="$1"; _base="$2"
86 [ -n "$_base" ] || _base="refs/top-bases/$_bname"
87 # The caller should've verified $name is valid
88 _commits="$(git rev-list "$_bname" ^"$_base" -- | wc -l)"
89 _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" -- | wc -l)"
90 if [ $_commits -gt 1 ]; then
91 _suffix="commits"
92 else
93 _suffix="commit"
95 echo "$_commits/$_nmcommits $_suffix"
98 # branch_contains B1 B2
99 # Whether B1 is a superset of B2.
100 branch_contains()
102 [ -z "$(git rev-list --max-count=1 ^"$1" "$2" --)" ]
105 # ref_exists REF
106 # Whether REF is a valid ref name
107 ref_exists()
109 git rev-parse --verify "$@" >/dev/null 2>&1
112 # has_remote BRANCH
113 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
114 has_remote()
116 [ -n "$base_remote" ] && ref_exists "remotes/$base_remote/$1"
119 branch_annihilated()
121 _name="$1";
123 # use the merge base in case the base is ahead.
124 mb="$(git merge-base "refs/top-bases/$_name" "$_name")";
126 test "$(git rev-parse "$mb^{tree}")" = "$(git rev-parse "$_name^{tree}")";
129 # is_sha1 REF
130 # Whether REF is a SHA1 (compared to a symbolic name).
131 is_sha1()
133 [ "$(git rev-parse "$1")" = "$1" ]
136 # recurse_deps CMD NAME [BRANCHPATH...]
137 # Recursively eval CMD on all dependencies of NAME.
138 # CMD can refer to $_name for queried branch name,
139 # $_dep for dependency name,
140 # $_depchain for space-seperated branch backtrace,
141 # and the $_dep_is_tgish boolean.
142 # It can modify $_ret to affect the return value
143 # of the whole function.
144 # If recurse_deps() hits missing dependencies, it will append
145 # them to space-separated $missing_deps list and skip them.
146 # remote dependencies are processed if no_remotes is unset.
147 recurse_deps()
149 _cmd="$1"; shift
150 _name="$1"; # no shift
151 _depchain="$*"
153 _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
154 # If no_remotes is unset check also our base against remote base.
155 # Checking our head against remote head has to be done in the helper.
156 if test -z "$no_remotes" && has_remote "top-bases/$_name"; then
157 echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
160 # if the branch was annihilated, there exists no .topdeps file
161 if ! branch_annihilated "$_name"; then
162 #TODO: handle nonexisting .topdeps?
163 git cat-file blob "$_name:.topdeps" >>"$_depsfile";
166 _ret=0
167 while read _dep; do
168 if ! ref_exists "$_dep" ; then
169 # All hope is lost
170 missing_deps="$missing_deps $_dep"
171 continue
174 _dep_is_tgish=1
175 ref_exists "refs/top-bases/$_dep" ||
176 _dep_is_tgish=
178 # Shoo shoo, keep our environment alone!
179 [ -z "$_dep_is_tgish" ] ||
180 (recurse_deps "$_cmd" "$_dep" "$@") ||
181 _ret=$?
183 eval "$_cmd"
184 done <"$_depsfile"
185 missing_deps="${missing_deps# }"
186 rm "$_depsfile"
187 return $_ret
190 # branch_needs_update
191 # This is a helper function for determining whether given branch
192 # is up-to-date wrt. its dependencies. It expects input as if it
193 # is called as a recurse_deps() helper.
194 # In case the branch does need update, it will echo it together
195 # with the branch backtrace on the output (see needs_update()
196 # description for details) and set $_ret to non-zero.
197 branch_needs_update()
199 _dep_base_update=
200 if [ -n "$_dep_is_tgish" ]; then
201 if has_remote "$_dep"; then
202 branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
204 # This can possibly override the remote check result;
205 # we want to sync with our base first
206 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
209 if [ -n "$_dep_base_update" ]; then
210 # _dep needs to be synced with its base/remote
211 echo "$_dep_base_update $_dep $_depchain"
212 _ret=1
213 elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
214 # Some new commits in _dep
215 echo "$_dep $_depchain"
216 _ret=1
220 # needs_update NAME
221 # This function is recursive; it outputs reverse path from NAME
222 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
223 # inner paths first. Innermost name can be ':' if the head is
224 # not in sync with the base or '%' if the head is not in sync
225 # with the remote (in this order of priority).
226 # It will also return non-zero status if NAME needs update.
227 # If needs_update() hits missing dependencies, it will append
228 # them to space-separated $missing_deps list and skip them.
229 needs_update()
231 recurse_deps branch_needs_update "$@"
234 # branch_empty NAME
235 branch_empty()
237 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" -- | fgrep -v " .top")" ]
240 # list_deps
241 list_deps()
243 git for-each-ref refs/top-bases |
244 while read rev type ref; do
245 name="${ref#refs/top-bases/}"
246 if branch_annihilated "$name"; then
247 continue;
250 git cat-file blob "$name:.topdeps" | while read dep; do
251 dep_is_tgish=true
252 ref_exists "refs/top-bases/$dep" ||
253 dep_is_tgish=false
254 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
255 echo "$name $dep"
257 done
258 done
261 # switch_to_base NAME [SEED]
262 switch_to_base()
264 _base="refs/top-bases/$1"; _seed="$2"
265 # We have to do all the hard work ourselves :/
266 # This is like git checkout -b "$_base" "$_seed"
267 # (or just git checkout "$_base"),
268 # but does not create a detached HEAD.
269 git read-tree -u -m HEAD "${_seed:-$_base}"
270 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
271 git symbolic-ref HEAD "$_base"
274 # Show the help messages.
275 do_help()
277 if [ -z "$1" ] ; then
278 # This is currently invoked in all kinds of circumstances,
279 # including when the user made a usage error. Should we end up
280 # providing more than a short help message, then we should
281 # differentiate.
282 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
284 ## Build available commands list for help output
286 cmds=
287 sep=
288 for cmd in "@cmddir@"/tg-*; do
289 ! [ -r "$cmd" ] && continue
290 # strip directory part and "tg-" prefix
291 cmd="$(basename "$cmd")"
292 cmd="${cmd#tg-}"
293 cmds="$cmds$sep$cmd"
294 sep="|"
295 done
297 echo "TopGit v$TG_VERSION - A different patch queue manager"
298 echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
299 elif [ -r "@cmddir@"/tg-$1 ] ; then
300 setup_pager
301 @cmddir@/tg-$1 -h 2>&1 || :
302 echo
303 if [ -r "@sharedir@/tg-$1.txt" ] ; then
304 cat "@sharedir@/tg-$1.txt"
306 else
307 echo "`basename $0`: no help for $1" 1>&2
308 do_help
309 exit 1
313 ## Pager stuff
315 # isatty FD
316 isatty()
318 test -t $1
321 # setup_pager
322 # Spawn pager process and redirect the rest of our output to it
323 setup_pager()
325 isatty 1 || return 0
327 # TG_PAGER = GIT_PAGER | PAGER | less
328 # NOTE: GIT_PAGER='' is significant
329 TG_PAGER=${GIT_PAGER-${PAGER-less}}
331 [ -z "$TG_PAGER" -o "$TG_PAGER" = "cat" ] && return 0
334 # now spawn pager
335 export LESS="${LESS:-FRSX}" # as in pager.c:pager_preexec()
337 _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
338 _pager_fifo="$_pager_fifo_dir/0"
339 mkfifo -m 600 "$_pager_fifo"
341 "$TG_PAGER" < "$_pager_fifo" &
342 exec > "$_pager_fifo" # dup2(pager_fifo.in, 1)
344 # this is needed so e.g. `git diff` will still colorize it's output if
345 # requested in ~/.gitconfig with color.diff=auto
346 export GIT_PAGER_IN_USE=1
348 # atexit(close(1); wait pager)
349 trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
352 ## Startup
354 [ -d "@cmddir@" ] ||
355 die "No command directory: '@cmddir@'"
357 ## Initial setup
359 set -e
360 git_dir="$(git rev-parse --git-dir)"
361 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
362 # Make sure root_dir doesn't end with a trailing slash.
363 root_dir="${root_dir%/}"
364 base_remote="$(git config topgit.remote 2>/dev/null)" || :
365 tg="tg"
366 # make sure merging the .top* files will always behave sanely
367 setup_ours
368 setup_hook "pre-commit"
370 ## Dispatch
372 # We were sourced from another script for our utility functions;
373 # this is set by hooks. Skip the rest of the file. A simple return doesn't
374 # work as expected in every shell. See http://bugs.debian.org/516188
375 if [ -z "$tg__include" ]; then
377 if [ "$1" = "-r" ]; then
378 shift
379 if [ -z "$1" ]; then
380 echo "Option -r requires an argument." >&2
381 do_help
382 exit 1
384 base_remote="$1"; shift
385 tg="$tg -r $base_remote"
388 cmd="$1"
389 [ -n "$cmd" ] || { do_help; exit 1; }
390 shift
392 case "$cmd" in
393 help|--help|-h)
394 do_help "$1"
395 exit 0;;
396 --hooks-path)
397 # Internal command
398 echo "@hooksdir@";;
400 [ -r "@cmddir@"/tg-$cmd ] || {
401 echo "Unknown subcommand: $cmd" >&2
402 do_help
403 exit 1
405 . "@cmddir@"/tg-$cmd;;
406 esac
410 # vim:noet