Skip to content

Commit 6f05dcb

Browse files
committed
Adds range support to compare refs
1 parent 8d499ec commit 6f05dcb

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

src/git/models/models.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GlyphChars } from '../../constants';
55

66
const emptyStr = '';
77

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

9192
return ref.substr(0, len);
9293
}
94+
95+
export function splitRange(ref: string): { ref1: string; ref2: string; notation: '..' | '...' } | undefined {
96+
const match = rangeRegex.exec(ref);
97+
if (match == null) return undefined;
98+
99+
const [, ref1, notation, ref2] = match;
100+
return {
101+
ref1: ref1,
102+
notation: notation as '..' | '...',
103+
ref2: ref2,
104+
};
105+
}
93106
}
94107

95108
export interface GitBranchReference {

src/quickpicks/referencePicker.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export enum ReferencesQuickPickIncludes {
2020
}
2121

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

4343
quickpick.title = title;
44-
quickpick.placeholder = options.allowEnteringRefs
45-
? `${placeHolder}${GlyphChars.Space.repeat(3)}(or enter a reference using #)`
46-
: placeHolder;
44+
quickpick.placeholder =
45+
options.allowEnteringRefs != null
46+
? `${placeHolder}${GlyphChars.Space.repeat(3)}(or enter a reference using #)`
47+
: placeHolder;
4748
quickpick.matchOnDescription = true;
4849

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

8889
quickpick.show();
8990

90-
const getValidateGitReference = getValidateGitReferenceFn((await Container.git.getRepository(repoPath))!);
91+
const getValidateGitReference = getValidateGitReferenceFn((await Container.git.getRepository(repoPath))!, {
92+
ranges:
93+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
94+
options?.allowEnteringRefs && typeof options.allowEnteringRefs !== 'boolean'
95+
? options.allowEnteringRefs.ranges
96+
: undefined,
97+
});
9198

9299
quickpick.items = await items;
93100

@@ -105,6 +112,7 @@ export namespace ReferencePicker {
105112
resolve(quickpick.activeItems[0]);
106113
}),
107114
quickpick.onDidChangeValue(async e => {
115+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
108116
if (options.allowEnteringRefs) {
109117
if (!(await getValidateGitReference(quickpick, e))) {
110118
quickpick.items = await items;

src/views/searchAndCompareView.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,10 @@ export class SearchAndCompareViewNode extends ViewNode<SearchAndCompareView> {
189189
this.removeComparePicker(true);
190190

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

211212
ref = pick.ref;
212213

214+
if (GitRevision.isRange(ref)) {
215+
const range = GitRevision.splitRange(ref);
216+
if (range != null) {
217+
ref = range.ref1 || 'HEAD';
218+
ref2 = range.ref2 || 'HEAD';
219+
}
220+
}
221+
213222
prompt = true;
214223
}
215224

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

228237
if (prompt) {
229-
await this.compareWithSelected();
238+
await this.compareWithSelected(repoPath, ref2);
230239
}
231240
}
232241

0 commit comments

Comments
 (0)