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