chiark / gitweb /
New TopGit dependency: fixes/ensure-worktree
[topgit.git] / tg-update.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6 name=
7
8
9 ## Parse options
10
11 while [ -n "$1" ]; do
12         arg="$1"; shift
13         case "$arg" in
14         -*)
15                 echo "Usage: tg [...] update [NAME]" >&2
16                 exit 1;;
17         *)
18                 [ -z "$name" ] || die "name already specified ($name)"
19                 name="$arg";;
20         esac
21 done
22
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"
26
27
28 ## First, take care of our base
29
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"
40         fi
41
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-)"
50
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.
56
57                         if [ x"$action" = x+ ]; then
58                                 info "Recursing to $dep..."
59                                 git checkout -q "$dep"
60                                 (
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
71                                         fi
72                                 done
73                                 )
74                                 switch_to_base "$name"
75                         fi
76
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!)
80
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'
87                                 fi
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
94                         fi
95                 done
96 else
97         info "The base is up-to-date."
98 fi
99
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"
104
105 merge_with="refs/top-bases/$name"
106
107
108 ## Second, update our head with the remote branch
109
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
124                 fi
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"
128         fi
129 fi
130
131
132 ## Third, update our head with the base
133
134 if branch_contains "$name" "$merge_with"; then
135         info "The $name head is up-to-date wrt. the base."
136         exit 0
137 fi
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\`."
147         fi
148         exit 3
149 fi
150
151 # vim:noet