X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-checkout.sh;h=bd74d701a1f0abb63e2bcdcb47035cb2c6eee745;hb=ecb879f8772d5970bf1eb3677f9c9ebdd6e116a1;hp=ed7c2c5f6aab338c844329e3ae3d9c7ce003680f;hpb=404fdef22f1084141aeef5781d5a322554fed481;p=git.git diff --git a/git-checkout.sh b/git-checkout.sh index ed7c2c5f6..bd74d701a 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -1,13 +1,23 @@ #!/bin/sh -USAGE='[-q] [-f] [-b ] [-m] [] [...]' +OPTIONS_KEEPDASHDASH=t +OPTIONS_SPEC="\ +git-checkout [options] [] [...] +-- +b= create a new branch started at +l create the new branch's reflog +track arrange that the new branch tracks the remote branch +f proceed even if the index or working tree is not HEAD +m merge local modifications into the new branch +q,quiet be quiet +" SUBDIRECTORY_OK=Sometimes . git-sh-setup require_work_tree old_name=HEAD -old=$(git-rev-parse --verify $old_name 2>/dev/null) -oldbranch=$(git-symbolic-ref $old_name 2>/dev/null) +old=$(git rev-parse --verify $old_name 2>/dev/null) +oldbranch=$(git symbolic-ref $old_name 2>/dev/null) new= new_name= force= @@ -20,77 +30,68 @@ quiet= v=-v LF=' ' -while [ "$#" != "0" ]; do - arg="$1" - shift - case "$arg" in - "-b") - newbranch="$1" + +while test $# != 0; do + case "$1" in + -b) shift + newbranch="$1" [ -z "$newbranch" ] && die "git checkout: -b needs a branch name" - git-show-ref --verify --quiet -- "refs/heads/$newbranch" && + git show-ref --verify --quiet -- "refs/heads/$newbranch" && die "git checkout: branch $newbranch already exists" - git-check-ref-format "heads/$newbranch" || + git check-ref-format "heads/$newbranch" || die "git checkout: we do not like '$newbranch' as a branch name." ;; - "-l") + -l) newbranch_log=-l ;; - "--track"|"--no-track") - track="$arg" + --track|--no-track) + track="$1" ;; - "-f") + -f) force=1 ;; -m) merge=1 ;; - "-q") + -q|--quiet) quiet=1 v= ;; --) + shift break ;; - -*) - usage - ;; *) - if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null) - then - if [ -z "$rev" ]; then - echo "unknown flag $arg" - exit 1 - fi - new="$rev" - new_name="$arg" - if git-show-ref --verify --quiet -- "refs/heads/$arg" - then - branch="$arg" - fi - elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null) - then - # checking out selected paths from a tree-ish. - new="$rev" - new_name="$arg^{tree}" - branch= - else - new= - new_name= - branch= - set x "$arg" "$@" - shift - fi - case "$1" in - --) - shift ;; - esac - break + usage ;; - esac + esac + shift done +arg="$1" +rev=$(git rev-parse --verify "$arg" 2>/dev/null) +if rev=$(git rev-parse --verify "$rev^0" 2>/dev/null) +then + [ -z "$rev" ] && die "unknown flag $arg" + new_name="$arg" + if git show-ref --verify --quiet -- "refs/heads/$arg" + then + rev=$(git rev-parse --verify "refs/heads/$arg^0") + branch="$arg" + fi + new="$rev" + shift +elif rev=$(git rev-parse --verify "$rev^{tree}" 2>/dev/null) +then + # checking out selected paths from a tree-ish. + new="$rev" + new_name="$rev^{tree}" + shift +fi +[ "$1" = "--" ] && shift + case "$newbranch,$track" in ,--*) die "git checkout: --track and --no-track require -b" @@ -128,14 +129,21 @@ Did you intend to checkout '$@' which can not be resolved as commit?" # from a specific tree-ish; note that this is for # rescuing paths and is never meant to remove what # is not in the named tree-ish. - git-ls-tree --full-name -r "$new" "$@" | - git-update-index --index-info || exit $? + git ls-tree --full-name -r "$new" "$@" | + git update-index --index-info || exit $? fi # Make sure the request is about existing paths. - git-ls-files --error-unmatch -- "$@" >/dev/null || exit - git-ls-files -- "$@" | - git-checkout-index -f -u --stdin + git ls-files --full-name --error-unmatch -- "$@" >/dev/null || exit + git ls-files --full-name -- "$@" | + (cd_to_toplevel && git checkout-index -f -u --stdin) + + # Run a post-checkout hook -- the HEAD does not change so the + # current HEAD is passed in for both args + if test -x "$GIT_DIR"/hooks/post-checkout; then + "$GIT_DIR"/hooks/post-checkout $old $old 0 + fi + exit $? else # Make sure we did not fall back on $arg^{tree} codepath @@ -143,7 +151,7 @@ else # but switching branches. if test '' != "$new" then - git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 || + git rev-parse --verify "$new^{commit}" >/dev/null 2>&1 || die "Cannot switch branch to a non-commit." fi fi @@ -168,7 +176,7 @@ detach_warn= describe_detached_head () { test -n "$quiet" || { printf >&2 "$1 " - GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2" + GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2" -- } } @@ -199,10 +207,10 @@ fi if [ "$force" ] then - git-read-tree $v --reset -u $new + git read-tree $v --reset -u $new else - git-update-index --refresh >/dev/null - merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || ( + git update-index --refresh >/dev/null + merge_error=$(git read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || ( case "$merge" in '') echo >&2 "$merge_error" @@ -210,7 +218,7 @@ else esac # Match the index to the working tree, and do a three-way. - git diff-files --name-only | git update-index --remove --stdin && + git diff-files --name-only | git update-index --remove --stdin && work=`git write-tree` && git read-tree $v --reset -u $new || exit @@ -245,7 +253,7 @@ else (exit $saved_err) fi -# +# # Switch the HEAD pointer to the new branch if we # checked out a branch head, and remove any potential # old MERGE_HEAD's (subsequent commits will clearly not @@ -253,12 +261,13 @@ fi # if [ "$?" -eq 0 ]; then if [ "$newbranch" ]; then - git-branch $track $newbranch_log "$newbranch" "$new_name" || exit + git branch $track $newbranch_log "$newbranch" "$new_name" || exit branch="$newbranch" fi if test -n "$branch" then - GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch" + old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'` + GIT_DIR="$GIT_DIR" git symbolic-ref -m "checkout: moving from ${old_branch_name:-$old} to $branch" HEAD "refs/heads/$branch" if test -n "$quiet" then true # nothing @@ -270,15 +279,8 @@ if [ "$?" -eq 0 ]; then fi elif test -n "$detached" then - # NEEDSWORK: we would want a command to detach the HEAD - # atomically, instead of this handcrafted command sequence. - # Perhaps: - # git update-ref --detach HEAD $new - # or something like that... - # - git-rev-parse HEAD >"$GIT_DIR/HEAD.new" && - mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" && - git-update-ref -m "checkout: moving to $arg" HEAD "$detached" || + old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'` + git update-ref --no-deref -m "checkout: moving from ${old_branch_name:-$old} to $arg" HEAD "$detached" || die "Cannot detach HEAD" if test -n "$detach_warn" then @@ -290,3 +292,8 @@ if [ "$?" -eq 0 ]; then else exit 1 fi + +# Run a post-checkout hook +if test -x "$GIT_DIR"/hooks/post-checkout; then + "$GIT_DIR"/hooks/post-checkout $old $new 1 +fi