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 <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/334
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: khmarbaise <khmarbaise@noreply.gitea.io>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin 2021-03-05 18:27:09 +08:00 committed by 6543
parent 0d98cbd657
commit d474883e90
2 changed files with 8 additions and 26 deletions

View File

@ -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
}

View File

@ -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 <remote> <branch>")
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
}