Code

Merge branch 'mw/maint-gcc-warns-unused-write' into maint
[git.git] / t / t7201-co.sh
index c9abed6a2b18404306e281c7b5d161686f1c3b20..d20ed61b481539b25c054c49bd82aa6fb9b3a981 100755 (executable)
@@ -166,19 +166,31 @@ test_expect_success 'checkout -m with merge conflict' '
        ! test -s current
 '
 
-test_expect_success 'checkout to detach HEAD' '
+test_expect_success 'checkout to detach HEAD (with advice declined)' '
 
+       git config advice.detachedHead false &&
        git checkout -f renamer && git clean -f &&
        git checkout renamer^ 2>messages &&
-       (cat >messages.expect <<EOF
-Note: moving to "renamer^" which isn'"'"'t a local branch
-If you want to create a new branch from this checkout, you may do so
-(now or later) by using -b with the checkout command again. Example:
-  git checkout -b <new_branch_name>
-HEAD is now at 7329388... Initial A one, A two
-EOF
-) &&
-       test_cmp messages.expect messages &&
+       grep "HEAD is now at 7329388" messages &&
+       test 1 -eq $(wc -l <messages) &&
+       H=$(git rev-parse --verify HEAD) &&
+       M=$(git show-ref -s --verify refs/heads/master) &&
+       test "z$H" = "z$M" &&
+       if git symbolic-ref HEAD >/dev/null 2>&1
+       then
+               echo "OOPS, HEAD is still symbolic???"
+               false
+       else
+               : happy
+       fi
+'
+
+test_expect_success 'checkout to detach HEAD' '
+       git config advice.detachedHead true &&
+       git checkout -f renamer && git clean -f &&
+       git checkout renamer^ 2>messages &&
+       grep "HEAD is now at 7329388" messages &&
+       test 1 -lt $(wc -l <messages) &&
        H=$(git rev-parse --verify HEAD) &&
        M=$(git show-ref -s --verify refs/heads/master) &&
        test "z$H" = "z$M" &&
@@ -351,7 +363,39 @@ test_expect_success 'detach a symbolic link HEAD' '
     test "z$(git rev-parse --verify refs/heads/master)" = "z$here"
 '
 
-test_expect_success 'checkout an unmerged path should fail' '
+test_expect_success \
+    'checkout with --track fakes a sensible -b <name>' '
+    git update-ref refs/remotes/origin/koala/bear renamer &&
+    git update-ref refs/new/koala/bear renamer &&
+
+    git checkout --track origin/koala/bear &&
+    test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+    test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
+
+    git checkout master && git branch -D koala/bear &&
+
+    git checkout --track refs/remotes/origin/koala/bear &&
+    test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+    test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
+
+    git checkout master && git branch -D koala/bear &&
+
+    git checkout --track remotes/origin/koala/bear &&
+    test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+    test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
+
+    git checkout master && git branch -D koala/bear &&
+
+    git checkout --track refs/new/koala/bear &&
+    test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+    test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
+'
+
+test_expect_success \
+    'checkout with --track, but without -b, fails with too short tracked name' '
+    test_must_fail git checkout --track renamer'
+
+setup_conflicting_index () {
        rm -f .git/index &&
        O=$(echo original | git hash-object -w --stdin) &&
        A=$(echo ourside | git hash-object -w --stdin) &&
@@ -362,7 +406,11 @@ test_expect_success 'checkout an unmerged path should fail' '
                echo "100644 $A 2       file" &&
                echo "100644 $B 3       file" &&
                echo "100644 $A 0       filf"
-       ) | git update-index --index-info &&
+       ) | git update-index --index-info
+}
+
+test_expect_success 'checkout an unmerged path should fail' '
+       setup_conflicting_index &&
        echo "none of the above" >sample &&
        cat sample >fild &&
        cat sample >file &&
@@ -373,6 +421,121 @@ test_expect_success 'checkout an unmerged path should fail' '
        test_cmp sample file
 '
 
+test_expect_success 'checkout with an unmerged path can be ignored' '
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout -f fild file filf &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp sample file
+'
+
+test_expect_success 'checkout unmerged stage' '
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout --ours . &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp expect file &&
+       git checkout --theirs file &&
+       test ztheirside = "z$(cat file)"
+'
+
+test_expect_success 'checkout with --merge' '
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout -m -- fild file filf &&
+       (
+               echo "<<<<<<< ours"
+               echo ourside
+               echo "======="
+               echo theirside
+               echo ">>>>>>> theirs"
+       ) >merged &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp merged file
+'
+
+test_expect_success 'checkout with --merge, in diff3 -m style' '
+       git config merge.conflictstyle diff3 &&
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout -m -- fild file filf &&
+       (
+               echo "<<<<<<< ours"
+               echo ourside
+               echo "|||||||"
+               echo original
+               echo "======="
+               echo theirside
+               echo ">>>>>>> theirs"
+       ) >merged &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp merged file
+'
+
+test_expect_success 'checkout --conflict=merge, overriding config' '
+       git config merge.conflictstyle diff3 &&
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout --conflict=merge -- fild file filf &&
+       (
+               echo "<<<<<<< ours"
+               echo ourside
+               echo "======="
+               echo theirside
+               echo ">>>>>>> theirs"
+       ) >merged &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp merged file
+'
+
+test_expect_success 'checkout --conflict=diff3' '
+       git config --unset merge.conflictstyle
+       setup_conflicting_index &&
+       echo "none of the above" >sample &&
+       echo ourside >expect &&
+       cat sample >fild &&
+       cat sample >file &&
+       cat sample >filf &&
+       git checkout --conflict=diff3 -- fild file filf &&
+       (
+               echo "<<<<<<< ours"
+               echo ourside
+               echo "|||||||"
+               echo original
+               echo "======="
+               echo theirside
+               echo ">>>>>>> theirs"
+       ) >merged &&
+       test_cmp expect fild &&
+       test_cmp expect filf &&
+       test_cmp merged file
+'
+
 test_expect_success 'failing checkout -b should not break working tree' '
        git reset --hard master &&
        git symbolic-ref HEAD refs/heads/master &&
@@ -383,4 +546,69 @@ test_expect_success 'failing checkout -b should not break working tree' '
 
 '
 
+test_expect_success 'switch out of non-branch' '
+       git reset --hard master &&
+       git checkout master^0 &&
+       echo modified >one &&
+       test_must_fail git checkout renamer 2>error.log &&
+       ! grep "^Previous HEAD" error.log
+'
+
+(
+ echo "#!$SHELL_PATH"
+ cat <<\EOF
+O=$1 A=$2 B=$3
+cat "$A" >.tmp
+exec >"$A"
+echo '<<<<<<< filfre-theirs'
+cat "$B"
+echo '||||||| filfre-common'
+cat "$O"
+echo '======='
+cat ".tmp"
+echo '>>>>>>> filfre-ours'
+rm -f .tmp
+exit 1
+EOF
+) >filfre.sh
+chmod +x filfre.sh
+
+test_expect_success 'custom merge driver with checkout -m' '
+       git reset --hard &&
+
+       git config merge.filfre.driver "./filfre.sh %O %A %B" &&
+       git config merge.filfre.name "Feel-free merge driver" &&
+       git config merge.filfre.recursive binary &&
+       echo "arm merge=filfre" >.gitattributes &&
+
+       git checkout -b left &&
+       echo neutral >arm &&
+       git add arm .gitattributes &&
+       test_tick &&
+       git commit -m neutral &&
+       git branch right &&
+
+       echo left >arm &&
+       test_tick &&
+       git commit -a -m left &&
+       git checkout right &&
+
+       echo right >arm &&
+       test_tick &&
+       git commit -a -m right &&
+
+       test_must_fail git merge left &&
+       (
+               for t in filfre-common left right
+               do
+                       grep $t arm || exit 1
+               done
+               exit 0
+       ) &&
+
+       mv arm expect &&
+       git checkout -m arm &&
+       test_cmp expect arm
+'
+
 test_done