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.
This commit is contained in:
Benoit Gagnon 2019-07-24 22:17:11 -04:00
parent c9a5b0a6b2
commit 30d7929566

View file

@ -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