better error messages if git commands fail
All checks were successful
checks-impure / test (pull_request) Successful in 9s
checks / test (pull_request) Successful in 24s

This commit is contained in:
Jörg Thalheim 2023-09-22 16:25:30 +02:00
parent fa9e8d757d
commit d9fd0e2a65

View File

@ -1,3 +1,4 @@
import shlex
import subprocess
from pathlib import Path
from typing import Optional
@ -41,15 +42,38 @@ def _commit_file_to_git(repo_dir: Path, file_path: Path, commit_message: str) ->
:param commit_message: The commit message.
:raises ClanError: If the file is not in the git repository.
"""
cmd = nix_shell(
["git"],
["git", "-C", str(repo_dir), "add", str(file_path)],
)
# add the file to the git index
subprocess.run(["git", "add", file_path], cwd=repo_dir, check=True)
try:
subprocess.run(cmd, cwd=repo_dir, check=True)
except subprocess.CalledProcessError as e:
raise ClanError(
f"Failed to add {file_path} to git repository {repo_dir}:\n{shlex.join(cmd)}\n exited with {e.returncode}"
) from e
# commit only that file
cmd = nix_shell(
["git"],
["git", "commit", "-m", commit_message, str(file_path.relative_to(repo_dir))],
)
subprocess.run(
cmd,
cwd=repo_dir,
check=True,
[
"git",
"-C",
str(repo_dir),
"commit",
"-m",
commit_message,
str(file_path.relative_to(repo_dir)),
],
)
try:
subprocess.run(
cmd,
cwd=repo_dir,
check=True,
)
except subprocess.CalledProcessError as e:
raise ClanError(
f"Failed to commit {file_path} to git repository {repo_dir}:\n{shlex.join(cmd)}\n exited with {e.returncode}"
) from e