github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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 "github.com/sirupsen/logrus" 29 30 "k8s.io/test-infra/prow/git" 31 ) 32 33 // LocalGit stores the repos in a temp dir. Create with New and delete with 34 // Clean. 35 type LocalGit struct { 36 // Dir is the path to the base temp dir. Repos are at Dir/org/repo. 37 Dir string 38 // Git is the location of the git binary. 39 Git string 40 } 41 42 // New creates a LocalGit and a git.Client pointing at it. 43 func New() (*LocalGit, *git.Client, error) { 44 g, err := exec.LookPath("git") 45 if err != nil { 46 return nil, nil, err 47 } 48 t, err := ioutil.TempDir("", "localgit") 49 if err != nil { 50 return nil, nil, err 51 } 52 c, err := git.NewClient() 53 if err != nil { 54 os.RemoveAll(t) 55 return nil, nil, err 56 } 57 c.Logger = logrus.NewEntry(logrus.StandardLogger()) 58 c.SetRemote(t) 59 return &LocalGit{ 60 Dir: t, 61 Git: g, 62 }, c, nil 63 } 64 65 // Clean deletes the local git dir. 66 func (lg *LocalGit) Clean() error { 67 return os.RemoveAll(lg.Dir) 68 } 69 70 func runCmd(cmd, dir string, arg ...string) error { 71 c := exec.Command(cmd, arg...) 72 c.Dir = dir 73 if b, err := c.CombinedOutput(); err != nil { 74 return fmt.Errorf("%s %v: %v, %s", cmd, arg, err, string(b)) 75 } 76 return nil 77 } 78 79 // MakeFakeRepo creates the given repo and makes an initial commit. 80 func (lg *LocalGit) MakeFakeRepo(org, repo string) error { 81 rdir := filepath.Join(lg.Dir, org, repo) 82 if err := os.MkdirAll(rdir, os.ModePerm); err != nil { 83 return err 84 } 85 86 if err := runCmd(lg.Git, rdir, "init"); err != nil { 87 return err 88 } 89 if err := runCmd(lg.Git, rdir, "config", "user.email", "test@test.test"); err != nil { 90 return err 91 } 92 if err := runCmd(lg.Git, rdir, "config", "user.name", "test test"); err != nil { 93 return err 94 } 95 if err := lg.AddCommit(org, repo, map[string][]byte{"initial": {}}); err != nil { 96 return err 97 } 98 99 return nil 100 } 101 102 // AddCommit adds the files to a new commit in the repo. 103 func (lg *LocalGit) AddCommit(org, repo string, files map[string][]byte) error { 104 rdir := filepath.Join(lg.Dir, org, repo) 105 for f, b := range files { 106 if err := ioutil.WriteFile(filepath.Join(rdir, f), b, os.ModePerm); err != nil { 107 return err 108 } 109 if err := runCmd(lg.Git, rdir, "add", f); err != nil { 110 return err 111 } 112 } 113 return runCmd(lg.Git, rdir, "commit", "-m", "wow") 114 } 115 116 // CheckoutNewBranch does git checkout -b. 117 func (lg *LocalGit) CheckoutNewBranch(org, repo, branch string) error { 118 rdir := filepath.Join(lg.Dir, org, repo) 119 return runCmd(lg.Git, rdir, "checkout", "-b", branch) 120 } 121 122 // Checkout does git checkout. 123 func (lg *LocalGit) Checkout(org, repo, commitlike string) error { 124 rdir := filepath.Join(lg.Dir, org, repo) 125 return runCmd(lg.Git, rdir, "checkout", commitlike) 126 }