From 4c4fe595c26c4ac03ff33b68eb9d59e3527b5546 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Wed, 28 May 2025 14:44:51 +0200 Subject: [PATCH] chore(cleanup): fix and simplify API comparison helper (#7978) headIsTag := headGitRepo.IsCommitExist(baseBranch) is wrong on two counts: - it must be assigned to commitIsTag - it must check headBranch and not baseBranch this is not a bug but it certainly is confusing. Also, the logic below headBranchRef := headBranch if headIsBranch { headBranchRef = headBranch } else if headIsTag { headBranchRef = headBranch } can be simplified as: headBranchRef := headBranch Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7978 Reviewed-by: Gusted Co-authored-by: Earl Warren Co-committed-by: Earl Warren --- routers/api/v1/repo/pull.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 8456dcff59..b0bc7be90f 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1194,10 +1194,17 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) return nil, nil, nil, "", "" } + baseBranchRef := baseBranch + if baseIsBranch { + baseBranchRef = git.BranchPrefix + baseBranch + } else if baseIsTag { + baseBranchRef = git.TagPrefix + baseBranch + } + // Check if head branch is valid. - headIsCommit := headGitRepo.IsBranchExist(headBranch) - headIsBranch := headGitRepo.IsTagExist(headBranch) - headIsTag := headGitRepo.IsCommitExist(baseBranch) + headIsCommit := ctx.Repo.GitRepo.IsCommitExist(headBranch) + headIsBranch := ctx.Repo.GitRepo.IsBranchExist(headBranch) + headIsTag := ctx.Repo.GitRepo.IsTagExist(headBranch) if !headIsCommit && !headIsBranch && !headIsTag { // Check if headBranch is short sha commit hash if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil { @@ -1209,18 +1216,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) } } - baseBranchRef := baseBranch - if baseIsBranch { - baseBranchRef = git.BranchPrefix + baseBranch - } else if baseIsTag { - baseBranchRef = git.TagPrefix + baseBranch - } headBranchRef := headBranch - if headIsBranch { - headBranchRef = headBranch - } else if headIsTag { - headBranchRef = headBranch - } compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranchRef, headBranchRef, false, false) if err != nil {