From d474883e90e85d3688a3e314b65c1bbad94e0bf7 Mon Sep 17 00:00:00 2001 From: Norwin Date: Fri, 5 Mar 2021 18:27:09 +0800 Subject: [PATCH] don't push before creating a pull (#334) Not sure if this is the best way, but it's the simplest way to fix #333. Everything else is overly complex due to a chicken-egg problem: Knowing which remote / branch to push involves requires prompting the user, which requires to have a upstream branch pushed to detect default values. Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/334 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Reviewed-by: khmarbaise Co-authored-by: Norwin Co-committed-by: Norwin --- modules/git/branch.go | 2 +- modules/task/pull_create.go | 32 +++++++------------------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/modules/git/branch.go b/modules/git/branch.go index 31c1304..6fef68d 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -223,5 +223,5 @@ func (r TeaRepo) TeaGetCurrentBranchName() (string, error) { return "", fmt.Errorf("active ref is no branch") } - return strings.TrimPrefix(localHead.Name().String(), "refs/heads/"), nil + return localHead.Name().Short(), nil } diff --git a/modules/task/pull_create.go b/modules/task/pull_create.go index a8e0f1a..bedf019 100644 --- a/modules/task/pull_create.go +++ b/modules/task/pull_create.go @@ -13,8 +13,6 @@ import ( local_git "code.gitea.io/tea/modules/git" "code.gitea.io/tea/modules/print" "code.gitea.io/tea/modules/utils" - - "github.com/go-git/go-git/v5" ) // CreatePull creates a PR in the given repo and prints the result @@ -26,13 +24,6 @@ func CreatePull(login *config.Login, repoOwner, repoName, base, head, title, des return fmt.Errorf("Could not open local repo: %s", err) } - // push if possible - fmt.Println("git push") - err = localRepo.Push(&git.PushOptions{}) - if err != nil && err != git.NoErrAlreadyUpToDate { - fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error()) - } - // default is default branch if len(base) == 0 { base, err = GetDefaultPRBase(login, repoOwner, repoName) @@ -92,32 +83,23 @@ func GetDefaultPRBase(login *config.Login, owner, repo string) (string, error) { return meta.DefaultBranch, nil } -// GetDefaultPRHead uses the currently checked out branch, checks if -// a remote currently holds the commit it points to, extracts the owner -// from its URL, and assembles the result to a valid head spec for gitea. +// GetDefaultPRHead uses the currently checked out branch, tries to find a remote +// that has a branch with the same name, and extracts the owner from its URL. +// If no remote matches, owner is empty, meaning same as head repo owner. func GetDefaultPRHead(localRepo *local_git.TeaRepo) (owner, branch string, err error) { - headBranch, err := localRepo.Head() - if err != nil { + if branch, err = localRepo.TeaGetCurrentBranchName(); err != nil { return } - sha := headBranch.Hash().String() - remote, err := localRepo.TeaFindBranchRemote("", sha) + remote, err := localRepo.TeaFindBranchRemote(branch, "") if err != nil { err = fmt.Errorf("could not determine remote for current branch: %s", err) return } if remote == nil { - // if no remote branch is found for the local hash, we abort: - // user has probably not configured a remote for the local branch, - // or local branch does not represent remote state. - err = fmt.Errorf("no matching remote found for this branch. try git push -u ") - return - } - - branch, err = localRepo.TeaGetCurrentBranchName() - if err != nil { + // if no remote branch is found for the local branch, + // we leave owner empty, meaning "use same repo as head" to gitea. return }