Skip to content

Commit c33ce1d

Browse files
committed
switch to rev-parse
1 parent 774c906 commit c33ce1d

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

__test__/git-directory-helper.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ async function setup(testName: string): Promise<void> {
416416
log1: jest.fn(),
417417
remoteAdd: jest.fn(),
418418
removeEnvironmentVariable: jest.fn(),
419+
revParse: jest.fn(),
419420
setEnvironmentVariable: jest.fn(),
420421
shaExists: jest.fn(),
421422
submoduleForeach: jest.fn(),

dist/index.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,7 @@ function testRef(git, ref, commit) {
35153515
if (upperRef.startsWith('REFS/HEADS/')) {
35163516
const branch = ref.substring('refs/heads/'.length);
35173517
return ((yield git.branchExists(true, `origin/${branch}`)) &&
3518-
commit === (yield git.getCommitSha(`refs/remotes/origin/${branch}`)));
3518+
commit === (yield git.revParse(`refs/remotes/origin/${branch}`)));
35193519
}
35203520
// refs/pull/
35213521
else if (upperRef.startsWith('REFS/PULL/')) {
@@ -3525,8 +3525,7 @@ function testRef(git, ref, commit) {
35253525
// refs/tags/
35263526
else if (upperRef.startsWith('REFS/TAGS/')) {
35273527
const tagName = ref.substring('refs/tags/'.length);
3528-
return ((yield git.tagExists(tagName)) &&
3529-
commit === (yield git.getCommitSha(commit)));
3528+
return ((yield git.tagExists(tagName)) && commit === (yield git.revParse(ref)));
35303529
}
35313530
// Unexpected
35323531
else {
@@ -5831,17 +5830,6 @@ class GitCommandManager {
58315830
}));
58325831
});
58335832
}
5834-
getCommitSha(ref) {
5835-
return __awaiter(this, void 0, void 0, function* () {
5836-
const output = yield this.execGit([
5837-
'log',
5838-
'-1',
5839-
'--format=format:%H%n',
5840-
ref
5841-
]);
5842-
return output.stdout.trim();
5843-
});
5844-
}
58455833
getWorkingDirectory() {
58465834
return this.workingDirectory;
58475835
}
@@ -5885,6 +5873,18 @@ class GitCommandManager {
58855873
removeEnvironmentVariable(name) {
58865874
delete this.gitEnv[name];
58875875
}
5876+
/**
5877+
* Resolves a ref to a SHA. For a branch or lightweight tag, the commit SHA is returned.
5878+
* For an annotated tag, the tag SHA is returned.
5879+
* @param {string} ref For example: 'refs/heads/master' or '/refs/tags/v1'
5880+
* @returns {Promise<string>}
5881+
*/
5882+
revParse(ref) {
5883+
return __awaiter(this, void 0, void 0, function* () {
5884+
const output = yield this.execGit(['rev-parse', ref]);
5885+
return output.stdout.trim();
5886+
});
5887+
}
58885888
setEnvironmentVariable(name, value) {
58895889
this.gitEnv[name] = value;
58905890
}

src/git-command-manager.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export interface IGitCommandManager {
2525
): Promise<void>
2626
configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
2727
fetch(refSpec: string[], fetchDepth?: number): Promise<void>
28-
getCommitSha(ref: string): Promise<string>
2928
getWorkingDirectory(): string
3029
init(): Promise<void>
3130
isDetached(): Promise<boolean>
@@ -34,6 +33,7 @@ export interface IGitCommandManager {
3433
log1(): Promise<string>
3534
remoteAdd(remoteName: string, remoteUrl: string): Promise<void>
3635
removeEnvironmentVariable(name: string): void
36+
revParse(ref: string): Promise<string>
3737
setEnvironmentVariable(name: string, value: string): void
3838
shaExists(sha: string): Promise<boolean>
3939
submoduleForeach(command: string, recursive: boolean): Promise<string>
@@ -195,16 +195,6 @@ class GitCommandManager {
195195
})
196196
}
197197

198-
async getCommitSha(ref: string): Promise<string> {
199-
const output = await this.execGit([
200-
'log',
201-
'-1',
202-
'--format=format:%H%n',
203-
ref
204-
])
205-
return output.stdout.trim()
206-
}
207-
208198
getWorkingDirectory(): string {
209199
return this.workingDirectory
210200
}
@@ -248,6 +238,17 @@ class GitCommandManager {
248238
delete this.gitEnv[name]
249239
}
250240

241+
/**
242+
* Resolves a ref to a SHA. For a branch or lightweight tag, the commit SHA is returned.
243+
* For an annotated tag, the tag SHA is returned.
244+
* @param {string} ref For example: 'refs/heads/master' or '/refs/tags/v1'
245+
* @returns {Promise<string>}
246+
*/
247+
async revParse(ref: string): Promise<string> {
248+
const output = await this.execGit(['rev-parse', ref])
249+
return output.stdout.trim()
250+
}
251+
251252
setEnvironmentVariable(name: string, value: string): void {
252253
this.gitEnv[name] = value
253254
}

src/ref-helper.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export async function testRef(
158158
const branch = ref.substring('refs/heads/'.length)
159159
return (
160160
(await git.branchExists(true, `origin/${branch}`)) &&
161-
commit === (await git.getCommitSha(`refs/remotes/origin/${branch}`))
161+
commit === (await git.revParse(`refs/remotes/origin/${branch}`))
162162
)
163163
}
164164
// refs/pull/
@@ -170,8 +170,7 @@ export async function testRef(
170170
else if (upperRef.startsWith('REFS/TAGS/')) {
171171
const tagName = ref.substring('refs/tags/'.length)
172172
return (
173-
(await git.tagExists(tagName)) &&
174-
commit === (await git.getCommitSha(commit))
173+
(await git.tagExists(tagName)) && commit === (await git.revParse(ref))
175174
)
176175
}
177176
// Unexpected

0 commit comments

Comments
 (0)