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