Skip to content

Commit a565578

Browse files
committed
Add commit log to brief diff output
Shows oneline commit summaries between the compared refs. The git log runs concurrently with the detection engine to avoid adding latency.
1 parent 21b5bc1 commit a565578

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

brief.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type Report struct {
161161
Version string `json:"version"`
162162
Path string `json:"path"`
163163
DiffRef string `json:"diff_ref,omitempty"`
164+
DiffCommits []string `json:"diff_commits,omitempty"`
164165
ChangedFiles []string `json:"changed_files,omitempty"`
165166
Languages []Detection `json:"languages"`
166167
PackageManagers []Detection `json:"package_managers"`

cmd/brief/diff.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,24 @@ func cmdDiff(args []string) {
7474
os.Exit(1)
7575
}
7676

77+
// Run detection and git log concurrently.
78+
var commits []string
79+
done := make(chan struct{})
80+
go func() {
81+
commits = gitCommitLog(root, ref1, ref2)
82+
close(done)
83+
}()
84+
7785
engine := detect.New(knowledgeBase, root)
7886
r, err := engine.Run()
7987
if err != nil {
8088
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
8189
os.Exit(1)
8290
}
8391

92+
<-done
8493
r.DiffRef = diffRef
94+
r.DiffCommits = commits
8595
r.ChangedFiles = changedFiles
8696

8797
r = detect.FilterByChangedFiles(r, knowledgeBase, changedFiles)
@@ -142,7 +152,7 @@ func gitChangedFiles(ctx context.Context, root, ref1, ref2 string, includeUncomm
142152
var files []string
143153

144154
addFiles := func(output string) {
145-
for _, f := range strings.Split(strings.TrimSpace(output), "\n") {
155+
for f := range strings.SplitSeq(strings.TrimSpace(output), "\n") {
146156
f = strings.TrimSpace(f)
147157
if f != "" && !seen[f] {
148158
seen[f] = true
@@ -216,3 +226,28 @@ func gitChangedFiles(ctx context.Context, root, ref1, ref2 string, includeUncomm
216226

217227
return files, nil
218228
}
229+
230+
// gitCommitLog returns oneline commit summaries between two refs.
231+
func gitCommitLog(root, ref1, ref2 string) []string {
232+
var args []string
233+
if ref2 != "" {
234+
args = []string{"log", "--oneline", ref1 + ".." + ref2}
235+
} else {
236+
args = []string{"log", "--oneline", ref1 + "..HEAD"}
237+
}
238+
239+
cmd := exec.Command("git", args...)
240+
cmd.Dir = root
241+
out, err := cmd.Output()
242+
if err != nil {
243+
return nil
244+
}
245+
246+
var commits []string
247+
for line := range strings.SplitSeq(strings.TrimSpace(string(out)), "\n") {
248+
if line != "" {
249+
commits = append(commits, line)
250+
}
251+
}
252+
return commits
253+
}

detect/filter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func FilterByChangedFiles(r *brief.Report, knowledgeBase *kb.KnowledgeBase, chan
4040
Version: r.Version,
4141
Path: r.Path,
4242
DiffRef: r.DiffRef,
43+
DiffCommits: r.DiffCommits,
4344
ChangedFiles: r.ChangedFiles,
4445
Tools: make(map[string][]brief.Detection),
4546
Git: r.Git,

report/report.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ func Human(w io.Writer, r *brief.Report, verbose bool) {
4242

4343
_, _ = fmt.Fprintln(w)
4444

45+
// Commits (in diff mode only)
46+
if len(r.DiffCommits) > 0 {
47+
_, _ = fmt.Fprintf(w, "Commits:\n")
48+
limit := min(len(r.DiffCommits), 20)
49+
for _, c := range r.DiffCommits[:limit] {
50+
_, _ = fmt.Fprintf(w, " %s\n", sanitize(c))
51+
}
52+
if len(r.DiffCommits) > 20 {
53+
_, _ = fmt.Fprintf(w, " ... and %d more\n", len(r.DiffCommits)-20)
54+
}
55+
_, _ = fmt.Fprintln(w)
56+
}
57+
4558
// Changed files (in diff mode only)
4659
if len(r.ChangedFiles) > 0 {
4760
_, _ = fmt.Fprintf(w, "Changed:\n")

0 commit comments

Comments
 (0)