rebase: stricter check of standalone sub command
[gitweb.git] / git-rebase--interactive.sh
index 6b86abc64bfe0034caf23cf9530955128766de32..824f4f303d9de2cf779981464888a03cfda17a01 100755 (executable)
@@ -28,6 +28,7 @@ continue           continue rebasing process
 abort              abort rebasing process and restore original branch
 skip               skip current patch and continue rebasing process
 no-verify          override pre-rebase hook from stopping the operation
+verify             allow pre-rebase hook to run
 root               rebase all reachable commmits up to the root(s)
 autosquash         move commits that begin with squash!/fixup! under -i
 "
@@ -111,15 +112,16 @@ VERBOSE=
 OK_TO_SKIP_PRE_REBASE=
 REBASE_ROOT=
 AUTOSQUASH=
+test "$(git config --bool rebase.autosquash)" = "true" && AUTOSQUASH=t
 NEVER_FF=
 
-GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
-mark the corrected paths with 'git add <paths>', and
-run 'git rebase --continue'"
+GIT_CHERRY_PICK_HELP="\
+hint: after resolving the conflicts, mark the corrected paths
+hint: with 'git add <paths>' and run 'git rebase --continue'"
 export GIT_CHERRY_PICK_HELP
 
 warn () {
-       echo "$*" >&2
+       printf '%s\n' "$*" >&2
 }
 
 output () {
@@ -152,14 +154,6 @@ run_pre_rebase_hook () {
        fi
 }
 
-require_clean_work_tree () {
-       # test if working tree is dirty
-       git rev-parse --verify HEAD > /dev/null &&
-       git update-index --ignore-submodules --refresh &&
-       git diff-files --quiet --ignore-submodules &&
-       git diff-index --cached --quiet HEAD --ignore-submodules -- ||
-       die "Working tree is dirty"
-}
 
 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
 
@@ -263,10 +257,10 @@ pick_one_preserving_merges () {
        then
                if test "$fast_forward" = t
                then
-                       cat "$DOTEST"/current-commit | while read current_commit
+                       while read current_commit
                        do
                                git rev-parse HEAD > "$REWRITTEN"/$current_commit
-                       done
+                       done <"$DOTEST"/current-commit
                        rm "$DOTEST"/current-commit ||
                        die "Cannot write current commit's replacement sha1"
                fi
@@ -440,9 +434,9 @@ record_in_rewritten() {
        echo "$oldsha1" >> "$REWRITTEN_PENDING"
 
        case "$(peek_next_command)" in
-           squash|s|fixup|f)
+       squash|s|fixup|f)
                ;;
-           *)
+       *)
                flush_rewritten_pending
                ;;
        esac
@@ -450,7 +444,7 @@ record_in_rewritten() {
 
 do_next () {
        rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
-       read command sha1 rest < "$TODO"
+       read -r command sha1 rest < "$TODO"
        case "$command" in
        '#'*|''|noop)
                mark_action_done
@@ -537,6 +531,34 @@ do_next () {
                esac
                record_in_rewritten $sha1
                ;;
+       x|"exec")
+               read -r command rest < "$TODO"
+               mark_action_done
+               printf 'Executing: %s\n' "$rest"
+               # "exec" command doesn't take a sha1 in the todo-list.
+               # => can't just use $sha1 here.
+               git rev-parse --verify HEAD > "$DOTEST"/stopped-sha
+               ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
+               status=$?
+               if test "$status" -ne 0
+               then
+                       warn "Execution failed: $rest"
+                       warn "You can fix the problem, and then run"
+                       warn
+                       warn "  git rebase --continue"
+                       warn
+                       exit "$status"
+               fi
+               # Run in subshell because require_clean_work_tree can die.
+               if ! (require_clean_work_tree "rebase")
+               then
+                       warn "Commit or stash your changes, and then run"
+                       warn
+                       warn "  git rebase --continue"
+                       warn
+                       exit 1
+               fi
+               ;;
        *)
                warn "Unknown command: $command $sha1 $rest"
                if git rev-parse --verify -q "$sha1" >/dev/null
@@ -591,22 +613,30 @@ do_rest () {
 # skip picking commits whose parents are unchanged
 skip_unnecessary_picks () {
        fd=3
-       while read command sha1 rest
+       while read -r command rest
        do
                # fd=3 means we skip the command
-               case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
-               3,pick,"$ONTO"*|3,p,"$ONTO"*)
+               case "$fd,$command" in
+               3,pick|3,p)
                        # pick a commit whose parent is current $ONTO -> skip
-                       ONTO=$sha1
+                       sha1=${rest%% *}
+                       case "$(git rev-parse --verify --quiet "$sha1"^)" in
+                       "$ONTO"*)
+                               ONTO=$sha1
+                               ;;
+                       *)
+                               fd=1
+                               ;;
+                       esac
                        ;;
-               3,#*|3,,*)
+               3,#*|3,)
                        # copy comments
                        ;;
                *)
                        fd=1
                        ;;
                esac
-               echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
+               printf '%s\n' "$command${rest:+ }$rest" >&$fd
        done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
        mv -f "$TODO".new "$TODO" &&
        case "$(peek_next_command)" in
@@ -638,26 +668,56 @@ get_saved_options () {
 # comes immediately after the former, and change "pick" to
 # "fixup"/"squash".
 rearrange_squash () {
-       sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
-               -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
-               "$1" >"$1.sq"
+       # extract fixup!/squash! lines and resolve any referenced sha1's
+       while read -r pick sha1 message
+       do
+               case "$message" in
+               "squash! "*|"fixup! "*)
+                       action="${message%%!*}"
+                       rest="${message#*! }"
+                       echo "$sha1 $action $rest"
+                       # if it's a single word, try to resolve to a full sha1 and
+                       # emit a second copy. This allows us to match on both message
+                       # and on sha1 prefix
+                       if test "${rest#* }" = "$rest"; then
+                               fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
+                               if test -n "$fullsha"; then
+                                       # prefix the action to uniquely identify this line as
+                                       # intended for full sha1 match
+                                       echo "$sha1 +$action $fullsha"
+                               fi
+                       fi
+               esac
+       done >"$1.sq" <"$1"
        test -s "$1.sq" || return
 
        used=
-       while read pick sha1 message
+       while read -r pick sha1 message
        do
                case " $used" in
                *" $sha1 "*) continue ;;
                esac
-               echo "$pick $sha1 $message"
-               while read squash action msg
+               printf '%s\n' "$pick $sha1 $message"
+               used="$used$sha1 "
+               while read -r squash action msg
                do
-                       case "$message" in
-                       "$msg"*)
-                               echo "$action $squash $action! $msg"
-                               used="$used$squash "
-                               ;;
+                       case " $used" in
+                       *" $squash "*) continue ;;
                        esac
+                       emit=0
+                       case "$action" in
+                       +*)
+                               action="${action#+}"
+                               # full sha1 prefix test
+                               case "$msg" in "$sha1"*) emit=1;; esac ;;
+                       *)
+                               # message prefix test
+                               case "$message" in "$msg"*) emit=1;; esac ;;
+                       esac
+                       if test $emit = 1; then
+                               printf '%s\n' "$action $squash $action! $msg"
+                               used="$used$squash "
+                       fi
                done <"$1.sq"
        done >"$1.rearranged" <"$1"
        cat "$1.rearranged" >"$1"
@@ -690,6 +750,7 @@ do
                OK_TO_SKIP_PRE_REBASE=yes
                ;;
        --verify)
+               OK_TO_SKIP_PRE_REBASE=
                ;;
        --continue)
                is_standalone "$@" || usage
@@ -731,7 +792,7 @@ first and then run 'git rebase --continue' again."
 
                record_in_rewritten "$(cat "$DOTEST"/stopped-sha)"
 
-               require_clean_work_tree
+               require_clean_work_tree "rebase"
                do_rest
                ;;
        --abort)
@@ -795,6 +856,9 @@ first and then run 'git rebase --continue' again."
        --autosquash)
                AUTOSQUASH=t
                ;;
+       --no-autosquash)
+               AUTOSQUASH=
+               ;;
        --onto)
                shift
                ONTO=$(parse_onto "$1") ||
@@ -802,151 +866,158 @@ first and then run 'git rebase --continue' again."
                ;;
        --)
                shift
-               test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
-               test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
-               test -d "$DOTEST" &&
-                       die "Interactive rebase already started"
-
-               git var GIT_COMMITTER_IDENT >/dev/null ||
-                       die "You need to set your committer info first"
-
-               if test -z "$REBASE_ROOT"
-               then
-                       UPSTREAM_ARG="$1"
-                       UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
-                       test -z "$ONTO" && ONTO=$UPSTREAM
-                       shift
-               else
-                       UPSTREAM=
-                       UPSTREAM_ARG=--root
-                       test -z "$ONTO" &&
-                               die "You must specify --onto when using --root"
-               fi
-               run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
-
-               comment_for_reflog start
-
-               require_clean_work_tree
-
-               if test ! -z "$1"
-               then
-                       output git checkout "$1" ||
-                               die "Could not checkout $1"
-               fi
+               break
+               ;;
+       esac
+       shift
+done
 
-               HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
-               mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
+test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
+test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
+test -d "$DOTEST" &&
+       die "Interactive rebase already started"
 
-               : > "$DOTEST"/interactive || die "Could not mark as interactive"
-               git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
-                       echo "detached HEAD" > "$DOTEST"/head-name
+git var GIT_COMMITTER_IDENT >/dev/null ||
+       die "You need to set your committer info first"
 
-               echo $HEAD > "$DOTEST"/head
-               case "$REBASE_ROOT" in
-               '')
-                       rm -f "$DOTEST"/rebase-root ;;
-               *)
-                       : >"$DOTEST"/rebase-root ;;
-               esac
-               echo $ONTO > "$DOTEST"/onto
-               test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
-               test t = "$VERBOSE" && : > "$DOTEST"/verbose
-               if test t = "$PRESERVE_MERGES"
-               then
-                       if test -z "$REBASE_ROOT"
-                       then
-                               mkdir "$REWRITTEN" &&
-                               for c in $(git merge-base --all $HEAD $UPSTREAM)
-                               do
-                                       echo $ONTO > "$REWRITTEN"/$c ||
-                                               die "Could not init rewritten commits"
-                               done
-                       else
-                               mkdir "$REWRITTEN" &&
-                               echo $ONTO > "$REWRITTEN"/root ||
-                                       die "Could not init rewritten commits"
-                       fi
-                       # No cherry-pick because our first pass is to determine
-                       # parents to rewrite and skipping dropped commits would
-                       # prematurely end our probe
-                       MERGES_OPTION=
-                       first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
-               else
-                       MERGES_OPTION="--no-merges --cherry-pick"
-               fi
-
-               SHORTHEAD=$(git rev-parse --short $HEAD)
-               SHORTONTO=$(git rev-parse --short $ONTO)
-               if test -z "$REBASE_ROOT"
-                       # this is now equivalent to ! -z "$UPSTREAM"
-               then
-                       SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
-                       REVISIONS=$UPSTREAM...$HEAD
-                       SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
-               else
-                       REVISIONS=$ONTO...$HEAD
-                       SHORTREVISIONS=$SHORTHEAD
-               fi
-               git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
-                       --abbrev=7 --reverse --left-right --topo-order \
-                       $REVISIONS | \
-                       sed -n "s/^>//p" | while read shortsha1 rest
+if test -z "$REBASE_ROOT"
+then
+       UPSTREAM_ARG="$1"
+       UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
+       test -z "$ONTO" && ONTO=$UPSTREAM
+       shift
+else
+       UPSTREAM=
+       UPSTREAM_ARG=--root
+       test -z "$ONTO" &&
+       die "You must specify --onto when using --root"
+fi
+run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
+
+comment_for_reflog start
+
+require_clean_work_tree "rebase" "Please commit or stash them."
+
+if test ! -z "$1"
+then
+       output git checkout "$1" -- ||
+               die "Could not checkout $1"
+fi
+
+HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
+mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
+
+: > "$DOTEST"/interactive || die "Could not mark as interactive"
+git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
+       echo "detached HEAD" > "$DOTEST"/head-name
+
+echo $HEAD > "$DOTEST"/head
+case "$REBASE_ROOT" in
+'')
+       rm -f "$DOTEST"/rebase-root ;;
+*)
+       : >"$DOTEST"/rebase-root ;;
+esac
+echo $ONTO > "$DOTEST"/onto
+test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
+test t = "$VERBOSE" && : > "$DOTEST"/verbose
+if test t = "$PRESERVE_MERGES"
+then
+       if test -z "$REBASE_ROOT"
+       then
+               mkdir "$REWRITTEN" &&
+               for c in $(git merge-base --all $HEAD $UPSTREAM)
                do
-                       if test t != "$PRESERVE_MERGES"
-                       then
-                               echo "pick $shortsha1 $rest" >> "$TODO"
-                       else
-                               sha1=$(git rev-parse $shortsha1)
-                               if test -z "$REBASE_ROOT"
-                               then
-                                       preserve=t
-                                       for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
-                                       do
-                                               if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
-                                               then
-                                                       preserve=f
-                                               fi
-                                       done
-                               else
-                                       preserve=f
-                               fi
-                               if test f = "$preserve"
-                               then
-                                       touch "$REWRITTEN"/$sha1
-                                       echo "pick $shortsha1 $rest" >> "$TODO"
-                               fi
-                       fi
+                       echo $ONTO > "$REWRITTEN"/$c ||
+                               die "Could not init rewritten commits"
                done
-
-               # Watch for commits that been dropped by --cherry-pick
-               if test t = "$PRESERVE_MERGES"
+       else
+               mkdir "$REWRITTEN" &&
+               echo $ONTO > "$REWRITTEN"/root ||
+                       die "Could not init rewritten commits"
+       fi
+       # No cherry-pick because our first pass is to determine
+       # parents to rewrite and skipping dropped commits would
+       # prematurely end our probe
+       MERGES_OPTION=
+       first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
+else
+       MERGES_OPTION="--no-merges --cherry-pick"
+fi
+
+SHORTHEAD=$(git rev-parse --short $HEAD)
+SHORTONTO=$(git rev-parse --short $ONTO)
+if test -z "$REBASE_ROOT"
+       # this is now equivalent to ! -z "$UPSTREAM"
+then
+       SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
+       REVISIONS=$UPSTREAM...$HEAD
+       SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
+else
+       REVISIONS=$ONTO...$HEAD
+       SHORTREVISIONS=$SHORTHEAD
+fi
+git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
+       --abbrev=7 --reverse --left-right --topo-order \
+       $REVISIONS | \
+       sed -n "s/^>//p" |
+while read -r shortsha1 rest
+do
+       if test t != "$PRESERVE_MERGES"
+       then
+               printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
+       else
+               sha1=$(git rev-parse $shortsha1)
+               if test -z "$REBASE_ROOT"
                then
-                       mkdir "$DROPPED"
-                       # Save all non-cherry-picked changes
-                       git rev-list $REVISIONS --left-right --cherry-pick | \
-                               sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
-                       # Now all commits and note which ones are missing in
-                       # not-cherry-picks and hence being dropped
-                       git rev-list $REVISIONS |
-                       while read rev
+                       preserve=t
+                       for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
                        do
-                               if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
+                               if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
                                then
-                                       # Use -f2 because if rev-list is telling us this commit is
-                                       # not worthwhile, we don't want to track its multiple heads,
-                                       # just the history of its first-parent for others that will
-                                       # be rebasing on top of it
-                                       git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
-                                       short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
-                                       sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
-                                       rm "$REWRITTEN"/$rev
+                                       preserve=f
                                fi
                        done
+               else
+                       preserve=f
+               fi
+               if test f = "$preserve"
+               then
+                       touch "$REWRITTEN"/$sha1
+                       printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
+               fi
+       fi
+done
+
+# Watch for commits that been dropped by --cherry-pick
+if test t = "$PRESERVE_MERGES"
+then
+       mkdir "$DROPPED"
+       # Save all non-cherry-picked changes
+       git rev-list $REVISIONS --left-right --cherry-pick | \
+               sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
+       # Now all commits and note which ones are missing in
+       # not-cherry-picks and hence being dropped
+       git rev-list $REVISIONS |
+       while read rev
+       do
+               if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
+               then
+                       # Use -f2 because if rev-list is telling us this commit is
+                       # not worthwhile, we don't want to track its multiple heads,
+                       # just the history of its first-parent for others that will
+                       # be rebasing on top of it
+                       git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
+                       short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
+                       sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
+                       rm "$REWRITTEN"/$rev
                fi
+       done
+fi
 
-               test -s "$TODO" || echo noop >> "$TODO"
-               test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
-               cat >> "$TODO" << EOF
+test -s "$TODO" || echo noop >> "$TODO"
+test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
+cat >> "$TODO" << EOF
 
 # Rebase $SHORTREVISIONS onto $SHORTONTO
 #
@@ -956,28 +1027,25 @@ first and then run 'git rebase --continue' again."
 #  e, edit = use commit, but stop for amending
 #  s, squash = use commit, but meld into previous commit
 #  f, fixup = like "squash", but discard this commit's log message
+#  x, exec = run command (the rest of the line) using shell
 #
 # If you remove a line here THAT COMMIT WILL BE LOST.
 # However, if you remove everything, the rebase will be aborted.
 #
 EOF
 
-               has_action "$TODO" ||
-                       die_abort "Nothing to do"
+has_action "$TODO" ||
+       die_abort "Nothing to do"
 
-               cp "$TODO" "$TODO".backup
-               git_editor "$TODO" ||
-                       die_abort "Could not execute editor"
+cp "$TODO" "$TODO".backup
+git_editor "$TODO" ||
+       die_abort "Could not execute editor"
 
-               has_action "$TODO" ||
-                       die_abort "Nothing to do"
+has_action "$TODO" ||
+       die_abort "Nothing to do"
 
-               test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
+test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
 
-               output git checkout $ONTO || die_abort "could not detach HEAD"
-               git update-ref ORIG_HEAD $HEAD
-               do_rest
-               ;;
-       esac
-       shift
-done
+output git checkout $ONTO || die_abort "could not detach HEAD"
+git update-ref ORIG_HEAD $HEAD
+do_rest