Skip to content

Commit ca30698

Browse files
authored
Change how merged PR commit info are prepared (#3368)
* Change how merged PR commits and diff are made * Update code.gitea.io/git dependency * Fix typo * Remove unneeded local variable
1 parent b0d5bb9 commit ca30698

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

models/pull.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func (pr *PullRequest) GetDefaultSquashMessage() string {
132132
return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index)
133133
}
134134

135+
// GetGitRefName returns git ref for hidden pull request branch
136+
func (pr *PullRequest) GetGitRefName() string {
137+
return fmt.Sprintf("refs/pull/%d/head", pr.Index)
138+
}
139+
135140
// APIFormat assumes following fields have been assigned with valid values:
136141
// Required - Issue
137142
// Optional - Merger
@@ -562,7 +567,7 @@ func (pr *PullRequest) getMergeCommit() (*git.Commit, error) {
562567
indexTmpPath := filepath.Join(os.TempDir(), "gitea-"+pr.BaseRepo.Name+"-"+strconv.Itoa(time.Now().Nanosecond()))
563568
defer os.Remove(indexTmpPath)
564569

565-
headFile := fmt.Sprintf("refs/pull/%d/head", pr.Index)
570+
headFile := pr.GetGitRefName()
566571

567572
// Check if a pull request is merged into BaseBranch
568573
_, stderr, err := process.GetManager().ExecDirEnv(-1, "", fmt.Sprintf("isMerged (git merge-base --is-ancestor): %d", pr.BaseRepo.ID),
@@ -980,7 +985,7 @@ func (pr *PullRequest) UpdatePatch() (err error) {
980985
// corresponding branches of base repository.
981986
// FIXME: Only push branches that are actually updates?
982987
func (pr *PullRequest) PushToBaseRepo() (err error) {
983-
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo 'refs/pull/%d/head'", pr.BaseRepoID, pr.Index)
988+
log.Trace("PushToBaseRepo[%d]: pushing commits to base repo '%s'", pr.BaseRepoID, pr.GetGitRefName())
984989

985990
headRepoPath := pr.HeadRepo.RepoPath()
986991
headGitRepo, err := git.OpenRepository(headRepoPath)
@@ -995,7 +1000,7 @@ func (pr *PullRequest) PushToBaseRepo() (err error) {
9951000
// Make sure to remove the remote even if the push fails
9961001
defer headGitRepo.RemoveRemote(tmpRemoteName)
9971002

998-
headFile := fmt.Sprintf("refs/pull/%d/head", pr.Index)
1003+
headFile := pr.GetGitRefName()
9991004

10001005
// Remove head in case there is a conflict.
10011006
file := path.Join(pr.BaseRepo.RepoPath(), headFile)

routers/repo/pull.go

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -253,40 +253,30 @@ func setMergeTarget(ctx *context.Context, pull *models.PullRequest) {
253253
}
254254

255255
// PrepareMergedViewPullInfo show meta information for a merged pull request view page
256-
func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) {
256+
func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.PullRequestInfo {
257257
pull := issue.PullRequest
258258

259-
var err error
260-
if err = pull.GetHeadRepo(); err != nil {
261-
ctx.ServerError("GetHeadRepo", err)
262-
return
263-
}
264-
265259
setMergeTarget(ctx, pull)
266260
ctx.Data["HasMerged"] = true
267261

268-
mergedCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
269-
if err != nil {
270-
ctx.ServerError("GetCommit", err)
271-
return
272-
}
273-
// the ID of the last commit in the PR (not including the merge commit)
274-
endCommitID, err := mergedCommit.ParentID(mergedCommit.ParentCount() - 1)
275-
if err != nil {
276-
ctx.ServerError("ParentID", err)
277-
return
278-
}
262+
prInfo, err := ctx.Repo.GitRepo.GetPullRequestInfo(ctx.Repo.Repository.RepoPath(),
263+
pull.MergeBase, pull.GetGitRefName())
279264

280-
ctx.Data["NumCommits"], err = ctx.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, endCommitID.String())
281-
if err != nil {
282-
ctx.ServerError("Repo.GitRepo.CommitsCountBetween", err)
283-
return
284-
}
285-
ctx.Data["NumFiles"], err = ctx.Repo.GitRepo.FilesCountBetween(pull.MergeBase, endCommitID.String())
286265
if err != nil {
287-
ctx.ServerError("Repo.GitRepo.FilesCountBetween", err)
288-
return
266+
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
267+
ctx.Data["IsPullReuqestBroken"] = true
268+
ctx.Data["BaseTarget"] = "deleted"
269+
ctx.Data["NumCommits"] = 0
270+
ctx.Data["NumFiles"] = 0
271+
return nil
272+
}
273+
274+
ctx.ServerError("GetPullRequestInfo", err)
275+
return nil
289276
}
277+
ctx.Data["NumCommits"] = prInfo.Commits.Len()
278+
ctx.Data["NumFiles"] = prInfo.NumFiles
279+
return prInfo
290280
}
291281

292282
// PrepareViewPullInfo show meta information for a pull request preview page
@@ -351,28 +341,16 @@ func ViewPullCommits(ctx *context.Context) {
351341

352342
var commits *list.List
353343
if pull.HasMerged {
354-
PrepareMergedViewPullInfo(ctx, issue)
344+
prInfo := PrepareMergedViewPullInfo(ctx, issue)
355345
if ctx.Written() {
356346
return
347+
} else if prInfo == nil {
348+
ctx.NotFound("ViewPullCommits", nil)
349+
return
357350
}
358351
ctx.Data["Username"] = ctx.Repo.Owner.Name
359352
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
360-
361-
mergedCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
362-
if err != nil {
363-
ctx.ServerError("Repo.GitRepo.GetCommit", err)
364-
return
365-
}
366-
endCommitID, err := mergedCommit.ParentID(mergedCommit.ParentCount() - 1)
367-
if err != nil {
368-
ctx.ServerError("ParentID", err)
369-
return
370-
}
371-
commits, err = ctx.Repo.GitRepo.CommitsBetweenIDs(endCommitID.String(), pull.MergeBase)
372-
if err != nil {
373-
ctx.ServerError("Repo.GitRepo.CommitsBetweenIDs", err)
374-
return
375-
}
353+
commits = prInfo.Commits
376354
} else {
377355
prInfo := PrepareViewPullInfo(ctx, issue)
378356
if ctx.Written() {
@@ -415,25 +393,25 @@ func ViewPullFiles(ctx *context.Context) {
415393

416394
var headTarget string
417395
if pull.HasMerged {
418-
PrepareMergedViewPullInfo(ctx, issue)
396+
prInfo := PrepareMergedViewPullInfo(ctx, issue)
419397
if ctx.Written() {
420398
return
399+
} else if prInfo == nil {
400+
ctx.NotFound("ViewPullFiles", nil)
401+
return
421402
}
422403

423404
diffRepoPath = ctx.Repo.GitRepo.Path
424-
startCommitID = pull.MergeBase
425-
mergedCommit, err := ctx.Repo.GitRepo.GetCommit(pull.MergedCommitID)
426-
if err != nil {
427-
ctx.ServerError("GetCommit", err)
428-
return
429-
}
430-
endCommitSha, err := mergedCommit.ParentID(mergedCommit.ParentCount() - 1)
405+
gitRepo = ctx.Repo.GitRepo
406+
407+
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
431408
if err != nil {
432-
ctx.ServerError("ParentID", err)
409+
ctx.ServerError("GetRefCommitID", err)
433410
return
434411
}
435-
endCommitID = endCommitSha.String()
436-
gitRepo = ctx.Repo.GitRepo
412+
413+
startCommitID = prInfo.MergeBase
414+
endCommitID = headCommitID
437415

438416
headTarget = path.Join(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
439417
ctx.Data["Username"] = ctx.Repo.Owner.Name

vendor/code.gitea.io/git/repo_commit.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"ignore": "test appengine",
44
"package": [
55
{
6-
"checksumSHA1": "1WHdGmDRsFRTD5N69l+MEbZr+nM=",
6+
"checksumSHA1": "Gz+a5Qo4PCiB/Gf2f02v8HEAxDM=",
77
"path": "code.gitea.io/git",
8-
"revision": "f4a91053671bee69f1995e456c1541668717c19d",
9-
"revisionTime": "2018-01-07T06:11:05Z"
8+
"revision": "6798d0f202cdc7187c00a467b586a4bdee27e8c9",
9+
"revisionTime": "2018-01-14T14:37:32Z"
1010
},
1111
{
1212
"checksumSHA1": "Qtq0kW+BnpYMOriaoCjMa86WGG8=",

0 commit comments

Comments
 (0)