TopGit - A different patch queue manager
[topgit.git] / tg.sh
blobae3dcda6446709183cf2f1032777f41655149a57
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 fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
25 # Another job well done!
26 return
28 # Prepare incanation
29 if [ -x "$git_dir/hooks/$1" ]; then
30 hook_call="$hook_call"' || exit $?'
31 else
32 hook_call="exec $hook_call"
34 # Insert call into the hook
36 echo "#!/bin/sh"
37 echo "$hook_call"
38 cat "$git_dir/hooks/$1"
39 } >"$git_dir/hooks/$1+"
40 chmod a+x "$git_dir/hooks/$1+"
41 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
44 # setup_ours (no arguments)
45 setup_ours()
47 if [ ! -s "$git_dir/info/gitattributes" ] || ! grep -q topmsg "$git_dir/info/gitattributes"; then
49 echo -e ".topmsg\tmerge=ours"
50 echo -e ".topdeps\tmerge=ours"
51 } >>"$git_dir/info/gitattributes"
53 if ! git config merge.ours.driver >/dev/null; then
54 git config merge.ours.name '"always keep ours" merge driver'
55 git config merge.ours.driver 'touch %A'
59 # measure_branch NAME [BASE]
60 measure_branch()
62 _name="$1"; _base="$2"
63 [ -n "$_base" ] || _base="refs/top-bases/$_name"
64 # The caller should've verified $name is valid
65 _commits="$(git rev-list "$_name" ^"$_base" | wc -l)"
66 _nmcommits="$(git rev-list --no-merges "$_name" ^"$_base" | wc -l)"
67 if [ $_commits -gt 1 ]; then
68 _suffix="commits"
69 else
70 _suffix="commit"
72 echo "$_commits/$_nmcommits $_suffix"
75 # branch_contains B1 B2
76 # Whether B1 is a superset of B2.
77 branch_contains()
79 [ "$(git rev-list ^"$1" "$2" | wc -l)" -eq 0 ]
82 # needs_update NAME [BRANCHPATH...]
83 # This function is recursive; it outputs reverse path from NAME
84 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
85 # inner paths first. Innermost name can be ':' if the head is
86 # not in sync with the base.
87 needs_update()
90 git cat-file blob "$1:.topdeps" 2>/dev/null |
91 while read _dep; do
92 _dep_is_tgish=1
93 git rev-parse --verify "refs/top-bases/$_dep" >/dev/null 2>&1 ||
94 _dep_is_tgish=
96 # Shoo shoo, keep our environment alone!
97 [ -z "$_dep_is_tgish" ] || (needs_update "$_dep" "$@")
99 _dep_base_uptodate=1
100 if [ -n "$_dep_is_tgish" ]; then
101 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_uptodate=
104 if [ -z "$_dep_base_uptodate" ]; then
105 # _dep needs to be synced with its base
106 echo ": $_dep $*"
107 elif ! branch_contains "refs/top-bases/$1" "$_dep"; then
108 # Some new commits in _dep
109 echo "$_dep $*"
111 done
112 } || : # $1 is not tracked by TopGit anymore
115 # switch_to_base NAME [SEED]
116 switch_to_base()
118 _base="refs/top-bases/$1"; _seed="$2"
119 # We have to do all the hard work ourselves :/
120 # This is like git checkout -b "$_base" "$_seed"
121 # (or just git checkout "$_base"),
122 # but does not create a detached HEAD.
123 git read-tree -u -m HEAD "${_seed:-$_base}"
124 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
125 git symbolic-ref HEAD "$_base"
129 ## Initial setup
131 set -e
132 git_dir="$(git rev-parse --git-dir)"
133 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
134 # make sure merging the .top* files will always behave sanely
135 setup_ours
136 setup_hook "pre-commit"
139 ## Dispatch
141 # We were sourced from another script for our utility functions;
142 # this is set by hooks.
143 [ -z "$tg__include" ] || return 0
145 cmd="$1"
146 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
147 shift
149 case "$cmd" in
150 help)
151 echo "TopGit - A different patch queue manager"
152 echo "Usage: tg (create|delete|info|patch|summary|update|help) ..."
153 exit 1;;
154 create|delete|info|patch|summary|update)
155 . "@cmddir@"/tg-$cmd;;
156 --hooks-path)
157 # Internal command
158 echo "@hooksdir@";;
160 echo "Unknown subcommand: $cmd" >&2
161 exit 1;;
162 esac