list_deps: accept -i/-w
[topgit.git] / tg-update.sh
blob5162c52b9d335f383a0bdb54ceb0c5929463b20f
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 while [ -n "$1" ]; do
12 arg="$1"; shift
13 case "$arg" in
14 -*)
15 echo "Usage: tg [...] update [NAME]" >&2
16 exit 1;;
18 [ -z "$name" ] || die "name already specified ($name)"
19 name="$arg";;
20 esac
21 done
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="$(get_temp tg-depcheck)"
31 missing_deps=
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"
42 cat "$depcheck" |
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
55 # in both cases.
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\`."
70 exit 3
72 done
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"
85 else # subshell
86 resume='exit'
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."
92 rm "$depcheck"
93 exit 2
95 done
96 else
97 info "The base is up-to-date."
100 # Home, sweet home...
101 # (We want to always switch back, in case we were on the base from failed
102 # previous merge.)
103 git checkout -q "$name"
105 merge_with="refs/top-bases/$name"
108 ## Second, update our head with the remote branch
110 if has_remote "$name"; then
111 rname="refs/remotes/$base_remote/$name"
112 if branch_contains "$name" "$rname"; then
113 info "The $name head is up-to-date wrt. its remote branch."
114 else
115 info "Reconciling remote branch updates with $name base..."
116 # *DETACH* our HEAD now!
117 git checkout -q "refs/top-bases/$name"
118 if ! git merge "$rname"; then
119 info "Oops, you will need to help me out here a bit."
120 info "Please commit merge resolution and call:"
121 info "git checkout $name && git merge <commitid>"
122 info "It is also safe to abort this operation using: git reset --hard $name"
123 exit 3
125 # Go back but remember we want to merge with this, not base
126 merge_with="$(git rev-parse HEAD)"
127 git checkout -q "$name"
132 ## Third, update our head with the base
134 if branch_contains "$name" "$merge_with"; then
135 info "The $name head is up-to-date wrt. the base."
136 exit 0
138 info "Updating $name against new base..."
139 if ! git merge "$merge_with"; then
140 if [ -z "$TG_RECURSIVE" ]; then
141 info "Please commit merge resolution. No need to do anything else"
142 info "You can abort this operation using \`git reset --hard\` now"
143 info "and retry this merge later using \`$tg update\`."
144 else # subshell
145 info "Please commit merge resolution and call exit."
146 info "You can abort this operation using \`git reset --hard\`."
148 exit 3
151 # vim:noet