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
13OPTIONS_KEEPDASHDASH=
14OPTIONS_SPEC="\
15git-rebase [-i] [options] [--] <upstream> [<branch>]
16git-rebase [-i] (--continue | --abort | --skip)
17--
18 Available options are
19v,verbose display a diffstat of what changed upstream
20onto= rebase onto given branch instead of upstream
21p,preserve-merges try to recreate merges instead of ignoring them
22s,strategy= use the given merge strategy
23m,merge always used (no-op)
24i,interactive always used (no-op)
25 Actions:
26continue continue rebasing process
27abort abort rebasing process and restore original branch
28skip skip current patch and continue rebasing process
29no-verify override pre-rebase hook from stopping the operation
30"
31
32. git-sh-setup
33require_work_tree
34
35DOTEST="$GIT_DIR/rebase-merge"
36TODO="$DOTEST"/git-rebase-todo
37DONE="$DOTEST"/done
38MSG="$DOTEST"/message
39SQUASH_MSG="$DOTEST"/message-squash
40REWRITTEN="$DOTEST"/rewritten
41DROPPED="$DOTEST"/dropped
42PRESERVE_MERGES=
43STRATEGY=
44ONTO=
45VERBOSE=
46OK_TO_SKIP_PRE_REBASE=
47
48GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
49mark the corrected paths with 'git add <paths>', and
50run 'git rebase --continue'"
51export GIT_CHERRY_PICK_HELP
52
53warn () {
54 echo "$*" >&2
55}
56
57output () {
58 case "$VERBOSE" in
59 '')
60 output=$("$@" 2>&1 )
61 status=$?
62 test $status != 0 && printf "%s\n" "$output"
63 return $status
64 ;;
65 *)
66 "$@"
67 ;;
68 esac
69}
70
71run_pre_rebase_hook () {
72 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
73 test -x "$GIT_DIR/hooks/pre-rebase"
74 then
75 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
76 echo >&2 "The pre-rebase hook refused to rebase."
77 exit 1
78 }
79 fi
80}
81
82require_clean_work_tree () {
83 # test if working tree is dirty
84 git rev-parse --verify HEAD > /dev/null &&
85 git update-index --ignore-submodules --refresh &&
86 git diff-files --quiet --ignore-submodules &&
87 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
88 die "Working tree is dirty"
89}
90
91ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
92
93comment_for_reflog () {
94 case "$ORIG_REFLOG_ACTION" in
95 ''|rebase*)
96 GIT_REFLOG_ACTION="rebase -i ($1)"
97 export GIT_REFLOG_ACTION
98 ;;
99 esac
100}
101
102last_count=
103mark_action_done () {
104 sed -e 1q < "$TODO" >> "$DONE"
105 sed -e 1d < "$TODO" >> "$TODO".new
106 mv -f "$TODO".new "$TODO"
107 count=$(grep -c '^[^#]' < "$DONE")
108 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
109 if test "$last_count" != "$count"
110 then
111 last_count=$count
112 printf "Rebasing (%d/%d)\r" $count $total
113 test -z "$VERBOSE" || echo
114 fi
115}
116
117make_patch () {
118 sha1_and_parents="$(git rev-list --parents -1 "$1")"
119 case "$sha1_and_parents" in
120 ?*' '?*' '?*)
121 git diff --cc $sha1_and_parents
122 ;;
123 ?*' '?*)
124 git diff-tree -p "$1^!"
125 ;;
126 *)
127 echo "Root commit"
128 ;;
129 esac > "$DOTEST"/patch
130 test -f "$DOTEST"/message ||
131 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
132 test -f "$DOTEST"/author-script ||
133 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
134}
135
136die_with_patch () {
137 make_patch "$1"
138 git rerere
139 die "$2"
140}
141
142die_abort () {
143 rm -rf "$DOTEST"
144 die "$1"
145}
146
147has_action () {
148 grep '^[^#]' "$1" >/dev/null
149}
150
151pick_one () {
152 no_ff=
153 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
154 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
155 test -d "$REWRITTEN" &&
156 pick_one_preserving_merges "$@" && return
157 parent_sha1=$(git rev-parse --verify $sha1^) ||
158 die "Could not get the parent of $sha1"
159 current_sha1=$(git rev-parse --verify HEAD)
160 if test "$no_ff$current_sha1" = "$parent_sha1"; then
161 output git reset --hard $sha1
162 test "a$1" = a-n && output git reset --soft $current_sha1
163 sha1=$(git rev-parse --short $sha1)
164 output warn Fast forward to $sha1
165 else
166 output git cherry-pick "$@"
167 fi
168}
169
170pick_one_preserving_merges () {
171 fast_forward=t
172 case "$1" in
173 -n)
174 fast_forward=f
175 sha1=$2
176 ;;
177 *)
178 sha1=$1
179 ;;
180 esac
181 sha1=$(git rev-parse $sha1)
182
183 if test -f "$DOTEST"/current-commit
184 then
185 if test "$fast_forward" = t
186 then
187 cat "$DOTEST"/current-commit | while read current_commit
188 do
189 git rev-parse HEAD > "$REWRITTEN"/$current_commit
190 done
191 rm "$DOTEST"/current-commit ||
192 die "Cannot write current commit's replacement sha1"
193 fi
194 fi
195
196 echo $sha1 >> "$DOTEST"/current-commit
197
198 # rewrite parents; if none were rewritten, we can fast-forward.
199 new_parents=
200 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
201 while [ "$pend" != "" ]
202 do
203 p=$(expr "$pend" : ' \([^ ]*\)')
204 pend="${pend# $p}"
205
206 if test -f "$REWRITTEN"/$p
207 then
208 new_p=$(cat "$REWRITTEN"/$p)
209
210 # If the todo reordered commits, and our parent is marked for
211 # rewriting, but hasn't been gotten to yet, assume the user meant to
212 # drop it on top of the current HEAD
213 if test -z "$new_p"
214 then
215 new_p=$(git rev-parse HEAD)
216 fi
217
218 test $p != $new_p && fast_forward=f
219 case "$new_parents" in
220 *$new_p*)
221 ;; # do nothing; that parent is already there
222 *)
223 new_parents="$new_parents $new_p"
224 ;;
225 esac
226 else
227 if test -f "$DROPPED"/$p
228 then
229 fast_forward=f
230 pend=" $(cat "$DROPPED"/$p)$pend"
231 else
232 new_parents="$new_parents $p"
233 fi
234 fi
235 done
236 case $fast_forward in
237 t)
238 output warn "Fast forward to $sha1"
239 output git reset --hard $sha1 ||
240 die "Cannot fast forward to $sha1"
241 ;;
242 f)
243 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
244
245 if [ "$1" != "-n" ]
246 then
247 # detach HEAD to current parent
248 output git checkout $first_parent 2> /dev/null ||
249 die "Cannot move HEAD to $first_parent"
250 fi
251
252 case "$new_parents" in
253 ' '*' '*)
254 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
255
256 # redo merge
257 author_script=$(get_author_ident_from_commit $sha1)
258 eval "$author_script"
259 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
260 # No point in merging the first parent, that's HEAD
261 new_parents=${new_parents# $first_parent}
262 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
263 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
264 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
265 output git merge $STRATEGY -m "$msg" \
266 $new_parents
267 then
268 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
269 die_with_patch $sha1 "Error redoing merge $sha1"
270 fi
271 ;;
272 *)
273 output git cherry-pick "$@" ||
274 die_with_patch $sha1 "Could not pick $sha1"
275 ;;
276 esac
277 ;;
278 esac
279}
280
281nth_string () {
282 case "$1" in
283 *1[0-9]|*[04-9]) echo "$1"th;;
284 *1) echo "$1"st;;
285 *2) echo "$1"nd;;
286 *3) echo "$1"rd;;
287 esac
288}
289
290make_squash_message () {
291 if test -f "$SQUASH_MSG"; then
292 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
293 < "$SQUASH_MSG" | sed -ne '$p')+1))
294 echo "# This is a combination of $COUNT commits."
295 sed -e 1d -e '2,/^./{
296 /^$/d
297 }' <"$SQUASH_MSG"
298 else
299 COUNT=2
300 echo "# This is a combination of two commits."
301 echo "# The first commit's message is:"
302 echo
303 git cat-file commit HEAD | sed -e '1,/^$/d'
304 fi
305 echo
306 echo "# This is the $(nth_string $COUNT) commit message:"
307 echo
308 git cat-file commit $1 | sed -e '1,/^$/d'
309}
310
311peek_next_command () {
312 sed -n "1s/ .*$//p" < "$TODO"
313}
314
315do_next () {
316 rm -f "$DOTEST"/message "$DOTEST"/author-script \
317 "$DOTEST"/amend || exit
318 read command sha1 rest < "$TODO"
319 case "$command" in
320 '#'*|''|noop)
321 mark_action_done
322 ;;
323 pick|p)
324 comment_for_reflog pick
325
326 mark_action_done
327 pick_one $sha1 ||
328 die_with_patch $sha1 "Could not apply $sha1... $rest"
329 ;;
330 edit|e)
331 comment_for_reflog edit
332
333 mark_action_done
334 pick_one $sha1 ||
335 die_with_patch $sha1 "Could not apply $sha1... $rest"
336 make_patch $sha1
337 git rev-parse --verify HEAD > "$DOTEST"/amend
338 warn "Stopped at $sha1... $rest"
339 warn "You can amend the commit now, with"
340 warn
341 warn " git commit --amend"
342 warn
343 warn "Once you are satisfied with your changes, run"
344 warn
345 warn " git rebase --continue"
346 warn
347 exit 0
348 ;;
349 squash|s)
350 comment_for_reflog squash
351
352 test -f "$DONE" && has_action "$DONE" ||
353 die "Cannot 'squash' without a previous commit"
354
355 mark_action_done
356 make_squash_message $sha1 > "$MSG"
357 failed=f
358 author_script=$(get_author_ident_from_commit HEAD)
359 output git reset --soft HEAD^
360 pick_one -n $sha1 || failed=t
361 case "$(peek_next_command)" in
362 squash|s)
363 EDIT_COMMIT=
364 USE_OUTPUT=output
365 MSG_OPT=-F
366 MSG_FILE="$MSG"
367 cp "$MSG" "$SQUASH_MSG"
368 ;;
369 *)
370 EDIT_COMMIT=-e
371 USE_OUTPUT=
372 MSG_OPT=
373 MSG_FILE=
374 rm -f "$SQUASH_MSG" || exit
375 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
376 rm -f "$GIT_DIR"/MERGE_MSG || exit
377 ;;
378 esac
379 echo "$author_script" > "$DOTEST"/author-script
380 if test $failed = f
381 then
382 # This is like --amend, but with a different message
383 eval "$author_script"
384 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
385 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
386 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
387 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
388 fi
389 if test $failed = t
390 then
391 cp "$MSG" "$GIT_DIR"/MERGE_MSG
392 warn
393 warn "Could not apply $sha1... $rest"
394 die_with_patch $sha1 ""
395 fi
396 ;;
397 *)
398 warn "Unknown command: $command $sha1 $rest"
399 die_with_patch $sha1 "Please fix this in the file $TODO."
400 ;;
401 esac
402 test -s "$TODO" && return
403
404 comment_for_reflog finish &&
405 HEADNAME=$(cat "$DOTEST"/head-name) &&
406 OLDHEAD=$(cat "$DOTEST"/head) &&
407 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
408 NEWHEAD=$(git rev-parse HEAD) &&
409 case $HEADNAME in
410 refs/*)
411 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
412 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
413 git symbolic-ref HEAD $HEADNAME
414 ;;
415 esac && {
416 test ! -f "$DOTEST"/verbose ||
417 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
418 } &&
419 rm -rf "$DOTEST" &&
420 git gc --auto &&
421 warn "Successfully rebased and updated $HEADNAME."
422
423 exit
424}
425
426do_rest () {
427 while :
428 do
429 do_next
430 done
431}
432
433# check if no other options are set
434is_standalone () {
435 test $# -eq 2 -a "$2" = '--' &&
436 test -z "$ONTO" &&
437 test -z "$PRESERVE_MERGES" &&
438 test -z "$STRATEGY" &&
439 test -z "$VERBOSE"
440}
441
442get_saved_options () {
443 test -d "$REWRITTEN" && PRESERVE_MERGES=t
444 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
445 test -f "$DOTEST"/verbose && VERBOSE=t
446}
447
448while test $# != 0
449do
450 case "$1" in
451 --no-verify)
452 OK_TO_SKIP_PRE_REBASE=yes
453 ;;
454 --verify)
455 ;;
456 --continue)
457 is_standalone "$@" || usage
458 get_saved_options
459 comment_for_reflog continue
460
461 test -d "$DOTEST" || die "No interactive rebase running"
462
463 # Sanity check
464 git rev-parse --verify HEAD >/dev/null ||
465 die "Cannot read HEAD"
466 git update-index --ignore-submodules --refresh &&
467 git diff-files --quiet --ignore-submodules ||
468 die "Working tree is dirty"
469
470 # do we have anything to commit?
471 if git diff-index --cached --quiet --ignore-submodules HEAD --
472 then
473 : Nothing to commit -- skip this
474 else
475 . "$DOTEST"/author-script ||
476 die "Cannot find the author identity"
477 amend=
478 if test -f "$DOTEST"/amend
479 then
480 amend=$(git rev-parse --verify HEAD)
481 test "$amend" = $(cat "$DOTEST"/amend) ||
482 die "\
483You have uncommitted changes in your working tree. Please, commit them
484first and then run 'git rebase --continue' again."
485 git reset --soft HEAD^ ||
486 die "Cannot rewind the HEAD"
487 fi
488 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
489 git commit --no-verify -F "$DOTEST"/message -e || {
490 test -n "$amend" && git reset --soft $amend
491 die "Could not commit staged changes."
492 }
493 fi
494
495 require_clean_work_tree
496 do_rest
497 ;;
498 --abort)
499 is_standalone "$@" || usage
500 get_saved_options
501 comment_for_reflog abort
502
503 git rerere clear
504 test -d "$DOTEST" || die "No interactive rebase running"
505
506 HEADNAME=$(cat "$DOTEST"/head-name)
507 HEAD=$(cat "$DOTEST"/head)
508 case $HEADNAME in
509 refs/*)
510 git symbolic-ref HEAD $HEADNAME
511 ;;
512 esac &&
513 output git reset --hard $HEAD &&
514 rm -rf "$DOTEST"
515 exit
516 ;;
517 --skip)
518 is_standalone "$@" || usage
519 get_saved_options
520 comment_for_reflog skip
521
522 git rerere clear
523 test -d "$DOTEST" || die "No interactive rebase running"
524
525 output git reset --hard && do_rest
526 ;;
527 -s)
528 case "$#,$1" in
529 *,*=*)
530 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
531 1,*)
532 usage ;;
533 *)
534 STRATEGY="-s $2"
535 shift ;;
536 esac
537 ;;
538 -m)
539 # we use merge anyway
540 ;;
541 -v)
542 VERBOSE=t
543 ;;
544 -p)
545 PRESERVE_MERGES=t
546 ;;
547 -i)
548 # yeah, we know
549 ;;
550 --onto)
551 shift
552 ONTO=$(git rev-parse --verify "$1") ||
553 die "Does not point to a valid commit: $1"
554 ;;
555 --)
556 shift
557 run_pre_rebase_hook ${1+"$@"}
558 test $# -eq 1 -o $# -eq 2 || usage
559 test -d "$DOTEST" &&
560 die "Interactive rebase already started"
561
562 git var GIT_COMMITTER_IDENT >/dev/null ||
563 die "You need to set your committer info first"
564
565 comment_for_reflog start
566
567 require_clean_work_tree
568
569 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
570 test -z "$ONTO" && ONTO=$UPSTREAM
571
572 if test ! -z "$2"
573 then
574 output git show-ref --verify --quiet "refs/heads/$2" ||
575 die "Invalid branchname: $2"
576 output git checkout "$2" ||
577 die "Could not checkout $2"
578 fi
579
580 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
581 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
582
583 : > "$DOTEST"/interactive || die "Could not mark as interactive"
584 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
585 echo "detached HEAD" > "$DOTEST"/head-name
586
587 echo $HEAD > "$DOTEST"/head
588 echo $UPSTREAM > "$DOTEST"/upstream
589 echo $ONTO > "$DOTEST"/onto
590 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
591 test t = "$VERBOSE" && : > "$DOTEST"/verbose
592 if test t = "$PRESERVE_MERGES"
593 then
594 # $REWRITTEN contains files for each commit that is
595 # reachable by at least one merge base of $HEAD and
596 # $UPSTREAM. They are not necessarily rewritten, but
597 # their children might be.
598 # This ensures that commits on merged, but otherwise
599 # unrelated side branches are left alone. (Think "X"
600 # in the man page's example.)
601 mkdir "$REWRITTEN" &&
602 for c in $(git merge-base --all $HEAD $UPSTREAM)
603 do
604 echo $ONTO > "$REWRITTEN"/$c ||
605 die "Could not init rewritten commits"
606 done
607 # No cherry-pick because our first pass is to determine
608 # parents to rewrite and skipping dropped commits would
609 # prematurely end our probe
610 MERGES_OPTION=
611 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
612 else
613 MERGES_OPTION="--no-merges --cherry-pick"
614 fi
615
616 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
617 SHORTHEAD=$(git rev-parse --short $HEAD)
618 SHORTONTO=$(git rev-parse --short $ONTO)
619 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
620 --abbrev=7 --reverse --left-right --topo-order \
621 $UPSTREAM...$HEAD | \
622 sed -n "s/^>//p" | while read shortsha1 rest
623 do
624 if test t != "$PRESERVE_MERGES"
625 then
626 echo "pick $shortsha1 $rest" >> "$TODO"
627 else
628 sha1=$(git rev-parse $shortsha1)
629 preserve=t
630 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
631 do
632 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
633 then
634 preserve=f
635 fi
636 done
637 if test f = "$preserve"
638 then
639 touch "$REWRITTEN"/$sha1
640 echo "pick $shortsha1 $rest" >> "$TODO"
641 fi
642 fi
643 done
644
645 # Watch for commits that been dropped by --cherry-pick
646 if test t = "$PRESERVE_MERGES"
647 then
648 mkdir "$DROPPED"
649 # Save all non-cherry-picked changes
650 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
651 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
652 # Now all commits and note which ones are missing in
653 # not-cherry-picks and hence being dropped
654 git rev-list $UPSTREAM..$HEAD |
655 while read rev
656 do
657 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
658 then
659 # Use -f2 because if rev-list is telling us this commit is
660 # not worthwhile, we don't want to track its multiple heads,
661 # just the history of its first-parent for others that will
662 # be rebasing on top of it
663 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
664 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
665 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
666 rm "$REWRITTEN"/$rev
667 fi
668 done
669 fi
670 test -s "$TODO" || echo noop >> "$TODO"
671 cat >> "$TODO" << EOF
672
673# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
674#
675# Commands:
676# p, pick = use commit
677# e, edit = use commit, but stop for amending
678# s, squash = use commit, but meld into previous commit
679#
680# If you remove a line here THAT COMMIT WILL BE LOST.
681# However, if you remove everything, the rebase will be aborted.
682#
683EOF
684
685 has_action "$TODO" ||
686 die_abort "Nothing to do"
687
688 cp "$TODO" "$TODO".backup
689 git_editor "$TODO" ||
690 die "Could not execute editor"
691
692 has_action "$TODO" ||
693 die_abort "Nothing to do"
694
695 git update-ref ORIG_HEAD $HEAD
696 output git checkout $ONTO && do_rest
697 ;;
698 esac
699 shift
700done