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