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
72 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
73 git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
74 test -d "$REWRITTEN" &&
75 pick_one_preserving_merges "$@" && return
76 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
77 current_sha1=$(git rev-parse --verify HEAD)
78 if [ $current_sha1 = $parent_sha1 ]; then
79 git reset --hard $sha1
80 sha1=$(git rev-parse --short $sha1)
81 warn Fast forward to $sha1
83 git cherry-pick $STRATEGY "$@"
87 pick_one_preserving_merges () {
88 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
89 sha1=$(git rev-parse $sha1)
91 if [ -f "$DOTEST"/current-commit ]
93 current_commit=$(cat "$DOTEST"/current-commit) &&
94 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
95 rm "$DOTEST"/current-commit ||
96 die "Cannot write current commit's replacement sha1"
99 # rewrite parents; if none were rewritten, we can fast-forward.
103 for p in $(git rev-list --parents -1 $sha1 | cut -d\ -f2-)
105 if [ -f "$REWRITTEN"/$p ]
108 new_p=$(cat "$REWRITTEN"/$p)
109 test $p != $new_p && fast_forward=f
110 case "$new_parents" in
112 ;; # do nothing; that parent is already there
114 new_parents="$new_parents $new_p"
118 case $fast_forward in
120 echo "Fast forward to $sha1"
121 test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
124 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
126 first_parent=$(expr "$new_parents" : " \([^ ]*\)")
127 # detach HEAD to current parent
128 git checkout $first_parent 2> /dev/null ||
129 die "Cannot move HEAD to $first_parent"
131 echo $sha1 > "$DOTEST"/current-commit
132 case "$new_parents" in
135 author_script=$(get_author_ident_from_commit $sha1)
136 eval "$author_script"
137 msg="$(git cat-file commit $sha1 | \
138 sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
139 # NEEDSWORK: give rerere a chance
140 if ! git merge $STRATEGY -m "$msg" $new_parents
142 echo "$msg" > "$GIT_DIR"/MERGE_MSG
143 warn Error redoing merge $sha1
145 warn After fixup, please use
146 die "$author_script git commit"
150 git cherry-pick $STRATEGY "$@" ||
151 die_with_patch $sha1 "Could not pick $sha1"
157 read command sha1 rest < "$TODO"
164 comment_for_reflog pick
168 die_with_patch $sha1 "Could not apply $sha1... $rest"
171 comment_for_reflog edit
175 die_with_patch $sha1 "Could not apply $sha1... $rest"
178 warn "You can amend the commit now, with"
180 warn " git commit --amend"
185 comment_for_reflog squash
187 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
188 die "Cannot 'squash' without a previous commit"
192 pick_one -n $sha1 || failed=t
193 MSG="$DOTEST"/message
194 echo "# This is a combination of two commits." > "$MSG"
195 echo "# The first commit's message is:" >> "$MSG"
197 git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
199 echo "# And this is the 2nd commit message:" >> "$MSG"
201 git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
202 git reset --soft HEAD^
203 author_script=$(get_author_ident_from_commit $sha1)
206 # This is like --amend, but with a different message
207 eval "$author_script"
208 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
209 git commit -F "$MSG" -e
212 cp "$MSG" "$GIT_DIR"/MERGE_MSG
214 warn "Could not apply $sha1... $rest"
215 warn "After you fixed that, commit the result with"
217 warn " $(echo $author_script | tr '\012' ' ') \\"
218 warn " git commit -F \"$GIT_DIR\"/MERGE_MSG -e"
219 die_with_patch $sha1 ""
223 warn "Unknown command: $command $sha1 $rest"
224 die_with_patch $sha1 "Please fix this in the file $TODO."
226 test -s "$TODO" && return
228 comment_for_reflog finish &&
229 HEADNAME=$(cat "$DOTEST"/head-name) &&
230 OLDHEAD=$(cat "$DOTEST"/head) &&
231 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
232 if [ -d "$REWRITTEN" ]
234 test -f "$DOTEST"/current-commit &&
235 current_commit=$(cat "$DOTEST"/current-commit) &&
236 git rev-parse HEAD > "$REWRITTEN"/$current_commit
237 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
239 NEWHEAD=$(git rev-parse HEAD)
241 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
242 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
243 git symbolic-ref HEAD $HEADNAME &&
245 warn "Successfully rebased and updated $HEADNAME."
255 test -f "$DOTEST"/verbose &&
256 git diff --stat $(cat "$DOTEST"/head)..HEAD
260 while case $# in 0) break ;; esac
264 comment_for_reflog continue
266 test -d "$DOTEST" || die "No interactive rebase running"
268 require_clean_work_tree
272 comment_for_reflog abort
274 test -d "$DOTEST" || die "No interactive rebase running"
276 HEADNAME=$(cat "$DOTEST"/head-name)
277 HEAD=$(cat "$DOTEST"/head)
278 git symbolic-ref HEAD $HEADNAME &&
279 git reset --hard $HEAD &&
284 comment_for_reflog skip
286 test -d "$DOTEST" || die "No interactive rebase running"
288 git reset --hard && do_rest
294 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
303 # we use merge anyway
306 die "Interactive rebase uses merge, so $1 does not make sense"
311 -p|--preserve-merges)
322 die "Interactive rebase already started"
324 git var GIT_COMMITTER_IDENT >/dev/null ||
325 die "You need to set your committer info first"
327 comment_for_reflog start
332 ONTO=$(git rev-parse --verify "$2") ||
333 die "Does not point to a valid commit: $2"
338 require_clean_work_tree
342 git show-ref --verify --quiet "refs/heads/$2" ||
343 die "Invalid branchname: $2"
345 die "Could not checkout $2"
348 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
349 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
351 test -z "$ONTO" && ONTO=$UPSTREAM
353 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
354 : > "$DOTEST"/interactive || die "Could not mark as interactive"
355 git symbolic-ref HEAD > "$DOTEST"/head-name ||
356 die "Could not get HEAD"
358 echo $HEAD > "$DOTEST"/head
359 echo $UPSTREAM > "$DOTEST"/upstream
360 echo $ONTO > "$DOTEST"/onto
361 test t = "$VERBOSE" && : > "$DOTEST"/verbose
362 if [ t = "$PRESERVE_MERGES" ]
364 # $REWRITTEN contains files for each commit that is
365 # reachable by at least one merge base of $HEAD and
366 # $UPSTREAM. They are not necessarily rewritten, but
367 # their children might be.
368 # This ensures that commits on merged, but otherwise
369 # unrelated side branches are left alone. (Think "X"
370 # in the man page's example.)
371 mkdir "$REWRITTEN" &&
372 for c in $(git merge-base --all $HEAD $UPSTREAM)
374 echo $ONTO > "$REWRITTEN"/$c ||
375 die "Could not init rewritten commits"
379 MERGES_OPTION=--no-merges
382 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
383 SHORTHEAD=$(git rev-parse --short $HEAD)
384 SHORTONTO=$(git rev-parse --short $ONTO)
386 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
390 # edit = use commit, but stop for amending
391 # squash = use commit, but meld into previous commit
393 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
394 --abbrev=7 --reverse $UPSTREAM..$HEAD | \
395 sed "s/^/pick /" >> "$TODO"
397 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
398 die_abort "Nothing to do"
400 cp "$TODO" "$TODO".backup
401 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
402 die "Could not execute editor"
404 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
405 die_abort "Nothing to do"
407 git checkout $ONTO && do_rest