@@ -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