showDiffVsMain shows files changed on this branch vs main. For large/unknown repos, uses lightweight git output to avoid expensive scans.
(root string, fileCount int, fileCountKnown bool, projCfg config.ProjectConfig)
| 384 | // showDiffVsMain shows files changed on this branch vs main. |
| 385 | // For large/unknown repos, uses lightweight git output to avoid expensive scans. |
| 386 | func showDiffVsMain(root string, fileCount int, fileCountKnown bool, projCfg config.ProjectConfig) { |
| 387 | // Check if we're on a branch other than main |
| 388 | branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") |
| 389 | branchCmd.Dir = root |
| 390 | branchOut, err := branchCmd.Output() |
| 391 | if err != nil { |
| 392 | return |
| 393 | } |
| 394 | branch := strings.TrimSpace(string(branchOut)) |
| 395 | if branch == "main" || branch == "master" { |
| 396 | return // No diff to show on main branch |
| 397 | } |
| 398 | |
| 399 | // Run codemap --diff to show changes |
| 400 | exe, err := os.Executable() |
| 401 | if err != nil { |
| 402 | return |
| 403 | } |
| 404 | |
| 405 | fmt.Println() |
| 406 | fmt.Printf("📝 Changes on branch '%s' vs main:\n", branch) |
| 407 | |
| 408 | // Unknown file count typically means daemon state is not ready. |
| 409 | // Use cheap git-based output in that case to avoid startup blowups. |
| 410 | if !fileCountKnown || fileCount > limits.LargeRepoFileCount { |
| 411 | showLightweightDiffVsMain(root) |
| 412 | return |
| 413 | } |
| 414 | |
| 415 | // Run codemap --diff to show richer impact analysis on manageable repos. |
| 416 | diffBudget := projCfg.DiffOutputBytes() |
| 417 | args := []string{"--diff"} |
| 418 | if len(projCfg.Only) > 0 { |
| 419 | args = append(args, "--only", strings.Join(projCfg.Only, ",")) |
| 420 | } |
| 421 | if len(projCfg.Exclude) > 0 { |
| 422 | args = append(args, "--exclude", strings.Join(projCfg.Exclude, ",")) |
| 423 | } |
| 424 | args = append(args, root) |
| 425 | cmd := exec.Command(exe, args...) |
| 426 | captureLimit := diffBudget + diffCaptureSlackBytes |
| 427 | if captureLimit < diffBudget { |
| 428 | captureLimit = diffBudget |
| 429 | } |
| 430 | buf := newCappedStringWriter(captureLimit) |
| 431 | cmd.Stdout = buf |
| 432 | cmd.Stderr = os.Stderr |
| 433 | cmd.Run() |
| 434 | |
| 435 | output := buf.String() |
| 436 | if len(output) > diffBudget || buf.Truncated() { |
| 437 | output = limits.TruncateAtLineBoundary( |
| 438 | output, |
| 439 | diffBudget, |
| 440 | "\n\n... (diff output truncated, run `codemap --diff` for full output)\n", |
| 441 | ) |
| 442 | } |
| 443 | fmt.Print(output) |