Skip to main content

ai_agent/commands/
commit.rs

1// Source: /data/home/swei/claudecode/openclaudecode/src/commands/commit.ts
2use super::{Command, CommandCallResult};
3use crate::constants::env::ai;
4
5const ALLOWED_TOOLS: &[&str] = &[
6    "Bash(git add:*)",
7    "Bash(git status:*)",
8    "Bash(git commit:*)",
9];
10
11fn get_prompt_content() -> String {
12    let prefix = if std::env::var(ai::USER_TYPE)
13        .map(|v| v == "ant")
14        .unwrap_or(false)
15    {
16        "".to_string()
17    } else {
18        "".to_string()
19    };
20
21    format!(
22        r#"{}## Context
23
24- Current git status: !`git status`
25- Current git diff (staged and unstaged changes): !`git diff HEAD`
26- Current branch: !`git branch --show-current`
27- Recent commits: !`git log --oneline -10`
28
29## Git Safety Protocol
30
31- NEVER update the git config
32- NEVER skip hooks (--no-verify, --no-gpg-sign, etc) unless the user explicitly requests it
33- CRITICAL: ALWAYS create NEW commits. NEVER use git commit --amend, unless the user explicitly requests it
34- Do not commit files that likely contain secrets (.env, credentials.json, etc). Warn the user if they specifically request to commit those files
35- If there are no changes to commit (i.e., no untracked files and no modifications), do not create an empty commit
36- Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported
37
38## Your task
39
40Based on the above changes, create a single git commit:
41
421. Analyze all staged changes and draft a commit message:
43   - Look at the recent commits above to follow this repository's commit message style
44   - Summarize the nature of the changes (new feature, enhancement, bug fix, refactoring, test, docs, etc.)
45   - Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
46   - Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
47
482. Stage relevant files and create the commit using HEREDOC syntax:
49```
50git commit -m "$(cat <<'EOF'
51Commit message here.
52EOF
53)"
54```
55
56You have the capability to call multiple tools in a single response. Stage and create the commit using a single message. Do not use any other tools or do anything else. Do not send any other text or messages besides these tool calls."#,
57        prefix
58    )
59}
60
61pub fn create_commit_command() -> Command {
62    Command::prompt("commit", "Create a git commit").argument_hint("[<description>]")
63}
64
65pub fn get_commit_allowed_tools() -> Vec<String> {
66    ALLOWED_TOOLS.iter().map(|s| s.to_string()).collect()
67}
68
69pub fn get_commit_prompt_content() -> String {
70    get_prompt_content()
71}