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