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>])'
19 DOTEST="$GIT_DIR/.dotest-merge"
20 TODO="$DOTEST"/git-rebase-todo
23 SQUASH_MSG="$DOTEST"/message-squash
24 REWRITTEN="$DOTEST"/rewritten
28 test -d "$REWRITTEN" && PRESERVE_MERGES=t
29 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
30 test -f "$DOTEST"/verbose && VERBOSE=t
41 test $status != 0 && printf "%s\n" "$output"
50 require_clean_work_tree () {
51 # test if working tree is dirty
52 git rev-parse --verify HEAD > /dev/null &&
53 git update-index --refresh &&
54 git diff-files --quiet &&
55 git diff-index --cached --quiet HEAD ||
56 die "Working tree is dirty"
59 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
61 comment_for_reflog () {
62 case "$ORIG_REFLOG_ACTION" in
64 GIT_REFLOG_ACTION="rebase -i ($1)"
65 export GIT_REFLOG_ACTION
71 sed -e 1q < "$TODO" >> "$DONE"
72 sed -e 1d < "$TODO" >> "$TODO".new
73 mv -f "$TODO".new "$TODO"
74 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
75 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
76 printf "Rebasing (%d/%d)\r" $count $total
77 test -z "$VERBOSE" || echo
81 parent_sha1=$(git rev-parse --verify "$1"^) ||
82 die "Cannot get patch for $1^"
83 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
84 test -f "$DOTEST"/message ||
85 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
86 test -f "$DOTEST"/author-script ||
87 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
101 grep -vqe '^$' -e '^#' "$1"
106 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
107 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
108 test -d "$REWRITTEN" &&
109 pick_one_preserving_merges "$@" && return
110 parent_sha1=$(git rev-parse --verify $sha1^) ||
111 die "Could not get the parent of $sha1"
112 current_sha1=$(git rev-parse --verify HEAD)
113 if test "$no_ff$current_sha1" = "$parent_sha1"; then
114 output git reset --hard $sha1
115 test "a$1" = a-n && output git reset --soft $current_sha1
116 sha1=$(git rev-parse --short $sha1)
117 output warn Fast forward to $sha1
119 output git cherry-pick $STRATEGY "$@"
123 pick_one_preserving_merges () {
124 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
125 sha1=$(git rev-parse $sha1)
127 if test -f "$DOTEST"/current-commit
129 current_commit=$(cat "$DOTEST"/current-commit) &&
130 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
131 rm "$DOTEST"/current-commit ||
132 die "Cannot write current commit's replacement sha1"
135 # rewrite parents; if none were rewritten, we can fast-forward.
139 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
141 if test -f "$REWRITTEN"/$p
144 new_p=$(cat "$REWRITTEN"/$p)
145 test $p != $new_p && fast_forward=f
146 case "$new_parents" in
148 ;; # do nothing; that parent is already there
150 new_parents="$new_parents $new_p"
155 case $fast_forward in
157 output warn "Fast forward to $sha1"
158 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
161 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
163 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
164 # detach HEAD to current parent
165 output git checkout $first_parent 2> /dev/null ||
166 die "Cannot move HEAD to $first_parent"
168 echo $sha1 > "$DOTEST"/current-commit
169 case "$new_parents" in
172 author_script=$(get_author_ident_from_commit $sha1)
173 eval "$author_script"
174 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
175 # NEEDSWORK: give rerere a chance
176 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
177 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
178 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
179 output git merge $STRATEGY -m "$msg" \
182 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
183 die Error redoing merge $sha1
187 output git cherry-pick $STRATEGY "$@" ||
188 die_with_patch $sha1 "Could not pick $sha1"
197 *1[0-9]|*[04-9]) echo "$1"th;;
204 make_squash_message () {
205 if test -f "$SQUASH_MSG"; then
206 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
207 < "$SQUASH_MSG" | tail -n 1)+1))
208 echo "# This is a combination of $COUNT commits."
209 sed -n "2,\$p" < "$SQUASH_MSG"
212 echo "# This is a combination of two commits."
213 echo "# The first commit's message is:"
215 git cat-file commit HEAD | sed -e '1,/^$/d'
218 echo "# This is the $(nth_string $COUNT) commit message:"
220 git cat-file commit $1 | sed -e '1,/^$/d'
223 peek_next_command () {
224 sed -n "1s/ .*$//p" < "$TODO"
228 rm -f "$DOTEST"/message "$DOTEST"/author-script \
229 "$DOTEST"/amend || exit
230 read command sha1 rest < "$TODO"
236 comment_for_reflog pick
240 die_with_patch $sha1 "Could not apply $sha1... $rest"
243 comment_for_reflog edit
247 die_with_patch $sha1 "Could not apply $sha1... $rest"
251 warn "You can amend the commit now, with"
253 warn " git commit --amend"
258 comment_for_reflog squash
260 has_action "$DONE" ||
261 die "Cannot 'squash' without a previous commit"
264 make_squash_message $sha1 > "$MSG"
265 case "$(peek_next_command)" in
269 cp "$MSG" "$SQUASH_MSG"
274 rm -f "$SQUASH_MSG" || exit
279 author_script=$(get_author_ident_from_commit HEAD)
280 output git reset --soft HEAD^
281 pick_one -n $sha1 || failed=t
282 echo "$author_script" > "$DOTEST"/author-script
285 # This is like --amend, but with a different message
286 eval "$author_script"
287 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
288 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
289 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
290 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
293 cp "$MSG" "$GIT_DIR"/MERGE_MSG
295 warn "Could not apply $sha1... $rest"
296 die_with_patch $sha1 ""
301 warn "Unknown command: $command $sha1 $rest"
302 die_with_patch $sha1 "Please fix this in the file $TODO."
305 test -s "$TODO" && return
307 comment_for_reflog finish &&
308 HEADNAME=$(cat "$DOTEST"/head-name) &&
309 OLDHEAD=$(cat "$DOTEST"/head) &&
310 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
311 if test -d "$REWRITTEN"
313 test -f "$DOTEST"/current-commit &&
314 current_commit=$(cat "$DOTEST"/current-commit) &&
315 git rev-parse HEAD > "$REWRITTEN"/$current_commit
316 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
318 NEWHEAD=$(git rev-parse HEAD)
322 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
323 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
324 git symbolic-ref HEAD $HEADNAME
327 test ! -f "$DOTEST"/verbose ||
328 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
332 warn "Successfully rebased and updated $HEADNAME."
348 comment_for_reflog continue
350 test -d "$DOTEST" || die "No interactive rebase running"
352 # commit if necessary
353 git rev-parse --verify HEAD > /dev/null &&
354 git update-index --refresh &&
355 git diff-files --quiet &&
356 ! git diff-index --cached --quiet HEAD &&
357 . "$DOTEST"/author-script && {
358 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
360 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
361 git commit -F "$DOTEST"/message -e
363 require_clean_work_tree
367 comment_for_reflog abort
369 test -d "$DOTEST" || die "No interactive rebase running"
371 HEADNAME=$(cat "$DOTEST"/head-name)
372 HEAD=$(cat "$DOTEST"/head)
375 git symbolic-ref HEAD $HEADNAME
378 output git reset --hard $HEAD &&
383 comment_for_reflog skip
385 test -d "$DOTEST" || die "No interactive rebase running"
387 output git reset --hard && do_rest
393 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
402 # we use merge anyway
405 die "Interactive rebase uses merge, so $1 does not make sense"
410 -p|--preserve-merges)
421 die "Interactive rebase already started"
423 git var GIT_COMMITTER_IDENT >/dev/null ||
424 die "You need to set your committer info first"
426 comment_for_reflog start
431 ONTO=$(git rev-parse --verify "$2") ||
432 die "Does not point to a valid commit: $2"
437 require_clean_work_tree
441 output git show-ref --verify --quiet "refs/heads/$2" ||
442 die "Invalid branchname: $2"
443 output git checkout "$2" ||
444 die "Could not checkout $2"
447 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
448 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
450 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
452 test -z "$ONTO" && ONTO=$UPSTREAM
454 : > "$DOTEST"/interactive || die "Could not mark as interactive"
455 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
456 echo "detached HEAD" > "$DOTEST"/head-name
458 echo $HEAD > "$DOTEST"/head
459 echo $UPSTREAM > "$DOTEST"/upstream
460 echo $ONTO > "$DOTEST"/onto
461 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
462 test t = "$VERBOSE" && : > "$DOTEST"/verbose
463 if test t = "$PRESERVE_MERGES"
465 # $REWRITTEN contains files for each commit that is
466 # reachable by at least one merge base of $HEAD and
467 # $UPSTREAM. They are not necessarily rewritten, but
468 # their children might be.
469 # This ensures that commits on merged, but otherwise
470 # unrelated side branches are left alone. (Think "X"
471 # in the man page's example.)
472 mkdir "$REWRITTEN" &&
473 for c in $(git merge-base --all $HEAD $UPSTREAM)
475 echo $ONTO > "$REWRITTEN"/$c ||
476 die "Could not init rewritten commits"
480 MERGES_OPTION=--no-merges
483 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
484 SHORTHEAD=$(git rev-parse --short $HEAD)
485 SHORTONTO=$(git rev-parse --short $ONTO)
487 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
491 # edit = use commit, but stop for amending
492 # squash = use commit, but meld into previous commit
494 # If you remove a line here THAT COMMIT WILL BE LOST.
497 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
498 --abbrev=7 --reverse --left-right --cherry-pick \
499 $UPSTREAM...$HEAD | \
500 sed -n "s/^>/pick /p" >> "$TODO"
502 has_action "$TODO" ||
503 die_abort "Nothing to do"
505 cp "$TODO" "$TODO".backup
506 git_editor "$TODO" ||
507 die "Could not execute editor"
509 has_action "$TODO" ||
510 die_abort "Nothing to do"
512 output git checkout $ONTO && do_rest