rebase -i: Fix numbers in progress report
[git/jrn.git] / git-rebase--interactive.sh
blobefa83f61348f2603872d01a48cc9ae2ffaadc1ff
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 . git-sh-setup
17 require_work_tree
19 DOTEST="$GIT_DIR/.dotest-merge"
20 TODO="$DOTEST"/git-rebase-todo
21 DONE="$DOTEST"/done
22 MSG="$DOTEST"/message
23 SQUASH_MSG="$DOTEST"/message-squash
24 REWRITTEN="$DOTEST"/rewritten
25 PRESERVE_MERGES=
26 STRATEGY=
27 VERBOSE=
28 test -d "$REWRITTEN" && PRESERVE_MERGES=t
29 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
30 test -f "$DOTEST"/verbose && VERBOSE=t
32 warn () {
33 echo "$*" >&2
36 output () {
37 case "$VERBOSE" in
38 '')
39 "$@" > "$DOTEST"/output 2>&1
40 status=$?
41 test $status != 0 &&
42 cat "$DOTEST"/output
43 return $status
46 "$@"
48 esac
51 require_clean_work_tree () {
52 # test if working tree is dirty
53 git rev-parse --verify HEAD > /dev/null &&
54 git update-index --refresh &&
55 git diff-files --quiet &&
56 git diff-index --cached --quiet HEAD ||
57 die "Working tree is dirty"
60 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
62 comment_for_reflog () {
63 case "$ORIG_REFLOG_ACTION" in
64 ''|rebase*)
65 GIT_REFLOG_ACTION="rebase -i ($1)"
66 export GIT_REFLOG_ACTION
68 esac
71 mark_action_done () {
72 sed -e 1q < "$TODO" >> "$DONE"
73 sed -e 1d < "$TODO" >> "$TODO".new
74 mv -f "$TODO".new "$TODO"
75 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
76 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
77 printf "Rebasing (%d/%d)\r" $count $total
78 test -z "$VERBOSE" || echo
81 make_patch () {
82 parent_sha1=$(git rev-parse --verify "$1"^) ||
83 die "Cannot get patch for $1^"
84 git diff "$parent_sha1".."$1" > "$DOTEST"/patch
85 test -f "$DOTEST"/message ||
86 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
87 test -f "$DOTEST"/author-script ||
88 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
91 die_with_patch () {
92 make_patch "$1"
93 die "$2"
96 die_abort () {
97 rm -rf "$DOTEST"
98 die "$1"
101 has_action () {
102 grep -vqe '^$' -e '^#' "$1"
105 pick_one () {
106 no_ff=
107 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
108 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
109 test -d "$REWRITTEN" &&
110 pick_one_preserving_merges "$@" && return
111 parent_sha1=$(git rev-parse --verify $sha1^) ||
112 die "Could not get the parent of $sha1"
113 current_sha1=$(git rev-parse --verify HEAD)
114 if test $no_ff$current_sha1 = $parent_sha1; then
115 output git reset --hard $sha1
116 test "a$1" = a-n && output git reset --soft $current_sha1
117 sha1=$(git rev-parse --short $sha1)
118 output warn Fast forward to $sha1
119 else
120 output git cherry-pick $STRATEGY "$@"
124 pick_one_preserving_merges () {
125 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
126 sha1=$(git rev-parse $sha1)
128 if test -f "$DOTEST"/current-commit
129 then
130 current_commit=$(cat "$DOTEST"/current-commit) &&
131 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
132 rm "$DOTEST"/current-commit ||
133 die "Cannot write current commit's replacement sha1"
136 # rewrite parents; if none were rewritten, we can fast-forward.
137 fast_forward=t
138 preserve=t
139 new_parents=
140 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
142 if test -f "$REWRITTEN"/$p
143 then
144 preserve=f
145 new_p=$(cat "$REWRITTEN"/$p)
146 test $p != $new_p && fast_forward=f
147 case "$new_parents" in
148 *$new_p*)
149 ;; # do nothing; that parent is already there
151 new_parents="$new_parents $new_p"
153 esac
155 done
156 case $fast_forward in
158 output warn "Fast forward to $sha1"
159 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
162 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
164 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
165 # detach HEAD to current parent
166 output git checkout $first_parent 2> /dev/null ||
167 die "Cannot move HEAD to $first_parent"
169 echo $sha1 > "$DOTEST"/current-commit
170 case "$new_parents" in
171 ' '*' '*)
172 # redo merge
173 author_script=$(get_author_ident_from_commit $sha1)
174 eval "$author_script"
175 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
176 # NEEDSWORK: give rerere a chance
177 if ! output git merge $STRATEGY -m "$msg" $new_parents
178 then
179 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
180 die Error redoing merge $sha1
184 output git cherry-pick $STRATEGY "$@" ||
185 die_with_patch $sha1 "Could not pick $sha1"
187 esac
189 esac
192 nth_string () {
193 case "$1" in
194 *1[0-9]|*[04-9]) echo "$1"th;;
195 *1) echo "$1"st;;
196 *2) echo "$1"nd;;
197 *3) echo "$1"rd;;
198 esac
201 make_squash_message () {
202 if test -f "$SQUASH_MSG"; then
203 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
204 < "$SQUASH_MSG" | tail -n 1)+1))
205 echo "# This is a combination of $COUNT commits."
206 sed -n "2,\$p" < "$SQUASH_MSG"
207 else
208 COUNT=2
209 echo "# This is a combination of two commits."
210 echo "# The first commit's message is:"
211 echo
212 git cat-file commit HEAD | sed -e '1,/^$/d'
213 echo
215 echo "# This is the $(nth_string $COUNT) commit message:"
216 echo
217 git cat-file commit $1 | sed -e '1,/^$/d'
220 peek_next_command () {
221 sed -n "1s/ .*$//p" < "$TODO"
224 do_next () {
225 rm -f "$DOTEST"/message "$DOTEST"/author-script \
226 "$DOTEST"/amend || exit
227 read command sha1 rest < "$TODO"
228 case "$command" in
229 '#'*|'')
230 mark_action_done
232 pick)
233 comment_for_reflog pick
235 mark_action_done
236 pick_one $sha1 ||
237 die_with_patch $sha1 "Could not apply $sha1... $rest"
239 edit)
240 comment_for_reflog edit
242 mark_action_done
243 pick_one $sha1 ||
244 die_with_patch $sha1 "Could not apply $sha1... $rest"
245 make_patch $sha1
246 : > "$DOTEST"/amend
247 warn
248 warn "You can amend the commit now, with"
249 warn
250 warn " git commit --amend"
251 warn
252 exit 0
254 squash)
255 comment_for_reflog squash
257 has_action "$DONE" ||
258 die "Cannot 'squash' without a previous commit"
260 mark_action_done
261 make_squash_message $sha1 > "$MSG"
262 case "$(peek_next_command)" in
263 squash)
264 EDIT_COMMIT=
265 USE_OUTPUT=output
266 cp "$MSG" "$SQUASH_MSG"
269 EDIT_COMMIT=-e
270 USE_OUTPUT=
271 rm -f "$SQUASH_MSG" || exit
273 esac
275 failed=f
276 output git reset --soft HEAD^
277 pick_one -n $sha1 || failed=t
278 author_script=$(get_author_ident_from_commit $sha1)
279 echo "$author_script" > "$DOTEST"/author-script
280 case $failed in
282 # This is like --amend, but with a different message
283 eval "$author_script"
284 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
285 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
288 cp "$MSG" "$GIT_DIR"/MERGE_MSG
289 warn
290 warn "Could not apply $sha1... $rest"
291 die_with_patch $sha1 ""
293 esac
296 warn "Unknown command: $command $sha1 $rest"
297 die_with_patch $sha1 "Please fix this in the file $TODO."
299 esac
300 test -s "$TODO" && return
302 comment_for_reflog finish &&
303 HEADNAME=$(cat "$DOTEST"/head-name) &&
304 OLDHEAD=$(cat "$DOTEST"/head) &&
305 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
306 if test -d "$REWRITTEN"
307 then
308 test -f "$DOTEST"/current-commit &&
309 current_commit=$(cat "$DOTEST"/current-commit) &&
310 git rev-parse HEAD > "$REWRITTEN"/$current_commit
311 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
312 else
313 NEWHEAD=$(git rev-parse HEAD)
314 fi &&
315 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
316 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
317 git symbolic-ref HEAD $HEADNAME && {
318 test ! -f "$DOTEST"/verbose ||
319 git diff --stat $(cat "$DOTEST"/head)..HEAD
320 } &&
321 rm -rf "$DOTEST" &&
322 warn "Successfully rebased and updated $HEADNAME."
324 exit
327 do_rest () {
328 while :
330 do_next
331 done
334 while test $# != 0
336 case "$1" in
337 --continue)
338 comment_for_reflog continue
340 test -d "$DOTEST" || die "No interactive rebase running"
342 # commit if necessary
343 git rev-parse --verify HEAD > /dev/null &&
344 git update-index --refresh &&
345 git diff-files --quiet &&
346 ! git diff-index --cached --quiet HEAD &&
347 . "$DOTEST"/author-script && {
348 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
349 } &&
350 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
351 git commit -F "$DOTEST"/message -e
353 require_clean_work_tree
354 do_rest
356 --abort)
357 comment_for_reflog abort
359 test -d "$DOTEST" || die "No interactive rebase running"
361 HEADNAME=$(cat "$DOTEST"/head-name)
362 HEAD=$(cat "$DOTEST"/head)
363 git symbolic-ref HEAD $HEADNAME &&
364 output git reset --hard $HEAD &&
365 rm -rf "$DOTEST"
366 exit
368 --skip)
369 comment_for_reflog skip
371 test -d "$DOTEST" || die "No interactive rebase running"
373 output git reset --hard && do_rest
375 -s|--strategy)
376 shift
377 case "$#,$1" in
378 *,*=*)
379 STRATEGY="-s `expr "z$1" : 'z-[^=]*=\(.*\)'`" ;;
380 1,*)
381 usage ;;
383 STRATEGY="-s $2"
384 shift ;;
385 esac
387 --merge)
388 # we use merge anyway
390 -C*)
391 die "Interactive rebase uses merge, so $1 does not make sense"
393 -v|--verbose)
394 VERBOSE=t
396 -p|--preserve-merges)
397 PRESERVE_MERGES=t
399 -i|--interactive)
400 # yeah, we know
402 ''|-h)
403 usage
406 test -d "$DOTEST" &&
407 die "Interactive rebase already started"
409 git var GIT_COMMITTER_IDENT >/dev/null ||
410 die "You need to set your committer info first"
412 comment_for_reflog start
414 ONTO=
415 case "$1" in
416 --onto)
417 ONTO=$(git rev-parse --verify "$2") ||
418 die "Does not point to a valid commit: $2"
419 shift; shift
421 esac
423 require_clean_work_tree
425 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
426 if test ! -z "$2"
427 then
428 output git show-ref --verify --quiet "refs/heads/$2" ||
429 die "Invalid branchname: $2"
430 output git checkout "$2" ||
431 die "Could not checkout $2"
434 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
435 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
437 test -z "$ONTO" && ONTO=$UPSTREAM
439 : > "$DOTEST"/interactive || die "Could not mark as interactive"
440 git symbolic-ref HEAD > "$DOTEST"/head-name ||
441 die "Could not get HEAD"
443 echo $HEAD > "$DOTEST"/head
444 echo $UPSTREAM > "$DOTEST"/upstream
445 echo $ONTO > "$DOTEST"/onto
446 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
447 test t = "$VERBOSE" && : > "$DOTEST"/verbose
448 if test t = "$PRESERVE_MERGES"
449 then
450 # $REWRITTEN contains files for each commit that is
451 # reachable by at least one merge base of $HEAD and
452 # $UPSTREAM. They are not necessarily rewritten, but
453 # their children might be.
454 # This ensures that commits on merged, but otherwise
455 # unrelated side branches are left alone. (Think "X"
456 # in the man page's example.)
457 mkdir "$REWRITTEN" &&
458 for c in $(git merge-base --all $HEAD $UPSTREAM)
460 echo $ONTO > "$REWRITTEN"/$c ||
461 die "Could not init rewritten commits"
462 done
463 MERGES_OPTION=
464 else
465 MERGES_OPTION=--no-merges
468 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
469 SHORTHEAD=$(git rev-parse --short $HEAD)
470 SHORTONTO=$(git rev-parse --short $ONTO)
471 cat > "$TODO" << EOF
472 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
474 # Commands:
475 # pick = use commit
476 # edit = use commit, but stop for amending
477 # squash = use commit, but meld into previous commit
479 # If you remove a line here THAT COMMIT WILL BE LOST.
482 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
483 --abbrev=7 --reverse --left-right --cherry-pick \
484 $UPSTREAM...$HEAD | \
485 sed -n "s/^>/pick /p" >> "$TODO"
487 has_action "$TODO" ||
488 die_abort "Nothing to do"
490 cp "$TODO" "$TODO".backup
491 git_editor "$TODO" ||
492 die "Could not execute editor"
494 has_action "$TODO" ||
495 die_abort "Nothing to do"
497 output git checkout $ONTO && do_rest
499 esac
500 shift
501 done