[TOPGIT] limit rev-list in branch_contains to a single rev
[topgit.git] / tg-update.sh
blob288ec14f849495734fec17b839abf678f366ad78
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
9 ## Parse options
11 if [ -n "$1" ]; then
12 echo "Usage: tg [...] update" >&2
13 exit 1
17 name="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
18 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
19 die "not a TopGit-controlled branch"
22 ## First, take care of our base
24 depcheck="$(mktemp -t tg-depcheck.XXXXXX)"
25 missing_deps=
26 needs_update "$name" >"$depcheck" || :
27 [ -z "$missing_deps" ] || die "some dependencies are missing: $missing_deps"
28 if [ -s "$depcheck" ]; then
29 # We need to switch to the base branch
30 # ...but only if we aren't there yet (from failed previous merge)
31 HEAD="$(git symbolic-ref HEAD)"
32 if [ "$HEAD" = "${HEAD#refs/top-bases/}" ]; then
33 switch_to_base "$name"
36 cat "$depcheck" |
37 sed 's/ [^ ]* *$//' | # last is $name
38 sed 's/.* \([^ ]*\)$/+\1/' | # only immediate dependencies
39 sed 's/^\([^+]\)/-\1/' | # now each line is +branch or -branch (+ == recurse)
40 uniq -s 1 | # fold branch lines; + always comes before - and thus wins within uniq
41 while read depline; do
42 action="$(echo "$depline" | cut -c 1)"
43 dep="$(echo "$depline" | cut -c 2-)"
45 # We do not distinguish between dependencies out-of-date
46 # and base/remote out-of-date cases for $dep here,
47 # but thanks to needs_update returning : or %
48 # for the latter, we do correctly recurse here
49 # in both cases.
51 if [ x"$action" = x+ ]; then
52 info "Recursing to $dep..."
53 git checkout -q "$dep"
55 export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
56 export PS1="[$dep] $PS1"
57 while ! $tg update; do
58 # The merge got stuck! Let the user fix it up.
59 info "You are in a subshell. If you abort the merge,"
60 info "use \`exit 1\` to abort the recursive update altogether."
61 if ! sh -i </dev/tty; then
62 info "Ok, you aborated the merge. Now, you just need to"
63 info "switch back to some sane branch using \`git checkout\`."
64 exit 3
66 done
68 switch_to_base "$name"
71 # This will be either a proper topic branch
72 # or a remote base. (branch_needs_update() is called
73 # only on the _dependencies_, not our branch itself!)
75 info "Updating base with $dep changes..."
76 if ! git merge "$dep"; then
77 if [ -z "$TG_RECURSIVE" ]; then
78 resume="\`git checkout $name && $tg update\` again"
79 else # subshell
80 resume='exit'
82 info "Please commit merge resolution and call $resume."
83 info "It is also safe to abort this operation using \`git reset --hard\`,"
84 info "but please remember that you are on the base branch now;"
85 info "you will want to switch to some normal branch afterwards."
86 rm "$depcheck"
87 exit 2
89 done
90 else
91 info "The base is up-to-date."
93 rm "$depcheck"
95 # Home, sweet home...
96 # (We want to always switch back, in case we were on the base from failed
97 # previous merge.)
98 git checkout -q "$name"
100 merge_with="refs/top-bases/$name"
103 ## Second, update our head with the remote branch
105 if has_remote "$name"; then
106 rname="refs/remotes/$base_remote/$name"
107 if branch_contains "$name" "$rname"; then
108 info "The $name head is up-to-date wrt. its remote branch."
109 else
110 info "Reconciling remote branch updates with $name base..."
111 # *DETACH* our HEAD now!
112 git checkout -q "refs/top-bases/$name"
113 if ! git merge "$rname"; then
114 info "Oops, you will need to help me out here a bit."
115 info "Please commit merge resolution and call:"
116 info "git checkout $name && git merge <commitid>"
117 info "It is also safe to abort this operation using: git reset --hard $name"
118 exit 3
120 # Go back but remember we want to merge with this, not base
121 merge_with="$(git rev-parse HEAD)"
122 git checkout -q "$name"
127 ## Third, update our head with the base
129 if branch_contains "$name" "$merge_with"; then
130 info "The $name head is up-to-date wrt. the base."
131 exit 0
133 info "Updating $name against new base..."
134 if ! git merge "$merge_with"; then
135 if [ -z "$TG_RECURSIVE" ]; then
136 info "Please commit merge resolution. No need to do anything else"
137 info "You can abort this operation using \`git reset --hard\` now"
138 info "and retry this merge later using \`$tg update\`."
139 else # subshell
140 info "Please commit merge resolution and call exit."
141 info "You can abort this operation using \`git reset --hard\`."
143 exit 3
146 # vim:noet