Skip to content

Commit

Permalink
switch to rev-parse
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple committed May 26, 2020
1 parent 774c906 commit c33ce1d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
1 change: 1 addition & 0 deletions __test__/git-directory-helper.test.ts
Expand Up @@ -416,6 +416,7 @@ async function setup(testName: string): Promise<void> {
log1: jest.fn(),
remoteAdd: jest.fn(),
removeEnvironmentVariable: jest.fn(),
revParse: jest.fn(),
setEnvironmentVariable: jest.fn(),
shaExists: jest.fn(),
submoduleForeach: jest.fn(),
Expand Down
28 changes: 14 additions & 14 deletions dist/index.js
Expand Up @@ -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/')) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string>}
*/
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;
}
Expand Down
23 changes: 12 additions & 11 deletions src/git-command-manager.ts
Expand Up @@ -25,7 +25,6 @@ export interface IGitCommandManager {
): Promise<void>
configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
fetch(refSpec: string[], fetchDepth?: number): Promise<void>
getCommitSha(ref: string): Promise<string>
getWorkingDirectory(): string
init(): Promise<void>
isDetached(): Promise<boolean>
Expand All @@ -34,6 +33,7 @@ export interface IGitCommandManager {
log1(): Promise<string>
remoteAdd(remoteName: string, remoteUrl: string): Promise<void>
removeEnvironmentVariable(name: string): void
revParse(ref: string): Promise<string>
setEnvironmentVariable(name: string, value: string): void
shaExists(sha: string): Promise<boolean>
submoduleForeach(command: string, recursive: boolean): Promise<string>
Expand Down Expand Up @@ -195,16 +195,6 @@ class GitCommandManager {
})
}

async getCommitSha(ref: string): Promise<string> {
const output = await this.execGit([
'log',
'-1',
'--format=format:%H%n',
ref
])
return output.stdout.trim()
}

getWorkingDirectory(): string {
return this.workingDirectory
}
Expand Down Expand Up @@ -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<string>}
*/
async revParse(ref: string): Promise<string> {
const output = await this.execGit(['rev-parse', ref])
return output.stdout.trim()
}

setEnvironmentVariable(name: string, value: string): void {
this.gitEnv[name] = value
}
Expand Down
5 changes: 2 additions & 3 deletions src/ref-helper.ts
Expand Up @@ -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/
Expand All @@ -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
Expand Down

0 comments on commit c33ce1d

Please sign in to comment.