@@ -6,14 +6,10 @@ package git
66import (
77 "context"
88 "fmt"
9- "strconv"
10- "time"
119
1210 repo_model "code.gitea.io/gitea/models/repo"
1311 "code.gitea.io/gitea/modules/git"
1412 "code.gitea.io/gitea/modules/gitrepo"
15- "code.gitea.io/gitea/modules/graceful"
16- logger "code.gitea.io/gitea/modules/log"
1713 "code.gitea.io/gitea/modules/util"
1814)
1915
@@ -48,25 +44,6 @@ func (ci *CompareInfo) DirectComparison() bool {
4844
4945// GetCompareInfo generates and returns compare information between base and head branches of repositories.
5046func GetCompareInfo (ctx context.Context , baseRepo , headRepo * repo_model.Repository , headGitRepo * git.Repository , baseRef , headRef git.RefName , directComparison , fileOnly bool ) (_ * CompareInfo , err error ) {
51- var (
52- remoteBranch string
53- tmpRemote string
54- )
55-
56- // We don't need a temporary remote for same repository.
57- if baseRepo .ID != headRepo .ID {
58- // Add a temporary remote
59- tmpRemote = strconv .FormatInt (time .Now ().UnixNano (), 10 )
60- if err = gitrepo .GitRemoteAdd (ctx , headRepo , tmpRemote , baseRepo .RepoPath ()); err != nil {
61- return nil , fmt .Errorf ("GitRemoteAdd: %w" , err )
62- }
63- defer func () {
64- if err := gitrepo .GitRemoteRemove (graceful .GetManager ().ShutdownContext (), headRepo , tmpRemote ); err != nil {
65- logger .Error ("GetPullRequestInfo: GitRemoteRemove: %v" , err )
66- }
67- }()
68- }
69-
7047 compareInfo := & CompareInfo {
7148 BaseRepo : baseRepo ,
7249 BaseRef : baseRef ,
@@ -76,47 +53,49 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
7653 CompareSeparator : util .Iif (directComparison , ".." , "..." ),
7754 }
7855
56+ compareInfo .BaseCommitID , err = gitrepo .GetFullCommitID (ctx , baseRepo , baseRef .String ())
57+ if err != nil {
58+ return nil , err
59+ }
7960 compareInfo .HeadCommitID , err = gitrepo .GetFullCommitID (ctx , headRepo , headRef .String ())
8061 if err != nil {
81- compareInfo . HeadCommitID = headRef . String ()
62+ return nil , err
8263 }
8364
84- // FIXME: It seems we don't need mergebase if it's a direct comparison?
85- compareInfo .MergeBase , remoteBranch , err = headGitRepo .GetMergeBase (tmpRemote , baseRef .String (), headRef .String ())
86- if err == nil {
87- compareInfo .BaseCommitID , err = gitrepo .GetFullCommitID (ctx , headRepo , remoteBranch )
88- if err != nil {
89- compareInfo .BaseCommitID = remoteBranch
90- }
91- separator := "..."
92- baseCommitID := compareInfo .MergeBase
93- if directComparison {
94- separator = ".."
95- baseCommitID = compareInfo .BaseCommitID
65+ // if they are not the same repository, then we need to fetch the base commit into the head repository
66+ // because we will use headGitRepo in the following code
67+ if baseRepo .ID != headRepo .ID {
68+ exist := headGitRepo .IsReferenceExist (compareInfo .BaseCommitID )
69+ if ! exist {
70+ if err := gitrepo .FetchRemoteCommit (ctx , headRepo , baseRepo , compareInfo .BaseCommitID ); err != nil {
71+ return nil , fmt .Errorf ("FetchRemoteCommit: %w" , err )
72+ }
9673 }
74+ }
9775
98- // We have a common base - therefore we know that ... should work
99- if ! fileOnly {
100- compareInfo .Commits , err = headGitRepo .ShowPrettyFormatLogToList (ctx , baseCommitID + separator + headRef .String ())
101- if err != nil {
102- return nil , fmt .Errorf ("ShowPrettyFormatLogToList: %w" , err )
103- }
104- } else {
105- compareInfo .Commits = []* git.Commit {}
76+ if ! directComparison {
77+ compareInfo .MergeBase , err = gitrepo .MergeBase (ctx , headRepo , compareInfo .BaseCommitID , compareInfo .HeadCommitID )
78+ if err != nil {
79+ return nil , fmt .Errorf ("MergeBase: %w" , err )
10680 }
10781 } else {
108- compareInfo .Commits = []* git.Commit {}
109- compareInfo .MergeBase , err = gitrepo .GetFullCommitID (ctx , headRepo , remoteBranch )
82+ compareInfo .MergeBase = compareInfo .BaseCommitID
83+ }
84+
85+ // We have a common base - therefore we know that ... should work
86+ if ! fileOnly {
87+ compareInfo .Commits , err = headGitRepo .ShowPrettyFormatLogToList (ctx , compareInfo .BaseCommitID + compareInfo .CompareSeparator + compareInfo .HeadCommitID )
11088 if err != nil {
111- compareInfo . MergeBase = remoteBranch
89+ return nil , fmt . Errorf ( "ShowPrettyFormatLogToList: %w" , err )
11290 }
113- compareInfo .BaseCommitID = compareInfo .MergeBase
91+ } else {
92+ compareInfo .Commits = []* git.Commit {}
11493 }
11594
11695 // Count number of changed files.
11796 // This probably should be removed as we need to use shortstat elsewhere
11897 // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
119- compareInfo .NumFiles , err = headGitRepo .GetDiffNumChangedFiles (remoteBranch , headRef . String () , directComparison )
98+ compareInfo .NumFiles , err = headGitRepo .GetDiffNumChangedFiles (compareInfo . BaseCommitID , compareInfo . HeadCommitID , directComparison )
12099 if err != nil {
121100 return nil , err
122101 }
0 commit comments