rebase -i: When an 'edit' stops, mention the commit
[git/platforms.git] / git-rebase--interactive.sh
blob4e334ba41dad3067394b79c15ebfe610b2d3e178
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
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 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
18 Available options are
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 m,merge always used (no-op)
24 i,interactive always used (no-op)
25 Actions:
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
31 . git-sh-setup
32 require_work_tree
34 DOTEST="$GIT_DIR/rebase-merge"
35 TODO="$DOTEST"/git-rebase-todo
36 DONE="$DOTEST"/done
37 MSG="$DOTEST"/message
38 SQUASH_MSG="$DOTEST"/message-squash
39 REWRITTEN="$DOTEST"/rewritten
40 PRESERVE_MERGES=
41 STRATEGY=
42 ONTO=
43 VERBOSE=
45 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
46 mark the corrected paths with 'git add <paths>', and
47 run 'git rebase --continue'"
48 export GIT_CHERRY_PICK_HELP
50 warn () {
51 echo "$*" >&2
54 output () {
55 case "$VERBOSE" in
56 '')
57 output=$("$@" 2>&1 )
58 status=$?
59 test $status != 0 && printf "%s\n" "$output"
60 return $status
63 "$@"
65 esac
68 require_clean_work_tree () {
69 # test if working tree is dirty
70 git rev-parse --verify HEAD > /dev/null &&
71 git update-index --ignore-submodules --refresh &&
72 git diff-files --quiet --ignore-submodules &&
73 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
74 die "Working tree is dirty"
77 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
79 comment_for_reflog () {
80 case "$ORIG_REFLOG_ACTION" in
81 ''|rebase*)
82 GIT_REFLOG_ACTION="rebase -i ($1)"
83 export GIT_REFLOG_ACTION
85 esac
88 last_count=
89 mark_action_done () {
90 sed -e 1q < "$TODO" >> "$DONE"
91 sed -e 1d < "$TODO" >> "$TODO".new
92 mv -f "$TODO".new "$TODO"
93 count=$(grep -c '^[^#]' < "$DONE")
94 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
95 if test "$last_count" != "$count"
96 then
97 last_count=$count
98 printf "Rebasing (%d/%d)\r" $count $total
99 test -z "$VERBOSE" || echo
103 make_patch () {
104 parent_sha1=$(git rev-parse --verify "$1"^) ||
105 die "Cannot get patch for $1^"
106 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
107 test -f "$DOTEST"/message ||
108 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
109 test -f "$DOTEST"/author-script ||
110 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
113 die_with_patch () {
114 make_patch "$1"
115 git rerere
116 die "$2"
119 die_abort () {
120 rm -rf "$DOTEST"
121 die "$1"
124 has_action () {
125 grep '^[^#]' "$1" >/dev/null
128 pick_one () {
129 no_ff=
130 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
131 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
132 test -d "$REWRITTEN" &&
133 pick_one_preserving_merges "$@" && return
134 parent_sha1=$(git rev-parse --verify $sha1^) ||
135 die "Could not get the parent of $sha1"
136 current_sha1=$(git rev-parse --verify HEAD)
137 if test "$no_ff$current_sha1" = "$parent_sha1"; then
138 output git reset --hard $sha1
139 test "a$1" = a-n && output git reset --soft $current_sha1
140 sha1=$(git rev-parse --short $sha1)
141 output warn Fast forward to $sha1
142 else
143 output git cherry-pick "$@"
147 pick_one_preserving_merges () {
148 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
149 sha1=$(git rev-parse $sha1)
151 if test -f "$DOTEST"/current-commit
152 then
153 current_commit=$(cat "$DOTEST"/current-commit) &&
154 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
155 rm "$DOTEST"/current-commit ||
156 die "Cannot write current commit's replacement sha1"
159 # rewrite parents; if none were rewritten, we can fast-forward.
160 fast_forward=t
161 preserve=t
162 new_parents=
163 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
165 if test -f "$REWRITTEN"/$p
166 then
167 preserve=f
168 new_p=$(cat "$REWRITTEN"/$p)
169 test $p != $new_p && fast_forward=f
170 case "$new_parents" in
171 *$new_p*)
172 ;; # do nothing; that parent is already there
174 new_parents="$new_parents $new_p"
176 esac
177 else
178 new_parents="$new_parents $p"
180 done
181 case $fast_forward in
183 output warn "Fast forward to $sha1"
184 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
187 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
189 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
190 # detach HEAD to current parent
191 output git checkout $first_parent 2> /dev/null ||
192 die "Cannot move HEAD to $first_parent"
194 echo $sha1 > "$DOTEST"/current-commit
195 case "$new_parents" in
196 ' '*' '*)
197 # redo merge
198 author_script=$(get_author_ident_from_commit $sha1)
199 eval "$author_script"
200 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
201 # No point in merging the first parent, that's HEAD
202 new_parents=${new_parents# $first_parent}
203 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
204 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
205 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
206 output git merge $STRATEGY -m "$msg" \
207 $new_parents
208 then
209 git rerere
210 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
211 die Error redoing merge $sha1
215 output git cherry-pick "$@" ||
216 die_with_patch $sha1 "Could not pick $sha1"
218 esac
220 esac
223 nth_string () {
224 case "$1" in
225 *1[0-9]|*[04-9]) echo "$1"th;;
226 *1) echo "$1"st;;
227 *2) echo "$1"nd;;
228 *3) echo "$1"rd;;
229 esac
232 make_squash_message () {
233 if test -f "$SQUASH_MSG"; then
234 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
235 < "$SQUASH_MSG" | sed -ne '$p')+1))
236 echo "# This is a combination of $COUNT commits."
237 sed -e 1d -e '2,/^./{
238 /^$/d
239 }' <"$SQUASH_MSG"
240 else
241 COUNT=2
242 echo "# This is a combination of two commits."
243 echo "# The first commit's message is:"
244 echo
245 git cat-file commit HEAD | sed -e '1,/^$/d'
247 echo
248 echo "# This is the $(nth_string $COUNT) commit message:"
249 echo
250 git cat-file commit $1 | sed -e '1,/^$/d'
253 peek_next_command () {
254 sed -n "1s/ .*$//p" < "$TODO"
257 do_next () {
258 rm -f "$DOTEST"/message "$DOTEST"/author-script \
259 "$DOTEST"/amend || exit
260 read command sha1 rest < "$TODO"
261 case "$command" in
262 '#'*|'')
263 mark_action_done
265 pick|p)
266 comment_for_reflog pick
268 mark_action_done
269 pick_one $sha1 ||
270 die_with_patch $sha1 "Could not apply $sha1... $rest"
272 edit|e)
273 comment_for_reflog edit
275 mark_action_done
276 pick_one $sha1 ||
277 die_with_patch $sha1 "Could not apply $sha1... $rest"
278 make_patch $sha1
279 : > "$DOTEST"/amend
280 warn "Stopped at $sha1... $rest"
281 warn "You can amend the commit now, with"
282 warn
283 warn " git commit --amend"
284 warn
285 warn "Once you are satisfied with your changes, run"
286 warn
287 warn " git rebase --continue"
288 warn
289 exit 0
291 squash|s)
292 comment_for_reflog squash
294 has_action "$DONE" ||
295 die "Cannot 'squash' without a previous commit"
297 mark_action_done
298 make_squash_message $sha1 > "$MSG"
299 case "$(peek_next_command)" in
300 squash|s)
301 EDIT_COMMIT=
302 USE_OUTPUT=output
303 cp "$MSG" "$SQUASH_MSG"
306 EDIT_COMMIT=-e
307 USE_OUTPUT=
308 rm -f "$SQUASH_MSG" || exit
310 esac
312 failed=f
313 author_script=$(get_author_ident_from_commit HEAD)
314 output git reset --soft HEAD^
315 pick_one -n $sha1 || failed=t
316 echo "$author_script" > "$DOTEST"/author-script
317 if test $failed = f
318 then
319 # This is like --amend, but with a different message
320 eval "$author_script"
321 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
322 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
323 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
324 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
326 if test $failed = t
327 then
328 cp "$MSG" "$GIT_DIR"/MERGE_MSG
329 warn
330 warn "Could not apply $sha1... $rest"
331 die_with_patch $sha1 ""
335 warn "Unknown command: $command $sha1 $rest"
336 die_with_patch $sha1 "Please fix this in the file $TODO."
338 esac
339 test -s "$TODO" && return
341 comment_for_reflog finish &&
342 HEADNAME=$(cat "$DOTEST"/head-name) &&
343 OLDHEAD=$(cat "$DOTEST"/head) &&
344 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
345 if test -d "$REWRITTEN"
346 then
347 test -f "$DOTEST"/current-commit &&
348 current_commit=$(cat "$DOTEST"/current-commit) &&
349 git rev-parse HEAD > "$REWRITTEN"/$current_commit
350 if test -f "$REWRITTEN"/$OLDHEAD
351 then
352 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
353 else
354 NEWHEAD=$OLDHEAD
356 else
357 NEWHEAD=$(git rev-parse HEAD)
358 fi &&
359 case $HEADNAME in
360 refs/*)
361 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
362 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
363 git symbolic-ref HEAD $HEADNAME
365 esac && {
366 test ! -f "$DOTEST"/verbose ||
367 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
368 } &&
369 rm -rf "$DOTEST" &&
370 git gc --auto &&
371 warn "Successfully rebased and updated $HEADNAME."
373 exit
376 do_rest () {
377 while :
379 do_next
380 done
383 # check if no other options are set
384 is_standalone () {
385 test $# -eq 2 -a "$2" = '--' &&
386 test -z "$ONTO" &&
387 test -z "$PRESERVE_MERGES" &&
388 test -z "$STRATEGY" &&
389 test -z "$VERBOSE"
392 get_saved_options () {
393 test -d "$REWRITTEN" && PRESERVE_MERGES=t
394 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
395 test -f "$DOTEST"/verbose && VERBOSE=t
398 while test $# != 0
400 case "$1" in
401 --continue)
402 is_standalone "$@" || usage
403 get_saved_options
404 comment_for_reflog continue
406 test -d "$DOTEST" || die "No interactive rebase running"
408 # Sanity check
409 git rev-parse --verify HEAD >/dev/null ||
410 die "Cannot read HEAD"
411 git update-index --ignore-submodules --refresh &&
412 git diff-files --quiet --ignore-submodules ||
413 die "Working tree is dirty"
415 # do we have anything to commit?
416 if git diff-index --cached --quiet --ignore-submodules HEAD --
417 then
418 : Nothing to commit -- skip this
419 else
420 . "$DOTEST"/author-script ||
421 die "Cannot find the author identity"
422 if test -f "$DOTEST"/amend
423 then
424 git reset --soft HEAD^ ||
425 die "Cannot rewind the HEAD"
427 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
428 git commit --no-verify -F "$DOTEST"/message -e ||
429 die "Could not commit staged changes."
432 require_clean_work_tree
433 do_rest
435 --abort)
436 is_standalone "$@" || usage
437 get_saved_options
438 comment_for_reflog abort
440 git rerere clear
441 test -d "$DOTEST" || die "No interactive rebase running"
443 HEADNAME=$(cat "$DOTEST"/head-name)
444 HEAD=$(cat "$DOTEST"/head)
445 case $HEADNAME in
446 refs/*)
447 git symbolic-ref HEAD $HEADNAME
449 esac &&
450 output git reset --hard $HEAD &&
451 rm -rf "$DOTEST"
452 exit
454 --skip)
455 is_standalone "$@" || usage
456 get_saved_options
457 comment_for_reflog skip
459 git rerere clear
460 test -d "$DOTEST" || die "No interactive rebase running"
462 output git reset --hard && do_rest
465 case "$#,$1" in
466 *,*=*)
467 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
468 1,*)
469 usage ;;
471 STRATEGY="-s $2"
472 shift ;;
473 esac
476 # we use merge anyway
479 VERBOSE=t
482 PRESERVE_MERGES=t
485 # yeah, we know
487 --onto)
488 shift
489 ONTO=$(git rev-parse --verify "$1") ||
490 die "Does not point to a valid commit: $1"
493 shift
494 test $# -eq 1 -o $# -eq 2 || usage
495 test -d "$DOTEST" &&
496 die "Interactive rebase already started"
498 git var GIT_COMMITTER_IDENT >/dev/null ||
499 die "You need to set your committer info first"
501 comment_for_reflog start
503 require_clean_work_tree
505 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
506 test -z "$ONTO" && ONTO=$UPSTREAM
508 if test ! -z "$2"
509 then
510 output git show-ref --verify --quiet "refs/heads/$2" ||
511 die "Invalid branchname: $2"
512 output git checkout "$2" ||
513 die "Could not checkout $2"
516 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
517 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
519 : > "$DOTEST"/interactive || die "Could not mark as interactive"
520 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
521 echo "detached HEAD" > "$DOTEST"/head-name
523 echo $HEAD > "$DOTEST"/head
524 echo $UPSTREAM > "$DOTEST"/upstream
525 echo $ONTO > "$DOTEST"/onto
526 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
527 test t = "$VERBOSE" && : > "$DOTEST"/verbose
528 if test t = "$PRESERVE_MERGES"
529 then
530 # $REWRITTEN contains files for each commit that is
531 # reachable by at least one merge base of $HEAD and
532 # $UPSTREAM. They are not necessarily rewritten, but
533 # their children might be.
534 # This ensures that commits on merged, but otherwise
535 # unrelated side branches are left alone. (Think "X"
536 # in the man page's example.)
537 mkdir "$REWRITTEN" &&
538 for c in $(git merge-base --all $HEAD $UPSTREAM)
540 echo $ONTO > "$REWRITTEN"/$c ||
541 die "Could not init rewritten commits"
542 done
543 MERGES_OPTION=
544 else
545 MERGES_OPTION=--no-merges
548 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
549 SHORTHEAD=$(git rev-parse --short $HEAD)
550 SHORTONTO=$(git rev-parse --short $ONTO)
551 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
552 --abbrev=7 --reverse --left-right --cherry-pick \
553 $UPSTREAM...$HEAD | \
554 sed -n "s/^>/pick /p" > "$TODO"
555 cat >> "$TODO" << EOF
557 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
559 # Commands:
560 # p, pick = use commit
561 # e, edit = use commit, but stop for amending
562 # s, squash = use commit, but meld into previous commit
564 # If you remove a line here THAT COMMIT WILL BE LOST.
565 # However, if you remove everything, the rebase will be aborted.
569 has_action "$TODO" ||
570 die_abort "Nothing to do"
572 cp "$TODO" "$TODO".backup
573 git_editor "$TODO" ||
574 die "Could not execute editor"
576 has_action "$TODO" ||
577 die_abort "Nothing to do"
579 git update-ref ORIG_HEAD $HEAD
580 output git checkout $ONTO && do_rest
582 esac
583 shift
584 done