Skip to content

Add configuration option to set the default branch name for new repos #653

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions builtin/init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,34 @@ static int create_default_files(const char *template_path,
die("failed to set up refs db: %s", err.buf);

/*
* Create the default symlink from ".git/HEAD" to the "master"
* branch, if it does not exist yet.
* Create the default symlink from ".git/HEAD" to the default
* branch name, if it does not exist yet.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

*/
path = git_path_buf(&buf, "HEAD");
reinit = (!access(path, R_OK)
|| readlink(path, junk, sizeof(junk)-1) != -1);
if (!reinit) {
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
const char *branch_name;
char *prefixed;

/* get the default branch name from config, or failing that, env */
if (git_config_get_string_const("init.defaultbranchname", &branch_name))
branch_name = getenv("GIT_DEFAULT_BRANCH_NAME");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use the GIT_TEST_ prefix here, otherwise the value will simply be unset by our test suite (meaning: we could not adjust the test suite incrementally).

Please use GIT_TEST_DEFAULT_BRANCH_NAME instead.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Very easy to miss.


if (!branch_name) {
branch_name = "master";
}

/* prepend "refs/heads/" to the branch name */
prefixed = xstrfmt("refs/heads/%s", branch_name);
if(check_refname_format(prefixed, 0)) {
die(_("Invalid value for default branch name %s"), branch_name);
}

if (create_symref("HEAD", prefixed, NULL) < 0)
exit(1);

free(prefixed);
Comment on lines +268 to +288

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you took @dscho's plan to consume the config directly here. Good.

I do think this method is complicated enough, so extracting your implementation might be good. There are also a few style issues with extra curly braces.

I made a fixup! commit for you at derrickstolee@a890fcb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. It is an abstraction at a different level: not just retrieving the config setting, but handling the default, the environment and the prefixing, too. I like it!

}

initialize_repository_version(fmt->hash_algo);
Expand Down
16 changes: 16 additions & 0 deletions t/t0001-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,20 @@ test_expect_success MINGW 'redirect std handles' '
grep "Needed a single revision" output.txt
'

test_expect_success 'custom default branch name from config' '
git config --global init.defaultbranchname nmb &&

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: tab here.

git init custom-config &&
git config --global --unset init.defaultbranchname &&
grep nmb custom-config/.git/HEAD
'

test_expect_success 'custom default branch name from env' '
GIT_DEFAULT_BRANCH_NAME=nmb git init custom-env &&
grep nmb custom-env/.git/HEAD
'
Comment on lines +474 to +477

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that your logic currently has the config option overriding the environment variable. I believe that is the wrong direction, but you should make the behavior explicit in a test. (I make this suggestion because I have hit that exact problem before.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the common pattern in Git is that if there are environment variables and config settings for the same feature, the environment variable overrides the config setting.


test_expect_fail 'invalid custom default branch name' '
GIT_DEFAULT_BRANCH_NAME=nmb. git init custom-invalid
'

test_done