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"
22 REWRITTEN
="$DOTEST"/rewritten
31 require_clean_work_tree
() {
32 # test if working tree is dirty
33 git rev-parse
--verify HEAD
> /dev
/null
&&
34 git update-index
--refresh &&
35 git diff-files
--quiet &&
36 git diff-index
--cached --quiet HEAD ||
37 die
"Working tree is dirty"
40 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
42 comment_for_reflog
() {
43 case "$ORIG_REFLOG_ACTION" in
45 GIT_REFLOG_ACTION
="rebase -i ($1)"
46 export GIT_REFLOG_ACTION
51 sed -e 1q
< "$TODO" >> "$DONE"
52 sed -e 1d
< "$TODO" >> "$TODO".new
53 mv -f "$TODO".new
"$TODO"
57 parent_sha1
=$
(git rev-parse
--verify "$1"^
2> /dev
/null
)
58 git
diff "$parent_sha1"..
"$1" > "$DOTEST"/patch
62 test -f "$DOTEST"/message ||
63 git cat-file commit
$sha1 |
sed "1,/^$/d" > "$DOTEST"/message
64 test -f "$DOTEST"/author-script ||
65 get_author_ident_from_commit
$sha1 > "$DOTEST"/author-script
76 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
77 git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
78 test -d "$REWRITTEN" &&
79 pick_one_preserving_merges
"$@" && return
80 parent_sha1
=$
(git rev-parse
--verify $sha1^
2>/dev
/null
)
81 current_sha1
=$
(git rev-parse
--verify HEAD
)
82 if [ $current_sha1 = $parent_sha1 ]; then
83 git
reset --hard $sha1
84 sha1
=$
(git rev-parse
--short $sha1)
85 warn Fast forward to
$sha1
87 git cherry-pick
$STRATEGY "$@"
91 pick_one_preserving_merges
() {
92 case "$1" in -n) sha1
=$2 ;; *) sha1
=$1 ;; esac
93 sha1
=$
(git rev-parse
$sha1)
95 if [ -f "$DOTEST"/current-commit
]
97 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
98 git rev-parse HEAD
> "$REWRITTEN"/$current_commit &&
99 rm "$DOTEST"/current-commit ||
100 die
"Cannot write current commit's replacement sha1"
103 # rewrite parents; if none were rewritten, we can fast-forward.
107 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d\
-f2-)
109 if [ -f "$REWRITTEN"/$p ]
112 new_p
=$
(cat "$REWRITTEN"/$p)
113 test $p != $new_p && fast_forward
=f
114 case "$new_parents" in
116 ;; # do nothing; that parent is already there
118 new_parents
="$new_parents $new_p"
122 case $fast_forward in
124 echo "Fast forward to $sha1"
125 test $preserve=f
&& echo $sha1 > "$REWRITTEN"/$sha1
128 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
130 first_parent
=$
(expr "$new_parents" : " \([^ ]*\)")
131 # detach HEAD to current parent
132 git checkout
$first_parent 2> /dev
/null ||
133 die
"Cannot move HEAD to $first_parent"
135 echo $sha1 > "$DOTEST"/current-commit
136 case "$new_parents" in
139 author_script
=$
(get_author_ident_from_commit
$sha1)
140 eval "$author_script"
141 msg
="$(git cat-file commit $sha1 | \
142 sed -e '1,/^$/d' -e "s
/[\"\\]/\\\\&/g
")"
143 # NEEDSWORK: give rerere a chance
144 if ! git merge
$STRATEGY -m "$msg" $new_parents
146 echo "$msg" > "$GIT_DIR"/MERGE_MSG
147 die Error redoing merge
$sha1
151 git cherry-pick
$STRATEGY "$@" ||
152 die_with_patch
$sha1 "Could not pick $sha1"
158 test -f "$DOTEST"/message
&& rm "$DOTEST"/message
159 test -f "$DOTEST"/author-script
&& rm "$DOTEST"/author-script
160 read command sha1 rest
< "$TODO"
166 comment_for_reflog pick
170 die_with_patch
$sha1 "Could not apply $sha1... $rest"
173 comment_for_reflog edit
177 die_with_patch
$sha1 "Could not apply $sha1... $rest"
180 warn
"You can amend the commit now, with"
182 warn
" git commit --amend"
187 comment_for_reflog squash
189 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
190 die
"Cannot 'squash' without a previous commit"
194 pick_one
-n $sha1 || failed
=t
195 MSG
="$DOTEST"/message
196 echo "# This is a combination of two commits." > "$MSG"
197 echo "# The first commit's message is:" >> "$MSG"
199 git cat-file commit HEAD |
sed -e '1,/^$/d' >> "$MSG"
201 echo "# And this is the 2nd commit message:" >> "$MSG"
203 git cat-file commit
$sha1 |
sed -e '1,/^$/d' >> "$MSG"
204 git
reset --soft HEAD^
205 author_script
=$
(get_author_ident_from_commit
$sha1)
206 echo "$author_script" > "$DOTEST"/author-script
209 # This is like --amend, but with a different message
210 eval "$author_script"
211 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
212 git commit
-F "$MSG" -e
215 cp "$MSG" "$GIT_DIR"/MERGE_MSG
217 warn
"Could not apply $sha1... $rest"
218 die_with_patch
$sha1 ""
222 warn
"Unknown command: $command $sha1 $rest"
223 die_with_patch
$sha1 "Please fix this in the file $TODO."
225 test -s "$TODO" && return
227 comment_for_reflog finish
&&
228 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
229 OLDHEAD
=$
(cat "$DOTEST"/head) &&
230 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
231 if [ -d "$REWRITTEN" ]
233 test -f "$DOTEST"/current-commit
&&
234 current_commit
=$
(cat "$DOTEST"/current-commit
) &&
235 git rev-parse HEAD
> "$REWRITTEN"/$current_commit
236 NEWHEAD
=$
(cat "$REWRITTEN"/$OLDHEAD)
238 NEWHEAD
=$
(git rev-parse HEAD
)
240 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
241 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
242 git symbolic-ref HEAD
$HEADNAME && {
243 test ! -f "$DOTEST"/verbose ||
244 git
diff --stat $
(cat "$DOTEST"/head)..HEAD
247 warn
"Successfully rebased and updated $HEADNAME."
259 while case $# in 0) break ;; esac
263 comment_for_reflog
continue
265 test -d "$DOTEST" || die
"No interactive rebase running"
267 # commit if necessary
268 git rev-parse
--verify HEAD
> /dev
/null
&&
269 git update-index
--refresh &&
270 git diff-files
--quiet &&
271 ! git diff-index
--cached --quiet HEAD
&&
272 .
"$DOTEST"/author-script
&&
273 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE
&&
274 git commit
-F "$DOTEST"/message
-e
276 require_clean_work_tree
280 comment_for_reflog abort
282 test -d "$DOTEST" || die
"No interactive rebase running"
284 HEADNAME
=$
(cat "$DOTEST"/head-name
)
285 HEAD
=$
(cat "$DOTEST"/head)
286 git symbolic-ref HEAD
$HEADNAME &&
287 git
reset --hard $HEAD &&
292 comment_for_reflog skip
294 test -d "$DOTEST" || die
"No interactive rebase running"
296 git
reset --hard && do_rest
302 STRATEGY
="-s `expr "z
$1" : 'z-[^=]*=\(.*\)'`" ;;
311 # we use merge anyway
314 die
"Interactive rebase uses merge, so $1 does not make sense"
319 -p|
--preserve-merges)
330 die
"Interactive rebase already started"
332 git var GIT_COMMITTER_IDENT
>/dev
/null ||
333 die
"You need to set your committer info first"
335 comment_for_reflog start
340 ONTO
=$
(git rev-parse
--verify "$2") ||
341 die
"Does not point to a valid commit: $2"
346 require_clean_work_tree
350 git show-ref
--verify --quiet "refs/heads/$2" ||
351 die
"Invalid branchname: $2"
353 die
"Could not checkout $2"
356 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
357 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
359 test -z "$ONTO" && ONTO
=$UPSTREAM
361 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
362 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
363 git symbolic-ref HEAD
> "$DOTEST"/head-name ||
364 die
"Could not get HEAD"
366 echo $HEAD > "$DOTEST"/head
367 echo $UPSTREAM > "$DOTEST"/upstream
368 echo $ONTO > "$DOTEST"/onto
369 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
370 if [ t
= "$PRESERVE_MERGES" ]
372 # $REWRITTEN contains files for each commit that is
373 # reachable by at least one merge base of $HEAD and
374 # $UPSTREAM. They are not necessarily rewritten, but
375 # their children might be.
376 # This ensures that commits on merged, but otherwise
377 # unrelated side branches are left alone. (Think "X"
378 # in the man page's example.)
379 mkdir
"$REWRITTEN" &&
380 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
382 echo $ONTO > "$REWRITTEN"/$c ||
383 die
"Could not init rewritten commits"
387 MERGES_OPTION
=--no-merges
390 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
391 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
392 SHORTONTO
=$
(git rev-parse
--short $ONTO)
394 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
398 # edit = use commit, but stop for amending
399 # squash = use commit, but meld into previous commit
401 git rev-list
$MERGES_OPTION --pretty=oneline
--abbrev-commit \
402 --abbrev=7 --reverse $UPSTREAM..
$HEAD | \
403 sed "s/^/pick /" >> "$TODO"
405 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
406 die_abort
"Nothing to do"
408 cp "$TODO" "$TODO".backup
409 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
410 die
"Could not execute editor"
412 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
413 die_abort
"Nothing to do"
415 git checkout
$ONTO && do_rest