From 28b51a4ddac95fc2323d4e43f5420ce7205e648c Mon Sep 17 00:00:00 2001 From: batsev <42235938+batsev@users.noreply.github.com> Date: Tue, 1 Apr 2025 21:30:32 +0300 Subject: [PATCH] Git - validate branch name before creation (#245029) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Git - validate branch name before creation * Git - tweak validation message --------- Co-authored-by: Бацев Никита Александрович Co-authored-by: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> --- extensions/git/src/commands.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 176a95b5432..146a7bc826f 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -2867,6 +2867,7 @@ export class CommandCenter { const branchWhitespaceChar = config.get('branchWhitespaceChar')!; const branchValidationRegex = config.get('branchValidationRegex')!; const branchRandomNameEnabled = config.get('branchRandomName.enable', false); + const refs = await repository.getRefs({ pattern: 'refs/heads' }); if (defaultName) { return sanitizeBranchName(defaultName, branchWhitespaceChar); @@ -2884,6 +2885,13 @@ export class CommandCenter { const getValidationMessage = (name: string): string | InputBoxValidationMessage | undefined => { const validateName = new RegExp(branchValidationRegex); const sanitizedName = sanitizeBranchName(name, branchWhitespaceChar); + + // Check if branch name already exists + const existingBranch = refs.find(ref => ref.name === sanitizedName); + if (existingBranch) { + return l10n.t('Branch "{0}" already exists', sanitizedName); + } + 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