Skip to content

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

Closed

Description

@nixthon

OpenCodeReview Version

1.8.3

Operating System

macOS (Apple Silicon)

Installation Method

npm (global)

LLM Provider

OpenAI

Bug Description

Description

In OpenCodeReview v1.8.3, every code_search call fails during range or commit reviews when using Git 2.43.0.

OCR constructs the command as follows:

git --no-pager grep [options] -e <pattern> --end-of-options <ref> -- <pathspec>

Unlike commands such as git show and git rev-parse, git grep does not accept --end-of-options between the pattern and revision arguments on Git 2.43.0. It treats --end-of-options as a revision and returns:

fatal: unable to resolve revision: --end-of-options

## Environment

- OpenCodeReview: v1.8.3
- Commit: 80a579466
- Git: 2.43.0
- OS: Linux amd64
- Review mode: range (--from main --to feature-branch)

## Reproduction

Run:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  --end-of-options main -- 'src/**/*.py'

Actual result:

fatal: unable to resolve revision: --end-of-options

Removing --end-of-options makes the command work:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  main -- 'src/**/*.py'

The same issue occurs through OCR:

ocr review --from main --to feature-branch

Whenever the model invokes code_search, the tool result contains:

Error: fatal: unable to resolve revision: --end-of-options

## Impact

The LLM request itself succeeds, but the search tool returns no usable results. The model repeatedly retries searches or falls back to file_read.

In one range review:

- 375 code_search calls were made.
- All 375 returned the same Git error.
- Affected files exhausted MAX_TOOL_REQUEST_TIMES=30.
- OCR finally reported main_task did not complete before stopping.

This makes the problem look like an LLM completion failure even though the endpoint is healthy.

The tool call is also recorded as ok: true, because the Git failure is returned as an Error: result string rather than a tool execution error.

## Root Cause

internal/tool/code_search.go adds --end-of-options before the ref:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, "--end-of-options")
    cmdArgs = append(cmdArgs, ref)
}

OCR already validates CLI refs in validateReviewRefs:

- The ref must not start with -.
- The ref must resolve to a commit through git rev-parse --verify.

However, CodeSearchProvider should still validate the ref independently because it can be constructed outside the CLI path.

## Proposed Fix

Reject option-like refs explicitly in gitGrep, then pass the validated ref positionally without --end-of-options:

func (p *CodeSearchProvider) gitGrep(
    ctx context.Context,
    searchText string,
    caseSensitive bool,
    usePerlRegexp bool,
    pathspec []string,
) (string, error) {
    if strings.HasPrefix(p.FileReader.Ref, "-") {
        return "Error: ref must not start with '-'", nil
    }

    cmdArgs := p.buildGrepArgs(
        searchText,
        caseSensitive,
        usePerlRegexp,
        false,
        pathspec,
    )
    // ...
}

Change the argument construction to:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, ref)
}

## Suggested Tests

1. Update TestBuildGrepArgs_CommitMode to expect:

-e myFunc abc1234 -- pkg/

and assert that --end-of-options is absent.

2. Add a test that rejects an option-like ref:

func TestGitGrep_RejectsOptionLikeRef(t *testing.T) {
    p := NewCodeSearch(&FileReader{
        RepoDir: "/tmp",
        Ref:     "-O./pwn.sh",
    })

    result, err := p.gitGrep(
        context.Background(),
        "myFunc",
        false,
        false,
        nil,
    )

    if err != nil {
        t.Fatal(err)
    }
    if result != "Error: ref must not start with '-'" {
        t.Fatalf("unexpected result: %s", result)
    }
}

3. Keep the existing commit-mode integration tests to verify real git grep execution.

## Verification

After applying this change on Git 2.43.0:

- Commit/range-mode code_search returns matches successfully.
- Option-like refs are rejected before invoking Git.
- The internal/tool test suite passes.
- In a real OCR range review, more than 100 code_search calls completed with zero occurrences of the previous Git error.

### Steps to Reproduce

## Reproduction

Run:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  --end-of-options main -- 'src/**/*.py'

Actual result:

fatal: unable to resolve revision: --end-of-options

Removing --end-of-options makes the command work:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  main -- 'src/**/*.py'

The same issue occurs through OCR:

ocr review --from main --to feature-branch

Whenever the model invokes code_search, the tool result contains:

Error: fatal: unable to resolve revision: --end-of-options

## Impact

The LLM request itself succeeds, but the search tool returns no usable results. The model repeatedly retries searches or falls back to file_read.

In one range review:

- 375 code_search calls were made.
- All 375 returned the same Git error.
- Affected files exhausted MAX_TOOL_REQUEST_TIMES=30.
- OCR finally reported main_task did not complete before stopping.

This makes the problem look like an LLM completion failure even though the endpoint is healthy.

The tool call is also recorded as ok: true, because the Git failure is returned as an Error: result string rather than a tool execution error.

## Root Cause

internal/tool/code_search.go adds --end-of-options before the ref:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, "--end-of-options")
    cmdArgs = append(cmdArgs, ref)
}

OCR already validates CLI refs in validateReviewRefs:

- The ref must not start with -.
- The ref must resolve to a commit through git rev-parse --verify.

However, CodeSearchProvider should still validate the ref independently because it can be constructed outside the CLI path.

## Proposed Fix

Reject option-like refs explicitly in gitGrep, then pass the validated ref positionally without --end-of-options:

func (p *CodeSearchProvider) gitGrep(
    ctx context.Context,
    searchText string,
    caseSensitive bool,
    usePerlRegexp bool,
    pathspec []string,
) (string, error) {
    if strings.HasPrefix(p.FileReader.Ref, "-") {
        return "Error: ref must not start with '-'", nil
    }

    cmdArgs := p.buildGrepArgs(
        searchText,
        caseSensitive,
        usePerlRegexp,
        false,
        pathspec,
    )
    // ...
}

Change the argument construction to:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, ref)
}


### Expected Behavior

## Reproduction

Run:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  --end-of-options main -- 'src/**/*.py'

Actual result:

fatal: unable to resolve revision: --end-of-options

Removing --end-of-options makes the command work:

git grep -F -n --no-color --max-count 100 \
  -e RepositoryFactory \
  main -- 'src/**/*.py'

The same issue occurs through OCR:

ocr review --from main --to feature-branch

Whenever the model invokes code_search, the tool result contains:

Error: fatal: unable to resolve revision: --end-of-options

## Impact

The LLM request itself succeeds, but the search tool returns no usable results. The model repeatedly retries searches or falls back to file_read.

In one range review:

- 375 code_search calls were made.
- All 375 returned the same Git error.
- Affected files exhausted MAX_TOOL_REQUEST_TIMES=30.
- OCR finally reported main_task did not complete before stopping.

This makes the problem look like an LLM completion failure even though the endpoint is healthy.

The tool call is also recorded as ok: true, because the Git failure is returned as an Error: result string rather than a tool execution error.

## Root Cause

internal/tool/code_search.go adds --end-of-options before the ref:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, "--end-of-options")
    cmdArgs = append(cmdArgs, ref)
}

OCR already validates CLI refs in validateReviewRefs:

- The ref must not start with -.
- The ref must resolve to a commit through git rev-parse --verify.

However, CodeSearchProvider should still validate the ref independently because it can be constructed outside the CLI path.

## Proposed Fix

Reject option-like refs explicitly in gitGrep, then pass the validated ref positionally without --end-of-options:

func (p *CodeSearchProvider) gitGrep(
    ctx context.Context,
    searchText string,
    caseSensitive bool,
    usePerlRegexp bool,
    pathspec []string,
) (string, error) {
    if strings.HasPrefix(p.FileReader.Ref, "-") {
        return "Error: ref must not start with '-'", nil
    }

    cmdArgs := p.buildGrepArgs(
        searchText,
        caseSensitive,
        usePerlRegexp,
        false,
        pathspec,
    )
    // ...
}

Change the argument construction to:

if ref := p.FileReader.Ref; ref != "" {
    cmdArgs = append(cmdArgs, ref)
}


### Logs / Error Output

```shell

Additional Context

No response

Activity

  1. added theissue type on Jul 31, 2026
  2. added
    bug
    on Jul 31, 2026
  3. stay-foolish-forever commented on Aug 1, 2026

    @stay-foolish-forever
    Contributor

    @tangxijin

    Thanks for the detailed report and the precise reproduction — the version-specific git grep behavior and the impact analysis made this easy to triage.

    We've confirmed the working behavior on current Git, and your 2.43.0 reproduction is consistent with Git's incremental rollout of --end-of-options across subcommands. We're now confirming the issue and validating the proposed fix, including the option-injection / pager security cases, to make sure a fix is safe across all review modes and Git versions. We'll follow up here once that's done.

    Thanks again!

  4. stay-foolish-forever commented on Aug 1, 2026

    @stay-foolish-forever
    Contributor

    @tangxijin

    First of all, thank you again for the thorough report and the precise reproduction — it has been genuinely helpful for our triage. I also apologize for the slight delay in getting back to you.

    After extensive internal discussion, we've found that this is more nuanced than it first appeared, and I'd like to share the background and our forward plan here.

    For context: --end-of-options was introduced specifically to mitigate the command-line injection / arbitrary file-write risk reported in #111, where option-like --from/--to/--commit values (e.g. --output=, --open-files-in-pager=) could be reinterpreted by git as flags. So removing it outright — as the seemingly most direct fix might suggest — would reopen that attack surface, which we're not willing to do.

    At the same time, we fully appreciate that the compatibility cost of --end-of-options is equally hard to ignore. As you've demonstrated, its support is inconsistent across subcommands and git versions, which is exactly what's breaking code_search on Git 2.43.0. We're truly sorry for the disruption and inconvenience this has caused you and other affected users.

    In the short term, we have to prioritize security: we don't want to ship a change that silently re-enables the injection path. As a temporary workaround, could we ask you to upgrade Git to a version that supports git grep --end-of-options (2.45+ is a safe choice)? This restores code_search without weakening the guard. We recognize this isn't an ideal ask, and we apologize again for the extra step it places on you.

    On the fix side, the maintainers are designing a more robust defense that does not rely on --end-of-options — ref validation and option/ref separation at the source, with defense-in-depth that holds across git versions and all subcommands. We expect to land this in the near future and will link the PR here as soon as it's open.

    Thank you once more for your patience and the high-quality report — it has been a real help to our design discussions. If you run into anything while applying the workaround, please don't hesitate to let us know here.

  5. nixthon commented on Aug 3, 2026

    @nixthon
    Author

    Thanks for the clarification. I’ll upgrade Git to 2.45+ as the temporary workaround.

    One compatibility concern worth noting: Ubuntu 24.04 LTS currently provides Git 2.43 through its official repositories. Upgrading to 2.45+ therefore requires a third-party
    repository, a separate package manager such as Pixi/Conda, or a manual installation.

    Some users, especially those in managed or production environments, may be unable or unwilling to replace the distribution-provided Git version. It would be helpful to document
    the temporary minimum Git requirement and prioritize the planned version-independent fix.

    Thanks again for investigating this.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions