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] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
20 DOTEST
="$GIT_DIR/.dotest-merge"
21 TODO
="$DOTEST"/git-rebase-todo
24 SQUASH_MSG
="$DOTEST"/message-squash
25 REWRITTEN
="$DOTEST"/rewritten
29 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
30 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
31 test -f "$DOTEST"/verbose
&& VERBOSE
=t
42 test $status != 0 && printf "%s\n" "$output"
51 require_clean_work_tree
() {
52 # test if working tree is dirty
53 git rev-parse
--verify HEAD
> /dev
/null
&&
54 git update-index
--refresh &&
55 git diff-files
--quiet &&
56 git diff-index
--cached --quiet HEAD
-- ||
57 die
"Working tree is dirty"
60 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
62 comment_for_reflog
() {
63 case "$ORIG_REFLOG_ACTION" in
65 GIT_REFLOG_ACTION
="rebase -i ($1)"
66 export GIT_REFLOG_ACTION
72 sed -e 1q
< "$TODO" >> "$DONE"
73 sed -e 1d
< "$TODO" >> "$TODO".new
74 mv -f "$TODO".new
"$TODO"
75 count
=$
(($
(grep -ve '^$' -e '^#' < "$DONE" |
wc -l)))
76 total
=$
(($count+$
(grep -ve '^$' -e '^#' < "$TODO" |
wc -l)))
77 printf "Rebasing (%d/%d)\r" $count $total
78 test -z "$VERBOSE" ||
echo
82 parent_sha1
=$
(git rev-parse
--verify "$1"^
) ||
83 die
"Cannot get patch for $1^"
84 git diff-tree
-p "$parent_sha1"..
"$1" > "$DOTEST"/patch
85 test -f "$DOTEST"/message ||
86 git cat-file commit
"$1" |
sed "1,/^$/d" > "$DOTEST"/message
87 test -f "$DOTEST"/author-script ||
88 get_author_ident_from_commit
"$1" > "$DOTEST"/author-script
103 grep -vqe '^$' -e '^#' "$1"
108 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
109 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
110 test -d "$REWRITTEN" &&
111 pick_one_preserving_merges
"$@" && return
112 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
113 die
"Could not get the parent of $sha1"
114 current_sha1
=$
(git rev-parse
--verify HEAD
)
115 if test "$no_ff$current_sha1" = "$parent_sha1"; then
116 output git
reset --hard $sha1
117 test "a$1" = a-n
&& output git
reset --soft $current_sha1
118 sha1
=$
(git rev-parse
--short $sha1)
119 output warn Fast forward to
$sha1
121 output git cherry-pick
"$@"
125 pick_one_preserving_merges
() {
126 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
127 sha1
=$
(git rev-parse
$sha1)
129 if test -f "$DOTEST"/current-commit
131 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
132 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
133 rm "$DOTEST"/current-commit ||
134 die
"Cannot write current commit's replacement sha1"
137 # rewrite parents; if none were rewritten, we can fast-forward.
141 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -f2-)
143 if test -f "$REWRITTEN"/$p
146 new_p
=$
(cat "$REWRITTEN"/$p)
147 test $p != $new_p && fast_forward
=f
148 case "$new_parents" in
150 ;; # do nothing; that parent is already there
152 new_parents
="$new_parents $new_p"
157 case $fast_forward in
159 output warn
"Fast forward to $sha1"
160 test $preserve = f ||
echo $sha1 > "$REWRITTEN"/$sha1
163 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
165 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
166 # detach HEAD to current parent
167 output git checkout
$first_parent 2> /dev
/null ||
168 die
"Cannot move HEAD to $first_parent"
170 echo $sha1 > "$DOTEST"/current-commit
171 case "$new_parents" in
174 author_script
=$
(get_author_ident_from_commit
$sha1)
175 eval "$author_script"
176 msg
="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
177 # No point in merging the first parent, that's HEAD
178 new_parents
=${new_parents# $first_parent}
179 if ! GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
180 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
181 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
182 output git merge
$STRATEGY -m "$msg" \
186 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
187 die Error redoing merge
$sha1
191 output git cherry-pick
"$@" ||
192 die_with_patch
$sha1 "Could not pick $sha1"
201 *1[0-9]|
*[04-9]) echo "$1"th
;;
208 make_squash_message
() {
209 if test -f "$SQUASH_MSG"; then
210 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
211 < "$SQUASH_MSG" |
tail -n 1)+1))
212 echo "# This is a combination of $COUNT commits."
213 sed -n "2,\$p" < "$SQUASH_MSG"
216 echo "# This is a combination of two commits."
217 echo "# The first commit's message is:"
219 git cat-file commit HEAD |
sed -e '1,/^$/d'
222 echo "# This is the $(nth_string $COUNT) commit message:"
224 git cat-file commit
$1 |
sed -e '1,/^$/d'
227 peek_next_command
() {
228 sed -n "1s/ .*$//p" < "$TODO"
232 rm -f "$DOTEST"/message
"$DOTEST"/author-script \
233 "$DOTEST"/amend ||
exit
234 read command sha1 rest
< "$TODO"
240 comment_for_reflog pick
244 die_with_patch
$sha1 "Could not apply $sha1... $rest"
247 comment_for_reflog edit
251 die_with_patch
$sha1 "Could not apply $sha1... $rest"
255 warn
"You can amend the commit now, with"
257 warn
" git commit --amend"
262 comment_for_reflog squash
264 has_action
"$DONE" ||
265 die
"Cannot 'squash' without a previous commit"
268 make_squash_message
$sha1 > "$MSG"
269 case "$(peek_next_command)" in
273 cp "$MSG" "$SQUASH_MSG"
278 rm -f "$SQUASH_MSG" ||
exit
283 author_script
=$
(get_author_ident_from_commit HEAD
)
284 output git
reset --soft HEAD^
285 pick_one
-n $sha1 || failed
=t
286 echo "$author_script" > "$DOTEST"/author-script
289 # This is like --amend, but with a different message
290 eval "$author_script"
291 GIT_AUTHOR_NAME
="$GIT_AUTHOR_NAME" \
292 GIT_AUTHOR_EMAIL
="$GIT_AUTHOR_EMAIL" \
293 GIT_AUTHOR_DATE
="$GIT_AUTHOR_DATE" \
294 $USE_OUTPUT git commit
-F "$MSG" $EDIT_COMMIT
297 cp "$MSG" "$GIT_DIR"/MERGE_MSG
299 warn
"Could not apply $sha1... $rest"
300 die_with_patch
$sha1 ""
305 warn
"Unknown command: $command $sha1 $rest"
306 die_with_patch
$sha1 "Please fix this in the file $TODO."
309 test -s "$TODO" && return
311 comment_for_reflog finish
&&
312 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
313 OLDHEAD
=$
(cat "$DOTEST"/head) &&
314 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
315 if test -d "$REWRITTEN"
317 test -f "$DOTEST"/current-commit
&&
318 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
319 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
320 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
322 NEWHEAD
=$
(git rev-parse HEAD
)
326 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
327 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
328 git symbolic-ref HEAD
$HEADNAME
331 test ! -f "$DOTEST"/verbose ||
332 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
336 warn
"Successfully rebased and updated $HEADNAME."
352 comment_for_reflog
continue
354 test -d "$DOTEST" || die
"No interactive rebase running"
356 # commit if necessary
357 git rev-parse
--verify HEAD
> /dev
/null
&&
358 git update-index
--refresh &&
359 git diff-files
--quiet &&
360 ! git diff-index
--cached --quiet HEAD
-- &&
361 .
"$DOTEST"/author-script
&& {
362 test ! -f "$DOTEST"/amend || git
reset --soft HEAD^
364 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE
&&
365 git commit
-F "$DOTEST"/message
-e
367 require_clean_work_tree
371 comment_for_reflog abort
374 test -d "$DOTEST" || die
"No interactive rebase running"
376 HEADNAME
=$
(cat "$DOTEST"/head-name
)
377 HEAD
=$
(cat "$DOTEST"/head)
380 git symbolic-ref HEAD
$HEADNAME
383 output git
reset --hard $HEAD &&
388 comment_for_reflog skip
391 test -d "$DOTEST" || die
"No interactive rebase running"
393 output git
reset --hard && do_rest
398 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
407 # we use merge anyway
410 die
"Interactive rebase uses merge, so $1 does not make sense"
415 -p|
--preserve-merges)
426 die
"Interactive rebase already started"
428 git var GIT_COMMITTER_IDENT
>/dev
/null ||
429 die
"You need to set your committer info first"
431 comment_for_reflog start
436 ONTO
=$
(git rev-parse
--verify "$2") ||
437 die
"Does not point to a valid commit: $2"
442 require_clean_work_tree
446 output git show-ref
--verify --quiet "refs/heads/$2" ||
447 die
"Invalid branchname: $2"
448 output git checkout
"$2" ||
449 die
"Could not checkout $2"
452 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
453 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
455 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
457 test -z "$ONTO" && ONTO
=$UPSTREAM
459 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
460 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
461 echo "detached HEAD" > "$DOTEST"/head-name
463 echo $HEAD > "$DOTEST"/head
464 echo $UPSTREAM > "$DOTEST"/upstream
465 echo $ONTO > "$DOTEST"/onto
466 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
467 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
468 if test t
= "$PRESERVE_MERGES"
470 # $REWRITTEN contains files for each commit that is
471 # reachable by at least one merge base of $HEAD and
472 # $UPSTREAM. They are not necessarily rewritten, but
473 # their children might be.
474 # This ensures that commits on merged, but otherwise
475 # unrelated side branches are left alone. (Think "X"
476 # in the man page's example.)
477 mkdir
"$REWRITTEN" &&
478 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
480 echo $ONTO > "$REWRITTEN"/$c ||
481 die
"Could not init rewritten commits"
485 MERGES_OPTION
=--no-merges
488 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
489 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
490 SHORTONTO
=$
(git rev-parse
--short $ONTO)
491 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
492 --abbrev=7 --reverse --left-right --cherry-pick \
493 $UPSTREAM...
$HEAD | \
494 sed -n "s/^>/pick /p" > "$TODO"
495 cat >> "$TODO" << EOF
497 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
501 # edit = use commit, but stop for amending
502 # squash = use commit, but meld into previous commit
504 # If you remove a line here THAT COMMIT WILL BE LOST.
505 # However, if you remove everything, the rebase will be aborted.
509 has_action
"$TODO" ||
510 die_abort
"Nothing to do"
512 cp "$TODO" "$TODO".backup
513 git_editor
"$TODO" ||
514 die
"Could not execute editor"
516 has_action
"$TODO" ||
517 die_abort
"Nothing to do"
519 output git checkout
$ONTO && do_rest