Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch name validation and whitespace config #50712

Merged
merged 2 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extensions/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,16 @@
"usesOnlineServices"
]
},
"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%",
Expand Down
4 changes: 3 additions & 1 deletion extensions/git/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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.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.",
Expand All @@ -93,4 +95,4 @@
"colors.ignored": "Color for ignored resources.",
"colors.conflict": "Color for resources with conflicts.",
"colors.submodule": "Color for submodule resources."
}
}
17 changes: 15 additions & 2 deletions extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,19 +1222,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<void> {
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, true);
}

Expand Down