Skip to content

Commit 5fcf9f0

Browse files
committed
Remove fetchTree 'shallow' hack
builtins.fetchTree was setting `shallow = true` when fetching from git. That's bad because it makes it behave inconsistently from non-fetchTree fetches, e.g. when updating an input. Instead, the Git fetcher now will do a shallow fetch automatically if `revCount` is already set (e.g. when fetching a lock). Fixes NixOS#14588.
1 parent 6bf5230 commit 5fcf9f0

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/libexpr/primops/fetchTree.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ static void fetchTree(
150150
attrs.emplace("exportIgnore", Explicit<bool>{true});
151151
}
152152

153-
// fetchTree should fetch git repos with shallow = true by default
154-
if (type == "git" && !params.isFetchGit && !attrs.contains("shallow")) {
155-
attrs.emplace("shallow", Explicit<bool>{true});
156-
}
157-
158153
if (!params.allowNameArgument)
159154
if (auto nameIter = attrs.find("name"); nameIter != attrs.end())
160155
state.error<EvalError>("argument 'name' isn’t supported in call to '%s'", fetcher)

src/libfetchers/git.cc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,16 @@ struct GitInputScheme : InputScheme
627627
}
628628
}
629629

630+
/**
631+
* Decide whether we can do a shallow clone, which is faster. This is possible if the user explicitly specified
632+
* `shallow = true`, or if we already have a `revCount`.
633+
*/
634+
bool canDoShallow(const Input & input) const
635+
{
636+
bool shallow = getShallowAttr(input);
637+
return shallow || input.getRevCount().has_value();
638+
}
639+
630640
std::pair<ref<SourceAccessor>, Input>
631641
getAccessorFromCommit(const Settings & settings, ref<Store> store, RepoInfo & repoInfo, Input && input) const
632642
{
@@ -635,7 +645,7 @@ struct GitInputScheme : InputScheme
635645
auto origRev = input.getRev();
636646

637647
auto originalRef = input.getRef();
638-
bool shallow = getShallowAttr(input);
648+
bool shallow = canDoShallow(input);
639649
auto ref = originalRef ? *originalRef : getDefaultRef(repoInfo, shallow);
640650
input.attrs.insert_or_assign("ref", ref);
641651

@@ -680,7 +690,6 @@ struct GitInputScheme : InputScheme
680690
}
681691

682692
if (doFetch) {
683-
bool shallow = getShallowAttr(input);
684693
try {
685694
auto fetchRef = getAllRefsAttr(input) ? "refs/*:refs/*"
686695
: input.getRev() ? input.getRev()->gitRev()
@@ -727,13 +736,6 @@ struct GitInputScheme : InputScheme
727736

728737
auto repo = GitRepo::openRepo(repoDir);
729738

730-
auto isShallow = repo->isShallow();
731-
732-
if (isShallow && !getShallowAttr(input))
733-
throw Error(
734-
"'%s' is a shallow Git repository, but shallow repositories are only allowed when `shallow = true;` is specified",
735-
repoInfo.locationToArg());
736-
737739
// FIXME: check whether rev is an ancestor of ref?
738740

739741
auto rev = *input.getRev();
@@ -744,10 +746,16 @@ struct GitInputScheme : InputScheme
744746
if (!input.attrs.contains("lastModified"))
745747
input.attrs.insert_or_assign("lastModified", getLastModified(settings, repoInfo, repoDir, rev));
746748

747-
if (!getShallowAttr(input)) {
748-
/* Like lastModified, skip revCount if supplied by the caller. */
749-
if (!input.attrs.contains("revCount"))
750-
input.attrs.insert_or_assign("revCount", getRevCount(settings, repoInfo, repoDir, rev));
749+
/* Like lastModified, skip revCount if supplied by the caller. */
750+
if (!shallow && !input.attrs.contains("revCount")) {
751+
auto isShallow = repo->isShallow();
752+
753+
if (isShallow && !shallow)
754+
throw Error(
755+
"'%s' is a shallow Git repository, but shallow repositories are only allowed when `shallow = true;` is specified",
756+
repoInfo.locationToArg());
757+
758+
input.attrs.insert_or_assign("revCount", getRevCount(settings, repoInfo, repoDir, rev));
751759
}
752760

753761
printTalkative("using revision %s of repo '%s'", rev.gitRev(), repoInfo.locationToArg());

0 commit comments

Comments
 (0)