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
102 grep -vqe '^$' -e '^#' "$1"
107 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
108 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
109 test -d "$REWRITTEN" &&
110 pick_one_preserving_merges "$@" && return
111 parent_sha1=$(git rev-parse --verify $sha1^) ||
112 die "Could not get the parent of $sha1"
113 current_sha1=$(git rev-parse --verify HEAD)
114 if test "$no_ff$current_sha1" = "$parent_sha1"; then
115 output git reset --hard $sha1
116 test "a$1" = a-n && output git reset --soft $current_sha1
117 sha1=$(git rev-parse --short $sha1)
118 output warn Fast forward to $sha1
120 output git cherry-pick "$@"
124 pick_one_preserving_merges () {
125 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
126 sha1=$(git rev-parse $sha1)
128 if test -f "$DOTEST"/current-commit
130 current_commit=$(cat "$DOTEST"/current-commit) &&
131 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
132 rm "$DOTEST"/current-commit ||
133 die "Cannot write current commit's replacement sha1"
136 # rewrite parents; if none were rewritten, we can fast-forward.
140 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
142 if test -f "$REWRITTEN"/$p
145 new_p=$(cat "$REWRITTEN"/$p)
146 test $p != $new_p && fast_forward=f
147 case "$new_parents" in
149 ;; # do nothing; that parent is already there
151 new_parents="$new_parents $new_p"
156 case $fast_forward in
158 output warn "Fast forward to $sha1"
159 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
162 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
164 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
165 # detach HEAD to current parent
166 output git checkout $first_parent 2> /dev/null ||
167 die "Cannot move HEAD to $first_parent"
169 echo $sha1 > "$DOTEST"/current-commit
170 case "$new_parents" in
173 author_script=$(get_author_ident_from_commit $sha1)
174 eval "$author_script"
175 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
176 # No point in merging the first parent, that's HEAD
177 new_parents=${new_parents# $first_parent}
178 # NEEDSWORK: give rerere a chance
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" \
185 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
186 die Error redoing merge $sha1
190 output git cherry-pick "$@" ||
191 die_with_patch $sha1 "Could not pick $sha1"
200 *1[0-9]|*[04-9]) echo "$1"th;;
207 make_squash_message () {
208 if test -f "$SQUASH_MSG"; then
209 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
210 < "$SQUASH_MSG" | tail -n 1)+1))
211 echo "# This is a combination of $COUNT commits."
212 sed -n "2,\$p" < "$SQUASH_MSG"
215 echo "# This is a combination of two commits."
216 echo "# The first commit's message is:"
218 git cat-file commit HEAD | sed -e '1,/^$/d'
221 echo "# This is the $(nth_string $COUNT) commit message:"
223 git cat-file commit $1 | sed -e '1,/^$/d'
226 peek_next_command () {
227 sed -n "1s/ .*$//p" < "$TODO"
231 rm -f "$DOTEST"/message "$DOTEST"/author-script \
232 "$DOTEST"/amend || exit
233 read command sha1 rest < "$TODO"
239 comment_for_reflog pick
243 die_with_patch $sha1 "Could not apply $sha1... $rest"
246 comment_for_reflog edit
250 die_with_patch $sha1 "Could not apply $sha1... $rest"
254 warn "You can amend the commit now, with"
256 warn " git commit --amend"
261 comment_for_reflog squash
263 has_action "$DONE" ||
264 die "Cannot 'squash' without a previous commit"
267 make_squash_message $sha1 > "$MSG"
268 case "$(peek_next_command)" in
272 cp "$MSG" "$SQUASH_MSG"
277 rm -f "$SQUASH_MSG" || exit
282 author_script=$(get_author_ident_from_commit HEAD)
283 output git reset --soft HEAD^
284 pick_one -n $sha1 || failed=t
285 echo "$author_script" > "$DOTEST"/author-script
288 # This is like --amend, but with a different message
289 eval "$author_script"
290 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
291 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
292 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
293 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
296 cp "$MSG" "$GIT_DIR"/MERGE_MSG
298 warn "Could not apply $sha1... $rest"
299 die_with_patch $sha1 ""
304 warn "Unknown command: $command $sha1 $rest"
305 die_with_patch $sha1 "Please fix this in the file $TODO."
308 test -s "$TODO" && return
310 comment_for_reflog finish &&
311 HEADNAME=$(cat "$DOTEST"/head-name) &&
312 OLDHEAD=$(cat "$DOTEST"/head) &&
313 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
314 if test -d "$REWRITTEN"
316 test -f "$DOTEST"/current-commit &&
317 current_commit=$(cat "$DOTEST"/current-commit) &&
318 git rev-parse HEAD > "$REWRITTEN"/$current_commit
319 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
321 NEWHEAD=$(git rev-parse HEAD)
325 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
326 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
327 git symbolic-ref HEAD $HEADNAME
330 test ! -f "$DOTEST"/verbose ||
331 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
335 warn "Successfully rebased and updated $HEADNAME."
351 comment_for_reflog continue
353 test -d "$DOTEST" || die "No interactive rebase running"
355 # commit if necessary
356 git rev-parse --verify HEAD > /dev/null &&
357 git update-index --refresh &&
358 git diff-files --quiet &&
359 ! git diff-index --cached --quiet HEAD &&
360 . "$DOTEST"/author-script && {
361 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
363 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
364 git commit -F "$DOTEST"/message -e
366 require_clean_work_tree
370 comment_for_reflog abort
372 test -d "$DOTEST" || die "No interactive rebase running"
374 HEADNAME=$(cat "$DOTEST"/head-name)
375 HEAD=$(cat "$DOTEST"/head)
378 git symbolic-ref HEAD $HEADNAME
381 output git reset --hard $HEAD &&
386 comment_for_reflog skip
388 test -d "$DOTEST" || die "No interactive rebase running"
390 output git reset --hard && do_rest
395 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
404 # we use merge anyway
407 die "Interactive rebase uses merge, so $1 does not make sense"
412 -p|--preserve-merges)
423 die "Interactive rebase already started"
425 git var GIT_COMMITTER_IDENT >/dev/null ||
426 die "You need to set your committer info first"
428 comment_for_reflog start
433 ONTO=$(git rev-parse --verify "$2") ||
434 die "Does not point to a valid commit: $2"
439 require_clean_work_tree
443 output git show-ref --verify --quiet "refs/heads/$2" ||
444 die "Invalid branchname: $2"
445 output git checkout "$2" ||
446 die "Could not checkout $2"
449 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
450 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
452 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
454 test -z "$ONTO" && ONTO=$UPSTREAM
456 : > "$DOTEST"/interactive || die "Could not mark as interactive"
457 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
458 echo "detached HEAD" > "$DOTEST"/head-name
460 echo $HEAD > "$DOTEST"/head
461 echo $UPSTREAM > "$DOTEST"/upstream
462 echo $ONTO > "$DOTEST"/onto
463 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
464 test t = "$VERBOSE" && : > "$DOTEST"/verbose
465 if test t = "$PRESERVE_MERGES"
467 # $REWRITTEN contains files for each commit that is
468 # reachable by at least one merge base of $HEAD and
469 # $UPSTREAM. They are not necessarily rewritten, but
470 # their children might be.
471 # This ensures that commits on merged, but otherwise
472 # unrelated side branches are left alone. (Think "X"
473 # in the man page's example.)
474 mkdir "$REWRITTEN" &&
475 for c in $(git merge-base --all $HEAD $UPSTREAM)
477 echo $ONTO > "$REWRITTEN"/$c ||
478 die "Could not init rewritten commits"
482 MERGES_OPTION=--no-merges
485 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
486 SHORTHEAD=$(git rev-parse --short $HEAD)
487 SHORTONTO=$(git rev-parse --short $ONTO)
489 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
493 # edit = use commit, but stop for amending
494 # squash = use commit, but meld into previous commit
496 # If you remove a line here THAT COMMIT WILL BE LOST.
499 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
500 --abbrev=7 --reverse --left-right --cherry-pick \
501 $UPSTREAM...$HEAD | \
502 sed -n "s/^>/pick /p" >> "$TODO"
504 has_action "$TODO" ||
505 die_abort "Nothing to do"
507 cp "$TODO" "$TODO".backup
508 git_editor "$TODO" ||
509 die "Could not execute editor"
511 has_action "$TODO" ||
512 die_abort "Nothing to do"
514 output git checkout $ONTO && do_rest