Skip to content

Commit

Permalink
Merge pull request #130 from motemen/remove-panic
Browse files Browse the repository at this point in the history
Remove panics
  • Loading branch information
Songmu authored Apr 27, 2019
2 parents 79a41ff + 0ae2baf commit 0706ba2
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 133 deletions.
28 changes: 28 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

================================================================

github.com/blang/semver
https://github.com/blang/semver
----------------------------------------------------------------
The MIT License

Copyright (c) 2014 Benedikt Lang <github at benediktlang.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


================================================================

github.com/daviddengcn/go-colortext
Expand Down
44 changes: 29 additions & 15 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ func doGet(c *cli.Context) error {
path := filepath.Clean(filepath.Join(wd, filepath.Join(parts...)))

var repoPath string
for _, r := range localRepositoryRoots() {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, r := range roots {
p := strings.TrimPrefix(path, r+string(filepath.Separator))
if p != path && (repoPath == "" || len(p) < len(repoPath)) {
repoPath = p
Expand Down Expand Up @@ -198,12 +202,15 @@ func doGet(c *cli.Context) error {
// If isShallow is true, does shallow cloning. (no effect if already cloned or the VCS is Mercurial and git-svn)
func getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool, vcsBackend string) error {
remoteURL := remote.URL()
local := LocalRepositoryFromURL(remoteURL)
local, err := LocalRepositoryFromURL(remoteURL)
if err != nil {
return err
}

path := local.FullPath
newPath := false

_, err := os.Stat(path)
_, err = os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
newPath = true
Expand Down Expand Up @@ -270,14 +277,14 @@ func doList(c *cli.Context) error {
}

repos := []*LocalRepository{}

walkLocalRepositories(func(repo *LocalRepository) {
if err := walkLocalRepositories(func(repo *LocalRepository) {
if filterFn(repo) == false {
return
}

repos = append(repos, repo)
})
}); err != nil {
return err
}

if printUniquePaths {
subpathCount := map[string]int{} // Count duplicated subpaths (ex. foo/dotfiles and bar/dotfiles)
Expand Down Expand Up @@ -327,18 +334,21 @@ func doLook(c *cli.Context) error {
}

reposFound := []*LocalRepository{}
walkLocalRepositories(func(repo *LocalRepository) {
if err := walkLocalRepositories(func(repo *LocalRepository) {
if repo.Matches(name) {
reposFound = append(reposFound, repo)
}
})
}); err != nil {
return err
}

if len(reposFound) == 0 {
url, err := NewURL(name)

if err == nil {
repo := LocalRepositoryFromURL(url)
_, err := os.Stat(repo.FullPath)
if url, err := NewURL(name); err == nil {
repo, err := LocalRepositoryFromURL(url)
if err != nil {
return err
}
_, err = os.Stat(repo.FullPath)

// if the directory exists
if err == nil {
Expand Down Expand Up @@ -472,7 +482,11 @@ func doImport(c *cli.Context) error {
func doRoot(c *cli.Context) error {
all := c.Bool("all")
if all {
for _, root := range localRepositoryRoots() {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, root := range roots {
fmt.Println(root)
}
} else {
Expand Down
44 changes: 18 additions & 26 deletions git.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"

"github.com/motemen/ghq/logger"
"github.com/blang/semver"
)

// GitConfigSingle fetches single git-config variable.
Expand Down Expand Up @@ -54,41 +54,33 @@ func GitConfig(args ...string) (string, error) {
return strings.TrimRight(string(buf), "\000"), nil
}

var versionRx = regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`)

var featureConfigURLMatchVersion = []uint{1, 8, 5}
var (
versionRx = regexp.MustCompile(`((?:\d+)\.(?:\d+)\.(?:\d+))`)
featureConfigURLMatchVersion = semver.MustParse("1.8.5")
)

func GitHasFeatureConfigURLMatch() bool {
func GitHasFeatureConfigURLMatch() error {
cmd := exec.Command("git", "--version")
buf, err := cmd.Output()

if err != nil {
return false
return fmt.Errorf("failed to execute %q: %s", "git --version", err)
}

return gitVersionOutputSatisfies(string(buf), featureConfigURLMatchVersion)
}

func gitVersionOutputSatisfies(gitVersionOutput string, baseVersionParts []uint) bool {
func gitVersionOutputSatisfies(gitVersionOutput string, baseVersion semver.Version) error {
versionStrings := versionRx.FindStringSubmatch(gitVersionOutput)
if versionStrings == nil {
return false
if len(versionStrings) == 0 {
return fmt.Errorf("failed to detect git version from %q", gitVersionOutput)
}

for i, v := range baseVersionParts {
thisV64, err := strconv.ParseUint(versionStrings[i+1], 10, 0)
logger.PanicIf(err)

thisV := uint(thisV64)

if thisV > v {
return true
} else if v == thisV {
continue
} else {
return false
}
ver, err := semver.Parse(versionStrings[1])
if err != nil {
return fmt.Errorf("failed to parse version string %q: %s", versionStrings[1], err)
}

return true
if ver.LT(baseVersion) {
return fmt.Errorf("This version of Git does not support `config --get-urlmatch`; per-URL settings are not available")
}
return nil
}
41 changes: 1 addition & 40 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGitConfigAll(t *testing.T) {
func TestGitConfigURL(t *testing.T) {
RegisterTestingT(t)

if GitHasFeatureConfigURLMatch() == false {
if GitHasFeatureConfigURLMatch() != nil {
t.Skip("Git does not have config --get-urlmatch feature")
}

Expand Down Expand Up @@ -46,42 +46,3 @@ vcs = hg
Expect(err).NotTo(HaveOccurred())
Expect(value).To(Equal(""))
}

func TestGitVersionOutputSatisfies(t *testing.T) {
RegisterTestingT(t)

Expect(
gitVersionOutputSatisfies(
"git version 1.7.9",
[]uint{1, 8, 5},
),
).To(BeFalse())

Expect(
gitVersionOutputSatisfies(
"git version 1.8.2.3",
[]uint{1, 8, 5},
),
).To(BeFalse())

Expect(
gitVersionOutputSatisfies(
"git version 1.8.5",
[]uint{1, 8, 5},
),
).To(BeTrue())

Expect(
gitVersionOutputSatisfies(
"git version 1.9.1",
[]uint{1, 8, 5},
),
).To(BeTrue())

Expect(
gitVersionOutputSatisfies(
"git version 2.0.0",
[]uint{1, 8, 5},
),
).To(BeTrue())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/motemen/ghq
go 1.12

require (
github.com/blang/semver v3.5.1+incompatible
github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 // indirect
github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450 // indirect
github.com/golangplus/fmt v0.0.0-20150411045040-2a5d6d7d2995 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920 h1:d/cVoZOrJPJHKH1NdeUjyVAWKp4OpOT+Q+6T1sH7jeU=
github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
Expand Down
Loading

0 comments on commit 0706ba2

Please sign in to comment.