recurse_deps+branch_needs_update(): Deal with remote branches
[topgit.git] / tg-update.sh
blob5caf6910a17f7c39906fe6456bd550faf4b537c2
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 out-of-date cases for $dep here,
47 # but thanks to needs_update returning : for the latter,
48 # we do correctly recurse here in both cases.
50 if [ x"$action" = x+ ]; then
51 info "Recursing to $dep..."
52 git checkout -q "$dep"
54 export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
55 export PS1="[$dep] $PS1"
56 while ! tg update; do
57 # The merge got stuck! Let the user fix it up.
58 info "You are in a subshell. If you abort the merge,"
59 info "use \`exit 1\` to abort the recursive update altogether."
60 if ! sh -i </dev/tty; then
61 info "Ok, you aborated the merge. Now, you just need to"
62 info "switch back to some sane branch using \`git checkout\`."
63 exit 3
65 done
67 switch_to_base "$name"
70 # This will always be a proper topic branch
71 # (not a base or remote), since for deep updates
72 # we recurse and immediate dependencies
73 # are always proper. (branch_needs_update() is called
74 # only on the _dependencies_, not our branch itself!)
76 info "Updating base with $dep changes..."
77 if ! git merge "$dep"; then
78 if [ -z "$TG_RECURSIVE" ]; then
79 resume='`tg update` again'
80 else # subshell
81 resume='exit'
83 info "Please commit merge resolution and call $resume."
84 info "It is also safe to abort this operation using \`git reset --hard\`,"
85 info "but please remember that you are on the base branch now;"
86 info "you will want to switch to some normal branch afterwards."
87 rm "$depcheck"
88 exit 2
90 done
92 # Home, sweet home...
93 git checkout -q "$name"
94 else
95 info "The base is up-to-date."
97 rm "$depcheck"
100 ## Second, update our head with the base
102 if branch_contains "$name" "refs/top-bases/$name"; then
103 info "The $name head is up-to-date wrt. the base."
104 exit 0
106 info "Updating $name against new base..."
107 if ! git merge "refs/top-bases/$name"; then
108 if [ -z "$TG_RECURSIVE" ]; then
109 info "Please commit merge resolution. No need to do anything else"
110 info "You can abort this operation using \`git reset --hard\` now"
111 info "and retry this merge later using \`tg update\`."
112 else # subshell
113 info "Please commit merge resolution and call exit."
114 info "You can abort this operation using \`git reset --hard\`."
116 exit 3