github.com/jenkins-x/jx/v2@v2.1.155/pkg/gits/testhelpers/testhelpers.go (about) 1 package testhelpers 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/jenkins-x/jx-logging/pkg/log" 11 "github.com/jenkins-x/jx/v2/pkg/util" 12 ) 13 14 // WriteFile creates a file with the specified name underneath the gitRepo directory adding the specified content. 15 // The file name can be path as well and intermediate directories are created. 16 func WriteFile(fail func(string, ...int), repoDir string, name string, contents string) { 17 path := filepath.Join(repoDir, name) 18 err := os.MkdirAll(filepath.Dir(path), 0755) 19 if err != nil { 20 log.Logger().Error(err.Error()) 21 fail("unable to create directory") 22 } 23 24 b := []byte(contents) 25 err = ioutil.WriteFile(path, b, 0600) 26 if err != nil { 27 log.Logger().Error(err.Error()) 28 fail("unable to write file content") 29 } 30 } 31 32 // HeadSha returns the commit SHA of the current HEAD commit within the specified git directory 33 func HeadSha(fail func(string, ...int), repoDir string) string { 34 data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", "HEAD")) 35 if err != nil { 36 log.Logger().Error(err.Error()) 37 fail("unable to read file") 38 } 39 40 var sha string 41 if strings.HasPrefix(string(data), "ref:") { 42 headRef := strings.TrimPrefix(string(data), "ref: ") 43 headRef = strings.Trim(headRef, "\n") 44 sha = ReadRef(fail, repoDir, headRef) 45 } else { 46 sha = string(data) 47 } 48 49 return sha 50 } 51 52 // ReadRef reads the commit SHA of the specified ref. Needs to be of the form /refs/heads/<name>. 53 func ReadRef(fail func(string, ...int), repoDir string, name string) string { 54 data, err := ioutil.ReadFile(filepath.Join(repoDir, ".git", name)) 55 if err != nil { 56 log.Logger().Error(err.Error()) 57 fail("unable to read file") 58 } 59 return strings.Trim(string(data), "\n") 60 } 61 62 // Add adds all unstaged changes to the index. 63 func Add(fail func(string, ...int), repoDir string) { 64 GitCmd(fail, repoDir, "add", ".") 65 } 66 67 // Commit commits all staged changes with the specified commit message. 68 func Commit(fail func(string, ...int), repoDir string, message string) string { 69 GitCmd(fail, repoDir, "commit", "-m", message, "--no-gpg-sign") 70 return HeadSha(fail, repoDir) 71 } 72 73 // Tag creates an annotated tag. 74 func Tag(fail func(string, ...int), repoDir string, tag string, message string) string { 75 GitCmd(fail, repoDir, "tag", "-a", "-m", message, tag) 76 return ReadRef(fail, repoDir, fmt.Sprintf("/refs/tags/%s", tag)) 77 } 78 79 // Checkout switches to the specified branch. 80 func Checkout(fail func(string, ...int), repoDir string, branch string) { 81 GitCmd(fail, repoDir, "checkout", branch) 82 } 83 84 // Branch creates a new branch with the specified name. 85 func Branch(fail func(string, ...int), repoDir string, branch string) { 86 GitCmd(fail, repoDir, "checkout", "-b", branch) 87 } 88 89 // DetachHead puts the repository in a detached head mode. 90 func DetachHead(fail func(string, ...int), repoDir string) { 91 head := HeadSha(fail, repoDir) 92 GitCmd(fail, repoDir, "checkout", head) 93 } 94 95 // Merge merges the specified commits into the current branch 96 func Merge(fail func(string, ...int), repoDir string, commits ...string) { 97 args := []string{"merge", "--no-gpg-sign"} 98 args = append(args, commits...) 99 GitCmd(fail, repoDir, args...) 100 } 101 102 // Revlist lists commits that are reachable by following the parent links from the given commit 103 func Revlist(fail func(string, ...int), repoDir string, maxCount int, commit string) string { 104 args := []string{"rev-list", fmt.Sprintf("--max-count=%d", maxCount), commit} 105 return GitCmd(fail, repoDir, args...) 106 } 107 108 // GitCmd runs a git command with arguments in the specified git repository 109 func GitCmd(fail func(string, ...int), repoDir string, args ...string) string { 110 cmd := util.Command{ 111 Dir: repoDir, 112 Name: "git", 113 Args: args, 114 } 115 out, err := cmd.RunWithoutRetry() 116 if err != nil { 117 log.Logger().Error(err.Error()) 118 fail("unable to write file content") 119 } 120 return out 121 }