Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incompatible] remove the "import" subcommand and merge this function into the "get" subcommand #244

Merged
merged 9 commits into from Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions CREDITS
Expand Up @@ -246,6 +246,21 @@ 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/mattn/go-isatty
https://github.com/mattn/go-isatty
----------------------------------------------------------------
Copyright (c) Yasuhiro MATSUMOTO <mattn.jp@gmail.com>

MIT License (Expat)

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/motemen/go-colorine
Expand Down Expand Up @@ -422,6 +437,39 @@ SOFTWARE.

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

golang.org/x/crypto
https://golang.org/x/crypto
----------------------------------------------------------------
Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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.

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

golang.org/x/net
https://golang.org/x/net
----------------------------------------------------------------
Expand Down Expand Up @@ -488,6 +536,39 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

golang.org/x/sys
https://golang.org/x/sys
----------------------------------------------------------------
Copyright (c) 2009 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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.

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

golang.org/x/tools
https://golang.org/x/tools
----------------------------------------------------------------
Expand Down
11 changes: 1 addition & 10 deletions README.adoc
Expand Up @@ -11,15 +11,13 @@ ghq - Manage remote repository clones
$ ghq get https://github.com/motemen/ghq
# Runs `git clone https://github.com/motemen/ghq ~/.ghq/github.com/motemen/ghq`

You can also list local repositories (+ghq list+), jump into local repositories (+ghq look+), and bulk get repositories by list of URLs (+ghq import+).
You can also list local repositories (+ghq list+).

== SYNOPSIS

[verse]
ghq get [-u] [-p] [--shallow] [--vcs] [--look] [--silent] [--branch] [--no-recursive] (<repository URL> | <host>/<user>/<project> | <user>/<project> | <project>)
ghq list [-p] [-e] [<query>]
ghq look (<project> | <path/to/project> | <host>/<user>/<project> | <repository URL>)
ghq import [-u] [-p] [--shalow] [--vcs] [--silent] [--no-recursive] [--parallel] < FILE
ghq root [--all]

== COMMANDS
Expand Down Expand Up @@ -51,13 +49,6 @@ list::
If '-p' ('--full-path') is given, the full paths to the repository root are
printed instead of relative ones.

look::
Look into a locally cloned repository with the shell.

import::
If no extra arguments given, reads repository URLs from stdin line by line
and performs 'get' for each of them.

root::
Prints repositories' root (i.e. `ghq.root`). Without '--all' option, the
primary one is shown.
Expand Down
154 changes: 146 additions & 8 deletions cmd_get.go
@@ -1,15 +1,28 @@
package main

import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/mattn/go-isatty"
"github.com/motemen/ghq/cmdutil"
"github.com/motemen/ghq/logger"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
)

func doGet(c *cli.Context) error {
var (
argURL = c.Args().Get(0)
andLook = c.Bool("look")
args = c.Args().Slice()
andLook = c.Bool("look")
parallel = c.Bool("parallel")
)
g := &getter{
update: c.Bool("update"),
Expand All @@ -20,16 +33,141 @@ func doGet(c *cli.Context) error {
branch: c.String("branch"),
recursive: !c.Bool("no-recursive"),
}

if argURL == "" {
return fmt.Errorf("no project args specified. see `ghq get -h` for more details")
if parallel {
// force silent in parallel import
g.silent = true
}

if err := g.get(argURL); err != nil {
var (
firstArg string
scr scanner
)
if len(args) > 0 {
scr = &sliceScanner{slice: args}
} else {
fd := os.Stdin.Fd()
if isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd) {
return fmt.Errorf("no target args specified. see `ghq get -h` for more details")
}
scr = bufio.NewScanner(os.Stdin)
}
eg := &errgroup.Group{}
sem := make(chan struct{}, 6)
for scr.Scan() {
target := scr.Text()
if firstArg != "" {
firstArg = target
}
if parallel {
eg.Go(func() error {
sem <- struct{}{}
defer func() { <-sem }()
if err := g.get(target); err != nil {
logger.Log("error", err.Error())
}
return nil
})
} else {
if err := g.get(target); err != nil {
return err
}
}
}
if err := scr.Err(); err != nil {
return fmt.Errorf("While reading input: %s", err)
}
if err := eg.Wait(); err != nil {
return err
}
if andLook {
return doLook(c)
if andLook && firstArg != "" {
return look(firstArg)
}
return nil
}

type sliceScanner struct {
slice []string
index int
}

func (s *sliceScanner) Scan() bool {
s.index++
return s.index <= len(s.slice)
}

func (s *sliceScanner) Text() string {
return s.slice[s.index-1]
}

func (s *sliceScanner) Err() error {
return nil
}

type scanner interface {
Scan() bool
Text() string
Err() error
}

func detectShell() string {
shell := os.Getenv("SHELL")
if shell != "" {
return shell
}
if runtime.GOOS == "windows" {
return os.Getenv("COMSPEC")
}
return "/bin/sh"
}

func look(name string) error {
var (
reposFound []*LocalRepository
mu sync.Mutex
)
if err := walkAllLocalRepositories(func(repo *LocalRepository) {
if repo.Matches(name) {
mu.Lock()
reposFound = append(reposFound, repo)
mu.Unlock()
}
}); err != nil {
return err
}

if len(reposFound) == 0 {
if url, err := newURL(name, false, false); err == nil {
repo, err := LocalRepositoryFromURL(url)
if err != nil {
return err
}
_, err = os.Stat(repo.FullPath)

// if the directory exists
if err == nil {
reposFound = append(reposFound, repo)
}
}
}

switch len(reposFound) {
case 0:
return fmt.Errorf("No repository found")
case 1:
repo := reposFound[0]
cmd := exec.Command(detectShell())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = repo.FullPath
cmd.Env = append(os.Environ(), "GHQ_LOOK="+filepath.ToSlash(repo.RelPath))
return cmdutil.RunCommand(cmd, true)
default:
b := &strings.Builder{}
b.WriteString("More than one repositories are found; Try more precise name\n")
for _, repo := range reposFound {
b.WriteString(fmt.Sprintf(" - %s\n", strings.Join(repo.PathParts, "/")))
}
return errors.New(b.String())
}
}