2024-05-28 19:43:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2019-10-26 12:09:50 +03:00
|
|
|
source common.sh
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
2023-03-16 22:00:20 +02:00
|
|
|
requireGit
|
2019-10-26 12:09:50 +03:00
|
|
|
|
2024-06-16 18:56:50 +03:00
|
|
|
clearStoreIfPossible
|
2019-10-26 12:09:50 +03:00
|
|
|
|
|
|
|
rootRepo=$TEST_ROOT/gitSubmodulesRoot
|
|
|
|
subRepo=$TEST_ROOT/gitSubmodulesSub
|
|
|
|
|
2020-04-07 14:45:17 +03:00
|
|
|
rm -rf ${rootRepo} ${subRepo} $TEST_HOME/.cache/nix
|
2019-10-26 12:09:50 +03:00
|
|
|
|
2022-12-02 21:24:05 +02:00
|
|
|
# Submodules can't be fetched locally by default, which can cause
|
|
|
|
# information leakage vulnerabilities, but for these tests our
|
|
|
|
# submodule is intentionally local and it's all trusted, so we
|
|
|
|
# disable this restriction. Setting it per repo is not sufficient, as
|
|
|
|
# the repo-local config does not apply to the commands run from
|
|
|
|
# outside the repos by Nix.
|
|
|
|
export XDG_CONFIG_HOME=$TEST_HOME/.config
|
|
|
|
git config --global protocol.file.allow always
|
|
|
|
|
2019-10-26 12:09:50 +03:00
|
|
|
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
|
|
|
|
addGitContent $subRepo
|
|
|
|
|
|
|
|
initGitRepo $rootRepo
|
|
|
|
|
|
|
|
git -C $rootRepo submodule init
|
|
|
|
git -C $rootRepo submodule add $subRepo sub
|
|
|
|
git -C $rootRepo add sub
|
|
|
|
git -C $rootRepo commit -m "Add submodule"
|
|
|
|
|
|
|
|
rev=$(git -C $rootRepo rev-parse HEAD)
|
|
|
|
|
2020-04-07 15:29:45 +03:00
|
|
|
r1=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; }).outPath")
|
|
|
|
r2=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; submodules = false; }).outPath")
|
|
|
|
r3=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; submodules = true; }).outPath")
|
2020-03-16 15:47:55 +02:00
|
|
|
|
2021-09-22 18:25:25 +03:00
|
|
|
[[ $r1 == $r2 ]]
|
|
|
|
[[ $r2 != $r3 ]]
|
2020-03-16 15:47:55 +02:00
|
|
|
|
2020-04-07 15:29:45 +03:00
|
|
|
r4=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; ref = \"master\"; rev = \"$rev\"; }).outPath")
|
|
|
|
r5=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; ref = \"master\"; rev = \"$rev\"; submodules = false; }).outPath")
|
|
|
|
r6=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; ref = \"master\"; rev = \"$rev\"; submodules = true; }).outPath")
|
|
|
|
r7=$(nix eval --raw --expr "(builtins.fetchGit { url = $rootRepo; ref = \"master\"; rev = \"$rev\"; submodules = true; }).outPath")
|
|
|
|
r8=$(nix eval --raw --expr "(builtins.fetchGit { url = $rootRepo; rev = \"$rev\"; submodules = true; }).outPath")
|
2020-03-16 15:47:55 +02:00
|
|
|
|
|
|
|
[[ $r1 == $r4 ]]
|
2021-09-22 18:25:25 +03:00
|
|
|
[[ $r4 == $r5 ]]
|
2020-03-16 15:47:55 +02:00
|
|
|
[[ $r3 == $r6 ]]
|
|
|
|
[[ $r6 == $r7 ]]
|
|
|
|
[[ $r7 == $r8 ]]
|
|
|
|
|
2020-04-07 15:29:45 +03:00
|
|
|
have_submodules=$(nix eval --expr "(builtins.fetchGit { url = $rootRepo; rev = \"$rev\"; }).submodules")
|
2021-09-22 18:25:25 +03:00
|
|
|
[[ $have_submodules == false ]]
|
2020-03-17 15:27:14 +02:00
|
|
|
|
2020-04-07 15:29:45 +03:00
|
|
|
have_submodules=$(nix eval --expr "(builtins.fetchGit { url = $rootRepo; rev = \"$rev\"; submodules = false; }).submodules")
|
2020-03-17 15:27:14 +02:00
|
|
|
[[ $have_submodules == false ]]
|
|
|
|
|
2020-04-07 15:29:45 +03:00
|
|
|
have_submodules=$(nix eval --expr "(builtins.fetchGit { url = $rootRepo; rev = \"$rev\"; submodules = true; }).submodules")
|
2020-03-17 15:27:14 +02:00
|
|
|
[[ $have_submodules == true ]]
|
|
|
|
|
2021-09-22 18:25:25 +03:00
|
|
|
pathWithoutSubmodules=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; }).outPath")
|
|
|
|
pathWithSubmodules=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; submodules = true; }).outPath")
|
2020-04-07 15:29:45 +03:00
|
|
|
pathWithSubmodulesAgain=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev\"; submodules = true; }).outPath")
|
|
|
|
pathWithSubmodulesAgainWithRef=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; ref = \"master\"; rev = \"$rev\"; submodules = true; }).outPath")
|
2020-03-17 21:14:19 +02:00
|
|
|
|
2019-10-26 12:09:50 +03:00
|
|
|
# The resulting store path cannot be the same.
|
|
|
|
[[ $pathWithoutSubmodules != $pathWithSubmodules ]]
|
|
|
|
|
2020-03-02 15:46:19 +02:00
|
|
|
# Checking out the same repo with submodules returns in the same store path.
|
|
|
|
[[ $pathWithSubmodules == $pathWithSubmodulesAgain ]]
|
|
|
|
|
2020-03-17 21:14:19 +02:00
|
|
|
# Checking out the same repo with submodules returns in the same store path.
|
|
|
|
[[ $pathWithSubmodulesAgain == $pathWithSubmodulesAgainWithRef ]]
|
|
|
|
|
2020-03-02 15:46:19 +02:00
|
|
|
# The submodules flag is actually honored.
|
2019-10-26 12:09:50 +03:00
|
|
|
[[ ! -e $pathWithoutSubmodules/sub/content ]]
|
|
|
|
[[ -e $pathWithSubmodules/sub/content ]]
|
|
|
|
|
2020-03-17 21:14:19 +02:00
|
|
|
[[ -e $pathWithSubmodulesAgainWithRef/sub/content ]]
|
|
|
|
|
2020-03-02 15:42:19 +02:00
|
|
|
# No .git directory or submodule reference files must be left
|
|
|
|
test "$(find "$pathWithSubmodules" -name .git)" = ""
|
2020-03-02 15:46:19 +02:00
|
|
|
|
|
|
|
# Git repos without submodules can be fetched with submodules = true.
|
2020-03-02 16:50:39 +02:00
|
|
|
subRev=$(git -C $subRepo rev-parse HEAD)
|
2020-04-07 15:29:45 +03:00
|
|
|
noSubmoduleRepoBaseline=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$subRepo; rev = \"$subRev\"; }).outPath")
|
|
|
|
noSubmoduleRepo=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$subRepo; rev = \"$subRev\"; submodules = true; }).outPath")
|
2020-03-02 15:46:19 +02:00
|
|
|
|
|
|
|
[[ $noSubmoduleRepoBaseline == $noSubmoduleRepo ]]
|
2023-02-07 23:46:25 +02:00
|
|
|
|
|
|
|
# Test relative submodule URLs.
|
|
|
|
rm $TEST_HOME/.cache/nix/fetcher-cache*
|
|
|
|
rm -rf $rootRepo/.git $rootRepo/.gitmodules $rootRepo/sub
|
|
|
|
initGitRepo $rootRepo
|
|
|
|
git -C $rootRepo submodule add ../gitSubmodulesSub sub
|
|
|
|
git -C $rootRepo commit -m "Add submodule"
|
|
|
|
rev2=$(git -C $rootRepo rev-parse HEAD)
|
|
|
|
pathWithRelative=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$rootRepo; rev = \"$rev2\"; submodules = true; }).outPath")
|
|
|
|
diff -r -x .gitmodules $pathWithSubmodules $pathWithRelative
|
|
|
|
|
|
|
|
# Test clones that have an upstream with relative submodule URLs.
|
|
|
|
rm $TEST_HOME/.cache/nix/fetcher-cache*
|
|
|
|
cloneRepo=$TEST_ROOT/a/b/gitSubmodulesClone # NB /a/b to make the relative path not work relative to $cloneRepo
|
|
|
|
git clone $rootRepo $cloneRepo
|
|
|
|
pathIndirect=$(nix eval --raw --expr "(builtins.fetchGit { url = file://$cloneRepo; rev = \"$rev2\"; submodules = true; }).outPath")
|
|
|
|
[[ $pathIndirect = $pathWithRelative ]]
|
2023-12-11 23:36:08 +02:00
|
|
|
|
|
|
|
# Test submodule export-ignore interaction
|
|
|
|
git -C $rootRepo/sub config user.email "foobar@example.com"
|
|
|
|
git -C $rootRepo/sub config user.name "Foobar"
|
|
|
|
|
|
|
|
echo "/exclude-from-root export-ignore" >> $rootRepo/.gitattributes
|
2024-01-05 20:49:39 +02:00
|
|
|
# TBD possible semantics for submodules + exportIgnore
|
|
|
|
# echo "/sub/exclude-deep export-ignore" >> $rootRepo/.gitattributes
|
2023-12-11 23:36:08 +02:00
|
|
|
echo nope > $rootRepo/exclude-from-root
|
|
|
|
git -C $rootRepo add .gitattributes exclude-from-root
|
|
|
|
git -C $rootRepo commit -m "Add export-ignore"
|
|
|
|
|
|
|
|
echo "/exclude-from-sub export-ignore" >> $rootRepo/sub/.gitattributes
|
|
|
|
echo nope > $rootRepo/sub/exclude-from-sub
|
2024-01-05 20:49:39 +02:00
|
|
|
# TBD possible semantics for submodules + exportIgnore
|
|
|
|
# echo aye > $rootRepo/sub/exclude-from-root
|
2023-12-11 23:36:08 +02:00
|
|
|
git -C $rootRepo/sub add .gitattributes exclude-from-sub
|
|
|
|
git -C $rootRepo/sub commit -m "Add export-ignore (sub)"
|
|
|
|
|
|
|
|
git -C $rootRepo add sub
|
|
|
|
git -C $rootRepo commit -m "Update submodule"
|
|
|
|
|
|
|
|
git -C $rootRepo status
|
|
|
|
|
2024-01-05 20:49:39 +02:00
|
|
|
# # TBD: not supported yet, because semantics are undecided and current implementation leaks rules from the root to submodules
|
|
|
|
# # exportIgnore can be used with submodules
|
|
|
|
# pathWithExportIgnore=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = file://$rootRepo; submodules = true; exportIgnore = true; }).outPath")
|
|
|
|
# # find $pathWithExportIgnore
|
|
|
|
# # git -C $rootRepo archive --format=tar HEAD | tar -t
|
|
|
|
# # cp -a $rootRepo /tmp/rootRepo
|
|
|
|
|
|
|
|
# [[ -e $pathWithExportIgnore/sub/content ]]
|
|
|
|
# [[ ! -e $pathWithExportIgnore/exclude-from-root ]]
|
|
|
|
# [[ ! -e $pathWithExportIgnore/sub/exclude-from-sub ]]
|
|
|
|
# TBD possible semantics for submodules + exportIgnore
|
|
|
|
# # root .gitattribute has no power across submodule boundary
|
|
|
|
# [[ -e $pathWithExportIgnore/sub/exclude-from-root ]]
|
|
|
|
# [[ -e $pathWithExportIgnore/sub/exclude-deep ]]
|
2023-12-11 23:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
# exportIgnore can be explicitly disabled with submodules
|
|
|
|
pathWithoutExportIgnore=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = file://$rootRepo; submodules = true; exportIgnore = false; }).outPath")
|
|
|
|
# find $pathWithoutExportIgnore
|
|
|
|
|
|
|
|
[[ -e $pathWithoutExportIgnore/exclude-from-root ]]
|
|
|
|
[[ -e $pathWithoutExportIgnore/sub/exclude-from-sub ]]
|
|
|
|
|
|
|
|
# exportIgnore defaults to false when submodules = true
|
|
|
|
pathWithSubmodules=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = file://$rootRepo; submodules = true; }).outPath")
|
|
|
|
|
|
|
|
[[ -e $pathWithoutExportIgnore/exclude-from-root ]]
|
|
|
|
[[ -e $pathWithoutExportIgnore/sub/exclude-from-sub ]]
|
2024-04-22 17:41:40 +03:00
|
|
|
|
|
|
|
test_submodule_nested() {
|
|
|
|
local repoA=$TEST_ROOT/submodule_nested/a
|
|
|
|
local repoB=$TEST_ROOT/submodule_nested/b
|
|
|
|
local repoC=$TEST_ROOT/submodule_nested/c
|
|
|
|
|
|
|
|
rm -rf $repoA $repoB $repoC $TEST_HOME/.cache/nix
|
|
|
|
|
|
|
|
initGitRepo $repoC
|
|
|
|
touch $repoC/inside-c
|
|
|
|
git -C $repoC add inside-c
|
|
|
|
addGitContent $repoC
|
|
|
|
|
|
|
|
initGitRepo $repoB
|
|
|
|
git -C $repoB submodule add $repoC c
|
|
|
|
git -C $repoB add c
|
|
|
|
addGitContent $repoB
|
|
|
|
|
|
|
|
initGitRepo $repoA
|
|
|
|
git -C $repoA submodule add $repoB b
|
|
|
|
git -C $repoA add b
|
|
|
|
addGitContent $repoA
|
|
|
|
|
|
|
|
|
|
|
|
# Check non-worktree fetch
|
|
|
|
local rev=$(git -C $repoA rev-parse HEAD)
|
|
|
|
out=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = \"file://$repoA\"; rev = \"$rev\"; submodules = true; }).outPath")
|
|
|
|
test -e $out/b/c/inside-c
|
|
|
|
test -e $out/content
|
|
|
|
test -e $out/b/content
|
|
|
|
test -e $out/b/c/content
|
|
|
|
local nonWorktree=$out
|
|
|
|
|
|
|
|
# Check worktree based fetch
|
|
|
|
# TODO: make it work without git submodule update
|
|
|
|
git -C $repoA submodule update --init --recursive
|
|
|
|
out=$(nix eval --impure --raw --expr "(builtins.fetchGit { url = \"file://$repoA\"; submodules = true; }).outPath")
|
|
|
|
find $out
|
|
|
|
[[ $out == $nonWorktree ]] || { find $out; false; }
|
|
|
|
|
|
|
|
}
|
|
|
|
test_submodule_nested
|