What's cooking (2014/06 #02)
[git/jrn.git] / Reintegrate
blob2be5bd659ac71914c5c6cf2e5abf00e59c1e9615
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 sub wildo_match {
29 s/^\s*//;
30 if (/^Will (?:\S+ ){0,2}(fast-track|keep|merge|drop|discard|cook|kick|defer|be re-?rolled)[,. ]/ ||
31 /^Not urgent/ || /^Not ready/ || /^Waiting for / ||
32 /^Can wait in / ||
33 /^Needs? / || /^Expecting / || /^May want to /) {
34 return 1;
36 return 0;
39 my ($branch) = $ARGV[0];
40 my ($in_section, $in_desc);
41 my @msg = ();
42 while (<STDIN>) {
43 chomp;
44 if (/^\* $branch /) {
45 $in_section = 1;
46 next;
48 last if (/^[-*\[]/ && $in_section);
49 next unless $in_section;
50 s/^\s+//;
51 if (/^$/) {
52 $in_desc = 1;
54 next unless ($in_section && $in_desc);
55 next if (/Originally merged to '\''next'\'' on ([-0-9]+)/);
56 next if (wildo_match($_));
57 push @msg, "$_\n";
60 if ($in_section && @msg) {
61 open(my $fh, "-|", qw(git cat-file commit HEAD));
62 my @original = (<$fh>);
63 close $fh;
64 my @final;
65 $in_section = 0;
66 for (@original) {
67 if (!$in_section) {
68 $in_section = 1 if (/^$/);
69 next;
71 if (/^Conflicts:$/ && $in_section == 2) {
72 $in_section = 3;
75 if ($in_section == 3) {
76 $_ = "# $_";
78 push @final, $_;
79 if (/^$/ && $in_section == 1) {
80 push @final, @msg;
81 push @final, "\n";
82 $in_section = 2;
85 open($fh, "|-", qw(git commit --amend -F -));
86 print $fh @final;
87 close $fh;
89 ' <Meta/whats-cooking.txt "$1"
92 case "$generate" in
93 no)
94 accept_rerere () {
95 if ! git write-tree 2>/dev/null >/dev/null
96 then
97 git rerere remaining
98 return 1
99 else
100 EDITOR=: git commit --no-verify
101 echo "Accepted previous resolution"
102 return 0
106 mark_cut () {
107 test -n "$stop_at_cut" && return
109 count_since_last_cut=$(( $count_since_last_cut + 1 ))
110 test -z "$prev_cut" && return
111 git commit --allow-empty -m "$prev_cut"
112 prev_cut=
115 cut_seen=0 prev_cut= count_since_last_cut=0
116 while read branch eh
118 case "$branch" in '###') cut_seen=$(( $cut_seen + 1 )) ;; esac
119 if test -n "$stop_at_cut" && test $stop_at_cut -le $cut_seen
120 then
121 continue ;# slurp the remainder and skip
124 case "$branch" in
125 '###')
126 if test "$count_since_last_cut" = 0
127 then
128 prev_cut=
129 else
130 echo >&2 "$branch $eh"
131 prev_cut="$branch $eh"
132 count_since_last_cut=0
134 continue ;;
135 '#'* | '')
136 continue ;;
137 esac
139 case "$eh" in
140 "" | "#"*)
141 echo >&2 "* $branch"
143 save=$(git rev-parse --verify HEAD) &&
144 tip=$(git rev-parse --verify "$branch^0") &&
145 mb=$(git merge-base "$tip" "$save") ||
146 exit
148 test "$mb" = "$tip" && continue
150 mark_cut
152 rebuild=$(git config "branch.$branch.rebuild" || :)
154 EDITOR=: git merge $rebuild $accept_rerere --edit "$branch" ||
155 accept_rerere ||
156 exit
158 annotate_merge "$branch" || exit
159 test -z "$edit" ||
160 git commit --amend || exit
162 this=$(git rev-parse --verify HEAD)
163 if test "$this" = "$save"
164 then
166 elif git show-ref -q --verify "refs/merge-fix/$branch"
167 then
168 echo >&2 "Fixing up the merge"
169 git cherry-pick --no-commit "refs/merge-fix/$branch" &&
170 EDITOR=: git commit --amend -a || exit
173 pick" "*)
174 echo >&2 "* $eh"
176 mark_cut
178 git cherry-pick "$branch" || exit ;;
179 *) echo >&2 "Eh? $branch $eh"; exit ;;
180 esac
181 done
182 exit
183 esac
185 if test -n "$update" && test $# = 0
186 then
187 set x $(sed -n -e '2s/^# //p' <"$update") &&
188 shift
191 # Generation (or updating)
193 x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
194 x40="$x40$x40$x40$x40$x40$x40$x40$x40"
195 LF='
198 show_merge () {
199 case "$msg" in
200 "Merge branch '"*"'"*)
201 branch=$(expr "$msg" : "Merge branch '\(.*\)'")
202 merge_hier=heads/
204 "Merge remote branch '"*"'"*)
205 branch=$(expr "$msg" : "Merge remote branch '\(.*\)'")
206 merge_hier=
209 echo 2>&1 "Huh?: $msg"
210 return
212 esac &&
213 tip=$(git rev-parse --verify "refs/$merge_hier$branch" 2>/dev/null) &&
214 merged=$(git name-rev --refs="refs/$merge_hier$branch" "$other" 2>/dev/null) &&
215 merged=$(expr "$merged" : "$x40 \(.*\)") &&
216 test "$merged" != undefined || {
217 other=$(git log -1 --pretty='format:%s' $other) &&
218 merged="$branch :rebased? $other"
222 show_pick () {
223 case "$msg" in
224 "### "* | "###")
225 merged="$msg"
228 merged="$(git rev-parse --verify "$commit") pick $msg"
230 esac
234 generate () {
235 PROGRAM=$1
236 shift
237 echo '#!/bin/sh'
238 echo "# $1"
239 echo 'case "$#,$1" in'
240 echo '1,-u|1,-d)'
241 echo " exec $PROGRAM" '"$1" "$0"'
242 echo 'esac'
243 echo "$PROGRAM" '"$@" <<\EOF'
244 git log --no-decorate --pretty=oneline --first-parent "$1" |
246 series=
247 while read commit msg
249 if other=$(git rev-parse -q --verify "$commit^2")
250 then
251 show_merge
252 else
253 show_pick
256 if test -z "$series"
257 then
258 series="$merged"
259 else
260 series="$merged$LF$series"
262 done
263 echo "$series"
265 echo EOF
268 if test -z "$update"
269 then
270 generate "$0" "$@"
271 elif test -z "$diff"
272 then
273 generate "$0" "$@" | diff -w -u "$update" -
274 if test $? = 0
275 then
276 echo >&2 "No changes."
277 else
278 echo >&2 -n "Update [y/N]? "
279 read yesno
280 case "$yesno" in
281 [Yy]*)
282 generate "$0" "$@" |
283 sed -e 's/ :rebased?.*//' >"$update" ;;
285 echo >&2 "No update then." ;;
286 esac
288 else
289 generate "$0" "$@" | diff -w -u "$update" -