@@ -80,7 +80,7 @@ func collectSystemData(workingDir string) (*SystemPromptData, error) {
}
// Try to collect git info
- gitInfo, err := collectGitInfo()
+ gitInfo, err := collectGitInfo(wd)
if err == nil {
data.GitInfo = gitInfo
}
@@ -132,9 +132,12 @@ func collectSystemData(workingDir string) (*SystemPromptData, error) {
return data, nil
}
-func collectGitInfo() (*GitInfo, error) {
+func collectGitInfo(dir string) (*GitInfo, error) {
// Find git root
rootCmd := exec.Command("git", "rev-parse", "--show-toplevel")
+ if dir != "" {
+ rootCmd.Dir = dir
+ }
rootOutput, err := rootCmd.Output()
if err != nil {
return nil, err
@@ -350,7 +353,7 @@ func GenerateSubagentSystemPrompt(workingDir string) (string, error) {
}
// Try to collect git info
- gitInfo, err := collectGitInfo()
+ gitInfo, err := collectGitInfo(wd)
if err == nil {
data.GitInfo = gitInfo
}
@@ -2,6 +2,7 @@ package server
import (
"os"
+ "os/exec"
"path/filepath"
"strings"
"testing"
@@ -64,6 +65,47 @@ func TestSystemPromptEmptyCwdFallsBackToCurrentDir(t *testing.T) {
}
}
+// TestSystemPromptDetectsGitInWorkingDir verifies that the system prompt
+// correctly detects a git repo in the specified working directory, not the
+// process's cwd. Regression test for https://github.com/boldsoftware/shelley/issues/71
+func TestSystemPromptDetectsGitInWorkingDir(t *testing.T) {
+ // Create a temp dir with a git repo
+ tmpDir, err := os.MkdirTemp("", "shelley_git_test")
+ if err != nil {
+ t.Fatalf("failed to create temp dir: %v", err)
+ }
+ defer os.RemoveAll(tmpDir)
+
+ // Initialize a git repo in the temp dir
+ cmd := exec.Command("git", "init")
+ cmd.Dir = tmpDir
+ if out, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("git init failed: %v\n%s", err, out)
+ }
+ cmd = exec.Command("git", "commit", "--allow-empty", "-m", "initial")
+ cmd.Dir = tmpDir
+ if out, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("git commit failed: %v\n%s", err, out)
+ }
+
+ // Generate system prompt for the git repo directory
+ prompt, err := GenerateSystemPrompt(tmpDir)
+ if err != nil {
+ t.Fatalf("GenerateSystemPrompt failed: %v", err)
+ }
+
+ // The prompt should say "Git repository root:" not "Not in a git repository"
+ if strings.Contains(prompt, "Not in a git repository") {
+ t.Errorf("system prompt incorrectly says 'Not in a git repository' for a directory that is a git repo")
+ }
+ if !strings.Contains(prompt, "Git repository root:") {
+ t.Errorf("system prompt should contain 'Git repository root:' for a git repo directory")
+ }
+ if !strings.Contains(prompt, tmpDir) {
+ t.Errorf("system prompt should reference the git root directory %s", tmpDir)
+ }
+}
+
func min(a, b int) int {
if a < b {
return a