Shallow fetch for Git packages

If the server supports it, fetch a specific
revision with --depth 1. Otherwise, fall back
to the normal fetch.

This replaces the previous "clone" operation. The bandwidth and time savings
can be significant depending on the history
of the repository (number of commits).
This commit is contained in:
Benoit Gagnon 2019-07-24 22:52:06 -04:00
parent 30d7929566
commit 671f860a19

View file

@ -37,15 +37,47 @@ func NewGitPackage(source *spec.GitSource) Interface {
}
func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVersion string, err error) {
cmd := exec.CommandContext(ctx, "git", "clone", "-n", p.Source.Remote, dir)
cmd := exec.CommandContext(ctx, "git", "init")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
return "", err
}
cmd = exec.CommandContext(ctx, "git", "remote", "add", "origin", p.Source.Remote)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
return "", err
}
// Attempt shallow fetch at specific revision
cmd = exec.CommandContext(ctx, "git", "fetch", "--depth", "1", "origin", version)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
// Fall back to normal fetch (all revisions)
cmd = exec.CommandContext(ctx, "git", "fetch", "origin")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err != nil {
return "", err
}
}
// If a Subdir is specificied, a sparsecheckout is sufficient
if p.Source.Subdir != "" {
cmd = exec.CommandContext(ctx, "git", "config", "core.sparsecheckout", "true")
cmd.Stdin = os.Stdin