2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
15 echo "Usage: tg [...] update [NAME]" >&2
18 [ -z "$name" ] || die "name already specified ($name)"
23 [ -n "$name" ] || name="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
24 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
25 die "not a TopGit-controlled branch"
28 ## First, take care of our base
30 depcheck="$(mktemp -t tg-depcheck.XXXXXX)"
32 needs_update "$name" >"$depcheck" || :
33 [ -z "$missing_deps" ] || die "some dependencies are missing: $missing_deps"
34 if [ -s "$depcheck" ]; then
35 # We need to switch to the base branch
36 # ...but only if we aren't there yet (from failed previous merge)
37 HEAD="$(git symbolic-ref HEAD)"
38 if [ "$HEAD" = "${HEAD#refs/top-bases/}" ]; then
39 switch_to_base "$name"
43 sed 's/ [^ ]* *$//' | # last is $name
44 sed 's/.* \([^ ]*\)$/+\1/' | # only immediate dependencies
45 sed 's/^\([^+]\)/-\1/' | # now each line is +branch or -branch (+ == recurse)
46 uniq -s 1 | # fold branch lines; + always comes before - and thus wins within uniq
47 while read depline; do
48 action="$(echo "$depline" | cut -c 1)"
49 dep="$(echo "$depline" | cut -c 2-)"
51 # We do not distinguish between dependencies out-of-date
52 # and base/remote out-of-date cases for $dep here,
53 # but thanks to needs_update returning : or %
54 # for the latter, we do correctly recurse here
57 if [ x"$action" = x+ ]; then
58 info "Recursing to $dep..."
59 git checkout -q "$dep"
61 export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
62 export PS1="[$dep] $PS1"
63 while ! $tg update; do
64 # The merge got stuck! Let the user fix it up.
65 info "You are in a subshell. If you abort the merge,"
66 info "use \`exit 1\` to abort the recursive update altogether."
67 if ! sh -i </dev/tty; then
68 info "Ok, you aborted the merge. Now, you just need to"
69 info "switch back to some sane branch using \`git checkout\`."
74 switch_to_base "$name"
77 # This will be either a proper topic branch
78 # or a remote base. (branch_needs_update() is called
79 # only on the _dependencies_, not our branch itself!)
81 info "Updating base with $dep changes..."
82 if ! git merge "$dep"; then
83 if [ -z "$TG_RECURSIVE" ]; then
84 resume="\`git checkout $name && $tg update\` again"
88 info "Please commit merge resolution and call $resume."
89 info "It is also safe to abort this operation using \`git reset --hard\`,"
90 info "but please remember that you are on the base branch now;"
91 info "you will want to switch to some normal branch afterwards."
97 info "The base is up-to-date."
101 # Home, sweet home...
102 # (We want to always switch back, in case we were on the base from failed
104 git checkout -q "$name"
106 merge_with="refs/top-bases/$name"
109 ## Second, update our head with the remote branch
111 if has_remote "$name"; then
112 rname="refs/remotes/$base_remote/$name"
113 if branch_contains "$name" "$rname"; then
114 info "The $name head is up-to-date wrt. its remote branch."
116 info "Reconciling remote branch updates with $name base..."
117 # *DETACH* our HEAD now!
118 git checkout -q "refs/top-bases/$name"
119 if ! git merge "$rname"; then
120 info "Oops, you will need to help me out here a bit."
121 info "Please commit merge resolution and call:"
122 info "git checkout $name && git merge <commitid>"
123 info "It is also safe to abort this operation using: git reset --hard $name"
126 # Go back but remember we want to merge with this, not base
127 merge_with="$(git rev-parse HEAD)"
128 git checkout -q "$name"
133 ## Third, update our head with the base
135 if branch_contains "$name" "$merge_with"; then
136 info "The $name head is up-to-date wrt. the base."
139 info "Updating $name against new base..."
140 if ! git merge "$merge_with"; then
141 if [ -z "$TG_RECURSIVE" ]; then
142 info "Please commit merge resolution. No need to do anything else"
143 info "You can abort this operation using \`git reset --hard\` now"
144 info "and retry this merge later using \`$tg update\`."
146 info "Please commit merge resolution and call exit."
147 info "You can abort this operation using \`git reset --hard\`."