diff --git a/server/system_prompt.go b/server/system_prompt.go index 1b0d1b0863bb681b5f0b70368ab39acf6dbb89c9..c9f3fffb7c54e983bb60fb3e10b72dcc066b2bdc 100644 --- a/server/system_prompt.go +++ b/server/system_prompt.go @@ -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 } diff --git a/server/system_prompt_test.go b/server/system_prompt_test.go index 2bc0c8643bfee3ff8235bf2e1fd8e1f67638a778..3c60d3065f3b660cd931539c5470018a326e5c4e 100644 --- a/server/system_prompt_test.go +++ b/server/system_prompt_test.go @@ -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