Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit 69cb7e9

Browse files
committed
Fix git initialization in post-generation hook
- Replace os.system() with subprocess.run() for better error handling - Add proper error handling and user feedback for git operations - Ensure git commands run in the correct directory with cwd parameter - Add fallback instructions if git initialization fails
1 parent 8801430 commit 69cb7e9

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

hooks/post_gen_project.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
"""Post-generation hook for cookiecutter-python-package."""
33

4-
import os
54
import shutil
5+
import subprocess
66
from pathlib import Path
77

88
# Get the project directory
@@ -57,9 +57,44 @@ def main() -> None:
5757
remove_file(cli_test_file)
5858

5959
# Create initial git repository
60-
os.system("git init")
61-
os.system("git add .")
62-
os.system('git commit -m "Initial commit from cookiecutter-python-package"')
60+
try:
61+
# Initialize git repository
62+
subprocess.run(["git", "init"], check=True, cwd=PROJECT_DIRECTORY)
63+
64+
# Configure git identity for the initial commit
65+
subprocess.run(
66+
["git", "config", "user.name", "{{ cookiecutter.full_name }}"],
67+
check=True,
68+
cwd=PROJECT_DIRECTORY,
69+
)
70+
subprocess.run(
71+
["git", "config", "user.email", "{{ cookiecutter.email }}"],
72+
check=True,
73+
cwd=PROJECT_DIRECTORY,
74+
)
75+
76+
# Add all files
77+
subprocess.run(["git", "add", "."], check=True, cwd=PROJECT_DIRECTORY)
78+
79+
# Create initial commit
80+
subprocess.run(
81+
[
82+
"git",
83+
"commit",
84+
"-m",
85+
"Initial commit from cookiecutter-python-package",
86+
],
87+
check=True,
88+
cwd=PROJECT_DIRECTORY,
89+
)
90+
print("✓ Git repository initialized with initial commit")
91+
92+
except subprocess.CalledProcessError as e:
93+
print(f"Warning: Git initialization failed: {e}")
94+
print("You can initialize git manually later with:")
95+
print(" git init")
96+
print(" git add .")
97+
print(' git commit -m "Initial commit"')
6398

6499
print(
65100
"\n*** Project '{ cookiecutter.project_name }' has been created successfully! ***"

0 commit comments

Comments
 (0)