@pie-element/cli 0.1.2-next.1 → 0.1.2-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/git.d.ts +7 -0
- package/dist/utils/git.js +18 -15
- package/dist/utils/git.js.map +1 -1
- package/package.json +1 -1
package/dist/utils/git.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment for a git subprocess aimed at a repository through `cwd`.
|
|
3
|
+
* A git hook exports `GIT_DIR`, which overrides `cwd`: from a worktree it is an
|
|
4
|
+
* absolute path, so `git init` in a temp dir re-initialises the hooked
|
|
5
|
+
* repository as bare and `rev-parse` reads the hooked repository's HEAD.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isolatedGitEnv(env?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
1
8
|
export declare function getCurrentCommit(cwd: string): string;
|
|
2
9
|
export declare function getCurrentCommitShort(cwd: string): string;
|
|
3
10
|
export declare function getCommitsSince(since: string, cwd: string): string[];
|
package/dist/utils/git.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { execSync } from 'node:child_process';
|
|
2
|
+
/**
|
|
3
|
+
* The environment for a git subprocess aimed at a repository through `cwd`.
|
|
4
|
+
* A git hook exports `GIT_DIR`, which overrides `cwd`: from a worktree it is an
|
|
5
|
+
* absolute path, so `git init` in a temp dir re-initialises the hooked
|
|
6
|
+
* repository as bare and `rev-parse` reads the hooked repository's HEAD.
|
|
7
|
+
*/
|
|
8
|
+
export function isolatedGitEnv(env = process.env) {
|
|
9
|
+
return Object.fromEntries(Object.entries(env).filter(([key]) => !key.startsWith('GIT_')));
|
|
10
|
+
}
|
|
11
|
+
function git(command, cwd) {
|
|
12
|
+
return execSync(`git ${command}`, { cwd, encoding: 'utf8', env: isolatedGitEnv() });
|
|
13
|
+
}
|
|
2
14
|
export function getCurrentCommit(cwd) {
|
|
3
15
|
try {
|
|
4
|
-
return
|
|
16
|
+
return git('rev-parse HEAD', cwd).trim();
|
|
5
17
|
}
|
|
6
18
|
catch (error) {
|
|
7
19
|
throw new Error(`Failed to get current commit: ${error}`);
|
|
@@ -9,7 +21,7 @@ export function getCurrentCommit(cwd) {
|
|
|
9
21
|
}
|
|
10
22
|
export function getCurrentCommitShort(cwd) {
|
|
11
23
|
try {
|
|
12
|
-
return
|
|
24
|
+
return git('rev-parse --short HEAD', cwd).trim();
|
|
13
25
|
}
|
|
14
26
|
catch (error) {
|
|
15
27
|
throw new Error(`Failed to get short commit: ${error}`);
|
|
@@ -17,10 +29,7 @@ export function getCurrentCommitShort(cwd) {
|
|
|
17
29
|
}
|
|
18
30
|
export function getCommitsSince(since, cwd) {
|
|
19
31
|
try {
|
|
20
|
-
const output =
|
|
21
|
-
cwd,
|
|
22
|
-
encoding: 'utf8',
|
|
23
|
-
}).trim();
|
|
32
|
+
const output = git(`log ${since}..HEAD --oneline`, cwd).trim();
|
|
24
33
|
if (!output)
|
|
25
34
|
return [];
|
|
26
35
|
return output.split('\n');
|
|
@@ -31,10 +40,7 @@ export function getCommitsSince(since, cwd) {
|
|
|
31
40
|
}
|
|
32
41
|
export function getFileModifiedDate(filePath, cwd) {
|
|
33
42
|
try {
|
|
34
|
-
const timestamp =
|
|
35
|
-
cwd,
|
|
36
|
-
encoding: 'utf8',
|
|
37
|
-
}).trim();
|
|
43
|
+
const timestamp = git(`log -1 --format=%ct -- "${filePath}"`, cwd).trim();
|
|
38
44
|
return new Date(Number.parseInt(timestamp, 10) * 1000);
|
|
39
45
|
}
|
|
40
46
|
catch (error) {
|
|
@@ -43,10 +49,7 @@ export function getFileModifiedDate(filePath, cwd) {
|
|
|
43
49
|
}
|
|
44
50
|
export function getCommitDetails(commit, cwd) {
|
|
45
51
|
try {
|
|
46
|
-
return
|
|
47
|
-
cwd,
|
|
48
|
-
encoding: 'utf8',
|
|
49
|
-
});
|
|
52
|
+
return git(`show --stat ${commit}`, cwd);
|
|
50
53
|
}
|
|
51
54
|
catch (error) {
|
|
52
55
|
throw new Error(`Failed to get commit details: ${error}`);
|
|
@@ -54,7 +57,7 @@ export function getCommitDetails(commit, cwd) {
|
|
|
54
57
|
}
|
|
55
58
|
export function getAuthorName(cwd) {
|
|
56
59
|
try {
|
|
57
|
-
return
|
|
60
|
+
return git('config user.name', cwd).trim();
|
|
58
61
|
}
|
|
59
62
|
catch (_error) {
|
|
60
63
|
return 'Unknown';
|
package/dist/utils/git.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"git.js","sourceRoot":"","sources":["../../src/utils/git.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"git.js","sourceRoot":"","sources":["../../src/utils/git.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,MAAyB,OAAO,CAAC,GAAG;IACjE,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;AAC5F,CAAC;AAED,SAAS,GAAG,CAAC,OAAe,EAAE,GAAW;IACvC,OAAO,QAAQ,CAAC,OAAO,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,cAAc,EAAE,EAAE,CAAC,CAAC;AACtF,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,IAAI,CAAC;QACH,OAAO,GAAG,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE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O,GAAG,CAAC,eAAe,MAAM,EAAE,EAAE,GAAG,CAAC,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,iCAAiC,KAAK,EAAE,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,IAAI,CAAC;QACH,OAAO,GAAG,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IAC7C,CAAC;IAAC,OAAO,MAAM,EAAE,CAAC;QAChB,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["import { execSync } from 'node:child_process';\n\n/**\n * The environment for a git subprocess aimed at a repository through `cwd`.\n * A git hook exports `GIT_DIR`, which overrides `cwd`: from a worktree it is an\n * absolute path, so `git init` in a temp dir re-initialises the hooked\n * repository as bare and `rev-parse` reads the hooked repository's HEAD.\n */\nexport function isolatedGitEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {\n return Object.fromEntries(Object.entries(env).filter(([key]) => !key.startsWith('GIT_')));\n}\n\nfunction git(command: string, cwd: string): string {\n return execSync(`git ${command}`, { cwd, encoding: 'utf8', env: isolatedGitEnv() });\n}\n\nexport function getCurrentCommit(cwd: string): string {\n try {\n return git('rev-parse HEAD', cwd).trim();\n } catch (error) {\n throw new Error(`Failed to get current commit: ${error}`);\n }\n}\n\nexport function getCurrentCommitShort(cwd: string): string {\n try {\n return git('rev-parse --short HEAD', cwd).trim();\n } catch (error) {\n throw new Error(`Failed to get short commit: ${error}`);\n }\n}\n\nexport function getCommitsSince(since: string, cwd: string): string[] {\n try {\n const output = git(`log ${since}..HEAD --oneline`, cwd).trim();\n\n if (!output) return [];\n return output.split('\\n');\n } catch (error) {\n throw new Error(`Failed to get commits since ${since}: ${error}`);\n }\n}\n\nexport function getFileModifiedDate(filePath: string, cwd: string): Date {\n try {\n const timestamp = git(`log -1 --format=%ct -- \"${filePath}\"`, cwd).trim();\n\n return new Date(Number.parseInt(timestamp, 10) * 1000);\n } catch (error) {\n throw new Error(`Failed to get file modification date: ${error}`);\n }\n}\n\nexport function getCommitDetails(commit: string, cwd: string): string {\n try {\n return git(`show --stat ${commit}`, cwd);\n } catch (error) {\n throw new Error(`Failed to get commit details: ${error}`);\n }\n}\n\nexport function getAuthorName(cwd: string): string {\n try {\n return git('config user.name', cwd).trim();\n } catch (_error) {\n return 'Unknown';\n }\n}\n"]}
|