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 | [--onto <branch>] <upstream> [<branch>])'
18 DOTEST="$GIT_DIR/.dotest-merge"
28 require_clean_work_tree () {
29 # test if working tree is dirty
30 git rev-parse --verify HEAD > /dev/null &&
31 git update-index --refresh &&
32 git diff-files --quiet &&
33 git diff-index --cached --quiet HEAD ||
34 die "Working tree is dirty"
37 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
39 comment_for_reflog () {
40 case "$ORIG_REFLOG_ACTION" in
42 GIT_REFLOG_ACTION="rebase -i ($1)"
43 export GIT_REFLOG_ACTION
48 sed -e 1q < "$TODO" >> "$DONE"
49 sed -e 1d < "$TODO" >> "$TODO".new
50 mv -f "$TODO".new "$TODO"
54 parent_sha1=$(git rev-parse --verify "$1"^ 2> /dev/null)
55 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
69 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
70 git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
71 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
72 current_sha1=$(git rev-parse --verify HEAD)
73 if [ $current_sha1 = $parent_sha1 ]; then
74 git reset --hard $sha1
75 sha1=$(git rev-parse --short $sha1)
76 warn Fast forward to $sha1
78 git cherry-pick $STRATEGY "$@"
83 read command sha1 rest < "$TODO"
90 comment_for_reflog pick
94 die_with_patch $sha1 "Could not apply $sha1... $rest"
97 comment_for_reflog edit
101 die_with_patch $sha1 "Could not apply $sha1... $rest"
104 warn "You can amend the commit now, with"
106 warn " git commit --amend"
111 comment_for_reflog squash
113 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
114 die "Cannot 'squash' without a previous commit"
118 pick_one -n $sha1 || failed=t
119 MSG="$DOTEST"/message
120 echo "# This is a combination of two commits." > "$MSG"
121 echo "# The first commit's message is:" >> "$MSG"
123 git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
125 echo "# And this is the 2nd commit message:" >> "$MSG"
127 git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
128 git reset --soft HEAD^
129 author_script=$(get_author_ident_from_commit $sha1)
132 # This is like --amend, but with a different message
133 eval "$author_script"
134 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
135 git commit -F "$MSG" -e
138 cp "$MSG" "$GIT_DIR"/MERGE_MSG
140 warn "Could not apply $sha1... $rest"
141 warn "After you fixed that, commit the result with"
143 warn " $(echo $author_script | tr '\012' ' ') \\"
144 warn " git commit -F \"$GIT_DIR\"/MERGE_MSG -e"
145 die_with_patch $sha1 ""
149 warn "Unknown command: $command $sha1 $rest"
150 die_with_patch $sha1 "Please fix this in the file $TODO."
152 test -s "$TODO" && return
154 HEAD=$(git rev-parse HEAD)
155 HEADNAME=$(cat "$DOTEST"/head-name)
157 warn "Successfully rebased and updated $HEADNAME."
167 test -f "$DOTEST"/verbose &&
168 git diff --stat $(cat "$DOTEST"/head)..HEAD
172 while case $# in 0) break ;; esac
176 comment_for_reflog continue
178 test -d "$DOTEST" || die "No interactive rebase running"
180 require_clean_work_tree
184 comment_for_reflog abort
186 test -d "$DOTEST" || die "No interactive rebase running"
188 HEADNAME=$(cat "$DOTEST"/head-name)
189 HEAD=$(cat "$DOTEST"/head)
190 git symbolic-ref HEAD $HEADNAME &&
191 git reset --hard $HEAD &&
196 comment_for_reflog skip
198 test -d "$DOTEST" || die "No interactive rebase running"
200 git reset --hard && do_rest
206 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
215 # we use merge anyway
218 die "Interactive rebase uses merge, so $1 does not make sense"
231 die "Interactive rebase already started"
233 git var GIT_COMMITTER_IDENT >/dev/null ||
234 die "You need to set your committer info first"
236 comment_for_reflog start
241 ONTO=$(git rev-parse --verify "$2") ||
242 die "Does not point to a valid commit: $2"
247 require_clean_work_tree
251 git show-ref --verify --quiet "refs/heads/$2" ||
252 die "Invalid branchname: $2"
254 die "Could not checkout $2"
257 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
258 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
260 test -z "$ONTO" && ONTO=$UPSTREAM
262 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
263 : > "$DOTEST"/interactive || die "Could not mark as interactive"
264 git symbolic-ref HEAD > "$DOTEST"/head-name ||
265 die "Could not get HEAD"
267 echo $HEAD > "$DOTEST"/head
268 echo $UPSTREAM > "$DOTEST"/upstream
269 echo $ONTO > "$DOTEST"/onto
270 test t = "$VERBOSE" && : > "$DOTEST"/verbose
272 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
273 SHORTHEAD=$(git rev-parse --short $HEAD)
274 SHORTONTO=$(git rev-parse --short $ONTO)
276 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
280 # edit = use commit, but stop for amending
281 # squash = use commit, but meld into previous commit
283 git rev-list --no-merges --pretty=oneline --abbrev-commit \
284 --abbrev=7 --reverse $UPSTREAM..$HEAD | \
285 sed "s/^/pick /" >> "$TODO"
287 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
288 die_abort "Nothing to do"
290 cp "$TODO" "$TODO".backup
291 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
292 die "Could not execute editor"
294 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
295 die_abort "Nothing to do"
297 git checkout $ONTO && do_rest