|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/git-pkgs/brief" |
| 13 | + "github.com/git-pkgs/brief/detect" |
| 14 | + "github.com/git-pkgs/brief/kb" |
| 15 | + "github.com/git-pkgs/brief/report" |
| 16 | +) |
| 17 | + |
| 18 | +func cmdDiff(args []string) { |
| 19 | + fs := flag.NewFlagSet("brief diff", flag.ExitOnError) |
| 20 | + jsonFlag := fs.Bool("json", false, "Force JSON output") |
| 21 | + humanFlag := fs.Bool("human", false, "Force human-readable output") |
| 22 | + verbose := fs.Bool("verbose", false, "Include breadcrumb/reference information") |
| 23 | + category := fs.String("category", "", "Only report on specific category") |
| 24 | + _ = fs.Parse(args) |
| 25 | + |
| 26 | + // Determine the project root (git toplevel). |
| 27 | + root, err := gitToplevel() |
| 28 | + if err != nil { |
| 29 | + _, _ = fmt.Fprintf(os.Stderr, "error: not a git repository\n") |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + |
| 33 | + // Determine diff refs. |
| 34 | + // No args: diff from default branch to working tree (including uncommitted). |
| 35 | + // One arg: diff from that ref to working tree. |
| 36 | + // Two args: diff between two refs. |
| 37 | + var ref1, ref2 string |
| 38 | + var diffRef string |
| 39 | + includeUncommitted := false |
| 40 | + |
| 41 | + switch fs.NArg() { |
| 42 | + case 0: |
| 43 | + // Default: compare against the default branch + uncommitted changes. |
| 44 | + ref1 = detectDefaultBranch(root) |
| 45 | + includeUncommitted = true |
| 46 | + diffRef = ref1 + "..HEAD (+ uncommitted)" |
| 47 | + case 1: |
| 48 | + ref1 = fs.Arg(0) |
| 49 | + includeUncommitted = true |
| 50 | + diffRef = ref1 + " (+ uncommitted)" |
| 51 | + case 2: |
| 52 | + ref1 = fs.Arg(0) |
| 53 | + ref2 = fs.Arg(1) |
| 54 | + diffRef = ref1 + ".." + ref2 |
| 55 | + default: |
| 56 | + _, _ = fmt.Fprintf(os.Stderr, "usage: brief diff [ref1] [ref2]\n") |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + changedFiles, err := gitChangedFiles(context.Background(), root, ref1, ref2, includeUncommitted) |
| 61 | + if err != nil { |
| 62 | + _, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + if len(changedFiles) == 0 { |
| 67 | + _, _ = fmt.Fprintf(os.Stderr, "no changed files\n") |
| 68 | + os.Exit(0) |
| 69 | + } |
| 70 | + |
| 71 | + knowledgeBase, err := kb.Load(brief.KnowledgeFS) |
| 72 | + if err != nil { |
| 73 | + _, _ = fmt.Fprintf(os.Stderr, "error loading knowledge base: %v\n", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + engine := detect.New(knowledgeBase, root) |
| 78 | + r, err := engine.Run() |
| 79 | + if err != nil { |
| 80 | + _, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + r.DiffRef = diffRef |
| 85 | + r.ChangedFiles = changedFiles |
| 86 | + |
| 87 | + r = detect.FilterByChangedFiles(r, knowledgeBase, changedFiles) |
| 88 | + |
| 89 | + if *category != "" { |
| 90 | + r = filterCategory(r, *category) |
| 91 | + } |
| 92 | + |
| 93 | + useJSON := *jsonFlag || (!*humanFlag && !isTTY()) |
| 94 | + |
| 95 | + if useJSON { |
| 96 | + if err := report.JSON(os.Stdout, r); err != nil { |
| 97 | + _, _ = fmt.Fprintf(os.Stderr, "error writing JSON: %v\n", err) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | + } else { |
| 101 | + report.Human(os.Stdout, r, *verbose) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// gitToplevel returns the root of the git repository. |
| 106 | +func gitToplevel() (string, error) { |
| 107 | + cmd := exec.Command("git", "rev-parse", "--show-toplevel") |
| 108 | + out, err := cmd.Output() |
| 109 | + if err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + return strings.TrimSpace(string(out)), nil |
| 113 | +} |
| 114 | + |
| 115 | +// detectDefaultBranch tries to find the default branch name (main or master). |
| 116 | +func detectDefaultBranch(root string) string { |
| 117 | + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "origin/HEAD") |
| 118 | + cmd.Dir = root |
| 119 | + if out, err := cmd.Output(); err == nil { |
| 120 | + ref := strings.TrimSpace(string(out)) |
| 121 | + if after, ok := strings.CutPrefix(ref, "origin/"); ok && after != "" { |
| 122 | + return after |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Fall back to checking if main or master exists. |
| 127 | + for _, branch := range []string{"main", "master"} { |
| 128 | + cmd := exec.Command("git", "rev-parse", "--verify", branch) |
| 129 | + cmd.Dir = root |
| 130 | + if err := cmd.Run(); err == nil { |
| 131 | + return branch |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return "main" |
| 136 | +} |
| 137 | + |
| 138 | +// gitChangedFiles returns the list of files that differ between refs. |
| 139 | +// If includeUncommitted is true, also includes staged and unstaged changes. |
| 140 | +func gitChangedFiles(ctx context.Context, root, ref1, ref2 string, includeUncommitted bool) ([]string, error) { |
| 141 | + seen := make(map[string]bool) |
| 142 | + var files []string |
| 143 | + |
| 144 | + addFiles := func(output string) { |
| 145 | + for _, f := range strings.Split(strings.TrimSpace(output), "\n") { |
| 146 | + f = strings.TrimSpace(f) |
| 147 | + if f != "" && !seen[f] { |
| 148 | + seen[f] = true |
| 149 | + files = append(files, f) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if ref2 != "" { |
| 155 | + // Two-ref diff: just diff between the two. |
| 156 | + cmd := exec.CommandContext(ctx, "git", "diff", "--name-only", ref1+"..."+ref2) |
| 157 | + cmd.Dir = root |
| 158 | + out, err := cmd.Output() |
| 159 | + if err != nil { |
| 160 | + // Try two-dot diff if three-dot fails. |
| 161 | + cmd = exec.CommandContext(ctx, "git", "diff", "--name-only", ref1, ref2) |
| 162 | + cmd.Dir = root |
| 163 | + out, err = cmd.Output() |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("git diff %s %s: %w", ref1, ref2, err) |
| 166 | + } |
| 167 | + } |
| 168 | + addFiles(string(out)) |
| 169 | + } else { |
| 170 | + // Compare ref1 to HEAD. |
| 171 | + cmd := exec.CommandContext(ctx, "git", "diff", "--name-only", ref1+"...HEAD") |
| 172 | + cmd.Dir = root |
| 173 | + out, err := cmd.Output() |
| 174 | + if err != nil { |
| 175 | + cmd = exec.CommandContext(ctx, "git", "diff", "--name-only", ref1, "HEAD") |
| 176 | + cmd.Dir = root |
| 177 | + out, err = cmd.Output() |
| 178 | + if err != nil { |
| 179 | + return nil, fmt.Errorf("git diff %s: %w", ref1, err) |
| 180 | + } |
| 181 | + } |
| 182 | + addFiles(string(out)) |
| 183 | + |
| 184 | + if includeUncommitted { |
| 185 | + // Staged changes. |
| 186 | + cmd = exec.CommandContext(ctx, "git", "diff", "--name-only", "--cached") |
| 187 | + cmd.Dir = root |
| 188 | + if out, err := cmd.Output(); err == nil { |
| 189 | + addFiles(string(out)) |
| 190 | + } |
| 191 | + |
| 192 | + // Unstaged changes. |
| 193 | + cmd = exec.CommandContext(ctx, "git", "diff", "--name-only") |
| 194 | + cmd.Dir = root |
| 195 | + if out, err := cmd.Output(); err == nil { |
| 196 | + addFiles(string(out)) |
| 197 | + } |
| 198 | + |
| 199 | + // Untracked files. |
| 200 | + cmd = exec.CommandContext(ctx, "git", "ls-files", "--others", "--exclude-standard") |
| 201 | + cmd.Dir = root |
| 202 | + if out, err := cmd.Output(); err == nil { |
| 203 | + addFiles(string(out)) |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // Make paths relative to root. |
| 209 | + for i, f := range files { |
| 210 | + if filepath.IsAbs(f) { |
| 211 | + if rel, err := filepath.Rel(root, f); err == nil { |
| 212 | + files[i] = rel |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return files, nil |
| 218 | +} |
0 commit comments