Skip to content

Commit 3deb186

Browse files
committed
Renames utils for clarity
1 parent 2b7ed4b commit 3deb186

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

src/commands/diffWith.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Container } from '../container';
55
import type { GitCommit } from '../git/models/commit';
66
import { isCommit } from '../git/models/commit';
77
import { deletedOrMissing } from '../git/models/revision';
8-
import { isShaLike, isUncommitted, shortenRevision } from '../git/utils/revision.utils';
8+
import { isShaWithOptionalRevisionSuffix, isUncommitted, shortenRevision } from '../git/utils/revision.utils';
99
import { showGenericErrorMessage } from '../messages';
1010
import { command } from '../system/-webview/command';
1111
import { openDiffEditor } from '../system/-webview/vscode/editors';
@@ -100,11 +100,11 @@ export class DiffWithCommand extends GlCommandBase {
100100
[args.lhs.sha, args.rhs.sha] = await Promise.all([
101101
await this.container.git.refs(args.repoPath).resolveReference(args.lhs.sha, args.lhs.uri, {
102102
// If the ref looks like a sha, don't wait too long, since it should work
103-
timeout: isShaLike(args.lhs.sha) ? 100 : undefined,
103+
timeout: isShaWithOptionalRevisionSuffix(args.lhs.sha) ? 100 : undefined,
104104
}),
105105
await this.container.git.refs(args.repoPath).resolveReference(args.rhs.sha, args.rhs.uri, {
106106
// If the ref looks like a sha, don't wait too long, since it should work
107-
timeout: isShaLike(args.rhs.sha) ? 100 : undefined,
107+
timeout: isShaWithOptionalRevisionSuffix(args.rhs.sha) ? 100 : undefined,
108108
}),
109109
]);
110110

src/env/node/git/sub-providers/refs.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import type { GitReference } from '../../../../git/models/reference';
88
import { deletedOrMissing } from '../../../../git/models/revision';
99
import type { GitTag } from '../../../../git/models/tag';
1010
import { createReference } from '../../../../git/utils/reference.utils';
11-
import { isSha, isShaLike, isUncommitted, isUncommittedParent } from '../../../../git/utils/revision.utils';
11+
import {
12+
isSha,
13+
isShaWithOptionalRevisionSuffix,
14+
isUncommitted,
15+
isUncommittedWithParentSuffix,
16+
} from '../../../../git/utils/revision.utils';
1217
import { TimedCancellationSource } from '../../../../system/-webview/cancellation';
1318
import { log } from '../../../../system/decorators/log';
1419
import { Logger } from '../../../../system/logger';
@@ -72,7 +77,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
7277

7378
if (!(await this.isValidReference(repoPath, ref))) return undefined;
7479

75-
if (ref !== 'HEAD' && !isShaLike(ref)) {
80+
if (ref !== 'HEAD' && !isShaWithOptionalRevisionSuffix(ref)) {
7681
const branch = await this.provider.branches.getBranch(repoPath, ref);
7782
if (branch != null) {
7883
return createReference(branch.ref, repoPath, {
@@ -150,7 +155,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
150155
pathOrUri?: string | Uri,
151156
options?: { force?: boolean; timeout?: number },
152157
): Promise<string> {
153-
if (pathOrUri != null && isUncommittedParent(ref)) {
158+
if (pathOrUri != null && isUncommittedWithParentSuffix(ref)) {
154159
ref = 'HEAD';
155160
}
156161

@@ -165,7 +170,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
165170

166171
if (pathOrUri == null) {
167172
// If it doesn't look like a sha at all (e.g. branch name) or is a stash ref (^3) don't try to resolve it
168-
if ((!options?.force && !isShaLike(ref)) || ref.endsWith('^3')) return ref;
173+
if ((!options?.force && !isShaWithOptionalRevisionSuffix(ref)) || ref.endsWith('^3')) return ref;
169174

170175
return (await this.validateReference(repoPath, ref)) ?? ref;
171176
}

src/git/models/commit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { GitUri } from '../gitUri';
1616
import type { RemoteProvider } from '../remotes/remoteProvider';
1717
import { mapFilesWithStats } from '../utils/-webview/fileChange.utils';
1818
import { getChangedFilesCount } from '../utils/commit.utils';
19-
import { isSha, isUncommitted, isUncommittedParent, isUncommittedStaged } from '../utils/revision.utils';
19+
import { isSha, isUncommitted, isUncommittedStaged, isUncommittedWithParentSuffix } from '../utils/revision.utils';
2020
import type { GitDiffFileStats } from './diff';
2121
import type { GitFile } from './file';
2222
import { GitFileChange } from './fileChange';
@@ -195,7 +195,7 @@ export class GitCommit implements GitRevisionReference {
195195
this._resolvedPreviousSha ??
196196
(this.file != null ? this.file.previousSha : this.parents[0]) ??
197197
`${this.sha}^`;
198-
return isUncommittedParent(previousSha) ? 'HEAD' : previousSha;
198+
return isUncommittedWithParentSuffix(previousSha) ? 'HEAD' : previousSha;
199199
}
200200

201201
private _etagFileSystem: number | undefined;

src/git/utils/reference.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
GitTagReference,
99
} from '../models/reference';
1010
import { getBranchNameWithoutRemote, getRemoteNameFromBranchName } from './branch.utils';
11-
import { isRevisionRange, isShaParent, shortenRevision } from './revision.utils';
11+
import { isRevisionRange, isShaWithParentSuffix, shortenRevision } from './revision.utils';
1212

1313
interface GitBranchReferenceOptions {
1414
refType: 'branch';
@@ -170,7 +170,7 @@ export function getReferenceLabel(
170170
}
171171

172172
let prefix;
173-
if (options.expand && options.label && isShaParent(ref.ref)) {
173+
if (options.expand && options.label && isShaWithParentSuffix(ref.ref)) {
174174
refName = ref.name.endsWith('^') ? ref.name.substring(0, ref.name.length - 1) : ref.name;
175175
if (options?.quoted) {
176176
refName = `'${refName}'`;

src/git/utils/revision.utils.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,47 @@ const rangeRegex = /^([\w\-/]+(?:\.[\w\-/]+)*)?(\.\.\.?)([\w\-/]+(?:\.[\w\-/]+)*
55
const qualifiedRangeRegex = /^([\w\-/]+(?:\.[\w\-/]+)*)(\.\.\.?)([\w\-/]+(?:\.[\w\-/]+)*)$/;
66
const qualifiedDoubleDotRange = /^([\w\-/]+(?:\.[\w\-/]+)*)(\.\.)([\w\-/]+(?:\.[\w\-/]+)*)$/;
77
const qualifiedTripleDotRange = /^([\w\-/]+(?:\.[\w\-/]+)*)(\.\.\.)([\w\-/]+(?:\.[\w\-/]+)*)$/;
8-
const shaLikeRegex = /(^[0-9a-f]{40}([\^@~:]\S*)?$)|(^[0]{40}(:|-)$)/;
8+
const shaWithOptionalRevisionSuffixRegex = /(^[0-9a-f]{40}([\^@~:]\S*)?$)|(^[0]{40}(:|-)$)/;
99
const shaRegex = /(^[0-9a-f]{40}$)|(^[0]{40}(:|-)$)/;
10+
const shaShortRegex = /(^[0-9a-f]{7,40}$)|(^[0]{40}(:|-)$)/;
1011
const shaParentRegex = /(^[0-9a-f]{40})\^[0-3]?$/;
1112
const shaShortenRegex = /^(.*?)([\^@~:].*)?$/;
1213
const uncommittedRegex = /^[0]{40}(?:[\^@~:]\S*)?:?$/;
1314
const uncommittedStagedRegex = /^[0]{40}([\^@~]\S*)?:$/;
1415

15-
function isMatch(regex: RegExp, ref: string | undefined) {
16-
return !ref ? false : regex.test(ref);
16+
function isMatch(regex: RegExp, rev: string | undefined) {
17+
return !rev ? false : regex.test(rev);
1718
}
1819

19-
export function isSha(ref: string): boolean {
20-
return isMatch(shaRegex, ref);
20+
/** Checks if the rev looks like a SHA-1 hash
21+
* @param allowShort If true, allows short SHAs (7-40 characters)
22+
*/
23+
export function isSha(rev: string, allowShort: boolean = false): boolean {
24+
return isMatch(allowShort ? shaShortRegex : shaRegex, rev);
2125
}
2226

23-
export function isShaLike(ref: string): boolean {
24-
return isMatch(shaLikeRegex, ref);
27+
/** Checks if the rev looks like a SHA-1 hash with an optional revision navigation suffixes (like ^, @, ~, or :) */
28+
export function isShaWithOptionalRevisionSuffix(rev: string): boolean {
29+
return isMatch(shaWithOptionalRevisionSuffixRegex, rev);
2530
}
2631

27-
export function isShaParent(ref: string): boolean {
28-
return isMatch(shaParentRegex, ref);
32+
/** Checks if the rev looks like a SHA-1 hash with a ^ parent suffix */
33+
export function isShaWithParentSuffix(rev: string): boolean {
34+
return isMatch(shaParentRegex, rev);
2935
}
3036

31-
export function isUncommitted(ref: string | undefined, exact: boolean = false): boolean {
32-
return ref === uncommitted || ref === uncommittedStaged || (!exact && isMatch(uncommittedRegex, ref));
37+
export function isUncommitted(rev: string | undefined, exact: boolean = false): boolean {
38+
return rev === uncommitted || rev === uncommittedStaged || (!exact && isMatch(uncommittedRegex, rev));
3339
}
3440

35-
export function isUncommittedParent(
36-
ref: string | undefined,
37-
): ref is '0000000000000000000000000000000000000000^' | '0000000000000000000000000000000000000000:^' {
38-
return ref === `${uncommitted}^` || ref === `${uncommittedStaged}^`;
41+
export function isUncommittedStaged(rev: string | undefined, exact: boolean = false): boolean {
42+
return rev === uncommittedStaged || (!exact && isMatch(uncommittedStagedRegex, rev));
3943
}
4044

41-
export function isUncommittedStaged(ref: string | undefined, exact: boolean = false): boolean {
42-
return ref === uncommittedStaged || (!exact && isMatch(uncommittedStagedRegex, ref));
45+
export function isUncommittedWithParentSuffix(
46+
rev: string | undefined,
47+
): rev is '0000000000000000000000000000000000000000^' | '0000000000000000000000000000000000000000:^' {
48+
return rev === `${uncommitted}^` || rev === `${uncommittedStaged}^`;
4349
}
4450

4551
let abbreviatedShaLength = 7;
@@ -52,25 +58,25 @@ export function setAbbreviatedShaLength(length: number): void {
5258
}
5359

5460
export function shortenRevision(
55-
ref: string | undefined,
61+
rev: string | undefined,
5662
options?: {
5763
strings?: { uncommitted?: string; uncommittedStaged?: string; working?: string };
5864
},
5965
): string {
60-
if (ref === deletedOrMissing) return '(deleted)';
61-
if (!ref) return options?.strings?.working ?? '';
62-
if (isUncommitted(ref)) {
63-
return isUncommittedStaged(ref)
66+
if (rev === deletedOrMissing) return '(deleted)';
67+
if (!rev) return options?.strings?.working ?? '';
68+
if (isUncommitted(rev)) {
69+
return isUncommittedStaged(rev)
6470
? options?.strings?.uncommittedStaged ?? 'Index'
6571
: options?.strings?.uncommitted ?? 'Working Tree';
6672
}
67-
if (isRevisionRange(ref) || !isShaLike(ref)) return ref;
73+
if (isRevisionRange(rev) || !isShaWithOptionalRevisionSuffix(rev)) return rev;
6874

6975
// Don't allow shas to be shortened to less than 5 characters
7076
const len = Math.max(5, getAbbreviatedShaLength());
7177

7278
// If we have a suffix, append it
73-
const match = shaShortenRegex.exec(ref);
79+
const match = shaShortenRegex.exec(rev);
7480
if (match != null) {
7581
const [, rev, suffix] = match;
7682

@@ -79,7 +85,7 @@ export function shortenRevision(
7985
}
8086
}
8187

82-
return ref.substring(0, len);
88+
return rev.substring(0, len);
8389
}
8490

8591
export function createRevisionRange(
@@ -91,9 +97,9 @@ export function createRevisionRange(
9197
}
9298

9399
export function getRevisionRangeParts(
94-
ref: GitRevisionRange,
100+
revRange: GitRevisionRange,
95101
): { left: string | undefined; right: string | undefined; notation: GitRevisionRangeNotation } | undefined {
96-
const match = rangeRegex.exec(ref);
102+
const match = rangeRegex.exec(revRange);
97103
if (match == null) return undefined;
98104

99105
const [, left, notation, right] = match;
@@ -105,19 +111,19 @@ export function getRevisionRangeParts(
105111
}
106112

107113
export function isRevisionRange(
108-
ref: string | undefined,
114+
rev: string | undefined,
109115
rangeType: 'any' | 'qualified' | 'qualified-double-dot' | 'qualified-triple-dot' = 'any',
110-
): ref is GitRevisionRange {
111-
if (ref == null) return false;
116+
): rev is GitRevisionRange {
117+
if (rev == null) return false;
112118

113119
switch (rangeType) {
114120
case 'qualified':
115-
return qualifiedRangeRegex.test(ref);
121+
return qualifiedRangeRegex.test(rev);
116122
case 'qualified-double-dot':
117-
return qualifiedDoubleDotRange.test(ref);
123+
return qualifiedDoubleDotRange.test(rev);
118124
case 'qualified-triple-dot':
119-
return qualifiedTripleDotRange.test(ref);
125+
return qualifiedTripleDotRange.test(rev);
120126
default:
121-
return rangeRegex.test(ref);
127+
return rangeRegex.test(rev);
122128
}
123129
}

src/plus/integrations/providers/github/sub-providers/refs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { createReference } from '../../../../../git/utils/reference.utils';
1010
import {
1111
createRevisionRange,
1212
isSha,
13-
isShaLike,
13+
isShaWithOptionalRevisionSuffix,
1414
isUncommitted,
15-
isUncommittedParent,
15+
isUncommittedWithParentSuffix,
1616
} from '../../../../../git/utils/revision.utils';
1717
import { log } from '../../../../../system/decorators/log';
1818
import { Logger } from '../../../../../system/logger';
@@ -70,7 +70,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
7070

7171
if (!(await this.isValidReference(repoPath, ref))) return undefined;
7272

73-
if (ref !== 'HEAD' && !isShaLike(ref)) {
73+
if (ref !== 'HEAD' && !isShaWithOptionalRevisionSuffix(ref)) {
7474
const branch = await this.provider.branches.getBranch(repoPath, ref);
7575
if (branch != null) {
7676
return createReference(branch.ref, repoPath, {
@@ -128,7 +128,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
128128
pathOrUri?: string | Uri,
129129
_options?: { force?: boolean; timeout?: number },
130130
): Promise<string> {
131-
if (pathOrUri != null && isUncommittedParent(ref)) {
131+
if (pathOrUri != null && isUncommittedWithParentSuffix(ref)) {
132132
ref = 'HEAD';
133133
}
134134

@@ -144,7 +144,7 @@ export class RefsGitSubProvider implements GitRefsSubProvider {
144144
let relativePath;
145145
if (pathOrUri != null) {
146146
relativePath = this.provider.getRelativePath(pathOrUri, repoPath);
147-
} else if (!isShaLike(ref) || ref.endsWith('^3')) {
147+
} else if (!isShaWithOptionalRevisionSuffix(ref) || ref.endsWith('^3')) {
148148
// If it doesn't look like a sha at all (e.g. branch name) or is a stash ref (^3) don't try to resolve it
149149
return ref;
150150
}

0 commit comments

Comments
 (0)