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