@@ -247,13 +247,13 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
247247
248248 // If there is no head repository, it means compare between the same repository.
249249 headInfos := strings .Split (infos [1 ], ":" )
250- if len (headInfos ) == 1 {
250+ if len (headInfos ) == 1 { // {:headBranch} case, guaranteed baseRepo is headRepo
251251 isSameRepo = true
252252 ci .HeadUser = ctx .Repo .Owner
253- ci .HeadBranch , ci .RawDiffType = parseRefForRawDiff (ctx , ctx . Repo . Repository , headInfos [0 ])
254- } else if len (headInfos ) == 2 {
253+ ci .HeadBranch , ci .RawDiffType = parseRefForRawDiff (ctx , baseRepo , headInfos [0 ])
254+ } else if len (headInfos ) == 2 { // {:headOwner}:{:headBranch} or {:headOwner}/{:headRepoName}:{:headBranch} case
255255 headInfosSplit := strings .Split (headInfos [0 ], "/" )
256- if len (headInfosSplit ) == 1 {
256+ if len (headInfosSplit ) == 1 { // {:headOwner}:{:headBranch} case, guaranteed baseRepo.Name is headRepo.Name
257257 ci .HeadUser , err = user_model .GetUserByName (ctx , headInfos [0 ])
258258 if err != nil {
259259 if user_model .IsErrUserNotExist (err ) {
@@ -263,13 +263,23 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
263263 }
264264 return nil
265265 }
266- // FIXME: how to correctly choose the head repository? The logic below (3-8) is quite complex, the real head repo is determined there
267- ci .HeadBranch , ci .RawDiffType = parseRefForRawDiff (ctx , ... , headInfos [1 ])
266+
267+ headRepo , err := repo_model .GetRepositoryByOwnerAndName (ctx , ci .HeadUser .Name , baseRepo .Name )
268+ if err != nil {
269+ if repo_model .IsErrRepoNotExist (err ) {
270+ ctx .NotFound (nil )
271+ } else {
272+ ctx .ServerError ("GetRepositoryByOwnerAndName" , err )
273+ }
274+ return nil
275+ }
276+ ci .HeadBranch , ci .RawDiffType = parseRefForRawDiff (ctx , headRepo , headInfos [1 ])
277+
268278 isSameRepo = ci .HeadUser .ID == ctx .Repo .Owner .ID
269- if isSameRepo {
279+ if isSameRepo { // not a fork
270280 ci .HeadRepo = baseRepo
271281 }
272- } else {
282+ } else { // {:headOwner}/{:headRepoName}:{:headBranch} case, across forks
273283 ci .HeadRepo , err = repo_model .GetRepositoryByOwnerAndName (ctx , headInfosSplit [0 ], headInfosSplit [1 ])
274284 if err != nil {
275285 if repo_model .IsErrRepoNotExist (err ) {
0 commit comments