This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
91 lines (76 loc) · 2.03 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package commands
import (
"path/filepath"
"regexp"
"strings"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
var cmdInit = &Command{
Run: gitInit,
GitExtension: true,
Usage: "init -g",
Short: "Create an empty git repository or reinitialize an existing one",
Long: `Create a git repository as with git-init(1) and add remote origin at
"git@github.com:USER/REPOSITORY.git"; USER is your GitHub username and
REPOSITORY is the current working directory's basename.
`,
}
func init() {
CmdRunner.Use(cmdInit)
}
/*
$ hub init -g
> git init
> git remote add origin git@github.com:USER/REPO.git
*/
func gitInit(command *Command, args *Args) {
err := transformInitArgs(args)
utils.Check(err)
}
func transformInitArgs(args *Args) error {
if !parseInitFlag(args) {
return nil
}
var err error
dirToInit := "."
hasValueRegxp := regexp.MustCompile("^--(template|separate-git-dir|shared)$")
// Find the first argument that isn't related to any of the init flags.
// We assume this is the optional `directory` argument to git init.
for i := 0; i < args.ParamsSize(); i++ {
arg := args.Params[i]
if hasValueRegxp.MatchString(arg) {
i++
} else if !strings.HasPrefix(arg, "-") {
dirToInit = arg
break
}
}
dirToInit, err = filepath.Abs(dirToInit)
if err != nil {
return err
}
config := github.CurrentConfig()
host, err := config.DefaultHost()
if err != nil {
utils.Check(github.FormatError("initializing repository", err))
}
// Assume that the name of the working directory is going to be the name of
// the project on GitHub.
projectName := strings.Replace(filepath.Base(dirToInit), " ", "-", -1)
project := github.NewProject(host.User, projectName, "")
url := project.GitURL("", "", true)
addRemote := []string{
"git", "--git-dir", filepath.Join(dirToInit, ".git"),
"remote", "add", "origin", url,
}
args.After(addRemote...)
return nil
}
func parseInitFlag(args *Args) bool {
if i := args.IndexOfParam("-g"); i != -1 {
args.RemoveParam(i)
return true
}
return false
}