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