Skip to content

Commit

Permalink
source-repository: Use git shallow clones
Browse files Browse the repository at this point in the history
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.

Note that this doesn't change the behaviour of `cabal get -s` which
still does a full clone (--depth=1 is only used in vcsSyncRepo, not in
vcsCloneRepo)

Fixes #7264
  • Loading branch information
alt-romes committed Nov 16, 2024
1 parent 7ab3a5e commit b6c28ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions cabal-install/src/Distribution/Client/VCS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ vcsGit =
resetArgs tag = "reset" : verboseArg ++ ["--hard", tag, "--"]
verboseArg = ["--quiet" | verbosity < Verbosity.normal]

-- Note: No --depth=1 for vcsCloneRepo since that is used for `cabal get -s`,
-- whereas `vcsSyncRepo` is used for source-repository-package where we do want shallow clones.

vcsSyncRepos
:: Verbosity
-> ConfiguredProgram
Expand Down Expand Up @@ -529,7 +532,10 @@ vcsGit =
(removePathForcibly gitModulesDir)
(\e -> if isPermissionError e then removePathForcibly gitModulesDir else throw e)
else removeDirectoryRecursive gitModulesDir
git localDir resetArgs
when (resetTarget /= "HEAD") $ do
git localDir fetchArgs -- first fetch the tag if needed
git localDir setTagArgs
git localDir resetArgs -- only then reset to the commit
git localDir $ ["submodule", "sync", "--recursive"] ++ verboseArg
git localDir $ ["submodule", "update", "--force", "--init", "--recursive"] ++ verboseArg
git localDir $ ["submodule", "foreach", "--recursive"] ++ verboseArg ++ ["git clean -ffxdq"]
Expand All @@ -543,13 +549,20 @@ vcsGit =
}

cloneArgs =
["clone", "--no-checkout", loc, localDir]
["clone", "--depth=1", "--no-checkout", loc, localDir]
++ case peer of
Nothing -> []
Just peerLocalDir -> ["--reference", peerLocalDir]
++ verboseArg
where
loc = srpLocation
-- To checkout/reset to a particular commit, we must first fetch it
-- (since the base clone is shallow).
fetchArgs = "fetch" : verboseArg ++ ["origin", resetTarget]
-- And then create the Tag from the FETCH_HEAD (which we should have just fetched)
setTagArgs = ["tag", "-f", resetTarget, "FETCH_HEAD"]
-- Then resetting to that tag will work (if we don't create the tag
-- locally from FETCH_HEAD, it won't exist).
resetArgs = "reset" : verboseArg ++ ["--hard", resetTarget, "--"]
resetTarget = fromMaybe "HEAD" (srpBranch `mplus` srpTag)
verboseArg = ["--quiet" | verbosity < Verbosity.normal]
Expand Down
4 changes: 2 additions & 2 deletions cabal-testsuite/PackageTests/ExtraProgPath/setup.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# cabal v2-build
Configuration is affected by the following files:
- cabal.project
Warning: cannot determine version of <ROOT>/pkg-config :
""
Configuration is affected by the following files:
- cabal.project
Resolving dependencies...
Error: [Cabal-7107]
Could not resolve dependencies:
Expand Down

0 comments on commit b6c28ee

Please sign in to comment.