From 83da71325c6a6a63c4ef3937eecdf76ba58e07e6 Mon Sep 17 00:00:00 2001 From: Morten Henriksen Date: Tue, 29 May 2018 19:55:41 +0200 Subject: [PATCH 1/2] Add branch name validation and whitespace config * Adds `git.createBranchNameConvention.regexp` * Adds `git.createBranchWhitespaceChar` Fixes #50241 --- extensions/git/package.json | 10 ++++++++++ extensions/git/package.nls.json | 4 +++- extensions/git/src/commands.ts | 17 +++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index c2851ea34c2..4e126a5e626 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -895,6 +895,16 @@ "description": "%config.autofetch%", "default": false }, + "git.createBranchNameConvention.regexp": { + "type": "string", + "description": "%config.createBranchNameConvention.regexp%", + "default": "" + }, + "git.createBranchWhitespaceChar": { + "type": "string", + "description": "%config.createBranchWhitespaceChar%", + "default": "-" + }, "git.confirmSync": { "type": "boolean", "description": "%config.confirmSync%", diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index 7f2c43dd233..83405fe6980 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -54,6 +54,8 @@ "config.autoRepositoryDetection": "Whether repositories should be automatically detected", "config.autorefresh": "Whether auto refreshing is enabled", "config.autofetch": "Whether auto fetching is enabled", + "config.createBranchNameConvention.regexp": "The regex that is used to match create branch against convention.", + "config.createBranchWhitespaceChar": "The char to replace whitespace in branch name when creating branch.", "config.enableLongCommitWarning": "Whether long commit messages should be warned about", "config.confirmSync": "Confirm before synchronizing git repositories", "config.countBadge": "Controls the git badge counter. `all` counts all changes. `tracked` counts only the tracked changes. `off` turns it off.", @@ -77,4 +79,4 @@ "colors.ignored": "Color for ignored resources.", "colors.conflict": "Color for resources with conflicts.", "colors.submodule": "Color for submodule resources." -} \ No newline at end of file +} diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 1ca402bf14f..b38ca7a2544 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1171,19 +1171,32 @@ export class CommandCenter { await choice.run(repository); } + private removeBranchNameWhitespace(name: string) { + const config = workspace.getConfiguration('git'); + return name.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$/g, config.createBranchWhitespaceChar); + } + @command('git.branch', { repository: true }) async branch(repository: Repository): Promise { + const config = workspace.getConfiguration('git'); + const validateName = new RegExp(config.createBranchNameConvention.regexp); const result = await window.showInputBox({ placeHolder: localize('branch name', "Branch name"), prompt: localize('provide branch name', "Please provide a branch name"), - ignoreFocusOut: true + ignoreFocusOut: true, + validateInput: (name: string) => { + if (validateName.test(this.removeBranchNameWhitespace(name))) { + return null; + } + return `${localize('branch name format invalid', "Have to be in the format")} : ${config.createBranchNameConvention.regexp}`; + } }); if (!result) { return; } - const name = result.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$/g, '-'); + const name = this.removeBranchNameWhitespace(result); await repository.branch(name); } From 6c504e7ee36a760f805c459c169c86bb33d9d5c9 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Thu, 13 Sep 2018 15:35:58 +0200 Subject: [PATCH 2/2] :lipstick: --- extensions/git/package.json | 8 ++++---- extensions/git/package.nls.json | 6 +++--- extensions/git/src/commands.ts | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index 8cc40b3980b..71a59270211 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -936,14 +936,14 @@ "usesOnlineServices" ] }, - "git.createBranchNameConvention.regexp": { + "git.branchValidationRegex": { "type": "string", - "description": "%config.createBranchNameConvention.regexp%", + "description": "%config.branchValidationRegex%", "default": "" }, - "git.createBranchWhitespaceChar": { + "git.branchWhitespaceChar": { "type": "string", - "description": "%config.createBranchWhitespaceChar%", + "description": "%config.branchWhitespaceChar%", "default": "-" }, "git.confirmSync": { diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index df9b1a71615..2887582e89f 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -72,8 +72,8 @@ "config.checkoutType.local": "Show only local branches.", "config.checkoutType.tags": "Show only tags.", "config.checkoutType.remote": "Show only remote branches.", - "config.createBranchNameConvention.regexp": "The regex that is used to match create branch against convention.", - "config.createBranchWhitespaceChar": "The char to replace whitespace in branch name when creating branch.", + "config.branchValidationRegex": "A regular expression to validate new branch names.", + "config.branchWhitespaceChar": "The character to replace whitespace in new branch names.", "config.ignoreLegacyWarning": "Ignores the legacy Git warning.", "config.ignoreMissingGitWarning": "Ignores the warning when Git is missing.", "config.ignoreLimitWarning": "Ignores the warning when there are too many changes in a repository.", @@ -102,4 +102,4 @@ "colors.ignored": "Color for ignored resources.", "colors.conflict": "Color for resources with conflicts.", "colors.submodule": "Color for submodule resources." -} +} \ No newline at end of file diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 135f2b4a138..59225dd6f46 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1261,32 +1261,42 @@ export class CommandCenter { await choice.run(repository); } - private removeBranchNameWhitespace(name: string) { - const config = workspace.getConfiguration('git'); - return name.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.|\[|\]|$/g, config.createBranchWhitespaceChar); - } @command('git.branch', { repository: true }) async branch(repository: Repository): Promise { const config = workspace.getConfiguration('git'); - const validateName = new RegExp(config.createBranchNameConvention.regexp); + const branchValidationRegex = config.get('branchValidationRegex')!; + const branchWhitespaceChar = config.get('branchWhitespaceChar')!; + const validateName = new RegExp(branchValidationRegex); + const sanitize = (name: string) => { + name = name.trim(); + + if (!name) { + return name; + } + + return name.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.|\[|\]$/g, branchWhitespaceChar); + }; + const result = await window.showInputBox({ placeHolder: localize('branch name', "Branch name"), prompt: localize('provide branch name', "Please provide a branch name"), ignoreFocusOut: true, validateInput: (name: string) => { - if (validateName.test(this.removeBranchNameWhitespace(name))) { + if (validateName.test(sanitize(name))) { return null; } - return `${localize('branch name format invalid', "Have to be in the format")} : ${config.createBranchNameConvention.regexp}`; + + return localize('branch name format invalid', "Branch name needs to match regex: {0}", branchValidationRegex); } }); - if (!result) { + const name = sanitize(result || ''); + + if (!name) { return; } - const name = this.removeBranchNameWhitespace(result); await repository.branch(name, true); }