Skip to content

Commit eb654ac

Browse files
authored
Merge pull request #14610 from NixOS/git-accessor-options
Introduce GitAccessorOptions
2 parents 152e7e4 + 6c4d2a7 commit eb654ac

File tree

6 files changed

+37
-28
lines changed

6 files changed

+37
-28
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: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,15 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
541541
}
542542

543543
/**
544-
* A 'GitSourceAccessor' with no regard for export-ignore or any other transformations.
544+
* A 'GitSourceAccessor' with no regard for export-ignore.
545545
*/
546-
ref<GitSourceAccessor> getRawAccessor(const Hash & rev, bool smudgeLfs = false);
546+
ref<GitSourceAccessor> getRawAccessor(const Hash & rev, const GitAccessorOptions & options);
547547

548548
ref<SourceAccessor>
549-
getAccessor(const Hash & rev, bool exportIgnore, std::string displayPrefix, bool smudgeLfs = false) override;
549+
getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix) override;
550550

551-
ref<SourceAccessor> getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError e) override;
551+
ref<SourceAccessor>
552+
getAccessor(const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError e) override;
552553

553554
ref<GitFileSystemObjectSink> getFileSystemObjectSink() override;
554555

@@ -668,7 +669,7 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
668669

669670
Hash treeHashToNarHash(const fetchers::Settings & settings, const Hash & treeHash) override
670671
{
671-
auto accessor = getAccessor(treeHash, false, "");
672+
auto accessor = getAccessor(treeHash, {}, "");
672673

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

@@ -716,15 +717,17 @@ struct GitSourceAccessor : SourceAccessor
716717
ref<GitRepoImpl> repo;
717718
Object root;
718719
std::optional<lfs::Fetch> lfsFetch = std::nullopt;
720+
GitAccessorOptions options;
719721
};
720722

721723
Sync<State> state_;
722724

723-
GitSourceAccessor(ref<GitRepoImpl> repo_, const Hash & rev, bool smudgeLfs)
725+
GitSourceAccessor(ref<GitRepoImpl> repo_, const Hash & rev, const GitAccessorOptions & options)
724726
: state_{State{
725727
.repo = repo_,
726728
.root = peelToTreeOrBlob(lookupObject(*repo_, hashToOID(rev)).get()),
727-
.lfsFetch = smudgeLfs ? std::make_optional(lfs::Fetch(*repo_, hashToOID(rev))) : std::nullopt,
729+
.lfsFetch = options.smudgeLfs ? std::make_optional(lfs::Fetch(*repo_, hashToOID(rev))) : std::nullopt,
730+
.options = options,
728731
}}
729732
{
730733
}
@@ -1254,26 +1257,26 @@ struct GitFileSystemObjectSinkImpl : GitFileSystemObjectSink
12541257
}
12551258
};
12561259

1257-
ref<GitSourceAccessor> GitRepoImpl::getRawAccessor(const Hash & rev, bool smudgeLfs)
1260+
ref<GitSourceAccessor> GitRepoImpl::getRawAccessor(const Hash & rev, const GitAccessorOptions & options)
12581261
{
12591262
auto self = ref<GitRepoImpl>(shared_from_this());
1260-
return make_ref<GitSourceAccessor>(self, rev, smudgeLfs);
1263+
return make_ref<GitSourceAccessor>(self, rev, options);
12611264
}
12621265

12631266
ref<SourceAccessor>
1264-
GitRepoImpl::getAccessor(const Hash & rev, bool exportIgnore, std::string displayPrefix, bool smudgeLfs)
1267+
GitRepoImpl::getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix)
12651268
{
12661269
auto self = ref<GitRepoImpl>(shared_from_this());
1267-
ref<GitSourceAccessor> rawGitAccessor = getRawAccessor(rev, smudgeLfs);
1270+
ref<GitSourceAccessor> rawGitAccessor = getRawAccessor(rev, options);
12681271
rawGitAccessor->setPathDisplay(std::move(displayPrefix));
1269-
if (exportIgnore)
1272+
if (options.exportIgnore)
12701273
return make_ref<GitExportIgnoreSourceAccessor>(self, rawGitAccessor, rev);
12711274
else
12721275
return rawGitAccessor;
12731276
}
12741277

1275-
ref<SourceAccessor>
1276-
GitRepoImpl::getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError makeNotAllowedError)
1278+
ref<SourceAccessor> GitRepoImpl::getAccessor(
1279+
const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError makeNotAllowedError)
12771280
{
12781281
auto self = ref<GitRepoImpl>(shared_from_this());
12791282
ref<SourceAccessor> fileAccessor = AllowListSourceAccessor::create(
@@ -1283,10 +1286,9 @@ GitRepoImpl::getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllow
12831286
boost::unordered_flat_set<CanonPath>{CanonPath::root},
12841287
std::move(makeNotAllowedError))
12851288
.cast<SourceAccessor>();
1286-
if (exportIgnore)
1287-
return make_ref<GitExportIgnoreSourceAccessor>(self, fileAccessor, std::nullopt);
1288-
else
1289-
return fileAccessor;
1289+
if (options.exportIgnore)
1290+
fileAccessor = make_ref<GitExportIgnoreSourceAccessor>(self, fileAccessor, std::nullopt);
1291+
return fileAccessor;
12901292
}
12911293

12921294
ref<GitFileSystemObjectSink> GitRepoImpl::getFileSystemObjectSink()
@@ -1299,7 +1301,7 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
12991301
/* Read the .gitmodules files from this revision. */
13001302
CanonPath modulesFile(".gitmodules");
13011303

1302-
auto accessor = getAccessor(rev, exportIgnore, "");
1304+
auto accessor = getAccessor(rev, {.exportIgnore = exportIgnore}, "");
13031305
if (!accessor->pathExists(modulesFile))
13041306
return {};
13051307

@@ -1316,7 +1318,7 @@ std::vector<std::tuple<GitRepoImpl::Submodule, Hash>> GitRepoImpl::getSubmodules
13161318

13171319
std::vector<std::tuple<Submodule, Hash>> result;
13181320

1319-
auto rawAccessor = getRawAccessor(rev);
1321+
auto rawAccessor = getRawAccessor(rev, {});
13201322

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

src/libfetchers/git.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,8 @@ struct GitInputScheme : InputScheme
900900

901901
bool exportIgnore = getExportIgnoreAttr(input);
902902
bool smudgeLfs = getLfsAttr(input);
903-
auto accessor = repo->getAccessor(rev, exportIgnore, "«" + input.to_string() + "»", smudgeLfs);
903+
auto accessor = repo->getAccessor(
904+
rev, {.exportIgnore = exportIgnore, .smudgeLfs = smudgeLfs}, "«" + input.to_string() + "»");
904905

905906
/* If the repo has submodules, fetch them and return a mounted
906907
input accessor consisting of the accessor for the top-level
@@ -967,7 +968,7 @@ struct GitInputScheme : InputScheme
967968
auto exportIgnore = getExportIgnoreAttr(input);
968969

969970
ref<SourceAccessor> accessor =
970-
repo->getAccessor(repoInfo.workdirInfo, exportIgnore, makeNotAllowedError(repoPath));
971+
repo->getAccessor(repoInfo.workdirInfo, {.exportIgnore = exportIgnore}, makeNotAllowedError(repoPath));
971972

972973
/* If the repo has submodules, return a mounted input accessor
973974
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
@@ -351,7 +351,7 @@ struct GitArchiveInputScheme : InputScheme
351351
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
352352

353353
auto accessor =
354-
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»");
354+
settings.getTarballCache()->getAccessor(tarballInfo.treeHash, {}, "«" + input.to_string() + "»");
355355

356356
return {accessor, input};
357357
}

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

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

25+
struct GitAccessorOptions
26+
{
27+
bool exportIgnore = false;
28+
bool smudgeLfs = false;
29+
};
30+
2531
struct GitRepo
2632
{
2733
virtual ~GitRepo() {}
@@ -89,10 +95,10 @@ struct GitRepo
8995
virtual bool hasObject(const Hash & oid) = 0;
9096

9197
virtual ref<SourceAccessor>
92-
getAccessor(const Hash & rev, bool exportIgnore, std::string displayPrefix, bool smudgeLfs = false) = 0;
98+
getAccessor(const Hash & rev, const GitAccessorOptions & options, std::string displayPrefix) = 0;
9399

94-
virtual ref<SourceAccessor>
95-
getAccessor(const WorkdirInfo & wd, bool exportIgnore, MakeNotAllowedError makeNotAllowedError) = 0;
100+
virtual ref<SourceAccessor> getAccessor(
101+
const WorkdirInfo & wd, const GitAccessorOptions & options, MakeNotAllowedError makeNotAllowedError) = 0;
96102

97103
virtual ref<GitFileSystemObjectSink> getFileSystemObjectSink() = 0;
98104

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)