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

Git quick open should correct invalid branch names #12194

7 changes: 5 additions & 2 deletions src/vs/workbench/parts/git/browser/gitQuickOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { localize } from 'vs/nls';
import { matchesContiguousSubString } from 'vs/base/common/filters';
import { TPromise } from 'vs/base/common/winjs.base';
import Severity from 'vs/base/common/severity';
import { IGitService, RefType, IRef, isValidBranchName } from 'vs/workbench/parts/git/common/git';
import { IGitService, RefType, IRef, isValidBranchName, correctBranchName } from 'vs/workbench/parts/git/common/git';
import { ICommand, CommandQuickOpenHandler } from 'vs/workbench/browser/quickopen';
import { Mode } from 'vs/base/parts/quickopen/common/quickOpen';
import { QuickOpenEntry, IHighlight, IContext, QuickOpenEntryGroup } from 'vs/base/parts/quickopen/browser/quickOpenModel';
Expand Down Expand Up @@ -190,7 +190,10 @@ class CheckoutCommand implements ICommand {
if (currentHeadMatches.length > 0) {
entries.unshift(new CurrentHeadEntry(this.gitService, this.messageService, currentHeadMatches[0].head, currentHeadMatches[0].highlights));

} else if (exactMatches.length === 0 && isValidBranchName(input)) {
} else if (exactMatches.length === 0) {
if (!isValidBranchName(input)) {
input = correctBranchName(input);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do the same for the git branch command?

}
const branchEntry = new BranchEntry(this.gitService, this.messageService, input);
entries.push(new QuickOpenEntryGroup(branchEntry, 'branch', checkoutEntries.length > 0 || remoteHeadEntries.length > 0));
}
Expand Down
6 changes: 5 additions & 1 deletion src/vs/workbench/parts/git/common/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ export interface IAskpassService {
}

// Utils
const invalidBranchPatternName = /(^\.)|(\/\.)|(\.\.)|(~)|(\^)|(:)|(\/$)|(\.lock$)|(\.lock\/)|(\\)|(\*)|(\s)|(^\s*$)|(\.$)/g;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wrapping everything in groups? The regex is now broken since the ^ and $ were making sure the whole string is matched.


export function isValidBranchName(value: string): boolean {
return !/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$/.test(value);
return !invalidBranchPatternName.test(value);
}
export function correctBranchName(branchName: string): string{
return branchName.replace(invalidBranchPatternName, '-');
}