clan-cli: clan_cli.git -> clan_lib.git
This commit is contained in:
@@ -9,6 +9,7 @@ from tempfile import TemporaryDirectory
|
||||
|
||||
from clan_lib.cmd import RunOpts, run
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
from clan_lib.nix import nix_shell
|
||||
|
||||
from clan_cli.completions import (
|
||||
@@ -16,7 +17,6 @@ from clan_cli.completions import (
|
||||
complete_machines,
|
||||
complete_services_for_machine,
|
||||
)
|
||||
from clan_cli.git import commit_files
|
||||
from clan_cli.machines.list import list_machines
|
||||
from clan_cli.machines.machines import Machine
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from clan_lib.cmd import Log, RunOpts, run
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.nix import nix_shell
|
||||
|
||||
from .locked_open import locked_open
|
||||
|
||||
|
||||
def commit_file(
|
||||
file_path: Path,
|
||||
repo_dir: Path,
|
||||
commit_message: str | None = None,
|
||||
) -> None:
|
||||
"""Commit a file to a git repository.
|
||||
|
||||
:param file_path: The path to the file to commit.
|
||||
:param repo_dir: The path to the git repository.
|
||||
:param commit_message: The commit message.
|
||||
:raises ClanError: If the file is not in the git repository.
|
||||
"""
|
||||
commit_files([file_path], repo_dir, commit_message)
|
||||
|
||||
|
||||
# generic vcs agnostic commit function
|
||||
def commit_files(
|
||||
file_paths: list[Path],
|
||||
repo_dir: Path,
|
||||
commit_message: str | None = None,
|
||||
) -> None:
|
||||
if os.environ.get("CLAN_NO_COMMIT", None):
|
||||
return
|
||||
if not file_paths:
|
||||
return
|
||||
# check that the file is in the git repository
|
||||
for file_path in file_paths:
|
||||
if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()):
|
||||
msg = f"File {file_path} is not in the git repository {repo_dir}"
|
||||
raise ClanError(msg)
|
||||
# generate commit message if not provided
|
||||
if commit_message is None:
|
||||
commit_message = ""
|
||||
for file_path in file_paths:
|
||||
# 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_paths, commit_message)
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def _commit_file_to_git(
|
||||
repo_dir: Path, file_paths: list[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.
|
||||
"""
|
||||
dotgit = repo_dir / ".git"
|
||||
real_git_dir = repo_dir / ".git"
|
||||
# resolve worktree
|
||||
if dotgit.is_file():
|
||||
actual_git_dir = dotgit.read_text().strip()
|
||||
if not actual_git_dir.startswith("gitdir: "):
|
||||
msg = f"Invalid .git file: {actual_git_dir}"
|
||||
raise ClanError(msg)
|
||||
real_git_dir = repo_dir / actual_git_dir[len("gitdir: ") :]
|
||||
|
||||
with locked_open(real_git_dir / "clan.lock", "w+"):
|
||||
for file_path in file_paths:
|
||||
cmd = nix_shell(
|
||||
["git"],
|
||||
["git", "-C", str(repo_dir), "add", "--", str(file_path)],
|
||||
)
|
||||
# add the file to the git index
|
||||
|
||||
run(
|
||||
cmd,
|
||||
RunOpts(
|
||||
log=Log.BOTH,
|
||||
error_msg=f"Failed to add {file_path} file to git index",
|
||||
),
|
||||
)
|
||||
|
||||
# check if there is a diff
|
||||
cmd = nix_shell(
|
||||
["git"],
|
||||
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", "--"]
|
||||
+ [str(file_path) for file_path in file_paths],
|
||||
)
|
||||
result = run(cmd, RunOpts(check=False, cwd=repo_dir))
|
||||
# if there is no diff, return
|
||||
if result.returncode == 0:
|
||||
return
|
||||
|
||||
# commit only that file
|
||||
cmd = nix_shell(
|
||||
["git"],
|
||||
[
|
||||
"git",
|
||||
"-C",
|
||||
str(repo_dir),
|
||||
"commit",
|
||||
"-m",
|
||||
commit_message,
|
||||
"--no-verify", # dont run pre-commit hooks
|
||||
]
|
||||
+ [str(file_path) for file_path in file_paths],
|
||||
)
|
||||
|
||||
run(
|
||||
cmd,
|
||||
RunOpts(
|
||||
error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}"
|
||||
),
|
||||
)
|
||||
@@ -18,6 +18,7 @@ from typing import Any
|
||||
from clan_lib.api import API
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.git import commit_file
|
||||
from clan_lib.nix_models.inventory import Inventory
|
||||
from clan_lib.persist.inventory_store import WriteInfo
|
||||
from clan_lib.persist.util import (
|
||||
@@ -27,8 +28,6 @@ from clan_lib.persist.util import (
|
||||
determine_writeability,
|
||||
)
|
||||
|
||||
from clan_cli.git import commit_file
|
||||
|
||||
|
||||
def get_inventory_path(flake: Flake) -> Path:
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,7 @@ from clan_lib.api import API
|
||||
from clan_lib.dirs import get_clan_flake_toplevel_or_env
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.git import commit_file
|
||||
from clan_lib.nix_models.inventory import (
|
||||
Machine as InventoryMachine,
|
||||
)
|
||||
@@ -18,7 +19,6 @@ from clan_lib.persist.inventory_store import InventoryStore
|
||||
from clan_lib.persist.util import apply_patch
|
||||
|
||||
from clan_cli.completions import add_dynamic_completer, complete_tags
|
||||
from clan_cli.git import commit_file
|
||||
from clan_cli.machines.list import list_machines
|
||||
from clan_cli.templates import (
|
||||
InputPrio,
|
||||
|
||||
@@ -9,10 +9,10 @@ from clan_lib.api import API
|
||||
from clan_lib.cmd import RunOpts, run
|
||||
from clan_lib.dirs import specific_machine_dir
|
||||
from clan_lib.errors import ClanCmdError, ClanError
|
||||
from clan_lib.git import commit_file
|
||||
from clan_lib.nix import nix_config, nix_eval
|
||||
|
||||
from clan_cli.completions import add_dynamic_completer, complete_machines
|
||||
from clan_cli.git import commit_file
|
||||
from clan_cli.machines.machines import Machine
|
||||
|
||||
from .types import machine_name_type
|
||||
|
||||
@@ -4,6 +4,7 @@ from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from clan_cli.completions import (
|
||||
add_dynamic_completer,
|
||||
@@ -12,7 +13,6 @@ from clan_cli.completions import (
|
||||
complete_secrets,
|
||||
complete_users,
|
||||
)
|
||||
from clan_cli.git import commit_files
|
||||
from clan_cli.machines.types import machine_name_type, validate_hostname
|
||||
|
||||
from . import secrets
|
||||
|
||||
@@ -4,8 +4,7 @@ import logging
|
||||
import sys
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
|
||||
from clan_cli.git import commit_files
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from . import sops
|
||||
from .secrets import update_secrets
|
||||
|
||||
@@ -2,13 +2,13 @@ import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from clan_cli.completions import (
|
||||
add_dynamic_completer,
|
||||
complete_machines,
|
||||
complete_secrets,
|
||||
)
|
||||
from clan_cli.git import commit_files
|
||||
from clan_cli.machines.types import machine_name_type, validate_hostname
|
||||
|
||||
from . import secrets, sops
|
||||
|
||||
@@ -10,6 +10,7 @@ from pathlib import Path
|
||||
from typing import IO
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from clan_cli.completions import (
|
||||
add_dynamic_completer,
|
||||
@@ -18,7 +19,6 @@ from clan_cli.completions import (
|
||||
complete_secrets,
|
||||
complete_users,
|
||||
)
|
||||
from clan_cli.git import commit_files
|
||||
|
||||
from . import sops
|
||||
from .folders import (
|
||||
|
||||
@@ -6,9 +6,9 @@ from collections.abc import Iterable
|
||||
from pathlib import Path
|
||||
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from clan_cli.completions import add_dynamic_completer, complete_secrets, complete_users
|
||||
from clan_cli.git import commit_files
|
||||
|
||||
from . import groups, secrets, sops
|
||||
from .filters import get_secrets_filter_for_user
|
||||
|
||||
@@ -5,7 +5,6 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from clan_cli.git import commit_file
|
||||
from clan_cli.locked_open import locked_open
|
||||
from clan_cli.templates import (
|
||||
ClanExports,
|
||||
@@ -19,6 +18,7 @@ from clan_cli.templates import (
|
||||
from clan_cli.tests.fixtures_flakes import FlakeForTest
|
||||
from clan_lib.cmd import run
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.git import commit_file
|
||||
from clan_lib.nix import nix_command
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from clan_cli import git
|
||||
from clan_lib import git
|
||||
from clan_lib.errors import ClanError
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ from clan_cli.completions import (
|
||||
complete_machines,
|
||||
complete_services_for_machine,
|
||||
)
|
||||
from clan_cli.git import commit_files
|
||||
from clan_cli.machines.list import list_machines
|
||||
from clan_cli.vars._types import StoreBase
|
||||
from clan_cli.vars.migration import check_can_migrate, migrate_files
|
||||
@@ -22,6 +21,7 @@ from clan_lib.api import API
|
||||
from clan_lib.cmd import RunOpts, run
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.git import commit_files
|
||||
from clan_lib.nix import nix_config, nix_shell, nix_test_store
|
||||
|
||||
from .check import check_vars
|
||||
|
||||
@@ -2,8 +2,8 @@ import logging
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from clan_cli.git import commit_files
|
||||
from clan_lib.errors import ClanError
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ import logging
|
||||
import sys
|
||||
|
||||
from clan_cli.completions import add_dynamic_completer, complete_machines
|
||||
from clan_cli.git import commit_files
|
||||
from clan_cli.machines.machines import Machine
|
||||
from clan_cli.vars.get import get_var
|
||||
from clan_cli.vars.prompt import PromptType
|
||||
from clan_lib.flake import Flake
|
||||
from clan_lib.git import commit_files
|
||||
|
||||
from .generate import Var
|
||||
from .prompt import ask
|
||||
|
||||
Reference in New Issue
Block a user