Repository: plone/cookieplone

Issue:
Use main as default branch instead of master branch to be in sync with github
The default generated branch is named "master" and should be named "main" to be in sync with github and the other repositories.  Being consistent reduces the failure of Github workflows.

Discussion:
@davisagli: @kittauri Cookieplone initializes a repository by calling the `git init` command, without specifying a branch name. So the default branch name comes from the user's git configuration.

On my machine I have this in my ~/.gitconfig file:
```
[init]
        defaultBranch = main
```
so I get a branch named `main` when I create a new project with Cookieplone.

We could make Cookieplone override the git configuration and explicitly create a default branch named `main`, but I'm not sure we should ignore the git configuration.@kittauri: @davisagli Ok, my mistake, I had not set the config file correctly. Question, should a project specify the name or should the user decide? Not sure about that. If this question is irrelevant, then you can close this issue.@davisagli: @kittauri It's not an irrelevant question, but the right answer is not obvious. My first gut feeling is that we should use the user's configuration. On the other hand, if the user hasn't configured this, then the default name in git (`master`) is different from the default name in github (`main`), which can cause confusion.

That confusion lands on a newbie who doesn't know how to fix it, while forcing the name to be `main` would maybe be annoying for an experienced developer, but they would know how to rename the branch and move on.

The whole point of cookieplone is to try to make things smooth for people who are new, so I'm rather leaning toward forcing the default branch to be named `main`.

@ericof Do you have an opinion here?@stevepiercy: +1 for `main`. It's the GitHub default now, it's easier for less experienced developers, and it avoids the racist baggage.@ericof: The branch main is also default in Git since some versions @davisagli: Ideally I think we should read the user's init.defaultBranch setting from git config. If it's there, use it. Otherwise default to `main`.

Solution patch:
diff --git a/README.md b/README.md
index 4355255..d24877e 100644
--- a/README.md
+++ b/README.md
@@ -125,6 +125,12 @@ Install all development dependencies, including Hatch, and create a local virtua
 make install
 ```
 
+### Run the checked out branch of Cookieplone
+
+```shell
+hatch run cookieplone
+```
+
 ### Check and format the codebase
 
 ```shell
diff --git a/cookieplone/utils/git.py b/cookieplone/utils/git.py
index e30372a..08a2406 100644
--- a/cookieplone/utils/git.py
+++ b/cookieplone/utils/git.py
@@ -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:
@@ -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)
diff --git a/news/135.bugfix b/news/135.bugfix
new file mode 100644
index 0000000..0706b70
--- /dev/null
+++ b/news/135.bugfix
@@ -0,0 +1,2 @@
+Use the `init.defaultBranch` setting in git `config` if available, else `main`, as the branch name for new git repositories. @davisagli
+customized via the `init.defaultBranch` setting in git config. @davisagli


Test patch:
diff --git a/tests/utils/test_git.py b/tests/utils/test_git.py
index 84b536f..691c6ea 100644
--- a/tests/utils/test_git.py
+++ b/tests/utils/test_git.py
@@ -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.name == "main"
 
 
 def test_get_last_commit(tmp_repo):