Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions loopx/capabilities/repository_change_window/git_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,11 @@ def _reference_transaction_introduces_commit(
ReferenceTransactionPhase.PREPARED,
}:
return False
# Git can invoke admission hooks for a zero-update transaction (including
# Git 2.55's pull path). No refs means no new commit to guard; provider
# integrity and previous-hook delegation remain owned by the caller.
if not hook_stdin:
return False
if not hook_stdin.strip():
raise RepositoryChangeWindowError(
f"reference-transaction {phase.value} phase requires at least one ref update"
Expand Down
42 changes: 40 additions & 2 deletions tests/capabilities/test_change_window_git_fast_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
)

def git(repo, *args, check=True):
return subprocess.run(
["git", "-C", str(repo), *args], check=check, capture_output=True, text=True
result = subprocess.run(
["git", "-C", str(repo), *args], check=False, capture_output=True, text=True
)
if check:
assert result.returncode == 0, result.stdout + result.stderr
return result

git(remote, "config", "user.name", "Synthetic User")
git(remote, "config", "user.email", "user@example.invalid")
Expand Down Expand Up @@ -130,7 +133,7 @@
hook_args=[phase],
hook_stdin=row,
)
assert result["ok"] and not result["policy_evaluated"]

Check warning on line 136 in tests/capabilities/test_change_window_git_fast_path.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaCKxSK2_wRb3VnUmZx2&open=AaCKxSK2_wRb3VnUmZx2&pullRequest=4171
assert result["decision"] is None
previous = tmp_path / "previous" / "reference-transaction"
previous.write_text("#!/bin/sh\necho previous-failure >&2\nexit 7\n")
Expand All @@ -141,7 +144,7 @@
hook_args=["prepared"],
hook_stdin=row,
)
assert result["exit_code"] == 7 and result["status"] == "previous_hook_failed"

Check warning on line 147 in tests/capabilities/test_change_window_git_fast_path.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaCKxSK2_wRb3VnUmZx3&open=AaCKxSK2_wRb3VnUmZx3&pullRequest=4171
managed = repo / ".git/loopx/repository-change-window/hooks/pre-push"
managed.write_text("#!/bin/sh\nexit 0\n")
result = owner.run_git_hook_provider(
Expand All @@ -163,6 +166,41 @@
assert result.returncode == 0, result.stderr


@pytest.mark.parametrize("phase", ["preparing", "prepared"])
def test_empty_transaction_is_noop_but_keeps_hook_guards(
installed, tmp_path, monkeypatch, phase
):
repo, _remote, _git, _policy = installed

def unexpected(*args, **kwargs):
raise AssertionError("empty transaction evaluated the time policy")

monkeypatch.setattr(owner, "evaluate_policy", unexpected)
args = {
"repo_path": repo,
"runtime_root": tmp_path / "runtime",
"event": "reference-transaction",
"hook_args": [phase],
"hook_stdin": b"",
}
result = owner.run_git_hook_provider(**args)
assert result["ok"] and result["previous_hook_invoked"]

Check warning on line 187 in tests/capabilities/test_change_window_git_fast_path.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaCKxSK2_wRb3VnUmZx4&open=AaCKxSK2_wRb3VnUmZx4&pullRequest=4171
assert not result["guarded_change"] and not result["policy_evaluated"]

Check warning on line 188 in tests/capabilities/test_change_window_git_fast_path.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaCKxSK2_wRb3VnUmZx5&open=AaCKxSK2_wRb3VnUmZx5&pullRequest=4171
assert result["decision"] is None
assert (tmp_path / "phases").read_text().splitlines() == [phase]
assert (tmp_path / "payloads").read_bytes() == b""
# An empty batch is legal; a nonempty malformed row is not.
with pytest.raises(ValueError, match="at least one ref update"):
owner.run_git_hook_provider(**{**args, "hook_stdin": b" \n"})
previous = tmp_path / "previous" / "reference-transaction"
previous.write_text("#!/bin/sh\nexit 7\n")
result = owner.run_git_hook_provider(**args)
assert result["status"] == "previous_hook_failed" and result["exit_code"] == 7

Check warning on line 198 in tests/capabilities/test_change_window_git_fast_path.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=huangruiteng_loopx&issues=AaCKxSK2_wRb3VnUmZx6&open=AaCKxSK2_wRb3VnUmZx6&pullRequest=4171
managed = repo / ".git/loopx/repository-change-window/hooks/reference-transaction"
managed.write_text(managed.read_text() + "# modified\n")
assert owner.run_git_hook_provider(**args)["status"] == "provider_drift"


def test_old_valid_hook_generation_requires_explicit_refresh(installed):
repo, _remote, _git, policy = installed
state_path = repo / ".git/loopx/repository-change-window/provider.json"
Expand Down