rebase: Document --no-verify option to bypass pre-rebase hook
[git/jnareb-git.git] / git-rebase--interactive.sh
blobb0d757db5dbb97d91014b22e34938ca0764dd4ef
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 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
18 Available options are
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 m,merge always used (no-op)
24 i,interactive always used (no-op)
25 Actions:
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
29 no-verify override pre-rebase hook from stopping the operation
32 . git-sh-setup
33 require_work_tree
35 DOTEST="$GIT_DIR/rebase-merge"
36 TODO="$DOTEST"/git-rebase-todo
37 DONE="$DOTEST"/done
38 MSG="$DOTEST"/message
39 SQUASH_MSG="$DOTEST"/message-squash
40 REWRITTEN="$DOTEST"/rewritten
41 PRESERVE_MERGES=
42 STRATEGY=
43 ONTO=
44 VERBOSE=
45 OK_TO_SKIP_PRE_REBASE=
47 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
48 mark the corrected paths with 'git add <paths>', and
49 run 'git rebase --continue'"
50 export GIT_CHERRY_PICK_HELP
52 warn () {
53 echo "$*" >&2
56 output () {
57 case "$VERBOSE" in
58 '')
59 output=$("$@" 2>&1 )
60 status=$?
61 test $status != 0 && printf "%s\n" "$output"
62 return $status
65 "$@"
67 esac
70 run_pre_rebase_hook () {
71 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
72 test -x "$GIT_DIR/hooks/pre-rebase"
73 then
74 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
75 echo >&2 "The pre-rebase hook refused to rebase."
76 exit 1
81 require_clean_work_tree () {
82 # test if working tree is dirty
83 git rev-parse --verify HEAD > /dev/null &&
84 git update-index --ignore-submodules --refresh &&
85 git diff-files --quiet --ignore-submodules &&
86 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
87 die "Working tree is dirty"
90 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
92 comment_for_reflog () {
93 case "$ORIG_REFLOG_ACTION" in
94 ''|rebase*)
95 GIT_REFLOG_ACTION="rebase -i ($1)"
96 export GIT_REFLOG_ACTION
98 esac
101 last_count=
102 mark_action_done () {
103 sed -e 1q < "$TODO" >> "$DONE"
104 sed -e 1d < "$TODO" >> "$TODO".new
105 mv -f "$TODO".new "$TODO"
106 count=$(grep -c '^[^#]' < "$DONE")
107 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
108 if test "$last_count" != "$count"
109 then
110 last_count=$count
111 printf "Rebasing (%d/%d)\r" $count $total
112 test -z "$VERBOSE" || echo
116 make_patch () {
117 parent_sha1=$(git rev-parse --verify "$1"^) ||
118 die "Cannot get patch for $1^"
119 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
120 test -f "$DOTEST"/message ||
121 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
122 test -f "$DOTEST"/author-script ||
123 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
126 die_with_patch () {
127 make_patch "$1"
128 git rerere
129 die "$2"
132 die_abort () {
133 rm -rf "$DOTEST"
134 die "$1"
137 has_action () {
138 grep '^[^#]' "$1" >/dev/null
141 pick_one () {
142 no_ff=
143 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
144 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
145 test -d "$REWRITTEN" &&
146 pick_one_preserving_merges "$@" && return
147 parent_sha1=$(git rev-parse --verify $sha1^) ||
148 die "Could not get the parent of $sha1"
149 current_sha1=$(git rev-parse --verify HEAD)
150 if test "$no_ff$current_sha1" = "$parent_sha1"; then
151 output git reset --hard $sha1
152 test "a$1" = a-n && output git reset --soft $current_sha1
153 sha1=$(git rev-parse --short $sha1)
154 output warn Fast forward to $sha1
155 else
156 output git cherry-pick "$@"
160 pick_one_preserving_merges () {
161 fast_forward=t
162 case "$1" in
164 fast_forward=f
165 sha1=$2
168 sha1=$1
170 esac
171 sha1=$(git rev-parse $sha1)
173 if test -f "$DOTEST"/current-commit
174 then
175 current_commit=$(cat "$DOTEST"/current-commit) &&
176 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
177 rm "$DOTEST"/current-commit ||
178 die "Cannot write current commit's replacement sha1"
181 echo $sha1 > "$DOTEST"/current-commit
183 # rewrite parents; if none were rewritten, we can fast-forward.
184 new_parents=
185 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
187 if test -f "$REWRITTEN"/$p
188 then
189 new_p=$(cat "$REWRITTEN"/$p)
190 test $p != $new_p && fast_forward=f
191 case "$new_parents" in
192 *$new_p*)
193 ;; # do nothing; that parent is already there
195 new_parents="$new_parents $new_p"
197 esac
198 else
199 new_parents="$new_parents $p"
201 done
202 case $fast_forward in
204 output warn "Fast forward to $sha1"
205 output git reset --hard $sha1 ||
206 die "Cannot fast forward to $sha1"
209 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
211 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
212 # detach HEAD to current parent
213 output git checkout $first_parent 2> /dev/null ||
214 die "Cannot move HEAD to $first_parent"
216 case "$new_parents" in
217 ' '*' '*)
218 # redo merge
219 author_script=$(get_author_ident_from_commit $sha1)
220 eval "$author_script"
221 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
222 # No point in merging the first parent, that's HEAD
223 new_parents=${new_parents# $first_parent}
224 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
225 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
226 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
227 output git merge $STRATEGY -m "$msg" \
228 $new_parents
229 then
230 git rerere
231 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
232 die Error redoing merge $sha1
236 output git cherry-pick "$@" ||
237 die_with_patch $sha1 "Could not pick $sha1"
239 esac
241 esac
244 nth_string () {
245 case "$1" in
246 *1[0-9]|*[04-9]) echo "$1"th;;
247 *1) echo "$1"st;;
248 *2) echo "$1"nd;;
249 *3) echo "$1"rd;;
250 esac
253 make_squash_message () {
254 if test -f "$SQUASH_MSG"; then
255 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
256 < "$SQUASH_MSG" | sed -ne '$p')+1))
257 echo "# This is a combination of $COUNT commits."
258 sed -e 1d -e '2,/^./{
259 /^$/d
260 }' <"$SQUASH_MSG"
261 else
262 COUNT=2
263 echo "# This is a combination of two commits."
264 echo "# The first commit's message is:"
265 echo
266 git cat-file commit HEAD | sed -e '1,/^$/d'
268 echo
269 echo "# This is the $(nth_string $COUNT) commit message:"
270 echo
271 git cat-file commit $1 | sed -e '1,/^$/d'
274 peek_next_command () {
275 sed -n "1s/ .*$//p" < "$TODO"
278 do_next () {
279 rm -f "$DOTEST"/message "$DOTEST"/author-script \
280 "$DOTEST"/amend || exit
281 read command sha1 rest < "$TODO"
282 case "$command" in
283 '#'*|'')
284 mark_action_done
286 pick|p)
287 comment_for_reflog pick
289 mark_action_done
290 pick_one $sha1 ||
291 die_with_patch $sha1 "Could not apply $sha1... $rest"
293 edit|e)
294 comment_for_reflog edit
296 mark_action_done
297 pick_one $sha1 ||
298 die_with_patch $sha1 "Could not apply $sha1... $rest"
299 make_patch $sha1
300 git rev-parse --verify HEAD > "$DOTEST"/amend
301 warn "Stopped at $sha1... $rest"
302 warn "You can amend the commit now, with"
303 warn
304 warn " git commit --amend"
305 warn
306 warn "Once you are satisfied with your changes, run"
307 warn
308 warn " git rebase --continue"
309 warn
310 exit 0
312 squash|s)
313 comment_for_reflog squash
315 has_action "$DONE" ||
316 die "Cannot 'squash' without a previous commit"
318 mark_action_done
319 make_squash_message $sha1 > "$MSG"
320 case "$(peek_next_command)" in
321 squash|s)
322 EDIT_COMMIT=
323 USE_OUTPUT=output
324 cp "$MSG" "$SQUASH_MSG"
327 EDIT_COMMIT=-e
328 USE_OUTPUT=
329 rm -f "$SQUASH_MSG" || exit
331 esac
333 failed=f
334 author_script=$(get_author_ident_from_commit HEAD)
335 output git reset --soft HEAD^
336 pick_one -n $sha1 || failed=t
337 echo "$author_script" > "$DOTEST"/author-script
338 if test $failed = f
339 then
340 # This is like --amend, but with a different message
341 eval "$author_script"
342 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
343 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
344 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
345 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
347 if test $failed = t
348 then
349 cp "$MSG" "$GIT_DIR"/MERGE_MSG
350 warn
351 warn "Could not apply $sha1... $rest"
352 die_with_patch $sha1 ""
356 warn "Unknown command: $command $sha1 $rest"
357 die_with_patch $sha1 "Please fix this in the file $TODO."
359 esac
360 test -s "$TODO" && return
362 comment_for_reflog finish &&
363 HEADNAME=$(cat "$DOTEST"/head-name) &&
364 OLDHEAD=$(cat "$DOTEST"/head) &&
365 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
366 if test -d "$REWRITTEN"
367 then
368 test -f "$DOTEST"/current-commit &&
369 current_commit=$(cat "$DOTEST"/current-commit) &&
370 git rev-parse HEAD > "$REWRITTEN"/$current_commit
371 if test -f "$REWRITTEN"/$OLDHEAD
372 then
373 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
374 else
375 NEWHEAD=$OLDHEAD
377 else
378 NEWHEAD=$(git rev-parse HEAD)
379 fi &&
380 case $HEADNAME in
381 refs/*)
382 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
383 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
384 git symbolic-ref HEAD $HEADNAME
386 esac && {
387 test ! -f "$DOTEST"/verbose ||
388 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
389 } &&
390 rm -rf "$DOTEST" &&
391 git gc --auto &&
392 warn "Successfully rebased and updated $HEADNAME."
394 exit
397 do_rest () {
398 while :
400 do_next
401 done
404 # check if no other options are set
405 is_standalone () {
406 test $# -eq 2 -a "$2" = '--' &&
407 test -z "$ONTO" &&
408 test -z "$PRESERVE_MERGES" &&
409 test -z "$STRATEGY" &&
410 test -z "$VERBOSE"
413 get_saved_options () {
414 test -d "$REWRITTEN" && PRESERVE_MERGES=t
415 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
416 test -f "$DOTEST"/verbose && VERBOSE=t
419 while test $# != 0
421 case "$1" in
422 --no-verify)
423 OK_TO_SKIP_PRE_REBASE=yes
425 --verify)
427 --continue)
428 is_standalone "$@" || usage
429 get_saved_options
430 comment_for_reflog continue
432 test -d "$DOTEST" || die "No interactive rebase running"
434 # Sanity check
435 git rev-parse --verify HEAD >/dev/null ||
436 die "Cannot read HEAD"
437 git update-index --ignore-submodules --refresh &&
438 git diff-files --quiet --ignore-submodules ||
439 die "Working tree is dirty"
441 # do we have anything to commit?
442 if git diff-index --cached --quiet --ignore-submodules HEAD --
443 then
444 : Nothing to commit -- skip this
445 else
446 . "$DOTEST"/author-script ||
447 die "Cannot find the author identity"
448 amend=
449 if test -f "$DOTEST"/amend
450 then
451 amend=$(git rev-parse --verify HEAD)
452 test "$amend" = $(cat "$DOTEST"/amend) ||
453 die "\
454 You have uncommitted changes in your working tree. Please, commit them
455 first and then run 'git rebase --continue' again."
456 git reset --soft HEAD^ ||
457 die "Cannot rewind the HEAD"
459 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
460 git commit --no-verify -F "$DOTEST"/message -e || {
461 test -n "$amend" && git reset --soft $amend
462 die "Could not commit staged changes."
466 require_clean_work_tree
467 do_rest
469 --abort)
470 is_standalone "$@" || usage
471 get_saved_options
472 comment_for_reflog abort
474 git rerere clear
475 test -d "$DOTEST" || die "No interactive rebase running"
477 HEADNAME=$(cat "$DOTEST"/head-name)
478 HEAD=$(cat "$DOTEST"/head)
479 case $HEADNAME in
480 refs/*)
481 git symbolic-ref HEAD $HEADNAME
483 esac &&
484 output git reset --hard $HEAD &&
485 rm -rf "$DOTEST"
486 exit
488 --skip)
489 is_standalone "$@" || usage
490 get_saved_options
491 comment_for_reflog skip
493 git rerere clear
494 test -d "$DOTEST" || die "No interactive rebase running"
496 output git reset --hard && do_rest
499 case "$#,$1" in
500 *,*=*)
501 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
502 1,*)
503 usage ;;
505 STRATEGY="-s $2"
506 shift ;;
507 esac
510 # we use merge anyway
513 VERBOSE=t
516 PRESERVE_MERGES=t
519 # yeah, we know
521 --onto)
522 shift
523 ONTO=$(git rev-parse --verify "$1") ||
524 die "Does not point to a valid commit: $1"
527 shift
528 run_pre_rebase_hook ${1+"$@"}
529 test $# -eq 1 -o $# -eq 2 || usage
530 test -d "$DOTEST" &&
531 die "Interactive rebase already started"
533 git var GIT_COMMITTER_IDENT >/dev/null ||
534 die "You need to set your committer info first"
536 comment_for_reflog start
538 require_clean_work_tree
540 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
541 test -z "$ONTO" && ONTO=$UPSTREAM
543 if test ! -z "$2"
544 then
545 output git show-ref --verify --quiet "refs/heads/$2" ||
546 die "Invalid branchname: $2"
547 output git checkout "$2" ||
548 die "Could not checkout $2"
551 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
552 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
554 : > "$DOTEST"/interactive || die "Could not mark as interactive"
555 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
556 echo "detached HEAD" > "$DOTEST"/head-name
558 echo $HEAD > "$DOTEST"/head
559 echo $UPSTREAM > "$DOTEST"/upstream
560 echo $ONTO > "$DOTEST"/onto
561 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
562 test t = "$VERBOSE" && : > "$DOTEST"/verbose
563 if test t = "$PRESERVE_MERGES"
564 then
565 # $REWRITTEN contains files for each commit that is
566 # reachable by at least one merge base of $HEAD and
567 # $UPSTREAM. They are not necessarily rewritten, but
568 # their children might be.
569 # This ensures that commits on merged, but otherwise
570 # unrelated side branches are left alone. (Think "X"
571 # in the man page's example.)
572 mkdir "$REWRITTEN" &&
573 for c in $(git merge-base --all $HEAD $UPSTREAM)
575 echo $ONTO > "$REWRITTEN"/$c ||
576 die "Could not init rewritten commits"
577 done
578 MERGES_OPTION=
579 else
580 MERGES_OPTION=--no-merges
583 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
584 SHORTHEAD=$(git rev-parse --short $HEAD)
585 SHORTONTO=$(git rev-parse --short $ONTO)
586 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
587 --abbrev=7 --reverse --left-right --cherry-pick \
588 $UPSTREAM...$HEAD | \
589 sed -n "s/^>/pick /p" > "$TODO"
590 cat >> "$TODO" << EOF
592 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
594 # Commands:
595 # p, pick = use commit
596 # e, edit = use commit, but stop for amending
597 # s, squash = use commit, but meld into previous commit
599 # If you remove a line here THAT COMMIT WILL BE LOST.
600 # However, if you remove everything, the rebase will be aborted.
604 has_action "$TODO" ||
605 die_abort "Nothing to do"
607 cp "$TODO" "$TODO".backup
608 git_editor "$TODO" ||
609 die "Could not execute editor"
611 has_action "$TODO" ||
612 die_abort "Nothing to do"
614 git update-ref ORIG_HEAD $HEAD
615 output git checkout $ONTO && do_rest
617 esac
618 shift
619 done