clan-cli git: add commit_files function
This commit is contained in:
parent
0e076e52c8
commit
8114cebaa4
@ -7,29 +7,49 @@ from clan_cli.nix import nix_shell
|
|||||||
from .cmd import Log, run
|
from .cmd import Log, run
|
||||||
|
|
||||||
|
|
||||||
# generic vcs agnostic commit function
|
|
||||||
def commit_file(
|
def commit_file(
|
||||||
file_path: Path,
|
file_path: Path,
|
||||||
repo_dir: Path,
|
repo_dir: Path,
|
||||||
commit_message: str | None = None,
|
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:
|
) -> None:
|
||||||
# check that the file is in the git repository and exists
|
# check that the file is in the git repository and exists
|
||||||
|
for file_path in file_paths:
|
||||||
if not Path(file_path).resolve().is_relative_to(repo_dir.resolve()):
|
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}")
|
raise ClanError(f"File {file_path} is not in the git repository {repo_dir}")
|
||||||
if not file_path.exists():
|
if not file_path.exists():
|
||||||
raise ClanError(f"File {file_path} does not exist")
|
raise ClanError(f"File {file_path} does not exist")
|
||||||
# generate commit message if not provided
|
# generate commit message if not provided
|
||||||
if commit_message is None:
|
if commit_message is None:
|
||||||
|
commit_message = ""
|
||||||
|
for file_path in file_paths:
|
||||||
# ensure that mentioned file path is relative to repo
|
# ensure that mentioned file path is relative to repo
|
||||||
commit_message = f"Add {file_path.relative_to(repo_dir)}"
|
commit_message += f"Add {file_path.relative_to(repo_dir)}"
|
||||||
# check if the repo is a git repo and commit
|
# check if the repo is a git repo and commit
|
||||||
if (repo_dir / ".git").exists():
|
if (repo_dir / ".git").exists():
|
||||||
_commit_file_to_git(repo_dir, file_path, commit_message)
|
_commit_file_to_git(repo_dir, file_paths, commit_message)
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) -> None:
|
def _commit_file_to_git(
|
||||||
|
repo_dir: Path, file_paths: list[Path], commit_message: str
|
||||||
|
) -> None:
|
||||||
"""Commit a file to a git repository.
|
"""Commit a file to a git repository.
|
||||||
|
|
||||||
:param repo_dir: The path to the git repository.
|
:param repo_dir: The path to the git repository.
|
||||||
@ -37,6 +57,7 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) ->
|
|||||||
:param commit_message: The commit message.
|
:param commit_message: The commit message.
|
||||||
:raises ClanError: If the file is not in the git repository.
|
:raises ClanError: If the file is not in the git repository.
|
||||||
"""
|
"""
|
||||||
|
for file_path in file_paths:
|
||||||
cmd = nix_shell(
|
cmd = nix_shell(
|
||||||
["nixpkgs#git"],
|
["nixpkgs#git"],
|
||||||
["git", "-C", str(repo_dir), "add", str(file_path)],
|
["git", "-C", str(repo_dir), "add", str(file_path)],
|
||||||
@ -48,7 +69,8 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) ->
|
|||||||
# check if there is a diff
|
# check if there is a diff
|
||||||
cmd = nix_shell(
|
cmd = nix_shell(
|
||||||
["nixpkgs#git"],
|
["nixpkgs#git"],
|
||||||
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", str(file_path)],
|
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code"]
|
||||||
|
+ [str(file_path) for file_path in file_paths],
|
||||||
)
|
)
|
||||||
result = run(cmd, check=False, cwd=repo_dir)
|
result = run(cmd, check=False, cwd=repo_dir)
|
||||||
# if there is no diff, return
|
# if there is no diff, return
|
||||||
@ -65,8 +87,8 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) ->
|
|||||||
"commit",
|
"commit",
|
||||||
"-m",
|
"-m",
|
||||||
commit_message,
|
commit_message,
|
||||||
str(file_path.relative_to(repo_dir)),
|
]
|
||||||
],
|
+ [str(file_path) for file_path in file_paths],
|
||||||
)
|
)
|
||||||
|
|
||||||
run(cmd, error_msg=f"Failed to commit {file_path} to git repository {repo_dir}")
|
run(cmd, error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}")
|
||||||
|
Loading…
Reference in New Issue
Block a user