Skip to content

Commit bc362ea

Browse files
authored
remove package code.gitea.io/gitea/modules/git import out of models (#11025)
1 parent 0a2cba9 commit bc362ea

File tree

6 files changed

+206
-205
lines changed

6 files changed

+206
-205
lines changed

models/pull.go

-200
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"strings"
1212

13-
"code.gitea.io/gitea/modules/git"
1413
"code.gitea.io/gitea/modules/log"
1514
"code.gitea.io/gitea/modules/setting"
1615
"code.gitea.io/gitea/modules/timeutil"
@@ -215,143 +214,6 @@ func (pr *PullRequest) GetDefaultMergeMessage() string {
215214
return fmt.Sprintf("Merge pull request '%s' (#%d) from %s:%s into %s", pr.Issue.Title, pr.Issue.Index, pr.HeadRepo.FullName(), pr.HeadBranch, pr.BaseBranch)
216215
}
217216

218-
// GetCommitMessages returns the commit messages between head and merge base (if there is one)
219-
func (pr *PullRequest) GetCommitMessages() string {
220-
if err := pr.LoadIssue(); err != nil {
221-
log.Error("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err)
222-
return ""
223-
}
224-
225-
if err := pr.Issue.LoadPoster(); err != nil {
226-
log.Error("Cannot load poster %d for pr id %d, index %d Error: %v", pr.Issue.PosterID, pr.ID, pr.Index, err)
227-
return ""
228-
}
229-
230-
if pr.HeadRepo == nil {
231-
var err error
232-
pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID)
233-
if err != nil {
234-
log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
235-
return ""
236-
}
237-
}
238-
239-
gitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
240-
if err != nil {
241-
log.Error("Unable to open head repository: Error: %v", err)
242-
return ""
243-
}
244-
defer gitRepo.Close()
245-
246-
headCommit, err := gitRepo.GetBranchCommit(pr.HeadBranch)
247-
if err != nil {
248-
log.Error("Unable to get head commit: %s Error: %v", pr.HeadBranch, err)
249-
return ""
250-
}
251-
252-
mergeBase, err := gitRepo.GetCommit(pr.MergeBase)
253-
if err != nil {
254-
log.Error("Unable to get merge base commit: %s Error: %v", pr.MergeBase, err)
255-
return ""
256-
}
257-
258-
limit := setting.Repository.PullRequest.DefaultMergeMessageCommitsLimit
259-
260-
list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, 0)
261-
if err != nil {
262-
log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
263-
return ""
264-
}
265-
266-
maxSize := setting.Repository.PullRequest.DefaultMergeMessageSize
267-
268-
posterSig := pr.Issue.Poster.NewGitSig().String()
269-
270-
authorsMap := map[string]bool{}
271-
authors := make([]string, 0, list.Len())
272-
stringBuilder := strings.Builder{}
273-
element := list.Front()
274-
for element != nil {
275-
commit := element.Value.(*git.Commit)
276-
277-
if maxSize < 0 || stringBuilder.Len() < maxSize {
278-
toWrite := []byte(commit.CommitMessage)
279-
if len(toWrite) > maxSize-stringBuilder.Len() && maxSize > -1 {
280-
toWrite = append(toWrite[:maxSize-stringBuilder.Len()], "..."...)
281-
}
282-
if _, err := stringBuilder.Write(toWrite); err != nil {
283-
log.Error("Unable to write commit message Error: %v", err)
284-
return ""
285-
}
286-
287-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
288-
log.Error("Unable to write commit message Error: %v", err)
289-
return ""
290-
}
291-
}
292-
293-
authorString := commit.Author.String()
294-
if !authorsMap[authorString] && authorString != posterSig {
295-
authors = append(authors, authorString)
296-
authorsMap[authorString] = true
297-
}
298-
element = element.Next()
299-
}
300-
301-
// Consider collecting the remaining authors
302-
if limit >= 0 && setting.Repository.PullRequest.DefaultMergeMessageAllAuthors {
303-
skip := limit
304-
limit = 30
305-
for {
306-
list, err := gitRepo.CommitsBetweenLimit(headCommit, mergeBase, limit, skip)
307-
if err != nil {
308-
log.Error("Unable to get commits between: %s %s Error: %v", pr.HeadBranch, pr.MergeBase, err)
309-
return ""
310-
311-
}
312-
if list.Len() == 0 {
313-
break
314-
}
315-
element := list.Front()
316-
for element != nil {
317-
commit := element.Value.(*git.Commit)
318-
319-
authorString := commit.Author.String()
320-
if !authorsMap[authorString] && authorString != posterSig {
321-
authors = append(authors, authorString)
322-
authorsMap[authorString] = true
323-
}
324-
element = element.Next()
325-
}
326-
327-
}
328-
}
329-
330-
if len(authors) > 0 {
331-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
332-
log.Error("Unable to write to string builder Error: %v", err)
333-
return ""
334-
}
335-
}
336-
337-
for _, author := range authors {
338-
if _, err := stringBuilder.Write([]byte("Co-authored-by: ")); err != nil {
339-
log.Error("Unable to write to string builder Error: %v", err)
340-
return ""
341-
}
342-
if _, err := stringBuilder.Write([]byte(author)); err != nil {
343-
log.Error("Unable to write to string builder Error: %v", err)
344-
return ""
345-
}
346-
if _, err := stringBuilder.WriteRune('\n'); err != nil {
347-
log.Error("Unable to write to string builder Error: %v", err)
348-
return ""
349-
}
350-
}
351-
352-
return stringBuilder.String()
353-
}
354-
355217
// ReviewCount represents a count of Reviews
356218
type ReviewCount struct {
357219
IssueID int64
@@ -465,39 +327,6 @@ func (pr *PullRequest) CanAutoMerge() bool {
465327
return pr.Status == PullRequestStatusMergeable
466328
}
467329

468-
// GetLastCommitStatus returns the last commit status for this pull request.
469-
func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
470-
if err = pr.LoadHeadRepo(); err != nil {
471-
return nil, err
472-
}
473-
474-
if pr.HeadRepo == nil {
475-
return nil, ErrPullRequestHeadRepoMissing{pr.ID, pr.HeadRepoID}
476-
}
477-
478-
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
479-
if err != nil {
480-
return nil, err
481-
}
482-
defer headGitRepo.Close()
483-
484-
lastCommitID, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
485-
if err != nil {
486-
return nil, err
487-
}
488-
489-
err = pr.LoadBaseRepo()
490-
if err != nil {
491-
return nil, err
492-
}
493-
494-
statusList, err := GetLatestCommitStatus(pr.BaseRepo, lastCommitID, 0)
495-
if err != nil {
496-
return nil, err
497-
}
498-
return CalcCommitStatus(statusList), nil
499-
}
500-
501330
// MergeStyle represents the approach to merge commits into base branch.
502331
type MergeStyle string
503332

@@ -786,35 +615,6 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string {
786615
return ""
787616
}
788617

789-
// IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
790-
func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
791-
var err error
792-
if err = pr.LoadBaseRepo(); err != nil {
793-
return false, err
794-
}
795-
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
796-
if err != nil {
797-
return false, err
798-
}
799-
baseCommit, err := baseGitRepo.GetBranchCommit(branchName)
800-
if err != nil {
801-
return false, err
802-
}
803-
804-
if err = pr.LoadHeadRepo(); err != nil {
805-
return false, err
806-
}
807-
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
808-
if err != nil {
809-
return false, err
810-
}
811-
headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch)
812-
if err != nil {
813-
return false, err
814-
}
815-
return baseCommit.HasPreviousCommit(headCommit.ID)
816-
}
817-
818618
// IsSameRepo returns true if base repo and head repo is the same
819619
func (pr *PullRequest) IsSameRepo() bool {
820620
return pr.BaseRepoID == pr.HeadRepoID

routers/repo/issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func issues(ctx *context.Context, milestoneID int64, isPullOption util.OptionalB
240240
return
241241
}
242242

243-
commitStatus[issues[i].PullRequest.ID], _ = issues[i].PullRequest.GetLastCommitStatus()
243+
commitStatus[issues[i].PullRequest.ID], _ = pull_service.GetLastCommitStatus(issues[i].PullRequest)
244244
}
245245
}
246246

routers/repo/pull.go

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
433433
return nil
434434
}
435435
ctx.Data["Divergence"] = divergence
436+
ctx.Data["GetCommitMessages"] = pull_service.GetCommitMessages(pull)
436437
}
437438

438439
sha, err := baseGitRepo.GetRefCommitID(pull.GetGitRefName())

routers/user/home.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/markup/markdown"
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/util"
25+
pull_service "code.gitea.io/gitea/services/pull"
2526

2627
"github.com/keybase/go-crypto/openpgp"
2728
"github.com/keybase/go-crypto/openpgp/armor"
@@ -553,7 +554,7 @@ func Issues(ctx *context.Context) {
553554
issue.Repo = showReposMap[issue.RepoID]
554555

555556
if isPullList {
556-
commitStatus[issue.PullRequest.ID], _ = issue.PullRequest.GetLastCommitStatus()
557+
commitStatus[issue.PullRequest.ID], _ = pull_service.GetLastCommitStatus(issue.PullRequest)
557558
}
558559
}
559560

0 commit comments

Comments
 (0)