Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches not at ref commit ID should not be listed as Merged #9614

Merged
merged 16 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions routers/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/repofiles"
"code.gitea.io/gitea/modules/util"
"gopkg.in/src-d/go-git.v4/plumbing"
)

const (
Expand All @@ -33,6 +34,7 @@ type Branch struct {
CommitsAhead int
CommitsBehind int
LatestPullRequest *models.PullRequest
MergeMovedOn bool
}

// Branches render repository branch page
Expand Down Expand Up @@ -213,11 +215,54 @@ func loadBranches(ctx *context.Context) []*Branch {
ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
return nil
}
mergeMovedOn := false
if pr != nil {
if err := pr.LoadIssue(); err != nil {
ctx.ServerError("pr.LoadIssue", err)
return nil
}
if pr.BaseRepoID == ctx.Repo.Repository.ID {
pr.HeadRepo = ctx.Repo.Repository
} else if err := pr.LoadBaseRepo(); err != nil {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
ctx.ServerError("pr.LoadBaseRepo", err)
return nil
}
if pr.HeadRepoID == ctx.Repo.Repository.ID {
pr.HeadRepo = ctx.Repo.Repository
} else if err := pr.LoadHeadRepo(); err != nil {
ctx.ServerError("pr.LoadHeadRepo", err)
return nil
}
if pr.HasMerged && pr.HeadRepo != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can pr.HeadRepo be nil here?

Copy link
Contributor Author

@zeripath zeripath Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~~Well... not the way I'd written the code... ~~

It can now.

I'm an idiot. Of course it can't be...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 219 only gets the PRs by HeadInfo - that is those that are in the head repo. Therefore the head repo is always going to be the ctx.Repo.Repository.

headRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil
}
defer headRepo.Close()
baseRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil
}
defer baseRepo.Close()
pullCommit, err := baseRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil && err != plumbing.ErrReferenceNotFound {
ctx.ServerError("GetBranchCommitID", err)
return nil
}
if err == nil {
headCommit, err := headRepo.GetBranchCommitID(pr.HeadBranch)
if err != nil && err != plumbing.ErrReferenceNotFound {
ctx.ServerError("GetBranchCommitID", err)
return nil
} else if err == nil && headCommit != pullCommit {
// the head has moved on from the merge - we shouldn't delete
mergeMovedOn = true
}
}
}

}

isIncluded := divergence.Ahead == 0 && ctx.Repo.Repository.DefaultBranch != branchName
Expand All @@ -230,6 +275,7 @@ func loadBranches(ctx *context.Context) []*Branch {
CommitsAhead: divergence.Ahead,
CommitsBehind: divergence.Behind,
LatestPullRequest: pr,
MergeMovedOn: mergeMovedOn,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is an edge case and is covered elsewhere but, perhaps if we've got ErrReferenceNotFound we should hide the new PR button as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this referring to when I thought it was possible to not get the head branch commit id? We actually always know that's there because otherwise there would have been a ServerError at line 200. So that can't happen and I've optimised that check away.

OR

is this referring to if we can't find the PR pulls head in the base repo?

I'm not sure what to do in that case. If I try to enforce that the pulls head is actually there I suspect that will break a lot of integration tests - as our fixtures do not necessarily match the ground truth. I also expect that there is a race between the db and the presence of the pull head. (We should check what happens here - the index is needed to create the pull head - so that means that the db has to have a pull request committed before we can push to the pull head.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that MergeMovedOn, which has a restrictive meaning, is only set to true if both heads are found, but the restriction (which is to hide the New PR button) still should apply if any of the heads is missing (ErrReferenceNotFound). Maybe when any of the heads is missing it should also be set to true, but then its name should be changed because it will no longer reflect the exact scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand.

Perhaps some of what you're discussing is already fixed within the logic of the template?

If you don't have a PR already then merge moved on is of no consequence.

The head branch we already know exists because it's the branch we've provided.

If the base branch does not exist - that's a server error - although this won't show that. You shouldn't be able to delete a branch with PRs against it - (although I haven't checked that.) If the git pull head isn't there well... that's technically a server error but I think there could be a race between between a pull request being made and the ref being updated so I think we should allow it.

}
}

Expand Down
31 changes: 31 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
comment_service "code.gitea.io/gitea/services/comments"
issue_service "code.gitea.io/gitea/services/issue"
pull_service "code.gitea.io/gitea/services/pull"
"gopkg.in/src-d/go-git.v4/plumbing"

"github.com/unknwon/com"
)
Expand Down Expand Up @@ -966,6 +967,36 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["IsBlockedByRejection"] = pull.ProtectedBranch.MergeBlockedByRejectedReview(pull)
ctx.Data["GrantedApprovals"] = cnt
}
if pull.HasMerged && pull.HeadRepo != nil {
// && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch) {
headRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
defer headRepo.Close()
baseRepo, err := git.OpenRepository(pull.BaseRepo.RepoPath())
if err != nil {
ctx.ServerError("OpenRepository", err)
return
}
defer baseRepo.Close()
pullCommit, err := baseRepo.GetRefCommitID(pull.GetGitRefName())
if err != nil && err != plumbing.ErrReferenceNotFound {
ctx.ServerError("GetBranchCommitID", err)
return
}
if err == nil {
headCommit, err := headRepo.GetBranchCommitID(pull.HeadBranch)
if err != nil && err != plumbing.ErrReferenceNotFound {
ctx.ServerError("GetBranchCommitID", err)
return
} else if err == nil && headCommit != pullCommit {
// the head has moved on from the merge - we shouldn't delete
canDelete = false
}
}
}
ctx.Data["IsPullBranchDeletable"] = canDelete && pull.HeadRepo != nil && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)

ctx.Data["PullReviewers"], err = models.GetReviewersByIssueID(issue.ID)
Expand Down
6 changes: 6 additions & 0 deletions templates/repo/branch/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
</a>
{{end}}
{{else if and .LatestPullRequest.HasMerged .MergeMovedOn}}
{{if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}}
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
</a>
{{end}}
{{else}}
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}">#{{.LatestPullRequest.Issue.Index}}</a>
{{if .LatestPullRequest.HasMerged}}
Expand Down