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
4 changed files with 26 additions and 9 deletions

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> {