]>
Commit | Line | Data |
---|---|---|
1b1dce4b JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Johannes E. Schindelin | |
4 | ||
5 | # SHORT DESCRIPTION | |
6 | # | |
7 | # This script makes it easy to fix up commits in the middle of a series, | |
8 | # and rearrange commits. | |
9 | # | |
10 | # The original idea comes from Eric W. Biederman, in | |
11 | # http://article.gmane.org/gmane.comp.version-control.git/22407 | |
12 | ||
7970aaf0 SB |
13 | OPTIONS_KEEPDASHDASH= |
14 | OPTIONS_SPEC="\ | |
15 | git-rebase [-i] [options] [--] <upstream> [<branch>] | |
16 | git-rebase [-i] (--continue | --abort | --skip) | |
17 | -- | |
18 | Available options are | |
19 | v,verbose display a diffstat of what changed upstream | |
20 | onto= rebase onto given branch instead of upstream | |
21 | p,preserve-merges try to recreate merges instead of ignoring them | |
22 | s,strategy= use the given merge strategy | |
23 | m,merge always used (no-op) | |
24 | i,interactive always used (no-op) | |
25 | Actions: | |
26 | continue continue rebasing process | |
27 | abort abort rebasing process and restore original branch | |
28 | skip skip current patch and continue rebasing process | |
29 | " | |
1b1dce4b JS |
30 | |
31 | . git-sh-setup | |
32 | require_work_tree | |
33 | ||
28ed6e7b | 34 | DOTEST="$GIT_DIR/rebase-merge" |
c22486c9 | 35 | TODO="$DOTEST"/git-rebase-todo |
1b1dce4b | 36 | DONE="$DOTEST"/done |
6368f3f8 JS |
37 | MSG="$DOTEST"/message |
38 | SQUASH_MSG="$DOTEST"/message-squash | |
f09c9b8c JS |
39 | REWRITTEN="$DOTEST"/rewritten |
40 | PRESERVE_MERGES= | |
1b1dce4b | 41 | STRATEGY= |
7970aaf0 | 42 | ONTO= |
1b1dce4b JS |
43 | VERBOSE= |
44 | ||
804c7174 WC |
45 | GIT_CHERRY_PICK_HELP=" After resolving the conflicts, |
46 | mark the corrected paths with 'git add <paths>', and | |
47 | run 'git rebase --continue'" | |
48 | export GIT_CHERRY_PICK_HELP | |
49 | ||
1b1dce4b JS |
50 | warn () { |
51 | echo "$*" >&2 | |
52 | } | |
53 | ||
dfa49f33 JS |
54 | output () { |
55 | case "$VERBOSE" in | |
56 | '') | |
5166810b | 57 | output=$("$@" 2>&1 ) |
dfa49f33 | 58 | status=$? |
5166810b | 59 | test $status != 0 && printf "%s\n" "$output" |
dfa49f33 | 60 | return $status |
376ccb8c | 61 | ;; |
dfa49f33 JS |
62 | *) |
63 | "$@" | |
376ccb8c | 64 | ;; |
dfa49f33 JS |
65 | esac |
66 | } | |
67 | ||
1b1dce4b JS |
68 | require_clean_work_tree () { |
69 | # test if working tree is dirty | |
70 | git rev-parse --verify HEAD > /dev/null && | |
6848d58c JS |
71 | git update-index --ignore-submodules --refresh && |
72 | git diff-files --quiet --ignore-submodules && | |
73 | git diff-index --cached --quiet HEAD --ignore-submodules -- || | |
1b1dce4b JS |
74 | die "Working tree is dirty" |
75 | } | |
76 | ||
77 | ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION" | |
78 | ||
79 | comment_for_reflog () { | |
80 | case "$ORIG_REFLOG_ACTION" in | |
81 | ''|rebase*) | |
82 | GIT_REFLOG_ACTION="rebase -i ($1)" | |
83 | export GIT_REFLOG_ACTION | |
376ccb8c | 84 | ;; |
1b1dce4b JS |
85 | esac |
86 | } | |
87 | ||
4e673877 | 88 | last_count= |
1b1dce4b JS |
89 | mark_action_done () { |
90 | sed -e 1q < "$TODO" >> "$DONE" | |
91 | sed -e 1d < "$TODO" >> "$TODO".new | |
92 | mv -f "$TODO".new "$TODO" | |
aadbe44f JK |
93 | count=$(grep -c '^[^#]' < "$DONE") |
94 | total=$(($count+$(grep -c '^[^#]' < "$TODO"))) | |
4e673877 JH |
95 | if test "$last_count" != "$count" |
96 | then | |
97 | last_count=$count | |
98 | printf "Rebasing (%d/%d)\r" $count $total | |
99 | test -z "$VERBOSE" || echo | |
100 | fi | |
1b1dce4b JS |
101 | } |
102 | ||
103 | make_patch () { | |
be6ff208 JS |
104 | parent_sha1=$(git rev-parse --verify "$1"^) || |
105 | die "Cannot get patch for $1^" | |
f3d5e463 | 106 | git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch |
be6ff208 JS |
107 | test -f "$DOTEST"/message || |
108 | git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message | |
109 | test -f "$DOTEST"/author-script || | |
110 | get_author_ident_from_commit "$1" > "$DOTEST"/author-script | |
1b1dce4b JS |
111 | } |
112 | ||
113 | die_with_patch () { | |
114 | make_patch "$1" | |
ecfe72ff | 115 | git rerere |
1b1dce4b JS |
116 | die "$2" |
117 | } | |
118 | ||
c54b7817 JS |
119 | die_abort () { |
120 | rm -rf "$DOTEST" | |
121 | die "$1" | |
122 | } | |
123 | ||
376ccb8c | 124 | has_action () { |
aadbe44f | 125 | grep '^[^#]' "$1" >/dev/null |
376ccb8c JS |
126 | } |
127 | ||
1b1dce4b | 128 | pick_one () { |
1d25c8cf JS |
129 | no_ff= |
130 | case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac | |
dfa49f33 | 131 | output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1" |
f09c9b8c JS |
132 | test -d "$REWRITTEN" && |
133 | pick_one_preserving_merges "$@" && return | |
376ccb8c JS |
134 | parent_sha1=$(git rev-parse --verify $sha1^) || |
135 | die "Could not get the parent of $sha1" | |
1b1dce4b | 136 | current_sha1=$(git rev-parse --verify HEAD) |
2858028e | 137 | if test "$no_ff$current_sha1" = "$parent_sha1"; then |
dfa49f33 JS |
138 | output git reset --hard $sha1 |
139 | test "a$1" = a-n && output git reset --soft $current_sha1 | |
1b1dce4b | 140 | sha1=$(git rev-parse --short $sha1) |
dfa49f33 | 141 | output warn Fast forward to $sha1 |
1b1dce4b | 142 | else |
2a9c53e0 | 143 | output git cherry-pick "$@" |
1b1dce4b JS |
144 | fi |
145 | } | |
146 | ||
f09c9b8c JS |
147 | pick_one_preserving_merges () { |
148 | case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac | |
149 | sha1=$(git rev-parse $sha1) | |
150 | ||
3b38ec16 | 151 | if test -f "$DOTEST"/current-commit |
f09c9b8c JS |
152 | then |
153 | current_commit=$(cat "$DOTEST"/current-commit) && | |
154 | git rev-parse HEAD > "$REWRITTEN"/$current_commit && | |
155 | rm "$DOTEST"/current-commit || | |
156 | die "Cannot write current commit's replacement sha1" | |
157 | fi | |
158 | ||
159 | # rewrite parents; if none were rewritten, we can fast-forward. | |
160 | fast_forward=t | |
161 | preserve=t | |
162 | new_parents= | |
376ccb8c | 163 | for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-) |
f09c9b8c | 164 | do |
3b38ec16 | 165 | if test -f "$REWRITTEN"/$p |
f09c9b8c JS |
166 | then |
167 | preserve=f | |
168 | new_p=$(cat "$REWRITTEN"/$p) | |
169 | test $p != $new_p && fast_forward=f | |
170 | case "$new_parents" in | |
171 | *$new_p*) | |
172 | ;; # do nothing; that parent is already there | |
173 | *) | |
174 | new_parents="$new_parents $new_p" | |
376ccb8c | 175 | ;; |
f09c9b8c | 176 | esac |
1c5fa0a1 SB |
177 | else |
178 | new_parents="$new_parents $p" | |
f09c9b8c JS |
179 | fi |
180 | done | |
181 | case $fast_forward in | |
182 | t) | |
dfa49f33 | 183 | output warn "Fast forward to $sha1" |
376ccb8c | 184 | test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1 |
f09c9b8c JS |
185 | ;; |
186 | f) | |
187 | test "a$1" = a-n && die "Refusing to squash a merge: $sha1" | |
188 | ||
376ccb8c | 189 | first_parent=$(expr "$new_parents" : ' \([^ ]*\)') |
f09c9b8c | 190 | # detach HEAD to current parent |
dfa49f33 | 191 | output git checkout $first_parent 2> /dev/null || |
f09c9b8c JS |
192 | die "Cannot move HEAD to $first_parent" |
193 | ||
194 | echo $sha1 > "$DOTEST"/current-commit | |
195 | case "$new_parents" in | |
376ccb8c | 196 | ' '*' '*) |
f09c9b8c JS |
197 | # redo merge |
198 | author_script=$(get_author_ident_from_commit $sha1) | |
199 | eval "$author_script" | |
376ccb8c | 200 | msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')" |
f91333d6 BS |
201 | # No point in merging the first parent, that's HEAD |
202 | new_parents=${new_parents# $first_parent} | |
ae830ed7 JS |
203 | if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
204 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
205 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
206 | output git merge $STRATEGY -m "$msg" \ | |
207 | $new_parents | |
f09c9b8c | 208 | then |
ecfe72ff | 209 | git rerere |
376ccb8c | 210 | printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG |
18640d99 | 211 | die Error redoing merge $sha1 |
f09c9b8c JS |
212 | fi |
213 | ;; | |
214 | *) | |
2a9c53e0 | 215 | output git cherry-pick "$@" || |
f09c9b8c | 216 | die_with_patch $sha1 "Could not pick $sha1" |
376ccb8c | 217 | ;; |
f09c9b8c | 218 | esac |
376ccb8c | 219 | ;; |
f09c9b8c JS |
220 | esac |
221 | } | |
222 | ||
6368f3f8 JS |
223 | nth_string () { |
224 | case "$1" in | |
225 | *1[0-9]|*[04-9]) echo "$1"th;; | |
226 | *1) echo "$1"st;; | |
227 | *2) echo "$1"nd;; | |
228 | *3) echo "$1"rd;; | |
229 | esac | |
230 | } | |
231 | ||
232 | make_squash_message () { | |
3b38ec16 | 233 | if test -f "$SQUASH_MSG"; then |
c7965afd | 234 | COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \ |
b4ce54fc | 235 | < "$SQUASH_MSG" | sed -ne '$p')+1)) |
6368f3f8 | 236 | echo "# This is a combination of $COUNT commits." |
8ad1065e JH |
237 | sed -e 1d -e '2,/^./{ |
238 | /^$/d | |
239 | }' <"$SQUASH_MSG" | |
6368f3f8 JS |
240 | else |
241 | COUNT=2 | |
242 | echo "# This is a combination of two commits." | |
243 | echo "# The first commit's message is:" | |
244 | echo | |
245 | git cat-file commit HEAD | sed -e '1,/^$/d' | |
6368f3f8 | 246 | fi |
8ad1065e | 247 | echo |
6368f3f8 JS |
248 | echo "# This is the $(nth_string $COUNT) commit message:" |
249 | echo | |
250 | git cat-file commit $1 | sed -e '1,/^$/d' | |
251 | } | |
252 | ||
253 | peek_next_command () { | |
254 | sed -n "1s/ .*$//p" < "$TODO" | |
255 | } | |
256 | ||
1b1dce4b | 257 | do_next () { |
376ccb8c JS |
258 | rm -f "$DOTEST"/message "$DOTEST"/author-script \ |
259 | "$DOTEST"/amend || exit | |
1b1dce4b JS |
260 | read command sha1 rest < "$TODO" |
261 | case "$command" in | |
376ccb8c | 262 | '#'*|'') |
1b1dce4b | 263 | mark_action_done |
1b1dce4b | 264 | ;; |
f8babc4d | 265 | pick|p) |
1b1dce4b JS |
266 | comment_for_reflog pick |
267 | ||
268 | mark_action_done | |
269 | pick_one $sha1 || | |
270 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
271 | ;; | |
f8babc4d | 272 | edit|e) |
1b1dce4b JS |
273 | comment_for_reflog edit |
274 | ||
275 | mark_action_done | |
276 | pick_one $sha1 || | |
277 | die_with_patch $sha1 "Could not apply $sha1... $rest" | |
278 | make_patch $sha1 | |
be6ff208 | 279 | : > "$DOTEST"/amend |
a8ccc204 | 280 | warn "Stopped at $sha1... $rest" |
1b1dce4b JS |
281 | warn "You can amend the commit now, with" |
282 | warn | |
283 | warn " git commit --amend" | |
284 | warn | |
0460fb44 JS |
285 | warn "Once you are satisfied with your changes, run" |
286 | warn | |
287 | warn " git rebase --continue" | |
288 | warn | |
1b1dce4b JS |
289 | exit 0 |
290 | ;; | |
f8babc4d | 291 | squash|s) |
1b1dce4b JS |
292 | comment_for_reflog squash |
293 | ||
376ccb8c | 294 | has_action "$DONE" || |
1b1dce4b JS |
295 | die "Cannot 'squash' without a previous commit" |
296 | ||
297 | mark_action_done | |
6368f3f8 JS |
298 | make_squash_message $sha1 > "$MSG" |
299 | case "$(peek_next_command)" in | |
f8babc4d | 300 | squash|s) |
6368f3f8 | 301 | EDIT_COMMIT= |
91e1ee77 | 302 | USE_OUTPUT=output |
6368f3f8 | 303 | cp "$MSG" "$SQUASH_MSG" |
376ccb8c | 304 | ;; |
6368f3f8 JS |
305 | *) |
306 | EDIT_COMMIT=-e | |
91e1ee77 | 307 | USE_OUTPUT= |
376ccb8c JS |
308 | rm -f "$SQUASH_MSG" || exit |
309 | ;; | |
6368f3f8 JS |
310 | esac |
311 | ||
793ad041 | 312 | failed=f |
81ab1cb4 | 313 | author_script=$(get_author_ident_from_commit HEAD) |
dfa49f33 | 314 | output git reset --soft HEAD^ |
fb47cfbd | 315 | pick_one -n $sha1 || failed=t |
18640d99 | 316 | echo "$author_script" > "$DOTEST"/author-script |
dbedf972 SP |
317 | if test $failed = f |
318 | then | |
1b1dce4b JS |
319 | # This is like --amend, but with a different message |
320 | eval "$author_script" | |
ae830ed7 JS |
321 | GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \ |
322 | GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \ | |
323 | GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \ | |
dbedf972 SP |
324 | $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t |
325 | fi | |
326 | if test $failed = t | |
327 | then | |
1b1dce4b JS |
328 | cp "$MSG" "$GIT_DIR"/MERGE_MSG |
329 | warn | |
330 | warn "Could not apply $sha1... $rest" | |
1b1dce4b | 331 | die_with_patch $sha1 "" |
dbedf972 | 332 | fi |
1b1dce4b JS |
333 | ;; |
334 | *) | |
335 | warn "Unknown command: $command $sha1 $rest" | |
336 | die_with_patch $sha1 "Please fix this in the file $TODO." | |
376ccb8c | 337 | ;; |
1b1dce4b JS |
338 | esac |
339 | test -s "$TODO" && return | |
340 | ||
68a163c9 JS |
341 | comment_for_reflog finish && |
342 | HEADNAME=$(cat "$DOTEST"/head-name) && | |
343 | OLDHEAD=$(cat "$DOTEST"/head) && | |
344 | SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) && | |
3b38ec16 | 345 | if test -d "$REWRITTEN" |
f09c9b8c JS |
346 | then |
347 | test -f "$DOTEST"/current-commit && | |
348 | current_commit=$(cat "$DOTEST"/current-commit) && | |
349 | git rev-parse HEAD > "$REWRITTEN"/$current_commit | |
34454e85 JS |
350 | if test -f "$REWRITTEN"/$OLDHEAD |
351 | then | |
352 | NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD) | |
353 | else | |
354 | NEWHEAD=$OLDHEAD | |
355 | fi | |
f09c9b8c JS |
356 | else |
357 | NEWHEAD=$(git rev-parse HEAD) | |
358 | fi && | |
73697a0b JS |
359 | case $HEADNAME in |
360 | refs/*) | |
361 | message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" && | |
362 | git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD && | |
363 | git symbolic-ref HEAD $HEADNAME | |
364 | ;; | |
365 | esac && { | |
3df0a859 | 366 | test ! -f "$DOTEST"/verbose || |
f3d5e463 | 367 | git diff-tree --stat $(cat "$DOTEST"/head)..HEAD |
3df0a859 | 368 | } && |
1b1dce4b | 369 | rm -rf "$DOTEST" && |
73697a0b | 370 | git gc --auto && |
1b1dce4b JS |
371 | warn "Successfully rebased and updated $HEADNAME." |
372 | ||
373 | exit | |
374 | } | |
375 | ||
376 | do_rest () { | |
377 | while : | |
378 | do | |
379 | do_next | |
380 | done | |
1b1dce4b JS |
381 | } |
382 | ||
7970aaf0 SB |
383 | # check if no other options are set |
384 | is_standalone () { | |
385 | test $# -eq 2 -a "$2" = '--' && | |
386 | test -z "$ONTO" && | |
387 | test -z "$PRESERVE_MERGES" && | |
388 | test -z "$STRATEGY" && | |
389 | test -z "$VERBOSE" | |
390 | } | |
391 | ||
392 | get_saved_options () { | |
393 | test -d "$REWRITTEN" && PRESERVE_MERGES=t | |
394 | test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)" | |
395 | test -f "$DOTEST"/verbose && VERBOSE=t | |
396 | } | |
397 | ||
822f7c73 | 398 | while test $# != 0 |
1b1dce4b JS |
399 | do |
400 | case "$1" in | |
401 | --continue) | |
7970aaf0 SB |
402 | is_standalone "$@" || usage |
403 | get_saved_options | |
1b1dce4b JS |
404 | comment_for_reflog continue |
405 | ||
406 | test -d "$DOTEST" || die "No interactive rebase running" | |
407 | ||
ab119032 JH |
408 | # Sanity check |
409 | git rev-parse --verify HEAD >/dev/null || | |
410 | die "Cannot read HEAD" | |
6848d58c JS |
411 | git update-index --ignore-submodules --refresh && |
412 | git diff-files --quiet --ignore-submodules || | |
ab119032 JH |
413 | die "Working tree is dirty" |
414 | ||
415 | # do we have anything to commit? | |
6848d58c | 416 | if git diff-index --cached --quiet --ignore-submodules HEAD -- |
03270628 | 417 | then |
ab119032 JH |
418 | : Nothing to commit -- skip this |
419 | else | |
420 | . "$DOTEST"/author-script || | |
421 | die "Cannot find the author identity" | |
422 | if test -f "$DOTEST"/amend | |
423 | then | |
424 | git reset --soft HEAD^ || | |
425 | die "Cannot rewind the HEAD" | |
426 | fi | |
427 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE && | |
428 | git commit --no-verify -F "$DOTEST"/message -e || | |
dbedf972 | 429 | die "Could not commit staged changes." |
03270628 | 430 | fi |
18640d99 | 431 | |
1b1dce4b JS |
432 | require_clean_work_tree |
433 | do_rest | |
434 | ;; | |
435 | --abort) | |
7970aaf0 SB |
436 | is_standalone "$@" || usage |
437 | get_saved_options | |
1b1dce4b JS |
438 | comment_for_reflog abort |
439 | ||
ecfe72ff | 440 | git rerere clear |
1b1dce4b JS |
441 | test -d "$DOTEST" || die "No interactive rebase running" |
442 | ||
443 | HEADNAME=$(cat "$DOTEST"/head-name) | |
444 | HEAD=$(cat "$DOTEST"/head) | |
73697a0b JS |
445 | case $HEADNAME in |
446 | refs/*) | |
447 | git symbolic-ref HEAD $HEADNAME | |
448 | ;; | |
449 | esac && | |
dfa49f33 | 450 | output git reset --hard $HEAD && |
1b1dce4b JS |
451 | rm -rf "$DOTEST" |
452 | exit | |
453 | ;; | |
454 | --skip) | |
7970aaf0 SB |
455 | is_standalone "$@" || usage |
456 | get_saved_options | |
1b1dce4b JS |
457 | comment_for_reflog skip |
458 | ||
ecfe72ff | 459 | git rerere clear |
1b1dce4b JS |
460 | test -d "$DOTEST" || die "No interactive rebase running" |
461 | ||
dfa49f33 | 462 | output git reset --hard && do_rest |
1b1dce4b | 463 | ;; |
7970aaf0 | 464 | -s) |
1b1dce4b JS |
465 | case "$#,$1" in |
466 | *,*=*) | |
b5e960b1 | 467 | STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;; |
1b1dce4b JS |
468 | 1,*) |
469 | usage ;; | |
470 | *) | |
471 | STRATEGY="-s $2" | |
472 | shift ;; | |
473 | esac | |
474 | ;; | |
7970aaf0 | 475 | -m) |
1b1dce4b JS |
476 | # we use merge anyway |
477 | ;; | |
7970aaf0 | 478 | -v) |
1b1dce4b JS |
479 | VERBOSE=t |
480 | ;; | |
7970aaf0 | 481 | -p) |
f09c9b8c JS |
482 | PRESERVE_MERGES=t |
483 | ;; | |
7970aaf0 | 484 | -i) |
1b1dce4b JS |
485 | # yeah, we know |
486 | ;; | |
7970aaf0 SB |
487 | --onto) |
488 | shift | |
489 | ONTO=$(git rev-parse --verify "$1") || | |
490 | die "Does not point to a valid commit: $1" | |
1b1dce4b | 491 | ;; |
7970aaf0 SB |
492 | --) |
493 | shift | |
494 | test $# -eq 1 -o $# -eq 2 || usage | |
1b1dce4b JS |
495 | test -d "$DOTEST" && |
496 | die "Interactive rebase already started" | |
497 | ||
498 | git var GIT_COMMITTER_IDENT >/dev/null || | |
499 | die "You need to set your committer info first" | |
500 | ||
501 | comment_for_reflog start | |
502 | ||
1b1dce4b JS |
503 | require_clean_work_tree |
504 | ||
69e66f55 JS |
505 | UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base" |
506 | test -z "$ONTO" && ONTO=$UPSTREAM | |
507 | ||
3b38ec16 | 508 | if test ! -z "$2" |
1b1dce4b | 509 | then |
dfa49f33 | 510 | output git show-ref --verify --quiet "refs/heads/$2" || |
1b1dce4b | 511 | die "Invalid branchname: $2" |
dfa49f33 | 512 | output git checkout "$2" || |
1b1dce4b JS |
513 | die "Could not checkout $2" |
514 | fi | |
515 | ||
516 | HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?" | |
5166810b MK |
517 | mkdir "$DOTEST" || die "Could not create temporary $DOTEST" |
518 | ||
1b1dce4b | 519 | : > "$DOTEST"/interactive || die "Could not mark as interactive" |
73697a0b JS |
520 | git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null || |
521 | echo "detached HEAD" > "$DOTEST"/head-name | |
1b1dce4b JS |
522 | |
523 | echo $HEAD > "$DOTEST"/head | |
524 | echo $UPSTREAM > "$DOTEST"/upstream | |
525 | echo $ONTO > "$DOTEST"/onto | |
8e4a91bd | 526 | test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy |
1b1dce4b | 527 | test t = "$VERBOSE" && : > "$DOTEST"/verbose |
3b38ec16 | 528 | if test t = "$PRESERVE_MERGES" |
f09c9b8c JS |
529 | then |
530 | # $REWRITTEN contains files for each commit that is | |
531 | # reachable by at least one merge base of $HEAD and | |
532 | # $UPSTREAM. They are not necessarily rewritten, but | |
533 | # their children might be. | |
534 | # This ensures that commits on merged, but otherwise | |
535 | # unrelated side branches are left alone. (Think "X" | |
536 | # in the man page's example.) | |
537 | mkdir "$REWRITTEN" && | |
538 | for c in $(git merge-base --all $HEAD $UPSTREAM) | |
539 | do | |
540 | echo $ONTO > "$REWRITTEN"/$c || | |
541 | die "Could not init rewritten commits" | |
542 | done | |
543 | MERGES_OPTION= | |
544 | else | |
545 | MERGES_OPTION=--no-merges | |
546 | fi | |
1b1dce4b | 547 | |
c54b7817 JS |
548 | SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM) |
549 | SHORTHEAD=$(git rev-parse --short $HEAD) | |
550 | SHORTONTO=$(git rev-parse --short $ONTO) | |
6047a234 JS |
551 | git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \ |
552 | --abbrev=7 --reverse --left-right --cherry-pick \ | |
553 | $UPSTREAM...$HEAD | \ | |
554 | sed -n "s/^>/pick /p" > "$TODO" | |
555 | cat >> "$TODO" << EOF | |
556 | ||
557 | # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO | |
1b1dce4b JS |
558 | # |
559 | # Commands: | |
88b1f0b8 MV |
560 | # p, pick = use commit |
561 | # e, edit = use commit, but stop for amending | |
562 | # s, squash = use commit, but meld into previous commit | |
82576ddb JS |
563 | # |
564 | # If you remove a line here THAT COMMIT WILL BE LOST. | |
6047a234 | 565 | # However, if you remove everything, the rebase will be aborted. |
82576ddb | 566 | # |
1b1dce4b | 567 | EOF |
1b1dce4b | 568 | |
376ccb8c | 569 | has_action "$TODO" || |
c54b7817 | 570 | die_abort "Nothing to do" |
1b1dce4b JS |
571 | |
572 | cp "$TODO" "$TODO".backup | |
ef0c2abf | 573 | git_editor "$TODO" || |
1b1dce4b JS |
574 | die "Could not execute editor" |
575 | ||
376ccb8c | 576 | has_action "$TODO" || |
c54b7817 JS |
577 | die_abort "Nothing to do" |
578 | ||
22e40795 | 579 | git update-ref ORIG_HEAD $HEAD |
dfa49f33 | 580 | output git checkout $ONTO && do_rest |
376ccb8c | 581 | ;; |
1b1dce4b JS |
582 | esac |
583 | shift | |
584 | done |