2023-09-21 12:07:54 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-10-25 17:23:28 +00:00
|
|
|
# from clan_cli.dirs import find_git_repo_root
|
2024-01-10 19:33:41 +00:00
|
|
|
from clan_cli.errors import ClanError
|
2023-09-21 12:07:54 +00:00
|
|
|
from clan_cli.nix import nix_shell
|
|
|
|
|
2024-01-10 19:33:41 +00:00
|
|
|
from .cmd import Log, run
|
2024-01-10 17:39:19 +00:00
|
|
|
|
2023-09-21 12:07:54 +00:00
|
|
|
|
|
|
|
# generic vcs agnostic commit function
|
|
|
|
def commit_file(
|
|
|
|
file_path: Path,
|
2023-10-25 17:23:28 +00:00
|
|
|
repo_dir: Path,
|
2023-11-29 11:40:48 +00:00
|
|
|
commit_message: str | None = None,
|
2023-09-21 12:07:54 +00:00
|
|
|
) -> None:
|
|
|
|
# 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.
|
|
|
|
"""
|
2023-09-22 14:25:30 +00:00
|
|
|
cmd = nix_shell(
|
2023-12-08 14:00:11 +00:00
|
|
|
["nixpkgs#git"],
|
2023-09-22 14:25:30 +00:00
|
|
|
["git", "-C", str(repo_dir), "add", str(file_path)],
|
|
|
|
)
|
2023-09-21 12:07:54 +00:00
|
|
|
# add the file to the git index
|
2024-01-10 19:33:41 +00:00
|
|
|
|
|
|
|
run(cmd, log=Log.BOTH, error_msg=f"Failed to add {file_path} file to git index")
|
2023-09-22 14:25:30 +00:00
|
|
|
|
2023-09-22 14:38:46 +00:00
|
|
|
# check if there is a diff
|
|
|
|
cmd = nix_shell(
|
2023-12-08 14:00:11 +00:00
|
|
|
["nixpkgs#git"],
|
2023-11-13 12:59:20 +00:00
|
|
|
["git", "-C", str(repo_dir), "diff", "--cached", "--exit-code", str(file_path)],
|
2023-09-22 14:38:46 +00:00
|
|
|
)
|
2024-01-10 17:46:54 +00:00
|
|
|
result = run(cmd, check=False, cwd=repo_dir)
|
2023-09-22 14:38:46 +00:00
|
|
|
# if there is no diff, return
|
|
|
|
if result.returncode == 0:
|
|
|
|
return
|
|
|
|
|
2023-09-21 12:07:54 +00:00
|
|
|
# commit only that file
|
2023-09-22 11:24:25 +00:00
|
|
|
cmd = nix_shell(
|
2023-12-08 14:00:11 +00:00
|
|
|
["nixpkgs#git"],
|
2023-09-22 14:25:30 +00:00
|
|
|
[
|
|
|
|
"git",
|
|
|
|
"-C",
|
|
|
|
str(repo_dir),
|
|
|
|
"commit",
|
|
|
|
"-m",
|
|
|
|
commit_message,
|
|
|
|
str(file_path.relative_to(repo_dir)),
|
|
|
|
],
|
2023-09-21 12:07:54 +00:00
|
|
|
)
|
2024-01-10 19:33:41 +00:00
|
|
|
|
|
|
|
run(cmd, error_msg=f"Failed to commit {file_path} to git repository {repo_dir}")
|