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
26 test -d "$REWRITTEN" && PRESERVE_MERGES=t
27 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
28 test -f "$DOTEST"/verbose && VERBOSE=t
34 require_clean_work_tree () {
35 # test if working tree is dirty
36 git rev-parse --verify HEAD > /dev/null &&
37 git update-index --refresh &&
38 git diff-files --quiet &&
39 git diff-index --cached --quiet HEAD ||
40 die "Working tree is dirty"
43 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
45 comment_for_reflog () {
46 case "$ORIG_REFLOG_ACTION" in
48 GIT_REFLOG_ACTION="rebase -i ($1)"
49 export GIT_REFLOG_ACTION
54 sed -e 1q < "$TODO" >> "$DONE"
55 sed -e 1d < "$TODO" >> "$TODO".new
56 mv -f "$TODO".new "$TODO"
60 parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
61 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
65 test -f "$DOTEST"/message ||
66 git cat-file commit $sha1 | sed "1,/^$/d" > "$DOTEST"/message
67 test -f "$DOTEST"/author-script ||
68 get_author_ident_from_commit $sha1 > "$DOTEST"/author-script
79 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
80 git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
81 test -d "$REWRITTEN" &&
82 pick_one_preserving_merges "$@" && return
83 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
84 current_sha1=$(git rev-parse --verify HEAD)
85 if [ $current_sha1 = $parent_sha1 ]; then
86 git reset --hard $sha1
87 test "a$1" = a-n && git reset --soft $current_sha1
88 sha1=$(git rev-parse --short $sha1)
89 warn Fast forward to $sha1
91 git cherry-pick $STRATEGY "$@"
95 pick_one_preserving_merges () {
96 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
97 sha1=$(git rev-parse $sha1)
99 if [ -f "$DOTEST"/current-commit ]
101 current_commit=$(cat "$DOTEST"/current-commit) &&
102 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
103 rm "$DOTEST"/current-commit ||
104 die "Cannot write current commit's replacement sha1"
107 # rewrite parents; if none were rewritten, we can fast-forward.
111 for p in $(git rev-list --parents -1 $sha1 | cut -d\ -f2-)
113 if [ -f "$REWRITTEN"/$p ]
116 new_p=$(cat "$REWRITTEN"/$p)
117 test $p != $new_p && fast_forward=f
118 case "$new_parents" in
120 ;; # do nothing; that parent is already there
122 new_parents="$new_parents $new_p"
126 case $fast_forward in
128 echo "Fast forward to $sha1"
129 test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
132 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
134 first_parent=$(expr "$new_parents" : " \([^ ]*\)")
135 # detach HEAD to current parent
136 git checkout $first_parent 2> /dev/null ||
137 die "Cannot move HEAD to $first_parent"
139 echo $sha1 > "$DOTEST"/current-commit
140 case "$new_parents" in
143 author_script=$(get_author_ident_from_commit $sha1)
144 eval "$author_script"
145 msg="$(git cat-file commit $sha1 | \
146 sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
147 # NEEDSWORK: give rerere a chance
148 if ! git merge $STRATEGY -m "$msg" $new_parents
150 echo "$msg" > "$GIT_DIR"/MERGE_MSG
151 die Error redoing merge $sha1
155 git cherry-pick $STRATEGY "$@" ||
156 die_with_patch $sha1 "Could not pick $sha1"
162 test -f "$DOTEST"/message && rm "$DOTEST"/message
163 test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
164 read command sha1 rest < "$TODO"
170 comment_for_reflog pick
174 die_with_patch $sha1 "Could not apply $sha1... $rest"
177 comment_for_reflog edit
181 die_with_patch $sha1 "Could not apply $sha1... $rest"
184 warn "You can amend the commit now, with"
186 warn " git commit --amend"
191 comment_for_reflog squash
193 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
194 die "Cannot 'squash' without a previous commit"
197 MSG="$DOTEST"/message
198 echo "# This is a combination of two commits." > "$MSG"
199 echo "# The first commit's message is:" >> "$MSG"
201 git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
204 pick_one -n $sha1 || failed=t
205 echo "# And this is the 2nd commit message:" >> "$MSG"
207 git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
208 git reset --soft HEAD^
209 author_script=$(get_author_ident_from_commit $sha1)
210 echo "$author_script" > "$DOTEST"/author-script
213 # This is like --amend, but with a different message
214 eval "$author_script"
215 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
216 git commit -F "$MSG" -e
219 cp "$MSG" "$GIT_DIR"/MERGE_MSG
221 warn "Could not apply $sha1... $rest"
222 die_with_patch $sha1 ""
226 warn "Unknown command: $command $sha1 $rest"
227 die_with_patch $sha1 "Please fix this in the file $TODO."
229 test -s "$TODO" && return
231 comment_for_reflog finish &&
232 HEADNAME=$(cat "$DOTEST"/head-name) &&
233 OLDHEAD=$(cat "$DOTEST"/head) &&
234 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
235 if [ -d "$REWRITTEN" ]
237 test -f "$DOTEST"/current-commit &&
238 current_commit=$(cat "$DOTEST"/current-commit) &&
239 git rev-parse HEAD > "$REWRITTEN"/$current_commit
240 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
242 NEWHEAD=$(git rev-parse HEAD)
244 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
245 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
246 git symbolic-ref HEAD $HEADNAME && {
247 test ! -f "$DOTEST"/verbose ||
248 git diff --stat $(cat "$DOTEST"/head)..HEAD
251 warn "Successfully rebased and updated $HEADNAME."
263 while case $# in 0) break ;; esac
267 comment_for_reflog continue
269 test -d "$DOTEST" || die "No interactive rebase running"
271 # commit if necessary
272 git rev-parse --verify HEAD > /dev/null &&
273 git update-index --refresh &&
274 git diff-files --quiet &&
275 ! git diff-index --cached --quiet HEAD &&
276 . "$DOTEST"/author-script &&
277 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
278 git commit -F "$DOTEST"/message -e
280 require_clean_work_tree
284 comment_for_reflog abort
286 test -d "$DOTEST" || die "No interactive rebase running"
288 HEADNAME=$(cat "$DOTEST"/head-name)
289 HEAD=$(cat "$DOTEST"/head)
290 git symbolic-ref HEAD $HEADNAME &&
291 git reset --hard $HEAD &&
296 comment_for_reflog skip
298 test -d "$DOTEST" || die "No interactive rebase running"
300 git reset --hard && do_rest
306 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
315 # we use merge anyway
318 die "Interactive rebase uses merge, so $1 does not make sense"
323 -p|--preserve-merges)
334 die "Interactive rebase already started"
336 git var GIT_COMMITTER_IDENT >/dev/null ||
337 die "You need to set your committer info first"
339 comment_for_reflog start
344 ONTO=$(git rev-parse --verify "$2") ||
345 die "Does not point to a valid commit: $2"
350 require_clean_work_tree
354 git show-ref --verify --quiet "refs/heads/$2" ||
355 die "Invalid branchname: $2"
357 die "Could not checkout $2"
360 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
361 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
363 test -z "$ONTO" && ONTO=$UPSTREAM
365 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
366 : > "$DOTEST"/interactive || die "Could not mark as interactive"
367 git symbolic-ref HEAD > "$DOTEST"/head-name ||
368 die "Could not get HEAD"
370 echo $HEAD > "$DOTEST"/head
371 echo $UPSTREAM > "$DOTEST"/upstream
372 echo $ONTO > "$DOTEST"/onto
373 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
374 test t = "$VERBOSE" && : > "$DOTEST"/verbose
375 if [ t = "$PRESERVE_MERGES" ]
377 # $REWRITTEN contains files for each commit that is
378 # reachable by at least one merge base of $HEAD and
379 # $UPSTREAM. They are not necessarily rewritten, but
380 # their children might be.
381 # This ensures that commits on merged, but otherwise
382 # unrelated side branches are left alone. (Think "X"
383 # in the man page's example.)
384 mkdir "$REWRITTEN" &&
385 for c in $(git merge-base --all $HEAD $UPSTREAM)
387 echo $ONTO > "$REWRITTEN"/$c ||
388 die "Could not init rewritten commits"
392 MERGES_OPTION=--no-merges
395 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
396 SHORTHEAD=$(git rev-parse --short $HEAD)
397 SHORTONTO=$(git rev-parse --short $ONTO)
399 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
403 # edit = use commit, but stop for amending
404 # squash = use commit, but meld into previous commit
406 # If you remove a line here THAT COMMIT WILL BE LOST.
409 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
410 --abbrev=7 --reverse $UPSTREAM..$HEAD | \
411 sed "s/^/pick /" >> "$TODO"
413 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
414 die_abort "Nothing to do"
416 cp "$TODO" "$TODO".backup
417 git_editor "$TODO" ||
418 die "Could not execute editor"
420 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
421 die_abort "Nothing to do"
423 git checkout $ONTO && do_rest