diff --git a/git/git.go b/git/git.go index 8824872a4..7732751bd 100644 --- a/git/git.go +++ b/git/git.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io/ioutil" - "os" "os/exec" "path/filepath" "strings" @@ -31,16 +30,6 @@ func Dir() (string, error) { return gitDir, nil } -func WorkdirName() (string, error) { - toplevelCmd := exec.Command("git", "rev-parse", "--show-toplevel") - output, err := utils.PrepareCmd(toplevelCmd).Output() - dir := firstLine(output) - if dir == "" { - return "", fmt.Errorf("unable to determine git working directory") - } - return dir, err -} - func VerifyRef(ref string) bool { showRef := exec.Command("git", "show-ref", "--verify", "--quiet", ref) err := utils.PrepareCmd(showRef).Run() @@ -73,72 +62,10 @@ func BranchAtRef(paths ...string) (name string, err error) { return } -func Editor() (string, error) { - varCmd := exec.Command("git", "var", "GIT_EDITOR") - output, err := utils.PrepareCmd(varCmd).Output() - if err != nil { - return "", fmt.Errorf("Can't load git var: GIT_EDITOR") - } - - return os.ExpandEnv(firstLine(output)), nil -} - func Head() (string, error) { return BranchAtRef("HEAD") } -func SymbolicFullName(name string) (string, error) { - parseCmd := exec.Command("git", "rev-parse", "--symbolic-full-name", name) - output, err := utils.PrepareCmd(parseCmd).Output() - if err != nil { - return "", fmt.Errorf("Unknown revision or path not in the working tree: %s", name) - } - - return firstLine(output), nil -} - -func CommentChar(text string) (string, error) { - char, err := Config("core.commentchar") - if err != nil { - return "#", nil - } else if char == "auto" { - lines := strings.Split(text, "\n") - commentCharCandidates := strings.Split("#;@!$%^&|:", "") - candidateLoop: - for _, candidate := range commentCharCandidates { - for _, line := range lines { - if strings.HasPrefix(line, candidate) { - continue candidateLoop - } - } - return candidate, nil - } - return "", fmt.Errorf("unable to select a comment character that is not used in the current message") - } else { - return char, nil - } -} - -func Show(sha string) (string, error) { - cmd := exec.Command("git", "-c", "log.showSignature=false", "show", "-s", "--format=%s%n%+b", sha) - output, err := utils.PrepareCmd(cmd).Output() - return strings.TrimSpace(string(output)), err -} - -func Log(sha1, sha2 string) (string, error) { - shaRange := fmt.Sprintf("%s...%s", sha1, sha2) - cmd := exec.Command( - "-c", "log.showSignature=false", "log", "--no-color", - "--format=%h (%aN, %ar)%n%w(78,3,3)%s%n%+b", - "--cherry", shaRange) - outputs, err := utils.PrepareCmd(cmd).Output() - if err != nil { - return "", fmt.Errorf("Can't load git log %s..%s", sha1, sha2) - } - - return string(outputs), nil -} - func listRemotes() ([]string, error) { remoteCmd := exec.Command("git", "remote", "-v") output, err := utils.PrepareCmd(remoteCmd).Output() @@ -156,34 +83,6 @@ func Config(name string) (string, error) { } -func ConfigAll(name string) ([]string, error) { - mode := "--get-all" - if strings.Contains(name, "*") { - mode = "--get-regexp" - } - - configCmd := exec.Command("git", "config", mode, name) - output, err := utils.PrepareCmd(configCmd).Output() - if err != nil { - return nil, fmt.Errorf("Unknown config %s", name) - } - return outputLines(output), nil -} - -func LocalBranches() ([]string, error) { - branchesCmd := exec.Command("git", "branch", "--list") - output, err := utils.PrepareCmd(branchesCmd).Output() - if err != nil { - return nil, err - } - - branches := []string{} - for _, branch := range outputLines(output) { - branches = append(branches, branch[2:]) - } - return branches, nil -} - var GitCommand = func(args ...string) *exec.Cmd { return exec.Command("git", args...) } diff --git a/ui/ui.go b/ui/ui.go deleted file mode 100644 index a824ce963..000000000 --- a/ui/ui.go +++ /dev/null @@ -1,98 +0,0 @@ -package ui - -import ( - "fmt" - "io" - "os" - - "github.com/mattn/go-colorable" - "github.com/mattn/go-isatty" -) - -type UI interface { - Print(a ...interface{}) (n int, err error) - Printf(format string, a ...interface{}) (n int, err error) - Println(a ...interface{}) (n int, err error) - Errorf(format string, a ...interface{}) (n int, err error) - Errorln(a ...interface{}) (n int, err error) -} - -var ( - Stdout = colorable.NewColorableStdout() - Stderr = colorable.NewColorableStderr() - Default UI = Console{Stdout: Stdout, Stderr: Stderr} -) - -func Print(a ...interface{}) (n int) { - n, err := Default.Print(a...) - if err != nil { - // If something as basic as printing to stdout fails, just panic and exit - os.Exit(1) - } - return -} - -func Printf(format string, a ...interface{}) (n int) { - n, err := Default.Printf(format, a...) - if err != nil { - // If something as basic as printing to stdout fails, just panic and exit - os.Exit(1) - } - return -} - -func Println(a ...interface{}) (n int) { - n, err := Default.Println(a...) - if err != nil { - // If something as basic as printing to stdout fails, just panic and exit - os.Exit(1) - } - return -} - -func Errorf(format string, a ...interface{}) (n int) { - n, err := Default.Errorf(format, a...) - if err != nil { - // If something as basic as printing to stderr fails, just panic and exit - os.Exit(1) - } - return -} - -func Errorln(a ...interface{}) (n int) { - n, err := Default.Errorln(a...) - if err != nil { - // If something as basic as printing to stderr fails, just panic and exit - os.Exit(1) - } - return -} - -func IsTerminal(f *os.File) bool { - return isatty.IsTerminal(f.Fd()) -} - -type Console struct { - Stdout io.Writer - Stderr io.Writer -} - -func (c Console) Print(a ...interface{}) (n int, err error) { - return fmt.Fprint(c.Stdout, a...) -} - -func (c Console) Printf(format string, a ...interface{}) (n int, err error) { - return fmt.Fprintf(c.Stdout, format, a...) -} - -func (c Console) Println(a ...interface{}) (n int, err error) { - return fmt.Fprintln(c.Stdout, a...) -} - -func (c Console) Errorf(format string, a ...interface{}) (n int, err error) { - return fmt.Fprintf(c.Stderr, format, a...) -} - -func (c Console) Errorln(a ...interface{}) (n int, err error) { - return fmt.Fprintln(c.Stderr, a...) -} diff --git a/utils/utils.go b/utils/utils.go index b034a18d9..6a1730cb8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,31 +2,13 @@ package utils import ( "errors" - "fmt" "os" "os/exec" - "path/filepath" "runtime" - "strings" - "time" - "github.com/github/gh-cli/ui" "github.com/kballard/go-shellquote" ) -var timeNow = time.Now - -func Check(err error) { - if err != nil { - ui.Errorln(err) - os.Exit(1) - } -} - -func ConcatPaths(paths ...string) string { - return strings.Join(paths, "/") -} - func OpenInBrowser(url string) error { browser := os.Getenv("BROWSER") if browser == "" { @@ -69,58 +51,3 @@ func searchBrowserLauncher(goos string) (browser string) { return browser } - -func CommandPath(cmd string) (string, error) { - if runtime.GOOS == "windows" { - cmd = cmd + ".exe" - } - - path, err := exec.LookPath(cmd) - if err != nil { - return "", err - } - - path, err = filepath.Abs(path) - if err != nil { - return "", err - } - - return filepath.EvalSymlinks(path) -} - -func TimeAgo(t time.Time) string { - duration := timeNow().Sub(t) - minutes := duration.Minutes() - hours := duration.Hours() - days := hours / 24 - months := days / 30 - years := months / 12 - - var val int - var unit string - - if minutes < 1 { - return "now" - } else if hours < 1 { - val = int(minutes) - unit = "minute" - } else if days < 1 { - val = int(hours) - unit = "hour" - } else if months < 1 { - val = int(days) - unit = "day" - } else if years < 1 { - val = int(months) - unit = "month" - } else { - val = int(years) - unit = "year" - } - - var plural string - if val > 1 { - plural = "s" - } - return fmt.Sprintf("%d %s%s ago", val, unit, plural) -}