Skip to content

Commit 02a5f92

Browse files
committed
Binary diffs
1 parent c9902bf commit 02a5f92

25 files changed

Lines changed: 220 additions & 291 deletions

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
golang 1.18.1
1+
golang 1.19.3
22
shfmt 3.2.0
33
shellcheck 0.7.1

cmd/src/batch_apply.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
"flag"
66
"fmt"
77

8-
"github.com/sourcegraph/src-cli/internal/batches/ui"
98
"github.com/sourcegraph/src-cli/internal/cmderrors"
10-
11-
"github.com/sourcegraph/sourcegraph/lib/output"
129
)
1310

1411
func init() {
@@ -47,15 +44,7 @@ Examples:
4744
ctx, cancel := contextCancelOnInterrupt(context.Background())
4845
defer cancel()
4946

50-
var execUI ui.ExecUI
51-
if flags.textOnly {
52-
execUI = &ui.JSONLines{}
53-
} else {
54-
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
55-
execUI = &ui.TUI{Out: out}
56-
}
57-
58-
if err = executeBatchSpec(ctx, execUI, executeBatchSpecOpts{
47+
if err = executeBatchSpec(ctx, executeBatchSpecOpts{
5948
flags: flags,
6049
client: cfg.apiClient(flags.api, flagSet.Output()),
6150
file: file,

cmd/src/batch_common.go

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/mattn/go-isatty"
1919

2020
"github.com/sourcegraph/sourcegraph/lib/errors"
21+
"github.com/sourcegraph/sourcegraph/lib/output"
2122

2223
batcheslib "github.com/sourcegraph/sourcegraph/lib/batches"
2324
"github.com/sourcegraph/sourcegraph/lib/batches/template"
@@ -253,20 +254,32 @@ type executeBatchSpecOpts struct {
253254
// executeBatchSpec performs all the steps required to upload the batch spec to
254255
// Sourcegraph, including execution as needed and applying the resulting batch
255256
// spec if specified.
256-
func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOpts) (err error) {
257+
func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error) {
258+
svc := service.New(&service.Opts{
259+
Client: opts.client,
260+
})
261+
262+
ffs, err := svc.DetermineFeatureFlags(ctx)
263+
if err != nil {
264+
return err
265+
}
266+
var execUI ui.ExecUI
267+
if opts.flags.textOnly {
268+
execUI = &ui.JSONLines{BinaryDiffs: ffs.BinaryDiffs}
269+
} else {
270+
out := output.NewOutput(os.Stderr, output.OutputOpts{Verbose: *verbose})
271+
execUI = &ui.TUI{Out: out}
272+
}
273+
257274
defer func() {
258275
if err != nil {
259-
ui.ExecutionError(err)
276+
execUI.ExecutionError(err)
260277
}
261278
}()
262279

263-
svc := service.New(&service.Opts{
264-
Client: opts.client,
265-
})
266-
267280
imageCache := docker.NewImageCache()
268281

269-
if err := validateSourcegraphVersionConstraint(ctx, svc); err != nil {
282+
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
270283
return err
271284
}
272285

@@ -309,45 +322,45 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
309322
}
310323

311324
// Parse flags and build up our service and executor options.
312-
ui.ParsingBatchSpec()
325+
execUI.ParsingBatchSpec()
313326
batchSpec, batchSpecDir, rawSpec, err := parseBatchSpec(ctx, opts.file, svc)
314327
if err != nil {
315328
var multiErr errors.MultiError
316329
if errors.As(err, &multiErr) {
317-
ui.ParsingBatchSpecFailure(multiErr)
330+
execUI.ParsingBatchSpecFailure(multiErr)
318331
return cmderrors.ExitCode(2, nil)
319332
} else {
320333
// This shouldn't happen; let's just punt and let the normal
321334
// rendering occur.
322335
return err
323336
}
324337
}
325-
ui.ParsingBatchSpecSuccess()
338+
execUI.ParsingBatchSpecSuccess()
326339

327-
ui.ResolvingNamespace()
340+
execUI.ResolvingNamespace()
328341
namespace, err := svc.ResolveNamespace(ctx, opts.flags.namespace)
329342
if err != nil {
330343
return err
331344
}
332-
ui.ResolvingNamespaceSuccess(namespace.ID)
345+
execUI.ResolvingNamespaceSuccess(namespace.ID)
333346

334347
var workspaceCreator workspace.Creator
335348

336349
if len(batchSpec.Steps) > 0 {
337-
ui.PreparingContainerImages()
350+
execUI.PreparingContainerImages()
338351
images, err := svc.EnsureDockerImages(
339352
ctx,
340353
imageCache,
341354
batchSpec.Steps,
342355
parallelism,
343-
ui.PreparingContainerImagesProgress,
356+
execUI.PreparingContainerImagesProgress,
344357
)
345358
if err != nil {
346359
return err
347360
}
348-
ui.PreparingContainerImagesSuccess()
361+
execUI.PreparingContainerImagesSuccess()
349362

350-
ui.DeterminingWorkspaceCreatorType()
363+
execUI.DeterminingWorkspaceCreatorType()
351364
var typ workspace.CreatorType
352365
workspaceCreator, typ = workspace.NewCreator(ctx, opts.flags.workspace, opts.flags.cacheDir, opts.flags.tempDir, images)
353366
if typ == workspace.CreatorTypeVolume {
@@ -357,21 +370,21 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
357370
return err
358371
}
359372
}
360-
ui.DeterminingWorkspaceCreatorTypeSuccess(typ)
373+
execUI.DeterminingWorkspaceCreatorTypeSuccess(typ)
361374
}
362375

363-
ui.DeterminingWorkspaces()
376+
execUI.DeterminingWorkspaces()
364377
workspaces, repos, err := svc.ResolveWorkspacesForBatchSpec(ctx, batchSpec, opts.flags.allowUnsupported, opts.flags.allowIgnored)
365378
if err != nil {
366379
if repoSet, ok := err.(batches.UnsupportedRepoSet); ok {
367-
ui.DeterminingWorkspacesSuccess(len(workspaces), len(repos), repoSet, nil)
380+
execUI.DeterminingWorkspacesSuccess(len(workspaces), len(repos), repoSet, nil)
368381
} else if repoSet, ok := err.(batches.IgnoredRepoSet); ok {
369-
ui.DeterminingWorkspacesSuccess(len(workspaces), len(repos), nil, repoSet)
382+
execUI.DeterminingWorkspacesSuccess(len(workspaces), len(repos), nil, repoSet)
370383
} else {
371384
return errors.Wrap(err, "resolving repositories")
372385
}
373386
} else {
374-
ui.DeterminingWorkspacesSuccess(len(workspaces), len(repos), nil, nil)
387+
execUI.DeterminingWorkspacesSuccess(len(workspaces), len(repos), nil, nil)
375388
}
376389

377390
archiveRegistry := repozip.NewArchiveRegistry(opts.client, opts.flags.cacheDir, opts.flags.cleanArchives)
@@ -389,14 +402,16 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
389402
TempDir: opts.flags.tempDir,
390403
GlobalEnv: os.Environ(),
391404
ForceRoot: opts.flags.runAsRoot,
405+
BinaryDiffs: ffs.BinaryDiffs,
392406
},
393-
Logger: logManager,
394-
Cache: executor.NewDiskCache(opts.flags.cacheDir),
395-
GlobalEnv: os.Environ(),
407+
Logger: logManager,
408+
Cache: executor.NewDiskCache(opts.flags.cacheDir),
409+
BinaryDiffs: ffs.BinaryDiffs,
410+
GlobalEnv: os.Environ(),
396411
},
397412
)
398413

399-
ui.CheckingCache()
414+
execUI.CheckingCache()
400415
tasks := svc.BuildTasks(
401416
&template.BatchChangeAttributes{
402417
Name: batchSpec.Name,
@@ -421,9 +436,9 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
421436
return err
422437
}
423438
}
424-
ui.CheckingCacheSuccess(len(specs), len(uncachedTasks))
439+
execUI.CheckingCacheSuccess(len(specs), len(uncachedTasks))
425440

426-
taskExecUI := ui.ExecutingTasks(*verbose, parallelism)
441+
taskExecUI := execUI.ExecutingTasks(*verbose, parallelism)
427442
freshSpecs, logFiles, execErr := coord.ExecuteAndBuildSpecs(ctx, batchSpec, uncachedTasks, taskExecUI)
428443
// Add external changeset specs.
429444
importedSpecs, importErr := svc.CreateImportChangesetSpecs(ctx, batchSpec)
@@ -440,7 +455,7 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
440455
if err == nil {
441456
taskExecUI.Success()
442457
} else {
443-
ui.ExecutingTasksSkippingErrors(err)
458+
execUI.ExecutingTasksSkippingErrors(err)
444459
}
445460
} else {
446461
if err != nil {
@@ -450,7 +465,7 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
450465
}
451466

452467
if len(logFiles) > 0 && opts.flags.keepLogs {
453-
ui.LogFilesKept(logFiles)
468+
execUI.LogFilesKept(logFiles)
454469
}
455470

456471
specs = append(specs, freshSpecs...)
@@ -464,29 +479,29 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
464479
ids := make([]graphql.ChangesetSpecID, len(specs))
465480

466481
if len(specs) > 0 {
467-
ui.UploadingChangesetSpecs(len(specs))
482+
execUI.UploadingChangesetSpecs(len(specs))
468483

469484
for i, spec := range specs {
470485
id, err := svc.CreateChangesetSpec(ctx, spec)
471486
if err != nil {
472487
return err
473488
}
474489
ids[i] = id
475-
ui.UploadingChangesetSpecsProgress(i+1, len(specs))
490+
execUI.UploadingChangesetSpecsProgress(i+1, len(specs))
476491
}
477492

478-
ui.UploadingChangesetSpecsSuccess(ids)
493+
execUI.UploadingChangesetSpecsSuccess(ids)
479494
} else if len(repos) == 0 {
480-
ui.NoChangesetSpecs()
495+
execUI.NoChangesetSpecs()
481496
}
482497

483-
ui.CreatingBatchSpec()
498+
execUI.CreatingBatchSpec()
484499
id, url, err := svc.CreateBatchSpec(ctx, namespace.ID, rawSpec, ids)
485500
if err != nil {
486-
return ui.CreatingBatchSpecError(err)
501+
return execUI.CreatingBatchSpecError(err)
487502
}
488503
previewURL := cfg.Endpoint + url
489-
ui.CreatingBatchSpecSuccess(previewURL)
504+
execUI.CreatingBatchSpecSuccess(previewURL)
490505

491506
hasWorkspaceFiles := false
492507
for _, step := range batchSpec.Steps {
@@ -496,26 +511,26 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
496511
}
497512
}
498513
if hasWorkspaceFiles {
499-
ui.UploadingWorkspaceFiles()
514+
execUI.UploadingWorkspaceFiles()
500515
if err = svc.UploadBatchSpecWorkspaceFiles(ctx, batchSpecDir, string(id), batchSpec.Steps); err != nil {
501516
// Since failing to upload workspace files should not stop processing, just warn
502-
ui.UploadingWorkspaceFilesWarning(errors.Wrap(err, "uploading workspace files"))
517+
execUI.UploadingWorkspaceFilesWarning(errors.Wrap(err, "uploading workspace files"))
503518
} else {
504-
ui.UploadingWorkspaceFilesSuccess()
519+
execUI.UploadingWorkspaceFilesSuccess()
505520
}
506521
}
507522

508523
if !opts.applyBatchSpec {
509-
ui.PreviewBatchSpec(previewURL)
524+
execUI.PreviewBatchSpec(previewURL)
510525
return
511526
}
512527

513-
ui.ApplyingBatchSpec()
528+
execUI.ApplyingBatchSpec()
514529
batch, err := svc.ApplyBatchChange(ctx, id)
515530
if err != nil {
516531
return err
517532
}
518-
ui.ApplyingBatchSpecSuccess(cfg.Endpoint + batch.URL)
533+
execUI.ApplyingBatchSpecSuccess(cfg.Endpoint + batch.URL)
519534

520535
return nil
521536
}
@@ -617,11 +632,7 @@ func getBatchParallelism(ctx context.Context, flag int) (int, error) {
617632
return docker.NCPU(ctx)
618633
}
619634

620-
func validateSourcegraphVersionConstraint(ctx context.Context, svc *service.Service) error {
621-
ffs, err := svc.DetermineFeatureFlags(ctx)
622-
if err != nil {
623-
return err
624-
}
635+
func validateSourcegraphVersionConstraint(ctx context.Context, ffs *batches.FeatureFlags) error {
625636
if ffs.Sourcegraph40 {
626637
return nil
627638
}

cmd/src/batch_exec.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type executorModeFlags struct {
3737
tempDir string
3838
repoDir string
3939
workspaceFilesDir string
40+
binaryDiffs bool
4041
}
4142

4243
func newExecutorModeFlags(flagSet *flag.FlagSet) (f *executorModeFlags) {
@@ -47,6 +48,7 @@ func newExecutorModeFlags(flagSet *flag.FlagSet) (f *executorModeFlags) {
4748
flagSet.StringVar(&f.tempDir, "tmp", "", "Directory for storing temporary data.")
4849
flagSet.StringVar(&f.repoDir, "repo", "", "Path of the checked out repo on disk.")
4950
flagSet.StringVar(&f.workspaceFilesDir, "workspaceFiles", "", "Path of workspace files on disk.")
51+
flagSet.BoolVar(&f.binaryDiffs, "binaryDiffs", false, "Whether to encode diffs as base64.")
5052

5153
return f
5254
}
@@ -121,7 +123,7 @@ Examples:
121123
}
122124

123125
func executeBatchSpecInWorkspaces(ctx context.Context, flags *executorModeFlags) (err error) {
124-
ui := &ui.JSONLines{}
126+
ui := &ui.JSONLines{BinaryDiffs: flags.binaryDiffs}
125127

126128
// Ensure the temp dir exists.
127129
tempDir := flags.tempDir
@@ -211,6 +213,7 @@ func executeBatchSpecInWorkspaces(ctx context.Context, flags *executorModeFlags)
211213
RepoArchive: &repozip.NoopArchive{},
212214
UI: taskExecUI.StepsExecutionUI(task),
213215
ForceRoot: !flags.runAsImageUser,
216+
BinaryDiffs: flags.binaryDiffs,
214217
}
215218
results, err := executor.RunSteps(ctx, opts)
216219

cmd/src/batch_new.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ Examples:
4848
Client: cfg.apiClient(apiFlags, flagSet.Output()),
4949
})
5050

51-
if err := validateSourcegraphVersionConstraint(ctx, svc); err != nil {
51+
ffs, err := svc.DetermineFeatureFlags(ctx)
52+
if err != nil {
53+
return err
54+
}
55+
56+
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
5257
return err
5358
}
5459

cmd/src/batch_preview.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import (
55
"flag"
66
"fmt"
77

8-
"github.com/sourcegraph/src-cli/internal/batches/ui"
98
"github.com/sourcegraph/src-cli/internal/cmderrors"
10-
11-
"github.com/sourcegraph/sourcegraph/lib/output"
129
)
1310

1411
func init() {
@@ -45,15 +42,7 @@ Examples:
4542
ctx, cancel := contextCancelOnInterrupt(context.Background())
4643
defer cancel()
4744

48-
var execUI ui.ExecUI
49-
if flags.textOnly {
50-
execUI = &ui.JSONLines{}
51-
} else {
52-
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
53-
execUI = &ui.TUI{Out: out}
54-
}
55-
56-
if err = executeBatchSpec(ctx, execUI, executeBatchSpecOpts{
45+
if err = executeBatchSpec(ctx, executeBatchSpecOpts{
5746
flags: flags,
5847
client: cfg.apiClient(flags.api, flagSet.Output()),
5948
file: file,

cmd/src/batch_remote.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ Examples:
5252
Client: cfg.apiClient(flags.api, flagSet.Output()),
5353
})
5454

55-
if err := validateSourcegraphVersionConstraint(ctx, svc); err != nil {
55+
ffs, err := svc.DetermineFeatureFlags(ctx)
56+
if err != nil {
57+
return err
58+
}
59+
60+
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
5661
return err
5762
}
5863

cmd/src/batch_repositories.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ Examples:
6464
Client: client,
6565
})
6666

67-
if err := validateSourcegraphVersionConstraint(ctx, svc); err != nil {
67+
ffs, err := svc.DetermineFeatureFlags(ctx)
68+
if err != nil {
69+
return err
70+
}
71+
72+
if err := validateSourcegraphVersionConstraint(ctx, ffs); err != nil {
6873
return err
6974
}
7075

0 commit comments

Comments
 (0)