chiark / gitweb /
tg update: Support updating from remote branches
[topgit.git] / tg.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6
7 ## Auxiliary functions
8
9 info()
10 {
11         echo "${TG_RECURSIVE}tg: $*"
12 }
13
14 die()
15 {
16         info "fatal: $*"
17         exit 1
18 }
19
20 # setup_hook NAME
21 setup_hook()
22 {
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
28         fi
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"
34         fi
35         # Insert call into the hook
36         {
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"
43 }
44
45 # setup_ours (no arguments)
46 setup_ours()
47 {
48         if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
49                 {
50                         echo ".topmsg   merge=ours"
51                         echo ".topdeps  merge=ours"
52                 } >>"$git_dir/info/attributes"
53         fi
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'
57         fi
58 }
59
60 # measure_branch NAME [BASE]
61 measure_branch()
62 {
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"
72         fi
73         echo "$_commits/$_nmcommits $_suffix"
74 }
75
76 # branch_contains B1 B2
77 # Whether B1 is a superset of B2.
78 branch_contains()
79 {
80         [ -z "$(git rev-list ^"$1" "$2")" ]
81 }
82
83 # ref_exists REF
84 # Whether REF is a valid ref name
85 ref_exists()
86 {
87         git rev-parse --verify "$@" >/dev/null 2>&1
88 }
89
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()
101 {
102         _cmd="$1"; shift
103         _name="$1"; # no shift
104         _depchain="$*"
105
106         _depsfile="$(mktemp -t tg-depsfile.XXXXXX)"
107         # Check also our base against remote base. Checking our head
108         # against remote head has to be done in the helper.
109         _remotebase="refs/remotes/$base_remote/top-bases/$_name"
110         if [ -n "$base_remote" ] && ref_exists "$_remotebase"; then
111                 echo "$_remotebase" >>"$_depsfile"
112         fi
113         git cat-file blob "$_name:.topdeps" >>"$_depsfile"
114
115         _ret=0
116         while read _dep; do
117                 if ! ref_exists "$_dep" ; then
118                         # All hope is lost
119                         missing_deps="$missing_deps $_dep"
120                         continue
121                 fi
122
123                 _dep_is_tgish=1
124                 ref_exists "refs/top-bases/$_dep"  ||
125                         _dep_is_tgish=
126
127                 # Shoo shoo, keep our environment alone!
128                 [ -z "$_dep_is_tgish" ] ||
129                         (recurse_deps "$_cmd" "$_dep" "$@") ||
130                         _ret=$?
131
132                 eval "$_cmd"
133         done <"$_depsfile"
134         missing_deps="${missing_deps# }"
135         rm "$_depsfile"
136         return $_ret
137 }
138
139 # branch_needs_update
140 # This is a helper function for determining whether given branch
141 # is up-to-date wrt. its dependencies. It expects input as if it
142 # is called as a recurse_deps() helper.
143 # In case the branch does need update, it will echo it together
144 # with the branch backtrace on the output (see needs_update()
145 # description for details) and set $_ret to non-zero.
146 branch_needs_update()
147 {
148         _dep_base_update=
149         if [ -n "$_dep_is_tgish" ]; then
150                 if [ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$_dep"; then
151                         branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
152                 fi
153                 # This can possibly override the remote check result;
154                 # we want to sync with our base first
155                 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
156         fi
157
158         if [ -n "$_dep_base_update" ]; then
159                 # _dep needs to be synced with its base/remote
160                 echo "$_dep_base_update $_dep $_depchain"
161                 _ret=1
162         elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
163                 # Some new commits in _dep
164                 echo "$_dep $_depchain"
165                 _ret=1
166         fi
167 }
168
169 # needs_update NAME
170 # This function is recursive; it outputs reverse path from NAME
171 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
172 # inner paths first. Innermost name can be ':' if the head is
173 # not in sync with the base or '%' if the head is not in sync
174 # with the remote (in this order of priority).
175 # It will also return non-zero status if NAME needs update.
176 # If needs_update() hits missing dependencies, it will append
177 # them to space-separated $missing_deps list and skip them.
178 needs_update()
179 {
180         recurse_deps branch_needs_update "$@"
181 }
182
183 # branch_empty NAME
184 branch_empty()
185 {
186         [ -z "$(git diff-tree "refs/top-bases/$1" "$1" | fgrep -v "     .top")" ]
187 }
188
189 # switch_to_base NAME [SEED]
190 switch_to_base()
191 {
192         _base="refs/top-bases/$1"; _seed="$2"
193         # We have to do all the hard work ourselves :/
194         # This is like git checkout -b "$_base" "$_seed"
195         # (or just git checkout "$_base"),
196         # but does not create a detached HEAD.
197         git read-tree -u -m HEAD "${_seed:-$_base}"
198         [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
199         git symbolic-ref HEAD "$_base"
200 }
201
202 # Show the help messages.
203 do_help()
204 {
205         if [ -z "$1" ] ; then
206                 ## Build available commands list for help output
207
208                 cmds=
209                 sep=
210                 for cmd in "@cmddir@"/tg-*; do
211                         ! [ -r "$cmd" ] && continue
212                         # strip directory part and "tg-" prefix
213                         cmd="$(basename "$cmd")"
214                         cmd="${cmd#tg-}"
215                         cmds="$cmds$sep$cmd"
216                         sep="|"
217                 done
218
219                 echo "TopGit v0.2 - A different patch queue manager"
220                 echo "Usage: tg ($cmds|help) ..."
221         elif [ -r "@sharedir@/tg-$1.txt" ] ; then
222                 cat "@sharedir@/tg-$1.txt"
223         else
224                 echo "`basename $0`: no help for $1" 1>&2
225         fi
226 }
227
228
229 ## Initial setup
230
231 set -e
232 git_dir="$(git rev-parse --git-dir)"
233 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
234 base_remote="$(git config topgit.remote 2>/dev/null)" || :
235 # make sure merging the .top* files will always behave sanely
236 setup_ours
237 setup_hook "pre-commit"
238
239 [ -d "@cmddir@" ] ||
240         die "No command directory: '@cmddir@'"
241
242 ## Dispatch
243
244 # We were sourced from another script for our utility functions;
245 # this is set by hooks.
246 [ -z "$tg__include" ] || return 0
247
248 cmd="$1"
249 [ -n "$cmd" ] || die "He took a duck in the face at two hundred and fifty knots"
250 shift
251
252 case "$cmd" in
253 help|--help|-h)
254         do_help "$1"
255         exit 1;;
256 --hooks-path)
257         # Internal command
258         echo "@hooksdir@";;
259 *)
260         [ -r "@cmddir@"/tg-$cmd ] || {
261                 echo "Unknown subcommand: $cmd" >&2
262                 exit 1
263         }
264         . "@cmddir@"/tg-$cmd;;
265 esac