README: really update documentation
[topgit.git] / tg-update.sh
blob0a5193dcead2c808a85e42d2882a466fcad5c5eb
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 all=
8 pattern=
9 current=
12 ## Parse options
14 while [ -n "$1" ]; do
15 arg="$1"; shift
16 case "$arg" in
17 -a)
18 all=1;;
19 -*)
20 echo "Usage: tg [...] update [-a] [NAME|PATTERN ...]" >&2
21 exit 1;;
23 if [ -z "$all" ]; then
24 [ -z "$name" ] || die "name already specified ($name)"
25 name="$arg"
26 else
27 pattern="$pattern refs/top-bases/${arg#refs/top-bases/}"
30 esac
31 done
32 [ -z "$pattern" ] && pattern=refs/top-bases
34 current="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
35 if [ -z "$all" -a -z "$name" ]; then
36 name="$current"
37 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
38 die "not a TopGit-controlled branch"
41 update_branch() {
42 local name="$1" base_rev depcheck missing_deps HEAD
43 ## First, take care of our base
45 depcheck="$(get_temp tg-depcheck)"
46 missing_deps=
47 needs_update "$name" >"$depcheck" || :
48 [ -z "$missing_deps" ] || { info "some dependencies are missing: $missing_deps; skpping"; return; }
49 if [ -s "$depcheck" ]; then
50 # We need to switch to the base branch
51 # ...but only if we aren't there yet (from failed previous merge)
52 HEAD="$(git symbolic-ref HEAD)"
53 if [ "$HEAD" = "${HEAD#refs/top-bases/}" ]; then
54 switch_to_base "$name"
57 cat "$depcheck" |
58 sed 's/ [^ ]* *$//' | # last is $name
59 sed 's/.* \([^ ]*\)$/+\1/' | # only immediate dependencies
60 sed 's/^\([^+]\)/-\1/' | # now each line is +branch or -branch (+ == recurse)
61 uniq -s 1 | # fold branch lines; + always comes before - and thus wins within uniq
62 while read depline; do
63 action="$(echo "$depline" | cut -c 1)"
64 dep="$(echo "$depline" | cut -c 2-)"
66 # We do not distinguish between dependencies out-of-date
67 # and base/remote out-of-date cases for $dep here,
68 # but thanks to needs_update returning : or %
69 # for the latter, we do correctly recurse here
70 # in both cases.
72 if [ x"$action" = x+ ]; then
73 info "Recursing to $dep..."
74 git checkout -q "$dep"
76 export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
77 export PS1="[$dep] $PS1"
78 while ! $tg update; do
79 # The merge got stuck! Let the user fix it up.
80 info "You are in a subshell. If you abort the merge,"
81 info "use \`exit 1\` to abort the recursive update altogether."
82 if ! sh -i </dev/tty; then
83 info "Ok, you aborted the merge. Now, you just need to"
84 info "switch back to some sane branch using \`git checkout\`."
85 exit 3
87 done
89 switch_to_base "$name"
92 # This will be either a proper topic branch
93 # or a remote base. (branch_needs_update() is called
94 # only on the _dependencies_, not our branch itself!)
96 info "Updating base with $dep changes..."
97 if ! git merge "$dep"; then
98 if [ -z "$TG_RECURSIVE" ]; then
99 resume="\`git checkout $name && $tg update\` again"
100 else # subshell
101 resume='exit'
103 info "Please commit merge resolution and call $resume."
104 info "It is also safe to abort this operation using \`git reset --hard\`,"
105 info "but please remember that you are on the base branch now;"
106 info "you will want to switch to some normal branch afterwards."
107 rm "$depcheck"
108 exit 2
110 done
111 else
112 info "The base is up-to-date."
115 # Home, sweet home...
116 # (We want to always switch back, in case we were on the base from failed
117 # previous merge.)
118 git checkout -q "$name"
120 merge_with="refs/top-bases/$name"
123 ## Second, update our head with the remote branch
125 if has_remote "$name"; then
126 rname="refs/remotes/$base_remote/$name"
127 if branch_contains "$name" "$rname"; then
128 info "The $name head is up-to-date wrt. its remote branch."
129 else
130 info "Reconciling remote branch updates with $name base..."
131 # *DETACH* our HEAD now!
132 git checkout -q "refs/top-bases/$name"
133 if ! git merge "$rname"; then
134 info "Oops, you will need to help me out here a bit."
135 info "Please commit merge resolution and call:"
136 info "git checkout $name && git merge <commitid>"
137 info "It is also safe to abort this operation using: git reset --hard $name"
138 exit 3
140 # Go back but remember we want to merge with this, not base
141 merge_with="$(git rev-parse HEAD)"
142 git checkout -q "$name"
147 ## Third, update our head with the base
149 if branch_contains "$name" "$merge_with"; then
150 info "The $name head is up-to-date wrt. the base."
151 return 0
153 info "Updating $name against new base..."
154 if ! git merge "$merge_with"; then
155 if [ -z "$TG_RECURSIVE" ]; then
156 info "Please commit merge resolution. No need to do anything else"
157 info "You can abort this operation using \`git reset --hard\` now"
158 info "and retry this merge later using \`$tg update\`."
159 else # subshell
160 info "Please commit merge resolution and call exit."
161 info "You can abort this operation using \`git reset --hard\`."
163 exit 3
167 [ -z "$all" ] && { update_branch $name; exit; }
169 git for-each-ref $pattern |
170 while read rev type ref; do
171 name="${ref#refs/top-bases/}"
172 if branch_annihilated "$name"; then
173 continue;
175 info "Procesing $name..."
176 update_branch "$name" || exit
177 done
179 info "Returning to $current..."
180 git checkout -q "$current"
181 # vim:noet