| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='verify safe.bareRepository checks' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | pwd="$(pwd)" |
| 8 | |
| 9 | expect_accepted_implicit () { |
| 10 | test_when_finished 'rm "$pwd/trace.perf"' && |
| 11 | GIT_TRACE2_PERF="$pwd/trace.perf" git "$@" rev-parse --git-dir && |
| 12 | # Note: we're intentionally only checking that the bare repo has a |
| 13 | # directory *prefix* of $pwd |
| 14 | grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf" |
| 15 | } |
| 16 | |
| 17 | expect_accepted_explicit () { |
| 18 | test_when_finished 'rm "$pwd/trace.perf"' && |
| 19 | GIT_DIR="$1" GIT_TRACE2_PERF="$pwd/trace.perf" git rev-parse --git-dir && |
| 20 | ! grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf" |
| 21 | } |
| 22 | |
| 23 | expect_rejected () { |
| 24 | test_when_finished 'rm "$pwd/trace.perf"' && |
| 25 | test_env GIT_TRACE2_PERF="$pwd/trace.perf" \ |
| 26 | test_must_fail git "$@" rev-parse --git-dir 2>err && |
| 27 | grep -F "cannot use bare repository" err && |
| 28 | grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf" |
| 29 | } |
| 30 | |
| 31 | test_expect_success 'setup an embedded bare repo, secondary worktree and submodule' ' |
| 32 | git init outer-repo && |
| 33 | git init --bare --initial-branch=main outer-repo/bare-repo && |
| 34 | git -C outer-repo worktree add ../outer-secondary && |
| 35 | test_path_is_dir outer-secondary && |
| 36 | ( |
| 37 | cd outer-repo && |
| 38 | test_commit A && |
| 39 | git push bare-repo +HEAD:refs/heads/main && |
| 40 | git -c protocol.file.allow=always \ |
| 41 | submodule add --name subn -- ./bare-repo subd |
| 42 | ) && |
| 43 | test_path_is_dir outer-repo/.git/worktrees/outer-secondary && |
| 44 | test_path_is_dir outer-repo/.git/modules/subn |
| 45 | ' |
| 46 | |
| 47 | test_expect_success !WITH_BREAKING_CHANGES 'safe.bareRepository unset' ' |
| 48 | test_unconfig --global safe.bareRepository && |
| 49 | expect_accepted_implicit -C outer-repo/bare-repo |
| 50 | ' |
| 51 | |
| 52 | test_expect_success WITH_BREAKING_CHANGES 'safe.bareRepository unset (defaults to explicit)' ' |
| 53 | test_unconfig --global safe.bareRepository && |
| 54 | expect_rejected -C outer-repo/bare-repo |
| 55 | ' |
| 56 | |
| 57 | test_expect_success 'safe.bareRepository=all' ' |
| 58 | test_config_global safe.bareRepository all && |
| 59 | expect_accepted_implicit -C outer-repo/bare-repo |
| 60 | ' |
| 61 | |
| 62 | test_expect_success 'safe.bareRepository=explicit' ' |
| 63 | test_config_global safe.bareRepository explicit && |
| 64 | expect_rejected -C outer-repo/bare-repo |
| 65 | ' |
| 66 | |
| 67 | test_expect_success 'safe.bareRepository in the repository' ' |
| 68 | # safe.bareRepository must not be "explicit", otherwise |
| 69 | # git config fails with "fatal: not in a git directory" (like |
| 70 | # safe.directory) |
| 71 | test_when_finished "git config --file outer-repo/bare-repo/config --unset safe.bareRepository" && |
| 72 | git config --file outer-repo/bare-repo/config safe.bareRepository all && |
| 73 | test_config_global safe.bareRepository explicit && |
| 74 | expect_rejected -C outer-repo/bare-repo |
| 75 | ' |
| 76 | |
| 77 | test_expect_success 'safe.bareRepository on the command line' ' |
| 78 | test_config_global safe.bareRepository explicit && |
| 79 | expect_accepted_implicit -C outer-repo/bare-repo \ |
| 80 | -c safe.bareRepository=all |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'safe.bareRepository in included file' ' |
| 84 | cat >gitconfig-include <<-\EOF && |
| 85 | [safe] |
| 86 | bareRepository = explicit |
| 87 | EOF |
| 88 | git config --global --add include.path "$(pwd)/gitconfig-include" && |
| 89 | expect_rejected -C outer-repo/bare-repo |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'no trace when GIT_DIR is explicitly provided' ' |
| 93 | expect_accepted_explicit "$pwd/outer-repo/bare-repo" |
| 94 | ' |
| 95 | |
| 96 | test_expect_success 'no trace when "bare repository" is .git' ' |
| 97 | expect_accepted_implicit -C outer-repo/.git |
| 98 | ' |
| 99 | |
| 100 | test_expect_success 'no trace when "bare repository" is a subdir of .git' ' |
| 101 | expect_accepted_implicit -C outer-repo/.git/objects |
| 102 | ' |
| 103 | |
| 104 | test_expect_success 'no trace in $GIT_DIR of secondary worktree' ' |
| 105 | expect_accepted_implicit -C outer-repo/.git/worktrees/outer-secondary |
| 106 | ' |
| 107 | |
| 108 | test_expect_success 'no trace in $GIT_DIR of a submodule' ' |
| 109 | expect_accepted_implicit -C outer-repo/.git/modules/subn |
| 110 | ' |
| 111 | |
| 112 | test_done |