Skip to content

fix(tool/code_search): guard option-like refs in buildGrepArgs - #737

Closed
aalhadxx wants to merge 1 commit into
alibaba:mainfrom
aalhadxx:fix/code-search-end-of-options
Closed

fix(tool/code_search): guard option-like refs in buildGrepArgs#737
aalhadxx wants to merge 1 commit into
alibaba:mainfrom
aalhadxx:fix/code-search-end-of-options

Conversation

@aalhadxx

@aalhadxx aalhadxx commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Related: closes #645, follow-up to #663

Summary

As discussed in #663 (comment), this PR moves the defense-in-depth check for option-like refs into buildGrepArgs instead of keeping it in gitGrep. This keeps the guard co-located with argument construction (the same pattern the other tools follow) and adds a clear comment explaining why git grep is the one invocation where we can't use --end-of-options.

What changed

  • internal/tool/code_search.go:

    • buildGrepArgs now rejects refs starting with - and returns nil.
    • Added a comment noting that git grep < 2.45 doesn't support --end-of-options before the revision.
    • gitGrep propagates the nil from buildGrepArgs as the existing "Error: ref must not start with '-'" message.
  • internal/tool/code_search_test.go:

    • Added TestBuildGrepArgs_RejectsOptionLikeRef to assert the guard lives in buildGrepArgs.
    • Kept the existing TestGitGrep_RejectsOptionLikeRef and TestGitGrep_OptionLikeRefDoesNotLaunchPager for end-to-end coverage.

Why this structure

The codebase already validates refs upstream in validateReviewRefs, but the buildGrepArgs guard ensures the defense-in-depth is visible right where arguments are built. If anyone ever calls buildGrepArgs from a new path, the guard still holds.

Moves the defense-in-depth check from gitGrep() into buildGrepArgs()
so the guard is co-located with argument construction.

Adds a comment explaining why --end-of-options can't be used here:
git grep < 2.45 does not support --end-of-options before the revision.

Adds TestBuildGrepArgs_RejectsOptionLikeRef.

Signed-off-by: Aalhad <aalhadxx@users.noreply.github.com>
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@lizhengfeng101

Copy link
Copy Markdown
Collaborator

Hi @aalhadxx — this PR is identical to #727 (same branch, same diff), which was already merged earlier today. Closing this as a duplicate. Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

code_search fails in range/commit reviews because git grep treats --end-of-options as a revision

2 participants