| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test local clone' |
| 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 6 | |
| 7 | . ./test-lib.sh |
| 8 | |
| 9 | repo_is_hardlinked() { |
| 10 | find "$1/objects" -type f -links 1 >output && |
| 11 | test_line_count = 0 output |
| 12 | } |
| 13 | |
| 14 | test_expect_success 'preparing origin repository' ' |
| 15 | : >file && git add . && git commit -m1 && |
| 16 | git clone --bare . a.git && |
| 17 | git clone --bare . x && |
| 18 | echo true >expect && |
| 19 | git -C a.git config --bool core.bare >actual && |
| 20 | test_cmp expect actual && |
| 21 | echo true >expect && |
| 22 | git -C x config --bool core.bare >actual && |
| 23 | test_cmp expect actual && |
| 24 | git bundle create b1.bundle --all && |
| 25 | git bundle create b2.bundle main && |
| 26 | mkdir dir && |
| 27 | cp b1.bundle dir/b3 && |
| 28 | cp b1.bundle b4 && |
| 29 | git branch not-main main && |
| 30 | git bundle create b5.bundle not-main |
| 31 | ' |
| 32 | |
| 33 | test_expect_success 'local clone without .git suffix' ' |
| 34 | git clone -l -s a b && |
| 35 | (cd b && |
| 36 | echo false >expect && |
| 37 | git config --bool core.bare >actual && |
| 38 | test_cmp expect actual && |
| 39 | git fetch) |
| 40 | ' |
| 41 | |
| 42 | test_expect_success 'local clone with .git suffix' ' |
| 43 | git clone -l -s a.git c && |
| 44 | (cd c && git fetch) |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'local clone from x' ' |
| 48 | git clone -l -s x y && |
| 49 | (cd y && git fetch) |
| 50 | ' |
| 51 | |
| 52 | test_expect_success 'local clone from x.git that does not exist' ' |
| 53 | test_must_fail git clone -l -s x.git z |
| 54 | ' |
| 55 | |
| 56 | test_expect_success 'With -no-hardlinks, local will make a copy' ' |
| 57 | git clone --bare --no-hardlinks x w && |
| 58 | ! repo_is_hardlinked w |
| 59 | ' |
| 60 | |
| 61 | test_expect_success 'Even without -l, local will make a hardlink' ' |
| 62 | rm -fr w && |
| 63 | git clone -l --bare x w && |
| 64 | repo_is_hardlinked w |
| 65 | ' |
| 66 | |
| 67 | test_expect_success 'local clone of repo with nonexistent ref in HEAD' ' |
| 68 | git -C a.git symbolic-ref HEAD refs/heads/nonexistent && |
| 69 | git clone a d && |
| 70 | (cd d && |
| 71 | git fetch && |
| 72 | test_ref_missing refs/remotes/origin/HEAD) |
| 73 | ' |
| 74 | |
| 75 | test_expect_success 'bundle clone without .bundle suffix' ' |
| 76 | git clone dir/b3 && |
| 77 | (cd b3 && git fetch) |
| 78 | ' |
| 79 | |
| 80 | test_expect_success 'bundle clone with .bundle suffix' ' |
| 81 | git clone b1.bundle && |
| 82 | (cd b1 && git fetch) |
| 83 | ' |
| 84 | |
| 85 | test_expect_success 'bundle clone from b4' ' |
| 86 | git clone b4 bdl && |
| 87 | (cd bdl && git fetch) |
| 88 | ' |
| 89 | |
| 90 | test_expect_success 'bundle clone from b4.bundle that does not exist' ' |
| 91 | test_must_fail git clone b4.bundle bb |
| 92 | ' |
| 93 | |
| 94 | test_expect_success 'bundle clone with nonexistent HEAD (match default)' ' |
| 95 | git clone b2.bundle b2 && |
| 96 | (cd b2 && |
| 97 | git fetch && |
| 98 | git rev-parse --verify refs/heads/main) |
| 99 | ' |
| 100 | |
| 101 | test_expect_success 'bundle clone with nonexistent HEAD (no match default)' ' |
| 102 | git clone b5.bundle b5 && |
| 103 | (cd b5 && |
| 104 | git fetch && |
| 105 | test_must_fail git rev-parse --verify refs/heads/main && |
| 106 | test_must_fail git rev-parse --verify refs/heads/not-main) |
| 107 | ' |
| 108 | |
| 109 | test_expect_success 'clone empty repository' ' |
| 110 | mkdir empty && |
| 111 | (cd empty && |
| 112 | git init && |
| 113 | git config receive.denyCurrentBranch warn) && |
| 114 | git clone empty empty-clone && |
| 115 | test_tick && |
| 116 | (cd empty-clone && |
| 117 | echo "content" >> foo && |
| 118 | git add foo && |
| 119 | git commit -m "Initial commit" && |
| 120 | git push origin main && |
| 121 | expected=$(git rev-parse main) && |
| 122 | actual=$(git --git-dir=../empty/.git rev-parse main) && |
| 123 | test $actual = $expected) |
| 124 | ' |
| 125 | |
| 126 | test_expect_success 'clone empty repository, and then push should not segfault.' ' |
| 127 | rm -fr empty/ empty-clone/ && |
| 128 | mkdir empty && |
| 129 | (cd empty && git init) && |
| 130 | git clone empty empty-clone && |
| 131 | (cd empty-clone && |
| 132 | test_must_fail git push) |
| 133 | ' |
| 134 | |
| 135 | test_expect_success 'cloning non-existent directory fails' ' |
| 136 | rm -rf does-not-exist && |
| 137 | test_must_fail git clone does-not-exist |
| 138 | ' |
| 139 | |
| 140 | test_expect_success 'cloning non-git directory fails' ' |
| 141 | rm -rf not-a-git-repo not-a-git-repo-clone && |
| 142 | mkdir not-a-git-repo && |
| 143 | test_must_fail git clone not-a-git-repo not-a-git-repo-clone |
| 144 | ' |
| 145 | |
| 146 | test_expect_success 'cloning file:// does not hardlink' ' |
| 147 | git clone --bare file://"$(pwd)"/a non-local && |
| 148 | ! repo_is_hardlinked non-local |
| 149 | ' |
| 150 | |
| 151 | test_expect_success 'cloning a local path with --no-local does not hardlink' ' |
| 152 | git clone --bare --no-local a force-nonlocal && |
| 153 | ! repo_is_hardlinked force-nonlocal |
| 154 | ' |
| 155 | |
| 156 | test_expect_success 'cloning a local path with --no-local from a different user succeeds' ' |
| 157 | git clone --upload-pack="GIT_TEST_ASSUME_DIFFERENT_OWNER=true git-upload-pack" \ |
| 158 | --no-local a nonlocal-otheruser 2>err && |
| 159 | ! repo_is_hardlinked nonlocal-otheruser/.git && |
| 160 | # Verify that this is a git repository. |
| 161 | git -C nonlocal-otheruser rev-parse --show-toplevel && |
| 162 | test_grep ! "detected dubious ownership" err |
| 163 | ' |
| 164 | |
| 165 | test_expect_success 'cloning locally respects "-u" for fetching refs' ' |
| 166 | test_must_fail git clone --bare -u false a should_not_work.git |
| 167 | ' |
| 168 | |
| 169 | test_expect_success REFFILES 'local clone from repo with corrupt refs fails gracefully' ' |
| 170 | git init corrupt && |
| 171 | test_commit -C corrupt one && |
| 172 | echo a >corrupt/.git/refs/heads/topic && |
| 173 | |
| 174 | test_must_fail git clone corrupt working 2>err && |
| 175 | test_grep "has neither a valid OID nor a target" err |
| 176 | ' |
| 177 | |
| 178 | test_done |