Code

Merge branch 'sp/maint-index-pack' into maint
[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 USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14         [--onto <branch>] <upstream> [<branch>])'
16 OPTIONS_SPEC=
17 . git-sh-setup
18 require_work_tree
20 DOTEST="$GIT_DIR/.dotest-merge"
21 TODO="$DOTEST"/git-rebase-todo
22 DONE="$DOTEST"/done
23 MSG="$DOTEST"/message
24 SQUASH_MSG="$DOTEST"/message-squash
25 REWRITTEN="$DOTEST"/rewritten
26 PRESERVE_MERGES=
27 STRATEGY=
28 VERBOSE=
29 test -d "$REWRITTEN" && PRESERVE_MERGES=t
30 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31 test -f "$DOTEST"/verbose && VERBOSE=t
33 GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
34 mark the corrected paths with 'git add <paths>', and
35 run 'git rebase --continue'"
36 export GIT_CHERRY_PICK_HELP
38 warn () {
39         echo "$*" >&2
40 }
42 output () {
43         case "$VERBOSE" in
44         '')
45                 output=$("$@" 2>&1 )
46                 status=$?
47                 test $status != 0 && printf "%s\n" "$output"
48                 return $status
49                 ;;
50         *)
51                 "$@"
52                 ;;
53         esac
54 }
56 require_clean_work_tree () {
57         # test if working tree is dirty
58         git rev-parse --verify HEAD > /dev/null &&
59         git update-index --ignore-submodules --refresh &&
60         git diff-files --quiet --ignore-submodules &&
61         git diff-index --cached --quiet HEAD --ignore-submodules -- ||
62         die "Working tree is dirty"
63 }
65 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
67 comment_for_reflog () {
68         case "$ORIG_REFLOG_ACTION" in
69         ''|rebase*)
70                 GIT_REFLOG_ACTION="rebase -i ($1)"
71                 export GIT_REFLOG_ACTION
72                 ;;
73         esac
74 }
76 last_count=
77 mark_action_done () {
78         sed -e 1q < "$TODO" >> "$DONE"
79         sed -e 1d < "$TODO" >> "$TODO".new
80         mv -f "$TODO".new "$TODO"
81         count=$(grep -c '^[^#]' < "$DONE")
82         total=$(($count+$(grep -c '^[^#]' < "$TODO")))
83         if test "$last_count" != "$count"
84         then
85                 last_count=$count
86                 printf "Rebasing (%d/%d)\r" $count $total
87                 test -z "$VERBOSE" || echo
88         fi
89 }
91 make_patch () {
92         parent_sha1=$(git rev-parse --verify "$1"^) ||
93                 die "Cannot get patch for $1^"
94         git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
95         test -f "$DOTEST"/message ||
96                 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
97         test -f "$DOTEST"/author-script ||
98                 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
99 }
101 die_with_patch () {
102         make_patch "$1"
103         git rerere
104         die "$2"
107 die_abort () {
108         rm -rf "$DOTEST"
109         die "$1"
112 has_action () {
113         grep '^[^#]' "$1" >/dev/null
116 pick_one () {
117         no_ff=
118         case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
119         output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
120         test -d "$REWRITTEN" &&
121                 pick_one_preserving_merges "$@" && return
122         parent_sha1=$(git rev-parse --verify $sha1^) ||
123                 die "Could not get the parent of $sha1"
124         current_sha1=$(git rev-parse --verify HEAD)
125         if test "$no_ff$current_sha1" = "$parent_sha1"; then
126                 output git reset --hard $sha1
127                 test "a$1" = a-n && output git reset --soft $current_sha1
128                 sha1=$(git rev-parse --short $sha1)
129                 output warn Fast forward to $sha1
130         else
131                 output git cherry-pick "$@"
132         fi
135 pick_one_preserving_merges () {
136         case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
137         sha1=$(git rev-parse $sha1)
139         if test -f "$DOTEST"/current-commit
140         then
141                 current_commit=$(cat "$DOTEST"/current-commit) &&
142                 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
143                 rm "$DOTEST"/current-commit ||
144                 die "Cannot write current commit's replacement sha1"
145         fi
147         # rewrite parents; if none were rewritten, we can fast-forward.
148         fast_forward=t
149         preserve=t
150         new_parents=
151         for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
152         do
153                 if test -f "$REWRITTEN"/$p
154                 then
155                         preserve=f
156                         new_p=$(cat "$REWRITTEN"/$p)
157                         test $p != $new_p && fast_forward=f
158                         case "$new_parents" in
159                         *$new_p*)
160                                 ;; # do nothing; that parent is already there
161                         *)
162                                 new_parents="$new_parents $new_p"
163                                 ;;
164                         esac
165                 else
166                         new_parents="$new_parents $p"
167                 fi
168         done
169         case $fast_forward in
170         t)
171                 output warn "Fast forward to $sha1"
172                 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
173                 ;;
174         f)
175                 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
177                 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
178                 # detach HEAD to current parent
179                 output git checkout $first_parent 2> /dev/null ||
180                         die "Cannot move HEAD to $first_parent"
182                 echo $sha1 > "$DOTEST"/current-commit
183                 case "$new_parents" in
184                 ' '*' '*)
185                         # redo merge
186                         author_script=$(get_author_ident_from_commit $sha1)
187                         eval "$author_script"
188                         msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
189                         # No point in merging the first parent, that's HEAD
190                         new_parents=${new_parents# $first_parent}
191                         if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
192                                 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
193                                 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
194                                 output git merge $STRATEGY -m "$msg" \
195                                         $new_parents
196                         then
197                                 git rerere
198                                 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
199                                 die Error redoing merge $sha1
200                         fi
201                         ;;
202                 *)
203                         output git cherry-pick "$@" ||
204                                 die_with_patch $sha1 "Could not pick $sha1"
205                         ;;
206                 esac
207                 ;;
208         esac
211 nth_string () {
212         case "$1" in
213         *1[0-9]|*[04-9]) echo "$1"th;;
214         *1) echo "$1"st;;
215         *2) echo "$1"nd;;
216         *3) echo "$1"rd;;
217         esac
220 make_squash_message () {
221         if test -f "$SQUASH_MSG"; then
222                 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
223                         < "$SQUASH_MSG" | sed -ne '$p')+1))
224                 echo "# This is a combination of $COUNT commits."
225                 sed -e 1d -e '2,/^./{
226                         /^$/d
227                 }' <"$SQUASH_MSG"
228         else
229                 COUNT=2
230                 echo "# This is a combination of two commits."
231                 echo "# The first commit's message is:"
232                 echo
233                 git cat-file commit HEAD | sed -e '1,/^$/d'
234         fi
235         echo
236         echo "# This is the $(nth_string $COUNT) commit message:"
237         echo
238         git cat-file commit $1 | sed -e '1,/^$/d'
241 peek_next_command () {
242         sed -n "1s/ .*$//p" < "$TODO"
245 do_next () {
246         rm -f "$DOTEST"/message "$DOTEST"/author-script \
247                 "$DOTEST"/amend || exit
248         read command sha1 rest < "$TODO"
249         case "$command" in
250         '#'*|'')
251                 mark_action_done
252                 ;;
253         pick|p)
254                 comment_for_reflog pick
256                 mark_action_done
257                 pick_one $sha1 ||
258                         die_with_patch $sha1 "Could not apply $sha1... $rest"
259                 ;;
260         edit|e)
261                 comment_for_reflog edit
263                 mark_action_done
264                 pick_one $sha1 ||
265                         die_with_patch $sha1 "Could not apply $sha1... $rest"
266                 make_patch $sha1
267                 : > "$DOTEST"/amend
268                 warn
269                 warn "You can amend the commit now, with"
270                 warn
271                 warn "  git commit --amend"
272                 warn
273                 warn "Once you are satisfied with your changes, run"
274                 warn
275                 warn "  git rebase --continue"
276                 warn
277                 exit 0
278                 ;;
279         squash|s)
280                 comment_for_reflog squash
282                 has_action "$DONE" ||
283                         die "Cannot 'squash' without a previous commit"
285                 mark_action_done
286                 make_squash_message $sha1 > "$MSG"
287                 case "$(peek_next_command)" in
288                 squash|s)
289                         EDIT_COMMIT=
290                         USE_OUTPUT=output
291                         cp "$MSG" "$SQUASH_MSG"
292                         ;;
293                 *)
294                         EDIT_COMMIT=-e
295                         USE_OUTPUT=
296                         rm -f "$SQUASH_MSG" || exit
297                         ;;
298                 esac
300                 failed=f
301                 author_script=$(get_author_ident_from_commit HEAD)
302                 output git reset --soft HEAD^
303                 pick_one -n $sha1 || failed=t
304                 echo "$author_script" > "$DOTEST"/author-script
305                 if test $failed = f
306                 then
307                         # This is like --amend, but with a different message
308                         eval "$author_script"
309                         GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
310                         GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
311                         GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
312                         $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
313                 fi
314                 if test $failed = t
315                 then
316                         cp "$MSG" "$GIT_DIR"/MERGE_MSG
317                         warn
318                         warn "Could not apply $sha1... $rest"
319                         die_with_patch $sha1 ""
320                 fi
321                 ;;
322         *)
323                 warn "Unknown command: $command $sha1 $rest"
324                 die_with_patch $sha1 "Please fix this in the file $TODO."
325                 ;;
326         esac
327         test -s "$TODO" && return
329         comment_for_reflog finish &&
330         HEADNAME=$(cat "$DOTEST"/head-name) &&
331         OLDHEAD=$(cat "$DOTEST"/head) &&
332         SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
333         if test -d "$REWRITTEN"
334         then
335                 test -f "$DOTEST"/current-commit &&
336                         current_commit=$(cat "$DOTEST"/current-commit) &&
337                         git rev-parse HEAD > "$REWRITTEN"/$current_commit
338                 if test -f "$REWRITTEN"/$OLDHEAD
339                 then
340                         NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
341                 else
342                         NEWHEAD=$OLDHEAD
343                 fi
344         else
345                 NEWHEAD=$(git rev-parse HEAD)
346         fi &&
347         case $HEADNAME in
348         refs/*)
349                 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
350                 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
351                 git symbolic-ref HEAD $HEADNAME
352                 ;;
353         esac && {
354                 test ! -f "$DOTEST"/verbose ||
355                         git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
356         } &&
357         rm -rf "$DOTEST" &&
358         git gc --auto &&
359         warn "Successfully rebased and updated $HEADNAME."
361         exit
364 do_rest () {
365         while :
366         do
367                 do_next
368         done
371 while test $# != 0
372 do
373         case "$1" in
374         --continue)
375                 comment_for_reflog continue
377                 test -d "$DOTEST" || die "No interactive rebase running"
379                 # Sanity check
380                 git rev-parse --verify HEAD >/dev/null ||
381                         die "Cannot read HEAD"
382                 git update-index --ignore-submodules --refresh &&
383                         git diff-files --quiet --ignore-submodules ||
384                         die "Working tree is dirty"
386                 # do we have anything to commit?
387                 if git diff-index --cached --quiet --ignore-submodules HEAD --
388                 then
389                         : Nothing to commit -- skip this
390                 else
391                         . "$DOTEST"/author-script ||
392                                 die "Cannot find the author identity"
393                         if test -f "$DOTEST"/amend
394                         then
395                                 git reset --soft HEAD^ ||
396                                 die "Cannot rewind the HEAD"
397                         fi
398                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
399                         git commit --no-verify -F "$DOTEST"/message -e ||
400                         die "Could not commit staged changes."
401                 fi
403                 require_clean_work_tree
404                 do_rest
405                 ;;
406         --abort)
407                 comment_for_reflog abort
409                 git rerere clear
410                 test -d "$DOTEST" || die "No interactive rebase running"
412                 HEADNAME=$(cat "$DOTEST"/head-name)
413                 HEAD=$(cat "$DOTEST"/head)
414                 case $HEADNAME in
415                 refs/*)
416                         git symbolic-ref HEAD $HEADNAME
417                         ;;
418                 esac &&
419                 output git reset --hard $HEAD &&
420                 rm -rf "$DOTEST"
421                 exit
422                 ;;
423         --skip)
424                 comment_for_reflog skip
426                 git rerere clear
427                 test -d "$DOTEST" || die "No interactive rebase running"
429                 output git reset --hard && do_rest
430                 ;;
431         -s|--strategy)
432                 case "$#,$1" in
433                 *,*=*)
434                         STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
435                 1,*)
436                         usage ;;
437                 *)
438                         STRATEGY="-s $2"
439                         shift ;;
440                 esac
441                 ;;
442         -m|--merge)
443                 # we use merge anyway
444                 ;;
445         -C*)
446                 die "Interactive rebase uses merge, so $1 does not make sense"
447                 ;;
448         -v|--verbose)
449                 VERBOSE=t
450                 ;;
451         -p|--preserve-merges)
452                 PRESERVE_MERGES=t
453                 ;;
454         -i|--interactive)
455                 # yeah, we know
456                 ;;
457         ''|-h)
458                 usage
459                 ;;
460         *)
461                 test -d "$DOTEST" &&
462                         die "Interactive rebase already started"
464                 git var GIT_COMMITTER_IDENT >/dev/null ||
465                         die "You need to set your committer info first"
467                 comment_for_reflog start
469                 ONTO=
470                 case "$1" in
471                 --onto)
472                         ONTO=$(git rev-parse --verify "$2") ||
473                                 die "Does not point to a valid commit: $2"
474                         shift; shift
475                         ;;
476                 esac
478                 require_clean_work_tree
480                 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
481                 test -z "$ONTO" && ONTO=$UPSTREAM
483                 if test ! -z "$2"
484                 then
485                         output git show-ref --verify --quiet "refs/heads/$2" ||
486                                 die "Invalid branchname: $2"
487                         output git checkout "$2" ||
488                                 die "Could not checkout $2"
489                 fi
491                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
492                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
494                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
495                 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
496                         echo "detached HEAD" > "$DOTEST"/head-name
498                 echo $HEAD > "$DOTEST"/head
499                 echo $UPSTREAM > "$DOTEST"/upstream
500                 echo $ONTO > "$DOTEST"/onto
501                 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
502                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
503                 if test t = "$PRESERVE_MERGES"
504                 then
505                         # $REWRITTEN contains files for each commit that is
506                         # reachable by at least one merge base of $HEAD and
507                         # $UPSTREAM. They are not necessarily rewritten, but
508                         # their children might be.
509                         # This ensures that commits on merged, but otherwise
510                         # unrelated side branches are left alone. (Think "X"
511                         # in the man page's example.)
512                         mkdir "$REWRITTEN" &&
513                         for c in $(git merge-base --all $HEAD $UPSTREAM)
514                         do
515                                 echo $ONTO > "$REWRITTEN"/$c ||
516                                         die "Could not init rewritten commits"
517                         done
518                         MERGES_OPTION=
519                 else
520                         MERGES_OPTION=--no-merges
521                 fi
523                 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
524                 SHORTHEAD=$(git rev-parse --short $HEAD)
525                 SHORTONTO=$(git rev-parse --short $ONTO)
526                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
527                         --abbrev=7 --reverse --left-right --cherry-pick \
528                         $UPSTREAM...$HEAD | \
529                         sed -n "s/^>/pick /p" > "$TODO"
530                 cat >> "$TODO" << EOF
532 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
534 # Commands:
535 #  p, pick = use commit
536 #  e, edit = use commit, but stop for amending
537 #  s, squash = use commit, but meld into previous commit
539 # If you remove a line here THAT COMMIT WILL BE LOST.
540 # However, if you remove everything, the rebase will be aborted.
542 EOF
544                 has_action "$TODO" ||
545                         die_abort "Nothing to do"
547                 cp "$TODO" "$TODO".backup
548                 git_editor "$TODO" ||
549                         die "Could not execute editor"
551                 has_action "$TODO" ||
552                         die_abort "Nothing to do"
554                 output git checkout $ONTO && do_rest
555                 ;;
556         esac
557         shift
558 done