t0027: Tests for core.eol=native, eol=lf, eol=crlf
[gitweb.git] / contrib / examples / git-checkout.sh
index 5621c69d86062c7c75c0b8c2749d34efc78cafb4..683cae7c3fad09bec012351b530b7e909d70890f 100755 (executable)
@@ -71,7 +71,8 @@ while test $# != 0; do
 done
 
 arg="$1"
-if rev=$(git rev-parse --verify "$arg^0" 2>/dev/null)
+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"
@@ -82,11 +83,11 @@ then
        fi
        new="$rev"
        shift
-elif rev=$(git rev-parse --verify "$arg^{tree}" 2>/dev/null)
+elif rev=$(git rev-parse --verify "$rev^{tree}" 2>/dev/null)
 then
        # checking out selected paths from a tree-ish.
        new="$rev"
-       new_name="$arg^{tree}"
+       new_name="$rev^{tree}"
        shift
 fi
 [ "$1" = "--" ] && shift
@@ -167,7 +168,7 @@ cd_to_toplevel
 # branch.  However, if "git checkout HEAD" detaches the HEAD
 # from the current branch, even though that may be logically
 # correct, it feels somewhat funny.  More importantly, we do not
-# want "git checkout" nor "git checkout -f" to detach HEAD.
+# want "git checkout" or "git checkout -f" to detach HEAD.
 
 detached=
 detach_warn=
@@ -209,16 +210,19 @@ then
     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) || (
-       case "$merge" in
-       '')
-               echo >&2 "$merge_error"
+    git read-tree $v -m -u --exclude-per-directory=.gitignore $old $new || (
+       case "$merge,$v" in
+       ,*)
                exit 1 ;;
+       1,)
+               ;; # quiet
+       *)
+               echo >&2 "Falling back to 3-way merge..." ;;
        esac
 
        # Match the index to the working tree, and do a three-way.
        git diff-files --name-only | git update-index --remove --stdin &&
-       work=`git write-tree` &&
+       work=$(git write-tree) &&
        git read-tree $v --reset -u $new || exit
 
        eval GITHEAD_$new='${new_name:-${branch:-$new}}' &&
@@ -229,7 +233,7 @@ else
        # Do not register the cleanly merged paths in the index yet.
        # this is not a real merge before committing, but just carrying
        # the working tree changes along.
-       unmerged=`git ls-files -u`
+       unmerged=$(git ls-files -u)
        git read-tree $v --reset $new
        case "$unmerged" in
        '')     ;;
@@ -265,7 +269,7 @@ if [ "$?" -eq 0 ]; then
        fi
        if test -n "$branch"
        then
-               old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
+               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
@@ -278,7 +282,7 @@ if [ "$?" -eq 0 ]; then
                fi
        elif test -n "$detached"
        then
-               old_branch_name=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
+               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"