Skip to content

Commit

Permalink
Shallow clone submodules by default (#106)
Browse files Browse the repository at this point in the history
* shallow clone submodules by default

* adjust tests

* add docs
  • Loading branch information
6543 authored Nov 17, 2023
1 parent 94aaafe commit 74efdd5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
1 change: 1 addition & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ clone:
| `tags` | `false` (except on tag event) | Fetches tags when set to true, default is false if event is not tag else true |
| `submodule_overrides` | _none_ | Override submodule urls |
| `submodule_update_remote` | `false` | Pass the --remote flag to git submodule update |
| `submodule_partial` | `true` | Update submodules via partial clone (depth=1) |
| `custom_ssl_path` | _none_ | Set path to custom cert |
| `custom_ssl_url` | _none_ | Set url to custom cert |
| `backoff` | `5sec` | Change backoff duration |
Expand Down
6 changes: 6 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ var globalFlags = []cli.Flag{
EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"},
Value: &MapFlag{},
},
&cli.BoolFlag{
Name: "submodule-partial",
Usage: "update submodules via partial clone (depth=1) (default)",
EnvVars: []string{"PLUGIN_SUBMODULES_PARTIAL", "PLUGIN_SUBMODULE_PARTIAL"},
Value: true,
},
&cli.DurationFlag{
Name: "backoff",
Usage: "backoff duration",
Expand Down
29 changes: 15 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ func run(c *cli.Context) error {
Password: c.String("netrc.password"),
},
Config: Config{
Depth: c.Int("depth"),
Tags: c.Bool("tags"),
Recursive: c.Bool("recursive"),
SkipVerify: c.Bool("skip-verify"),
CustomCert: c.String("custom-cert"),
SubmoduleRemote: c.Bool("submodule-update-remote"),
Submodules: c.Generic("submodule-override").(*MapFlag).Get(),
Lfs: c.Bool("lfs"),
Branch: c.String("branch"),
Partial: c.Bool("partial"),
Home: c.String("home"),
SafeDirectory: c.String("safe-directory"),
UseSSH: c.Bool("use-ssh"),
SSHKey: c.String("ssh-key"),
Depth: c.Int("depth"),
Tags: c.Bool("tags"),
Recursive: c.Bool("recursive"),
SkipVerify: c.Bool("skip-verify"),
CustomCert: c.String("custom-cert"),
SubmoduleRemote: c.Bool("submodule-update-remote"),
Submodules: c.Generic("submodule-override").(*MapFlag).Get(),
SubmodulePartial: c.Bool("submodule-partial"),
Lfs: c.Bool("lfs"),
Branch: c.String("branch"),
Partial: c.Bool("partial"),
Home: c.String("home"),
SafeDirectory: c.String("safe-directory"),
UseSSH: c.Bool("use-ssh"),
SSHKey: c.String("ssh-key"),
},
Backoff: Backoff{
Attempts: c.Int("backoff-attempts"),
Expand Down
16 changes: 7 additions & 9 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (p Plugin) Exec() error {
}

if p.Config.Recursive {
cmds = append(cmds, updateSubmodules(p.Config.SubmoduleRemote))
cmds = append(cmds, updateSubmodules(p.Config.SubmoduleRemote, p.Config.SubmodulePartial))
}

if p.Config.Lfs {
Expand Down Expand Up @@ -295,14 +295,12 @@ func fetch(ref string, tags bool, depth int, filter string) *exec.Cmd {
}

// updateSubmodules recursively initializes and updates submodules.
func updateSubmodules(remote bool) *exec.Cmd {
cmd := exec.Command(
"git",
"submodule",
"update",
"--init",
"--recursive",
)
func updateSubmodules(remote, partial bool) *exec.Cmd {
args := []string{"submodule", "update", "--init", "--recursive"}
if partial {
args = append(args, "--depth=1", "--recommend-shallow")
}
cmd := exec.Command("git", args...)

if remote {
cmd.Args = append(cmd.Args, "--remote")
Expand Down
19 changes: 9 additions & 10 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ func TestFetch(t *testing.T) {
// are constructed properly.
func TestUpdateSubmodules(t *testing.T) {
testdata := []struct {
depth int
exp []string
partial bool
exp []string
}{
{
50,
false,
[]string{
"git",
"submodule",
Expand All @@ -292,18 +292,20 @@ func TestUpdateSubmodules(t *testing.T) {
},
},
{
100,
true,
[]string{
"git",
"submodule",
"update",
"--init",
"--recursive",
"--depth=1",
"--recommend-shallow",
},
},
}
for _, td := range testdata {
c := updateSubmodules(false)
c := updateSubmodules(false, td.partial)
if len(c.Args) != len(td.exp) {
t.Errorf("Expected: %s, got %s", td.exp, c.Args)
}
Expand Down Expand Up @@ -374,11 +376,9 @@ func TestCustomCertFile(t *testing.T) {
// are constructed properly.
func TestUpdateSubmodulesRemote(t *testing.T) {
testdata := []struct {
depth int
exp []string
exp []string
}{
{
50,
[]string{
"git",
"submodule",
Expand All @@ -389,7 +389,6 @@ func TestUpdateSubmodulesRemote(t *testing.T) {
},
},
{
100,
[]string{
"git",
"submodule",
Expand All @@ -401,7 +400,7 @@ func TestUpdateSubmodulesRemote(t *testing.T) {
},
}
for _, td := range testdata {
c := updateSubmodules(true)
c := updateSubmodules(true, false)
if len(c.Args) != len(td.exp) {
t.Errorf("Expected: %s, got %s", td.exp, c.Args)
}
Expand Down
31 changes: 16 additions & 15 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ type (
}

Config struct {
Depth int
Recursive bool
SkipVerify bool
Tags bool
Submodules map[string]string
SubmoduleRemote bool
CustomCert string
Lfs bool
Branch string
Home string
Partial bool
filter string
SafeDirectory string
UseSSH bool
SSHKey string
Depth int
Recursive bool
SkipVerify bool
Tags bool
Submodules map[string]string
SubmoduleRemote bool
SubmodulePartial bool
CustomCert string
Lfs bool
Branch string
Home string
Partial bool
filter string
SafeDirectory string
UseSSH bool
SSHKey string
}

Backoff struct {
Expand Down

0 comments on commit 74efdd5

Please sign in to comment.