Skip to content

Commit f8cea67

Browse files
committed
improvements
1 parent 345ae04 commit f8cea67

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

modules/git/commit.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,7 @@ func (c *Commit) CommitsBeforeLimit(num int) ([]*Commit, error) {
258258

259259
// CommitsBeforeUntil returns the commits between commitID to current revision
260260
func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) {
261-
endCommit, err := c.repo.GetCommit(commitID)
262-
if err != nil {
263-
return nil, err
264-
}
265-
return c.repo.CommitsBetween(c, endCommit)
261+
return c.repo.CommitsBetween(c.ID.String(), commitID)
266262
}
267263

268264
// SearchCommitsOptions specify the parameters for SearchCommits

modules/git/diff.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616

1717
"code.gitea.io/gitea/modules/log"
18-
"code.gitea.io/gitea/modules/util"
1918
)
2019

2120
// RawDiffType type of a raw diff.
@@ -109,15 +108,15 @@ func ParseDiffHunkString(diffHunk string) (leftLine, leftHunk, rightLine, rightH
109108
if len(leftRange) > 1 {
110109
leftHunk, _ = strconv.Atoi(leftRange[1])
111110
} else {
112-
leftHunk = util.Iif(leftLine > 0, leftLine, -leftLine)
111+
leftHunk = 1
113112
}
114113
if len(ranges) > 1 {
115114
rightRange := strings.Split(ranges[1], ",")
116115
rightLine, _ = strconv.Atoi(rightRange[0])
117116
if len(rightRange) > 1 {
118117
rightHunk, _ = strconv.Atoi(rightRange[1])
119118
} else {
120-
rightHunk = rightLine
119+
rightHunk = 1
121120
}
122121
} else {
123122
log.Debug("Parse line number failed: %v", diffHunk)

modules/git/diff_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ func TestParseDiffHunkString(t *testing.T) {
185185
leftLine, leftHunk, rightLine, rightHunk = ParseDiffHunkString("@@ -1 +0,0 @@")
186186
assert.Equal(t, 1, leftLine)
187187
assert.Equal(t, 1, leftHunk)
188-
assert.Equal(t, 0, rightLine)
188+
assert.Equal(t, 1, rightLine)
189189
assert.Equal(t, 0, rightHunk)
190+
191+
leftLine, leftHunk, rightLine, rightHunk = ParseDiffHunkString("@@ -2 +2 @@")
192+
assert.Equal(t, 2, leftLine)
193+
assert.Equal(t, 1, leftHunk)
194+
assert.Equal(t, 2, rightLine)
195+
assert.Equal(t, 1, rightHunk)
190196
}

modules/git/repo_commit.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66

77
import (
88
"bytes"
9+
"context"
910
"io"
1011
"os"
1112
"strconv"
@@ -304,23 +305,47 @@ func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (in
304305

305306
// CommitsBetween returns a list that contains commits between [before, last).
306307
// If before is detached (removed by reset + push) it is not included.
307-
func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) {
308+
func (repo *Repository) CommitsBetween(lastCommitID, beforeCommitID string) ([]*Commit, error) {
309+
commitIDs, err := CommitIDsBetween(repo.Ctx, repo.Path, beforeCommitID, lastCommitID)
310+
if err != nil {
311+
return nil, err
312+
}
313+
314+
commits := make([]*Commit, 0, len(commitIDs))
315+
for _, commitID := range commitIDs {
316+
commit, err := repo.GetCommit(commitID)
317+
if err != nil {
318+
return nil, err
319+
}
320+
commits = append(commits, commit)
321+
}
322+
return commits, nil
323+
}
324+
325+
func CommitIDsBetween(ctx context.Context, repoPath, beforeCommitID, afterCommitID string) ([]string, error) {
308326
var stdout []byte
309327
var err error
310-
if before == nil {
311-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
328+
if beforeCommitID == "" {
329+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(afterCommitID).RunStdBytes(ctx, &RunOpts{Dir: repoPath})
312330
} else {
313-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
331+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(beforeCommitID+".."+afterCommitID).RunStdBytes(ctx, &RunOpts{Dir: repoPath})
314332
if err != nil && strings.Contains(err.Error(), "no merge base") {
315333
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
316334
// previously it would return the results of git rev-list before last so let's try that...
317-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
335+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(beforeCommitID, afterCommitID).RunStdBytes(ctx, &RunOpts{Dir: repoPath})
318336
}
319337
}
320338
if err != nil {
321339
return nil, err
322340
}
323-
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
341+
342+
commitIDs := make([]string, 0, 10)
343+
for commitID := range bytes.SplitSeq(stdout, []byte{'\n'}) {
344+
if len(commitID) > 0 {
345+
commitIDs = append(commitIDs, string(commitID))
346+
}
347+
}
348+
return commitIDs, nil
324349
}
325350

326351
// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last)
@@ -375,18 +400,17 @@ func (repo *Repository) CommitsBetweenNotBase(last, before *Commit, baseBranch s
375400

376401
// CommitsBetweenIDs return commits between twoe commits
377402
func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) {
378-
lastCommit, err := repo.GetCommit(last)
403+
_, err := repo.GetCommit(last)
379404
if err != nil {
380405
return nil, err
381406
}
382-
if before == "" {
383-
return repo.CommitsBetween(lastCommit, nil)
384-
}
385-
beforeCommit, err := repo.GetCommit(before)
386-
if err != nil {
387-
return nil, err
407+
if before != "" {
408+
_, err := repo.GetCommit(before)
409+
if err != nil {
410+
return nil, err
411+
}
388412
}
389-
return repo.CommitsBetween(lastCommit, beforeCommit)
413+
return repo.CommitsBetween(last, before)
390414
}
391415

392416
// CommitsCountBetween return numbers of commits between two commits

services/issue/comments.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion
134134
func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) error {
135135
err := db.WithTx(ctx, func(ctx context.Context) error {
136136
return issues_model.DeleteComment(ctx, comment)
137+
// TODO: delete review if the comment is the last comment of the review
138+
// TODO: delete comment attachments
137139
})
138140
if err != nil {
139141
return err

services/pull/review.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"regexp"
13+
"slices"
1314
"sort"
1415
"strings"
1516

@@ -111,14 +112,23 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
111112
defer closer.Close()
112113
}
113114

114-
if beforeCommitID == "" {
115+
prCommitIDs, err := git.CommitIDsBetween(ctx, gitRepo.Path, beforeCommitID, afterCommitID)
116+
if err != nil {
117+
return nil, fmt.Errorf("CommitIDsBetween[%s, %s]: %w", beforeCommitID, afterCommitID, err)
118+
}
119+
120+
if beforeCommitID == "" || beforeCommitID == issue.PullRequest.MergeBase {
115121
beforeCommitID = issue.PullRequest.MergeBase
116122
} else {
117-
beforeCommit, err := gitRepo.GetCommit(beforeCommitID) // Ensure beforeCommitID is valid
123+
// beforeCommitID must be one of the pull request commits
124+
if !slices.Contains(prCommitIDs, beforeCommitID) {
125+
return nil, fmt.Errorf("beforeCommitID[%s] is not a valid pull request commit", beforeCommitID)
126+
}
127+
128+
beforeCommit, err := gitRepo.GetCommit(beforeCommitID)
118129
if err != nil {
119130
return nil, fmt.Errorf("GetCommit[%s]: %w", beforeCommitID, err)
120131
}
121-
// TODO: beforeCommitID must be one of the pull request commits
122132

123133
beforeCommit, err = beforeCommit.Parent(0)
124134
if err != nil {
@@ -131,8 +141,11 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
131141
if err != nil {
132142
return nil, fmt.Errorf("GetRefCommitID[%s]: %w", issue.PullRequest.GetGitHeadRefName(), err)
133143
}
134-
} else { //nolint
135-
// TODO: afterCommitID must be one of the pull request commits
144+
} else {
145+
// afterCommitID must be one of the pull request commits
146+
if !slices.Contains(prCommitIDs, afterCommitID) {
147+
return nil, fmt.Errorf("afterCommitID[%s] is not a valid pull request commit", afterCommitID)
148+
}
136149
}
137150

138151
// CreateCodeComment() is used for:

0 commit comments

Comments
 (0)