-
Notifications
You must be signed in to change notification settings - Fork 143
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
*/ | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to use the Please use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good!