Skip to content

Commit

Permalink
Change default initial branch to match github
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Feb 3, 2025
1 parent 83aa5cf commit d0a2108
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cookieplone/utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# SPDX-License-Identifier: MIT
from pathlib import Path

from git import Commit, Repo
from git.exc import InvalidGitRepositoryError
from git import Commit, Git, Repo
from git.exc import GitCommandError, InvalidGitRepositoryError


def repo_from_path(path: Path) -> Repo | None:
Expand All @@ -24,7 +24,11 @@ def check_path_is_repository(path: Path) -> bool:
def initialize_repository(path: Path) -> Repo:
"""Initialize a git repository and add all files."""
if not check_path_is_repository(path):
repo = Repo.init(path)
try:
initial_branch = Git().config("init.defaultBranch")
except GitCommandError:
initial_branch = "main"
repo = Repo.init(path, initial_branch=initial_branch)
repo.git.add(path)
else:
repo = Repo(path)
Expand Down
2 changes: 2 additions & 0 deletions news/135.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use `main` as the default branch for new git repositories, unless it has been
customized via the `init.defaultBranch` setting in git config. @davisagli
1 change: 1 addition & 0 deletions tests/utils/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_initialize_repository_existing_repo(tmp_repo):
def test_initialize_repository_new_repo(no_repo):
repo = git.initialize_repository(no_repo)
assert isinstance(repo, Repo)
assert repo.active_branch == "main"


def test_get_last_commit(tmp_repo):
Expand Down

0 comments on commit d0a2108

Please sign in to comment.