From 4f1a63160b37960f0580b6c4a7ce488f1c792a33 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Fri, 30 Oct 2026 13:40:22 -0500 Subject: [PATCH] fix(provenance): report non-zero git status exits and update bootstrap rebuild hint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Update get_local_checkout_status in scripts/provenance.py to surface non-zero git status exit codes as '⚠️ Git Error (exit )' rather than masking them as '✓ Clean' - Update scripts/bootstrap_token.py rebuild instruction to use path:$HOME/Core#server --impure, ensuring instance.nix evaluation is respected - Add unit test coverage for non-zero git exit handling and bootstrap rebuild guidance --- scripts/bootstrap_token.py | 2 +- scripts/provenance.py | 20 ++++++++++++++------ tests/unit/test_bootstrap_token.py | 1 + tests/unit/test_provenance.py | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/scripts/bootstrap_token.py b/scripts/bootstrap_token.py index abc3b01..8c3782c 100755 --- a/scripts/bootstrap_token.py +++ b/scripts/bootstrap_token.py @@ -163,7 +163,7 @@ def main(argv: list[str] | None = None) -> int: f"Add the following descriptor to your SOPS tokens store (nixos/secrets.yaml -> appctl/tokens_json):\n\n" f"{json_snippet}\n\n" f"After updating nixos/secrets.yaml with `sops nixos/secrets.yaml`, apply changes with:\n" - f" sudo nixos-rebuild switch --flake ~/Core#server\n" + f" sudo nixos-rebuild switch --flake path:$HOME/Core#server --impure\n" ) return 0 diff --git a/scripts/provenance.py b/scripts/provenance.py index 4673f74..f9a700e 100644 --- a/scripts/provenance.py +++ b/scripts/provenance.py @@ -109,6 +109,7 @@ def get_local_checkout_status(dir_path: Path | str) -> CheckoutStatus: # 1. Check cleanliness is_dirty = False + git_error: str | None = None try: status_res = subprocess.run( ["git", "-C", str(path), "status", "--porcelain"], @@ -118,10 +119,14 @@ def get_local_checkout_status(dir_path: Path | str) -> CheckoutStatus: timeout=5.0, env=git_env, ) - if status_res.returncode == 0 and status_res.stdout.strip(): - is_dirty = True - except (subprocess.SubprocessError, OSError): + if status_res.returncode == 0: + if status_res.stdout.strip(): + is_dirty = True + else: + git_error = f"⚠️ Git Error (exit {status_res.returncode})" + except (subprocess.SubprocessError, OSError) as exc: is_dirty = False + git_error = f"⚠️ Git Error ({exc.__class__.__name__})" # 2. Get active branch branch = "" @@ -168,9 +173,12 @@ def get_local_checkout_status(dir_path: Path | str) -> CheckoutStatus: commit_sha = "" # 4. Format status label - state_symbol = "* Dirty" if is_dirty else "✓ Clean" - ref_desc = f"{branch}@{commit_sha}" if commit_sha else branch - status_label = f"{state_symbol} ({ref_desc})" if ref_desc else state_symbol + if git_error: + status_label = git_error + else: + state_symbol = "* Dirty" if is_dirty else "✓ Clean" + ref_desc = f"{branch}@{commit_sha}" if commit_sha else branch + status_label = f"{state_symbol} ({ref_desc})" if ref_desc else state_symbol return CheckoutStatus( is_git=True, diff --git a/tests/unit/test_bootstrap_token.py b/tests/unit/test_bootstrap_token.py index 7993c59..a30a986 100644 --- a/tests/unit/test_bootstrap_token.py +++ b/tests/unit/test_bootstrap_token.py @@ -126,6 +126,7 @@ def test_bootstrap_cli_never_prints_raw_secret_to_stdout_or_stderr( # Ensure stdout provides helpful human guidance assert "operator-admin" in captured.out assert "sops nixos/secrets.yaml" in captured.out + assert "sudo nixos-rebuild switch --flake path:$HOME/Core#server --impure" in captured.out def test_bootstrap_cli_print_json_outputs_valid_json_descriptor( diff --git a/tests/unit/test_provenance.py b/tests/unit/test_provenance.py index 01c5834..458afad 100644 --- a/tests/unit/test_provenance.py +++ b/tests/unit/test_provenance.py @@ -250,3 +250,19 @@ def test_resolve_upstream_provenance_class_b_and_default_timeout(): prov = resolve_upstream_provenance(source, local_commit="abc1234") assert prov.status == ProvenanceStatus.SYNCED assert mock_run.call_args[1]["timeout"] == 10.0 + + +def test_get_local_checkout_status_git_error(tmp_path: Path): + app_dir = tmp_path / "git_error_app" + app_dir.mkdir() + (app_dir / ".git").mkdir() + + with patch("subprocess.run") as mock_run: + mock_run.return_value = MagicMock( + returncode=128, stdout="", stderr="fatal: dubious ownership" + ) + status = get_local_checkout_status(app_dir) + assert status.is_git is True + assert status.is_dirty is False + assert status.status_label == "⚠️ Git Error (exit 128)" +