github.com/abayer/test-infra@v0.0.5/prow/git/localgit/localgit.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package localgit creates a local git repo that can be used for testing code 18 // that uses a git.Client. 19 package localgit 20 21 import ( 22 "fmt" 23 "io/ioutil" 24 "os" 25 "os/exec" 26 "path/filepath" 27 28 "k8s.io/test-infra/prow/git" 29 ) 30 31 // LocalGit stores the repos in a temp dir. Create with New and delete with 32 // Clean. 33 type LocalGit struct { 34 // Dir is the path to the base temp dir. Repos are at Dir/org/repo. 35 Dir string 36 // Git is the location of the git binary. 37 Git string 38 } 39 40 // New creates a LocalGit and a git.Client pointing at it. 41 func New() (*LocalGit, *git.Client, error) { 42 g, err := exec.LookPath("git") 43 if err != nil { 44 return nil, nil, err 45 } 46 t, err := ioutil.TempDir("", "localgit") 47 if err != nil { 48 return nil, nil, err 49 } 50 c, err := git.NewClient() 51 if err != nil { 52 os.RemoveAll(t) 53 return nil, nil, err 54 } 55 56 getSecret := func() []byte { 57 return []byte("") 58 } 59 60 c.SetCredentials("", getSecret) 61 62 c.SetRemote(t) 63 return &LocalGit{ 64 Dir: t, 65 Git: g, 66 }, c, nil 67 } 68 69 // Clean deletes the local git dir. 70 func (lg *LocalGit) Clean() error { 71 return os.RemoveAll(lg.Dir) 72 } 73 74 func runCmd(cmd, dir string, arg ...string) error { 75 c := exec.Command(cmd, arg...) 76 c.Dir = dir 77 if b, err := c.CombinedOutput(); err != nil { 78 return fmt.Errorf("%s %v: %v, %s", cmd, arg, err, string(b)) 79 } 80 return nil 81 } 82 83 // MakeFakeRepo creates the given repo and makes an initial commit. 84 func (lg *LocalGit) MakeFakeRepo(org, repo string) error { 85 rdir := filepath.Join(lg.Dir, org, repo) 86 if err := os.MkdirAll(rdir, os.ModePerm); err != nil { 87 return err 88 } 89 90 if err := runCmd(lg.Git, rdir, "init"); err != nil { 91 return err 92 } 93 if err := runCmd(lg.Git, rdir, "config", "user.email", "test@test.test"); err != nil { 94 return err 95 } 96 if err := runCmd(lg.Git, rdir, "config", "user.name", "test test"); err != nil { 97 return err 98 } 99 if err := runCmd(lg.Git, rdir, "config", "commit.gpgsign", "false"); err != nil { 100 return err 101 } 102 if err := lg.AddCommit(org, repo, map[string][]byte{"initial": {}}); err != nil { 103 return err 104 } 105 106 return nil 107 } 108 109 // AddCommit adds the files to a new commit in the repo. 110 func (lg *LocalGit) AddCommit(org, repo string, files map[string][]byte) error { 111 rdir := filepath.Join(lg.Dir, org, repo) 112 for f, b := range files { 113 path := filepath.Join(rdir, f) 114 if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { 115 return err 116 } 117 if err := ioutil.WriteFile(path, b, os.ModePerm); err != nil { 118 return err 119 } 120 if err := runCmd(lg.Git, rdir, "add", f); err != nil { 121 return err 122 } 123 } 124 return runCmd(lg.Git, rdir, "commit", "-m", "wow") 125 } 126 127 // CheckoutNewBranch does git checkout -b. 128 func (lg *LocalGit) CheckoutNewBranch(org, repo, branch string) error { 129 rdir := filepath.Join(lg.Dir, org, repo) 130 return runCmd(lg.Git, rdir, "checkout", "-b", branch) 131 } 132 133 // Checkout does git checkout. 134 func (lg *LocalGit) Checkout(org, repo, commitlike string) error { 135 rdir := filepath.Join(lg.Dir, org, repo) 136 return runCmd(lg.Git, rdir, "checkout", commitlike) 137 }