Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/import-subcommand'
Browse files Browse the repository at this point in the history
Closes #38

Conflicts:
	commands.go
  • Loading branch information
motemen committed Oct 6, 2014
2 parents 56cae94 + 1faa3f3 commit def109f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
43 changes: 42 additions & 1 deletion commands.go
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"runtime"
Expand Down Expand Up @@ -322,7 +323,45 @@ func doImport(c *cli.Context) {
isShallow = c.Bool("shallow")
)

scanner := bufio.NewScanner(os.Stdin)
var (
in io.Reader
finalize func() error
)

if len(c.Args()) == 0 {
// `ghq import` reads URLs from stdin
in = os.Stdin
finalize = func() error { return nil }
} else {
// Handle `ghq import starred motemen` case
// with `git config --global ghq.import.starred "!github-list-starred"`
subCommand := c.Args().First()
command, err := GitConfigSingle("ghq.import." + subCommand)
if err == nil && command == "" {
err = fmt.Errorf("ghq.import.%s configuration not found", subCommand)
}
utils.DieIf(err)

// execute `sh -c 'COMMAND "$@"' -- ARG...`
// TODO: Windows
command = strings.TrimLeft(command, "!")
shellCommand := append([]string{"sh", "-c", command + ` "$@"`, "--"}, c.Args().Tail()...)

utils.Log("run", strings.Join(append([]string{command}, c.Args().Tail()...), " "))

cmd := exec.Command(shellCommand[0], shellCommand[1:]...)
cmd.Stderr = os.Stderr

in, err = cmd.StdoutPipe()
utils.DieIf(err)

err = cmd.Start()
utils.DieIf(err)

finalize = cmd.Wait
}

scanner := bufio.NewScanner(in)
for scanner.Scan() {
line := scanner.Text()
url, err := NewURL(line)
Expand Down Expand Up @@ -353,6 +392,8 @@ func doImport(c *cli.Context) {
utils.Log("error", fmt.Sprintf("While reading input: %s", err))
os.Exit(1)
}

utils.DieIf(finalize())
}

func doRoot(c *cli.Context) {
Expand Down
28 changes: 24 additions & 4 deletions ghq.txt
Expand Up @@ -11,15 +11,16 @@ 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 cloning repositories from several web services (+ghq import+).
You can also list local repositories (+ghq list+), jump into local repositories (+ghq look+), and bulk get repositories by list of URLs (+ghq import+).

== SYNOPSIS

[verse]
'ghq' get [-u] [-p] (<repository URL> | <user>/<project> | <project>)
'ghq' list [-p] [-e] [<query>]
'ghq' look (<project> | <path/to/project>)
'ghq' import < FILE
'ghq' import [-u] [-p] < FILE
'ghq' import <subcommand> [<args>...]

== COMMANDS

Expand Down Expand Up @@ -48,7 +49,12 @@ look::
Look into a locally cloned repository with the shell.

import::
Reads repository URLs from stdin and performs 'get' for each of them.
If no extra arguments given, reads repository URLs from stdin line by line
and performs 'get' for each of them. +
If given a subcommand name e.g. 'ghq import <subcommand> [<args>...]',
ghq looks up a configuration 'ghq.import.<subcommand>' for a command, invokes
it, and uses its output as URLs list. See below for 'ghq.import.<subcommand>'
in CONFIGURATION section.

== CONFIGURATION

Expand All @@ -69,14 +75,28 @@ ghq.<url>.vcs::
Accepted values are "git", "github" (an alias for "git"), "mercurial", "hg"
(an alias for "mercurial"). +
To get this configuration variable effective, you will need Git 1.8.5 or higher. +
For example in .gitconfig: +
For example in .gitconfig:

....
[ghq "https://git.example.com/repos/"]
vcs = git
....


ghq.import.<subcommand>::
When 'import' is called with extra arguments e.g. 'ghq import <subcommand> [<args>...]',
first of them is treated as a subcommand name and this configuration value
will be used for a command. The command is invoked with rest arguments
and expected to print remote repository URLs line by line. +
For example with https://github.com/motemen/github-list-starred[github-list-starred]:

....
# Invoke as `ghq import starred motemen`
[ghq "import"]
starred = github-list-starred
....


ghq.ghe.host::
The hostname of your GitHub Enterprise installation. A repository that has a
hostname set with this key will be regarded as same one as one on GitHub.
Expand Down

0 comments on commit def109f

Please sign in to comment.