2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
11 echo "${TG_RECURSIVE}tg: $*"
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!
30 if [ -x "$git_dir/hooks/$1" ]; then
31 hook_call
="$hook_call"' || exit $?'
33 hook_call
="exec $hook_call"
35 # Insert call into the hook
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)
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]
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
73 echo "$_commits/$_nmcommits $_suffix"
76 # branch_contains B1 B2
77 # Whether B1 is a superset of B2.
80 [ -z "$(git rev-list --max-count=1 ^"$1" "$2" --)" ]
84 # Whether REF is a valid ref name
87 git rev-parse
--verify "$@" >/dev
/null
2>&1
91 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
94 [ -n "$base_remote" ] && ref_exists
"remotes/$base_remote/$1"
101 # use the merge base in case the base is ahead.
102 mb
="$(git merge-base "refs
/top-bases
/$_name" "$_name")";
104 test "$(git rev-parse "$mb^
{tree
}")" = "$(git rev-parse "$_name^
{tree
}")";
107 # recurse_deps CMD NAME [BRANCHPATH...]
108 # Recursively eval CMD on all dependencies of NAME.
109 # CMD can refer to $_name for queried branch name,
110 # $_dep for dependency name,
111 # $_depchain for space-seperated branch backtrace,
112 # and the $_dep_is_tgish boolean.
113 # It can modify $_ret to affect the return value
114 # of the whole function.
115 # If recurse_deps() hits missing dependencies, it will append
116 # them to space-separated $missing_deps list and skip them.
120 _name
="$1"; # no shift
123 _depsfile
="$(mktemp -t tg-depsfile.XXXXXX)"
124 # Check also our base against remote base. Checking our head
125 # against remote head has to be done in the helper.
126 if has_remote
"top-bases/$_name"; then
127 echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
130 # if the branch was annihilated, there exists no .topdeps file
131 if ! branch_annihilated
"$_name"; then
132 #TODO: handle nonexisting .topdeps?
133 git cat-file blob
"$_name:.topdeps" >>"$_depsfile";
138 if ! ref_exists
"$_dep" ; then
140 missing_deps
="$missing_deps $_dep"
145 ref_exists
"refs/top-bases/$_dep" ||
148 # Shoo shoo, keep our environment alone!
149 [ -z "$_dep_is_tgish" ] ||
150 (recurse_deps
"$_cmd" "$_dep" "$@") ||
155 missing_deps
="${missing_deps# }"
160 # branch_needs_update
161 # This is a helper function for determining whether given branch
162 # is up-to-date wrt. its dependencies. It expects input as if it
163 # is called as a recurse_deps() helper.
164 # In case the branch does need update, it will echo it together
165 # with the branch backtrace on the output (see needs_update()
166 # description for details) and set $_ret to non-zero.
167 branch_needs_update
()
170 if [ -n "$_dep_is_tgish" ]; then
171 if has_remote
"$_dep"; then
172 branch_contains
"$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update
=%
174 # This can possibly override the remote check result;
175 # we want to sync with our base first
176 branch_contains
"$_dep" "refs/top-bases/$_dep" || _dep_base_update
=:
179 if [ -n "$_dep_base_update" ]; then
180 # _dep needs to be synced with its base/remote
181 echo "$_dep_base_update $_dep $_depchain"
183 elif [ -n "$_name" ] && ! branch_contains
"refs/top-bases/$_name" "$_dep"; then
184 # Some new commits in _dep
185 echo "$_dep $_depchain"
191 # This function is recursive; it outputs reverse path from NAME
192 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
193 # inner paths first. Innermost name can be ':' if the head is
194 # not in sync with the base or '%' if the head is not in sync
195 # with the remote (in this order of priority).
196 # It will also return non-zero status if NAME needs update.
197 # If needs_update() hits missing dependencies, it will append
198 # them to space-separated $missing_deps list and skip them.
201 recurse_deps branch_needs_update
"$@"
207 [ -z "$(git diff-tree "refs
/top-bases
/$1" "$1" | fgrep -v " .top
")" ]
210 # switch_to_base NAME [SEED]
213 _base
="refs/top-bases/$1"; _seed
="$2"
214 # We have to do all the hard work ourselves :/
215 # This is like git checkout -b "$_base" "$_seed"
216 # (or just git checkout "$_base"),
217 # but does not create a detached HEAD.
218 git read-tree
-u -m HEAD
"${_seed:-$_base}"
219 [ -z "$_seed" ] || git update-ref
"$_base" "$_seed"
220 git symbolic-ref HEAD
"$_base"
223 # Show the help messages.
226 if [ -z "$1" ] ; then
227 # This is currently invoked in all kinds of circumstances,
228 # including when the user made a usage error. Should we end up
229 # providing more than a short help message, then we should
231 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
233 ## Build available commands list for help output
237 for cmd
in "@cmddir@"/tg-
*; do
238 ! [ -r "$cmd" ] && continue
239 # strip directory part and "tg-" prefix
240 cmd
="$(basename "$cmd")"
246 echo "TopGit v0.5 - A different patch queue manager"
247 echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
248 elif [ -r "@cmddir@"/tg-
$1 ] ; then
249 @cmddir@
/tg-
$1 -h ||
:
251 if [ -r "@sharedir@/tg-$1.txt" ] ; then
252 cat "@sharedir@/tg-$1.txt"
255 echo "`basename $0`: no help for $1" 1>&2
265 die
"No command directory: '@cmddir@'"
270 git_dir
="$(git rev-parse --git-dir)"
271 root_dir
="$(git rev-parse --show-cdup)"; root_dir
="${root_dir:-.}"
272 # Make sure root_dir doesn't end with a trailing slash.
273 root_dir
="${root_dir%/}"
274 base_remote
="$(git config topgit.remote 2>/dev/null)" ||
:
276 # make sure merging the .top* files will always behave sanely
278 setup_hook
"pre-commit"
282 # We were sourced from another script for our utility functions;
283 # this is set by hooks.
284 [ -z "$tg__include" ] ||
return 0
286 if [ "$1" = "-r" ]; then
289 echo "Option -r requires an argument." >&2
293 base_remote
="$1"; shift
294 tg
="$tg -r $base_remote"
298 [ -n "$cmd" ] ||
{ do_help
; exit 1; }
309 [ -r "@cmddir@"/tg-
$cmd ] ||
{
310 echo "Unknown subcommand: $cmd" >&2
314 .
"@cmddir@"/tg-
$cmd;;