发布

  • [OPIK-6088] [INFRA] fix: pre-commit hook stages phantom deletions in worktrees (#6387)

    frostbyte_neo 发布于 2026-04-30 10:38:44 +00:00

    • [OPIK-6088] [INFRA] fix: pre-commit hook stages phantom deletions in worktrees

    Pin git's work tree in .hooks/pre-commit by calling git -C "$REPO_ROOT" add -u
    instead of relying on cwd. When git commit runs in a worktree it exports GIT_DIR,
    which makes git add -u fall back to cwd as the work tree; the hook's cd scripts
    then caused every tracked file outside scripts/ to stage as deleted.

    Adds a divergence warning that nudges make hooks when the installed hook has
    drifted from the tracked source, and changes the Makefile's HOOKS_DEST to resolve
    via git rev-parse --git-common-dir so make hooks works from worktrees as well
    as the main clone.

    Implements OPIK-6088.

    Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com

    • fix(hooks): guard HOOKS_DEST when not in a git repo, drop divergence warning

    The Makefile fallback $(shell git rev-parse --git-common-dir 2>/dev/null)/hooks
    collapsed to /hooks outside a git repo, so make hooks/make hooks-remove
    could touch files at the filesystem root. Cache the git-common-dir lookup,
    leave HOOKS_DEST empty when git rev-parse fails, and bail with a clear
    "not in a git repository" error from both targets before any cp/rm runs.

    Drop the installed-vs-tracked divergence warning from the hook: it duplicated
    the git rev-parse call in the Makefile and only fired in the case where the
    catastrophic bug is already prevented (post-fix hook installed but stale by a
    revision). The pre-OPIK-6088 buggy hook doesn't carry the warning code, so
    the catastrophic case it can't detect anyway.

    Addresses https://github.com/comet-ml/opik/pull/6387#discussion_r3110977345

    Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com

    • fix(hooks): anchor make hooks paths to MAKEFILE_DIR for cwd robustness

    The previous Makefile resolved $(HOOKS_SRC) and ran git rev-parse from make's
    invocation cwd. That meant make -f /opik/Makefile hooks from a different git
    repo with its own .hooks/pre-commit would silently install that repo's hook
    into that repo's .git/hooks/ — confusing, opik never touched, exit 0 with a
    misleading success message.

    Anchor MAKEFILE_DIR to the Makefile's own directory via realpath, run
    git rev-parse from there, and cd "$(MAKEFILE_DIR)" before the cp/chmod so all
    paths resolve to the Opik repo regardless of cwd.

    Verified:

    • From Opik worktree: installs into main clone's .git/hooks/ as before.
    • From a main-clone subdir: still works (was loud-error before).
    • From /tmp via -f flag: now installs into Opik (was silent no-op).
    • From /tmp/adversarial-repo with own .hooks/: now installs into Opik,
      adversarial repo's .git/hooks/ untouched.
    • From a copy of this Makefile in a non-git dir: errors loudly.

    Addresses https://github.com/comet-ml/opik/pull/6387#discussion_r3159753458

    Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com

    • fix(hooks): mirror MAKEFILE_DIR cd in hooks-remove target

    The hooks target cd's into MAKEFILE_DIR before the cp/chmod, but
    hooks-remove was missed in the previous commit. From the main clone,
    $(HOOKS_DEST) resolves to a relative path (.git/hooks). Without the cd,
    running make hooks-remove from any other cwd would either silently report
    "No pre-commit hook found" (false negative) or delete an unrelated
    .git/hooks/pre-commit if one happened to be present at the cwd-relative
    path.

    Verified: from /tmp/adversarial2 (a non-Opik repo with its own decoy
    .git/hooks/pre-commit), make -f /opik/Makefile hooks-remove now correctly
    removes Opik's installed hook and leaves the adversarial decoy intact.

    Addresses https://github.com/comet-ml/opik/pull/6387#discussion_r3161250878

    Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com


    Co-authored-by: Claude Opus 4.7 (1M context) noreply@anthropic.com

    下载附件