Debian Codegraph @

api/internal/git/cloner.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package git
5
6 import (
7 "sigs.k8s.io/kustomize/kyaml/filesys"
8 )
9
10 // Cloner is a function that can clone a git repo.
11 type Cloner func(repoSpec *RepoSpec) error
12
13 // ClonerUsingGitExec uses a local git install, as opposed
14 // to say, some remote API, to obtain a local clone of
15 // a remote repo.
16 func ClonerUsingGitExec(repoSpec *RepoSpec) error {
17 r, err := newCmdRunner(repoSpec.Timeout)
18 if err != nil {
19 return err
20 }
21 repoSpec.Dir = r.dir
22 if err = r.run("init"); err != nil {
23 return err
24 }
25 // git relative submodule need origin, see https://github.com/kubernetes-sigs/kustomize/issues/5131
26 if err = r.run("remote", "add", "origin", repoSpec.CloneSpec()); err != nil {
27 return err
28 }
29 ref := "HEAD"
30 if repoSpec.Ref != "" {
31 ref = repoSpec.Ref
32 }
33 // we use repoSpec.CloneSpec() instead of origin because on error,
34 // the prior prints the actual repo url for the user.
35 if err = r.run("fetch", "--depth=1", repoSpec.CloneSpec(), ref); err != nil {
36 return err
37 }
38 if err = r.run("checkout", "FETCH_HEAD"); err != nil {
39 return err
40 }
41 if repoSpec.Submodules {
42 return r.run("submodule", "update", "--init", "--recursive")
43 }
44 return nil
45 }
46
47 // DoNothingCloner returns a cloner that only sets
48 // cloneDir field in the repoSpec. It's assumed that
49 // the cloneDir is associated with some fake filesystem
50 // used in a test.
51 func DoNothingCloner(dir filesys.ConfirmedDir) Cloner {
52 return func(rs *RepoSpec) error {
53 rs.Dir = dir
54 return nil
55 }
56 }