Skip to content

Commit 3aae35d

Browse files
committed
Introduce GitAccessorOptions
1 parent c1ab3bb commit 3aae35d

File tree

6 files changed

+42
-40
lines changed

6 files changed

+42
-40
lines changed

src/libfetchers-tests/git-utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ TEST_F(GitUtilsTest, sink_basic)
9292
// sink->createHardlink("foo-1.1/links/foo-2", CanonPath("foo-1.1/hello"));
9393

9494
auto result = repo->dereferenceSingletonDirectory(sink->flush());
95-
auto accessor = repo->getAccessor(result, false, getRepoName());
95+
auto accessor = repo->getAccessor(result, {}, getRepoName());
9696
auto entries = accessor->readDirectory(CanonPath::root);
9797
ASSERT_EQ(entries.size(), 5u);
9898
ASSERT_EQ(accessor->readFile(CanonPath("hello")), "hello world");

src/libfetchers/git-utils.cc

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,15 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
550550
}
551551

552552
/**
553-
* A 'GitSourceAccessor' with no regard for export-ignore or any other transformations.
553+
* A 'GitSourceAccessor' with no regard for export-ignore.
554554
*/
555-
ref<GitSourceAccessor> getRawAccessor(const Hash & rev, bool smudgeLfs = false, bool applyFilters = false);
555+
ref<GitSourceAccessor> getRawAccessor(const Hash & rev, const GitAccessorOptions & options);
556556

557-
ref<SourceAccessor> getAccessor(
558-
const Hash & rev,
559-
bool exportIgnore,
560-
std::string displayPrefix,
561-
bool smudgeLfs = false,
562-
bool applyFilters = false) override;
557+
ref<SourceAccessor>
558+
getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix) override;
563559

564-
ref<SourceAccessor> getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError e) override;
560+
ref<SourceAccessor>
561+
getAccessor(const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError e) override;
565562

566563
ref<GitFileSystemObjectSink> getFileSystemObjectSink() override;
567564

@@ -704,7 +701,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
704701

705702
Hash treeHashToNarHash(const fetchers::Settings & settings, const Hash & treeHash) override
706703
{
707-
auto accessor = getAccessor(treeHash, false, "");
704+
auto accessor = getAccessor(treeHash, {}, "");
708705

709706
fetchers::Cache::Key cacheKey{"treeHashToNarHash", {{"treeHash", treeHash.gitRev()}}};
710707

@@ -753,18 +750,18 @@ struct GitSourceAccessor : SourceAccessor
753750
git_oid oid;
754751
Object root;
755752
std::optional<lfs::Fetch> lfsFetch = std::nullopt;
756-
bool applyFilters;
753+
GitAccessorOptions options;
757754
};
758755

759756
Sync<State> state_;
760757

761-
GitSourceAccessor(ref<GitRepoImpl> repo_, const Hash & rev, bool smudgeLfs, bool applyFilters_)
758+
GitSourceAccessor(ref<GitRepoImpl> repo_, const Hash & rev, const GitAccessorOptions & options)
762759
: state_{State{
763760
.repo = repo_,
764761
.oid = hashToOID(rev),
765762
.root = peelToTreeOrBlob(lookupObject(*repo_, hashToOID(rev)).get()),
766-
.lfsFetch = smudgeLfs ? std::make_optional(lfs::Fetch(*repo_, hashToOID(rev))) : std::nullopt,
767-
.applyFilters = applyFilters_,
763+
.lfsFetch = options.smudgeLfs ? std::make_optional(lfs::Fetch(*repo_, hashToOID(rev))) : std::nullopt,
764+
.options = options,
768765
}}
769766
{
770767
}
@@ -792,7 +789,7 @@ struct GitSourceAccessor : SourceAccessor
792789
}
793790
}
794791

795-
if (!state->applyFilters)
792+
if (!state->options.applyFilters)
796793
return std::string((const char *) git_blob_rawcontent(blob.get()), git_blob_rawsize(blob.get()));
797794
else {
798795
// Apply git filters including potential CRLF conversion
@@ -1336,26 +1333,26 @@ struct GitFileSystemObjectSinkImpl : GitFileSystemObjectSink
13361333
}
13371334
};
13381335

1339-
ref<GitSourceAccessor> GitRepoImpl::getRawAccessor(const Hash & rev, bool smudgeLfs, bool applyFilters)
1336+
ref<GitSourceAccessor> GitRepoImpl::getRawAccessor(const Hash & rev, const GitAccessorOptions & options)
13401337
{
13411338
auto self = ref<GitRepoImpl>(shared_from_this());
1342-
return make_ref<GitSourceAccessor>(self, rev, smudgeLfs, applyFilters);
1339+
return make_ref<GitSourceAccessor>(self, rev, options);
13431340
}
13441341

1345-
ref<SourceAccessor> GitRepoImpl::getAccessor(
1346-
const Hash & rev, bool exportIgnore, std::string displayPrefix, bool smudgeLfs, bool applyFilters)
1342+
ref<SourceAccessor>
1343+
GitRepoImpl::getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix)
13471344
{
13481345
auto self = ref<GitRepoImpl>(shared_from_this());
1349-
ref<GitSourceAccessor> rawGitAccessor = getRawAccessor(rev, smudgeLfs, applyFilters);
1346+
ref<GitSourceAccessor> rawGitAccessor = getRawAccessor(rev, options);
13501347
rawGitAccessor->setPathDisplay(std::move(displayPrefix));
1351-
if (exportIgnore)
1348+
if (options.exportIgnore)
13521349
return make_ref<GitExportIgnoreSourceAccessor>(self, rawGitAccessor, rev);
13531350
else
13541351
return rawGitAccessor;
13551352
}
13561353

1357-
ref<SourceAccessor>
1358-
GitRepoImpl::getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError makeNotAllowedError)
1354+
ref<SourceAccessor> GitRepoImpl::getAccessor(
1355+
const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError makeNotAllowedError)
13591356
{
13601357
auto self = ref<GitRepoImpl>(shared_from_this());
13611358
ref<SourceAccessor> fileAccessor = AllowListSourceAccessor::create(
@@ -1365,7 +1362,7 @@ GitRepoImpl::getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllow
13651362
boost::unordered_flat_set<CanonPath>{CanonPath::root},
13661363
std::move(makeNotAllowedError))
13671364
.cast<SourceAccessor>();
1368-
if (exportIgnore)
1365+
if (options.exportIgnore)
13691366
fileAccessor = make_ref<GitExportIgnoreSourceAccessor>(self, fileAccessor, std::nullopt);
13701367
return fileAccessor;
13711368
}
@@ -1380,7 +1377,7 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
13801377
/* Read the .gitmodules files from this revision. */
13811378
CanonPath modulesFile(".gitmodules");
13821379

1383-
auto accessor = getAccessor(rev, exportIgnore, "");
1380+
auto accessor = getAccessor(rev, {.exportIgnore = exportIgnore}, "");
13841381
if (!accessor->pathExists(modulesFile))
13851382
return {};
13861383

@@ -1397,7 +1394,7 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
13971394

13981395
std::vector<std::tuple<Submodule, Hash>> result;
13991396

1400-
auto rawAccessor = getRawAccessor(rev);
1397+
auto rawAccessor = getRawAccessor(rev, {});
14011398

14021399
for (auto & submodule : parseSubmodules(pathTemp)) {
14031400
/* Filter out .gitmodules entries that don't exist or are not

src/libfetchers/git.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ struct GitInputScheme : InputScheme
788788

789789
bool exportIgnore = getExportIgnoreAttr(input);
790790
bool smudgeLfs = getLfsAttr(input);
791-
auto accessor = repo->getAccessor(rev, exportIgnore, "«" + input.to_string(true) + "»", smudgeLfs);
791+
auto accessor = repo->getAccessor(
792+
rev, {.exportIgnore = exportIgnore, .smudgeLfs = smudgeLfs}, "«" + input.to_string(true) + "»");
792793

793794
/* Backward compatibility hack for locks produced by Nix < 2.20 that depend on Nix applying Git filters or
794795
* `export-ignore`. Nix >= 2.20 doesn't do those, so we may get a NAR hash mismatch. If that happens, try again
@@ -799,7 +800,8 @@ struct GitInputScheme : InputScheme
799800
auto narHashNew =
800801
fetchToStore2(settings, *store, {accessor}, FetchMode::DryRun, input.getName()).second;
801802
if (expectedNarHash != narHashNew) {
802-
auto accessor2 = repo->getAccessor(rev, true, "«" + input.to_string(true) + "»", smudgeLfs, true);
803+
auto accessor2 = repo->getAccessor(
804+
rev, {.exportIgnore = true, .applyFilters = true}, "«" + input.to_string(true) + "»");
803805
accessor2->fingerprint = makeFingerprint(input, rev) + ";e;f";
804806
auto narHashOld =
805807
fetchToStore2(settings, *store, {accessor2}, FetchMode::DryRun, input.getName()).second;
@@ -882,7 +884,7 @@ struct GitInputScheme : InputScheme
882884
auto exportIgnore = getExportIgnoreAttr(input);
883885

884886
ref<SourceAccessor> accessor =
885-
repo->getAccessor(repoInfo.workdirInfo, exportIgnore, makeNotAllowedError(repoPath));
887+
repo->getAccessor(repoInfo.workdirInfo, {.exportIgnore = exportIgnore}, makeNotAllowedError(repoPath));
886888

887889
/* If the repo has submodules, return a mounted input accessor
888890
consisting of the accessor for the top-level repo and the

src/libfetchers/github.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ struct GitArchiveInputScheme : InputScheme
326326
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
327327

328328
auto accessor =
329-
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string(true) + "»");
329+
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, {}, "«" + input.to_string(true) + "»");
330330

331331
if (!settings.trustTarballsFromGitForges)
332332
// FIXME: computing the NAR hash here is wasteful if

src/libfetchers/include/nix/fetchers/git-utils.hh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ struct GitFileSystemObjectSink : ExtendedFileSystemObjectSink
2222
virtual Hash flush() = 0;
2323
};
2424

25+
struct GitAccessorOptions
26+
{
27+
bool exportIgnore = false;
28+
bool smudgeLfs = false;
29+
bool applyFilters = false;
30+
};
31+
2532
struct GitRepo
2633
{
2734
virtual ~GitRepo() {}
@@ -88,15 +95,11 @@ struct GitRepo
8895

8996
virtual bool hasObject(const Hash & oid) = 0;
9097

91-
virtual ref<SourceAccessor> getAccessor(
92-
const Hash & rev,
93-
bool exportIgnore,
94-
std::string displayPrefix,
95-
bool smudgeLfs = false,
96-
bool applyFilters = false) = 0;
97-
9898
virtual ref<SourceAccessor>
99-
getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError makeNotAllowedError) = 0;
99+
getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix) = 0;
100+
101+
virtual ref<SourceAccessor> getAccessor(
102+
const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError makeNotAllowedError) = 0;
100103

101104
virtual ref<GitFileSystemObjectSink> getFileSystemObjectSink() = 0;
102105

src/libfetchers/tarball.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static DownloadTarballResult downloadTarball_(
136136
.treeHash = treeHash,
137137
.lastModified = (time_t) getIntAttr(infoAttrs, "lastModified"),
138138
.immutableUrl = maybeGetStrAttr(infoAttrs, "immutableUrl"),
139-
.accessor = settings.getTarballCache()->getAccessor(treeHash, false, displayPrefix),
139+
.accessor = settings.getTarballCache()->getAccessor(treeHash, {}, displayPrefix),
140140
};
141141
};
142142

0 commit comments

Comments
 (0)