Compare commits

..

No commits in common. "d92b88e90b026ffba71c94ef60d5ff7bfcec1996" and "11faf95573d31fb1616e7382c9c4107a2e4f978f" have entirely different histories.

2 changed files with 13 additions and 60 deletions

View File

@ -14,23 +14,17 @@ var prDescribeCmd = &cobra.Command{
Run: runPRDescribe, Run: runPRDescribe,
} }
func init() {
prDescribeCmd.Flags().StringP("base", "b", "master", "Base branch to compare against")
}
func runPRDescribe(cmd *cobra.Command, args []string) { func runPRDescribe(cmd *cobra.Command, args []string) {
base, _ := cmd.Flags().GetString("base") diff, err := gitRun([]string{"diff", "main..HEAD", "--no-color"})
diff, err := gitRun([]string{"diff", fmt.Sprintf("%s..HEAD", base), "--no-color"})
if err != nil || diff == "" { if err != nil || diff == "" {
diff, err = gitRun([]string{"diff", fmt.Sprintf("origin/%s..HEAD", base), "--no-color"}) diff, err = gitRun([]string{"diff", "origin/main..HEAD", "--no-color"})
if err != nil { if err != nil {
color.Red("Failed to get branch diff: %v", err) color.Red("Failed to get branch diff: %v", err)
return return
} }
} }
if diff == "" { if diff == "" {
color.Yellow("No changes on this branch compared to %s/origin/%s.", base, base) color.Yellow("No changes on this branch compared to main/origin/main.")
return return
} }
modelFlag, _ := cmd.Flags().GetString("model") modelFlag, _ := cmd.Flags().GetString("model")

View File

@ -47,11 +47,10 @@ func withMockGit(fn func([]string) (string, error)) func() {
return func() { gitRun = orig } return func() { gitRun = orig }
} }
// testCmd returns a minimal cobra command with common flags registered. // testCmd returns a minimal cobra command with the model flag registered.
func testCmd() *cobra.Command { func testCmd() *cobra.Command {
c := &cobra.Command{} c := &cobra.Command{}
c.Flags().String("model", "", "") c.Flags().String("model", "", "")
c.Flags().String("base", "master", "")
return c return c
} }
@ -309,62 +308,22 @@ func TestRunPRDescribe(t *testing.T) {
} }
}) })
t.Run("uses custom base branch", func(t *testing.T) { t.Run("second diff error — skips AI", func(t *testing.T) {
mock := &mockStreamer{response: "PR description"} mock := &mockStreamer{}
defer withMockClient(mock)() defer withMockClient(mock)()
var capturedArgs []string callCount := 0
defer withMockGit(func(args []string) (string, error) { defer withMockGit(func(args []string) (string, error) {
capturedArgs = args callCount++
return "diff content", nil if callCount == 2 {
})() return "", errors.New("no remote")
cmd := testCmd()
if err := cmd.Flags().Set("base", "develop"); err != nil {
t.Fatal(err)
}
runPRDescribe(cmd, nil)
if mock.calls != 1 {
t.Errorf("expected 1 AI call, got %d", mock.calls)
}
// Expect "diff", "develop..HEAD", "--no-color"
expectedArg := "develop..HEAD"
found := false
for _, arg := range capturedArgs {
if arg == expectedArg {
found = true
break
} }
} return "", nil
if !found {
t.Errorf("expected arg %q not found in %v", expectedArg, capturedArgs)
}
})
t.Run("defaults to master", func(t *testing.T) {
mock := &mockStreamer{response: "PR description"}
defer withMockClient(mock)()
var capturedArgs []string
defer withMockGit(func(args []string) (string, error) {
capturedArgs = args
return "diff content", nil
})() })()
runPRDescribe(testCmd(), nil) runPRDescribe(testCmd(), nil)
if mock.calls != 1 { if mock.calls != 0 {
t.Errorf("expected 1 AI call, got %d", mock.calls) t.Errorf("expected 0 AI calls, got %d", mock.calls)
}
expectedArg := "master..HEAD"
found := false
for _, arg := range capturedArgs {
if arg == expectedArg {
found = true
break
}
}
if !found {
t.Errorf("expected arg %q not found in %v", expectedArg, capturedArgs)
} }
}) })
} }