chiark / gitweb /
tg-update.sh: Fix recursive update shell call
[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 if [ -n "$1" ]; then
12         echo "Usage: tg update" >&2
13         exit 1
14 fi
15
16
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"
20
21
22 ## First, take care of our base
23
24 depcheck="$(mktemp)"
25 needs_update "$name" >"$depcheck"
26 if [ -s "$depcheck" ]; then
27         # We need to switch to the base branch
28         # ...but only if we aren't there yet (from failed previous merge)
29         HEAD="$(git symbolic-ref HEAD)"
30         if [ "$HEAD" = "${HEAD#refs/top-bases/}" ]; then
31                 switch_to_base "$name"
32         fi
33
34         cat "$depcheck" |
35                 sed 's/ [^ ]* *$//' | # last is $name
36                 sed 's/.* \([^ ]*\)$/+\1/' | # only immediate dependencies
37                 sed 's/^\([^+]\)/-\1/' | # now each line is +branch or -branch (+ == recurse)
38                 uniq -s 1 | # fold branch lines; + always comes before - and thus wins within uniq
39                 while read depline; do
40                         action="${depline:0:1}"
41                         dep="${depline:1}"
42
43                         # We do not distinguish between dependencies out-of-date
44                         # and base out-of-date cases for $dep here, but thanks
45                         # to needs_update returning : for the latter, we do
46                         # correctly recurse here in both cases.
47
48                         if [ x"$action" = x+ ]; then
49                                 info "Recursing to $dep..."
50                                 git checkout -q "$dep"
51                                 (
52                                 export TG_RECURSIVE="[$dep] $TG_RECURSIVE"
53                                 export PS1="[$dep] $PS1"
54                                 while ! tg update; do
55                                         # The merge got stuck! Let the user fix it up.
56                                         info "You are in a subshell. If you abort the merge,"
57                                         info "use \`exit 1\` to abort the recursive update altogether."
58                                         if ! sh -i </dev/tty; then
59                                                 info "Ok, you aborated the merge. Now, you just need to"
60                                                 info "switch back to some sane branch using \`git checkout\`."
61                                                 exit 3
62                                         fi
63                                 done
64                                 )
65                                 switch_to_base "$name"
66                         fi
67
68                         info "Updating base with $dep changes..."
69                         if ! git merge "$dep"; then
70                                 if [ -z "$TG_RECURSIVE" ]; then
71                                         resume='`tg update` again'
72                                 else # subshell
73                                         resume='exit'
74                                 fi
75                                 info "Please commit merge resolution and call $resume."
76                                 info "It is also safe to abort this operation using \`git reset --hard\`,"
77                                 info "but please remember that you are on the base branch now;"
78                                 info "you will want to switch to some normal branch afterwards."
79                                 rm "$depcheck"
80                                 exit 2
81                         fi
82                 done
83
84         # Home, sweet home...
85         git checkout -q "$name"
86 else
87         info "The base is up-to-date."
88 fi
89 rm "$depcheck"
90
91
92 ## Second, update our head with the base
93
94 if branch_contains "$name" "refs/top-bases/$name"; then
95         info "The $name head is up-to-date wrt. the base."
96         exit 0
97 fi
98 info "Updating $name against new base..."
99 if ! git merge "refs/top-bases/$name"; then
100         if [ -z "$TG_RECURSIVE" ]; then
101                 info "Please commit merge resolution. No need to do anything else"
102                 info "You can abort this operation using \`git reset --hard\` now"
103                 info "and retry this merge later using \`tg update\`."
104         else # subshell
105                 info "Please commit merge resolution and call exit."
106                 info "You can abort this operation using \`git reset --hard\`."
107         fi
108         exit 3
109 fi