@@ -16,20 +16,8 @@ import (
1616	"regexp" 
1717	"strconv" 
1818	"strings" 
19- 	"time" 
20- 
21- 	logger "code.gitea.io/gitea/modules/log" 
2219)
2320
24- // CompareInfo represents needed information for comparing references. 
25- type  CompareInfo  struct  {
26- 	MergeBase     string 
27- 	BaseCommitID  string 
28- 	HeadCommitID  string 
29- 	Commits       []* Commit 
30- 	NumFiles      int 
31- }
32- 
3321// GetMergeBase checks and returns merge base of two branches and the reference used as base. 
3422func  (repo  * Repository ) GetMergeBase (tmpRemote , base , head  string ) (string , string , error ) {
3523	if  tmpRemote  ==  ""  {
@@ -49,83 +37,6 @@ func (repo *Repository) GetMergeBase(tmpRemote, base, head string) (string, stri
4937	return  strings .TrimSpace (stdout ), base , err 
5038}
5139
52- // GetCompareInfo generates and returns compare information between base and head branches of repositories. 
53- func  (repo  * Repository ) GetCompareInfo (basePath , baseBranch , headBranch  string , directComparison , fileOnly  bool ) (_  * CompareInfo , err  error ) {
54- 	var  (
55- 		remoteBranch  string 
56- 		tmpRemote     string 
57- 	)
58- 
59- 	// We don't need a temporary remote for same repository. 
60- 	if  repo .Path  !=  basePath  {
61- 		// Add a temporary remote 
62- 		tmpRemote  =  strconv .FormatInt (time .Now ().UnixNano (), 10 )
63- 		if  err  =  repo .AddRemote (tmpRemote , basePath , false ); err  !=  nil  {
64- 			return  nil , fmt .Errorf ("AddRemote: %w" , err )
65- 		}
66- 		defer  func () {
67- 			if  err  :=  repo .RemoveRemote (tmpRemote ); err  !=  nil  {
68- 				logger .Error ("GetPullRequestInfo: RemoveRemote: %v" , err )
69- 			}
70- 		}()
71- 	}
72- 
73- 	compareInfo  :=  new (CompareInfo )
74- 
75- 	compareInfo .HeadCommitID , err  =  GetFullCommitID (repo .Ctx , repo .Path , headBranch )
76- 	if  err  !=  nil  {
77- 		compareInfo .HeadCommitID  =  headBranch 
78- 	}
79- 
80- 	compareInfo .MergeBase , remoteBranch , err  =  repo .GetMergeBase (tmpRemote , baseBranch , headBranch )
81- 	if  err  ==  nil  {
82- 		compareInfo .BaseCommitID , err  =  GetFullCommitID (repo .Ctx , repo .Path , remoteBranch )
83- 		if  err  !=  nil  {
84- 			compareInfo .BaseCommitID  =  remoteBranch 
85- 		}
86- 		separator  :=  "..." 
87- 		baseCommitID  :=  compareInfo .MergeBase 
88- 		if  directComparison  {
89- 			separator  =  ".." 
90- 			baseCommitID  =  compareInfo .BaseCommitID 
91- 		}
92- 
93- 		// We have a common base - therefore we know that ... should work 
94- 		if  ! fileOnly  {
95- 			// avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git <command> [<revision>...] -- [<file>...]' 
96- 			var  logs  []byte 
97- 			logs , _ , err  =  NewCommand ("log" ).AddArguments (prettyLogFormat ).
98- 				AddDynamicArguments (baseCommitID + separator + headBranch ).AddArguments ("--" ).
99- 				RunStdBytes (repo .Ctx , & RunOpts {Dir : repo .Path })
100- 			if  err  !=  nil  {
101- 				return  nil , err 
102- 			}
103- 			compareInfo .Commits , err  =  repo .parsePrettyFormatLogToList (logs )
104- 			if  err  !=  nil  {
105- 				return  nil , fmt .Errorf ("parsePrettyFormatLogToList: %w" , err )
106- 			}
107- 		} else  {
108- 			compareInfo .Commits  =  []* Commit {}
109- 		}
110- 	} else  {
111- 		compareInfo .Commits  =  []* Commit {}
112- 		compareInfo .MergeBase , err  =  GetFullCommitID (repo .Ctx , repo .Path , remoteBranch )
113- 		if  err  !=  nil  {
114- 			compareInfo .MergeBase  =  remoteBranch 
115- 		}
116- 		compareInfo .BaseCommitID  =  compareInfo .MergeBase 
117- 	}
118- 
119- 	// Count number of changed files. 
120- 	// This probably should be removed as we need to use shortstat elsewhere 
121- 	// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly 
122- 	compareInfo .NumFiles , err  =  repo .GetDiffNumChangedFiles (remoteBranch , headBranch , directComparison )
123- 	if  err  !=  nil  {
124- 		return  nil , err 
125- 	}
126- 	return  compareInfo , nil 
127- }
128- 
12940type  lineCountWriter  struct  {
13041	numLines  int 
13142}
0 commit comments