chiark / gitweb /
README: move note about shortcomings of using --first-parent from tg base to tg log
[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="$(mktemp -t tg-depcheck.XXXXXX)"
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 rm "$depcheck"
100
101 # Home, sweet home...
102 # (We want to always switch back, in case we were on the base from failed
103 # previous merge.)
104 git checkout -q "$name"
105
106 merge_with="refs/top-bases/$name"
107
108
109 ## Second, update our head with the remote branch
110
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."
115         else
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"
124                         exit 3
125                 fi
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"
129         fi
130 fi
131
132
133 ## Third, update our head with the base
134
135 if branch_contains "$name" "$merge_with"; then
136         info "The $name head is up-to-date wrt. the base."
137         exit 0
138 fi
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\`."
145         else # subshell
146                 info "Please commit merge resolution and call exit."
147                 info "You can abort this operation using \`git reset --hard\`."
148         fi
149         exit 3
150 fi
151
152 # vim:noet