What's cooking (2013/01 #04)
[alt-git.git] / Reintegrate
blob1fad661b6ca6508530498c230ff203dec626aad1
1 #!/bin/sh
3 accept_rerere="--rerere-autoupdate" generate=no update= diff= edit= stop_at_cut=
4 while case "$#,$1" in 0,*) break;; *,-*) ;; esac
5 do
6 case "$1" in
7 -n) accept_rerere= ;;
8 -e) edit=t ;;
9 -c) stop_at_cut=1 ;;
10 -c?*) stop_at_cut=${1#-c} ;;
11 -d) update=${2?"diff with what?"}
12 diff=yes
13 generate=yes
14 shift ;;
15 -u) update=${2?"update what?"}
16 generate=yes
17 shift ;;
18 *) generate=yes
19 break ;;
20 esac
21 shift
22 done
24 annotate_merge () {
25 test -f Meta/whats-cooking.txt || return 0
27 perl -e '
28 my ($branch) = $ARGV[0];
29 my ($in_section, $in_desc);
30 my @msg = ();
31 while (<STDIN>) {
32 chomp;
33 if (/^\* $branch /) {
34 $in_section = 1;
35 next;
37 last if (/^[-*\[]/ && $in_section);
38 next unless $in_section;
39 s/^\s+//;
40 if (/^$/) {
41 $in_desc = 1;
43 next unless ($in_section && $in_desc);
44 push @msg, "$_\n";
47 if ($in_section && @msg) {
48 open(my $fh, "-|", qw(git cat-file commit HEAD));
49 my @original = (<$fh>);
50 my @final;
51 $in_section = 0;
52 for (@original) {
53 if (!$in_section) {
54 $in_section = 1 if (/^$/);
55 next;
57 push @final, $_;
58 if (/^$/ && $in_section == 1) {
59 push @final, @msg;
60 push @final, "\n";
61 $in_section = 2;
64 close $fh;
65 open($fh, "|-", qw(git commit --amend -F -));
66 print $fh @final;
67 close $fh;
69 ' <Meta/whats-cooking.txt "$1"
72 case "$generate" in
73 no)
74 accept_rerere () {
75 if ! git write-tree 2>/dev/null >/dev/null
76 then
77 git rerere remaining
78 return 1
79 else
80 EDITOR=: git commit --no-verify
81 echo "Accepted previous resolution"
82 return 0
86 mark_cut () {
87 test -z "$prev_cut" && return
88 git commit --allow-empty -m "$prev_cut"
89 prev_cut=
92 cut_seen=0 prev_cut=
93 while read branch eh
95 case "$branch" in '###') cut_seen=$(( $cut_seen + 1 )) ;; esac
96 if test -n "$stop_at_cut" && test $stop_at_cut -le $cut_seen
97 then
98 continue ;# slurp the remainder and skip
101 case "$branch" in
102 '###')
103 echo >&2 "$branch $eh"
104 prev_cut="$branch $eh"
105 continue ;;
106 '#'* | '')
107 continue ;;
108 esac
110 case "$eh" in
111 "" | "#"*)
112 echo >&2 "* $branch"
114 save=$(git rev-parse --verify HEAD)
115 tip=$(git rev-parse --verify "$branch^0")
116 mb=$(git merge-base "$tip" "$save")
117 test "$mb" = "$tip" && continue
119 mark_cut
121 EDITOR=: git merge $accept_rerere --edit "$branch" ||
122 accept_rerere ||
123 exit
125 annotate_merge "$branch" || exit
126 test -z "$edit" ||
127 git commit --amend || exit
129 this=$(git rev-parse --verify HEAD)
130 if test "$this" = "$save"
131 then
133 elif git show-ref -q --verify "refs/merge-fix/$branch"
134 then
135 echo >&2 "Fixing up the merge"
136 git cherry-pick --no-commit "refs/merge-fix/$branch" &&
137 EDITOR=: git commit --amend -a || exit
140 pick" "*)
141 echo >&2 "* $eh"
143 mark_cut
145 git cherry-pick "$branch" || exit ;;
146 *) echo >&2 "Eh? $branch $eh"; exit ;;
147 esac
148 done
149 exit
150 esac
152 if test -n "$update" && test $# = 0
153 then
154 set x $(sed -n -e '2s/^# //p' <"$update") &&
155 shift
158 # Generation (or updating)
160 x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
161 x40="$x40$x40$x40$x40$x40$x40$x40$x40"
162 LF='
165 show_merge () {
166 case "$msg" in
167 "Merge branch '"*"'"*)
168 branch=$(expr "$msg" : "Merge branch '\(.*\)'")
169 merge_hier=heads/
171 "Merge remote branch '"*"'"*)
172 branch=$(expr "$msg" : "Merge remote branch '\(.*\)'")
173 merge_hier=
176 echo 2>&1 "Huh?: $msg"
177 return
179 esac &&
180 tip=$(git rev-parse --verify "refs/$merge_hier$branch" 2>/dev/null) &&
181 merged=$(git name-rev --refs="refs/$merge_hier$branch" "$other" 2>/dev/null) &&
182 merged=$(expr "$merged" : "$x40 \(.*\)") &&
183 test "$merged" != undefined || {
184 other=$(git log -1 --pretty='format:%s' $other) &&
185 merged="$branch :rebased? $other"
189 show_pick () {
190 case "$msg" in
191 "### "* | "###")
192 merged="$msg"
195 merged="$(git rev-parse --verify "$commit") pick $msg"
197 esac
201 generate () {
202 PROGRAM=$1
203 shift
204 echo '#!/bin/sh'
205 echo "# $1"
206 echo 'case "$#,$1" in'
207 echo '1,-u|1,-d)'
208 echo " exec $PROGRAM" '"$1" "$0"'
209 echo 'esac'
210 echo "$PROGRAM" '"$@" <<\EOF'
211 git log --no-decorate --pretty=oneline --first-parent "$1" |
213 series=
214 while read commit msg
216 if other=$(git rev-parse -q --verify "$commit^2")
217 then
218 show_merge
219 else
220 show_pick
223 if test -z "$series"
224 then
225 series="$merged"
226 else
227 series="$merged$LF$series"
229 done
230 echo "$series"
232 echo EOF
235 if test -z "$update"
236 then
237 generate "$0" "$@"
238 elif test -z "$diff"
239 then
240 generate "$0" "$@" | diff -u "$update" -
241 if test $? = 0
242 then
243 echo >&2 "No changes."
244 else
245 echo >&2 -n "Update [y/N]? "
246 read yesno
247 case "$yesno" in
248 [Yy]*)
249 generate "$0" "$@" |
250 sed -e 's/ :rebased?.*//' >"$update" ;;
252 echo >&2 "No update then." ;;
253 esac
255 else
256 generate "$0" "$@" | diff -u "$update" -