pi-git-commit 1.0.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -0
  3. package/index.ts +158 -0
  4. package/package.json +51 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 YuGiMob
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # pi-git-commit
2
+
3
+ A [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent) extension that keeps mutative git operations out of the agent's bash and provides a safe commit flow.
4
+
5
+ ## Features
6
+
7
+ - **Bash git guard.** Blocks `git add`, `commit`, `push`, `pull`, `merge`, `rebase`, `reset`, `clean`, `rm`, `restore`, `switch`, `cherry-pick`, `revert`, `mv`, `init`, `clone` and other mutative forms in the agent's bash tool. Read-only commands (`status`, `diff`, `log`, `fetch`, `branch`, `tag`, `stash list`, ...) stay allowed.
8
+ - **`git_commit` tool.** The agent stages everything and commits with a `FIX` / `IMPROVE` / `NEW` type prefix. Enabled automatically on session start.
9
+ - **`/commit` command.** Stages all changes, shows the staged diff, and asks the agent to review it and commit via `git_commit` (never via bash).
10
+ - **`/toggle-allow-git` command.** Temporarily allows mutative git commands in bash for the current session.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pi install npm:pi-git-commit
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Run `/commit` after making changes. The extension stages the working tree, presents the diff to the agent, and the agent commits using the `git_commit` tool.
21
+
22
+ Use `/toggle-allow-git` if you need to run mutative git commands in bash yourself for the current session (the guard re-arms on the next session).
package/index.ts ADDED
@@ -0,0 +1,158 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { Type } from "typebox";
3
+
4
+ const COMMIT_TYPES = ["FIX", "IMPROVE", "NEW"] as const;
5
+
6
+ export default function (pi: ExtensionAPI) {
7
+ let gitBlocked = true;
8
+
9
+ const containsBlockedGitCommand = (command: string): boolean => {
10
+ const alwaysBlocked = /\bgit\s+(add|commit|push|pull|merge|rebase|reset|clean|rm|restore|switch|cherry-pick|revert|mv|init|clone)\b/;
11
+ if (alwaysBlocked.test(command)) return true;
12
+
13
+ const readOnlyForms = [
14
+ /\bgit\s+fetch\b/,
15
+ /\bgit\s+stash\s+(list|show)\b/,
16
+ /\bgit\s+remote\s*$/,
17
+ /\bgit\s+config\s+(--list|-l|--get|--get-all|--get-regexp|--show-origin|--show-scope)\b/,
18
+ /\bgit\s+remote\s+(-v|show|get-url)\b/,
19
+ /\bgit\s+apply\s+--(check|stat)\b/,
20
+ /\bgit\s+notes\s+(list|show)\b/,
21
+ /\bgit\s+lfs\s+(ls-files|status)\b/,
22
+ /\bgit\s+sparse-checkout\s+list\b/,
23
+ ];
24
+ if (readOnlyForms.some((re) => re.test(command))) return false;
25
+
26
+ if (/\bgit\s+(config|remote|apply|am|notes|replace|update-ref|symbolic-ref|update-index|gc|maintenance|sparse-checkout|lfs)\b/.test(command)) return true;
27
+
28
+ if (/\bgit\s+branch\b/.test(command)) {
29
+ if (/\bgit\s+branch\s+(-d|-D|-m|-M|--delete|--move)\b/.test(command)) return true;
30
+ return false;
31
+ }
32
+
33
+ if (/\bgit\s+tag\b/.test(command)) {
34
+ if (/\bgit\s+tag\s+(-d|-a|-s|-f|--delete|--annotate|--sign|--force)\b/.test(command)) return true;
35
+ return false;
36
+ }
37
+
38
+ if (/\bgit\s+checkout\s+--\s/.test(command)) return false;
39
+ if (/\bgit\s+checkout\b/.test(command)) return true;
40
+
41
+ if (/\bgit\s+submodule\s+(status|init|summary)\b/.test(command)) return false;
42
+ if (/\bgit\s+submodule\b/.test(command)) return true;
43
+
44
+ if (/\bgit\s+worktree\s+list\b/.test(command)) return false;
45
+ if (/\bgit\s+worktree\b/.test(command)) return true;
46
+
47
+ if (/\bgit\s+stash\b/.test(command)) return true;
48
+
49
+ return false;
50
+ };
51
+
52
+ pi.on("tool_call", async (event) => {
53
+ if (event.toolName !== "bash") return undefined;
54
+ const command = (event.input.command as string).trim();
55
+ if (gitBlocked && containsBlockedGitCommand(command)) {
56
+ return { block: true, reason: "Mutative git commands are blocked. Use /toggle-allow-git to allow for this session." };
57
+ }
58
+ return undefined;
59
+ });
60
+
61
+ pi.registerTool({
62
+ name: "git_commit",
63
+ label: "Git Commit",
64
+ description: "Stage all changes and create a commit. Only use when the user has run /commit and asked you to commit. Do not call this tool unprompted.",
65
+ promptSnippet: "Commit staged changes (only after user runs /commit)",
66
+ promptGuidelines: [
67
+ "Only use git_commit when the user explicitly asks you to commit after they ran /commit",
68
+ "Do not call git_commit on its own — wait for the user to run /commit first",
69
+ ],
70
+ parameters: Type.Object({
71
+ type: Type.Union(COMMIT_TYPES.map((t) => Type.Literal(t))),
72
+ message: Type.String({
73
+ description: "Commit message (imperative mood). Multi-line allowed for detailed changes.",
74
+ }),
75
+ }),
76
+ async execute(_toolCallId, params, signal, _onUpdate, _ctx) {
77
+
78
+ const { type, message } = params;
79
+ const fullMessage = `${type}: ${message}`;
80
+ const addResult = await pi.exec("git", ["add", "."], { signal });
81
+ if (addResult.code !== 0) {
82
+ return { content: [{ type: "text", text: `Staging failed: ${addResult.stderr}` }], details: {}, isError: true };
83
+ }
84
+
85
+ const result = await pi.exec("git", ["commit", "-m", fullMessage], { signal });
86
+ if (result.code !== 0) {
87
+ return { content: [{ type: "text", text: `Commit failed: ${result.stderr}` }], details: {}, isError: true };
88
+ }
89
+
90
+ return { content: [{ type: "text", text: `✓ Committed: ${fullMessage}` }], details: {} };
91
+ },
92
+ });
93
+
94
+ pi.on("session_start", () => {
95
+ gitBlocked = true;
96
+ const activeTools = pi.getActiveTools();
97
+ if (!activeTools.includes("git_commit")) {
98
+ pi.setActiveTools([...activeTools, "git_commit"]);
99
+ }
100
+ });
101
+
102
+ pi.registerCommand("commit", {
103
+ description: "Stage files and show diff for commit",
104
+ handler: async (_args, ctx) => {
105
+ if (!ctx.hasUI) {
106
+ ctx.ui.notify("commit requires interactive mode", "error");
107
+ return;
108
+ }
109
+
110
+ try {
111
+ await ctx.ui.setWorkingMessage("Waiting for queued messages to complete...");
112
+ await ctx.waitForIdle();
113
+
114
+ await ctx.ui.setWorkingMessage("Staging files...");
115
+ const addResult = await pi.exec("git", ["add", "."]);
116
+ if (addResult.code !== 0) {
117
+ ctx.ui.notify(`git add failed: ${addResult.stderr}`, "error");
118
+ return;
119
+ }
120
+
121
+ await ctx.ui.setWorkingMessage("Getting diff...");
122
+ const diffResult = await pi.exec("git", ["diff", "--staged"]);
123
+ if (diffResult.code !== 0) {
124
+ ctx.ui.notify(`git diff failed: ${diffResult.stderr}`, "error");
125
+ return;
126
+ }
127
+
128
+ if (!diffResult.stdout.trim()) {
129
+ ctx.ui.notify("Nothing to commit (empty diff). Stage files first.", "warning");
130
+ return;
131
+ }
132
+
133
+ const diff = diffResult.stdout || "(no changes staged)";
134
+
135
+ const prompt = `DO NOT use bash for git. Use ONLY the \`git_commit\` tool.\n\nReview staged changes:\n\`\`\`diff\n${diff}\`\`\`\n\nUse \`git_commit\` tool with:\n- type: FIX (bug fix), IMPROVE (improvement), or NEW (new feature)\n- message: brief description (imperative mood). Multi-line allowed for detailed changes.`;
136
+ pi.sendUserMessage(prompt, { deliverAs: "followUp" });
137
+ } finally {
138
+ ctx.ui.setWorkingMessage();
139
+ }
140
+ },
141
+ });
142
+ pi.registerCommand("toggle-allow-git", {
143
+ description: "Toggle whether mutative git commands are allowed in bash for this session",
144
+ handler: async (_args, ctx) => {
145
+ if (!ctx.hasUI) {
146
+ ctx.ui.notify("toggle-allow-git requires interactive mode", "error");
147
+ return;
148
+ }
149
+ gitBlocked = !gitBlocked;
150
+ if (gitBlocked) {
151
+ ctx.ui.notify("Mutative git commands are blocked again in bash", "info");
152
+ } else {
153
+ ctx.ui.notify("Mutative git commands are now allowed in bash for this session", "warning");
154
+ }
155
+ },
156
+ });
157
+
158
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "pi-git-commit",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "Pi extension: block mutative git commands in bash and provide a git_commit tool plus /commit and /toggle-allow-git commands",
6
+ "main": "index.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/YuGiMob/pi-git-commit.git"
10
+ },
11
+ "author": "YuGiMob",
12
+ "keywords": [
13
+ "pi-package",
14
+ "pi",
15
+ "coding-agent",
16
+ "extension",
17
+ "git",
18
+ "commit",
19
+ "guard"
20
+ ],
21
+ "license": "MIT",
22
+ "files": [
23
+ "index.ts",
24
+ "src",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "pi": {
29
+ "extensions": [
30
+ "./index.ts"
31
+ ]
32
+ },
33
+ "peerDependencies": {
34
+ "@earendil-works/pi-coding-agent": "*",
35
+ "typebox": "*"
36
+ },
37
+ "engines": {
38
+ "node": ">=22.19.0"
39
+ },
40
+ "scripts": {
41
+ "test": "vitest run",
42
+ "typecheck": "tsc --noEmit"
43
+ },
44
+ "devDependencies": {
45
+ "@earendil-works/pi-coding-agent": "^0.84.0",
46
+ "@types/node": "^24.0.0",
47
+ "typescript": "~5.9.3",
48
+ "vitest": "^4.1.9",
49
+ "typebox": "^1.3.7"
50
+ }
51
+ }