forked from stablyai/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-object-ref.ts
More file actions
26 lines (21 loc) · 784 Bytes
/
Copy pathcommit-object-ref.ts
File metadata and controls
26 lines (21 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { gitExecFileAsync } from './runner'
type GitExec = (args: string[]) => Promise<unknown>
const FULL_GIT_OBJECT_ID_PATTERN = /^[0-9a-f]{40}$/i
export function isFullGitObjectId(value: string): boolean {
return FULL_GIT_OBJECT_ID_PATTERN.test(value.trim())
}
export async function hasCommitObjectViaGitExec(gitExec: GitExec, ref: string): Promise<boolean> {
const candidate = ref.trim()
if (!isFullGitObjectId(candidate)) {
return false
}
try {
await gitExec(['rev-parse', '--verify', '--quiet', `${candidate}^{commit}`])
return true
} catch {
return false
}
}
export function hasLocalCommitObject(repoPath: string, ref: string): Promise<boolean> {
return hasCommitObjectViaGitExec((args) => gitExecFileAsync(args, { cwd: repoPath }), ref)
}