本文整理匯總了Golang中github.com/github/hub/github.LocalRepo函數的典型用法代碼示例。如果您正苦於以下問題:Golang LocalRepo函數的具體用法?Golang LocalRepo怎麽用?Golang LocalRepo使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了LocalRepo函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: downloadRelease
func downloadRelease(cmd *Command, args *Args) {
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
gh := github.NewClient(project.Host)
release, err := gh.FetchRelease(project, tagName)
utils.Check(err)
for _, asset := range release.Assets {
ui.Printf("Downloading %s ...\n", asset.Name)
err := downloadReleaseAsset(asset, gh)
utils.Check(err)
}
args.NoForward()
}
示例2: tranformFetchArgs
func tranformFetchArgs(args *Args) error {
names := parseRemoteNames(args)
localRepo, err := github.LocalRepo()
utils.Check(err)
currentProject, currentProjectErr := localRepo.CurrentProject()
projects := make(map[*github.Project]bool)
ownerRegexp := regexp.MustCompile(fmt.Sprintf("^%s$", OwnerRe))
for _, name := range names {
if ownerRegexp.MatchString(name) && !isCloneable(name) {
_, err := localRepo.RemoteByName(name)
if err != nil {
utils.Check(currentProjectErr)
project := github.NewProject(name, currentProject.Name, "")
gh := github.NewClient(project.Host)
repo, err := gh.Repository(project)
if err != nil {
continue
}
projects[project] = repo.Private
}
}
}
for project, private := range projects {
args.Before("git", "remote", "add", project.Owner, project.GitURL("", "", private))
}
return nil
}
示例3: ciStatus
/*
$ gh ci-status
> (prints CI state of HEAD and exits with appropriate code)
> One of: success (0), error (1), failure (1), pending (2), no status (3)
$ gh ci-status -v
> (prints CI state of HEAD, the URL to the CI build results and exits with appropriate code)
> One of: success (0), error (1), failure (1), pending (2), no status (3)
$ gh ci-status BRANCH
> (prints CI state of BRANCH and exits with appropriate code)
> One of: success (0), error (1), failure (1), pending (2), no status (3)
$ gh ci-status SHA
> (prints CI state of SHA and exits with appropriate code)
> One of: success (0), error (1), failure (1), pending (2), no status (3)
*/
func ciStatus(cmd *Command, args *Args) {
ref := "HEAD"
if !args.IsParamsEmpty() {
ref = args.RemoveParam(0)
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
sha, err := git.Ref(ref)
if err != nil {
err = fmt.Errorf("Aborted: no revision could be determined from '%s'", ref)
}
utils.Check(err)
if args.Noop {
ui.Printf("Would request CI status for %s\n", sha)
} else {
state, targetURL, exitCode, err := fetchCiStatus(project, sha)
utils.Check(err)
if flagCiStatusVerbose && targetURL != "" {
ui.Printf("%s: %s\n", state, targetURL)
} else {
ui.Println(state)
}
os.Exit(exitCode)
}
}
示例4: transformPushArgs
func transformPushArgs(args *Args) {
refs := []string{}
if args.ParamsSize() > 1 {
refs = args.Params[1:]
}
remotes := strings.Split(args.FirstParam(), ",")
args.ReplaceParam(0, remotes[0])
if len(refs) == 0 {
localRepo, err := github.LocalRepo()
utils.Check(err)
head, err := localRepo.CurrentBranch()
utils.Check(err)
refs = []string{head.ShortName()}
args.AppendParams(refs...)
}
for _, remote := range remotes[1:] {
afterCmd := []string{"git", "push", remote}
afterCmd = append(afterCmd, refs...)
args.After(afterCmd...)
}
}
示例5: parseCherryPickProjectAndSha
func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string) {
url, err := github.ParseURL(ref)
if err == nil {
commitRegex := regexp.MustCompile("^commit\\/([a-f0-9]{7,40})")
projectPath := url.ProjectPath()
if commitRegex.MatchString(projectPath) {
sha = commitRegex.FindStringSubmatch(projectPath)[1]
project = url.Project
return
}
}
ownerWithShaRegexp := regexp.MustCompile("^([a-zA-Z0-9][a-zA-Z0-9-]*)@([a-f0-9]{7,40})$")
if ownerWithShaRegexp.MatchString(ref) {
matches := ownerWithShaRegexp.FindStringSubmatch(ref)
sha = matches[2]
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err = localRepo.CurrentProject()
utils.Check(err)
project.Owner = matches[1]
}
return
}
示例6: compare
/*
$ gh compare refactor
> open https://github.com/CURRENT_REPO/compare/refactor
$ gh compare 1.0..1.1
> open https://github.com/CURRENT_REPO/compare/1.0...1.1
$ gh compare -u other-user patch
> open https://github.com/other-user/REPO/compare/patch
*/
func compare(command *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
var (
branch *github.Branch
project *github.Project
r string
)
branch, project, err = localRepo.RemoteBranchAndProject("", false)
utils.Check(err)
usageHelp := func() {
utils.Check(fmt.Errorf("Usage: hub compare [-u] [-b <BASE>] [<USER>] [[<START>...]<END>]"))
}
if args.IsParamsEmpty() {
if branch == nil ||
(branch.IsMaster() && flagCompareBase == "") ||
(flagCompareBase == branch.ShortName()) {
usageHelp()
} else {
r = branch.ShortName()
if flagCompareBase != "" {
r = parseCompareRange(flagCompareBase + "..." + r)
}
}
} else {
if flagCompareBase != "" {
usageHelp()
} else {
r = parseCompareRange(args.RemoveParam(args.ParamsSize() - 1))
if args.IsParamsEmpty() {
project, err = localRepo.CurrentProject()
utils.Check(err)
} else {
project = github.NewProject(args.RemoveParam(args.ParamsSize()-1), "", "")
}
}
}
if project == nil {
project, err = localRepo.CurrentProject()
utils.Check(err)
}
subpage := utils.ConcatPaths("compare", rangeQueryEscape(r))
url := project.WebURL("", "", subpage)
launcher, err := utils.BrowserLauncher()
utils.Check(err)
if flagCompareURLOnly {
args.Replace("echo", url)
} else {
args.Replace(launcher[0], "", launcher[1:]...)
args.AppendParams(url)
}
}
示例7: create
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() {
dirName, err := git.WorkdirName()
utils.Check(err)
newRepoName = github.SanitizeProjectName(dirName)
} 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)
if gh.IsRepositoryExist(project) {
ui.Errorln("Existing repository detected. Updating git remote")
} else {
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.Before("git", "remote", "add", "-f", "origin", url)
}
webUrl := project.WebURL("", "", "")
args.NoForward()
printBrowseOrCopy(args, webUrl, flagCreateBrowse, flagCreateCopy)
}
示例8: fork
func fork(cmd *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
if err != nil {
utils.Check(fmt.Errorf("Error: repository under 'origin' remote is not a GitHub project"))
}
config := github.CurrentConfig()
host, err := config.PromptForHost(project.Host)
if err != nil {
utils.Check(github.FormatError("forking repository", err))
}
originRemote, err := localRepo.OriginRemote()
if err != nil {
utils.Check(fmt.Errorf("Error creating fork: %s", err))
}
forkProject := github.NewProject(host.User, project.Name, project.Host)
newRemoteName := forkProject.Owner
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 || !project.SameAs(parentURL.Project) {
err = fmt.Errorf("Error creating fork: %s already exists on %s",
forkProject, forkProject.Host)
utils.Check(err)
}
} else {
if !args.Noop {
newRepo, err := client.ForkRepository(project)
utils.Check(err)
forkProject.Owner = newRepo.Owner.Login
forkProject.Name = newRepo.Name
}
}
args.NoForward()
if !flagForkNoRemote {
originURL := originRemote.URL.String()
url := forkProject.GitURL("", "", true)
args.Before("git", "remote", "add", "-f", newRemoteName, originURL)
args.Before("git", "remote", "set-url", newRemoteName, url)
args.AfterFn(func() error {
ui.Printf("new remote: %s\n", newRemoteName)
return nil
})
}
}
示例9: transformMergeArgs
func transformMergeArgs(args *Args) error {
words := args.Words()
if len(words) == 0 {
return nil
}
mergeURL := words[0]
url, err := github.ParseURL(mergeURL)
if err != nil {
return nil
}
pullURLRegex := regexp.MustCompile("^pull/(\\d+)")
projectPath := url.ProjectPath()
if !pullURLRegex.MatchString(projectPath) {
return nil
}
id := pullURLRegex.FindStringSubmatch(projectPath)[1]
gh := github.NewClient(url.Project.Host)
pullRequest, err := gh.PullRequest(url.Project, id)
if err != nil {
return err
}
repo, err := github.LocalRepo()
if err != nil {
return err
}
remote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
if err != nil {
return err
}
branch := pullRequest.Head.Ref
headRepo := pullRequest.Head.Repo
if headRepo == nil {
return fmt.Errorf("Error: that fork is not available anymore")
}
args.Before("git", "fetch", remote.Name, fmt.Sprintf("refs/pull/%s/head", id))
// Remove pull request URL
idx := args.IndexOfParam(mergeURL)
args.RemoveParam(idx)
mergeMsg := fmt.Sprintf("Merge pull request #%s from %s/%s\n\n%s", id, headRepo.Owner.Login, branch, pullRequest.Title)
args.AppendParams("FETCH_HEAD", "-m", mergeMsg)
if args.IndexOfParam("--ff-only") == -1 && args.IndexOfParam("--squash") == -1 && args.IndexOfParam("--ff") == -1 {
i := args.IndexOfParam("-m")
args.InsertParam(i, "--no-ff")
}
return nil
}
示例10: transformRemoteArgs
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 {
dirName, err := git.WorkdirName()
utils.Check(err)
repoName = github.SanitizeProjectName(dirName)
}
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)
}
示例11: ciStatus
func ciStatus(cmd *Command, args *Args) {
ref := "HEAD"
if !args.IsParamsEmpty() {
ref = args.RemoveParam(0)
}
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.MainProject()
utils.Check(err)
sha, err := git.Ref(ref)
if err != nil {
err = fmt.Errorf("Aborted: no revision could be determined from '%s'", ref)
}
utils.Check(err)
if args.Noop {
ui.Printf("Would request CI status for %s\n", sha)
} else {
gh := github.NewClient(project.Host)
response, err := gh.FetchCIStatus(project, sha)
utils.Check(err)
state := response.State
if len(response.Statuses) == 0 {
state = ""
}
var exitCode int
switch state {
case "success":
exitCode = 0
case "failure", "error":
exitCode = 1
case "pending":
exitCode = 2
default:
exitCode = 3
}
if flagCiStatusVerbose && len(response.Statuses) > 0 {
verboseFormat(response.Statuses)
} else {
if state != "" {
ui.Println(state)
} else {
ui.Println("no status")
}
}
os.Exit(exitCode)
}
}
示例12: getOnwerRepo
func getOnwerRepo() (owner string, name string, err error) {
repo, err := github.LocalRepo()
if err != nil {
return
}
project, err := repo.CurrentProject()
if err != nil {
return
}
return project.Owner, project.Name, err
}
示例13: runInLocalRepo
func runInLocalRepo(fn func(localRepo *github.GitHubRepo, project *github.Project, client *github.Client)) {
localRepo, err := github.LocalRepo()
utils.Check(err)
project, err := localRepo.CurrentProject()
utils.Check(err)
client := github.NewClient(project.Host)
fn(localRepo, project, client)
os.Exit(0)
}
示例14: parseCherryPickProjectAndSha
func parseCherryPickProjectAndSha(ref string) (project *github.Project, sha string, isPrivate bool) {
shaRe := "[a-f0-9]{7,40}"
var mainProject *github.Project
localRepo, mainProjectErr := github.LocalRepo()
if mainProjectErr == nil {
mainProject, mainProjectErr = localRepo.MainProject()
}
url, err := github.ParseURL(ref)
if err == nil {
projectPath := url.ProjectPath()
commitRegex := regexp.MustCompile(fmt.Sprintf("^commit/(%s)", shaRe))
if matches := commitRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
sha = matches[1]
project = url.Project
return
}
pullRegex := regexp.MustCompile(fmt.Sprintf(`^pull/(\d+)/commits/(%s)`, shaRe))
if matches := pullRegex.FindStringSubmatch(projectPath); len(matches) > 0 {
pullId := matches[1]
sha = matches[2]
utils.Check(mainProjectErr)
api := github.NewClient(mainProject.Host)
pullRequest, err := api.PullRequest(url.Project, pullId)
utils.Check(err)
headRepo := pullRequest.Head.Repo
project = github.NewProject(headRepo.Owner.Login, headRepo.Name, mainProject.Host)
isPrivate = headRepo.Private
return
}
}
ownerWithShaRegexp := regexp.MustCompile(fmt.Sprintf("^(%s)@(%s)$", OwnerRe, shaRe))
if matches := ownerWithShaRegexp.FindStringSubmatch(ref); len(matches) > 0 {
utils.Check(mainProjectErr)
project = mainProject
project.Owner = matches[1]
sha = matches[2]
}
return
}
示例15: compare
/*
$ gh compare refactor
> open https://github.com/CURRENT_REPO/compare/refactor
$ gh compare 1.0..1.1
> open https://github.com/CURRENT_REPO/compare/1.0...1.1
$ gh compare -u other-user patch
> open https://github.com/other-user/REPO/compare/patch
*/
func compare(command *Command, args *Args) {
localRepo, err := github.LocalRepo()
utils.Check(err)
var (
branch *github.Branch
project *github.Project
r string
)
branch, project, err = localRepo.RemoteBranchAndProject("", false)
utils.Check(err)
if args.IsParamsEmpty() {
if branch != nil && !branch.IsMaster() {
r = branch.ShortName()
} else {
err = fmt.Errorf("Usage: hub compare [USER] [<START>...]<END>")
utils.Check(err)
}
} else {
r = parseCompareRange(args.RemoveParam(args.ParamsSize() - 1))
if args.IsParamsEmpty() {
project, err = localRepo.CurrentProject()
utils.Check(err)
} else {
project = github.NewProject(args.RemoveParam(args.ParamsSize()-1), "", "")
}
}
subpage := utils.ConcatPaths("compare", rangeQueryEscape(r))
url := project.WebURL("", "", subpage)
launcher, err := utils.BrowserLauncher()
utils.Check(err)
if flagCompareURLOnly {
args.Replace("echo", url)
} else {
args.Replace(launcher[0], "", launcher[1:]...)
args.AppendParams(url)
}
}