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"
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
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
70 sed -e 1q
< "$TODO" >> "$DONE"
71 sed -e 1d
< "$TODO" >> "$TODO".new
72 mv -f "$TODO".new
"$TODO"
73 count
=$
(($
(wc -l < "$DONE")))
74 total
=$
(($count+$
(wc -l < "$TODO")))
75 printf "Rebasing (%d/%d)\r" $count $total
76 test -z "$VERBOSE" ||
echo
80 parent_sha1
=$
(git rev-parse
--verify "$1"^
2> /dev
/null
)
81 git
diff "$parent_sha1"..
"$1" > "$DOTEST"/patch
85 test -f "$DOTEST"/message ||
86 git cat-file commit
$sha1 |
sed "1,/^$/d" > "$DOTEST"/message
87 test -f "$DOTEST"/author-script ||
88 get_author_ident_from_commit
$sha1 > "$DOTEST"/author-script
99 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
100 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
101 test -d "$REWRITTEN" &&
102 pick_one_preserving_merges
"$@" && return
103 parent_sha1
=$
(git rev-parse
--verify $sha1^
2>/dev
/null
)
104 current_sha1
=$
(git rev-parse
--verify HEAD
)
105 if test $current_sha1 = $parent_sha1; then
106 output git
reset --hard $sha1
107 test "a$1" = a-n
&& output git
reset --soft $current_sha1
108 sha1
=$
(git rev-parse
--short $sha1)
109 output warn Fast forward to
$sha1
111 output git cherry-pick
$STRATEGY "$@"
115 pick_one_preserving_merges
() {
116 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
117 sha1
=$
(git rev-parse
$sha1)
119 if test -f "$DOTEST"/current-commit
121 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
122 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
123 rm "$DOTEST"/current-commit ||
124 die
"Cannot write current commit's replacement sha1"
127 # rewrite parents; if none were rewritten, we can fast-forward.
131 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d\
-f2-)
133 if test -f "$REWRITTEN"/$p
136 new_p
=$
(cat "$REWRITTEN"/$p)
137 test $p != $new_p && fast_forward
=f
138 case "$new_parents" in
140 ;; # do nothing; that parent is already there
142 new_parents
="$new_parents $new_p"
146 case $fast_forward in
148 output warn
"Fast forward to $sha1"
149 test $preserve=f
&& echo $sha1 > "$REWRITTEN"/$sha1
152 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
154 first_parent
=$
(expr "$new_parents" : " \([^ ]*\)")
155 # detach HEAD to current parent
156 output git checkout
$first_parent 2> /dev
/null ||
157 die
"Cannot move HEAD to $first_parent"
159 echo $sha1 > "$DOTEST"/current-commit
160 case "$new_parents" in
163 author_script
=$
(get_author_ident_from_commit
$sha1)
164 eval "$author_script"
165 msg
="$(git cat-file commit $sha1 | \
166 sed -e '1,/^$/d' -e "s
/[\"\\]/\\\\&/g
")"
167 # NEEDSWORK: give rerere a chance
168 if ! output git merge
$STRATEGY -m "$msg" $new_parents
170 echo "$msg" > "$GIT_DIR"/MERGE_MSG
171 die Error redoing merge
$sha1
175 output git cherry-pick
$STRATEGY "$@" ||
176 die_with_patch
$sha1 "Could not pick $sha1"
183 *1[0-9]|
*[04-9]) echo "$1"th
;;
190 make_squash_message
() {
191 if test -f "$SQUASH_MSG"; then
192 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([0-9]\+\).*/\1/p" \
193 < "$SQUASH_MSG" |
tail -n 1)+1))
194 echo "# This is a combination of $COUNT commits."
195 sed -n "2,\$p" < "$SQUASH_MSG"
198 echo "# This is a combination of two commits."
199 echo "# The first commit's message is:"
201 git cat-file commit HEAD |
sed -e '1,/^$/d'
204 echo "# This is the $(nth_string $COUNT) commit message:"
206 git cat-file commit
$1 |
sed -e '1,/^$/d'
209 peek_next_command
() {
210 sed -n "1s/ .*$//p" < "$TODO"
214 test -f "$DOTEST"/message
&& rm "$DOTEST"/message
215 test -f "$DOTEST"/author-script
&& rm "$DOTEST"/author-script
216 read command sha1 rest
< "$TODO"
222 comment_for_reflog pick
226 die_with_patch
$sha1 "Could not apply $sha1... $rest"
229 comment_for_reflog edit
233 die_with_patch
$sha1 "Could not apply $sha1... $rest"
236 warn
"You can amend the commit now, with"
238 warn
" git commit --amend"
243 comment_for_reflog squash
245 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
246 die
"Cannot 'squash' without a previous commit"
249 make_squash_message
$sha1 > "$MSG"
250 case "$(peek_next_command)" in
253 cp "$MSG" "$SQUASH_MSG"
257 test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
261 pick_one
-n $sha1 || failed
=t
262 output git
reset --soft HEAD^
263 author_script
=$
(get_author_ident_from_commit
$sha1)
264 echo "$author_script" > "$DOTEST"/author-script
267 # This is like --amend, but with a different message
268 eval "$author_script"
269 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
270 output git commit
-F "$MSG" $EDIT_COMMIT
273 cp "$MSG" "$GIT_DIR"/MERGE_MSG
275 warn
"Could not apply $sha1... $rest"
276 die_with_patch
$sha1 ""
280 warn
"Unknown command: $command $sha1 $rest"
281 die_with_patch
$sha1 "Please fix this in the file $TODO."
283 test -s "$TODO" && return
285 comment_for_reflog finish
&&
286 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
287 OLDHEAD
=$
(cat "$DOTEST"/head) &&
288 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
289 if test -d "$REWRITTEN"
291 test -f "$DOTEST"/current-commit
&&
292 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
293 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
294 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
296 NEWHEAD
=$
(git rev-parse HEAD
)
298 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
299 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
300 git symbolic-ref HEAD
$HEADNAME && {
301 test ! -f "$DOTEST"/verbose ||
302 git
diff --stat $
(cat "$DOTEST"/head)..HEAD
305 warn
"Successfully rebased and updated $HEADNAME."
317 while case $# in 0) break ;; esac
321 comment_for_reflog
continue
323 test -d "$DOTEST" || die
"No interactive rebase running"
325 # commit if necessary
326 git rev-parse
--verify HEAD
> /dev
/null
&&
327 git update-index
--refresh &&
328 git diff-files
--quiet &&
329 ! git diff-index
--cached --quiet HEAD
&&
330 .
"$DOTEST"/author-script
&&
331 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE
&&
332 git commit
-F "$DOTEST"/message
-e
334 require_clean_work_tree
338 comment_for_reflog abort
340 test -d "$DOTEST" || die
"No interactive rebase running"
342 HEADNAME
=$
(cat "$DOTEST"/head-name
)
343 HEAD
=$
(cat "$DOTEST"/head)
344 git symbolic-ref HEAD
$HEADNAME &&
345 output git
reset --hard $HEAD &&
350 comment_for_reflog skip
352 test -d "$DOTEST" || die
"No interactive rebase running"
354 output git
reset --hard && do_rest
360 STRATEGY
="-s `expr "z
$1" : 'z-[^=]*=\(.*\)'`" ;;
369 # we use merge anyway
372 die
"Interactive rebase uses merge, so $1 does not make sense"
377 -p|
--preserve-merges)
388 die
"Interactive rebase already started"
390 git var GIT_COMMITTER_IDENT
>/dev
/null ||
391 die
"You need to set your committer info first"
393 comment_for_reflog start
398 ONTO
=$
(git rev-parse
--verify "$2") ||
399 die
"Does not point to a valid commit: $2"
404 require_clean_work_tree
408 output git show-ref
--verify --quiet "refs/heads/$2" ||
409 die
"Invalid branchname: $2"
410 output git checkout
"$2" ||
411 die
"Could not checkout $2"
414 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
415 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
417 test -z "$ONTO" && ONTO
=$UPSTREAM
419 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
420 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
421 git symbolic-ref HEAD
> "$DOTEST"/head-name ||
422 die
"Could not get HEAD"
424 echo $HEAD > "$DOTEST"/head
425 echo $UPSTREAM > "$DOTEST"/upstream
426 echo $ONTO > "$DOTEST"/onto
427 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
428 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
429 if test t
= "$PRESERVE_MERGES"
431 # $REWRITTEN contains files for each commit that is
432 # reachable by at least one merge base of $HEAD and
433 # $UPSTREAM. They are not necessarily rewritten, but
434 # their children might be.
435 # This ensures that commits on merged, but otherwise
436 # unrelated side branches are left alone. (Think "X"
437 # in the man page's example.)
438 mkdir
"$REWRITTEN" &&
439 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
441 echo $ONTO > "$REWRITTEN"/$c ||
442 die
"Could not init rewritten commits"
446 MERGES_OPTION
=--no-merges
449 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
450 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
451 SHORTONTO
=$
(git rev-parse
--short $ONTO)
453 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
457 # edit = use commit, but stop for amending
458 # squash = use commit, but meld into previous commit
460 # If you remove a line here THAT COMMIT WILL BE LOST.
463 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
464 --abbrev=7 --reverse $UPSTREAM..
$HEAD | \
465 sed "s/^/pick /" >> "$TODO"
467 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
468 die_abort
"Nothing to do"
470 cp "$TODO" "$TODO".backup
471 git_editor
"$TODO" ||
472 die
"Could not execute editor"
474 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
475 die_abort
"Nothing to do"
477 output git checkout
$ONTO && do_rest