git-sh-setup: document git_editor() and get_author_ident_from_commit()
[git/dscho.git] / git-rebase--interactive.sh
blob090c3e51437969459f2b4f4625d5785d79c05084
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
16 OPTIONS_SPEC=
17 . git-sh-setup
18 require_work_tree
20 DOTEST="$GIT_DIR/.dotest-merge"
21 TODO="$DOTEST"/git-rebase-todo
22 DONE="$DOTEST"/done
23 MSG="$DOTEST"/message
24 SQUASH_MSG="$DOTEST"/message-squash
25 REWRITTEN="$DOTEST"/rewritten
26 PRESERVE_MERGES=
27 STRATEGY=
28 VERBOSE=
29 test -d "$REWRITTEN" && PRESERVE_MERGES=t
30 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31 test -f "$DOTEST"/verbose && VERBOSE=t
33 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34 mark the corrected paths with 'git add <paths>', and
35 run 'git rebase --continue'"
36 export GIT_CHERRY_PICK_HELP
38 warn () {
39 echo "$*" >&2
42 output () {
43 case "$VERBOSE" in
44 '')
45 output=$("$@" 2>&1 )
46 status=$?
47 test $status != 0 && printf "%s\n" "$output"
48 return $status
51 "$@"
53 esac
56 require_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 &&
61 git diff-index --cached --quiet HEAD -- ||
62 die "Working tree is dirty"
65 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
67 comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
69 ''|rebase*)
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
73 esac
76 mark_action_done () {
77 sed -e 1q < "$TODO" >> "$DONE"
78 sed -e 1d < "$TODO" >> "$TODO".new
79 mv -f "$TODO".new "$TODO"
80 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
81 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
82 printf "Rebasing (%d/%d)\r" $count $total
83 test -z "$VERBOSE" || echo
86 make_patch () {
87 parent_sha1=$(git rev-parse --verify "$1"^) ||
88 die "Cannot get patch for $1^"
89 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
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
96 die_with_patch () {
97 make_patch "$1"
98 git rerere
99 die "$2"
102 die_abort () {
103 rm -rf "$DOTEST"
104 die "$1"
107 has_action () {
108 grep -vqe '^$' -e '^#' "$1"
111 pick_one () {
112 no_ff=
113 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
114 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
115 test -d "$REWRITTEN" &&
116 pick_one_preserving_merges "$@" && return
117 parent_sha1=$(git rev-parse --verify $sha1^) ||
118 die "Could not get the parent of $sha1"
119 current_sha1=$(git rev-parse --verify HEAD)
120 if test "$no_ff$current_sha1" = "$parent_sha1"; then
121 output git reset --hard $sha1
122 test "a$1" = a-n && output git reset --soft $current_sha1
123 sha1=$(git rev-parse --short $sha1)
124 output warn Fast forward to $sha1
125 else
126 output git cherry-pick "$@"
130 pick_one_preserving_merges () {
131 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
132 sha1=$(git rev-parse $sha1)
134 if test -f "$DOTEST"/current-commit
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"
142 # rewrite parents; if none were rewritten, we can fast-forward.
143 fast_forward=t
144 preserve=t
145 new_parents=
146 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
148 if test -f "$REWRITTEN"/$p
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
157 new_parents="$new_parents $new_p"
159 esac
161 done
162 case $fast_forward in
164 output warn "Fast forward to $sha1"
165 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
168 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
170 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
171 # detach HEAD to current parent
172 output git checkout $first_parent 2> /dev/null ||
173 die "Cannot move HEAD to $first_parent"
175 echo $sha1 > "$DOTEST"/current-commit
176 case "$new_parents" in
177 ' '*' '*)
178 # redo merge
179 author_script=$(get_author_ident_from_commit $sha1)
180 eval "$author_script"
181 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
182 # No point in merging the first parent, that's HEAD
183 new_parents=${new_parents# $first_parent}
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
189 then
190 git rerere
191 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
192 die Error redoing merge $sha1
196 output git cherry-pick "$@" ||
197 die_with_patch $sha1 "Could not pick $sha1"
199 esac
201 esac
204 nth_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
213 make_squash_message () {
214 if test -f "$SQUASH_MSG"; then
215 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
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
227 echo "# This is the $(nth_string $COUNT) commit message:"
228 echo
229 git cat-file commit $1 | sed -e '1,/^$/d'
232 peek_next_command () {
233 sed -n "1s/ .*$//p" < "$TODO"
236 do_next () {
237 rm -f "$DOTEST"/message "$DOTEST"/author-script \
238 "$DOTEST"/amend || exit
239 read command sha1 rest < "$TODO"
240 case "$command" in
241 '#'*|'')
242 mark_action_done
244 pick|p)
245 comment_for_reflog pick
247 mark_action_done
248 pick_one $sha1 ||
249 die_with_patch $sha1 "Could not apply $sha1... $rest"
251 edit|e)
252 comment_for_reflog edit
254 mark_action_done
255 pick_one $sha1 ||
256 die_with_patch $sha1 "Could not apply $sha1... $rest"
257 make_patch $sha1
258 : > "$DOTEST"/amend
259 warn
260 warn "You can amend the commit now, with"
261 warn
262 warn " git commit --amend"
263 warn
264 exit 0
266 squash|s)
267 comment_for_reflog squash
269 has_action "$DONE" ||
270 die "Cannot 'squash' without a previous commit"
272 mark_action_done
273 make_squash_message $sha1 > "$MSG"
274 case "$(peek_next_command)" in
275 squash|s)
276 EDIT_COMMIT=
277 USE_OUTPUT=output
278 cp "$MSG" "$SQUASH_MSG"
281 EDIT_COMMIT=-e
282 USE_OUTPUT=
283 rm -f "$SQUASH_MSG" || exit
285 esac
287 failed=f
288 author_script=$(get_author_ident_from_commit HEAD)
289 output git reset --soft HEAD^
290 pick_one -n $sha1 || failed=t
291 echo "$author_script" > "$DOTEST"/author-script
292 if test $failed = f
293 then
294 # This is like --amend, but with a different message
295 eval "$author_script"
296 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
297 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
298 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
299 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
301 if test $failed = t
302 then
303 cp "$MSG" "$GIT_DIR"/MERGE_MSG
304 warn
305 warn "Could not apply $sha1... $rest"
306 die_with_patch $sha1 ""
310 warn "Unknown command: $command $sha1 $rest"
311 die_with_patch $sha1 "Please fix this in the file $TODO."
313 esac
314 test -s "$TODO" && return
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)) &&
320 if test -d "$REWRITTEN"
321 then
322 test -f "$DOTEST"/current-commit &&
323 current_commit=$(cat "$DOTEST"/current-commit) &&
324 git rev-parse HEAD > "$REWRITTEN"/$current_commit
325 if test -f "$REWRITTEN"/$OLDHEAD
326 then
327 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
328 else
329 NEWHEAD=$OLDHEAD
331 else
332 NEWHEAD=$(git rev-parse HEAD)
333 fi &&
334 case $HEADNAME in
335 refs/*)
336 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
337 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
338 git symbolic-ref HEAD $HEADNAME
340 esac && {
341 test ! -f "$DOTEST"/verbose ||
342 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
343 } &&
344 rm -rf "$DOTEST" &&
345 git gc --auto &&
346 warn "Successfully rebased and updated $HEADNAME."
348 exit
351 do_rest () {
352 while :
354 do_next
355 done
358 while test $# != 0
360 case "$1" in
361 --continue)
362 comment_for_reflog continue
364 test -d "$DOTEST" || die "No interactive rebase running"
366 # commit if necessary
367 git rev-parse --verify HEAD > /dev/null &&
368 git update-index --refresh &&
369 git diff-files --quiet &&
370 ! git diff-index --cached --quiet HEAD -- &&
371 . "$DOTEST"/author-script && {
372 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
373 } &&
374 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
375 if ! git commit --no-verify -F "$DOTEST"/message -e
376 then
377 die "Could not commit staged changes."
380 require_clean_work_tree
381 do_rest
383 --abort)
384 comment_for_reflog abort
386 git rerere clear
387 test -d "$DOTEST" || die "No interactive rebase running"
389 HEADNAME=$(cat "$DOTEST"/head-name)
390 HEAD=$(cat "$DOTEST"/head)
391 case $HEADNAME in
392 refs/*)
393 git symbolic-ref HEAD $HEADNAME
395 esac &&
396 output git reset --hard $HEAD &&
397 rm -rf "$DOTEST"
398 exit
400 --skip)
401 comment_for_reflog skip
403 git rerere clear
404 test -d "$DOTEST" || die "No interactive rebase running"
406 output git reset --hard && do_rest
408 -s|--strategy)
409 case "$#,$1" in
410 *,*=*)
411 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
412 1,*)
413 usage ;;
415 STRATEGY="-s $2"
416 shift ;;
417 esac
419 --merge)
420 # we use merge anyway
422 -C*)
423 die "Interactive rebase uses merge, so $1 does not make sense"
425 -v|--verbose)
426 VERBOSE=t
428 -p|--preserve-merges)
429 PRESERVE_MERGES=t
431 -i|--interactive)
432 # yeah, we know
434 ''|-h)
435 usage
438 test -d "$DOTEST" &&
439 die "Interactive rebase already started"
441 git var GIT_COMMITTER_IDENT >/dev/null ||
442 die "You need to set your committer info first"
444 comment_for_reflog start
446 ONTO=
447 case "$1" in
448 --onto)
449 ONTO=$(git rev-parse --verify "$2") ||
450 die "Does not point to a valid commit: $2"
451 shift; shift
453 esac
455 require_clean_work_tree
457 if test ! -z "$2"
458 then
459 output git show-ref --verify --quiet "refs/heads/$2" ||
460 die "Invalid branchname: $2"
461 output git checkout "$2" ||
462 die "Could not checkout $2"
465 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
466 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
468 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
470 test -z "$ONTO" && ONTO=$UPSTREAM
472 : > "$DOTEST"/interactive || die "Could not mark as interactive"
473 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
474 echo "detached HEAD" > "$DOTEST"/head-name
476 echo $HEAD > "$DOTEST"/head
477 echo $UPSTREAM > "$DOTEST"/upstream
478 echo $ONTO > "$DOTEST"/onto
479 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
480 test t = "$VERBOSE" && : > "$DOTEST"/verbose
481 if test t = "$PRESERVE_MERGES"
482 then
483 # $REWRITTEN contains files for each commit that is
484 # reachable by at least one merge base of $HEAD and
485 # $UPSTREAM. They are not necessarily rewritten, but
486 # their children might be.
487 # This ensures that commits on merged, but otherwise
488 # unrelated side branches are left alone. (Think "X"
489 # in the man page's example.)
490 mkdir "$REWRITTEN" &&
491 for c in $(git merge-base --all $HEAD $UPSTREAM)
493 echo $ONTO > "$REWRITTEN"/$c ||
494 die "Could not init rewritten commits"
495 done
496 MERGES_OPTION=
497 else
498 MERGES_OPTION=--no-merges
501 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
502 SHORTHEAD=$(git rev-parse --short $HEAD)
503 SHORTONTO=$(git rev-parse --short $ONTO)
504 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
505 --abbrev=7 --reverse --left-right --cherry-pick \
506 $UPSTREAM...$HEAD | \
507 sed -n "s/^>/pick /p" > "$TODO"
508 cat >> "$TODO" << EOF
510 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
512 # Commands:
513 # pick = use commit
514 # edit = use commit, but stop for amending
515 # squash = use commit, but meld into previous commit
517 # If you remove a line here THAT COMMIT WILL BE LOST.
518 # However, if you remove everything, the rebase will be aborted.
522 has_action "$TODO" ||
523 die_abort "Nothing to do"
525 cp "$TODO" "$TODO".backup
526 git_editor "$TODO" ||
527 die "Could not execute editor"
529 has_action "$TODO" ||
530 die_abort "Nothing to do"
532 output git checkout $ONTO && do_rest
534 esac
535 shift
536 done