]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
Merge branch 'maint-1.5.5' into maint-1.5.6
[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 15
8f321a39 16OPTIONS_SPEC=
1b1dce4b
JS
17. git-sh-setup
18require_work_tree
19
20DOTEST="$GIT_DIR/.dotest-merge"
c22486c9 21TODO="$DOTEST"/git-rebase-todo
1b1dce4b 22DONE="$DOTEST"/done
6368f3f8
JS
23MSG="$DOTEST"/message
24SQUASH_MSG="$DOTEST"/message-squash
f09c9b8c
JS
25REWRITTEN="$DOTEST"/rewritten
26PRESERVE_MERGES=
1b1dce4b
JS
27STRATEGY=
28VERBOSE=
8e4a91bd
JS
29test -d "$REWRITTEN" && PRESERVE_MERGES=t
30test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31test -f "$DOTEST"/verbose && VERBOSE=t
1b1dce4b 32
804c7174
WC
33GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34mark the corrected paths with 'git add <paths>', and
35run 'git rebase --continue'"
36export GIT_CHERRY_PICK_HELP
37
1b1dce4b
JS
38warn () {
39 echo "$*" >&2
40}
41
dfa49f33
JS
42output () {
43 case "$VERBOSE" in
44 '')
5166810b 45 output=$("$@" 2>&1 )
dfa49f33 46 status=$?
5166810b 47 test $status != 0 && printf "%s\n" "$output"
dfa49f33 48 return $status
376ccb8c 49 ;;
dfa49f33
JS
50 *)
51 "$@"
376ccb8c 52 ;;
dfa49f33
JS
53 esac
54}
55
1b1dce4b
JS
56require_clean_work_tree () {
57 # test if working tree is dirty
58 git rev-parse --verify HEAD > /dev/null &&
6848d58c
JS
59 git update-index --ignore-submodules --refresh &&
60 git diff-files --quiet --ignore-submodules &&
61 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
1b1dce4b
JS
62 die "Working tree is dirty"
63}
64
65ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
66
67comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
69 ''|rebase*)
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
376ccb8c 72 ;;
1b1dce4b
JS
73 esac
74}
75
4e673877 76last_count=
1b1dce4b
JS
77mark_action_done () {
78 sed -e 1q < "$TODO" >> "$DONE"
79 sed -e 1d < "$TODO" >> "$TODO".new
80 mv -f "$TODO".new "$TODO"
aadbe44f
JK
81 count=$(grep -c '^[^#]' < "$DONE")
82 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
4e673877
JH
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
1b1dce4b
JS
89}
90
91make_patch () {
be6ff208
JS
92 parent_sha1=$(git rev-parse --verify "$1"^) ||
93 die "Cannot get patch for $1^"
f3d5e463 94 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
be6ff208
JS
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
1b1dce4b
JS
99}
100
101die_with_patch () {
102 make_patch "$1"
ecfe72ff 103 git rerere
1b1dce4b
JS
104 die "$2"
105}
106
c54b7817
JS
107die_abort () {
108 rm -rf "$DOTEST"
109 die "$1"
110}
111
376ccb8c 112has_action () {
aadbe44f 113 grep '^[^#]' "$1" >/dev/null
376ccb8c
JS
114}
115
1b1dce4b 116pick_one () {
1d25c8cf
JS
117 no_ff=
118 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
dfa49f33 119 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
f09c9b8c
JS
120 test -d "$REWRITTEN" &&
121 pick_one_preserving_merges "$@" && return
376ccb8c
JS
122 parent_sha1=$(git rev-parse --verify $sha1^) ||
123 die "Could not get the parent of $sha1"
1b1dce4b 124 current_sha1=$(git rev-parse --verify HEAD)
2858028e 125 if test "$no_ff$current_sha1" = "$parent_sha1"; then
dfa49f33
JS
126 output git reset --hard $sha1
127 test "a$1" = a-n && output git reset --soft $current_sha1
1b1dce4b 128 sha1=$(git rev-parse --short $sha1)
dfa49f33 129 output warn Fast forward to $sha1
1b1dce4b 130 else
2a9c53e0 131 output git cherry-pick "$@"
1b1dce4b
JS
132 fi
133}
134
f09c9b8c
JS
135pick_one_preserving_merges () {
136 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
137 sha1=$(git rev-parse $sha1)
138
3b38ec16 139 if test -f "$DOTEST"/current-commit
f09c9b8c
JS
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
146
147 # rewrite parents; if none were rewritten, we can fast-forward.
148 fast_forward=t
149 preserve=t
150 new_parents=
376ccb8c 151 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
f09c9b8c 152 do
3b38ec16 153 if test -f "$REWRITTEN"/$p
f09c9b8c
JS
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"
376ccb8c 163 ;;
f09c9b8c 164 esac
1c5fa0a1
SB
165 else
166 new_parents="$new_parents $p"
f09c9b8c
JS
167 fi
168 done
169 case $fast_forward in
170 t)
dfa49f33 171 output warn "Fast forward to $sha1"
376ccb8c 172 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
f09c9b8c
JS
173 ;;
174 f)
175 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
176
376ccb8c 177 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
f09c9b8c 178 # detach HEAD to current parent
dfa49f33 179 output git checkout $first_parent 2> /dev/null ||
f09c9b8c
JS
180 die "Cannot move HEAD to $first_parent"
181
182 echo $sha1 > "$DOTEST"/current-commit
183 case "$new_parents" in
376ccb8c 184 ' '*' '*)
f09c9b8c
JS
185 # redo merge
186 author_script=$(get_author_ident_from_commit $sha1)
187 eval "$author_script"
376ccb8c 188 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
f91333d6
BS
189 # No point in merging the first parent, that's HEAD
190 new_parents=${new_parents# $first_parent}
ae830ed7
JS
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
f09c9b8c 196 then
ecfe72ff 197 git rerere
376ccb8c 198 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
18640d99 199 die Error redoing merge $sha1
f09c9b8c
JS
200 fi
201 ;;
202 *)
2a9c53e0 203 output git cherry-pick "$@" ||
f09c9b8c 204 die_with_patch $sha1 "Could not pick $sha1"
376ccb8c 205 ;;
f09c9b8c 206 esac
376ccb8c 207 ;;
f09c9b8c
JS
208 esac
209}
210
6368f3f8
JS
211nth_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
218}
219
220make_squash_message () {
3b38ec16 221 if test -f "$SQUASH_MSG"; then
c7965afd 222 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
b4ce54fc 223 < "$SQUASH_MSG" | sed -ne '$p')+1))
6368f3f8 224 echo "# This is a combination of $COUNT commits."
8ad1065e
JH
225 sed -e 1d -e '2,/^./{
226 /^$/d
227 }' <"$SQUASH_MSG"
6368f3f8
JS
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'
6368f3f8 234 fi
8ad1065e 235 echo
6368f3f8
JS
236 echo "# This is the $(nth_string $COUNT) commit message:"
237 echo
238 git cat-file commit $1 | sed -e '1,/^$/d'
239}
240
241peek_next_command () {
242 sed -n "1s/ .*$//p" < "$TODO"
243}
244
1b1dce4b 245do_next () {
376ccb8c
JS
246 rm -f "$DOTEST"/message "$DOTEST"/author-script \
247 "$DOTEST"/amend || exit
1b1dce4b
JS
248 read command sha1 rest < "$TODO"
249 case "$command" in
376ccb8c 250 '#'*|'')
1b1dce4b 251 mark_action_done
1b1dce4b 252 ;;
f8babc4d 253 pick|p)
1b1dce4b
JS
254 comment_for_reflog pick
255
256 mark_action_done
257 pick_one $sha1 ||
258 die_with_patch $sha1 "Could not apply $sha1... $rest"
259 ;;
f8babc4d 260 edit|e)
1b1dce4b
JS
261 comment_for_reflog edit
262
263 mark_action_done
264 pick_one $sha1 ||
265 die_with_patch $sha1 "Could not apply $sha1... $rest"
266 make_patch $sha1
be6ff208 267 : > "$DOTEST"/amend
1b1dce4b
JS
268 warn
269 warn "You can amend the commit now, with"
270 warn
271 warn " git commit --amend"
272 warn
0460fb44
JS
273 warn "Once you are satisfied with your changes, run"
274 warn
275 warn " git rebase --continue"
276 warn
1b1dce4b
JS
277 exit 0
278 ;;
f8babc4d 279 squash|s)
1b1dce4b
JS
280 comment_for_reflog squash
281
376ccb8c 282 has_action "$DONE" ||
1b1dce4b
JS
283 die "Cannot 'squash' without a previous commit"
284
285 mark_action_done
6368f3f8
JS
286 make_squash_message $sha1 > "$MSG"
287 case "$(peek_next_command)" in
f8babc4d 288 squash|s)
6368f3f8 289 EDIT_COMMIT=
91e1ee77 290 USE_OUTPUT=output
6368f3f8 291 cp "$MSG" "$SQUASH_MSG"
376ccb8c 292 ;;
6368f3f8
JS
293 *)
294 EDIT_COMMIT=-e
91e1ee77 295 USE_OUTPUT=
376ccb8c
JS
296 rm -f "$SQUASH_MSG" || exit
297 ;;
6368f3f8
JS
298 esac
299
793ad041 300 failed=f
81ab1cb4 301 author_script=$(get_author_ident_from_commit HEAD)
dfa49f33 302 output git reset --soft HEAD^
fb47cfbd 303 pick_one -n $sha1 || failed=t
18640d99 304 echo "$author_script" > "$DOTEST"/author-script
dbedf972
SP
305 if test $failed = f
306 then
1b1dce4b
JS
307 # This is like --amend, but with a different message
308 eval "$author_script"
ae830ed7
JS
309 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
310 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
311 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
dbedf972
SP
312 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
313 fi
314 if test $failed = t
315 then
1b1dce4b
JS
316 cp "$MSG" "$GIT_DIR"/MERGE_MSG
317 warn
318 warn "Could not apply $sha1... $rest"
1b1dce4b 319 die_with_patch $sha1 ""
dbedf972 320 fi
1b1dce4b
JS
321 ;;
322 *)
323 warn "Unknown command: $command $sha1 $rest"
324 die_with_patch $sha1 "Please fix this in the file $TODO."
376ccb8c 325 ;;
1b1dce4b
JS
326 esac
327 test -s "$TODO" && return
328
68a163c9
JS
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)) &&
3b38ec16 333 if test -d "$REWRITTEN"
f09c9b8c
JS
334 then
335 test -f "$DOTEST"/current-commit &&
336 current_commit=$(cat "$DOTEST"/current-commit) &&
337 git rev-parse HEAD > "$REWRITTEN"/$current_commit
34454e85
JS
338 if test -f "$REWRITTEN"/$OLDHEAD
339 then
340 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
341 else
342 NEWHEAD=$OLDHEAD
343 fi
f09c9b8c
JS
344 else
345 NEWHEAD=$(git rev-parse HEAD)
346 fi &&
73697a0b
JS
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 && {
3df0a859 354 test ! -f "$DOTEST"/verbose ||
f3d5e463 355 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
3df0a859 356 } &&
1b1dce4b 357 rm -rf "$DOTEST" &&
73697a0b 358 git gc --auto &&
1b1dce4b
JS
359 warn "Successfully rebased and updated $HEADNAME."
360
361 exit
362}
363
364do_rest () {
365 while :
366 do
367 do_next
368 done
1b1dce4b
JS
369}
370
822f7c73 371while test $# != 0
1b1dce4b
JS
372do
373 case "$1" in
374 --continue)
375 comment_for_reflog continue
376
377 test -d "$DOTEST" || die "No interactive rebase running"
378
ab119032
JH
379 # Sanity check
380 git rev-parse --verify HEAD >/dev/null ||
381 die "Cannot read HEAD"
6848d58c
JS
382 git update-index --ignore-submodules --refresh &&
383 git diff-files --quiet --ignore-submodules ||
ab119032
JH
384 die "Working tree is dirty"
385
386 # do we have anything to commit?
6848d58c 387 if git diff-index --cached --quiet --ignore-submodules HEAD --
03270628 388 then
ab119032
JH
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 ||
dbedf972 400 die "Could not commit staged changes."
03270628 401 fi
18640d99 402
1b1dce4b
JS
403 require_clean_work_tree
404 do_rest
405 ;;
406 --abort)
407 comment_for_reflog abort
408
ecfe72ff 409 git rerere clear
1b1dce4b
JS
410 test -d "$DOTEST" || die "No interactive rebase running"
411
412 HEADNAME=$(cat "$DOTEST"/head-name)
413 HEAD=$(cat "$DOTEST"/head)
73697a0b
JS
414 case $HEADNAME in
415 refs/*)
416 git symbolic-ref HEAD $HEADNAME
417 ;;
418 esac &&
dfa49f33 419 output git reset --hard $HEAD &&
1b1dce4b
JS
420 rm -rf "$DOTEST"
421 exit
422 ;;
423 --skip)
424 comment_for_reflog skip
425
ecfe72ff 426 git rerere clear
1b1dce4b
JS
427 test -d "$DOTEST" || die "No interactive rebase running"
428
dfa49f33 429 output git reset --hard && do_rest
1b1dce4b
JS
430 ;;
431 -s|--strategy)
1b1dce4b
JS
432 case "$#,$1" in
433 *,*=*)
b5e960b1 434 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1b1dce4b
JS
435 1,*)
436 usage ;;
437 *)
438 STRATEGY="-s $2"
439 shift ;;
440 esac
441 ;;
65ae89bc 442 -m|--merge)
1b1dce4b
JS
443 # we use merge anyway
444 ;;
445 -C*)
446 die "Interactive rebase uses merge, so $1 does not make sense"
447 ;;
c54b7817 448 -v|--verbose)
1b1dce4b
JS
449 VERBOSE=t
450 ;;
f09c9b8c
JS
451 -p|--preserve-merges)
452 PRESERVE_MERGES=t
453 ;;
1b1dce4b
JS
454 -i|--interactive)
455 # yeah, we know
456 ;;
457 ''|-h)
458 usage
459 ;;
460 *)
461 test -d "$DOTEST" &&
462 die "Interactive rebase already started"
463
464 git var GIT_COMMITTER_IDENT >/dev/null ||
465 die "You need to set your committer info first"
466
467 comment_for_reflog start
468
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
477
478 require_clean_work_tree
479
69e66f55
JS
480 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
481 test -z "$ONTO" && ONTO=$UPSTREAM
482
3b38ec16 483 if test ! -z "$2"
1b1dce4b 484 then
dfa49f33 485 output git show-ref --verify --quiet "refs/heads/$2" ||
1b1dce4b 486 die "Invalid branchname: $2"
dfa49f33 487 output git checkout "$2" ||
1b1dce4b
JS
488 die "Could not checkout $2"
489 fi
490
491 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
5166810b
MK
492 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
493
1b1dce4b 494 : > "$DOTEST"/interactive || die "Could not mark as interactive"
73697a0b
JS
495 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
496 echo "detached HEAD" > "$DOTEST"/head-name
1b1dce4b
JS
497
498 echo $HEAD > "$DOTEST"/head
499 echo $UPSTREAM > "$DOTEST"/upstream
500 echo $ONTO > "$DOTEST"/onto
8e4a91bd 501 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4b 502 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec16 503 if test t = "$PRESERVE_MERGES"
f09c9b8c
JS
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
1b1dce4b 522
c54b7817
JS
523 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
524 SHORTHEAD=$(git rev-parse --short $HEAD)
525 SHORTONTO=$(git rev-parse --short $ONTO)
6047a234
JS
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
531
532# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
1b1dce4b
JS
533#
534# Commands:
88b1f0b8
MV
535# p, pick = use commit
536# e, edit = use commit, but stop for amending
537# s, squash = use commit, but meld into previous commit
82576ddb
JS
538#
539# If you remove a line here THAT COMMIT WILL BE LOST.
6047a234 540# However, if you remove everything, the rebase will be aborted.
82576ddb 541#
1b1dce4b 542EOF
1b1dce4b 543
376ccb8c 544 has_action "$TODO" ||
c54b7817 545 die_abort "Nothing to do"
1b1dce4b
JS
546
547 cp "$TODO" "$TODO".backup
ef0c2abf 548 git_editor "$TODO" ||
1b1dce4b
JS
549 die "Could not execute editor"
550
376ccb8c 551 has_action "$TODO" ||
c54b7817
JS
552 die_abort "Nothing to do"
553
dfa49f33 554 output git checkout $ONTO && do_rest
376ccb8c 555 ;;
1b1dce4b
JS
556 esac
557 shift
558done