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 comment_for_reflog finish
&&
155 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
156 OLDHEAD
=$
(cat "$DOTEST"/head) &&
157 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
158 NEWHEAD
=$
(git rev-parse HEAD
) &&
159 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
160 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
161 git symbolic-ref HEAD
$HEADNAME &&
163 warn
"Successfully rebased and updated $HEADNAME."
173 test -f "$DOTEST"/verbose
&&
174 git
diff --stat $
(cat "$DOTEST"/head)..HEAD
178 while case $# in 0) break ;; esac
182 comment_for_reflog
continue
184 test -d "$DOTEST" || die
"No interactive rebase running"
186 require_clean_work_tree
190 comment_for_reflog abort
192 test -d "$DOTEST" || die
"No interactive rebase running"
194 HEADNAME
=$
(cat "$DOTEST"/head-name
)
195 HEAD
=$
(cat "$DOTEST"/head)
196 git symbolic-ref HEAD
$HEADNAME &&
197 git
reset --hard $HEAD &&
202 comment_for_reflog skip
204 test -d "$DOTEST" || die
"No interactive rebase running"
206 git
reset --hard && do_rest
212 STRATEGY
="-s `expr "z
$1" : 'z-[^=]*=\(.*\)'`" ;;
221 # we use merge anyway
224 die
"Interactive rebase uses merge, so $1 does not make sense"
237 die
"Interactive rebase already started"
239 git var GIT_COMMITTER_IDENT
>/dev
/null ||
240 die
"You need to set your committer info first"
242 comment_for_reflog start
247 ONTO
=$
(git rev-parse
--verify "$2") ||
248 die
"Does not point to a valid commit: $2"
253 require_clean_work_tree
257 git show-ref
--verify --quiet "refs/heads/$2" ||
258 die
"Invalid branchname: $2"
260 die
"Could not checkout $2"
263 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
264 UPSTREAM
=$
(git rev-parse
--verify "$1") || die
"Invalid base"
266 test -z "$ONTO" && ONTO
=$UPSTREAM
268 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
269 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
270 git symbolic-ref HEAD
> "$DOTEST"/head-name ||
271 die
"Could not get HEAD"
273 echo $HEAD > "$DOTEST"/head
274 echo $UPSTREAM > "$DOTEST"/upstream
275 echo $ONTO > "$DOTEST"/onto
276 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
278 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
279 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
280 SHORTONTO
=$
(git rev-parse
--short $ONTO)
282 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
286 # edit = use commit, but stop for amending
287 # squash = use commit, but meld into previous commit
289 git rev-list
--no-merges --pretty=oneline
--abbrev-commit \
290 --abbrev=7 --reverse $UPSTREAM..
$HEAD | \
291 sed "s/^/pick /" >> "$TODO"
293 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
294 die_abort
"Nothing to do"
296 cp "$TODO" "$TODO".backup
297 ${VISUAL:-${EDITOR:-vi}} "$TODO" ||
298 die
"Could not execute editor"
300 test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
301 die_abort
"Nothing to do"
303 git checkout
$ONTO && do_rest