tea pulls create: detect head branch repo owner (#193)

dont print on already pushed branch

more consisten error handling

detect repo owner for default head branch

Co-authored-by: Norwin Roosen <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/193
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Norwin
2020-09-24 10:44:03 +00:00
committed by 6543
parent 288a8574c3
commit 159bf03d49
2 changed files with 78 additions and 10 deletions

View File

@ -341,13 +341,20 @@ func runPullsCreate(ctx *cli.Context) error {
repo, _, err := login.Client().GetRepo(ownerArg, repoArg)
if err != nil {
log.Fatal(err)
log.Fatal("could not fetch repo meta: ", err)
}
// open local git repo
localRepo, err := local_git.RepoForWorkdir()
if err != nil {
log.Fatal(err)
log.Fatal("could not open local repo: ", err)
}
// push if possible
log.Println("git push")
err = localRepo.Push(&git.PushOptions{})
if err != nil && err != git.NoErrAlreadyUpToDate {
log.Printf("Error occurred during 'git push':\n%s\n", err.Error())
}
base := ctx.String("base")
@ -359,10 +366,35 @@ func runPullsCreate(ctx *cli.Context) error {
head := ctx.String("head")
// default is current one
if len(head) == 0 {
head, err = localRepo.TeaGetCurrentBranchName()
headBranch, err := localRepo.Head()
if err != nil {
log.Fatal(err)
}
sha := headBranch.Hash().String()
remote, err := localRepo.TeaFindBranchRemote("", sha)
if err != nil {
return err
}
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.
log.Fatal("no matching remote found for this branch. try git push -u <remote> <branch>")
}
branchName, err := localRepo.TeaGetCurrentBranchName()
if err != nil {
log.Fatal(err)
}
url, err := local_git.ParseURL(remote.Config().URLs[0])
if err != nil {
log.Fatal(err)
}
owner, _ := getOwnerAndRepo(strings.TrimLeft(url.Path, "/"), "")
head = fmt.Sprintf("%s:%s", owner, branchName)
}
title := ctx.String("title")
@ -382,12 +414,6 @@ func runPullsCreate(ctx *cli.Context) error {
return nil
}
// push if possible
err = localRepo.Push(&git.PushOptions{})
if err != nil {
fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error())
}
pr, _, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{
Head: head,
Base: base,
@ -396,7 +422,7 @@ func runPullsCreate(ctx *cli.Context) error {
})
if err != nil {
log.Fatal(err)
log.Fatal("could not create PR: ", err)
}
in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", pr.Index,