feat(api): support ref suffixes in compare#38148
Conversation
Resolve a `^` or `~N` revision suffix to a commit before comparing, so the compare API accepts `base...head^` and `~N` on either ref. Pull request creation still requires branch refs and the web compare page keeps rejecting suffixes. Closes go-gitea#33943 Assisted-by: Claude Code:claude-opus-4-8
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0efa2beba6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if !validRefSuffix.MatchString(refSuffix) { | ||
| return "" | ||
| } | ||
| commit, err := gitRepo.GetCommit(oriRef + refSuffix) |
There was a problem hiding this comment.
Resolve suffixes after disambiguating refs
When a suffix is present this bypasses UnstableGuessRefByShortName and asks git to resolve the raw short name plus suffix. In repositories where a branch and tag share the same name, the non-suffixed compare path resolves the branch first, but compare/v1^...main feeds v1^ to git and git reports the ambiguous ref as missing, so users cannot compare the branch parent. Resolve oriRef to the branch/tag/SHA first and append the suffix to that full ref to preserve the existing branch-first semantics.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
There is a related edge case worth mentioning. When a branch and tag share a name, the suffix path uses the default git order instead of checking branches first (UnstableGuessRefByShortName), so name^ can pick the tag. I can fix it here, or open a follow-up if you want to keep this PR focused.
ResolveRefWithSuffix returned an empty ref for an unsupported suffix and for a valid suffix that did not resolve, so the compare endpoint answered 404 in both cases. Return an invalid-argument error for an unsupported suffix so the API responds 400, and keep 404 for a ref that does not resolve. Rename the headRef local to headOriRef to match the struct field. Assisted-by: Claude Code:claude-opus-4-8
Route the web compare page through common.ResolveRefWithSuffix, the resolver the API already uses, so `^` and `~N` ref suffixes resolve to a commit instead of being rejected. A resolved suffix is a commit rather than a branch, so the page stays diff-only and does not offer to open a pull request. Assisted-by: Claude Code:claude-opus-4-8
|
Done. The web compare page now resolves base and head suffixes through the same On the branch/tag name-clash from the codex thread, it lives in that shared resolver, so the web path inherits the same handling rather than adding anything new. I can fold the branch-first fix into this PR if you'd rather not leave it for a follow-up. |
A nil error now guarantees a usable RefName. A missing ref and a suffix that does not resolve return a not-found error instead of an empty RefName, so the callers no longer check for an empty ref after a nil error. Lazy-init the suffix regexp with sync.OnceValue. Assisted-by: Claude Code:claude-opus-4-8
| {"does-not-exist", ""}, | ||
| {"add-csv", "~50"}, | ||
| } { | ||
| ref, err := common.ResolveRefWithSuffix(gitRepo, tc.oriRef, tc.suffix) |
There was a problem hiding this comment.
It needs to assert that ResolveRefWithSuffix won't return branch ref if the ref has suffix.
Otherwise the "New Pull Request" logic would have problems.
wxiaoguang
left a comment
There was a problem hiding this comment.
Overall looks good. Just some nits.
|
@lunny why enable merge if there are some nits open |
|
@wxiaoguang addressed the minor nits |
…38172) This is really a follow-up to [#38148](#35305) , instead of having specific mappings of options for git configurations, just honor any user-provided gitconfig. I include a test which points out the specific config I have which was previously not honored, but more generally this means that gitea now only *adds* new gitconfig and never overwrites any config provided under `[git.config]`. --------- Signed-off-by: Royce Remer <royceremer@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Compare API requests with a
^or~Nrevision suffix (for examplecompare/main...feature^) were rejected with400 Unsupported comparison syntax: ref with suffix. The fix resolves the suffix to a commit before comparing, sobase...head^and~Nwork on either side, the same as git.Only
^/~Nnavigation is resolved. Pull request creation still requires plain branch refs, and the web compare page keeps rejecting suffixes since its branch selectors need separate UI work.Closes #33943
Assisted-by: Claude Code:claude-opus-4-8