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
+}