pkgs/lib: verbose git commits

Make sure the user knowns that a git command is run.

From the issue #4588:
> It is confusing at times, when executing some CLI command, expecting change,
then checking git status but no changes are observed.

We now log:
- git add (debug)
- git commit (info)

The git commit information is formatted the following way:
```
Committed machines/backup-target to git
```

Alternatives:
Currently this shows to the user what happened.
But we might want to show the user what is being run.
We could print the information before invoking the `git commit` itself.
Informing the user of a potential password input window.

Closes #4588
This commit is contained in:
a-kenji
2025-10-01 11:46:01 +02:00
parent e593d5da34
commit 08e2048eeb

View File

@@ -1,3 +1,4 @@
import logging
import os import os
from pathlib import Path from pathlib import Path
@@ -6,6 +7,8 @@ from clan_lib.errors import ClanError
from clan_lib.locked_open import locked_open from clan_lib.locked_open import locked_open
from clan_lib.nix import nix_shell from clan_lib.nix import nix_shell
log = logging.getLogger(__name__)
def commit_file( def commit_file(
file_path: Path, file_path: Path,
@@ -88,6 +91,9 @@ def _commit_file_to_git(
), ),
) )
relative_path = file_path.relative_to(repo_dir)
log.debug(f"Adding {relative_path} to git")
# check if there is a diff # check if there is a diff
cmd = nix_shell( cmd = nix_shell(
["git"], ["git"],
@@ -120,3 +126,10 @@ def _commit_file_to_git(
error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}", error_msg=f"Failed to commit {file_paths} to git repository {repo_dir}",
), ),
) )
# Provide user feedback about successful commit
if len(file_paths) == 1:
relative_path = file_paths[0].relative_to(repo_dir)
log.info(f"Committed {relative_path} to git")
else:
log.info(f"Committed {len(file_paths)} files to git")