Skip t3902-quoted.sh if the file system does not support funny names.
[git/git-p4.git] / git-rebase--interactive.sh
blob51063776d2540ed4ad6537e3578916b40d46c81a
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 output=$("$@" 2>&1 )
40 status=$?
41 test $status != 0 && printf "%s\n" "$output"
42 return $status
45 "$@"
47 esac
50 require_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"
59 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
61 comment_for_reflog () {
62 case "$ORIG_REFLOG_ACTION" in
63 ''|rebase*)
64 GIT_REFLOG_ACTION="rebase -i ($1)"
65 export GIT_REFLOG_ACTION
67 esac
70 mark_action_done () {
71 sed -e 1q < "$TODO" >> "$DONE"
72 sed -e 1d < "$TODO" >> "$TODO".new
73 mv -f "$TODO".new "$TODO"
74 count=$(($(grep -ve '^$' -e '^#' < "$DONE" | wc -l)))
75 total=$(($count+$(grep -ve '^$' -e '^#' < "$TODO" | wc -l)))
76 printf "Rebasing (%d/%d)\r" $count $total
77 test -z "$VERBOSE" || echo
80 make_patch () {
81 parent_sha1=$(git rev-parse --verify "$1"^) ||
82 die "Cannot get patch for $1^"
83 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
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
90 die_with_patch () {
91 make_patch "$1"
92 die "$2"
95 die_abort () {
96 rm -rf "$DOTEST"
97 die "$1"
100 has_action () {
101 grep -vqe '^$' -e '^#' "$1"
104 pick_one () {
105 no_ff=
106 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
107 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
108 test -d "$REWRITTEN" &&
109 pick_one_preserving_merges "$@" && return
110 parent_sha1=$(git rev-parse --verify $sha1^) ||
111 die "Could not get the parent of $sha1"
112 current_sha1=$(git rev-parse --verify HEAD)
113 if test "$no_ff$current_sha1" = "$parent_sha1"; then
114 output git reset --hard $sha1
115 test "a$1" = a-n && output git reset --soft $current_sha1
116 sha1=$(git rev-parse --short $sha1)
117 output warn Fast forward to $sha1
118 else
119 output git cherry-pick "$@"
123 pick_one_preserving_merges () {
124 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
125 sha1=$(git rev-parse $sha1)
127 if test -f "$DOTEST"/current-commit
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"
135 # rewrite parents; if none were rewritten, we can fast-forward.
136 fast_forward=t
137 preserve=t
138 new_parents=
139 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
141 if test -f "$REWRITTEN"/$p
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
150 new_parents="$new_parents $new_p"
152 esac
154 done
155 case $fast_forward in
157 output warn "Fast forward to $sha1"
158 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
161 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
163 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
164 # detach HEAD to current parent
165 output git checkout $first_parent 2> /dev/null ||
166 die "Cannot move HEAD to $first_parent"
168 echo $sha1 > "$DOTEST"/current-commit
169 case "$new_parents" in
170 ' '*' '*)
171 # redo merge
172 author_script=$(get_author_ident_from_commit $sha1)
173 eval "$author_script"
174 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
175 # No point in merging the first parent, that's HEAD
176 new_parents=${new_parents# $first_parent}
177 # NEEDSWORK: give rerere a chance
178 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
179 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
180 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
181 output git merge $STRATEGY -m "$msg" \
182 $new_parents
183 then
184 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
185 die Error redoing merge $sha1
189 output git cherry-pick "$@" ||
190 die_with_patch $sha1 "Could not pick $sha1"
192 esac
194 esac
197 nth_string () {
198 case "$1" in
199 *1[0-9]|*[04-9]) echo "$1"th;;
200 *1) echo "$1"st;;
201 *2) echo "$1"nd;;
202 *3) echo "$1"rd;;
203 esac
206 make_squash_message () {
207 if test -f "$SQUASH_MSG"; then
208 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
209 < "$SQUASH_MSG" | tail -n 1)+1))
210 echo "# This is a combination of $COUNT commits."
211 sed -n "2,\$p" < "$SQUASH_MSG"
212 else
213 COUNT=2
214 echo "# This is a combination of two commits."
215 echo "# The first commit's message is:"
216 echo
217 git cat-file commit HEAD | sed -e '1,/^$/d'
218 echo
220 echo "# This is the $(nth_string $COUNT) commit message:"
221 echo
222 git cat-file commit $1 | sed -e '1,/^$/d'
225 peek_next_command () {
226 sed -n "1s/ .*$//p" < "$TODO"
229 do_next () {
230 rm -f "$DOTEST"/message "$DOTEST"/author-script \
231 "$DOTEST"/amend || exit
232 read command sha1 rest < "$TODO"
233 case "$command" in
234 '#'*|'')
235 mark_action_done
237 pick|p)
238 comment_for_reflog pick
240 mark_action_done
241 pick_one $sha1 ||
242 die_with_patch $sha1 "Could not apply $sha1... $rest"
244 edit|e)
245 comment_for_reflog edit
247 mark_action_done
248 pick_one $sha1 ||
249 die_with_patch $sha1 "Could not apply $sha1... $rest"
250 make_patch $sha1
251 : > "$DOTEST"/amend
252 warn
253 warn "You can amend the commit now, with"
254 warn
255 warn " git commit --amend"
256 warn
257 exit 0
259 squash|s)
260 comment_for_reflog squash
262 has_action "$DONE" ||
263 die "Cannot 'squash' without a previous commit"
265 mark_action_done
266 make_squash_message $sha1 > "$MSG"
267 case "$(peek_next_command)" in
268 squash|s)
269 EDIT_COMMIT=
270 USE_OUTPUT=output
271 cp "$MSG" "$SQUASH_MSG"
274 EDIT_COMMIT=-e
275 USE_OUTPUT=
276 rm -f "$SQUASH_MSG" || exit
278 esac
280 failed=f
281 author_script=$(get_author_ident_from_commit HEAD)
282 output git reset --soft HEAD^
283 pick_one -n $sha1 || failed=t
284 echo "$author_script" > "$DOTEST"/author-script
285 case $failed in
287 # This is like --amend, but with a different message
288 eval "$author_script"
289 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
290 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
291 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
292 $USE_OUTPUT git commit -F "$MSG" $EDIT_COMMIT
295 cp "$MSG" "$GIT_DIR"/MERGE_MSG
296 warn
297 warn "Could not apply $sha1... $rest"
298 die_with_patch $sha1 ""
300 esac
303 warn "Unknown command: $command $sha1 $rest"
304 die_with_patch $sha1 "Please fix this in the file $TODO."
306 esac
307 test -s "$TODO" && return
309 comment_for_reflog finish &&
310 HEADNAME=$(cat "$DOTEST"/head-name) &&
311 OLDHEAD=$(cat "$DOTEST"/head) &&
312 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
313 if test -d "$REWRITTEN"
314 then
315 test -f "$DOTEST"/current-commit &&
316 current_commit=$(cat "$DOTEST"/current-commit) &&
317 git rev-parse HEAD > "$REWRITTEN"/$current_commit
318 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
319 else
320 NEWHEAD=$(git rev-parse HEAD)
321 fi &&
322 case $HEADNAME in
323 refs/*)
324 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
325 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
326 git symbolic-ref HEAD $HEADNAME
328 esac && {
329 test ! -f "$DOTEST"/verbose ||
330 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
331 } &&
332 rm -rf "$DOTEST" &&
333 git gc --auto &&
334 warn "Successfully rebased and updated $HEADNAME."
336 exit
339 do_rest () {
340 while :
342 do_next
343 done
346 while test $# != 0
348 case "$1" in
349 --continue)
350 comment_for_reflog continue
352 test -d "$DOTEST" || die "No interactive rebase running"
354 # commit if necessary
355 git rev-parse --verify HEAD > /dev/null &&
356 git update-index --refresh &&
357 git diff-files --quiet &&
358 ! git diff-index --cached --quiet HEAD &&
359 . "$DOTEST"/author-script && {
360 test ! -f "$DOTEST"/amend || git reset --soft HEAD^
361 } &&
362 export GIT_AUTHOR_NAME GIT_AUTHOR_NAME GIT_AUTHOR_DATE &&
363 git commit -F "$DOTEST"/message -e
365 require_clean_work_tree
366 do_rest
368 --abort)
369 comment_for_reflog abort
371 test -d "$DOTEST" || die "No interactive rebase running"
373 HEADNAME=$(cat "$DOTEST"/head-name)
374 HEAD=$(cat "$DOTEST"/head)
375 case $HEADNAME in
376 refs/*)
377 git symbolic-ref HEAD $HEADNAME
379 esac &&
380 output git reset --hard $HEAD &&
381 rm -rf "$DOTEST"
382 exit
384 --skip)
385 comment_for_reflog skip
387 test -d "$DOTEST" || die "No interactive rebase running"
389 output git reset --hard && do_rest
391 -s|--strategy)
392 case "$#,$1" in
393 *,*=*)
394 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
395 1,*)
396 usage ;;
398 STRATEGY="-s $2"
399 shift ;;
400 esac
402 --merge)
403 # we use merge anyway
405 -C*)
406 die "Interactive rebase uses merge, so $1 does not make sense"
408 -v|--verbose)
409 VERBOSE=t
411 -p|--preserve-merges)
412 PRESERVE_MERGES=t
414 -i|--interactive)
415 # yeah, we know
417 ''|-h)
418 usage
421 test -d "$DOTEST" &&
422 die "Interactive rebase already started"
424 git var GIT_COMMITTER_IDENT >/dev/null ||
425 die "You need to set your committer info first"
427 comment_for_reflog start
429 ONTO=
430 case "$1" in
431 --onto)
432 ONTO=$(git rev-parse --verify "$2") ||
433 die "Does not point to a valid commit: $2"
434 shift; shift
436 esac
438 require_clean_work_tree
440 if test ! -z "$2"
441 then
442 output git show-ref --verify --quiet "refs/heads/$2" ||
443 die "Invalid branchname: $2"
444 output git checkout "$2" ||
445 die "Could not checkout $2"
448 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
449 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
451 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
453 test -z "$ONTO" && ONTO=$UPSTREAM
455 : > "$DOTEST"/interactive || die "Could not mark as interactive"
456 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
457 echo "detached HEAD" > "$DOTEST"/head-name
459 echo $HEAD > "$DOTEST"/head
460 echo $UPSTREAM > "$DOTEST"/upstream
461 echo $ONTO > "$DOTEST"/onto
462 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
463 test t = "$VERBOSE" && : > "$DOTEST"/verbose
464 if test t = "$PRESERVE_MERGES"
465 then
466 # $REWRITTEN contains files for each commit that is
467 # reachable by at least one merge base of $HEAD and
468 # $UPSTREAM. They are not necessarily rewritten, but
469 # their children might be.
470 # This ensures that commits on merged, but otherwise
471 # unrelated side branches are left alone. (Think "X"
472 # in the man page's example.)
473 mkdir "$REWRITTEN" &&
474 for c in $(git merge-base --all $HEAD $UPSTREAM)
476 echo $ONTO > "$REWRITTEN"/$c ||
477 die "Could not init rewritten commits"
478 done
479 MERGES_OPTION=
480 else
481 MERGES_OPTION=--no-merges
484 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
485 SHORTHEAD=$(git rev-parse --short $HEAD)
486 SHORTONTO=$(git rev-parse --short $ONTO)
487 cat > "$TODO" << EOF
488 # Rebasing $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
490 # Commands:
491 # pick = use commit
492 # edit = use commit, but stop for amending
493 # squash = use commit, but meld into previous commit
495 # If you remove a line here THAT COMMIT WILL BE LOST.
498 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
499 --abbrev=7 --reverse --left-right --cherry-pick \
500 $UPSTREAM...$HEAD | \
501 sed -n "s/^>/pick /p" >> "$TODO"
503 has_action "$TODO" ||
504 die_abort "Nothing to do"
506 cp "$TODO" "$TODO".backup
507 git_editor "$TODO" ||
508 die "Could not execute editor"
510 has_action "$TODO" ||
511 die_abort "Nothing to do"
513 output git checkout $ONTO && do_rest
515 esac
516 shift
517 done