2013-07-04 09:07:23 +04:00
|
|
|
package commands
|
|
|
|
|
2013-07-05 03:18:54 +04:00
|
|
|
import (
|
2015-02-03 07:50:53 +03:00
|
|
|
"path/filepath"
|
2015-02-10 03:58:17 +03:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-02-03 07:50:53 +03:00
|
|
|
|
2014-02-10 20:22:36 +04:00
|
|
|
"github.com/github/hub/github"
|
|
|
|
"github.com/github/hub/utils"
|
2013-07-05 03:18:54 +04:00
|
|
|
)
|
|
|
|
|
2013-07-04 09:07:23 +04:00
|
|
|
var cmdInit = &Command{
|
|
|
|
Run: gitInit,
|
|
|
|
GitExtension: true,
|
|
|
|
Usage: "init -g",
|
2016-01-24 11:56:18 +03:00
|
|
|
Long: `Initialize a git repository and create it on GitHub.
|
|
|
|
|
|
|
|
## Options:
|
|
|
|
-g
|
|
|
|
After initializing the repository locally, create a "<USER>/<REPO>"
|
|
|
|
repository on GitHub and add it as the "origin" remote.
|
|
|
|
|
|
|
|
<USER> is your GitHub username, while <REPO> is the name of the current
|
|
|
|
working directory.
|
|
|
|
|
|
|
|
## Examples:
|
|
|
|
$ hub init -g
|
|
|
|
> git init
|
|
|
|
> git remote add origin git@github.com:USER/REPO.git
|
2016-01-24 18:50:01 +03:00
|
|
|
|
|
|
|
## See also:
|
|
|
|
|
|
|
|
hub-create(1), hub(1), git-init(1)
|
2013-07-04 09:07:23 +04:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
2013-12-30 02:18:14 +04:00
|
|
|
func init() {
|
|
|
|
CmdRunner.Use(cmdInit)
|
|
|
|
}
|
|
|
|
|
2013-07-04 09:07:23 +04:00
|
|
|
func gitInit(command *Command, args *Args) {
|
2015-02-10 03:58:17 +03:00
|
|
|
err := transformInitArgs(args)
|
|
|
|
utils.Check(err)
|
2013-07-05 03:18:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func transformInitArgs(args *Args) error {
|
|
|
|
if !parseInitFlag(args) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
var err error
|
|
|
|
dirToInit := "."
|
|
|
|
hasValueRegxp := regexp.MustCompile("^--(template|separate-git-dir|shared)$")
|
2015-02-03 07:50:53 +03:00
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
// 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
|
2015-02-03 07:50:53 +03:00
|
|
|
}
|
2013-07-05 03:18:54 +04:00
|
|
|
}
|
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
dirToInit, err = filepath.Abs(dirToInit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-22 13:27:40 +03:00
|
|
|
config := github.CurrentConfig()
|
|
|
|
host, err := config.DefaultHost()
|
|
|
|
if err != nil {
|
|
|
|
utils.Check(github.FormatError("initializing repository", err))
|
|
|
|
}
|
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
// 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)
|
2016-01-22 13:27:40 +03:00
|
|
|
project := github.NewProject(host.User, projectName, "")
|
2013-12-17 19:07:26 +04:00
|
|
|
url := project.GitURL("", "", true)
|
2015-02-03 07:50:53 +03:00
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
addRemote := []string{
|
|
|
|
"git", "--git-dir", filepath.Join(dirToInit, ".git"),
|
|
|
|
"remote", "add", "origin", url,
|
2015-02-03 07:50:53 +03:00
|
|
|
}
|
2015-02-10 03:58:17 +03:00
|
|
|
args.After(addRemote...)
|
2013-07-05 03:18:54 +04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInitFlag(args *Args) bool {
|
|
|
|
if i := args.IndexOfParam("-g"); i != -1 {
|
|
|
|
args.RemoveParam(i)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2013-07-04 09:07:23 +04:00
|
|
|
}
|