Skip to content

Commit 90bb354

Browse files
committed
Use git archive
1 parent ec9252d commit 90bb354

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/libfetchers/git.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,30 @@ struct GitInputScheme : InputScheme
647647
return options.makeFingerprint(rev) + (getSubmodulesAttr(input) ? ";s" : "");
648648
}
649649

650+
/**
651+
* Get a `SourceAccessor` for the given Git revision by creating a git archive and unpacking it to the Nix store.
652+
* This is used for Nix < 2.20 compatibility.
653+
*/
654+
ref<SourceAccessor> getGitArchiveAccessor(
655+
Store & store, RepoInfo & repoInfo, const std::filesystem::path & repoDir, const Hash & rev) const
656+
{
657+
auto tmpDir = createTempDir();
658+
AutoDelete delTmpDir(tmpDir, true);
659+
660+
auto source = sinkToSource([&](Sink & sink) {
661+
runProgram2(
662+
{.program = "git",
663+
.args = {"-C", repoDir, "--git-dir", repoInfo.gitDir, "archive", rev.gitRev()},
664+
.standardOut = &sink});
665+
});
666+
667+
unpackTarfile(*source, tmpDir);
668+
669+
auto storePath = store.addToStore("source", {getFSSourceAccessor(), CanonPath(tmpDir)});
670+
671+
return ref{store.getFSAccessor(storePath)};
672+
}
673+
650674
std::pair<ref<SourceAccessor>, Input>
651675
getAccessorFromCommit(const Settings & settings, ref<Store> store, RepoInfo & repoInfo, Input && input) const
652676
{
@@ -804,7 +828,8 @@ struct GitInputScheme : InputScheme
804828
fetchToStore2(settings, *store, {accessor}, FetchMode::DryRun, input.getName()).second;
805829
if (expectedNarHash != narHashNew) {
806830
GitAccessorOptions options2{.exportIgnore = true, .applyFilters = true};
807-
auto accessor2 = repo->getAccessor(rev, options2, "«" + input.to_string(true) + "»");
831+
auto accessor2 = getGitArchiveAccessor(*store, repoInfo, repoDir, rev);
832+
accessor2->fingerprint = options2.makeFingerprint(rev);
808833
auto narHashOld =
809834
fetchToStore2(settings, *store, {accessor2}, FetchMode::DryRun, input.getName()).second;
810835
if (expectedNarHash == narHashOld) {

tests/functional/fetchGit.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,24 @@ printf "Hello\nWorld\n" > "$eol/crlf"
321321
printf "ignore me" > "$eol/ignored"
322322
git -C "$eol" add crlf ignored
323323
git -C "$eol" commit -a -m Initial
324-
printf "crlf text eol=crlf\nignored export-ignore\n" > "$eol/.gitattributes"
325-
git -C "$eol" add .gitattributes
324+
echo "Version: \$Format:%s\$" > "$eol/version"
325+
printf "crlf text eol=crlf\nignored export-ignore\nversion export-subst\n" > "$eol/.gitattributes"
326+
git -C "$eol" add .gitattributes version
326327
git -C "$eol" commit -a -m 'Apply gitattributes'
327328

328329
rev="$(git -C "$eol" rev-parse HEAD)"
329330

330331
export _NIX_TEST_BARF_ON_UNCACHEABLE=1
331332

332-
oldHash="sha256-OizkFa+lIZbi+DCfbH8AYzsJ+BRmW0u/a7KKuzXADEU="
333-
newHash="sha256-hKHmbBsoVwqPNkL0MXL0RAtbOCqK6tP4xpX2tKR6GPI="
333+
oldHash="sha256-cOuYSqDjvOBmKCuH5nXEfHRIAUVJZlictW0raF+3ynk="
334+
newHash="sha256-WZ5VePvmUcbRbkWLlNtCywWrAcr7EvVeJP8xKdZR7pc="
334335

335336
expectStderr 0 nix eval --expr \
336-
"let tree = builtins.fetchTree { type = \"git\"; url = \"file://$eol\"; rev = \"$rev\"; narHash = \"$oldHash\"; }; in assert builtins.readFile \"\${tree}/crlf\" == \"Hello\r\nWorld\r\n\"; assert !builtins.pathExists \"\${tree}/ignored\"; true" \
337+
"let tree = builtins.fetchTree { type = \"git\"; url = \"file://$eol\"; rev = \"$rev\"; narHash = \"$oldHash\"; }; in assert builtins.readFile \"\${tree}/crlf\" == \"Hello\r\nWorld\r\n\"; assert !builtins.pathExists \"\${tree}/ignored\"; assert builtins.readFile \"\${tree}/version\" == \"Version: Apply gitattributes\n\"; true" \
337338
| grepQuiet "Please update the NAR hash to '$newHash'"
338339

339340
nix eval --expr \
340-
"let tree = builtins.fetchTree { type = \"git\"; url = \"file://$eol\"; rev = \"$rev\"; narHash = \"$newHash\"; }; in assert builtins.readFile \"\${tree}/crlf\" == \"Hello\nWorld\n\"; assert builtins.pathExists \"\${tree}/ignored\"; true"
341+
"let tree = builtins.fetchTree { type = \"git\"; url = \"file://$eol\"; rev = \"$rev\"; narHash = \"$newHash\"; }; in assert builtins.readFile \"\${tree}/crlf\" == \"Hello\nWorld\n\"; assert builtins.pathExists \"\${tree}/ignored\"; assert builtins.readFile \"\${tree}/version\" == \"Version: \$Format:%s\$\n\"; true"
341342

342343
expectStderr 102 nix eval --expr \
343344
"builtins.fetchTree { type = \"git\"; url = \"file://$eol\"; rev = \"$rev\"; narHash = \"sha256-DLDvcwdcwCxnuPTxSQ6gLAyopB20lD0bOQoQB3i2hsA=\"; }" \

0 commit comments

Comments
 (0)