Skip to content

Commit a8ef5d5

Browse files
committed
make head repo selection clearer
1 parent 180386b commit a8ef5d5

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

routers/web/repo/compare.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

tests/integration/compare_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func TestCompareRawDiffPatch(t *testing.T) {
229229
respTs = respTs.In(time.Local)
230230

231231
// Format the timestamp to match the expected format in the patch
232-
customFormat := "Mon, 02 Jan 2006 15:04:05 -0700"
232+
customFormat := "Mon, 2 Jan 2006 15:04:05 -0700"
233233
respTsStr := respTs.Format(customFormat)
234234

235235
req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s.patch", oldRef.ID.String(), newRef.ID.String()))

0 commit comments

Comments
 (0)