Skip to content

Commit c35b682

Browse files
committed
Cleanup Git fingerprinting
1 parent 3aae35d commit c35b682

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/libfetchers/git-utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,14 @@ ref<GitRepo> GitRepo::openRepo(const std::filesystem::path & path, bool create,
738738
return make_ref<GitRepoImpl>(path, create, bare);
739739
}
740740

741+
std::string GitAccessorOptions::makeFingerprint(const Hash & rev) const
742+
{
743+
return rev.gitRev() + (exportIgnore ? ";e" : "") + (smudgeLfs ? ";l" : "") + (applyFilters ? ";f" : "");
744+
}
745+
741746
/**
742747
* Raw git tree input accessor.
743748
*/
744-
745749
struct GitSourceAccessor : SourceAccessor
746750
{
747751
struct State

src/libfetchers/git.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,11 @@ struct GitInputScheme : InputScheme
640640

641641
std::string makeFingerprint(const Input & input, const Hash & rev) const
642642
{
643-
return rev.gitRev() + (getSubmodulesAttr(input) ? ";s" : "") + (getExportIgnoreAttr(input) ? ";e" : "")
644-
+ (getLfsAttr(input) ? ";l" : "");
643+
auto options = GitAccessorOptions{
644+
.exportIgnore = getExportIgnoreAttr(input),
645+
.smudgeLfs = getLfsAttr(input),
646+
};
647+
return options.makeFingerprint(rev) + (getSubmodulesAttr(input) ? ";s" : "");
645648
}
646649

647650
std::pair<ref<SourceAccessor>, Input>
@@ -786,23 +789,24 @@ struct GitInputScheme : InputScheme
786789

787790
verifyCommit(input, repo);
788791

789-
bool exportIgnore = getExportIgnoreAttr(input);
790-
bool smudgeLfs = getLfsAttr(input);
791-
auto accessor = repo->getAccessor(
792-
rev, {.exportIgnore = exportIgnore, .smudgeLfs = smudgeLfs}, "«" + input.to_string(true) + "»");
792+
GitAccessorOptions options{
793+
.exportIgnore = getExportIgnoreAttr(input),
794+
.smudgeLfs = getLfsAttr(input),
795+
};
796+
auto accessor = repo->getAccessor(rev, options, "«" + input.to_string(true) + "»");
793797

794798
/* Backward compatibility hack for locks produced by Nix < 2.20 that depend on Nix applying Git filters or
795799
* `export-ignore`. Nix >= 2.20 doesn't do those, so we may get a NAR hash mismatch. If that happens, try again
796800
* with filters and export-ignore enabled. */
797801
if (auto expectedNarHash = input.getNarHash()) {
798802
if (accessor->pathExists(CanonPath(".gitattributes"))) {
799-
accessor->fingerprint = makeFingerprint(input, rev);
803+
accessor->fingerprint = options.makeFingerprint(rev);
800804
auto narHashNew =
801805
fetchToStore2(settings, *store, {accessor}, FetchMode::DryRun, input.getName()).second;
802806
if (expectedNarHash != narHashNew) {
803-
auto accessor2 = repo->getAccessor(
804-
rev, {.exportIgnore = true, .applyFilters = true}, "«" + input.to_string(true) + "»");
805-
accessor2->fingerprint = makeFingerprint(input, rev) + ";e;f";
807+
GitAccessorOptions options2{.exportIgnore = true, .applyFilters = true};
808+
auto accessor2 = repo->getAccessor(rev, options2, "«" + input.to_string(true) + "»");
809+
accessor2->fingerprint = options2.makeFingerprint(rev);
806810
auto narHashOld =
807811
fetchToStore2(settings, *store, {accessor2}, FetchMode::DryRun, input.getName()).second;
808812
if (expectedNarHash == narHashOld) {
@@ -814,6 +818,7 @@ struct GitInputScheme : InputScheme
814818
expectedNarHash->to_string(HashFormat::SRI, true),
815819
narHashNew.to_string(HashFormat::SRI, true));
816820
accessor = accessor2;
821+
options = options2;
817822
}
818823
}
819824
}
@@ -825,7 +830,7 @@ struct GitInputScheme : InputScheme
825830
if (getSubmodulesAttr(input)) {
826831
std::map<CanonPath, nix::ref<SourceAccessor>> mounts;
827832

828-
for (auto & [submodule, submoduleRev] : repo->getSubmodules(rev, exportIgnore)) {
833+
for (auto & [submodule, submoduleRev] : repo->getSubmodules(rev, options.exportIgnore)) {
829834
auto resolved = repo->resolveSubmoduleUrl(submodule.url);
830835
debug(
831836
"Git submodule %s: %s %s %s -> %s",
@@ -848,9 +853,9 @@ struct GitInputScheme : InputScheme
848853
}
849854
}
850855
attrs.insert_or_assign("rev", submoduleRev.gitRev());
851-
attrs.insert_or_assign("exportIgnore", Explicit<bool>{exportIgnore});
856+
attrs.insert_or_assign("exportIgnore", Explicit<bool>{options.exportIgnore});
852857
attrs.insert_or_assign("submodules", Explicit<bool>{true});
853-
attrs.insert_or_assign("lfs", Explicit<bool>{smudgeLfs});
858+
attrs.insert_or_assign("lfs", Explicit<bool>{options.smudgeLfs});
854859
attrs.insert_or_assign("allRefs", Explicit<bool>{true});
855860
auto submoduleInput = fetchers::Input::fromAttrs(settings, std::move(attrs));
856861
auto [submoduleAccessor, submoduleInput2] = submoduleInput.getAccessor(settings, store);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct GitAccessorOptions
2727
bool exportIgnore = false;
2828
bool smudgeLfs = false;
2929
bool applyFilters = false;
30+
31+
std::string makeFingerprint(const Hash & rev) const;
3032
};
3133

3234
struct GitRepo

0 commit comments

Comments
 (0)