clan-cli: add git.commit_file() to auto commit files if inside a git
- commit only if inside a git repo - commit only the specified file and nothing else - auto-generate commit message if not specified
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Any, Optional, Type
|
||||
|
||||
from clan_cli.dirs import get_clan_flake_toplevel
|
||||
from clan_cli.errors import ClanError
|
||||
from clan_cli.git import commit_file
|
||||
from clan_cli.machines.folders import machine_settings_file
|
||||
from clan_cli.nix import nix_eval
|
||||
|
||||
@@ -240,6 +241,10 @@ def set_option(
|
||||
settings_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(settings_file, "w") as f:
|
||||
json.dump(new_config, f, indent=2)
|
||||
if settings_file.resolve().is_relative_to(get_clan_flake_toplevel()):
|
||||
commit_file(
|
||||
settings_file, commit_message=f"Set option {option_description}"
|
||||
)
|
||||
|
||||
|
||||
# takes a (sub)parser and configures it
|
||||
|
||||
@@ -7,6 +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.machines.folders import machine_folder, machine_settings_file
|
||||
from clan_cli.nix import nix_eval
|
||||
|
||||
@@ -36,6 +37,7 @@ 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)
|
||||
|
||||
|
||||
def schema_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict:
|
||||
|
||||
51
pkgs/clan-cli/clan_cli/git.py
Normal file
51
pkgs/clan-cli/clan_cli/git.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from clan_cli.dirs import get_clan_flake_toplevel
|
||||
from clan_cli.errors import ClanError
|
||||
from clan_cli.nix import nix_shell
|
||||
|
||||
|
||||
# generic vcs agnostic commit function
|
||||
def commit_file(
|
||||
file_path: Path,
|
||||
repo_dir: Optional[Path] = None,
|
||||
commit_message: Optional[str] = None,
|
||||
) -> None:
|
||||
# set default for repo_dir
|
||||
if repo_dir is None:
|
||||
repo_dir = get_clan_flake_toplevel()
|
||||
# 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}")
|
||||
if not file_path.exists():
|
||||
raise ClanError(f"File {file_path} does not exist")
|
||||
# generate commit message if not provided
|
||||
if commit_message is None:
|
||||
# ensure that mentioned file path is relative to repo
|
||||
commit_message = f"Add {file_path.relative_to(repo_dir)}"
|
||||
# check if the repo is a git repo and commit
|
||||
if (repo_dir / ".git").exists():
|
||||
_commit_file_to_git(repo_dir, file_path, commit_message)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> None:
|
||||
"""Commit a file to a git repository.
|
||||
|
||||
:param repo_dir: The path to the git repository.
|
||||
:param file_path: The path to the file to commit.
|
||||
:param commit_message: The commit message.
|
||||
:raises ClanError: If the file is not in the git repository.
|
||||
"""
|
||||
# add the file to the git index
|
||||
subprocess.run(["git", "add", file_path], cwd=repo_dir, check=True)
|
||||
# commit only that file
|
||||
cmd = nix_shell(["git"], ["git", "commit", "-m", commit_message, file_path.name])
|
||||
subprocess.run(
|
||||
cmd,
|
||||
cwd=repo_dir,
|
||||
check=True,
|
||||
)
|
||||
Reference in New Issue
Block a user