make git optional again

This commit is contained in:
Jörg Thalheim 2023-09-22 16:03:14 +02:00
parent e2cf3c1601
commit e3b2424d9d
4 changed files with 14 additions and 6 deletions

View File

@ -7,7 +7,7 @@ from typing import Optional
from fastapi import HTTPException
from clan_cli.dirs import get_clan_flake_toplevel, nixpkgs_source
from clan_cli.git import commit_file
from clan_cli.git import commit_file, find_git_repo_root
from clan_cli.machines.folders import machine_folder, machine_settings_file
from clan_cli.nix import nix_eval
@ -37,7 +37,10 @@ def set_config_for_machine(machine_name: str, config: dict) -> None:
settings_path.parent.mkdir(parents=True, exist_ok=True)
with open(settings_path, "w") as f:
json.dump(config, f)
commit_file(settings_path)
repo_dir = find_git_repo_root()
if repo_dir is not None:
commit_file(settings_path, repo_dir)
def schema_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict:

View File

@ -1,6 +1,7 @@
import os
import sys
from pathlib import Path
from typing import Optional
from .errors import ClanError
@ -9,8 +10,11 @@ def get_clan_flake_toplevel() -> Path:
return find_toplevel([".clan-flake", ".git", ".hg", ".svn", "flake.nix"])
def find_git_repo_root() -> Path:
return find_toplevel([".git"])
def find_git_repo_root() -> Optional[Path]:
try:
return find_toplevel([".git"])
except ClanError:
return None
def find_toplevel(top_level_files: list[str]) -> Path:

View File

@ -13,9 +13,10 @@ def commit_file(
repo_dir: Optional[Path] = None,
commit_message: Optional[str] = None,
) -> None:
# set default for repo_dir
if repo_dir is None:
repo_dir = find_git_repo_root()
if repo_dir is None:
return
# check that the file is in the git repository and exists
if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()):
raise ClanError(f"File {file_path} is not in the git repository {repo_dir}")

View File

@ -11,7 +11,7 @@ def test_get_clan_flake_toplevel(
) -> None:
monkeypatch.chdir(temporary_dir)
with pytest.raises(ClanError):
get_clan_flake_toplevel()
print(get_clan_flake_toplevel())
(temporary_dir / ".git").touch()
assert get_clan_flake_toplevel() == temporary_dir