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
39 "$@" > "$DOTEST"/output 2>&1
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 "$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 $STRATEGY "$@"
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 # NEEDSWORK: give rerere a chance
177 if ! output git merge $STRATEGY -m "$msg" $new_parents
179 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
180 die Error redoing merge $sha1
184 output git cherry-pick $STRATEGY "$@" ||
185 die_with_patch $sha1 "Could not pick $sha1"
194 *1[0-9]|*[04-9]) echo "$1"th;;
201 make_squash_message () {
202 if test -f "$SQUASH_MSG"; then
203 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
204 < "$SQUASH_MSG" | tail -n 1)+1))
205 echo "# This is a combination of $COUNT commits."
206 sed -n "2,\$p" < "$SQUASH_MSG"
209 echo "# This is a combination of two commits."
210 echo "# The first commit's message is:"
212 git cat-file commit HEAD | sed -e '1,/^$/d'
215 echo "# This is the $(nth_string $COUNT) commit message:"
217 git cat-file commit $1 | sed -e '1,/^$/d'
220 peek_next_command () {
221 sed -n "1s/ .*$//p" < "$TODO"
225 rm -f "$DOTEST"/message "$DOTEST"/author-script \
226 "$DOTEST"/amend || exit
227 read command sha1 rest < "$TODO"
233 comment_for_reflog pick
237 die_with_patch $sha1 "Could not apply $sha1... $rest"
240 comment_for_reflog edit
244 die_with_patch $sha1 "Could not apply $sha1... $rest"
248 warn "You can amend the commit now, with"
250 warn " git commit --amend"
255 comment_for_reflog squash
257 has_action "$DONE" ||
258 die "Cannot 'squash' without a previous commit"
261 make_squash_message $sha1 > "$MSG"
262 case "$(peek_next_command)" in
266 cp "$MSG" "$SQUASH_MSG"
271 rm -f "$SQUASH_MSG" || exit
276 output git reset --soft HEAD^
277 pick_one -n $sha1 || failed=t
278 author_script=$(get_author_ident_from_commit $sha1)
279 echo "$author_script" > "$DOTEST"/author-script
282 # This is like --amend, but with a different message
283 eval "$author_script"
284 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
285 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
288 cp "$MSG" "$GIT_DIR"/MERGE_MSG
290 warn "Could not apply $sha1... $rest"
291 die_with_patch $sha1 ""
296 warn "Unknown command: $command $sha1 $rest"
297 die_with_patch $sha1 "Please fix this in the file $TODO."
300 test -s "$TODO" && return
302 comment_for_reflog finish &&
303 HEADNAME=$(cat "$DOTEST"/head-name) &&
304 OLDHEAD=$(cat "$DOTEST"/head) &&
305 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
306 if test -d "$REWRITTEN"
308 test -f "$DOTEST"/current-commit &&
309 current_commit=$(cat "$DOTEST"/current-commit) &&
310 git rev-parse HEAD > "$REWRITTEN"/$current_commit
311 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
313 NEWHEAD=$(git rev-parse HEAD)
315 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
316 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
317 git symbolic-ref HEAD $HEADNAME && {
318 test ! -f "$DOTEST"/verbose ||
319 git diff --stat $(cat "$DOTEST"/head)..HEAD
322 warn "Successfully rebased and updated $HEADNAME."
338 comment_for_reflog continue
340 test -d "$DOTEST" || die "No interactive rebase running"
342 # commit if necessary
343 git rev-parse --verify HEAD > /dev/null &&
344 git update-index --refresh &&
345 git diff-files --quiet &&
346 ! git diff-index --cached --quiet HEAD &&
347 . "$DOTEST"/author-script && {
348 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
350 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
351 git commit -F "$DOTEST"/message -e
353 require_clean_work_tree
357 comment_for_reflog abort
359 test -d "$DOTEST" || die "No interactive rebase running"
361 HEADNAME=$(cat "$DOTEST"/head-name)
362 HEAD=$(cat "$DOTEST"/head)
363 git symbolic-ref HEAD $HEADNAME &&
364 output git reset --hard $HEAD &&
369 comment_for_reflog skip
371 test -d "$DOTEST" || die "No interactive rebase running"
373 output git reset --hard && do_rest
379 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
388 # we use merge anyway
391 die "Interactive rebase uses merge, so $1 does not make sense"
396 -p|--preserve-merges)
407 die "Interactive rebase already started"
409 git var GIT_COMMITTER_IDENT >/dev/null ||
410 die "You need to set your committer info first"
412 comment_for_reflog start
417 ONTO=$(git rev-parse --verify "$2") ||
418 die "Does not point to a valid commit: $2"
423 require_clean_work_tree
425 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
428 output git show-ref --verify --quiet "refs/heads/$2" ||
429 die "Invalid branchname: $2"
430 output git checkout "$2" ||
431 die "Could not checkout $2"
434 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
435 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
437 test -z "$ONTO" && ONTO=$UPSTREAM
439 : > "$DOTEST"/interactive || die "Could not mark as interactive"
440 git symbolic-ref HEAD > "$DOTEST"/head-name ||
441 die "Could not get HEAD"
443 echo $HEAD > "$DOTEST"/head
444 echo $UPSTREAM > "$DOTEST"/upstream
445 echo $ONTO > "$DOTEST"/onto
446 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
447 test t = "$VERBOSE" && : > "$DOTEST"/verbose
448 if test t = "$PRESERVE_MERGES"
450 # $REWRITTEN contains files for each commit that is
451 # reachable by at least one merge base of $HEAD and
452 # $UPSTREAM. They are not necessarily rewritten, but
453 # their children might be.
454 # This ensures that commits on merged, but otherwise
455 # unrelated side branches are left alone. (Think "X"
456 # in the man page's example.)
457 mkdir "$REWRITTEN" &&
458 for c in $(git merge-base --all $HEAD $UPSTREAM)
460 echo $ONTO > "$REWRITTEN"/$c ||
461 die "Could not init rewritten commits"
465 MERGES_OPTION=--no-merges
468 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
469 SHORTHEAD=$(git rev-parse --short $HEAD)
470 SHORTONTO=$(git rev-parse --short $ONTO)
472 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
476 # edit = use commit, but stop for amending
477 # squash = use commit, but meld into previous commit
479 # If you remove a line here THAT COMMIT WILL BE LOST.
482 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
483 --abbrev=7 --reverse --left-right --cherry-pick \
484 $UPSTREAM...$HEAD | \
485 sed -n "s/^>/pick /p" >> "$TODO"
487 has_action "$TODO" ||
488 die_abort "Nothing to do"
490 cp "$TODO" "$TODO".backup
491 git_editor "$TODO" ||
492 die "Could not execute editor"
494 has_action "$TODO" ||
495 die_abort "Nothing to do"
497 output git checkout $ONTO && do_rest