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 ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
178 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
179 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
180 output git merge $STRATEGY -m "$msg" \
183 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
184 die Error redoing merge $sha1
188 output git cherry-pick $STRATEGY "$@" ||
189 die_with_patch $sha1 "Could not pick $sha1"
198 *1[0-9]|*[04-9]) echo "$1"th;;
205 make_squash_message () {
206 if test -f "$SQUASH_MSG"; then
207 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
208 < "$SQUASH_MSG" | tail -n 1)+1))
209 echo "# This is a combination of $COUNT commits."
210 sed -n "2,\$p" < "$SQUASH_MSG"
213 echo "# This is a combination of two commits."
214 echo "# The first commit's message is:"
216 git cat-file commit HEAD | sed -e '1,/^$/d'
219 echo "# This is the $(nth_string $COUNT) commit message:"
221 git cat-file commit $1 | sed -e '1,/^$/d'
224 peek_next_command () {
225 sed -n "1s/ .*$//p" < "$TODO"
229 rm -f "$DOTEST"/message "$DOTEST"/author-script \
230 "$DOTEST"/amend || exit
231 read command sha1 rest < "$TODO"
237 comment_for_reflog pick
241 die_with_patch $sha1 "Could not apply $sha1... $rest"
244 comment_for_reflog edit
248 die_with_patch $sha1 "Could not apply $sha1... $rest"
252 warn "You can amend the commit now, with"
254 warn " git commit --amend"
259 comment_for_reflog squash
261 has_action "$DONE" ||
262 die "Cannot 'squash' without a previous commit"
265 make_squash_message $sha1 > "$MSG"
266 case "$(peek_next_command)" in
270 cp "$MSG" "$SQUASH_MSG"
275 rm -f "$SQUASH_MSG" || exit
280 output git reset --soft HEAD^
281 pick_one -n $sha1 || failed=t
282 author_script=$(get_author_ident_from_commit $sha1)
283 echo "$author_script" > "$DOTEST"/author-script
286 # This is like --amend, but with a different message
287 eval "$author_script"
288 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
289 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
290 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
291 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
294 cp "$MSG" "$GIT_DIR"/MERGE_MSG
296 warn "Could not apply $sha1... $rest"
297 die_with_patch $sha1 ""
302 warn "Unknown command: $command $sha1 $rest"
303 die_with_patch $sha1 "Please fix this in the file $TODO."
306 test -s "$TODO" && return
308 comment_for_reflog finish &&
309 HEADNAME=$(cat "$DOTEST"/head-name) &&
310 OLDHEAD=$(cat "$DOTEST"/head) &&
311 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
312 if test -d "$REWRITTEN"
314 test -f "$DOTEST"/current-commit &&
315 current_commit=$(cat "$DOTEST"/current-commit) &&
316 git rev-parse HEAD > "$REWRITTEN"/$current_commit
317 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
319 NEWHEAD=$(git rev-parse HEAD)
321 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
322 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
323 git symbolic-ref HEAD $HEADNAME && {
324 test ! -f "$DOTEST"/verbose ||
325 git diff --stat $(cat "$DOTEST"/head)..HEAD
328 warn "Successfully rebased and updated $HEADNAME."
344 comment_for_reflog continue
346 test -d "$DOTEST" || die "No interactive rebase running"
348 # commit if necessary
349 git rev-parse --verify HEAD > /dev/null &&
350 git update-index --refresh &&
351 git diff-files --quiet &&
352 ! git diff-index --cached --quiet HEAD &&
353 . "$DOTEST"/author-script && {
354 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
356 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
357 git commit -F "$DOTEST"/message -e
359 require_clean_work_tree
363 comment_for_reflog abort
365 test -d "$DOTEST" || die "No interactive rebase running"
367 HEADNAME=$(cat "$DOTEST"/head-name)
368 HEAD=$(cat "$DOTEST"/head)
369 git symbolic-ref HEAD $HEADNAME &&
370 output git reset --hard $HEAD &&
375 comment_for_reflog skip
377 test -d "$DOTEST" || die "No interactive rebase running"
379 output git reset --hard && do_rest
385 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
394 # we use merge anyway
397 die "Interactive rebase uses merge, so $1 does not make sense"
402 -p|--preserve-merges)
413 die "Interactive rebase already started"
415 git var GIT_COMMITTER_IDENT >/dev/null ||
416 die "You need to set your committer info first"
418 comment_for_reflog start
423 ONTO=$(git rev-parse --verify "$2") ||
424 die "Does not point to a valid commit: $2"
429 require_clean_work_tree
431 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
434 output git show-ref --verify --quiet "refs/heads/$2" ||
435 die "Invalid branchname: $2"
436 output git checkout "$2" ||
437 die "Could not checkout $2"
440 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
441 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
443 test -z "$ONTO" && ONTO=$UPSTREAM
445 : > "$DOTEST"/interactive || die "Could not mark as interactive"
446 git symbolic-ref HEAD > "$DOTEST"/head-name ||
447 die "Could not get HEAD"
449 echo $HEAD > "$DOTEST"/head
450 echo $UPSTREAM > "$DOTEST"/upstream
451 echo $ONTO > "$DOTEST"/onto
452 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
453 test t = "$VERBOSE" && : > "$DOTEST"/verbose
454 if test t = "$PRESERVE_MERGES"
456 # $REWRITTEN contains files for each commit that is
457 # reachable by at least one merge base of $HEAD and
458 # $UPSTREAM. They are not necessarily rewritten, but
459 # their children might be.
460 # This ensures that commits on merged, but otherwise
461 # unrelated side branches are left alone. (Think "X"
462 # in the man page's example.)
463 mkdir "$REWRITTEN" &&
464 for c in $(git merge-base --all $HEAD $UPSTREAM)
466 echo $ONTO > "$REWRITTEN"/$c ||
467 die "Could not init rewritten commits"
471 MERGES_OPTION=--no-merges
474 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
475 SHORTHEAD=$(git rev-parse --short $HEAD)
476 SHORTONTO=$(git rev-parse --short $ONTO)
478 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
482 # edit = use commit, but stop for amending
483 # squash = use commit, but meld into previous commit
485 # If you remove a line here THAT COMMIT WILL BE LOST.
488 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
489 --abbrev=7 --reverse --left-right --cherry-pick \
490 $UPSTREAM...$HEAD | \
491 sed -n "s/^>/pick /p" >> "$TODO"
493 has_action "$TODO" ||
494 die_abort "Nothing to do"
496 cp "$TODO" "$TODO".backup
497 git_editor "$TODO" ||
498 die "Could not execute editor"
500 has_action "$TODO" ||
501 die_abort "Nothing to do"
503 output git checkout $ONTO && do_rest