-
Notifications
You must be signed in to change notification settings - Fork 18
Do not mandate global git config #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,35 @@ | ||
| #!/bin/env python3 | ||
| import sys | ||
| from subprocess import run | ||
|
|
||
| # Check if git user.name and user.email are configured --> fail early if it's not set | ||
| run(["git", "config", "user.name"], check=True) | ||
| run(["git", "config", "user.email"], check=True) | ||
|
|
||
| def git_config_get(key: str) -> str | None: | ||
| """Return the value of a git config key, or an empty string if it is not set.""" | ||
| result = run(["git", "config", key], capture_output=True, text=True) | ||
| return result.stdout.strip() if result.returncode == 0 else None | ||
|
|
||
|
|
||
| # use 'main' as default branch irrespective of git configuration | ||
| run(["git", "init", "--initial-branch=main", "."], check=True) | ||
|
|
||
| # Resolve the author identity for the initial commit. | ||
| # We do *not* mandate a global git config: some users configure git per-repository only. | ||
| # Prefer an existing git config (global or system) and fall back to the values the user | ||
| # entered in cookiecutter. | ||
| name = git_config_get("user.name") or "{{ cookiecutter.author_full_name }}".strip() | ||
| email = git_config_get("user.email") or "{{ cookiecutter.author_email }}".strip() | ||
|
|
||
| if not name or not email: | ||
| sys.exit( | ||
| "ERROR: could not determine an author name/email for the initial commit.\n" | ||
| "Either configure git (`git config --global user.name ...` and " | ||
| "`git config --global user.email ...`) or provide the author name/email " | ||
| "when prompted by the template." | ||
| ) | ||
|
|
||
| # Set the identity at the repo level only when it is not already resolvable from existing | ||
| # git config, so we never clobber the user's real git identity. | ||
| if not git_config_get("user.name"): | ||
| run(["git", "config", "user.name", name], check=True) | ||
| if not git_config_get("user.email"): | ||
| run(["git", "config", "user.email", email], check=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.