func transformRemoteArgs(args *Args) { ownerWithName := args.LastParam() owner, name := parseRepoNameOwner(ownerWithName) if owner == "" { return } localRepo, err := github.LocalRepo() utils.Check(err) var repoName, host string if name == "" { project, err := localRepo.MainProject() if err == nil { repoName = project.Name host = project.Host } else { repoName, err = utils.DirName() utils.Check(err) } name = repoName } hostConfig, err := github.CurrentConfig().DefaultHost() if err != nil { utils.Check(github.FormatError("adding remote", err)) } words := args.Words() isPrivate := parseRemotePrivateFlag(args) if len(words) == 2 && words[1] == "origin" { // Origin special case triggers default user/repo owner = hostConfig.User name = repoName } else if len(words) == 2 { // gh remote add jingweno foo/bar if idx := args.IndexOfParam(words[1]); idx != -1 { args.ReplaceParam(idx, owner) } } else { args.RemoveParam(args.ParamsSize() - 1) } if strings.ToLower(owner) == strings.ToLower(hostConfig.User) { owner = hostConfig.User isPrivate = true } project := github.NewProject(owner, name, host) // for GitHub Enterprise isPrivate = isPrivate || project.Host != github.GitHubHost url := project.GitURL(name, owner, isPrivate) args.AppendParams(url) }
func newProject(owner, name, host, protocol string) *Project { if strings.Contains(owner, "/") { result := strings.SplitN(owner, "/", 2) owner = result[0] if name == "" { name = result[1] } } else if strings.Contains(name, "/") { result := strings.SplitN(name, "/", 2) if owner == "" { owner = result[0] } name = result[1] } if host == "" { host = DefaultGitHubHost() } if host == "ssh.github.com" { host = "github.com" } if protocol != "http" && protocol != "https" { protocol = "" } if protocol == "" { h := CurrentConfig().Find(host) if h != nil { protocol = h.Protocol } } if protocol == "" { protocol = "https" } if owner == "" { h := CurrentConfig().Find(host) if h != nil { owner = h.User } } if name == "" { name, _ = utils.DirName() } return &Project{ Name: name, Owner: owner, Host: host, Protocol: protocol, } }
func create(command *Command, args *Args) { _, err := git.Dir() if err != nil { err = fmt.Errorf("'create' must be run from inside a git repository") utils.Check(err) } var newRepoName string if args.IsParamsEmpty() { newRepoName, err = utils.DirName() utils.Check(err) } else { reg := regexp.MustCompile("^[^-]") if !reg.MatchString(args.FirstParam()) { err = fmt.Errorf("invalid argument: %s", args.FirstParam()) utils.Check(err) } newRepoName = args.FirstParam() } config := github.CurrentConfig() host, err := config.DefaultHost() if err != nil { utils.Check(github.FormatError("creating repository", err)) } owner := host.User if strings.Contains(newRepoName, "/") { split := strings.SplitN(newRepoName, "/", 2) owner = split[0] newRepoName = split[1] } project := github.NewProject(owner, newRepoName, host.Host) gh := github.NewClient(project.Host) var action string if gh.IsRepositoryExist(project) { ui.Printf("%s already exists on %s\n", project, project.Host) action = "set remote origin" } else { action = "created repository" if !args.Noop { repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate) utils.Check(err) project = github.NewProject(repo.FullName, "", project.Host) } } localRepo, err := github.LocalRepo() utils.Check(err) remote, _ := localRepo.OriginRemote() if remote == nil || remote.Name != "origin" { url := project.GitURL("", "", true) args.Replace("git", "remote", "add", "-f", "origin", url) } else { args.Replace("git", "remote", "-v") } args.After("echo", fmt.Sprintf("%s:", action), project.String()) }