Skip to content

Commit 33dbdc3

Browse files
Ensure git configs if user does not have git settings
1 parent 329ca42 commit 33dbdc3

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

git_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from configparser import NoOptionError, NoSectionError
23
from typing import Optional, Union
34

45
from git import Repo
@@ -42,6 +43,24 @@ def _get_full_commit_message(message, module_name, frid, render_id) -> str:
4243
return full_message
4344

4445

46+
def _ensure_git_config(repo: Repo) -> None:
47+
config = repo.config_reader()
48+
49+
try:
50+
config.get_value("user", "name")
51+
except (NoSectionError, NoOptionError):
52+
# user.name not configured, set a default at repo level
53+
with repo.config_writer(config_level="repository") as writer:
54+
writer.set_value("user", "name", "Codeplain")
55+
56+
try:
57+
config.get_value("user", "email")
58+
except (NoSectionError, NoOptionError):
59+
# user.email not configured, set a default at repo level
60+
with repo.config_writer(config_level="repository") as writer:
61+
writer.set_value("user", "email", "codeplain@localhost")
62+
63+
4564
def init_git_repo(
4665
path_to_repo: Union[str, os.PathLike], module_name: Optional[str] = None, render_id: Optional[str] = None
4766
) -> Repo:
@@ -56,6 +75,7 @@ def init_git_repo(
5675
os.makedirs(path_to_repo)
5776

5877
repo = Repo.init(path_to_repo)
78+
_ensure_git_config(repo)
5979
repo.git.commit(
6080
"--allow-empty", "-m", _get_full_commit_message(INITIAL_COMMIT_MESSAGE, module_name, None, render_id)
6181
)

0 commit comments

Comments
 (0)