// Gets the tags from the given remote git repo. func GetRemoteTags(log *log.Logger, repoUrl string) (TagList, error) { args := []string{ "ls-remote", "--tags", repoUrl, } output, err := util.Exec(log, cmdName, args...) if err != nil { return []string{}, maskAny(err) } tags := TagList{} scanner := bufio.NewScanner(strings.NewReader(output)) for scanner.Scan() { line := scanner.Text() index := strings.Index(line, tagMarker) if index < 0 { continue } tag := line[index+len(tagMarker):] tags = append(tags, tag) } if err := scanner.Err(); err != nil { return tags, maskAny(err) } // Sort tags from high to low sort.Sort(tags) return tags, nil }
// Execute a `git commit` func Commit(log *log.Logger, message string) error { if msg, err := util.Exec(log, cmdName, "commit", "-m", message); err != nil { fmt.Printf("%s\n", msg) return maskAny(err) } return nil }
// git show-branch --merge-base func GetMergeBase(log *log.Logger) (string, error) { args := []string{ "show-branch", "--merge-base", } output, err := util.Exec(log, cmdName, args...) if err != nil { return "", maskAny(err) } return strings.TrimSpace(output), nil }
// Gets the a config value func GetConfig(log *log.Logger, key string) (string, error) { args := []string{ "config", "--get", key, } output, err := util.Exec(log, cmdName, args...) if err != nil { return "", maskAny(err) } return strings.TrimSpace(output), nil }
// Gets the name of the current branch func GetLocalBranchName(log *log.Logger) (string, error) { args := []string{ "rev-parse", "--abbrev-ref", "HEAD", } output, err := util.Exec(log, cmdName, args...) if err != nil { return "", maskAny(err) } return strings.TrimSpace(output), nil }
// Gets the latest commit hash from the given remote git repo + optional branch. func GetLatestRemoteCommit(log *log.Logger, repoUrl, branch string) (string, error) { args := []string{ "ls-remote", repoUrl, } if branch != "" { args = append(args, branch) } output, err := util.Exec(log, cmdName, args...) if err != nil { return "", maskAny(err) } parts := strings.Split(output, "\t") return parts[0], nil }
// Gets the latest commit hash from the given local git folder. func GetLatestLocalCommit(log *log.Logger, folder, branch string, short bool) (string, error) { if branch == "" { branch = "HEAD" } args := []string{"rev-parse"} if short { args = append(args, "--short") } args = append(args, branch) output, err := util.Exec(log, cmdName, args...) if err != nil { return "", maskAny(err) } return strings.TrimSpace(output), nil }
// Execute a `git status a b` func Diff(log *log.Logger, a, b string) (string, error) { args := []string{"diff", a, b, } if msg, err := util.Exec(log, cmdName, args...); err != nil { if log != nil { log.Error(msg) } else { fmt.Printf("%s\n", msg) } return "", maskAny(err) } else { return strings.TrimSpace(msg), nil } }
// Execute a `git status` func Status(log *log.Logger, porcelain bool) (string, error) { args := []string{"status"} if porcelain { args = append(args, "--porcelain") } if msg, err := util.Exec(log, cmdName, args...); err != nil { if log != nil { log.Error(msg) } else { fmt.Printf("%s\n", msg) } return "", maskAny(err) } else { return strings.TrimSpace(msg), nil } }
func initGit(log *log.Logger, projectDir string) error { path := filepath.Join(projectDir, gitDirPath) if info, err := os.Stat(path); os.IsNotExist(err) { if err := util.ExecuteInDir(projectDir, func() error { output, err := util.Exec(log, "git", "init") if err != nil { log.Error(output) return maskAny(err) } return nil }); err != nil { return maskAny(err) } } else if err != nil { return maskAny(err) } else if !info.IsDir() { return maskAny(fmt.Errorf("%s must be a directory", path)) } else { log.Debugf("Git already initialized in %s", projectDir) } return nil }