Permalink
Browse files

Use strings.TrimSuffix(s, "\n") instead of trim.LastNewline(s).

This is simpler and more readable, since package strings is a part of
the standard library.

Done with:

	gofmt -w -r 'trim.LastNewline(s) -> strings.TrimSuffix(s, "\n")' .
	goimports -w .
  • Loading branch information...
dmitshur committed Dec 15, 2018
1 parent fe50575 commit fccce8a570166fef688838910e5314282dc4d54f
Showing with 15 additions and 19 deletions.
  1. +4 −5 git17.go
  2. +7 −8 git28.go
  3. +4 −6 hg.go
@@ -9,7 +9,6 @@ import (
"strings"

"github.com/shurcooL/go/osutil"
"github.com/shurcooL/go/trim"
)

// git17 implements git support using git version 1.7+ binary.
@@ -41,7 +40,7 @@ func (git17) Branch(dir string) (string, error) {
return "", err
}
// Since rev-parse is considered porcelain and may change, need to error-check its output.
return trim.LastNewline(string(out)), nil
return strings.TrimSuffix(string(out), "\n"), nil
}

func (git17) LocalRevision(dir string, defaultBranch string) (string, error) {
@@ -154,7 +153,7 @@ func (g git17) RemoteBranchAndRevision(dir string) (branch string, revision stri
case err != nil && bytes.HasPrefix(stderr, []byte("fatal: 'origin' does not appear to be a git repository\n")):
return "", "", ErrNoRemote
case err != nil:
return "", "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
_, revision, err = parseGit17LsRemote(stdout)
if err != nil {
@@ -179,7 +178,7 @@ func (git17) remoteBranch(dir string) (string, error) {

stdout, stderr, err := dividedOutput(cmd)
if err != nil {
return "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
const s = "\n HEAD branch: "
i := bytes.Index(stdout, []byte(s))
@@ -218,7 +217,7 @@ func (remoteGit17) RemoteBranchAndRevision(remoteURL string) (branch string, rev

stdout, stderr, err := dividedOutput(cmd)
if err != nil {
return "", "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
return parseGit17LsRemote(stdout)
}
@@ -9,7 +9,6 @@ import (
"strings"

"github.com/shurcooL/go/osutil"
"github.com/shurcooL/go/trim"
)

var gitBinaryVersion, gitBinaryError = exec.Command("git", "--version").Output()
@@ -43,7 +42,7 @@ func (git28) Branch(dir string) (string, error) {
return "", err
}
// Since rev-parse is considered porcelain and may change, need to error-check its output.
return trim.LastNewline(string(out)), nil
return strings.TrimSuffix(string(out), "\n"), nil
}

// gitRevisionLength is the length of a git revision hash.
@@ -138,7 +137,7 @@ func (git28) RemoteURL(dir string) (string, error) {
case err != nil:
return "", err
}
return trim.LastNewline(string(stdout)), nil
return strings.TrimSuffix(string(stdout), "\n"), nil
}

func (g git28) RemoteBranchAndRevision(dir string) (branch string, revision string, err error) {
@@ -158,14 +157,14 @@ func (g git28) RemoteBranchAndRevision(dir string) (branch string, revision stri
case err != nil && bytes.HasPrefix(stderr, []byte("fatal: 'origin' does not appear to be a git repository\n")):
return "", "", ErrNoRemote
case err != nil && bytes.HasPrefix(stderr, []byte("remote: Repository not found.\n")):
return "", "", NotFoundError{Err: fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))}
return "", "", NotFoundError{Err: fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))}
// TODO: Consider detecting connectivity errors specifically via "fatal: unable to access " prefix:
//
// (done with wi-fi turned off)
// gostatus $ git ls-remote --symref origin HEAD refs/heads/*
// fatal: unable to access 'https://github.com/shurcooL/gostatus/': Could not resolve host: github.com
case err != nil:
return "", "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
branch, revision, err = parseGit28LsRemote(stdout)
switch {
@@ -194,7 +193,7 @@ func (git28) remoteBranch(dir string) (string, error) {

stdout, stderr, err := dividedOutput(cmd)
if err != nil {
return "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
const s = "\n HEAD branch: "
i := bytes.Index(stdout, []byte(s))
@@ -234,10 +233,10 @@ func (remoteGit28) RemoteBranchAndRevision(remoteURL string) (branch string, rev
stdout, stderr, err := dividedOutput(cmd)
switch {
case err != nil && bytes.HasPrefix(stderr, []byte("remote: Repository not found.\n")):
return "", "", NotFoundError{Err: fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))}
return "", "", NotFoundError{Err: fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))}
// TODO: Consider detecting connectivity errors specifically via "fatal: unable to access " prefix.
case err != nil:
return "", "", fmt.Errorf("%v: %s", err, trim.LastNewline(string(stderr)))
return "", "", fmt.Errorf("%v: %s", err, strings.TrimSuffix(string(stderr), "\n"))
}
branch, revision, err = parseGit28LsRemote(stdout)
switch {
10 hg.go
@@ -5,8 +5,6 @@ import (
"fmt"
"os/exec"
"strings"

"github.com/shurcooL/go/trim"
)

var _, hgBinaryError = exec.LookPath("hg")
@@ -46,7 +44,7 @@ func (hg) Branch(dir string) (string, error) {
if err != nil {
return "", err
}
return trim.LastNewline(string(out)), nil
return strings.TrimSuffix(string(out), "\n"), nil
}

// hgRevisionLength is the length of a Mercurial revision hash.
@@ -112,7 +110,7 @@ func (hg) RemoteURL(dir string) (string, error) {
if err != nil {
return "", err
}
return trim.LastNewline(string(out)), nil
return strings.TrimSuffix(string(out), "\n"), nil
}

func (hg) RemoteBranchAndRevision(dir string) (branch string, revision string, err error) {
@@ -127,7 +125,7 @@ func (hg) RemoteBranchAndRevision(dir string) (branch string, revision string, e
return "", "", err
}
// Get the last line of output.
lines := strings.Split(trim.LastNewline(string(out)), "\n") // lines will always contain at least one element.
lines := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") // lines will always contain at least one element.
return defaultBranch, lines[len(lines)-1], nil
}

@@ -152,6 +150,6 @@ func (remoteHg) RemoteBranchAndRevision(remoteURL string) (branch string, revisi
return "", "", err
}
// Get the last line of output.
lines := strings.Split(trim.LastNewline(string(out)), "\n") // lines will always contain at least one element.
lines := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") // lines will always contain at least one element.
return defaultBranch, lines[len(lines)-1], nil
}

0 comments on commit fccce8a

Please sign in to comment.