Fix typo s/emmail/email/
[topgit/greenrd.git] / tg.sh
blobf0496f1b6ade99b1f9290f3cc5fe7096fc4373ba
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 TG_VERSION=0.6
8 ## Auxiliary functions
10 info()
12 echo "${TG_RECURSIVE}tg: $*"
15 die()
17 info "fatal: $*"
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 arg=$(echo "$arg" | tail --bytes=+5)
29 cat "$arg"
30 return
32 '(i):'*)
33 # ':file' means cat from index
34 arg=$(echo "$arg" | tail --bytes=+5)
35 git cat-file blob ":$arg"
38 git cat-file blob "$arg"
39 esac
42 # setup_hook NAME
43 setup_hook()
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
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"
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 # recurse_deps CMD NAME [BRANCHPATH...]
130 # Recursively eval CMD on all dependencies of NAME.
131 # CMD can refer to $_name for queried branch name,
132 # $_dep for dependency name,
133 # $_depchain for space-seperated branch backtrace,
134 # and the $_dep_is_tgish boolean.
135 # It can modify $_ret to affect the return value
136 # of the whole function.
137 # If recurse_deps() hits missing dependencies, it will append
138 # them to space-separated $missing_deps list and skip them.
139 recurse_deps()
141 _cmd="$1"; shift
142 _name="$1"; # no shift
143 _depchain="$*"
145 _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
146 # Check also our base against remote base. Checking our head
147 # against remote head has to be done in the helper.
148 if has_remote "top-bases/$_name"; then
149 echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
152 # if the branch was annihilated, there exists no .topdeps file
153 if ! branch_annihilated "$_name"; then
154 #TODO: handle nonexisting .topdeps?
155 git cat-file blob "$_name:.topdeps" >>"$_depsfile";
158 _ret=0
159 while read _dep; do
160 if ! ref_exists "$_dep" ; then
161 # All hope is lost
162 missing_deps="$missing_deps $_dep"
163 continue
166 _dep_is_tgish=1
167 ref_exists "refs/top-bases/$_dep" ||
168 _dep_is_tgish=
170 # Shoo shoo, keep our environment alone!
171 [ -z "$_dep_is_tgish" ] ||
172 (recurse_deps "$_cmd" "$_dep" "$@") ||
173 _ret=$?
175 eval "$_cmd"
176 done <"$_depsfile"
177 missing_deps="${missing_deps# }"
178 rm "$_depsfile"
179 return $_ret
182 # branch_needs_update
183 # This is a helper function for determining whether given branch
184 # is up-to-date wrt. its dependencies. It expects input as if it
185 # is called as a recurse_deps() helper.
186 # In case the branch does need update, it will echo it together
187 # with the branch backtrace on the output (see needs_update()
188 # description for details) and set $_ret to non-zero.
189 branch_needs_update()
191 _dep_base_update=
192 if [ -n "$_dep_is_tgish" ]; then
193 if has_remote "$_dep"; then
194 branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
196 # This can possibly override the remote check result;
197 # we want to sync with our base first
198 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
201 if [ -n "$_dep_base_update" ]; then
202 # _dep needs to be synced with its base/remote
203 echo "$_dep_base_update $_dep $_depchain"
204 _ret=1
205 elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
206 # Some new commits in _dep
207 echo "$_dep $_depchain"
208 _ret=1
212 # needs_update NAME
213 # This function is recursive; it outputs reverse path from NAME
214 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
215 # inner paths first. Innermost name can be ':' if the head is
216 # not in sync with the base or '%' if the head is not in sync
217 # with the remote (in this order of priority).
218 # It will also return non-zero status if NAME needs update.
219 # If needs_update() hits missing dependencies, it will append
220 # them to space-separated $missing_deps list and skip them.
221 needs_update()
223 recurse_deps branch_needs_update "$@"
226 # branch_empty NAME
227 branch_empty()
229 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v " .top")" ]
232 # switch_to_base NAME [SEED]
233 switch_to_base()
235 _base="refs/top-bases/$1"; _seed="$2"
236 # We have to do all the hard work ourselves :/
237 # This is like git checkout -b "$_base" "$_seed"
238 # (or just git checkout "$_base"),
239 # but does not create a detached HEAD.
240 git read-tree -u -m HEAD "${_seed:-$_base}"
241 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
242 git symbolic-ref HEAD "$_base"
245 # Show the help messages.
246 do_help()
248 if [ -z "$1" ] ; then
249 # This is currently invoked in all kinds of circumstances,
250 # including when the user made a usage error. Should we end up
251 # providing more than a short help message, then we should
252 # differentiate.
253 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
255 ## Build available commands list for help output
257 cmds=
258 sep=
259 for cmd in "@cmddir@"/tg-*; do
260 ! [ -r "$cmd" ] && continue
261 # strip directory part and "tg-" prefix
262 cmd="$(basename "$cmd")"
263 cmd="${cmd#tg-}"
264 cmds="$cmds$sep$cmd"
265 sep="|"
266 done
268 echo "TopGit v$TG_VERSION - A different patch queue manager"
269 echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
270 elif [ -r "@cmddir@"/tg-$1 ] ; then
271 @cmddir@/tg-$1 -h || :
272 echo
273 if [ -r "@sharedir@/tg-$1.txt" ] ; then
274 cat "@sharedir@/tg-$1.txt"
276 else
277 echo "`basename $0`: no help for $1" 1>&2
278 do_help
279 exit 1
283 ## Pager stuff
285 # isatty FD
286 isatty()
288 test -t $1
291 # setup_pager
292 # Spawn pager process and redirect the rest of our output to it
293 setup_pager()
295 isatty 1 || return 0
297 # TG_PAGER = GIT_PAGER | PAGER | less
298 # NOTE: GIT_PAGER='' is significant
299 TG_PAGER=${GIT_PAGER-${PAGER-less}}
301 [ -z "$TG_PAGER" -o "$TG_PAGER" = "cat" ] && return 0
304 # now spawn pager
305 export LESS=${LESS:-FRSX} # as in pager.c:pager_preexec()
307 _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
308 _pager_fifo="$_pager_fifo_dir/0"
309 mkfifo -m 600 "$_pager_fifo"
311 "$TG_PAGER" < "$_pager_fifo" &
312 exec > "$_pager_fifo" # dup2(pager_fifo.in, 1)
314 # this is needed so e.g. `git diff` will still colorize it's output if
315 # requested in ~/.gitconfig with color.diff=auto
316 export GIT_PAGER_IN_USE=1
318 # atexit(close(1); wait pager)
319 trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
322 ## Startup
324 [ -d "@cmddir@" ] ||
325 die "No command directory: '@cmddir@'"
327 ## Initial setup
329 set -e
330 git_dir="$(git rev-parse --git-dir)"
331 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
332 # Make sure root_dir doesn't end with a trailing slash.
333 root_dir="${root_dir%/}"
334 base_remote="$(git config topgit.remote 2>/dev/null)" || :
335 tg="tg"
336 # make sure merging the .top* files will always behave sanely
337 setup_ours
338 setup_hook "pre-commit"
340 ## Dispatch
342 # We were sourced from another script for our utility functions;
343 # this is set by hooks.
344 [ -z "$tg__include" ] || return 0
346 if [ "$1" = "-r" ]; then
347 shift
348 if [ -z "$1" ]; then
349 echo "Option -r requires an argument." >&2
350 do_help
351 exit 1
353 base_remote="$1"; shift
354 tg="$tg -r $base_remote"
357 cmd="$1"
358 [ -n "$cmd" ] || { do_help; exit 1; }
359 shift
361 case "$cmd" in
362 help|--help|-h)
363 do_help "$1"
364 exit 0;;
365 --hooks-path)
366 # Internal command
367 echo "@hooksdir@";;
369 [ -r "@cmddir@"/tg-$cmd ] || {
370 echo "Unknown subcommand: $cmd" >&2
371 do_help
372 exit 1
374 . "@cmddir@"/tg-$cmd;;
375 esac
377 # vim:noet