add --deep_git_clone flag

This commit is contained in:
john
2019-11-28 11:54:03 +00:00
parent fc92f4acd0
commit 6485a7cf3e
9 changed files with 87 additions and 6 deletions

View File

@@ -15,6 +15,41 @@ import (
// Cloner is a function that can clone a git repo.
type Cloner func(repoSpec *RepoSpec) error
// DeepClonerUsingGitExec uses a local git install
// to obtain a full-depth clone of a remote repo
func DeepClonerUsingGitExec(repoSpec *RepoSpec) error {
gitProgram, err := exec.LookPath("git")
if err != nil {
return errors.Wrap(err, "no 'git' program on path")
}
repoSpec.Dir, err = filesys.NewTmpConfirmedDir()
if err != nil {
return err
}
cmd := exec.Command(
gitProgram,
"clone",
repoSpec.CloneSpec(),
repoSpec.Dir.String())
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return errors.Wrapf(err, "trouble cloning %s", repoSpec.OrgRepo)
}
if repoSpec.Ref == "" {
repoSpec.Ref = "master"
}
cmd = exec.Command(gitProgram, "checkout", repoSpec.Ref)
cmd.Dir = repoSpec.Dir.String()
err = cmd.Run()
if err != nil {
return errors.Wrapf(
err, "trouble checking out href %s", repoSpec.Ref)
}
return nil
}
// ClonerUsingGitExec uses a local git install, as opposed
// to say, some remote API, to obtain a local clone of
// a remote repo.

View File

@@ -34,7 +34,8 @@ func NewFakeLoaderWithRestrictor(
// Create fake filesystem and inject it into initial Loader.
fSys := filesys.MakeFsInMemory()
fSys.Mkdir(initialDir)
ldr, err := loader.NewLoader(lr, initialDir, fSys)
dc := false
ldr, err := loader.NewLoader(lr, initialDir, fSys, dc)
if err != nil {
log.Fatalf("Unable to make loader: %v", err)
}

View File

@@ -57,8 +57,9 @@ metadata:
t.Fatalf("err %v", err)
}
dc := false
ldr, err := fLdr.NewLoader(
fLdr.RestrictionRootOnly, dir, fSys)
fLdr.RestrictionRootOnly, dir, fSys, dc)
if err != nil {
t.Fatalf("Err: %v", err)
}

View File

@@ -54,7 +54,8 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
if b.options.LoadRestrictions == types.LoadRestrictionsRootOnly {
lr = fLdr.RestrictionRootOnly
}
ldr, err := fLdr.NewLoader(lr, path, b.fSys)
ldr, err := fLdr.NewLoader(lr, path, b.fSys, b.options.DoDeepGitClone)
if err != nil {
return nil, err
}

View File

@@ -18,6 +18,10 @@ type Options struct {
// order as specified by the kustomization file(s).
DoLegacyResourceSort bool
// When true, use full-depth git clone, in order to allow
// reference to a specific commit
DoDeepGitClone bool
// Restrictions on what can be loaded from the file system.
// See type definition.
LoadRestrictions types.LoadRestrictions
@@ -33,6 +37,7 @@ type Options struct {
func MakeDefaultOptions() *Options {
return &Options{
DoLegacyResourceSort: true,
DoDeepGitClone: false,
LoadRestrictions: types.LoadRestrictionsRootOnly,
DoPrune: false,
PluginConfig: konfig.DisabledPluginConfig(),

View File

@@ -18,17 +18,21 @@ import (
// the remote bases will all be root-only restricted.
func NewLoader(
lr LoadRestrictorFunc,
target string, fSys filesys.FileSystem) (ifc.Loader, error) {
target string, fSys filesys.FileSystem, deepClone bool) (ifc.Loader, error) {
cloner := git.ClonerUsingGitExec
if deepClone {
cloner = git.DeepClonerUsingGitExec
}
repoSpec, err := git.NewRepoSpecFromUrl(target)
if err == nil {
// The target qualifies as a remote git target.
return newLoaderAtGitClone(
repoSpec, fSys, nil, git.ClonerUsingGitExec)
repoSpec, fSys, nil, cloner)
}
root, err := demandDirectoryRoot(fSys, target)
if err != nil {
return nil, err
}
return newLoaderAtConfirmedDir(
lr, root, fSys, nil, git.ClonerUsingGitExec), nil
lr, root, fSys, nil, cloner), nil
}