Skip to content

Commit b4469cf

Browse files
authored
Git - add setting to control default branch name (microsoft#181884)
* Initial implementation * Refactor based on discussions * More pull request feedback
1 parent 4bbf1ad commit b4469cf

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

extensions/git/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,12 @@
20732073
"markdownDescription": "%config.autofetchPeriod%",
20742074
"default": 180
20752075
},
2076+
"git.defaultBranchName": {
2077+
"type": "string",
2078+
"description": "%config.defaultBranchName%",
2079+
"default": "main",
2080+
"scope": "resource"
2081+
},
20762082
"git.branchPrefix": {
20772083
"type": "string",
20782084
"description": "%config.branchPrefix%",

extensions/git/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"config.checkoutType.local": "Local branches",
132132
"config.checkoutType.tags": "Tags",
133133
"config.checkoutType.remote": "Remote branches",
134+
"config.defaultBranchName": "The name of the default branch (ex: main, trunk, development) when initializing a new git repository. When set to empty, the default branch name configured in git will be used.",
134135
"config.branchPrefix": "Prefix used when creating a new branch.",
135136
"config.branchProtection": "List of protected branches. By default, a prompt is shown before changes are committed to a protected branch. The prompt can be controlled using the `#git.branchProtectionPrompt#` setting.",
136137
"config.branchProtectionPrompt": "Controls whether a prompt is being shown before changes are committed to a protected branch.",

extensions/git/src/commands.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ function getCheckoutProcessor(repository: Repository, type: string): CheckoutPro
307307
return undefined;
308308
}
309309

310+
function sanitizeBranchName(name: string, whitespaceChar: string): string {
311+
return name.trim().replace(/^-+/, '').replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, whitespaceChar);
312+
}
313+
310314
function sanitizeRemoteName(name: string) {
311315
name = name.trim();
312316
return name && name.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, '-');
@@ -772,7 +776,11 @@ export class CommandCenter {
772776
}
773777
}
774778

775-
await this.git.init(repositoryPath);
779+
const config = workspace.getConfiguration('git');
780+
const defaultBranchName = config.get<string>('defaultBranchName', 'main');
781+
const branchWhitespaceChar = config.get<string>('branchWhitespaceChar', '-');
782+
783+
await this.git.init(repositoryPath, { defaultBranch: sanitizeBranchName(defaultBranchName, branchWhitespaceChar) });
776784

777785
let message = l10n.t('Would you like to open the initialized repository?');
778786
const open = l10n.t('Open');
@@ -2179,9 +2187,6 @@ export class CommandCenter {
21792187
const branchPrefix = config.get<string>('branchPrefix')!;
21802188
const branchWhitespaceChar = config.get<string>('branchWhitespaceChar')!;
21812189
const branchValidationRegex = config.get<string>('branchValidationRegex')!;
2182-
const sanitize = (name: string) => name ?
2183-
name.trim().replace(/^-+/, '').replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, branchWhitespaceChar)
2184-
: name;
21852190

21862191
let rawBranchName = defaultName;
21872192

@@ -2206,7 +2211,7 @@ export class CommandCenter {
22062211
ignoreFocusOut: true,
22072212
validateInput: (name: string) => {
22082213
const validateName = new RegExp(branchValidationRegex);
2209-
const sanitizedName = sanitize(name);
2214+
const sanitizedName = sanitizeBranchName(name, branchWhitespaceChar);
22102215
if (validateName.test(sanitizedName)) {
22112216
// If the sanitized name that we will use is different than what is
22122217
// in the input box, show an info message to the user informing them
@@ -2224,7 +2229,7 @@ export class CommandCenter {
22242229
});
22252230
}
22262231

2227-
return sanitize(rawBranchName || '');
2232+
return sanitizeBranchName(rawBranchName || '', branchWhitespaceChar);
22282233
}
22292234

22302235
private async _branch(repository: Repository, defaultName?: string, from = false): Promise<void> {

extensions/git/src/git.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,14 @@ export class Git {
401401
return new Repository(this, repository, dotGit, logger);
402402
}
403403

404-
async init(repository: string): Promise<void> {
405-
await this.exec(repository, ['init']);
406-
return;
404+
async init(repository: string, options: { defaultBranch?: string } = {}): Promise<void> {
405+
const args = ['init'];
406+
407+
if (options.defaultBranch && options.defaultBranch !== '') {
408+
args.push('-b', options.defaultBranch);
409+
}
410+
411+
await this.exec(repository, args);
407412
}
408413

409414
async clone(url: string, options: ICloneOptions, cancellationToken?: CancellationToken): Promise<string> {

0 commit comments

Comments
 (0)