Skip to content

Commit acce6c8

Browse files
committed
improvements
1 parent 2c1e88a commit acce6c8

File tree

4 files changed

+33
-93
lines changed

4 files changed

+33
-93
lines changed

modules/git/repo_compare.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
package git
66

77
import (
8-
"bufio"
98
"bytes"
109
"context"
11-
"errors"
1210
"fmt"
1311
"io"
14-
"os"
15-
"path/filepath"
1612
"regexp"
1713
"strconv"
1814
"strings"
@@ -193,8 +189,6 @@ func GetDiffShortStatByCmdArgs(ctx context.Context, repoPath string, trustedArgs
193189
var shortStatFormat = regexp.MustCompile(
194190
`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`)
195191

196-
var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`)
197-
198192
func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) {
199193
if len(stdout) == 0 || stdout == "\n" {
200194
return 0, 0, 0, nil
@@ -282,25 +276,3 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err
282276

283277
return split, err
284278
}
285-
286-
// ReadPatchCommit will check if a diff patch exists and return stats
287-
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
288-
// Migrated repositories download patches to "pulls" location
289-
patchFile := fmt.Sprintf("pulls/%d.patch", prID)
290-
loadPatch, err := os.Open(filepath.Join(repo.Path, patchFile))
291-
if err != nil {
292-
return "", err
293-
}
294-
defer loadPatch.Close()
295-
// Read only the first line of the patch - usually it contains the first commit made in patch
296-
scanner := bufio.NewScanner(loadPatch)
297-
scanner.Scan()
298-
// Parse the Patch stats, sometimes Migration returns a 404 for the patch file
299-
commitSHAGroups := patchCommits.FindStringSubmatch(scanner.Text())
300-
if len(commitSHAGroups) != 0 {
301-
commitSHA = commitSHAGroups[1]
302-
} else {
303-
return "", errors.New("patch file doesn't contain valid commit ID")
304-
}
305-
return commitSHA, nil
306-
}

modules/git/repo_compare_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,6 @@ func TestGetFormatPatch(t *testing.T) {
4545
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
4646
}
4747

48-
func TestReadPatch(t *testing.T) {
49-
// Ensure we can read the patch files
50-
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
51-
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
52-
if err != nil {
53-
assert.NoError(t, err)
54-
return
55-
}
56-
defer repo.Close()
57-
// This patch doesn't exist
58-
noFile, err := repo.ReadPatchCommit(0)
59-
assert.Error(t, err)
60-
61-
// This patch is an empty one (sometimes it's a 404)
62-
noCommit, err := repo.ReadPatchCommit(1)
63-
assert.Error(t, err)
64-
65-
// This patch is legit and should return a commit
66-
oldCommit, err := repo.ReadPatchCommit(2)
67-
if err != nil {
68-
assert.NoError(t, err)
69-
return
70-
}
71-
72-
assert.Empty(t, noFile)
73-
assert.Empty(t, noCommit)
74-
assert.Len(t, oldCommit, 40)
75-
assert.Equal(t, "6e8e2a6f9efd71dbe6917816343ed8415ad696c3", oldCommit)
76-
}
77-
7848
func TestReadWritePullHead(t *testing.T) {
7949
// Ensure we can write SHA1 head corresponding to PR and open them
8050
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")

routers/web/repo/pull.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -210,46 +210,17 @@ func GetPullDiffStats(ctx *context.Context) {
210210
ctx.Data["DiffShortStat"] = diffShortStat
211211
}
212212

213-
func calcMergeBase(ctx *context.Context, pull *issues_model.PullRequest) string {
214-
var commitSHA, parentCommit string
215-
// If there is a head or a patch file, and it is readable, grab info
216-
commitSHA, err := ctx.Repo.GitRepo.GetRefCommitID(pull.GetGitRefName())
217-
if err != nil {
218-
// Head File does not exist, try the patch
219-
// FIXME: it seems this patch file is not used in the code, but it is still read
220-
commitSHA, err = ctx.Repo.GitRepo.ReadPatchCommit(pull.Index)
221-
if err == nil {
222-
// Recreate pull head in files for next time
223-
if err := git.UpdateRef(ctx, ctx.Repo.GitRepo.Path, pull.GetGitRefName(), commitSHA); err != nil {
224-
log.Error("Could not write head file", err)
225-
}
226-
} else {
227-
// There is no history available
228-
log.Trace("No history file available for PR %d", pull.Index)
229-
}
230-
}
231-
if commitSHA != "" {
232-
// Get immediate parent of the first commit in the patch, grab history back
233-
parentCommit, _, err = git.NewCommand("rev-list", "-1", "--skip=1").AddDynamicArguments(commitSHA).RunStdString(ctx, &git.RunOpts{Dir: ctx.Repo.GitRepo.Path})
234-
if err == nil {
235-
parentCommit = strings.TrimSpace(parentCommit)
236-
}
237-
// Special case on Git < 2.25 that doesn't fail on immediate empty history
238-
if err != nil || parentCommit == "" {
239-
log.Info("No known parent commit for PR %d, error: %v", pull.Index, err)
240-
// bring at least partial history if it can work
241-
parentCommit = commitSHA
242-
}
243-
}
244-
return parentCommit
245-
}
246-
247213
func GetMergedBaseCommitID(ctx *context.Context, pull *issues_model.PullRequest) string {
248214
if pull.MergeBase != "" {
249215
return pull.MergeBase
250216
}
251217

252-
return calcMergeBase(ctx, pull)
218+
var err error
219+
pull.MergeBase, err = pull_service.CalcMergeBase(ctx, pull)
220+
if err != nil {
221+
log.Error("CalcMergeBase: %v", err)
222+
}
223+
return pull.MergeBase
253224
}
254225

255226
func preparePullViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.CompareInfo {

services/pull/merge_merge.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
package pull
55

66
import (
7+
"context"
8+
"strings"
9+
10+
issues_model "code.gitea.io/gitea/models/issues"
711
repo_model "code.gitea.io/gitea/models/repo"
812
"code.gitea.io/gitea/modules/git"
913
"code.gitea.io/gitea/modules/log"
@@ -23,3 +27,26 @@ func doMergeStyleMerge(ctx *mergeContext, message string) error {
2327
}
2428
return nil
2529
}
30+
31+
// CalcMergeBase calculates the merge base for a pull request.
32+
func CalcMergeBase(ctx context.Context, pr *issues_model.PullRequest) (string, error) {
33+
repoPath := pr.BaseRepo.RepoPath()
34+
if pr.HasMerged {
35+
mergeBase, _, err := git.NewCommand("merge-base").AddDashesAndList(pr.MergedCommitID+"^", pr.GetGitRefName()).
36+
RunStdString(ctx, &git.RunOpts{Dir: repoPath})
37+
return strings.TrimSpace(mergeBase), err
38+
}
39+
40+
mergeBase, _, err := git.NewCommand("merge-base").AddDashesAndList(pr.BaseBranch, pr.GetGitRefName()).
41+
RunStdString(ctx, &git.RunOpts{Dir: repoPath})
42+
if err != nil {
43+
var err2 error
44+
mergeBase, _, err2 = git.NewCommand("rev-parse").AddDynamicArguments(git.BranchPrefix+pr.BaseBranch).
45+
RunStdString(ctx, &git.RunOpts{Dir: repoPath})
46+
if err2 != nil {
47+
log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, err, err2)
48+
return "", err2
49+
}
50+
}
51+
return strings.TrimSpace(mergeBase), nil
52+
}

0 commit comments

Comments
 (0)