From d9fd0e2a653757161bd52e67e6e7bd98c601b708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 22 Sep 2023 16:25:30 +0200 Subject: [PATCH] better error messages if git commands fail --- pkgs/clan-cli/clan_cli/git.py | 38 ++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/git.py b/pkgs/clan-cli/clan_cli/git.py index 013e07144..cc9b5e57b 100644 --- a/pkgs/clan-cli/clan_cli/git.py +++ b/pkgs/clan-cli/clan_cli/git.py @@ -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