diff --git a/tests/functional/common/functions.sh b/tests/functional/common/functions.sh index 1b2ec8fe0e8..771bbca785b 100644 --- a/tests/functional/common/functions.sh +++ b/tests/functional/common/functions.sh @@ -360,4 +360,25 @@ execUnshare () { exec unshare --mount --map-root-user "$SHELL" "$@" } +initGitRepo() { + local repo="$1" + local extraArgs="${2-}" + + # shellcheck disable=SC2086 # word splitting of extraArgs is intended + git -C "$repo" init $extraArgs + git -C "$repo" config user.email "foobar@example.com" + git -C "$repo" config user.name "Foobar" +} + +createGitRepo() { + local repo="$1" + local extraArgs="${2-}" + + rm -rf "$repo" "$repo".tmp + mkdir -p "$repo" + + # shellcheck disable=SC2086 # word splitting of extraArgs is intended + initGitRepo "$repo" $extraArgs +} + fi # COMMON_FUNCTIONS_SH_SOURCED diff --git a/tests/functional/fetchGit.sh b/tests/functional/fetchGit.sh index e3135328f40..da04cf852ce 100755 --- a/tests/functional/fetchGit.sh +++ b/tests/functional/fetchGit.sh @@ -12,11 +12,9 @@ repo=$TEST_ROOT/./git export _NIX_FORCE_HTTP=1 -rm -rf "$repo" "${repo}"-tmp "$TEST_HOME"/.cache/nix "$TEST_ROOT"/worktree "$TEST_ROOT"/minimal +rm -rf "${repo}"-tmp "$TEST_HOME"/.cache/nix "$TEST_ROOT"/worktree "$TEST_ROOT"/minimal -git init "$repo" -git -C "$repo" config user.email "foobar@example.com" -git -C "$repo" config user.name "Foobar" +createGitRepo "$repo" echo utrecht > "$repo"/hello touch "$repo"/.gitignore @@ -213,8 +211,7 @@ path5=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = $repo; ref = # Fetching from a repo with only a specific revision and no branches should # not fall back to copying files and record correct revision information. See: #5302 -mkdir "$TEST_ROOT"/minimal -git -C "$TEST_ROOT"/minimal init +createGitRepo "$TEST_ROOT"/minimal git -C "$TEST_ROOT"/minimal fetch "$repo" "$rev2" git -C "$TEST_ROOT"/minimal checkout "$rev2" [[ $(nix eval --impure --raw --expr "(builtins.fetchGit { url = $TEST_ROOT/minimal; }).rev") = "$rev2" ]] @@ -267,7 +264,7 @@ rm -rf "$TEST_HOME"/.cache/nix (! nix eval --impure --raw --expr "(builtins.fetchGit \"file://$repo\").outPath") # should succeed for a repo without commits -git init "$repo" +initGitRepo "$repo" git -C "$repo" add hello # need to add at least one file to cause the root of the repo to be visible # shellcheck disable=SC2034 path10=$(nix eval --impure --raw --expr "(builtins.fetchGit \"file://$repo\").outPath") @@ -275,9 +272,7 @@ path10=$(nix eval --impure --raw --expr "(builtins.fetchGit \"file://$repo\").ou # should succeed for a path with a space # regression test for #7707 repo="$TEST_ROOT/a b" -git init "$repo" -git -C "$repo" config user.email "foobar@example.com" -git -C "$repo" config user.name "Foobar" +createGitRepo "$repo" echo utrecht > "$repo/hello" touch "$repo/.gitignore" @@ -289,7 +284,7 @@ path11=$(nix eval --impure --raw --expr "(builtins.fetchGit ./.).outPath") # Test a workdir with no commits. empty="$TEST_ROOT/empty" -git init "$empty" +createGitRepo "$empty" emptyAttrs="{ lastModified = 0; lastModifiedDate = \"19700101000000\"; narHash = \"sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo=\"; rev = \"0000000000000000000000000000000000000000\"; revCount = 0; shortRev = \"0000000\"; submodules = false; }" result=$(nix eval --impure --expr "builtins.removeAttrs (builtins.fetchGit $empty) [\"outPath\"]") diff --git a/tests/functional/fetchGitRefs.sh b/tests/functional/fetchGitRefs.sh index a7d1a2a2931..9c7fb323eb7 100755 --- a/tests/functional/fetchGitRefs.sh +++ b/tests/functional/fetchGitRefs.sh @@ -8,11 +8,9 @@ clearStoreIfPossible repo="$TEST_ROOT/git" -rm -rf "$repo" "${repo}-tmp" "$TEST_HOME/.cache/nix" +rm -rf "${repo}-tmp" "$TEST_HOME/.cache/nix" -git init "$repo" -git -C "$repo" config user.email "foobar@example.com" -git -C "$repo" config user.name "Foobar" +createGitRepo "$repo" echo utrecht > "$repo/hello" git -C "$repo" add hello diff --git a/tests/functional/fetchGitShallow.sh b/tests/functional/fetchGitShallow.sh index 4c21bd7ac80..6b91d60cd9e 100644 --- a/tests/functional/fetchGitShallow.sh +++ b/tests/functional/fetchGitShallow.sh @@ -6,9 +6,7 @@ source common.sh requireGit # Create a test repo with multiple commits for all our tests -git init "$TEST_ROOT/shallow-parent" -git -C "$TEST_ROOT/shallow-parent" config user.email "foobar@example.com" -git -C "$TEST_ROOT/shallow-parent" config user.name "Foobar" +createGitRepo "$TEST_ROOT/shallow-parent" # Add several commits to have history echo "{ outputs = _: {}; }" > "$TEST_ROOT/shallow-parent/flake.nix" diff --git a/tests/functional/fetchGitSubmodules.sh b/tests/functional/fetchGitSubmodules.sh index 2a25245be75..bf5fe5df387 100755 --- a/tests/functional/fetchGitSubmodules.sh +++ b/tests/functional/fetchGitSubmodules.sh @@ -22,22 +22,16 @@ rm -rf "${rootRepo}" "${subRepo}" "$TEST_HOME"/.cache/nix export XDG_CONFIG_HOME=$TEST_HOME/.config git config --global protocol.file.allow always -initGitRepo() { - git init "$1" - git -C "$1" config user.email "foobar@example.com" - git -C "$1" config user.name "Foobar" -} - addGitContent() { echo "lorem ipsum" > "$1"/content git -C "$1" add content git -C "$1" commit -m "Initial commit" } -initGitRepo "$subRepo" +createGitRepo "$subRepo" addGitContent "$subRepo" -initGitRepo "$rootRepo" +createGitRepo "$rootRepo" git -C "$rootRepo" submodule init git -C "$rootRepo" submodule add "$subRepo" sub @@ -199,19 +193,19 @@ test_submodule_nested() { local repoB=$TEST_ROOT/submodule_nested/b local repoC=$TEST_ROOT/submodule_nested/c - rm -rf "$repoA" "$repoB" "$repoC" "$TEST_HOME"/.cache/nix + rm -rf "$TEST_HOME"/.cache/nix - initGitRepo "$repoC" + createGitRepo "$repoC" touch "$repoC"/inside-c git -C "$repoC" add inside-c addGitContent "$repoC" - initGitRepo "$repoB" + createGitRepo "$repoB" git -C "$repoB" submodule add "$repoC" c git -C "$repoB" add c addGitContent "$repoB" - initGitRepo "$repoA" + createGitRepo "$repoA" git -C "$repoA" submodule add "$repoB" b git -C "$repoA" add b addGitContent "$repoA" diff --git a/tests/functional/fetchGitVerification.sh b/tests/functional/fetchGitVerification.sh index 79c78d0c9f6..3b5f9b866b9 100755 --- a/tests/functional/fetchGitVerification.sh +++ b/tests/functional/fetchGitVerification.sh @@ -21,9 +21,7 @@ ssh-keygen -f "$keysDir/testkey2" -t rsa -P "" -C "test key 2" key2File="$keysDir/testkey2.pub" publicKey2=$(awk '{print $2}' "$key2File") -git init "$repo" -git -C "$repo" config user.email "foobar@example.com" -git -C "$repo" config user.name "Foobar" +createGitRepo "$repo" git -C "$repo" config gpg.format ssh echo 'hello' > "$repo"/text diff --git a/tests/functional/flakes/common.sh b/tests/functional/flakes/common.sh index 77bc030605f..2dcf2e0fd54 100644 --- a/tests/functional/flakes/common.sh +++ b/tests/functional/flakes/common.sh @@ -113,24 +113,3 @@ writeTrivialFlake() { } EOF } - -initGitRepo() { - local repo="$1" - local extraArgs="${2-}" - - # shellcheck disable=SC2086 # word splitting of extraArgs is intended - git -C "$repo" init $extraArgs - git -C "$repo" config user.email "foobar@example.com" - git -C "$repo" config user.name "Foobar" -} - -createGitRepo() { - local repo="$1" - local extraArgs="${2-}" - - rm -rf "$repo" "$repo".tmp - mkdir -p "$repo" - - # shellcheck disable=SC2086 # word splitting of extraArgs is intended - initGitRepo "$repo" $extraArgs -} diff --git a/tests/functional/flakes/relative-paths.sh b/tests/functional/flakes/relative-paths.sh index 7480cd50458..323e97ba9cc 100644 --- a/tests/functional/flakes/relative-paths.sh +++ b/tests/functional/flakes/relative-paths.sh @@ -135,10 +135,8 @@ EOF # https://github.com/NixOS/nix/issues/13164 mkdir -p "$TEST_ROOT/issue-13164/nested-flake1/nested-flake2" ( + initGitRepo "$TEST_ROOT/issue-13164" cd "$TEST_ROOT/issue-13164" - git init - git config --global user.email "you@example.com" - git config --global user.name "Your Name" cat >flake.nix <