@shelken/pi-co-authored-by 0.2.6
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/LICENSE +22 -0
- package/README.md +22 -0
- package/index.ts +39 -0
- package/lib/commit.ts +97 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bruno Garcia
|
|
4
|
+
Modifications Copyright (c) 2026 shelken
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# pi-co-authored-by
|
|
2
|
+
|
|
3
|
+
agent 通过 bash 工具执行 `git commit` 时,自动追加:
|
|
4
|
+
|
|
5
|
+
- `Co-Authored-By: <model> <noreply@pi.dev>`
|
|
6
|
+
- `Generated-By: pi <version>`
|
|
7
|
+
|
|
8
|
+
实现方式:session 级临时 `core.hooksPath` + `prepare-commit-msg` hook。
|
|
9
|
+
|
|
10
|
+
初始 fork 自 [bruno-garcia/pi-co-authored-by](https://github.com/bruno-garcia/pi-co-authored-by);本仓库有本地改动。
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pi install npm:@shelken/pi-co-authored-by
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 验证
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
bun --filter @shelken/pi-co-authored-by test
|
|
22
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { isToolCallEventType, VERSION } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import {
|
|
4
|
+
createCommitHookDirectory,
|
|
5
|
+
removeCommitHookDirectory,
|
|
6
|
+
wrapBashWithCommitHook,
|
|
7
|
+
} from "./lib/commit.ts";
|
|
8
|
+
|
|
9
|
+
export default function (pi: ExtensionAPI) {
|
|
10
|
+
let hooksDir: string | undefined;
|
|
11
|
+
|
|
12
|
+
function ensureHooksDir(): string {
|
|
13
|
+
hooksDir ??= createCommitHookDirectory();
|
|
14
|
+
return hooksDir;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
pi.on("session_start", async () => {
|
|
18
|
+
ensureHooksDir();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
pi.on("session_shutdown", async () => {
|
|
22
|
+
removeCommitHookDirectory(hooksDir);
|
|
23
|
+
hooksDir = undefined;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
27
|
+
if (!isToolCallEventType("bash", event)) return;
|
|
28
|
+
|
|
29
|
+
const model = ctx.model;
|
|
30
|
+
const modelName = model ? (model.name || `${model.provider}/${model.id}`) : "unknown";
|
|
31
|
+
|
|
32
|
+
event.input.command = wrapBashWithCommitHook(
|
|
33
|
+
event.input.command,
|
|
34
|
+
ensureHooksDir(),
|
|
35
|
+
modelName,
|
|
36
|
+
VERSION,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
}
|
package/lib/commit.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
export const CO_AUTHOR_NAME = "Co-Authored-By";
|
|
6
|
+
export const GENERATED_BY_NAME = "Generated-By";
|
|
7
|
+
|
|
8
|
+
/** Create a session-scoped Git hooks directory for commit trailer injection. */
|
|
9
|
+
export function createCommitHookDirectory(): string {
|
|
10
|
+
const hooksDir = mkdtempSync(join(tmpdir(), "pi-co-authored-by-hooks-"));
|
|
11
|
+
const hookPath = join(hooksDir, "prepare-commit-msg");
|
|
12
|
+
writeFileSync(hookPath, buildPrepareCommitMsgHook(), { mode: 0o755 });
|
|
13
|
+
chmodSync(hookPath, 0o755);
|
|
14
|
+
return hooksDir;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Remove the session-scoped Git hooks directory. */
|
|
18
|
+
export function removeCommitHookDirectory(hooksDir: string | undefined): void {
|
|
19
|
+
if (!hooksDir) return;
|
|
20
|
+
rmSync(hooksDir, { recursive: true, force: true });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Add process-local Git hook configuration and trailer metadata to a bash command. */
|
|
24
|
+
export function wrapBashWithCommitHook(
|
|
25
|
+
cmd: string,
|
|
26
|
+
hooksDir: string,
|
|
27
|
+
modelName: string,
|
|
28
|
+
piVersion: string,
|
|
29
|
+
): string {
|
|
30
|
+
const coAuthor = `${CO_AUTHOR_NAME}: ${modelName} <noreply@pi.dev>`;
|
|
31
|
+
const generatedBy = `${GENERATED_BY_NAME}: pi ${piVersion}`;
|
|
32
|
+
|
|
33
|
+
return `${buildEnvironmentPrefix(hooksDir, coAuthor, generatedBy)}\n${cmd}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function buildEnvironmentPrefix(
|
|
37
|
+
hooksDir: string,
|
|
38
|
+
coAuthor: string,
|
|
39
|
+
generatedBy: string,
|
|
40
|
+
): string {
|
|
41
|
+
return `__pi_co_authored_by_git_config_index="\${GIT_CONFIG_COUNT:-0}"
|
|
42
|
+
export PI_CO_AUTHORED_BY_GIT_CONFIG_INDEX="$__pi_co_authored_by_git_config_index"
|
|
43
|
+
export PI_CO_AUTHORED_BY_CO_AUTHOR=${shellQuote(coAuthor)}
|
|
44
|
+
export PI_CO_AUTHORED_BY_GENERATED_BY=${shellQuote(generatedBy)}
|
|
45
|
+
export "GIT_CONFIG_KEY_\${__pi_co_authored_by_git_config_index}=core.hooksPath"
|
|
46
|
+
export "GIT_CONFIG_VALUE_\${__pi_co_authored_by_git_config_index}=${escapeDoubleQuotedAssignmentValue(hooksDir)}"
|
|
47
|
+
export GIT_CONFIG_COUNT="$((__pi_co_authored_by_git_config_index + 1))"
|
|
48
|
+
unset __pi_co_authored_by_git_config_index`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function buildPrepareCommitMsgHook(): string {
|
|
52
|
+
return `#!/bin/sh
|
|
53
|
+
set -u
|
|
54
|
+
|
|
55
|
+
message_file="$1"
|
|
56
|
+
|
|
57
|
+
if [ -n "\${PI_CO_AUTHORED_BY_CO_AUTHOR:-}" ] && [ -n "\${PI_CO_AUTHORED_BY_GENERATED_BY:-}" ]; then
|
|
58
|
+
git \
|
|
59
|
+
-c trailer.co-authored-by.ifExists=addIfDifferent \
|
|
60
|
+
-c trailer.generated-by.ifExists=replace \
|
|
61
|
+
interpret-trailers \
|
|
62
|
+
--in-place \
|
|
63
|
+
--trailer "$PI_CO_AUTHORED_BY_CO_AUTHOR" \
|
|
64
|
+
--trailer "$PI_CO_AUTHORED_BY_GENERATED_BY" \
|
|
65
|
+
"$message_file"
|
|
66
|
+
fi
|
|
67
|
+
|
|
68
|
+
if [ -n "\${PI_CO_AUTHORED_BY_GIT_CONFIG_INDEX:-}" ]; then
|
|
69
|
+
__pi_config_index="$PI_CO_AUTHORED_BY_GIT_CONFIG_INDEX"
|
|
70
|
+
unset "GIT_CONFIG_KEY_$__pi_config_index"
|
|
71
|
+
unset "GIT_CONFIG_VALUE_$__pi_config_index"
|
|
72
|
+
export GIT_CONFIG_COUNT="$__pi_config_index"
|
|
73
|
+
fi
|
|
74
|
+
|
|
75
|
+
original_hooks_path="$(git config --get core.hooksPath || true)"
|
|
76
|
+
if [ -n "$original_hooks_path" ]; then
|
|
77
|
+
case "$original_hooks_path" in
|
|
78
|
+
/*) original_hook="$original_hooks_path/prepare-commit-msg" ;;
|
|
79
|
+
*) original_hook="$(git rev-parse --show-toplevel)/$original_hooks_path/prepare-commit-msg" ;;
|
|
80
|
+
esac
|
|
81
|
+
else
|
|
82
|
+
original_hook="$(git rev-parse --git-path hooks/prepare-commit-msg)"
|
|
83
|
+
fi
|
|
84
|
+
|
|
85
|
+
if [ -x "$original_hook" ] && [ "$original_hook" != "$0" ]; then
|
|
86
|
+
"$original_hook" "$@"
|
|
87
|
+
fi
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function shellQuote(value: string): string {
|
|
92
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function escapeDoubleQuotedAssignmentValue(value: string): string {
|
|
96
|
+
return value.replace(/[\\"$`]/g, "\\$&");
|
|
97
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shelken/pi-co-authored-by",
|
|
3
|
+
"version": "0.2.6",
|
|
4
|
+
"description": "agent 执行 git commit 时自动追加 Co-Authored-By / Generated-By trailer",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.ts",
|
|
9
|
+
"lib/commit.ts"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/shelken/pi-extensions.git",
|
|
14
|
+
"directory": "extensions/pi-co-authored-by"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "vitest run"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"pi-package",
|
|
24
|
+
"git",
|
|
25
|
+
"co-authored-by"
|
|
26
|
+
],
|
|
27
|
+
"pi": {
|
|
28
|
+
"extensions": [
|
|
29
|
+
"./index.ts"
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"@earendil-works/pi-coding-agent": {
|
|
37
|
+
"optional": true
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|