-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitdiffoutput.txt
47 lines (47 loc) · 1001 Bytes
/
gitdiffoutput.txt
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
diff --git a/source/git.ts b/source/git.ts
new file mode 100644
index 0000000..f236adb
--- /dev/null
+++ b/source/git.ts
@@ -0,0 +1,41 @@
+import {exec, ExecException as _4} from 'node:child_process';
+import {promisify} from 'node:util';
+
+const execAsync = promisify(exec);
+
+const getGitDiffOutput = async () => {
+ const {stdout, stderr} = await execAsync('git diff --staged');
+ if (stderr) {
+ throw new Error(stderr);
+ }
+
+ return stdout;
+};
+
+const isInGitRepository = async () => {
+ try {
+ const {stdout, stderr} = await execAsync('git rev-parse --is-inside-work-tree');
+ if (stderr) {
+ throw new Error(stderr);
+ }
+
+ return stdout.trim() === 'true';
+ } catch {
+ return false;
+ }
+};
+
+const execGitCommit = async (commitMessage: string) => {
+ const {stdout, stderr} = await execAsync(`git commit -m "${commitMessage}"`);
+ if (stderr) {
+ throw new Error(stderr);
+ }
+
+ return stdout;
+};
+
+export {
+ getGitDiffOutput,
+ isInGitRepository,
+ execGitCommit,
+};