nixos-rebuild-ng: remove explicit check for git and instead check exception

This commit is contained in:
Thiago Kenji Okada 2024-11-26 16:56:49 +00:00
parent 2e4d755351
commit 0479ef4106
3 changed files with 12 additions and 16 deletions

View File

@ -1,5 +1,4 @@
import os
import shutil
from datetime import datetime
from pathlib import Path
from subprocess import PIPE, CalledProcessError
@ -96,20 +95,19 @@ def get_nixpkgs_rev(nixpkgs_path: Path | None) -> str | None:
if not nixpkgs_path:
return None
# Git is not included in the closure for nixos-rebuild so we need to check
if not shutil.which("git"):
try:
# Get current revision
r = run_wrapper(
["git", "-C", nixpkgs_path, "rev-parse", "--short", "HEAD"],
check=False,
stdout=PIPE,
)
except FileNotFoundError:
# Git is not included in the closure so we need to check
info(f"warning: Git not found; cannot figure out revision of '{nixpkgs_path}'")
return None
# Get current revision
r = run_wrapper(
["git", "-C", nixpkgs_path, "rev-parse", "--short", "HEAD"],
check=False,
stdout=PIPE,
)
rev = r.stdout.strip()
if rev:
if rev := r.stdout.strip():
# Check if repo is dirty
if run_wrapper(
["git", "-C", nixpkgs_path, "diff", "--quiet"],

View File

@ -74,8 +74,7 @@ def test_parse_args() -> None:
@patch.dict(nr.process.os.environ, {}, clear=True)
@patch(get_qualified_name(nr.process.subprocess.run), autospec=True)
@patch(get_qualified_name(nr.nix.shutil.which), autospec=True, return_value="/bin/git")
def test_execute_nix_boot(mock_which: Any, mock_run: Any, tmp_path: Path) -> None:
def test_execute_nix_boot(mock_run: Any, tmp_path: Path) -> None:
nixpkgs_path = tmp_path / "nixpkgs"
nixpkgs_path.mkdir()
config_path = tmp_path / "test"

View File

@ -57,8 +57,7 @@ def test_edit(mock_run: Any, monkeypatch: Any, tmpdir: Any) -> None:
mock_run.assert_called_with(["editor", default_nix], check=False)
@patch(get_qualified_name(n.shutil.which), autospec=True, return_value="/bin/git")
def test_get_nixpkgs_rev(mock_which: Any) -> None:
def test_get_nixpkgs_rev() -> None:
assert n.get_nixpkgs_rev(None) is None
path = Path("/path/to/nix")