opencode modifies git index when executed in prepare-commit-msg hook #4044

Open
opened 2026-02-16 17:42:24 -05:00 by yindo · 1 comment
Owner

Originally created by @atsushifx on GitHub (Dec 31, 2025).

Originally assigned to: @rekram1-node on GitHub.

Description

When opencode is executed inside the prepare-commit-msg git hook,
it modifies the git index, even though the hook contract requires
only editing the commit message.

The index modification is detected by a change in the index tree hash
(git write-tree) before and after opencode execution.

This behavior reproduces only in the git hook context and does not occur
when running the same script directly from the command line.

A minimal reproduction repository with logs and steps is available here:
https://github.com/atsushifx/repro-opencode-modify-git-index-by-hook

OpenCode version

1.0.216

Steps to reproduce

  1. Clone the reproduction repository:

    git clone https://github.com/atsushifx/repro-opencode-modify-git-index-by-hook.git
    cd repro-opencode-modify-git-index-by-hook

  2. Install the git hook:

    cp hooks/prepare-commit-msg .git/hooks/prepare-commit-msg
    chmod +x .git/hooks/prepare-commit-msg

  3. Create and stage a file, while leaving another file unstaged:

    echo "test" > hello2
    git add hello2

    Ensure there is at least one unstaged change

    echo "# comment" >> scripts/repro.sh

  4. Run a commit:

    git commit -m "changed"

  5. Observe the output from the reproduction script:

    • The index tree hash printed under "index BEFORE"
      differs from the hash printed under "index AFTER".

    • The script reports:

      ERROR: index tree CHANGED

Screenshot and/or share link

C: /repro-index-bugs > git commit -m "changed"
Start message-hooks
[repro] prepare-commit-msg hook activated
[repro] commit msg file: .git/COMMIT_EDITMSG
== Repro: prompt + blank line + git diff --cached ==

git status (initial)

A hello2
M scripts/repro.sh

index BEFORE

9af142bbce0effe226a22361485392917edba583

running opencode (agent input)

Proposed commit message (Conventional Commits)

  • chore: add hello2 placeholder file

Optional body (if you want a bit more “why”):

  • Adds an empty file for future use / to keep repository structure in place.

index AFTER

24d4e051d0e457eacb0d59887d05a4e8b43f9688

index tree diff

ERROR: index tree CHANGED
C: /repro-index-bugs >

Operating System

Windows 11

Terminal

Windows Terminal

Originally created by @atsushifx on GitHub (Dec 31, 2025). Originally assigned to: @rekram1-node on GitHub. ### Description When opencode is executed inside the `prepare-commit-msg` git hook, it modifies the git index, even though the hook contract requires only editing the commit message. The index modification is detected by a change in the index tree hash (`git write-tree`) before and after opencode execution. This behavior reproduces only in the git hook context and does not occur when running the same script directly from the command line. A minimal reproduction repository with logs and steps is available here: <https://github.com/atsushifx/repro-opencode-modify-git-index-by-hook> ### OpenCode version 1.0.216 ### Steps to reproduce 1. Clone the reproduction repository: git clone https://github.com/atsushifx/repro-opencode-modify-git-index-by-hook.git cd repro-opencode-modify-git-index-by-hook 2. Install the git hook: cp hooks/prepare-commit-msg .git/hooks/prepare-commit-msg chmod +x .git/hooks/prepare-commit-msg 3. Create and stage a file, while leaving another file unstaged: echo "test" > hello2 git add hello2 # Ensure there is at least one unstaged change echo "# comment" >> scripts/repro.sh 4. Run a commit: git commit -m "changed" 5. Observe the output from the reproduction script: - The index tree hash printed under "index BEFORE" differs from the hash printed under "index AFTER". - The script reports: ERROR: index tree CHANGED ### Screenshot and/or share link C: /repro-index-bugs > git commit -m "changed" Start message-hooks [repro] prepare-commit-msg hook activated [repro] commit msg file: .git/COMMIT_EDITMSG == Repro: prompt + blank line + git diff --cached == ## git status (initial) A hello2 M scripts/repro.sh ## index BEFORE 9af142bbce0effe226a22361485392917edba583 ## running opencode (agent input) **Proposed commit message (Conventional Commits)** - `chore: add hello2 placeholder file` Optional body (if you want a bit more “why”): - `Adds an empty file for future use / to keep repository structure in place.` ## index AFTER 24d4e051d0e457eacb0d59887d05a4e8b43f9688 ## index tree diff ERROR: index tree CHANGED C: /repro-index-bugs > ### Operating System Windows 11 ### Terminal Windows Terminal
yindo added the bug label 2026-02-16 17:42:24 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Dec 31, 2025):

This issue might be a duplicate of existing issues. Please check:

  • #4125: opencode reverts developer modifications to the LLM generated code
  • #3064: Feature request for checking uncommitted changes before continuing

These issues relate to unintended file modifications and preventing opencode from overwriting or modifying code unexpectedly. While your issue is specifically about git index modification in hooks, the underlying problem (opencode making unintended modifications to the git state) might share a common root cause.

Feel free to ignore if this is indeed a distinct issue specific to git hook execution.

@github-actions[bot] commented on GitHub (Dec 31, 2025): This issue might be a duplicate of existing issues. Please check: - #4125: opencode reverts developer modifications to the LLM generated code - #3064: Feature request for checking uncommitted changes before continuing These issues relate to unintended file modifications and preventing opencode from overwriting or modifying code unexpectedly. While your issue is specifically about git index modification in hooks, the underlying problem (opencode making unintended modifications to the git state) might share a common root cause. Feel free to ignore if this is indeed a distinct issue specific to git hook execution.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#4044