Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: add a clone option to allow for shallow cloning of submodules #765

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ type CloneOptions struct {
// within, using their default settings. This option is ignored if the
// cloned repository does not have a worktree.
RecurseSubmodules SubmoduleRescursivity
// ShallowSubmodules limit cloning submodules to the 1 level of depth.
// It matches the git command --shallow-submodules.
ShallowSubmodules bool
pjbgf marked this conversation as resolved.
Show resolved Hide resolved
// Progress is where the human readable information sent by the server is
// stored, if nil nothing is stored and the capability (if supported)
// no-progress, is sent to the server to avoid send this information.
Expand Down
8 changes: 7 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,13 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
if o.RecurseSubmodules != NoRecurseSubmodules {
if err := w.updateSubmodules(&SubmoduleUpdateOptions{
RecurseSubmodules: o.RecurseSubmodules,
Auth: o.Auth,
Depth: func() int {
if o.ShallowSubmodules {
return 1
}
return 0
}(),
Auth: o.Auth,
}); err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,43 @@ func (s *RepositorySuite) TestPlainCloneWithRecurseSubmodules(c *C) {
c.Assert(cfg.Submodules, HasLen, 2)
}

func (s *RepositorySuite) TestPlainCloneWithShallowSubmodules(c *C) {
if testing.Short() {
c.Skip("skipping test in short mode.")
}

dir, clean := s.TemporalDir()
defer clean()

path := fixtures.ByTag("submodule").One().Worktree().Root()
mainRepo, err := PlainClone(dir, false, &CloneOptions{
URL: path,
RecurseSubmodules: 1,
ShallowSubmodules: true,
})
c.Assert(err, IsNil)

mainWorktree, err := mainRepo.Worktree()
c.Assert(err, IsNil)

submodule, err := mainWorktree.Submodule("basic")
c.Assert(err, IsNil)

subRepo, err := submodule.Repository()
c.Assert(err, IsNil)

lr, err := subRepo.Log(&LogOptions{})
c.Assert(err, IsNil)

commitCount := 0
for _, err := lr.Next(); err == nil; _, err = lr.Next() {
commitCount++
}
c.Assert(err, IsNil)

c.Assert(commitCount, Equals, 1)
}

func (s *RepositorySuite) TestPlainCloneNoCheckout(c *C) {
dir, clean := s.TemporalDir()
defer clean()
Expand Down