TopGit-0.4
[topgit.git] / tg.sh
blob7005f98c882bde7c97094862aeb88b07b28de390
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
7 ## Auxiliary functions
9 info()
11 echo "${TG_RECURSIVE}tg: $*"
14 die()
16 info "fatal: $*"
17 exit 1
20 # setup_hook NAME
21 setup_hook()
23 hook_call="\"\$($tg --hooks-path)\"/$1 \"\$@\""
24 if [ -f "$git_dir/hooks/$1" ] &&
25 fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
26 # Another job well done!
27 return
29 # Prepare incantation
30 if [ -x "$git_dir/hooks/$1" ]; then
31 hook_call="$hook_call"' || exit $?'
32 else
33 hook_call="exec $hook_call"
35 # Insert call into the hook
37 echo "#!/bin/sh"
38 echo "$hook_call"
39 [ ! -s "$git_dir/hooks/$1" ] || cat "$git_dir/hooks/$1"
40 } >"$git_dir/hooks/$1+"
41 chmod a+x "$git_dir/hooks/$1+"
42 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
45 # setup_ours (no arguments)
46 setup_ours()
48 if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
50 echo ".topmsg merge=ours"
51 echo ".topdeps merge=ours"
52 } >>"$git_dir/info/attributes"
54 if ! git config merge.ours.driver >/dev/null; then
55 git config merge.ours.name '"always keep ours" merge driver'
56 git config merge.ours.driver 'touch %A'
60 # measure_branch NAME [BASE]
61 measure_branch()
63 _bname="$1"; _base="$2"
64 [ -n "$_base" ] || _base="refs/top-bases/$_bname"
65 # The caller should've verified $name is valid
66 _commits="$(git rev-list "$_bname" ^"$_base" | wc -l)"
67 _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" | wc -l)"
68 if [ $_commits -gt 1 ]; then
69 _suffix="commits"
70 else
71 _suffix="commit"
73 echo "$_commits/$_nmcommits $_suffix"
76 # branch_contains B1 B2
77 # Whether B1 is a superset of B2.
78 branch_contains()
80 [ -z "$(git rev-list ^"$1" "$2" --)" ]
83 # ref_exists REF
84 # Whether REF is a valid ref name
85 ref_exists()
87 git rev-parse --verify "$@" >/dev/null 2>&1
90 # has_remote BRANCH
91 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
92 has_remote()
94 [ -n "$base_remote" ] && ref_exists "remotes/$base_remote/$1"
97 # recurse_deps CMD NAME [BRANCHPATH...]
98 # Recursively eval CMD on all dependencies of NAME.
99 # CMD can refer to $_name for queried branch name,
100 # $_dep for dependency name,
101 # $_depchain for space-seperated branch backtrace,
102 # and the $_dep_is_tgish boolean.
103 # It can modify $_ret to affect the return value
104 # of the whole function.
105 # If recurse_deps() hits missing dependencies, it will append
106 # them to space-separated $missing_deps list and skip them.
107 recurse_deps()
109 _cmd="$1"; shift
110 _name="$1"; # no shift
111 _depchain="$*"
113 _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
114 # Check also our base against remote base. Checking our head
115 # against remote head has to be done in the helper.
116 if has_remote "top-bases/$_name"; then
117 echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
119 git cat-file blob "$_name:.topdeps" >>"$_depsfile"
121 _ret=0
122 while read _dep; do
123 if ! ref_exists "$_dep" ; then
124 # All hope is lost
125 missing_deps="$missing_deps $_dep"
126 continue
129 _dep_is_tgish=1
130 ref_exists "refs/top-bases/$_dep" ||
131 _dep_is_tgish=
133 # Shoo shoo, keep our environment alone!
134 [ -z "$_dep_is_tgish" ] ||
135 (recurse_deps "$_cmd" "$_dep" "$@") ||
136 _ret=$?
138 eval "$_cmd"
139 done <"$_depsfile"
140 missing_deps="${missing_deps# }"
141 rm "$_depsfile"
142 return $_ret
145 # branch_needs_update
146 # This is a helper function for determining whether given branch
147 # is up-to-date wrt. its dependencies. It expects input as if it
148 # is called as a recurse_deps() helper.
149 # In case the branch does need update, it will echo it together
150 # with the branch backtrace on the output (see needs_update()
151 # description for details) and set $_ret to non-zero.
152 branch_needs_update()
154 _dep_base_update=
155 if [ -n "$_dep_is_tgish" ]; then
156 if has_remote "$_dep"; then
157 branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
159 # This can possibly override the remote check result;
160 # we want to sync with our base first
161 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
164 if [ -n "$_dep_base_update" ]; then
165 # _dep needs to be synced with its base/remote
166 echo "$_dep_base_update $_dep $_depchain"
167 _ret=1
168 elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
169 # Some new commits in _dep
170 echo "$_dep $_depchain"
171 _ret=1
175 # needs_update NAME
176 # This function is recursive; it outputs reverse path from NAME
177 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
178 # inner paths first. Innermost name can be ':' if the head is
179 # not in sync with the base or '%' if the head is not in sync
180 # with the remote (in this order of priority).
181 # It will also return non-zero status if NAME needs update.
182 # If needs_update() hits missing dependencies, it will append
183 # them to space-separated $missing_deps list and skip them.
184 needs_update()
186 recurse_deps branch_needs_update "$@"
189 # branch_empty NAME
190 branch_empty()
192 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v " .top")" ]
195 # switch_to_base NAME [SEED]
196 switch_to_base()
198 _base="refs/top-bases/$1"; _seed="$2"
199 # We have to do all the hard work ourselves :/
200 # This is like git checkout -b "$_base" "$_seed"
201 # (or just git checkout "$_base"),
202 # but does not create a detached HEAD.
203 git read-tree -u -m HEAD "${_seed:-$_base}"
204 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
205 git symbolic-ref HEAD "$_base"
208 # Show the help messages.
209 do_help()
211 if [ -z "$1" ] ; then
212 ## Build available commands list for help output
214 cmds=
215 sep=
216 for cmd in "@cmddir@"/tg-*; do
217 ! [ -r "$cmd" ] && continue
218 # strip directory part and "tg-" prefix
219 cmd="$(basename "$cmd")"
220 cmd="${cmd#tg-}"
221 cmds="$cmds$sep$cmd"
222 sep="|"
223 done
225 echo "TopGit v0.4 - A different patch queue manager"
226 echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
227 elif [ -r "@cmddir@"/tg-$1 ] ; then
228 @cmddir@/tg-$1 -h || :
229 echo
230 if [ -r "@sharedir@/tg-$1.txt" ] ; then
231 cat "@sharedir@/tg-$1.txt"
233 else
234 echo "`basename $0`: no help for $1" 1>&2
239 ## Initial setup
241 set -e
242 git_dir="$(git rev-parse --git-dir)"
243 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
244 base_remote="$(git config topgit.remote 2>/dev/null)" || :
245 tg="tg"
246 # make sure merging the .top* files will always behave sanely
247 setup_ours
248 setup_hook "pre-commit"
250 [ -d "@cmddir@" ] ||
251 die "No command directory: '@cmddir@'"
253 ## Dispatch
255 # We were sourced from another script for our utility functions;
256 # this is set by hooks.
257 [ -z "$tg__include" ] || return 0
259 if [ "$1" = "-r" ]; then
260 shift; base_remote="$1"; shift
261 tg="$tg -r $base_remote"
264 cmd="$1"
265 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
266 shift
268 case "$cmd" in
269 help|--help|-h)
270 do_help "$1"
271 exit 1;;
272 --hooks-path)
273 # Internal command
274 echo "@hooksdir@";;
276 [ -r "@cmddir@"/tg-$cmd ] || {
277 echo "Unknown subcommand: $cmd" >&2
278 exit 1
280 . "@cmddir@"/tg-$cmd;;
281 esac