Add WillRunGitCommit function to bashkit
This function inspects a bash script to determine if it will run 'git commit' commands.
The implementation is similar to the existing Check function but is kept separate as requested.
Co-Authored-By: sketch <hello@sketch.dev>
diff --git a/claudetool/bashkit/bashkit.go b/claudetool/bashkit/bashkit.go
index a56eef0..8e67a3b 100644
--- a/claudetool/bashkit/bashkit.go
+++ b/claudetool/bashkit/bashkit.go
@@ -95,3 +95,52 @@
// user.name/user.email is followed by a value
return true
}
+
+// WillRunGitCommit checks if the provided bash script will run 'git commit'.
+// It returns true if any command in the script is a git commit command.
+func WillRunGitCommit(bashScript string) (bool, error) {
+ r := strings.NewReader(bashScript)
+ parser := syntax.NewParser()
+ file, err := parser.Parse(r, "")
+ if err != nil {
+ // Parsing failed, but let's not consider this an error for the same reasons as in Check
+ return false, nil
+ }
+
+ willCommit := false
+
+ syntax.Walk(file, func(node syntax.Node) bool {
+ callExpr, ok := node.(*syntax.CallExpr)
+ if !ok {
+ return true
+ }
+ if isGitCommitCommand(callExpr) {
+ willCommit = true
+ return false
+ }
+ return true
+ })
+
+ return willCommit, nil
+}
+
+// isGitCommitCommand checks if a command is 'git commit'.
+func isGitCommitCommand(cmd *syntax.CallExpr) bool {
+ if len(cmd.Args) < 2 {
+ return false
+ }
+
+ // First argument must be 'git'
+ if cmd.Args[0].Lit() != "git" {
+ return false
+ }
+
+ // Look for 'commit' in any position after 'git'
+ for i := 1; i < len(cmd.Args); i++ {
+ if cmd.Args[i].Lit() == "commit" {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/claudetool/bashkit/bashkit_test.go b/claudetool/bashkit/bashkit_test.go
index 8bcdd8f..ea6e599 100644
--- a/claudetool/bashkit/bashkit_test.go
+++ b/claudetool/bashkit/bashkit_test.go
@@ -107,3 +107,95 @@
})
}
}
+
+func TestWillRunGitCommit(t *testing.T) {
+ tests := []struct {
+ name string
+ script string
+ wantCommit bool
+ }{
+ {
+ name: "simple git commit",
+ script: "git commit -m 'Add feature'",
+ wantCommit: true,
+ },
+ {
+ name: "git command without commit",
+ script: "git status",
+ wantCommit: false,
+ },
+ {
+ name: "multiline script with git commit",
+ script: "echo 'Making changes' && git add . && git commit -m 'Update files'",
+ wantCommit: true,
+ },
+ {
+ name: "multiline script without git commit",
+ script: "echo 'Checking status' && git status",
+ wantCommit: false,
+ },
+ {
+ name: "script with commented git commit",
+ script: "# git commit -m 'This is commented out'",
+ wantCommit: false,
+ },
+ {
+ name: "git commit with variables",
+ script: "MSG='Fix bug' && git commit -m 'Using variable'",
+ wantCommit: true,
+ },
+ {
+ name: "only git command",
+ script: "git",
+ wantCommit: false,
+ },
+ {
+ name: "script with invalid syntax",
+ script: "git commit -m 'unterminated string",
+ wantCommit: false,
+ },
+ {
+ name: "commit used in different context",
+ script: "echo 'commit message'",
+ wantCommit: false,
+ },
+ {
+ name: "git with flags before commit",
+ script: "git -C /path/to/repo commit -m 'Update'",
+ wantCommit: true,
+ },
+ {
+ name: "git with multiple flags",
+ script: "git --git-dir=.git -C repo commit -a -m 'Update'",
+ wantCommit: true,
+ },
+ {
+ name: "git with env vars",
+ script: "GIT_AUTHOR_NAME=\"Josh Bleecher Snyder\" GIT_AUTHOR_EMAIL=\"josharian@gmail.com\" git commit -am \"Updated code\"",
+ wantCommit: true,
+ },
+ {
+ name: "git with redirections",
+ script: "git commit -m 'Fix issue' > output.log 2>&1",
+ wantCommit: true,
+ },
+ {
+ name: "git with piped commands",
+ script: "echo 'Committing' | git commit -F -",
+ wantCommit: true,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ gotCommit, err := WillRunGitCommit(tc.script)
+ if err != nil {
+ t.Errorf("WillRunGitCommit() error = %v", err)
+ return
+ }
+ if gotCommit != tc.wantCommit {
+ t.Errorf("WillRunGitCommit() = %v, want %v", gotCommit, tc.wantCommit)
+ }
+ })
+ }
+}