Skip to content

Commit

Permalink
Adds range support to compare refs
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 20, 2020
1 parent 8d499ec commit 6f05dcb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/git/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GlyphChars } from '../../constants';

const emptyStr = '';

const rangeRegex = /^(\S*?)(\.\.\.?)(\S*)\s*$/;
const shaLikeRegex = /(^[0-9a-f]{40}([\^@~:]\S*)?$)|(^[0]{40}(:|-)$)/;
const shaRegex = /(^[0-9a-f]{40}$)|(^[0]{40}(:|-)$)/;
const shaParentRegex = /(^[0-9a-f]{40})\^[0-3]?$/;
Expand Down Expand Up @@ -90,6 +91,18 @@ export namespace GitRevision {

return ref.substr(0, len);
}

export function splitRange(ref: string): { ref1: string; ref2: string; notation: '..' | '...' } | undefined {
const match = rangeRegex.exec(ref);
if (match == null) return undefined;

const [, ref1, notation, ref2] = match;
return {
ref1: ref1,
notation: notation as '..' | '...',
ref2: ref2,
};
}
}

export interface GitBranchReference {
Expand Down
18 changes: 13 additions & 5 deletions src/quickpicks/referencePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export enum ReferencesQuickPickIncludes {
}

export interface ReferencesQuickPickOptions {
allowEnteringRefs?: boolean;
allowEnteringRefs?: boolean | { ranges?: boolean };
autoPick?: boolean;
picked?: string;
filter?: { branches?(b: GitBranch): boolean; tags?(t: GitTag): boolean };
Expand All @@ -41,9 +41,10 @@ export namespace ReferencePicker {
quickpick.ignoreFocusOut = getQuickPickIgnoreFocusOut();

quickpick.title = title;
quickpick.placeholder = options.allowEnteringRefs
? `${placeHolder}${GlyphChars.Space.repeat(3)}(or enter a reference using #)`
: placeHolder;
quickpick.placeholder =
options.allowEnteringRefs != null
? `${placeHolder}${GlyphChars.Space.repeat(3)}(or enter a reference using #)`
: placeHolder;
quickpick.matchOnDescription = true;

const disposables: Disposable[] = [];
Expand Down Expand Up @@ -87,7 +88,13 @@ export namespace ReferencePicker {

quickpick.show();

const getValidateGitReference = getValidateGitReferenceFn((await Container.git.getRepository(repoPath))!);
const getValidateGitReference = getValidateGitReferenceFn((await Container.git.getRepository(repoPath))!, {
ranges:
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
options?.allowEnteringRefs && typeof options.allowEnteringRefs !== 'boolean'
? options.allowEnteringRefs.ranges
: undefined,
});

quickpick.items = await items;

Expand All @@ -105,6 +112,7 @@ export namespace ReferencePicker {
resolve(quickpick.activeItems[0]);
}),
quickpick.onDidChangeValue(async e => {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (options.allowEnteringRefs) {
if (!(await getValidateGitReference(quickpick, e))) {
quickpick.items = await items;
Expand Down
13 changes: 11 additions & 2 deletions src/views/searchAndCompareView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {
this.removeComparePicker(true);

let prompt = options?.prompt ?? false;
let ref2;
if (ref == null) {
const pick = await ReferencePicker.show(repoPath, 'Compare', 'Choose a reference to compare', {
allowEnteringRefs: true,
allowEnteringRefs: { ranges: true },
// checkmarks: false,
include:
ReferencesQuickPickIncludes.BranchesAndTags |
Expand All @@ -210,6 +211,14 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {

ref = pick.ref;

if (GitRevision.isRange(ref)) {
const range = GitRevision.splitRange(ref);
if (range != null) {
ref = range.ref1 || 'HEAD';
ref2 = range.ref2 || 'HEAD';
}
}

prompt = true;
}

Expand All @@ -226,7 +235,7 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {
await this.view.reveal(this.comparePicker, { focus: false, select: true });

if (prompt) {
await this.compareWithSelected();
await this.compareWithSelected(repoPath, ref2);
}
}

Expand Down

0 comments on commit 6f05dcb

Please sign in to comment.