Skip to content

Commit

Permalink
add create subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Dec 23, 2019
1 parent 2237341 commit 379281c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
40 changes: 40 additions & 0 deletions cmd_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/motemen/ghq/cmdutil"
"github.com/urfave/cli"
)

func doCreate(c *cli.Context) error {
var (
name = c.Args().First()
w = c.App.Writer
)
u, err := newURL(name, false, true)
if err != nil {
return err
}
root, err := getRoot(u.String())
if err != nil {
return err
}
p := filepath.Join(root, u.Hostname(), u.Path)
if err := os.MkdirAll(p, 0755); err != nil {
return err
}

cmd := exec.Command("git", "init")
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
cmd.Dir = p
if err := cmdutil.RunCommand(cmd, true); err != nil {
return err
}
_, err = fmt.Fprintln(w, p)
return err
}
8 changes: 8 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var commands = []cli.Command{
commandLook,
commandImport,
commandRoot,
commandCreate,
}

// cloneFlags are comman flags of `get` and `import` subcommands
Expand Down Expand Up @@ -85,6 +86,12 @@ var commandRoot = cli.Command{
},
}

var commandCreate = cli.Command{
Name: "create",
Usage: "Create repository",
Action: doCreate,
}

type commandDoc struct {
Parent string
Arguments string
Expand All @@ -96,6 +103,7 @@ var commandDocs = map[string]commandDoc{
"look": {"", "<project> | <user>/<project> | <host>/<user>/<project>"},
"import": {"", "< file"},
"root": {"", ""},
"create": {"", "<project> | <user>/<project> | <host>/<user>/<project>"},
}

// Makes template conditionals to generate per-command documents.
Expand Down
25 changes: 17 additions & 8 deletions local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,11 @@ func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) {
return localRepository, nil
}

prim, err := gitconfig.Do("--path", "--get-urlmatch", "ghq.root", remoteURL.String())
if err != nil && !gitconfig.IsNotFound(err) {
prim, err := getRoot(remoteURL.String())
if err != nil {
return nil, err
}
if prim == "" {
prim, err = primaryLocalRepositoryRoot()
if err != nil {
return nil, err
}
}

// No local repository found, returning new one
return &LocalRepository{
FullPath: filepath.Join(prim, relPath),
Expand All @@ -115,6 +110,20 @@ func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) {
}, nil
}

func getRoot(u string) (string, error) {
prim, err := gitconfig.Do("--path", "--get-urlmatch", "ghq.root", u)
if err != nil && !gitconfig.IsNotFound(err) {
return "", err
}
if prim == "" {
prim, err = primaryLocalRepositoryRoot()
if err != nil {
return "", err
}
}
return prim, nil
}

// Subpaths returns lists of tail parts of relative path from the root directory (shortest first)
// for example, {"ghq", "motemen/ghq", "github.com/motemen/ghq"} for $root/github.com/motemen/ghq.
func (repo *LocalRepository) Subpaths() []string {
Expand Down

0 comments on commit 379281c

Please sign in to comment.