Skip to content

Commit 70c4f21

Browse files
alt-romesmpickering
authored andcommitted
source-repository: Use git shallow clones
Cloning the entire repository for the purpose of compiling packages specified in source-repository-packages is wasted effort. To read and compile the package, we need only the HEAD of the repository, thus a shallow clone is sufficient. Fixes #7264
1 parent a9f9d11 commit 70c4f21

File tree

1 file changed

+10
-4
lines changed
  • cabal-install/src/Distribution/Client

1 file changed

+10
-4
lines changed

cabal-install/src/Distribution/Client/VCS.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,19 +460,21 @@ vcsGit =
460460
vcsCloneRepo verbosity prog repo srcuri destdir =
461461
[programInvocation prog cloneArgs]
462462
-- And if there's a tag, we have to do that in a second step:
463-
++ [git (resetArgs tag) | tag <- maybeToList (srpTag repo)]
463+
++ [git (step tag) | tag <- maybeToList (srpTag repo), step <- [fetchArgs, resetArgs]]
464464
++ [ git (["submodule", "sync", "--recursive"] ++ verboseArg)
465465
, git (["submodule", "update", "--init", "--force", "--recursive"] ++ verboseArg)
466466
]
467467
where
468468
git args = (programInvocation prog args){progInvokeCwd = Just destdir}
469469
cloneArgs =
470-
["clone", srcuri, destdir]
470+
["clone", "--depth=1", srcuri, destdir]
471471
++ branchArgs
472472
++ verboseArg
473473
branchArgs = case srpBranch repo of
474474
Just b -> ["--branch", b]
475475
Nothing -> []
476+
-- To checkout/reset to a particular commit, we must first fetch it (since the base clone is shallow).
477+
fetchArgs tag = "fetch" : verboseArg ++ ["origin", tag]
476478
resetArgs tag = "reset" : verboseArg ++ ["--hard", tag, "--"]
477479
verboseArg = ["--quiet" | verbosity < Verbosity.normal]
478480

@@ -510,7 +512,9 @@ vcsGit =
510512
let gitModulesDir = localDir </> ".git" </> "modules"
511513
gitModulesExists <- doesDirectoryExist gitModulesDir
512514
when gitModulesExists $ removeDirectoryRecursive gitModulesDir
513-
git localDir resetArgs
515+
when (resetTarget /= "HEAD") $ do
516+
git localDir fetchArgs -- first fetch the tag if needed
517+
git localDir resetArgs -- only then reset to the commit
514518
git localDir $ ["submodule", "sync", "--recursive"] ++ verboseArg
515519
git localDir $ ["submodule", "update", "--force", "--init", "--recursive"] ++ verboseArg
516520
git localDir $ ["submodule", "foreach", "--recursive"] ++ verboseArg ++ ["git clean -ffxdq"]
@@ -524,13 +528,15 @@ vcsGit =
524528
}
525529

526530
cloneArgs =
527-
["clone", "--no-checkout", loc, localDir]
531+
["clone", "--depth=1", "--no-checkout", loc, localDir]
528532
++ case peer of
529533
Nothing -> []
530534
Just peerLocalDir -> ["--reference", peerLocalDir]
531535
++ verboseArg
532536
where
533537
loc = srpLocation
538+
-- To checkout/reset to a particular commit, we must first fetch it (since the base clone is shallow).
539+
fetchArgs = "fetch" : verboseArg ++ ["origin", resetTarget]
534540
resetArgs = "reset" : verboseArg ++ ["--hard", resetTarget, "--"]
535541
resetTarget = fromMaybe "HEAD" (srpBranch `mplus` srpTag)
536542
verboseArg = ["--quiet" | verbosity < Verbosity.normal]

0 commit comments

Comments
 (0)