Git - add setting to control default branch name (#181884)

* Initial implementation

* Refactor based on discussions

* More pull request feedback
This commit is contained in:
Ladislau Szomoru 2023-05-10 20:52:13 +02:00 committed by GitHub
parent 4bbf1ad5fb
commit b4469cf109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View file

@ -2073,6 +2073,12 @@
"markdownDescription": "%config.autofetchPeriod%",
"default": 180
},
"git.defaultBranchName": {
"type": "string",
"description": "%config.defaultBranchName%",
"default": "main",
"scope": "resource"
},
"git.branchPrefix": {
"type": "string",
"description": "%config.branchPrefix%",

View file

@ -131,6 +131,7 @@
"config.checkoutType.local": "Local branches",
"config.checkoutType.tags": "Tags",
"config.checkoutType.remote": "Remote branches",
"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.",
"config.branchPrefix": "Prefix used when creating a new branch.",
"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.",
"config.branchProtectionPrompt": "Controls whether a prompt is being shown before changes are committed to a protected branch.",

View file

@ -307,6 +307,10 @@ function getCheckoutProcessor(repository: Repository, type: string): CheckoutPro
return undefined;
}
function sanitizeBranchName(name: string, whitespaceChar: string): string {
return name.trim().replace(/^-+/, '').replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, whitespaceChar);
}
function sanitizeRemoteName(name: string) {
name = name.trim();
return name && name.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, '-');
@ -772,7 +776,11 @@ export class CommandCenter {
}
}
await this.git.init(repositoryPath);
const config = workspace.getConfiguration('git');
const defaultBranchName = config.get<string>('defaultBranchName', 'main');
const branchWhitespaceChar = config.get<string>('branchWhitespaceChar', '-');
await this.git.init(repositoryPath, { defaultBranch: sanitizeBranchName(defaultBranchName, branchWhitespaceChar) });
let message = l10n.t('Would you like to open the initialized repository?');
const open = l10n.t('Open');
@ -2179,9 +2187,6 @@ export class CommandCenter {
const branchPrefix = config.get<string>('branchPrefix')!;
const branchWhitespaceChar = config.get<string>('branchWhitespaceChar')!;
const branchValidationRegex = config.get<string>('branchValidationRegex')!;
const sanitize = (name: string) => name ?
name.trim().replace(/^-+/, '').replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, branchWhitespaceChar)
: name;
let rawBranchName = defaultName;
@ -2206,7 +2211,7 @@ export class CommandCenter {
ignoreFocusOut: true,
validateInput: (name: string) => {
const validateName = new RegExp(branchValidationRegex);
const sanitizedName = sanitize(name);
const sanitizedName = sanitizeBranchName(name, branchWhitespaceChar);
if (validateName.test(sanitizedName)) {
// If the sanitized name that we will use is different than what is
// in the input box, show an info message to the user informing them
@ -2224,7 +2229,7 @@ export class CommandCenter {
});
}
return sanitize(rawBranchName || '');
return sanitizeBranchName(rawBranchName || '', branchWhitespaceChar);
}
private async _branch(repository: Repository, defaultName?: string, from = false): Promise<void> {

View file

@ -401,9 +401,14 @@ export class Git {
return new Repository(this, repository, dotGit, logger);
}
async init(repository: string): Promise<void> {
await this.exec(repository, ['init']);
return;
async init(repository: string, options: { defaultBranch?: string } = {}): Promise<void> {
const args = ['init'];
if (options.defaultBranch && options.defaultBranch !== '') {
args.push('-b', options.defaultBranch);
}
await this.exec(repository, args);
}
async clone(url: string, options: ICloneOptions, cancellationToken?: CancellationToken): Promise<string> {