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
64 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
65 git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
66 parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
67 current_sha1=$(git rev-parse --verify HEAD)
68 if [ $current_sha1 = $parent_sha1 ]; then
69 git reset --hard $sha1
70 sha1=$(git rev-parse --short $sha1)
71 warn Fast forward to $sha1
73 git cherry-pick $STRATEGY "$@"
78 read command sha1 rest < "$TODO"
85 comment_for_reflog pick
89 die_with_patch $sha1 "Could not apply $sha1... $rest"
92 comment_for_reflog edit
96 die_with_patch $sha1 "Could not apply $sha1... $rest"
99 warn "You can amend the commit now, with"
101 warn " git commit --amend"
106 comment_for_reflog squash
108 test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
109 die "Cannot 'squash' without a previous commit"
113 pick_one -n $sha1 || failed=t
114 MSG="$DOTEST"/message
115 echo "# This is a combination of two commits." > "$MSG"
116 echo "# The first commit's message is:" >> "$MSG"
118 git cat-file commit HEAD | sed -e '1,/^$/d' >> "$MSG"
120 echo "# And this is the 2nd commit message:" >> "$MSG"
122 git cat-file commit $sha1 | sed -e '1,/^$/d' >> "$MSG"
123 git reset --soft HEAD^
124 author_script=$(get_author_ident_from_commit $sha1)
127 # This is like --amend, but with a different message
128 eval "$author_script"
129 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
130 git commit -F "$MSG" -e
133 cp "$MSG" "$GIT_DIR"/MERGE_MSG
135 warn "Could not apply $sha1... $rest"
136 warn "After you fixed that, commit the result with"
138 warn " $(echo $author_script | tr '\012' ' ') \\"
139 warn " git commit -F \"$GIT_DIR\"/MERGE_MSG -e"
140 die_with_patch $sha1 ""
144 warn "Unknown command: $command $sha1 $rest"
145 die_with_patch $sha1 "Please fix this in the file $TODO."
147 test -s "$TODO" && return
149 HEAD=$(git rev-parse HEAD)
150 HEADNAME=$(cat "$DOTEST"/head-name)
152 warn "Successfully rebased and updated $HEADNAME."
162 test -f "$DOTEST"/verbose &&
163 git diff --stat $(cat "$DOTEST"/head)..HEAD
167 while case $# in 0) break ;; esac
171 comment_for_reflog continue
173 test -d "$DOTEST" || die "No interactive rebase running"
175 require_clean_work_tree
179 comment_for_reflog abort
181 test -d "$DOTEST" || die "No interactive rebase running"
183 HEADNAME=$(cat "$DOTEST"/head-name)
184 HEAD=$(cat "$DOTEST"/head)
185 git symbolic-ref HEAD $HEADNAME &&
186 git reset --hard $HEAD &&
191 comment_for_reflog skip
193 test -d "$DOTEST" || die "No interactive rebase running"
195 git reset --hard && do_rest
201 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
210 # we use merge anyway
213 die "Interactive rebase uses merge, so $1 does not make sense"
226 die "Interactive rebase already started"
228 git var GIT_COMMITTER_IDENT >/dev/null ||
229 die "You need to set your committer info first"
231 comment_for_reflog start
236 ONTO=$(git rev-parse --verify "$2") ||
237 die "Does not point to a valid commit: $2"
242 require_clean_work_tree
246 git show-ref --verify --quiet "refs/heads/$2" ||
247 die "Invalid branchname: $2"
249 die "Could not checkout $2"
252 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
253 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
255 test -z "$ONTO" && ONTO=$UPSTREAM
257 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
258 : > "$DOTEST"/interactive || die "Could not mark as interactive"
259 git symbolic-ref HEAD > "$DOTEST"/head-name ||
260 die "Could not get HEAD"
262 echo $HEAD > "$DOTEST"/head
263 echo $UPSTREAM > "$DOTEST"/upstream
264 echo $ONTO > "$DOTEST"/onto
265 test t = "$VERBOSE" && : > "$DOTEST"/verbose
268 # Rebasing $UPSTREAM..$HEAD onto $ONTO
272 # edit = use commit, but stop for amending
273 # squash = use commit, but meld into previous commit
275 git rev-list --no-merges --pretty=oneline --abbrev-commit \
276 --abbrev=7 --reverse $UPSTREAM..$HEAD | \
277 sed "s/^/pick /" >> "$TODO"
279 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
282 cp "$TODO" "$TODO".backup
283 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
284 die "Could not execute editor"
286 git reset --hard $ONTO && do_rest