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
36 require_clean_work_tree
() {
37 # test if working tree is dirty
38 git rev-parse
--verify HEAD
> /dev
/null
&&
39 git update-index
--refresh &&
40 git diff-files
--quiet &&
41 git diff-index
--cached --quiet HEAD ||
42 die
"Working tree is dirty"
45 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
47 comment_for_reflog
() {
48 case "$ORIG_REFLOG_ACTION" in
50 GIT_REFLOG_ACTION
="rebase -i ($1)"
51 export GIT_REFLOG_ACTION
56 sed -e 1q
< "$TODO" >> "$DONE"
57 sed -e 1d
< "$TODO" >> "$TODO".new
58 mv -f "$TODO".new
"$TODO"
62 parent_sha1
=$
(git rev-parse
--verify "$1"^
2> /dev
/null
)
63 git
diff "$parent_sha1"..
"$1" > "$DOTEST"/patch
67 test -f "$DOTEST"/message ||
68 git cat-file commit
$sha1 |
sed "1,/^$/d" > "$DOTEST"/message
69 test -f "$DOTEST"/author-script ||
70 get_author_ident_from_commit
$sha1 > "$DOTEST"/author-script
81 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
82 git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
83 test -d "$REWRITTEN" &&
84 pick_one_preserving_merges
"$@" && return
85 parent_sha1
=$
(git rev-parse
--verify $sha1^
2>/dev
/null
)
86 current_sha1
=$
(git rev-parse
--verify HEAD
)
87 if [ $current_sha1 = $parent_sha1 ]; then
88 git
reset --hard $sha1
89 test "a$1" = a-n
&& git
reset --soft $current_sha1
90 sha1
=$
(git rev-parse
--short $sha1)
91 warn Fast forward to
$sha1
93 git cherry-pick
$STRATEGY "$@"
97 pick_one_preserving_merges
() {
98 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
99 sha1
=$
(git rev-parse
$sha1)
101 if [ -f "$DOTEST"/current-commit
]
103 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
104 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
105 rm "$DOTEST"/current-commit ||
106 die
"Cannot write current commit's replacement sha1"
109 # rewrite parents; if none were rewritten, we can fast-forward.
113 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d\
-f2-)
115 if [ -f "$REWRITTEN"/$p ]
118 new_p
=$
(cat "$REWRITTEN"/$p)
119 test $p != $new_p && fast_forward
=f
120 case "$new_parents" in
122 ;; # do nothing; that parent is already there
124 new_parents
="$new_parents $new_p"
128 case $fast_forward in
130 echo "Fast forward to $sha1"
131 test $preserve=f
&& echo $sha1 > "$REWRITTEN"/$sha1
134 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
136 first_parent
=$
(expr "$new_parents" : " \([^ ]*\)")
137 # detach HEAD to current parent
138 git checkout
$first_parent 2> /dev
/null ||
139 die
"Cannot move HEAD to $first_parent"
141 echo $sha1 > "$DOTEST"/current-commit
142 case "$new_parents" in
145 author_script
=$
(get_author_ident_from_commit
$sha1)
146 eval "$author_script"
147 msg
="$(git cat-file commit $sha1 | \
148 sed -e '1,/^$/d' -e "s
/[\"\\]/\\\\&/g
")"
149 # NEEDSWORK: give rerere a chance
150 if ! git merge
$STRATEGY -m "$msg" $new_parents
152 echo "$msg" > "$GIT_DIR"/MERGE_MSG
153 die Error redoing merge
$sha1
157 git cherry-pick
$STRATEGY "$@" ||
158 die_with_patch
$sha1 "Could not pick $sha1"
165 *1[0-9]|
*[04-9]) echo "$1"th
;;
172 make_squash_message
() {
173 if [ -f "$SQUASH_MSG" ]; then
174 COUNT
=$
(($
(sed -n "s/^# This is [^0-9]*\([0-9]\+\).*/\1/p" \
175 < "$SQUASH_MSG" |
tail -n 1)+1))
176 echo "# This is a combination of $COUNT commits."
177 sed -n "2,\$p" < "$SQUASH_MSG"
180 echo "# This is a combination of two commits."
181 echo "# The first commit's message is:"
183 git cat-file commit HEAD |
sed -e '1,/^$/d'
186 echo "# This is the $(nth_string $COUNT) commit message:"
188 git cat-file commit
$1 |
sed -e '1,/^$/d'
191 peek_next_command
() {
192 sed -n "1s/ .*$//p" < "$TODO"
196 test -f "$DOTEST"/message
&& rm "$DOTEST"/message
197 test -f "$DOTEST"/author-script
&& rm "$DOTEST"/author-script
198 read command sha1 rest
< "$TODO"
204 comment_for_reflog pick
208 die_with_patch
$sha1 "Could not apply $sha1... $rest"
211 comment_for_reflog edit
215 die_with_patch
$sha1 "Could not apply $sha1... $rest"
218 warn
"You can amend the commit now, with"
220 warn
" git commit --amend"
225 comment_for_reflog squash
227 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
228 die
"Cannot 'squash' without a previous commit"
231 make_squash_message
$sha1 > "$MSG"
232 case "$(peek_next_command)" in
235 cp "$MSG" "$SQUASH_MSG"
239 test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
243 pick_one
-n $sha1 || failed
=t
244 git
reset --soft HEAD^
245 author_script
=$
(get_author_ident_from_commit
$sha1)
246 echo "$author_script" > "$DOTEST"/author-script
249 # This is like --amend, but with a different message
250 eval "$author_script"
251 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
252 git commit
-F "$MSG" $EDIT_COMMIT
255 cp "$MSG" "$GIT_DIR"/MERGE_MSG
257 warn
"Could not apply $sha1... $rest"
258 die_with_patch
$sha1 ""
262 warn
"Unknown command: $command $sha1 $rest"
263 die_with_patch
$sha1 "Please fix this in the file $TODO."
265 test -s "$TODO" && return
267 comment_for_reflog finish
&&
268 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
269 OLDHEAD
=$
(cat "$DOTEST"/head) &&
270 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
271 if [ -d "$REWRITTEN" ]
273 test -f "$DOTEST"/current-commit
&&
274 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
275 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
276 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
278 NEWHEAD
=$
(git rev-parse HEAD
)
280 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
281 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
282 git symbolic-ref HEAD
$HEADNAME && {
283 test ! -f "$DOTEST"/verbose ||
284 git
diff --stat $
(cat "$DOTEST"/head)..HEAD
287 warn
"Successfully rebased and updated $HEADNAME."
299 while case $# in 0) break ;; esac
303 comment_for_reflog
continue
305 test -d "$DOTEST" || die
"No interactive rebase running"
307 # commit if necessary
308 git rev-parse
--verify HEAD
> /dev
/null
&&
309 git update-index
--refresh &&
310 git diff-files
--quiet &&
311 ! git diff-index
--cached --quiet HEAD
&&
312 .
"$DOTEST"/author-script
&&
313 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE
&&
314 git commit
-F "$DOTEST"/message
-e
316 require_clean_work_tree
320 comment_for_reflog abort
322 test -d "$DOTEST" || die
"No interactive rebase running"
324 HEADNAME
=$
(cat "$DOTEST"/head-name
)
325 HEAD
=$
(cat "$DOTEST"/head)
326 git symbolic-ref HEAD
$HEADNAME &&
327 git
reset --hard $HEAD &&
332 comment_for_reflog skip
334 test -d "$DOTEST" || die
"No interactive rebase running"
336 git
reset --hard && do_rest
342 STRATEGY
="-s `expr "z
$1" : 'z-[^=]*=\(.*\)'`" ;;
351 # we use merge anyway
354 die
"Interactive rebase uses merge, so $1 does not make sense"
359 -p|
--preserve-merges)
370 die
"Interactive rebase already started"
372 git var GIT_COMMITTER_IDENT
>/dev
/null ||
373 die
"You need to set your committer info first"
375 comment_for_reflog start
380 ONTO
=$
(git rev-parse
--verify "$2") ||
381 die
"Does not point to a valid commit: $2"
386 require_clean_work_tree
390 git show-ref
--verify --quiet "refs/heads/$2" ||
391 die
"Invalid branchname: $2"
393 die
"Could not checkout $2"
396 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
397 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
399 test -z "$ONTO" && ONTO
=$UPSTREAM
401 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
402 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
403 git symbolic-ref HEAD
> "$DOTEST"/head-name ||
404 die
"Could not get HEAD"
406 echo $HEAD > "$DOTEST"/head
407 echo $UPSTREAM > "$DOTEST"/upstream
408 echo $ONTO > "$DOTEST"/onto
409 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
410 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
411 if [ t
= "$PRESERVE_MERGES" ]
413 # $REWRITTEN contains files for each commit that is
414 # reachable by at least one merge base of $HEAD and
415 # $UPSTREAM. They are not necessarily rewritten, but
416 # their children might be.
417 # This ensures that commits on merged, but otherwise
418 # unrelated side branches are left alone. (Think "X"
419 # in the man page's example.)
420 mkdir
"$REWRITTEN" &&
421 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
423 echo $ONTO > "$REWRITTEN"/$c ||
424 die
"Could not init rewritten commits"
428 MERGES_OPTION
=--no-merges
431 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
432 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
433 SHORTONTO
=$
(git rev-parse
--short $ONTO)
435 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
439 # edit = use commit, but stop for amending
440 # squash = use commit, but meld into previous commit
442 # If you remove a line here THAT COMMIT WILL BE LOST.
445 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
446 --abbrev=7 --reverse $UPSTREAM..
$HEAD | \
447 sed "s/^/pick /" >> "$TODO"
449 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
450 die_abort
"Nothing to do"
452 cp "$TODO" "$TODO".backup
453 git_editor
"$TODO" ||
454 die
"Could not execute editor"
456 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
457 die_abort
"Nothing to do"
459 git checkout
$ONTO && do_rest