chiark / gitweb /
bump version number to 0.6
[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.6
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         # Insert call into the hook
58         {
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"
65 }
66
67 # setup_ours (no arguments)
68 setup_ours()
69 {
70         if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
71                 {
72                         echo ".topmsg   merge=ours"
73                         echo ".topdeps  merge=ours"
74                 } >>"$git_dir/info/attributes"
75         fi
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'
79         fi
80 }
81
82 # measure_branch NAME [BASE]
83 measure_branch()
84 {
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"
94         fi
95         echo "$_commits/$_nmcommits $_suffix"
96 }
97
98 # branch_contains B1 B2
99 # Whether B1 is a superset of B2.
100 branch_contains()
101 {
102         [ -z "$(git rev-list ^"$1" "$2" --)" ]
103 }
104
105 # ref_exists REF
106 # Whether REF is a valid ref name
107 ref_exists()
108 {
109         git rev-parse --verify "$@" >/dev/null 2>&1
110 }
111
112 # has_remote BRANCH
113 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
114 has_remote()
115 {
116         [ -n "$base_remote" ] && ref_exists "remotes/$base_remote/$1"
117 }
118
119 # recurse_deps CMD NAME [BRANCHPATH...]
120 # Recursively eval CMD on all dependencies of NAME.
121 # CMD can refer to $_name for queried branch name,
122 # $_dep for dependency name,
123 # $_depchain for space-seperated branch backtrace,
124 # and the $_dep_is_tgish boolean.
125 # It can modify $_ret to affect the return value
126 # of the whole function.
127 # If recurse_deps() hits missing dependencies, it will append
128 # them to space-separated $missing_deps list and skip them.
129 recurse_deps()
130 {
131         _cmd="$1"; shift
132         _name="$1"; # no shift
133         _depchain="$*"
134
135         _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
136         # Check also our base against remote base. Checking our head
137         # against remote head has to be done in the helper.
138         if has_remote "top-bases/$_name"; then
139                 echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
140         fi
141         git cat-file blob "$_name:.topdeps" >>"$_depsfile"
142
143         _ret=0
144         while read _dep; do
145                 if ! ref_exists "$_dep" ; then
146                         # All hope is lost
147                         missing_deps="$missing_deps $_dep"
148                         continue
149                 fi
150
151                 _dep_is_tgish=1
152                 ref_exists "refs/top-bases/$_dep"  ||
153                         _dep_is_tgish=
154
155                 # Shoo shoo, keep our environment alone!
156                 [ -z "$_dep_is_tgish" ] ||
157                         (recurse_deps "$_cmd" "$_dep" "$@") ||
158                         _ret=$?
159
160                 eval "$_cmd"
161         done <"$_depsfile"
162         missing_deps="${missing_deps# }"
163         rm "$_depsfile"
164         return $_ret
165 }
166
167 # branch_needs_update
168 # This is a helper function for determining whether given branch
169 # is up-to-date wrt. its dependencies. It expects input as if it
170 # is called as a recurse_deps() helper.
171 # In case the branch does need update, it will echo it together
172 # with the branch backtrace on the output (see needs_update()
173 # description for details) and set $_ret to non-zero.
174 branch_needs_update()
175 {
176         _dep_base_update=
177         if [ -n "$_dep_is_tgish" ]; then
178                 if has_remote "$_dep"; then
179                         branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
180                 fi
181                 # This can possibly override the remote check result;
182                 # we want to sync with our base first
183                 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
184         fi
185
186         if [ -n "$_dep_base_update" ]; then
187                 # _dep needs to be synced with its base/remote
188                 echo "$_dep_base_update $_dep $_depchain"
189                 _ret=1
190         elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
191                 # Some new commits in _dep
192                 echo "$_dep $_depchain"
193                 _ret=1
194         fi
195 }
196
197 # needs_update NAME
198 # This function is recursive; it outputs reverse path from NAME
199 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
200 # inner paths first. Innermost name can be ':' if the head is
201 # not in sync with the base or '%' if the head is not in sync
202 # with the remote (in this order of priority).
203 # It will also return non-zero status if NAME needs update.
204 # If needs_update() hits missing dependencies, it will append
205 # them to space-separated $missing_deps list and skip them.
206 needs_update()
207 {
208         recurse_deps branch_needs_update "$@"
209 }
210
211 # branch_empty NAME
212 branch_empty()
213 {
214         [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v "     .top")" ]
215 }
216
217 # switch_to_base NAME [SEED]
218 switch_to_base()
219 {
220         _base="refs/top-bases/$1"; _seed="$2"
221         # We have to do all the hard work ourselves :/
222         # This is like git checkout -b "$_base" "$_seed"
223         # (or just git checkout "$_base"),
224         # but does not create a detached HEAD.
225         git read-tree -u -m HEAD "${_seed:-$_base}"
226         [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
227         git symbolic-ref HEAD "$_base"
228 }
229
230 # Show the help messages.
231 do_help()
232 {
233         if [ -z "$1" ] ; then
234                 # This is currently invoked in all kinds of circumstances,
235                 # including when the user made a usage error. Should we end up
236                 # providing more than a short help message, then we should
237                 # differentiate.
238                 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
239
240                 ## Build available commands list for help output
241
242                 cmds=
243                 sep=
244                 for cmd in "@cmddir@"/tg-*; do
245                         ! [ -r "$cmd" ] && continue
246                         # strip directory part and "tg-" prefix
247                         cmd="$(basename "$cmd")"
248                         cmd="${cmd#tg-}"
249                         cmds="$cmds$sep$cmd"
250                         sep="|"
251                 done
252
253                 echo "TopGit v$TG_VERSION - A different patch queue manager"
254                 echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
255         elif [ -r "@cmddir@"/tg-$1 ] ; then
256                 @cmddir@/tg-$1 -h || :
257                 echo
258                 if [ -r "@sharedir@/tg-$1.txt" ] ; then
259                         cat "@sharedir@/tg-$1.txt"
260                 fi
261         else
262                 echo "`basename $0`: no help for $1" 1>&2
263                 do_help
264                 exit 1
265         fi
266 }
267
268 ## Pager stuff
269
270 # isatty FD
271 isatty()
272 {
273         test -t $1
274 }
275
276 # setup_pager
277 # Spawn pager process and redirect the rest of our output to it
278 setup_pager()
279 {
280         isatty 1 || return 0
281
282         # TG_PAGER = GIT_PAGER | PAGER | less
283         # NOTE: GIT_PAGER='' is significant
284         TG_PAGER=${GIT_PAGER-${PAGER-less}}
285
286         [ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
287
288
289         # now spawn pager
290         export LESS=${LESS:-FRSX}       # as in pager.c:pager_preexec()
291
292         _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
293         _pager_fifo="$_pager_fifo_dir/0"
294         mkfifo -m 600 "$_pager_fifo"
295
296         "$TG_PAGER" < "$_pager_fifo" &
297         exec > "$_pager_fifo"           # dup2(pager_fifo.in, 1)
298
299         # this is needed so e.g. `git diff` will still colorize it's output if
300         # requested in ~/.gitconfig with color.diff=auto
301         export GIT_PAGER_IN_USE=1
302
303         # atexit(close(1); wait pager)
304         trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
305 }
306
307 ## Startup
308
309 [ -d "@cmddir@" ] ||
310         die "No command directory: '@cmddir@'"
311
312 ## Initial setup
313
314 set -e
315 git_dir="$(git rev-parse --git-dir)"
316 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
317 # Make sure root_dir doesn't end with a trailing slash.
318 root_dir="${root_dir%/}"
319 base_remote="$(git config topgit.remote 2>/dev/null)" || :
320 tg="tg"
321 # make sure merging the .top* files will always behave sanely
322 setup_ours
323 setup_hook "pre-commit"
324
325 ## Dispatch
326
327 # We were sourced from another script for our utility functions;
328 # this is set by hooks.
329 [ -z "$tg__include" ] || return 0
330
331 if [ "$1" = "-r" ]; then
332         shift
333         if [ -z "$1" ]; then
334                 echo "Option -r requires an argument." >&2
335                 do_help
336                 exit 1
337         fi
338         base_remote="$1"; shift
339         tg="$tg -r $base_remote"
340 fi
341
342 cmd="$1"
343 [ -n "$cmd" ] || { do_help; exit 1; }
344 shift
345
346 case "$cmd" in
347 help|--help|-h)
348         do_help "$1"
349         exit 0;;
350 --hooks-path)
351         # Internal command
352         echo "@hooksdir@";;
353 *)
354         [ -r "@cmddir@"/tg-$cmd ] || {
355                 echo "Unknown subcommand: $cmd" >&2
356                 do_help
357                 exit 1
358         }
359         . "@cmddir@"/tg-$cmd;;
360 esac
361
362 # vim:noet