Find DefaultPRHead based on branch and SHA (#514)
Reviewed-on: https://gitea.com/gitea/tea/pulls/514 Reviewed-by: strk <strk@noreply.gitea.io> Reviewed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
parent
c1c7870ceb
commit
54b535a527
|
@ -173,6 +173,7 @@ func (r TeaRepo) TeaFindBranchByName(branchName, repoURL string) (b *git_config.
|
||||||
// TeaFindBranchRemote gives the first remote that has a branch with the same name or sha,
|
// TeaFindBranchRemote gives the first remote that has a branch with the same name or sha,
|
||||||
// depending on what is passed in.
|
// depending on what is passed in.
|
||||||
// This function is needed, as git does not always define branches in .git/config with remote entries.
|
// This function is needed, as git does not always define branches in .git/config with remote entries.
|
||||||
|
// Priority order is: first match of sha and branch -> first match of branch -> first match of sha
|
||||||
func (r TeaRepo) TeaFindBranchRemote(branchName, hash string) (*git.Remote, error) {
|
func (r TeaRepo) TeaFindBranchRemote(branchName, hash string) (*git.Remote, error) {
|
||||||
remotes, err := r.Remotes()
|
remotes, err := r.Remotes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,35 +194,57 @@ func (r TeaRepo) TeaFindBranchRemote(branchName, hash string) (*git.Remote, erro
|
||||||
}
|
}
|
||||||
defer iter.Close()
|
defer iter.Close()
|
||||||
|
|
||||||
var match *git.Remote
|
var shaMatch *git.Remote
|
||||||
err = iter.ForEach(func(ref *git_plumbing.Reference) error {
|
var branchMatch *git.Remote
|
||||||
|
var fullMatch *git.Remote
|
||||||
|
if err := iter.ForEach(func(ref *git_plumbing.Reference) error {
|
||||||
if ref.Name().IsRemote() {
|
if ref.Name().IsRemote() {
|
||||||
names := strings.SplitN(ref.Name().Short(), "/", 2)
|
names := strings.SplitN(ref.Name().Short(), "/", 2)
|
||||||
remote := names[0]
|
remote := names[0]
|
||||||
branch := names[1]
|
branch := names[1]
|
||||||
hashMatch := hash != "" && hash == ref.Hash().String()
|
if branchMatch == nil && branchName != "" && branchName == branch {
|
||||||
nameMatch := branchName != "" && branchName == branch
|
if branchMatch, err = r.Remote(remote); err != nil {
|
||||||
if hashMatch || nameMatch {
|
|
||||||
match, err = r.Remote(remote)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if shaMatch == nil && hash != "" && hash == ref.Hash().String() {
|
||||||
|
if shaMatch, err = r.Remote(remote); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fullMatch == nil && branchName != "" && branchName == branch && hash != "" && hash == ref.Hash().String() {
|
||||||
|
if fullMatch, err = r.Remote(remote); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// stop asap you have a full match
|
||||||
return nil
|
return nil
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return match, err
|
if fullMatch != nil {
|
||||||
|
return fullMatch, nil
|
||||||
|
} else if branchMatch != nil {
|
||||||
|
return branchMatch, nil
|
||||||
|
} else if shaMatch != nil {
|
||||||
|
return shaMatch, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TeaGetCurrentBranchName return the name of the branch witch is currently active
|
// TeaGetCurrentBranchNameAndSHA return the name and sha of the branch witch is currently active
|
||||||
func (r TeaRepo) TeaGetCurrentBranchName() (string, error) {
|
func (r TeaRepo) TeaGetCurrentBranchNameAndSHA() (string, string, error) {
|
||||||
localHead, err := r.Head()
|
localHead, err := r.Head()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !localHead.Name().IsBranch() {
|
if !localHead.Name().IsBranch() {
|
||||||
return "", fmt.Errorf("active ref is no branch")
|
return "", "", fmt.Errorf("active ref is no branch")
|
||||||
}
|
}
|
||||||
|
|
||||||
return localHead.Name().Short(), nil
|
return localHead.Name().Short(), localHead.Hash().String(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,11 +99,12 @@ func GetDefaultPRBase(login *config.Login, owner, repo string) (string, error) {
|
||||||
// that has a branch with the same name, and extracts the owner from its URL.
|
// 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.
|
// If no remote matches, owner is empty, meaning same as head repo owner.
|
||||||
func GetDefaultPRHead(localRepo *local_git.TeaRepo) (owner, branch string, err error) {
|
func GetDefaultPRHead(localRepo *local_git.TeaRepo) (owner, branch string, err error) {
|
||||||
if branch, err = localRepo.TeaGetCurrentBranchName(); err != nil {
|
var sha string
|
||||||
|
if branch, sha, err = localRepo.TeaGetCurrentBranchNameAndSHA(); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
remote, err := localRepo.TeaFindBranchRemote(branch, "")
|
remote, err := localRepo.TeaFindBranchRemote(branch, sha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("could not determine remote for current branch: %s", err)
|
err = fmt.Errorf("could not determine remote for current branch: %s", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue