rebase -i: remove leftover debugging
[git/git.git] / git-rebase--interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Johannes E. Schindelin
4
5 # SHORT DESCRIPTION
6 #
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
9 #
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
12
13 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
17 --
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
29 "
30
31 . git-sh-setup
32 require_work_tree
33
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=
44
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
49
50 warn () {
51 echo "$*" >&2
52 }
53
54 output () {
55 case "$VERBOSE" in
56 '')
57 output=$("$@" 2>&1 )
58 status=$?
59 test $status != 0 && printf "%s\n" "$output"
60 return $status
61 ;;
62 *)
63 "$@"
64 ;;
65 esac
66 }
67
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"
75 }
76
77 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
78
79 comment_for_reflog () {
80 case "$ORIG_REFLOG_ACTION" in
81 ''|rebase*)
82 GIT_REFLOG_ACTION="rebase -i ($1)"
83 export GIT_REFLOG_ACTION
84 ;;
85 esac
86 }
87
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
100 fi
101 }
102
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
111 }
112
113 die_with_patch () {
114 make_patch "$1"
115 git rerere
116 die "$2"
117 }
118
119 die_abort () {
120 rm -rf "$DOTEST"
121 die "$1"
122 }
123
124 has_action () {
125 grep '^[^#]' "$1" >/dev/null
126 }
127
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 "$@"
144 fi
145 }
146
147 pick_one_preserving_merges () {
148 fast_forward=t
149 case "$1" in
150 -n)
151 fast_forward=f
152 sha1=$2
153 ;;
154 *)
155 sha1=$1
156 ;;
157 esac
158 sha1=$(git rev-parse $sha1)
159
160 if test -f "$DOTEST"/current-commit
161 then
162 current_commit=$(cat "$DOTEST"/current-commit) &&
163 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
164 rm "$DOTEST"/current-commit ||
165 die "Cannot write current commit's replacement sha1"
166 fi
167
168 echo $sha1 > "$DOTEST"/current-commit
169
170 # rewrite parents; if none were rewritten, we can fast-forward.
171 new_parents=
172 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
173 do
174 if test -f "$REWRITTEN"/$p
175 then
176 new_p=$(cat "$REWRITTEN"/$p)
177 test $p != $new_p && fast_forward=f
178 case "$new_parents" in
179 *$new_p*)
180 ;; # do nothing; that parent is already there
181 *)
182 new_parents="$new_parents $new_p"
183 ;;
184 esac
185 else
186 new_parents="$new_parents $p"
187 fi
188 done
189 case $fast_forward in
190 t)
191 output warn "Fast forward to $sha1"
192 output git reset --hard $sha1 ||
193 die "Cannot fast forward to $sha1"
194 ;;
195 f)
196 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
197
198 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
199 # detach HEAD to current parent
200 output git checkout $first_parent 2> /dev/null ||
201 die "Cannot move HEAD to $first_parent"
202
203 case "$new_parents" in
204 ' '*' '*)
205 # redo merge
206 author_script=$(get_author_ident_from_commit $sha1)
207 eval "$author_script"
208 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
209 # No point in merging the first parent, that's HEAD
210 new_parents=${new_parents# $first_parent}
211 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
212 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
213 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
214 output git merge $STRATEGY -m "$msg" \
215 $new_parents
216 then
217 git rerere
218 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
219 die Error redoing merge $sha1
220 fi
221 ;;
222 *)
223 output git cherry-pick "$@" ||
224 die_with_patch $sha1 "Could not pick $sha1"
225 ;;
226 esac
227 ;;
228 esac
229 }
230
231 nth_string () {
232 case "$1" in
233 *1[0-9]|*[04-9]) echo "$1"th;;
234 *1) echo "$1"st;;
235 *2) echo "$1"nd;;
236 *3) echo "$1"rd;;
237 esac
238 }
239
240 make_squash_message () {
241 if test -f "$SQUASH_MSG"; then
242 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
243 < "$SQUASH_MSG" | sed -ne '$p')+1))
244 echo "# This is a combination of $COUNT commits."
245 sed -e 1d -e '2,/^./{
246 /^$/d
247 }' <"$SQUASH_MSG"
248 else
249 COUNT=2
250 echo "# This is a combination of two commits."
251 echo "# The first commit's message is:"
252 echo
253 git cat-file commit HEAD | sed -e '1,/^$/d'
254 fi
255 echo
256 echo "# This is the $(nth_string $COUNT) commit message:"
257 echo
258 git cat-file commit $1 | sed -e '1,/^$/d'
259 }
260
261 peek_next_command () {
262 sed -n "1s/ .*$//p" < "$TODO"
263 }
264
265 do_next () {
266 rm -f "$DOTEST"/message "$DOTEST"/author-script \
267 "$DOTEST"/amend || exit
268 read command sha1 rest < "$TODO"
269 case "$command" in
270 '#'*|'')
271 mark_action_done
272 ;;
273 pick|p)
274 comment_for_reflog pick
275
276 mark_action_done
277 pick_one $sha1 ||
278 die_with_patch $sha1 "Could not apply $sha1... $rest"
279 ;;
280 edit|e)
281 comment_for_reflog edit
282
283 mark_action_done
284 pick_one $sha1 ||
285 die_with_patch $sha1 "Could not apply $sha1... $rest"
286 make_patch $sha1
287 git rev-parse --verify HEAD > "$DOTEST"/amend
288 warn "Stopped at $sha1... $rest"
289 warn "You can amend the commit now, with"
290 warn
291 warn " git commit --amend"
292 warn
293 warn "Once you are satisfied with your changes, run"
294 warn
295 warn " git rebase --continue"
296 warn
297 exit 0
298 ;;
299 squash|s)
300 comment_for_reflog squash
301
302 has_action "$DONE" ||
303 die "Cannot 'squash' without a previous commit"
304
305 mark_action_done
306 make_squash_message $sha1 > "$MSG"
307 failed=f
308 author_script=$(get_author_ident_from_commit HEAD)
309 output git reset --soft HEAD^
310 pick_one -n $sha1 || failed=t
311 case "$(peek_next_command)" in
312 squash|s)
313 EDIT_COMMIT=
314 USE_OUTPUT=output
315 MSG_OPT=-F
316 MSG_FILE="$MSG"
317 cp "$MSG" "$SQUASH_MSG"
318 ;;
319 *)
320 EDIT_COMMIT=-e
321 USE_OUTPUT=
322 MSG_OPT=
323 MSG_FILE=
324 rm -f "$SQUASH_MSG" || exit
325 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
326 rm -f "$GIT_DIR"/MERGE_MSG || exit
327 ;;
328 esac
329 echo "$author_script" > "$DOTEST"/author-script
330 if test $failed = f
331 then
332 # This is like --amend, but with a different message
333 eval "$author_script"
334 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
335 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
336 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
337 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
338 fi
339 if test $failed = t
340 then
341 cp "$MSG" "$GIT_DIR"/MERGE_MSG
342 warn
343 warn "Could not apply $sha1... $rest"
344 die_with_patch $sha1 ""
345 fi
346 ;;
347 *)
348 warn "Unknown command: $command $sha1 $rest"
349 die_with_patch $sha1 "Please fix this in the file $TODO."
350 ;;
351 esac
352 test -s "$TODO" && return
353
354 comment_for_reflog finish &&
355 HEADNAME=$(cat "$DOTEST"/head-name) &&
356 OLDHEAD=$(cat "$DOTEST"/head) &&
357 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
358 if test -d "$REWRITTEN"
359 then
360 test -f "$DOTEST"/current-commit &&
361 current_commit=$(cat "$DOTEST"/current-commit) &&
362 git rev-parse HEAD > "$REWRITTEN"/$current_commit
363 if test -f "$REWRITTEN"/$OLDHEAD
364 then
365 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
366 else
367 NEWHEAD=$OLDHEAD
368 fi
369 else
370 NEWHEAD=$(git rev-parse HEAD)
371 fi &&
372 case $HEADNAME in
373 refs/*)
374 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
375 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
376 git symbolic-ref HEAD $HEADNAME
377 ;;
378 esac && {
379 test ! -f "$DOTEST"/verbose ||
380 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
381 } &&
382 rm -rf "$DOTEST" &&
383 git gc --auto &&
384 warn "Successfully rebased and updated $HEADNAME."
385
386 exit
387 }
388
389 do_rest () {
390 while :
391 do
392 do_next
393 done
394 }
395
396 # check if no other options are set
397 is_standalone () {
398 test $# -eq 2 -a "$2" = '--' &&
399 test -z "$ONTO" &&
400 test -z "$PRESERVE_MERGES" &&
401 test -z "$STRATEGY" &&
402 test -z "$VERBOSE"
403 }
404
405 get_saved_options () {
406 test -d "$REWRITTEN" && PRESERVE_MERGES=t
407 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
408 test -f "$DOTEST"/verbose && VERBOSE=t
409 }
410
411 while test $# != 0
412 do
413 case "$1" in
414 --continue)
415 is_standalone "$@" || usage
416 get_saved_options
417 comment_for_reflog continue
418
419 test -d "$DOTEST" || die "No interactive rebase running"
420
421 # Sanity check
422 git rev-parse --verify HEAD >/dev/null ||
423 die "Cannot read HEAD"
424 git update-index --ignore-submodules --refresh &&
425 git diff-files --quiet --ignore-submodules ||
426 die "Working tree is dirty"
427
428 # do we have anything to commit?
429 if git diff-index --cached --quiet --ignore-submodules HEAD --
430 then
431 : Nothing to commit -- skip this
432 else
433 . "$DOTEST"/author-script ||
434 die "Cannot find the author identity"
435 amend=
436 if test -f "$DOTEST"/amend
437 then
438 amend=$(git rev-parse --verify HEAD)
439 test "$amend" = $(cat "$DOTEST"/amend) ||
440 die "\
441 You have uncommitted changes in your working tree. Please, commit them
442 first and then run 'git rebase --continue' again."
443 git reset --soft HEAD^ ||
444 die "Cannot rewind the HEAD"
445 fi
446 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
447 git commit --no-verify -F "$DOTEST"/message -e || {
448 test -n "$amend" && git reset --soft $amend
449 die "Could not commit staged changes."
450 }
451 fi
452
453 require_clean_work_tree
454 do_rest
455 ;;
456 --abort)
457 is_standalone "$@" || usage
458 get_saved_options
459 comment_for_reflog abort
460
461 git rerere clear
462 test -d "$DOTEST" || die "No interactive rebase running"
463
464 HEADNAME=$(cat "$DOTEST"/head-name)
465 HEAD=$(cat "$DOTEST"/head)
466 case $HEADNAME in
467 refs/*)
468 git symbolic-ref HEAD $HEADNAME
469 ;;
470 esac &&
471 output git reset --hard $HEAD &&
472 rm -rf "$DOTEST"
473 exit
474 ;;
475 --skip)
476 is_standalone "$@" || usage
477 get_saved_options
478 comment_for_reflog skip
479
480 git rerere clear
481 test -d "$DOTEST" || die "No interactive rebase running"
482
483 output git reset --hard && do_rest
484 ;;
485 -s)
486 case "$#,$1" in
487 *,*=*)
488 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
489 1,*)
490 usage ;;
491 *)
492 STRATEGY="-s $2"
493 shift ;;
494 esac
495 ;;
496 -m)
497 # we use merge anyway
498 ;;
499 -v)
500 VERBOSE=t
501 ;;
502 -p)
503 PRESERVE_MERGES=t
504 ;;
505 -i)
506 # yeah, we know
507 ;;
508 --onto)
509 shift
510 ONTO=$(git rev-parse --verify "$1") ||
511 die "Does not point to a valid commit: $1"
512 ;;
513 --)
514 shift
515 test $# -eq 1 -o $# -eq 2 || usage
516 test -d "$DOTEST" &&
517 die "Interactive rebase already started"
518
519 git var GIT_COMMITTER_IDENT >/dev/null ||
520 die "You need to set your committer info first"
521
522 comment_for_reflog start
523
524 require_clean_work_tree
525
526 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
527 test -z "$ONTO" && ONTO=$UPSTREAM
528
529 if test ! -z "$2"
530 then
531 output git show-ref --verify --quiet "refs/heads/$2" ||
532 die "Invalid branchname: $2"
533 output git checkout "$2" ||
534 die "Could not checkout $2"
535 fi
536
537 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
538 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
539
540 : > "$DOTEST"/interactive || die "Could not mark as interactive"
541 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
542 echo "detached HEAD" > "$DOTEST"/head-name
543
544 echo $HEAD > "$DOTEST"/head
545 echo $UPSTREAM > "$DOTEST"/upstream
546 echo $ONTO > "$DOTEST"/onto
547 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
548 test t = "$VERBOSE" && : > "$DOTEST"/verbose
549 if test t = "$PRESERVE_MERGES"
550 then
551 # $REWRITTEN contains files for each commit that is
552 # reachable by at least one merge base of $HEAD and
553 # $UPSTREAM. They are not necessarily rewritten, but
554 # their children might be.
555 # This ensures that commits on merged, but otherwise
556 # unrelated side branches are left alone. (Think "X"
557 # in the man page's example.)
558 mkdir "$REWRITTEN" &&
559 for c in $(git merge-base --all $HEAD $UPSTREAM)
560 do
561 echo $ONTO > "$REWRITTEN"/$c ||
562 die "Could not init rewritten commits"
563 done
564 MERGES_OPTION=
565 else
566 MERGES_OPTION=--no-merges
567 fi
568
569 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
570 SHORTHEAD=$(git rev-parse --short $HEAD)
571 SHORTONTO=$(git rev-parse --short $ONTO)
572 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
573 --abbrev=7 --reverse --left-right --cherry-pick \
574 $UPSTREAM...$HEAD | \
575 sed -n "s/^>/pick /p" > "$TODO"
576 cat >> "$TODO" << EOF
577
578 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
579 #
580 # Commands:
581 # p, pick = use commit
582 # e, edit = use commit, but stop for amending
583 # s, squash = use commit, but meld into previous commit
584 #
585 # If you remove a line here THAT COMMIT WILL BE LOST.
586 # However, if you remove everything, the rebase will be aborted.
587 #
588 EOF
589
590 has_action "$TODO" ||
591 die_abort "Nothing to do"
592
593 cp "$TODO" "$TODO".backup
594 git_editor "$TODO" ||
595 die "Could not execute editor"
596
597 has_action "$TODO" ||
598 die_abort "Nothing to do"
599
600 git update-ref ORIG_HEAD $HEAD
601 output git checkout $ONTO && do_rest
602 ;;
603 esac
604 shift
605 done