tg.sh: Set $base_remote to topgit.remote config value
[topgit.git] / tg.sh
blobc252a87bbb3c0efd1f01d22d38d2e3418164d958
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 incanation
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 # recurse_deps CMD NAME [BRANCHPATH...]
91 # Recursively eval CMD on all dependencies of NAME.
92 # CMD can refer to $_name for queried branch name,
93 # $_dep for dependency name,
94 # $_depchain for space-seperated branch backtrace,
95 # and the $_dep_is_tgish boolean.
96 # It can modify $_ret to affect the return value
97 # of the whole function.
98 # If recurse_deps() hits missing dependencies, it will append
99 # them to space-separated $missing_deps list and skip them.
100 recurse_deps()
102 _cmd="$1"; shift
103 _name="$1"; # no shift
104 _depchain="$*"
105 _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
106 git cat-file blob "$_name:.topdeps" >"$_depsfile"
107 _ret=0
108 while read _dep; do
109 if ! ref_exists "$_dep" ; then
110 # All hope is lost
111 missing_deps="$missing_deps $_dep"
112 continue
115 _dep_is_tgish=1
116 ref_exists "refs/top-bases/$_dep" ||
117 _dep_is_tgish=
119 # Shoo shoo, keep our environment alone!
120 [ -z "$_dep_is_tgish" ] ||
121 (recurse_deps "$_cmd" "$_dep" "$@") ||
122 _ret=$?
124 eval "$_cmd"
125 done <"$_depsfile"
126 missing_deps="${missing_deps# }"
127 rm "$_depsfile"
128 return $_ret
131 # branch_needs_update
132 # This is a helper function for determining whether given branch
133 # is up-to-date wrt. its dependencies. It expects input as if it
134 # is called as a recurse_deps() helper.
135 # In case the branch does need update, it will echo it together
136 # with the branch backtrace on the output (see needs_update()
137 # description for details) and set $_ret to non-zero.
138 branch_needs_update()
140 _dep_base_uptodate=1
141 if [ -n "$_dep_is_tgish" ]; then
142 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
145 if [ -z "$_dep_base_uptodate" ]; then
146 # _dep needs to be synced with its base
147 echo ": $_dep $_depchain"
148 _ret=1
149 elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
150 # Some new commits in _dep
151 echo "$_dep $_depchain"
152 _ret=1
156 # needs_update NAME
157 # This function is recursive; it outputs reverse path from NAME
158 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
159 # inner paths first. Innermost name can be ':' if the head is
160 # not in sync with the base.
161 # It will also return non-zero status if NAME needs update.
162 # If needs_update() hits missing dependencies, it will append
163 # them to space-separated $missing_deps list and skip them.
164 needs_update()
166 recurse_deps branch_needs_update "$@"
169 # branch_empty NAME
170 branch_empty()
172 [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v " .top")" ]
175 # switch_to_base NAME [SEED]
176 switch_to_base()
178 _base="refs/top-bases/$1"; _seed="$2"
179 # We have to do all the hard work ourselves :/
180 # This is like git checkout -b "$_base" "$_seed"
181 # (or just git checkout "$_base"),
182 # but does not create a detached HEAD.
183 git read-tree -u -m HEAD "${_seed:-$_base}"
184 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
185 git symbolic-ref HEAD "$_base"
188 # Show the help messages.
189 do_help()
191 if [ -z "$1" ] ; then
192 ## Build available commands list for help output
194 cmds=
195 sep=
196 for cmd in "@cmddir@"/tg-*; do
197 ! [ -r "$cmd" ] && continue
198 # strip directory part and "tg-" prefix
199 cmd="$(basename "$cmd")"
200 cmd="${cmd#tg-}"
201 cmds="$cmds$sep$cmd"
202 sep="|"
203 done
205 echo "TopGit v0.2 - A different patch queue manager"
206 echo "Usage: tg ($cmds|help) ..."
207 elif [ -r "@sharedir@/tg-$1.txt" ] ; then
208 cat "@sharedir@/tg-$1.txt"
209 else
210 echo "`basename $0`: no help for $1" 1>&2
215 ## Initial setup
217 set -e
218 git_dir="$(git rev-parse --git-dir)"
219 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
220 base_remote="$(git config topgit.remote 2>/dev/null)" || :
221 # make sure merging the .top* files will always behave sanely
222 setup_ours
223 setup_hook "pre-commit"
225 [ -d "@cmddir@" ] ||
226 die "No command directory: '@cmddir@'"
228 ## Dispatch
230 # We were sourced from another script for our utility functions;
231 # this is set by hooks.
232 [ -z "$tg__include" ] || return 0
234 cmd="$1"
235 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
236 shift
238 case "$cmd" in
239 help|--help|-h)
240 do_help "$1"
241 exit 1;;
242 --hooks-path)
243 # Internal command
244 echo "@hooksdir@";;
246 [ -r "@cmddir@"/tg-$cmd ] || {
247 echo "Unknown subcommand: $cmd" >&2
248 exit 1
250 . "@cmddir@"/tg-$cmd;;
251 esac