-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgit.service.ts
30 lines (28 loc) · 946 Bytes
/
git.service.ts
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
27
28
29
30
import { exec } from "child_process"
import { parseGitRootPathOutput, trimLineEnding } from "./filename-utils"
export class GitService {
/**
* Finds the path of the local git directory.
* @returns A promise with the directory path
*/
getRootDirectory(): Promise<string> {
return new Promise((resolve, reject) => {
exec("git rev-parse --show-toplevel", (error, stdout, stderr) => {
const failed = error || stderr !== ""
resolve(failed ? __dirname : parseGitRootPathOutput(stdout))
})
})
}
/**
* Finds the current git commit.
* @returns A promise with the current git commit
*/
getCurrentCommit(): Promise<string> {
return new Promise((resolve, reject) => {
exec("git rev-list --no-merges --abbrev-commit -n 1 HEAD", (error, stdout, stderr) => {
const failed = error || stderr !== ""
resolve(failed ? "HEAD" : trimLineEnding(stdout))
})
})
}
}