示例#1
0
文件: create.go 项目: jingweno/gh
/*
  $ gh create
  ... create repo on github ...
  > git remote add -f origin [email protected]:YOUR_USER/CURRENT_REPO.git

  # with description:
  $ gh create -d 'It shall be mine, all mine!'

  $ gh create recipes
  [ repo created on GitHub ]
  > git remote add origin [email protected]:YOUR_USER/recipes.git

  $ gh create sinatra/recipes
  [ repo created in GitHub organization ]
  > git remote add origin [email protected]:sinatra/recipes.git
*/
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()
	}

	configs := github.CurrentConfigs()
	credentials := configs.DefaultCredentials()

	owner := credentials.User
	if strings.Contains(newRepoName, "/") {
		split := strings.SplitN(newRepoName, "/", 2)
		owner = split[0]
		newRepoName = split[1]
	}

	project := github.NewProject(owner, newRepoName, credentials.Host)
	gh := github.NewClient(project.Host)

	var action string
	if gh.IsRepositoryExist(project) {
		fmt.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)
		}
	}

	remote, _ := github.OriginRemote()
	if remote == nil {
		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())
}
示例#2
0
文件: remote.go 项目: jingweno/gh
func transformRemoteArgs(args *Args) {
	ownerWithName := args.LastParam()
	owner, name := parseRepoNameOwner(ownerWithName)
	if owner == "" {
		return
	}

	localRepo := github.LocalRepo()
	var repoName string
	if name == "" {
		project, err := localRepo.MainProject()
		if err == nil {
			repoName = project.Name
		} else {
			repoName, err = utils.DirName()
			utils.Check(err)
		}
		name = repoName
	}

	words := args.Words()
	isPriavte := parseRemotePrivateFlag(args)
	if len(words) == 2 && words[1] == "origin" {
		// gh add origin
		credentials := github.CurrentConfigs().DefaultCredentials()
		owner = credentials.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)
	}

	project := github.NewProject(owner, name, "")
	// for GitHub Enterprise
	isPriavte = isPriavte || project.Host != github.GitHubHost
	url := project.GitURL(name, owner, isPriavte)
	args.AppendParams(url)
}
示例#3
0
文件: clone.go 项目: jingweno/gh
func transformCloneArgs(args *Args) {
	isSSH := parseClonePrivateFlag(args)
	hasValueRegxp := regexp.MustCompile("^(--(upload-pack|template|depth|origin|branch|reference|name)|-[ubo])$")
	nameWithOwnerRegexp := regexp.MustCompile(NameWithOwnerRe)
	for i := 0; i < args.ParamsSize(); i++ {
		a := args.Params[i]

		if strings.HasPrefix(a, "-") {
			if hasValueRegxp.MatchString(a) {
				i++
			}
		} else {
			if nameWithOwnerRegexp.MatchString(a) && !isDir(a) {
				name, owner := parseCloneNameAndOwner(a)
				var credentials *github.Credentials
				if owner == "" {
					configs := github.CurrentConfigs()
					credentials = configs.DefaultCredentials()
					owner = credentials.User
				}

				var host string
				if credentials != nil {
					host = credentials.Host
				}

				project := github.NewProject(owner, name, host)
				isSSH = isSSH ||
					args.Command != "submodule" &&
						credentials != nil &&
						project.Owner == credentials.User

				url := project.GitURL(name, owner, isSSH)
				args.ReplaceParam(i, url)
			}

			break
		}
	}
}
示例#4
0
文件: fork.go 项目: jingweno/gh
/*
  $ gh fork
  [ repo forked on GitHub ]
  > git remote add -f YOUR_USER [email protected]:YOUR_USER/CURRENT_REPO.git

  $ gh fork --no-remote
  [ repo forked on GitHub ]
*/
func fork(cmd *Command, args *Args) {
	localRepo := github.LocalRepo()

	project, err := localRepo.MainProject()
	utils.Check(err)

	configs := github.CurrentConfigs()
	credentials := configs.PromptFor(project.Host)
	forkProject := github.NewProject(credentials.User, project.Name, project.Host)

	client := github.NewClient(project.Host)
	existingRepo, err := client.Repository(forkProject)
	if err == nil {
		var parentURL *github.URL
		if parent := existingRepo.Parent; parent != nil {
			parentURL, _ = github.ParseURL(parent.HTMLURL)
		}
		if parentURL == nil || !reflect.DeepEqual(parentURL.Project, project) {
			err = fmt.Errorf("Error creating fork: %s already exists on %s",
				forkProject, forkProject.Host)
			utils.Check(err)
		}
	} else {
		if !args.Noop {
			_, err := client.ForkRepository(project)
			utils.Check(err)
		}
	}

	if flagForkNoRemote {
		os.Exit(0)
	} else {
		u := forkProject.GitURL("", "", true)
		args.Replace("git", "remote", "add", "-f", forkProject.Owner, u)
		args.After("echo", fmt.Sprintf("new remote: %s", forkProject.Owner))
	}
}