diff --git a/__test__/git-directory-helper.test.ts b/__test__/git-directory-helper.test.ts index fac861929..7283102d3 100644 --- a/__test__/git-directory-helper.test.ts +++ b/__test__/git-directory-helper.test.ts @@ -416,6 +416,7 @@ async function setup(testName: string): Promise { log1: jest.fn(), remoteAdd: jest.fn(), removeEnvironmentVariable: jest.fn(), + revParse: jest.fn(), setEnvironmentVariable: jest.fn(), shaExists: jest.fn(), submoduleForeach: jest.fn(), diff --git a/dist/index.js b/dist/index.js index 2a73e3852..3dab1d1c4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3515,7 +3515,7 @@ function testRef(git, ref, commit) { if (upperRef.startsWith('REFS/HEADS/')) { const branch = ref.substring('refs/heads/'.length); return ((yield git.branchExists(true, `origin/${branch}`)) && - commit === (yield git.getCommitSha(`refs/remotes/origin/${branch}`))); + commit === (yield git.revParse(`refs/remotes/origin/${branch}`))); } // refs/pull/ else if (upperRef.startsWith('REFS/PULL/')) { @@ -3525,8 +3525,7 @@ function testRef(git, ref, commit) { // refs/tags/ else if (upperRef.startsWith('REFS/TAGS/')) { const tagName = ref.substring('refs/tags/'.length); - return ((yield git.tagExists(tagName)) && - commit === (yield git.getCommitSha(commit))); + return ((yield git.tagExists(tagName)) && commit === (yield git.revParse(ref))); } // Unexpected else { @@ -5831,17 +5830,6 @@ class GitCommandManager { })); }); } - getCommitSha(ref) { - return __awaiter(this, void 0, void 0, function* () { - const output = yield this.execGit([ - 'log', - '-1', - '--format=format:%H%n', - ref - ]); - return output.stdout.trim(); - }); - } getWorkingDirectory() { return this.workingDirectory; } @@ -5885,6 +5873,18 @@ class GitCommandManager { removeEnvironmentVariable(name) { delete this.gitEnv[name]; } + /** + * Resolves a ref to a SHA. For a branch or lightweight tag, the commit SHA is returned. + * For an annotated tag, the tag SHA is returned. + * @param {string} ref For example: 'refs/heads/master' or '/refs/tags/v1' + * @returns {Promise} + */ + revParse(ref) { + return __awaiter(this, void 0, void 0, function* () { + const output = yield this.execGit(['rev-parse', ref]); + return output.stdout.trim(); + }); + } setEnvironmentVariable(name, value) { this.gitEnv[name] = value; } diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 24f67fbcd..d4e305a13 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -25,7 +25,6 @@ export interface IGitCommandManager { ): Promise configExists(configKey: string, globalConfig?: boolean): Promise fetch(refSpec: string[], fetchDepth?: number): Promise - getCommitSha(ref: string): Promise getWorkingDirectory(): string init(): Promise isDetached(): Promise @@ -34,6 +33,7 @@ export interface IGitCommandManager { log1(): Promise remoteAdd(remoteName: string, remoteUrl: string): Promise removeEnvironmentVariable(name: string): void + revParse(ref: string): Promise setEnvironmentVariable(name: string, value: string): void shaExists(sha: string): Promise submoduleForeach(command: string, recursive: boolean): Promise @@ -195,16 +195,6 @@ class GitCommandManager { }) } - async getCommitSha(ref: string): Promise { - const output = await this.execGit([ - 'log', - '-1', - '--format=format:%H%n', - ref - ]) - return output.stdout.trim() - } - getWorkingDirectory(): string { return this.workingDirectory } @@ -248,6 +238,17 @@ class GitCommandManager { delete this.gitEnv[name] } + /** + * Resolves a ref to a SHA. For a branch or lightweight tag, the commit SHA is returned. + * For an annotated tag, the tag SHA is returned. + * @param {string} ref For example: 'refs/heads/master' or '/refs/tags/v1' + * @returns {Promise} + */ + async revParse(ref: string): Promise { + const output = await this.execGit(['rev-parse', ref]) + return output.stdout.trim() + } + setEnvironmentVariable(name: string, value: string): void { this.gitEnv[name] = value } diff --git a/src/ref-helper.ts b/src/ref-helper.ts index 87eefae3e..18780a59a 100644 --- a/src/ref-helper.ts +++ b/src/ref-helper.ts @@ -158,7 +158,7 @@ export async function testRef( const branch = ref.substring('refs/heads/'.length) return ( (await git.branchExists(true, `origin/${branch}`)) && - commit === (await git.getCommitSha(`refs/remotes/origin/${branch}`)) + commit === (await git.revParse(`refs/remotes/origin/${branch}`)) ) } // refs/pull/ @@ -170,8 +170,7 @@ export async function testRef( else if (upperRef.startsWith('REFS/TAGS/')) { const tagName = ref.substring('refs/tags/'.length) return ( - (await git.tagExists(tagName)) && - commit === (await git.getCommitSha(commit)) + (await git.tagExists(tagName)) && commit === (await git.revParse(ref)) ) } // Unexpected