@@ -190,7 +190,7 @@ func GetPullDiffStats(ctx *context.Context) {
190190 }
191191 pull := issue .PullRequest
192192
193- mergeBaseCommitID := GetMergedBaseCommitID (ctx , issue )
193+ mergeBaseCommitID := GetMergedBaseCommitID (ctx , pull )
194194 if mergeBaseCommitID == "" {
195195 return // no merge base, do nothing, do not stop the route handler, see below
196196 }
@@ -210,48 +210,46 @@ func GetPullDiffStats(ctx *context.Context) {
210210 ctx .Data ["DiffShortStat" ] = diffShortStat
211211}
212212
213- func GetMergedBaseCommitID (ctx * context.Context , issue * issues_model.Issue ) string {
214- pull := issue .PullRequest
215-
216- var baseCommit string
217- // Some migrated PR won't have any Base SHA and lose history, try to get one
218- if pull .MergeBase == "" {
219- var commitSHA , parentCommit string
220- // If there is a head or a patch file, and it is readable, grab info
221- commitSHA , err := ctx .Repo .GitRepo .GetRefCommitID (pull .GetGitRefName ())
222- if err != nil {
223- // Head File does not exist, try the patch
224- commitSHA , err = ctx .Repo .GitRepo .ReadPatchCommit (pull .Index )
225- if err == nil {
226- // Recreate pull head in files for next time
227- if err := ctx .Repo .GitRepo .SetReference (pull .GetGitRefName (), commitSHA ); err != nil {
228- log .Error ("Could not write head file" , err )
229- }
230- } else {
231- // There is no history available
232- log .Trace ("No history file available for PR %d" , pull .Index )
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 )
233225 }
226+ } else {
227+ // There is no history available
228+ log .Trace ("No history file available for PR %d" , pull .Index )
234229 }
235- if commitSHA != "" {
236- // Get immediate parent of the first commit in the patch, grab history back
237- parentCommit , _ , err = git .NewCommand ("rev-list" , "-1" , "--skip=1" ).AddDynamicArguments (commitSHA ).RunStdString (ctx , & git.RunOpts {Dir : ctx .Repo .GitRepo .Path })
238- if err == nil {
239- parentCommit = strings .TrimSpace (parentCommit )
240- }
241- // Special case on Git < 2.25 that doesn't fail on immediate empty history
242- if err != nil || parentCommit == "" {
243- log .Info ("No known parent commit for PR %d, error: %v" , pull .Index , err )
244- // bring at least partial history if it can work
245- parentCommit = commitSHA
246- }
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 )
247236 }
248- baseCommit = parentCommit
249- } else {
250- // Keep an empty history or original commit
251- baseCommit = pull .MergeBase
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+
247+ func GetMergedBaseCommitID (ctx * context.Context , pull * issues_model.PullRequest ) string {
248+ if pull .MergeBase != "" {
249+ return pull .MergeBase
252250 }
253251
254- return baseCommit
252+ return calcMergeBase ( ctx , pull )
255253}
256254
257255func preparePullViewPullInfo (ctx * context.Context , issue * issues_model.Issue ) * git.CompareInfo {
@@ -271,7 +269,13 @@ func prepareMergedViewPullInfo(ctx *context.Context, issue *issues_model.Issue)
271269 setMergeTarget (ctx , pull )
272270 ctx .Data ["HasMerged" ] = true
273271
274- baseCommit := GetMergedBaseCommitID (ctx , issue )
272+ baseCommit := GetMergedBaseCommitID (ctx , pull )
273+ if baseCommit == "" {
274+ ctx .Data ["BaseTarget" ] = pull .BaseBranch
275+ ctx .Data ["NumCommits" ] = 0
276+ ctx .Data ["NumFiles" ] = 0
277+ return nil // no merge base, do nothing
278+ }
275279
276280 compareInfo , err := ctx .Repo .GitRepo .GetCompareInfo (ctx .Repo .Repository .RepoPath (),
277281 baseCommit , pull .GetGitRefName (), false , false )
0 commit comments