3 # Copyright (c) 2006 Johannes E. Schindelin
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 USAGE
='(--continue | --abort | --skip | [--preserve-merges] [--first-parent]
14 [--preserve-tags] [--verbose] [--onto <branch>] <upstream> [<branch>])'
20 DOTEST
="$GIT_DIR/.dotest-merge"
21 TODO
="$DOTEST"/git-rebase-todo
24 SQUASH_MSG
="$DOTEST"/message-squash
28 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
29 test -f "$DOTEST"/verbose
&& VERBOSE
=t
31 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
32 mark the corrected paths with 'git add <paths>', and
33 run 'git rebase --continue'"
34 export GIT_CHERRY_PICK_HELP
36 mark_prefix
=refs
/rebase-marks
/
47 test $status != 0 && printf "%s\n" "$output"
56 require_clean_work_tree
() {
57 # test if working tree is dirty
58 git rev-parse
--verify HEAD
> /dev
/null
&&
59 git update-index
--refresh &&
60 git diff-files
--quiet &&
61 git diff-index
--cached --quiet HEAD
-- ||
62 die
"Working tree is dirty"
65 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
67 comment_for_reflog
() {
68 case "$ORIG_REFLOG_ACTION" in
70 GIT_REFLOG_ACTION
="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
78 sed -e 1q
< "$TODO" >> "$DONE"
79 sed -e 1d
< "$TODO" >> "$TODO".new
80 mv -f "$TODO".new
"$TODO"
81 count
=$
(grep -c '^[^#]' < "$DONE")
82 total
=$
(($count+$
(grep -c '^[^#]' < "$TODO")))
83 if test "$last_count" != "$count"
86 printf "Rebasing (%d/%d)\r" $count $total
87 test -z "$VERBOSE" ||
echo
92 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
93 die
"Cannot get patch for $1^"
94 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
95 test -f "$DOTEST"/message ||
96 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
97 test -f "$DOTEST"/author-script ||
98 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
107 cleanup_before_quit
() {
108 for ref
in $
(git for-each-ref
--format='%(refname)' "${mark_prefix%/}")
110 git update-ref
-d "$ref" "$ref" ||
return 1
121 grep '^[^#]' "$1" >/dev
/null
128 eval "$(get_author_ident_from_commit $rm_sha1)"
129 msg
="$(git cat-file commit $rm_sha1 | sed -e '1,/^$/d')"
131 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
132 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
133 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
134 output git merge
$STRATEGY -m "$msg" "$@"
137 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
138 die Error redoing merge
$rm_sha1
145 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
146 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
147 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
148 die
"Could not get the parent of $sha1"
149 current_sha1
=$
(git rev-parse
--verify HEAD
)
150 if test "$no_ff$current_sha1" = "$parent_sha1"; then
151 output git
reset --hard $sha1
152 test "a$1" = a-n
&& output git
reset --soft $current_sha1
153 sha1
=$
(git rev-parse
--short $sha1)
154 output warn Fast forward to
$sha1
156 output git cherry-pick
"$@"
162 *1[0-9]|
*[04-9]) echo "$1"th
;;
169 make_squash_message
() {
170 if test -f "$SQUASH_MSG"; then
171 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
172 < "$SQUASH_MSG" |
sed -ne '$p')+1))
173 echo "# This is a combination of $COUNT commits."
174 sed -e 1d
-e '2,/^./{
179 echo "# This is a combination of two commits."
180 echo "# The first commit's message is:"
182 git cat-file commit HEAD |
sed -e '1,/^$/d'
185 echo "# This is the $(nth_string $COUNT) commit message:"
187 git cat-file commit
$1 |
sed -e '1,/^$/d'
190 peek_next_command
() {
191 sed -n "1s/ .*$//p" < "$TODO"
195 if expr "$1" : "^:[0-9][0-9]*$" >/dev
/null
197 echo "$mark_prefix$(printf %d ${1#:})"
204 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
205 "$DOTEST"/amend ||
exit
206 read command sha1 rest
< "$TODO"
212 comment_for_reflog pick
216 die_with_patch
$sha1 "Could not apply $sha1... $rest"
219 comment_for_reflog edit
223 die_with_patch
$sha1 "Could not apply $sha1... $rest"
227 warn
"You can amend the commit now, with"
229 warn
" git commit --amend"
231 warn
"Once you are satisfied with your changes, run"
233 warn
" git rebase --continue"
238 comment_for_reflog squash
240 has_action
"$DONE" ||
241 die
"Cannot 'squash' without a previous commit"
244 make_squash_message
$sha1 > "$MSG"
245 case "$(peek_next_command)" in
249 cp "$MSG" "$SQUASH_MSG"
254 rm -f "$SQUASH_MSG" ||
exit
259 author_script
=$
(get_author_ident_from_commit HEAD
)
260 output git
reset --soft HEAD^
261 pick_one
-n $sha1 || failed
=t
262 echo "$author_script" > "$DOTEST"/author-script
265 # This is like --amend, but with a different message
266 eval "$author_script"
267 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
268 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
269 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
270 $USE_OUTPUT git commit
--no-verify -F "$MSG" $EDIT_COMMIT || failed
=t
274 cp "$MSG" "$GIT_DIR"/MERGE_MSG
276 warn
"Could not apply $sha1... $rest"
277 die_with_patch
$sha1 ""
283 mark
=$
(mark_to_ref
:${sha1#:})
284 test :${sha1#:} = "$mark" && die
"Invalid mark '$sha1'"
286 git rev-parse
--verify "$mark" > /dev
/null
2>&1 && \
287 warn
"mark $sha1 already exist; overwriting it"
289 git update-ref
"$mark" HEAD || die
"update-ref failed"
292 comment_for_reflog merge
294 if ! git rev-parse
--verify $sha1 > /dev
/null
296 die
"Invalid reference merge '$sha1' in"
297 "$command $sha1 $rest"
303 new_parents
="$new_parents $(mark_to_ref $p)"
305 new_parents
="${new_parents# }"
306 test -n "$new_parents" || \
307 die
"You forgot to give the parents for the" \
308 "merge $sha1. Please fix it in $TODO"
311 redo_merge
$sha1 $new_parents
314 comment_for_reflog
reset
316 tmp
=$
(git rev-parse
--verify "$(mark_to_ref $sha1)") ||
317 die
"Invalid parent '$sha1' in $command $sha1 $rest"
320 output git
reset --hard $tmp
323 comment_for_reflog tag
326 output git tag
-f "$sha1"
329 warn
"Unknown command: $command $sha1 $rest"
330 die_with_patch
$sha1 "Please fix this in the file $TODO."
333 test -s "$TODO" && return
335 comment_for_reflog finish
&&
336 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
337 OLDHEAD
=$
(cat "$DOTEST"/head) &&
338 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
339 NEWHEAD
=$
(git rev-parse HEAD
) &&
342 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
343 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
344 git symbolic-ref HEAD
$HEADNAME
347 test ! -f "$DOTEST"/verbose ||
348 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
350 cleanup_before_quit
&&
352 warn
"Successfully rebased and updated $HEADNAME."
364 get_value_from_list
() {
365 # args: "key" " key1#value1 key2#value2"
369 echo "${stm_tmp%% *}"
378 insert_value_at_key_into_list
() {
379 # args: "value" "key" " key1#value1 key2#value2"
394 create_extended_todo_list
() {
396 if test t
= "${PRESERVE_TAGS:-}"
398 tag_list
=$
(git show-ref
--abbrev=7 --tags | \
402 tag
=${tag#refs/tags/}
403 if test ${last_sha1:-0000} = $sha1
405 saved_tags
="$saved_tags:$tag"
407 printf "%s" "${last_sha1:+ $last_sha1#$saved_tags}"
412 echo "${last_sha1:+ $last_sha1:$saved_tags}"
417 while IFS
=_
read commit parents subject
419 if test t
= "$PRESERVE_MERGES" -a \
420 "${last_parent:-$commit}" != "$commit"
422 if test t
= "${delayed_mark:-f}"
424 marked_commits
=$
(insert_value_at_key_into_list \
425 dummy
$last_parent "${marked_commits:-}")
428 test "$last_parent" = $SHORTUPSTREAM && \
429 last_parent
=$SHORTONTO
430 echo "reset $last_parent"
432 last_parent
="${parents%% *}"
434 get_value_from_list
$commit "${marked_commits:-}" \
435 >/dev
/null
&& echo mark
437 if tmp
=$
(get_value_from_list
$commit "$tag_list")
439 for t
in $
(echo $tmp |
tr : ' ')
449 for p
in ${parents#* }
451 marked_commits
=$
(insert_value_at_key_into_list \
452 dummy
"$p" "${marked_commits:-}")
453 if test "$p" = $SHORTUPSTREAM
455 new_parents
="$new_parents $SHORTONTO"
457 new_parents
="$new_parents $p"
461 echo merge
$commit $new_parents
465 echo "pick $commit $subject"
469 test -n "${last_parent:-}" -a "${last_parent:-}" != $SHORTUPSTREAM && \
470 echo reset $last_parent
472 perl
-e 'print reverse <>' | \
475 : ${commit_mark_list:=} ${last_commit:=000}
478 last_commit
="${args%% *}"
482 if commit_mark_list
=$
(insert_value_at_key_into_list \
483 $next_mark $last_commit "$commit_mark_list")
486 next_mark
=$
(($next_mark + 1))
488 die
"Internal error: two marks for" \
493 if tmp
=$
(get_value_from_list
$args "$commit_mark_list")
502 if tmp
=$
(get_value_from_list
$i \
505 new_args
="$new_args :$tmp"
507 new_args
="$new_args $i"
510 last_commit
="${args%% *}"
511 args
="$last_commit ${new_args# }"
522 comment_for_reflog
continue
524 test -d "$DOTEST" || die
"No interactive rebase running"
527 git rev-parse
--verify HEAD
>/dev
/null ||
528 die
"Cannot read HEAD"
529 git update-index
--refresh && git diff-files
--quiet ||
530 die
"Working tree is dirty"
532 # do we have anything to commit?
533 if git diff-index
--cached --quiet HEAD
--
535 : Nothing to commit
-- skip this
537 .
"$DOTEST"/author-script ||
538 die
"Cannot find the author identity"
539 if test -f "$DOTEST"/amend
541 git
reset --soft HEAD^ ||
542 die
"Cannot rewind the HEAD"
544 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
&&
545 git commit
--no-verify -F "$DOTEST"/message
-e ||
546 die
"Could not commit staged changes."
549 require_clean_work_tree
553 comment_for_reflog abort
556 test -d "$DOTEST" || die
"No interactive rebase running"
558 HEADNAME
=$
(cat "$DOTEST"/head-name
)
559 HEAD
=$
(cat "$DOTEST"/head)
562 git symbolic-ref HEAD
$HEADNAME
565 output git
reset --hard $HEAD &&
570 comment_for_reflog skip
573 test -d "$DOTEST" || die
"No interactive rebase running"
575 output git
reset --hard && do_rest
580 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
589 # we use merge anyway
592 die
"Interactive rebase uses merge, so $1 does not make sense"
597 -p|
--preserve-merges)
615 die
"Interactive rebase already started"
617 git var GIT_COMMITTER_IDENT
>/dev
/null ||
618 die
"You need to set your committer info first"
620 comment_for_reflog start
625 ONTO
=$
(git rev-parse
--verify "$2") ||
626 die
"Does not point to a valid commit: $2"
631 require_clean_work_tree
635 output git show-ref
--verify --quiet "refs/heads/$2" ||
636 die
"Invalid branchname: $2"
637 output git checkout
"$2" ||
638 die
"Could not checkout $2"
641 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
642 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
644 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
646 test -z "$ONTO" && ONTO
=$UPSTREAM
648 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
649 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
650 echo "detached HEAD" > "$DOTEST"/head-name
652 echo $HEAD > "$DOTEST"/head
653 echo $UPSTREAM > "$DOTEST"/upstream
654 echo $ONTO > "$DOTEST"/onto
655 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
656 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
658 SHORTUPSTREAM
=$
(git rev-parse
--short=7 $UPSTREAM)
659 SHORTHEAD
=$
(git rev-parse
--short=7 $HEAD)
660 SHORTONTO
=$
(git rev-parse
--short=7 $ONTO)
661 common_rev_list_opts
="--abbrev-commit --abbrev=7
662 --left-right --cherry-pick $UPSTREAM...$HEAD"
663 if test t
= "$PRESERVE_MERGES" -o t
= "${FIRST_PARENT:-f}" \
664 -o t
= "${PRESERVE_TAGS:-}"
667 test t
= "${FIRST_PARENT:-f}" && \
668 opts
="$opts --first-parent"
669 test t
!= "$PRESERVE_MERGES" && \
670 opts
="$opts --no-merges"
671 git rev-list
--pretty='format:%h_%p_%s' --topo-order \
672 $opts $common_rev_list_opts | \
674 create_extended_todo_list
676 git rev-list
--no-merges --reverse --pretty=oneline \
677 $common_rev_list_opts |
sed -n "s/^>/pick /p"
680 cat >> "$TODO" << EOF
682 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
684 # In the todo insn whenever you need to refer to a commit, in addition
685 # to the usual commit object name, you can use ':mark' syntax to refer
686 # to a commit previously marked with the 'mark' insn.
690 # edit = use commit, but stop for amending
691 # squash = use commit, but meld into previous commit
692 # mark :mark = mark the current HEAD for later reference
693 # reset commit = reset HEAD to the commit
694 # merge commit-M commit-P ... = redo merge commit-M with the
695 # current HEAD and the parents commit-P
696 # tag = reset tag to the current HEAD
698 # If you remove a line here THAT COMMIT WILL BE LOST.
699 # However, if you remove everything, the rebase will be aborted.
703 has_action
"$TODO" ||
704 die_abort
"Nothing to do"
706 cp "$TODO" "$TODO".backup
707 git_editor
"$TODO" ||
708 die
"Could not execute editor"
710 has_action
"$TODO" ||
711 die_abort
"Nothing to do"
713 output git checkout
$ONTO && do_rest