From 750f8fa43ef51d720a9d3e4f44538dfc450e3962 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Thu, 17 Dec 2020 18:50:53 +0100 Subject: [PATCH 1/3] dont create local branch for remotes if matching branch exists, use it, otherwise use remote tracking branch. this avoids cluttering the local repo too much. --- modules/git/branch.go | 5 ++--- modules/task/pull_checkout.go | 26 +++++++++++--------------- modules/task/pull_clean.go | 4 +++- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/modules/git/branch.go b/modules/git/branch.go index f359868..31c1304 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -38,13 +38,12 @@ func (r TeaRepo) TeaCreateBranch(localBranchName, remoteBranchName, remoteName s } // TeaCheckout checks out the given branch in the worktree. -func (r TeaRepo) TeaCheckout(branchName string) error { +func (r TeaRepo) TeaCheckout(ref git_plumbing.ReferenceName) error { tree, err := r.Worktree() if err != nil { return err } - localBranchRefName := git_plumbing.NewBranchReferenceName(branchName) - return tree.Checkout(&git.CheckoutOptions{Branch: localBranchRefName}) + return tree.Checkout(&git.CheckoutOptions{Branch: ref}) } // TeaDeleteLocalBranch removes the given branch locally diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index fda9ec3..c7c2f93 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -11,6 +11,7 @@ import ( local_git "code.gitea.io/tea/modules/git" "github.com/go-git/go-git/v5" + git_plumbing "github.com/go-git/go-git/v5/plumbing" ) // PullCheckout checkout current workdir to the head branch of specified pull request @@ -40,12 +41,6 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, remoteURL = pr.Head.Repository.SSHURL } - // try to find a matching existing branch, otherwise return branch in pulls/ namespace - localBranchName := fmt.Sprintf("pulls/%v-%v", index, pr.Head.Ref) - if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil { - localBranchName = b.Name - } - newRemoteName := fmt.Sprintf("pulls/%v", pr.Head.Repository.Owner.UserName) // verify related remote is in local repo, otherwise add it @@ -72,15 +67,16 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, return err } - // checkout local branch - err = localRepo.TeaCreateBranch(localBranchName, pr.Head.Ref, localRemoteName) - if err == nil { - fmt.Printf("Created branch '%s'\n", localBranchName) - } else if err == git.ErrBranchExists { - fmt.Println("There may be changes since you last checked out, run `git pull` to get them.") - } else if err != nil { - return err + // try to find a matching existing branch, otherwise use the remote tracking branch + localRef := git_plumbing.NewRemoteReferenceName(localRemoteName, pr.Head.Ref) + info := fmt.Sprintf( + "Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s", + localRef.String(), pr.Head.Ref) + if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil { + localRef = git_plumbing.NewBranchReferenceName(b.Name) + info = fmt.Sprintf("Found matching local branch %s, checking it out", localRef.Short()) } - return localRepo.TeaCheckout(localBranchName) + fmt.Println(info) + return localRepo.TeaCheckout(localRef) } diff --git a/modules/task/pull_clean.go b/modules/task/pull_clean.go index 775748b..bcefbbc 100644 --- a/modules/task/pull_clean.go +++ b/modules/task/pull_clean.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/sdk/gitea" git_config "github.com/go-git/go-git/v5/config" + git_plumbing "github.com/go-git/go-git/v5/plumbing" ) // PullClean deletes local & remote feature-branches for a closed pull @@ -76,7 +77,8 @@ call me again with the --ignore-sha flag`, remoteBranch) } if headRef.Name().Short() == branch.Name { fmt.Printf("Checking out '%s' to delete local branch '%s'\n", defaultBranch, branch.Name) - if err = r.TeaCheckout(defaultBranch); err != nil { + ref := git_plumbing.NewBranchReferenceName(defaultBranch) + if err = r.TeaCheckout(ref); err != nil { return err } } -- 2.40.1 From 039a5036ad47773383bccd3f8a88a01b1f0a2d11 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Sat, 19 Dec 2020 11:17:57 +0100 Subject: [PATCH 2/3] add --branch flag --- cmd/pulls/checkout.go | 10 ++++++-- modules/task/pull_checkout.go | 45 ++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cmd/pulls/checkout.go b/cmd/pulls/checkout.go index 4008cb0..2876bed 100644 --- a/cmd/pulls/checkout.go +++ b/cmd/pulls/checkout.go @@ -24,7 +24,13 @@ var CmdPullsCheckout = cli.Command{ Description: `Locally check out the given PR`, Action: runPullsCheckout, ArgsUsage: "", - Flags: flags.AllDefaultFlags, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "branch", + Aliases: []string{"b"}, + Usage: "Create a local branch if it doesn't exist yet", + }, + }, flags.AllDefaultFlags...), } func runPullsCheckout(cmd *cli.Context) error { @@ -38,5 +44,5 @@ func runPullsCheckout(cmd *cli.Context) error { return err } - return task.PullCheckout(ctx.Login, ctx.Owner, ctx.Repo, idx, interact.PromptPassword) + return task.PullCheckout(ctx, idx, interact.PromptPassword) } diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index c7c2f93..a8d50e9 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -7,7 +7,7 @@ package task import ( "fmt" - "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/context" local_git "code.gitea.io/tea/modules/git" "github.com/go-git/go-git/v5" @@ -15,8 +15,9 @@ import ( ) // PullCheckout checkout current workdir to the head branch of specified pull request -func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, callback func(string) (string, error)) error { - client := login.Client() +func PullCheckout(ctx *context.TeaContext, index int64, callback func(string) (string, error)) error { + client := ctx.Login.Client() + forceCreateBranch := ctx.Bool("branch") localRepo, err := local_git.RepoForWorkdir() if err != nil { @@ -24,7 +25,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, } // fetch PR source-localRepo & -branch from gitea - pr, _, err := client.GetPullRequest(repoOwner, repoName, index) + pr, _, err := client.GetPullRequest(ctx.Owner, ctx.Repo, index) if err != nil { return err } @@ -34,7 +35,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, } remoteURL := pr.Head.Repository.CloneURL - if len(login.SSHKey) != 0 { + if len(ctx.Login.SSHKey) != 0 { // login.SSHKey is nonempty, if user specified a key manually or we automatically // found a matching private key on this machine during login creation. // this means, we are very likely to have a working ssh setup. @@ -55,7 +56,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback) + auth, err := local_git.GetAuthForURL(url, ctx.Login.Token, ctx.Login.SSHKey, callback) if err != nil { return err } @@ -67,16 +68,32 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, return err } - // try to find a matching existing branch, otherwise use the remote tracking branch - localRef := git_plumbing.NewRemoteReferenceName(localRemoteName, pr.Head.Ref) - info := fmt.Sprintf( - "Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s", - localRef.String(), pr.Head.Ref) + var info string + var checkoutRef git_plumbing.ReferenceName + if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil { - localRef = git_plumbing.NewBranchReferenceName(b.Name) - info = fmt.Sprintf("Found matching local branch %s, checking it out", localRef.Short()) + // if a matching branch exists, use that + checkoutRef = git_plumbing.NewBranchReferenceName(b.Name) + info = fmt.Sprintf("Found matching local branch %s, checking it out", checkoutRef.Short()) + } else if forceCreateBranch { + // create a branch if wanted + localBranchName := fmt.Sprintf("pulls/%v-%v", index, pr.Head.Ref) + checkoutRef = git_plumbing.NewBranchReferenceName(localBranchName) + if err = localRepo.TeaCreateBranch(localBranchName, pr.Head.Ref, localRemoteName); err == nil { + info = fmt.Sprintf("Created branch '%s'\n", localBranchName) + } else if err == git.ErrBranchExists { + info = "There may be changes since you last checked out, run `git pull` to get them." + } else { + return err + } + } else { + // use the remote tracking branch + checkoutRef = git_plumbing.NewRemoteReferenceName(localRemoteName, pr.Head.Ref) + info = fmt.Sprintf( + "Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s", + checkoutRef.String(), pr.Head.Ref) } fmt.Println(info) - return localRepo.TeaCheckout(localRef) + return localRepo.TeaCheckout(checkoutRef) } -- 2.40.1 From 2d2c6e50889cdeb5b259745f24a8f422ba9c9979 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 1 Mar 2021 00:41:43 +0100 Subject: [PATCH 3/3] code review --- cmd/pulls/checkout.go | 2 +- modules/task/pull_checkout.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/pulls/checkout.go b/cmd/pulls/checkout.go index 2876bed..7dade3d 100644 --- a/cmd/pulls/checkout.go +++ b/cmd/pulls/checkout.go @@ -44,5 +44,5 @@ func runPullsCheckout(cmd *cli.Context) error { return err } - return task.PullCheckout(ctx, idx, interact.PromptPassword) + return task.PullCheckout(ctx.Login, ctx.Owner, ctx.Repo, ctx.Bool("branch"), idx, interact.PromptPassword) } diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index a8d50e9..573b11d 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -7,7 +7,7 @@ package task import ( "fmt" - "code.gitea.io/tea/modules/context" + "code.gitea.io/tea/modules/config" local_git "code.gitea.io/tea/modules/git" "github.com/go-git/go-git/v5" @@ -15,9 +15,8 @@ import ( ) // PullCheckout checkout current workdir to the head branch of specified pull request -func PullCheckout(ctx *context.TeaContext, index int64, callback func(string) (string, error)) error { - client := ctx.Login.Client() - forceCreateBranch := ctx.Bool("branch") +func PullCheckout(login *config.Login, repoOwner, repoName string, forceCreateBranch bool, index int64, callback func(string) (string, error)) error { + client := login.Client() localRepo, err := local_git.RepoForWorkdir() if err != nil { @@ -25,7 +24,7 @@ func PullCheckout(ctx *context.TeaContext, index int64, callback func(string) (s } // fetch PR source-localRepo & -branch from gitea - pr, _, err := client.GetPullRequest(ctx.Owner, ctx.Repo, index) + pr, _, err := client.GetPullRequest(repoOwner, repoName, index) if err != nil { return err } @@ -35,7 +34,7 @@ func PullCheckout(ctx *context.TeaContext, index int64, callback func(string) (s } remoteURL := pr.Head.Repository.CloneURL - if len(ctx.Login.SSHKey) != 0 { + if len(login.SSHKey) != 0 { // login.SSHKey is nonempty, if user specified a key manually or we automatically // found a matching private key on this machine during login creation. // this means, we are very likely to have a working ssh setup. @@ -56,7 +55,7 @@ func PullCheckout(ctx *context.TeaContext, index int64, callback func(string) (s if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, ctx.Login.Token, ctx.Login.SSHKey, callback) + auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback) if err != nil { return err } -- 2.40.1