2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
6 name
= # Branch to update
7 all
= # Update all branches
8 pattern
= # Branch selection filter for -a
9 current
= # Branch we are currently on
20 echo "Usage: tg [...] update ([<name>] | -a [<pattern>...])" >&2
23 if [ -z "$all" ]; then
24 [ -z "$name" ] || die
"name already specified ($name)"
27 pattern
="$pattern refs/top-bases/${arg#refs/top-bases/}"
32 [ -z "$pattern" ] && pattern
=refs
/top-bases
34 current
="$(strip_ref "$
(git symbolic-ref HEAD
2>/dev
/null
)")"
35 if [ -z "$all" ]; then
36 if [ -z "$name" ]; then
38 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
39 die
"not a TopGit-controlled branch"
42 [ -n "$current" ] || die
"cannot return to detached tree; switch to another branch"
50 [ $_ret -eq 3 ] && exit 3
56 ## First, take care of our base
58 _depcheck
="$(get_temp tg-depcheck)"
60 needs_update
"$_update_name" >"$_depcheck" ||
:
61 if [ -n "$missing_deps" ]; then
62 if [ -z "$all" ]; then
63 die
"some dependencies are missing: $missing_deps"
65 info
"some dependencies are missing: $missing_deps; skipping"
69 if [ -s "$_depcheck" ]; then
70 # We need to switch to the base branch
71 # ...but only if we aren't there yet (from failed previous merge)
72 _HEAD
="$(git symbolic-ref HEAD)"
73 if [ "$_HEAD" = "${_HEAD#refs/top-bases/}" ]; then
74 switch_to_base
"$_update_name"
78 sed 's/ [^ ]* *$//' |
# last is $_update_name
79 sed 's/.* \([^ ]*\)$/+\1/' |
# only immediate dependencies
80 sed 's/^\([^+]\)/-\1/' |
# now each line is +branch or -branch (+ == recurse)
81 uniq -s 1 |
# fold branch lines; + always comes before - and thus wins within uniq
82 while read depline
; do
83 action
="$(echo "$depline" | cut -c 1)"
84 dep
="$(echo "$depline" | cut -c 2-)"
86 # We do not distinguish between dependencies out-of-date
87 # and base/remote out-of-date cases for $dep here,
88 # but thanks to needs_update returning : or %
89 # for the latter, we do correctly recurse here
92 if [ x
"$action" = x
+ ]; then
93 info
"Recursing to $dep..."
94 git checkout
-q "$dep"
96 export TG_RECURSIVE
="[$dep] $TG_RECURSIVE"
97 export PS1
="[$dep] $PS1"
98 while ! recursive_update
; do
99 # The merge got stuck! Let the user fix it up.
100 info
"You are in a subshell. If you abort the merge,"
101 info
"use \`exit 1\` to abort the recursive update altogether."
102 info
"Use \`exit 2\` to skip updating this branch and continue."
103 if sh
-i </dev
/tty
; then
104 # assume user fixed it
108 if [ $ret -eq 2 ]; then
109 info
"Ok, I will try to continue without updating this branch."
112 info
"Ok, you aborted the merge. Now, you just need to"
113 info
"switch back to some sane branch using \`git checkout\`."
119 switch_to_base
"$_update_name"
122 # This will be either a proper topic branch
123 # or a remote base. (branch_needs_update() is called
124 # only on the _dependencies_, not our branch itself!)
126 info
"Updating base with $dep changes..."
127 if ! git merge
"$dep"; then
128 if [ -z "$TG_RECURSIVE" ]; then
129 resume
="\`git checkout $_update_name && $tg update\` again"
133 info
"Please commit merge resolution and call $resume."
134 info
"It is also safe to abort this operation using \`git reset --hard\`,"
135 info
"but please remember that you are on the base branch now;"
136 info
"you will want to switch to some normal branch afterwards."
142 info
"The base is up-to-date."
145 # Home, sweet home...
146 # (We want to always switch back, in case we were on the base from failed
148 git checkout
-q "$_update_name"
150 merge_with
="refs/top-bases/$_update_name"
153 ## Second, update our head with the remote branch
155 if has_remote
"$_update_name"; then
156 _rname
="refs/remotes/$base_remote/$_update_name"
157 if branch_contains
"$_update_name" "$_rname"; then
158 info
"The $_update_name head is up-to-date wrt. its remote branch."
160 info
"Reconciling remote branch updates with $_update_name base..."
161 # *DETACH* our HEAD now!
162 git checkout
-q "refs/top-bases/$_update_name"
163 if ! git merge
"$_rname"; then
164 info
"Oops, you will need to help me out here a bit."
165 info
"Please commit merge resolution and call:"
166 info
"git checkout $_update_name && git merge <commitid>"
167 info
"It is also safe to abort this operation using: git reset --hard $_update_name"
170 # Go back but remember we want to merge with this, not base
171 merge_with
="$(git rev-parse HEAD)"
172 git checkout
-q "$_update_name"
177 ## Third, update our head with the base
179 if branch_contains
"$_update_name" "$merge_with"; then
180 info
"The $_update_name head is up-to-date wrt. the base."
183 info
"Updating $_update_name against new base..."
184 if ! git merge
"$merge_with"; then
185 if [ -z "$TG_RECURSIVE" ]; then
186 info
"Please commit merge resolution. No need to do anything else"
187 info
"You can abort this operation using \`git reset --hard\` now"
188 info
"and retry this merge later using \`$tg update\`."
190 info
"Please commit merge resolution and call exit."
191 info
"You can abort this operation using \`git reset --hard\`."
197 [ -z "$all" ] && { update_branch
$name; exit; }
199 non_annihilated_branches
$pattern |
201 info
"Proccessing $name..."
202 update_branch
"$name" ||
exit
205 info
"Returning to $current..."
206 git checkout
-q "$current"