Code

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