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