From 30d79295666969002248e2563f494f904570cb45 Mon Sep 17 00:00:00 2001 From: Benoit Gagnon Date: Wed, 24 Jul 2019 22:17:11 -0400 Subject: [PATCH] Use git sparse checkout whenever possible If a SubDir is configured for the package, everything but that directory will be thrown away after the package is installed. --- pkg/git.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/git.go b/pkg/git.go index 60fc997..d199ff9 100644 --- a/pkg/git.go +++ b/pkg/git.go @@ -17,6 +17,7 @@ package pkg import ( "bytes" "context" + "io/ioutil" "os" "os/exec" "path" @@ -45,6 +46,20 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers return "", err } + if p.Source.Subdir != "" { + cmd = exec.CommandContext(ctx, "git", "config", "core.sparsecheckout", "true") + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = dir + err = cmd.Run() + if err != nil { + return "", err + } + glob := []byte(p.Source.Subdir + "/*\n") + ioutil.WriteFile(dir + "/.git/info/sparse-checkout", glob, 0644) + } + cmd = exec.CommandContext(ctx, "git", "-c", "advice.detachedHead=false", "checkout", version) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout