Skip to content

Commit ac7ec26

Browse files
authored
More information in verbose mode to provide faster feedback loop (#390)
* Amazing new verbose mode * Clean up code * Add changelog entry
1 parent 4663f9a commit ac7ec26

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ All notable changes to `src-cli` are documented in this file.
1515

1616
### Changed
1717

18+
- `src campaign [apply|preview]` now prints more detailed information about the diffs produced in each repository when run in verbose mode with `-v`. [#390](https://github.com/sourcegraph/src-cli/pull/390)
19+
1820
### Fixed
1921

2022
### Removed

cmd/src/campaign_progress_printer.go

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"github.com/sourcegraph/src-cli/internal/output"
1010
)
1111

12-
func newCampaignProgressPrinter(out *output.Output, numParallelism int) *campaignProgressPrinter {
12+
func newCampaignProgressPrinter(out *output.Output, verbose bool, numParallelism int) *campaignProgressPrinter {
1313
return &campaignProgressPrinter{
1414
out: out,
1515

16+
verbose: verbose,
17+
1618
numParallelism: numParallelism,
1719

1820
completedTasks: map[string]bool{},
@@ -26,6 +28,8 @@ func newCampaignProgressPrinter(out *output.Output, numParallelism int) *campaig
2628
type campaignProgressPrinter struct {
2729
out *output.Output
2830

31+
verbose bool
32+
2933
progress output.ProgressWithStatusBars
3034
numStatusBars int
3135

@@ -134,18 +138,47 @@ func (p *campaignProgressPrinter) PrintStatuses(statuses []*campaigns.TaskStatus
134138
}
135139

136140
for _, ts := range newlyCompleted {
137-
statusText, err := taskStatusText(ts)
138-
if err != nil {
139-
p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err)
140-
continue
141+
var fileDiffs []*diff.FileDiff
142+
143+
if ts.ChangesetSpec != nil {
144+
var err error
145+
fileDiffs, err = diff.ParseMultiFileDiff([]byte(ts.ChangesetSpec.Commits[0].Diff))
146+
if err != nil {
147+
p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err)
148+
continue
149+
}
141150
}
142151

143-
p.progress.Verbosef("%-*s %s", p.maxRepoName, ts.RepoName, statusText)
152+
if p.verbose {
153+
p.progress.WriteLine(output.Linef("", output.StylePending, "%s", ts.RepoName))
154+
155+
if ts.ChangesetSpec == nil {
156+
p.progress.Verbosef(" No changes")
157+
} else {
158+
lines, err := verboseDiffSummary(fileDiffs)
159+
if err != nil {
160+
p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err)
161+
continue
162+
}
163+
164+
for _, line := range lines {
165+
p.progress.Verbose(line)
166+
}
167+
}
168+
169+
p.progress.Verbose("")
170+
}
144171

145172
if idx, ok := p.repoStatusBar[ts.RepoName]; ok {
146173
// Log that this task completed, but only if there is no
147174
// currently executing one in this bar, to avoid flicker.
148175
if _, ok := p.statusBarRepo[idx]; !ok {
176+
statusText, err := taskStatusBarText(ts)
177+
if err != nil {
178+
p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err)
179+
continue
180+
}
181+
149182
if ts.Err != nil {
150183
p.progress.StatusBarFailf(idx, statusText)
151184
} else {
@@ -163,7 +196,7 @@ func (p *campaignProgressPrinter) PrintStatuses(statuses []*campaigns.TaskStatus
163196
continue
164197
}
165198

166-
statusText, err := taskStatusText(ts)
199+
statusText, err := taskStatusBarText(ts)
167200
if err != nil {
168201
p.progress.Verbosef("%-*s failed to display status: %s", p.maxRepoName, ts.RepoName, err)
169202
continue
@@ -181,7 +214,7 @@ type statusTexter interface {
181214
StatusText() string
182215
}
183216

184-
func taskStatusText(ts *campaigns.TaskStatus) (string, error) {
217+
func taskStatusBarText(ts *campaigns.TaskStatus) (string, error) {
185218
var statusText string
186219

187220
if ts.IsCompleted() {
@@ -223,3 +256,57 @@ func taskStatusText(ts *campaigns.TaskStatus) (string, error) {
223256

224257
return statusText, nil
225258
}
259+
260+
func verboseDiffSummary(fileDiffs []*diff.FileDiff) ([]string, error) {
261+
var (
262+
lines []string
263+
264+
maxFilenameLen int
265+
sumInsertions int
266+
sumDeletions int
267+
)
268+
269+
fileStats := make(map[string]string, len(fileDiffs))
270+
271+
for _, f := range fileDiffs {
272+
name := f.NewName
273+
if name == "/dev/null" {
274+
name = f.OrigName
275+
}
276+
277+
if len(name) > maxFilenameLen {
278+
maxFilenameLen = len(name)
279+
}
280+
281+
stat := f.Stat()
282+
283+
sumInsertions += int(stat.Added) + int(stat.Changed)
284+
sumDeletions += int(stat.Deleted) + int(stat.Changed)
285+
286+
num := stat.Added + 2*stat.Changed + stat.Deleted
287+
288+
fileStats[name] = fmt.Sprintf("%d %s", num, diffStatDiagram(stat))
289+
}
290+
291+
for file, stats := range fileStats {
292+
lines = append(lines, fmt.Sprintf("\t%-*s | %s", maxFilenameLen, file, stats))
293+
}
294+
295+
var insertionsPlural string
296+
if sumInsertions != 0 {
297+
insertionsPlural = "s"
298+
}
299+
300+
var deletionsPlural string
301+
if sumDeletions != 1 {
302+
deletionsPlural = "s"
303+
}
304+
305+
lines = append(lines, fmt.Sprintf(" %s, %s, %s",
306+
diffStatDescription(fileDiffs),
307+
fmt.Sprintf("%d insertion%s", sumInsertions, insertionsPlural),
308+
fmt.Sprintf("%d deletion%s", sumDeletions, deletionsPlural),
309+
))
310+
311+
return lines, nil
312+
}

cmd/src/campaigns_common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func campaignsExecute(ctx context.Context, out *output.Output, svc *campaigns.Se
232232
campaignsCompletePending(pending, "Resolved repositories")
233233
}
234234

235-
p := newCampaignProgressPrinter(out, opts.Parallelism)
235+
p := newCampaignProgressPrinter(out, *verbose, opts.Parallelism)
236236
specs, err := svc.ExecuteCampaignSpec(ctx, repos, executor, campaignSpec, p.PrintStatuses)
237237
if err != nil {
238238
return "", "", err

internal/campaigns/service.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@ type ExecutorOpts struct {
188188
Parallelism int
189189
Timeout time.Duration
190190

191-
ClearCache bool
192-
KeepLogs bool
193-
VerboseLogger bool
194-
TempDir string
195-
CacheDir string
191+
ClearCache bool
192+
KeepLogs bool
193+
TempDir string
194+
CacheDir string
196195
}
197196

198197
func (svc *Service) NewExecutor(opts ExecutorOpts) Executor {

0 commit comments

Comments
 (0)