Files
kopia/cli/command_diff.go
Jarek Kowalski fcd507a56d Refactored most of the CLI tests to run in-process as opposed to using sub-processes (#1059)
* cli: fixed remaining testability indirections for output and logging

* cli: added cli.RunSubcommand() which is used in testing to execute a subcommand in the same process

* tests: refactored most e2e tests to invoke kopia subcommands in-process

* Makefile: enable code coverage for cli/ and internal/

* testing: pass 'testing' tag to unit tests which uses much faster (insecure) password hashing scheme

* Makefile: push coverage from PRs again

* tests: disable buffer management to reduce memory usage on ARM

* cli: fixed misaligned atomic field on ARMHF

also temporarily fixed statup-time benign race condition when setting
default on the timeZone variable, which is the last global variable.
2021-05-11 22:26:28 -07:00

79 lines
2.3 KiB
Go

package cli
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/kopia/kopia/fs"
"github.com/kopia/kopia/internal/diff"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/snapshot/snapshotfs"
)
type commandDiff struct {
diffFirstObjectPath string
diffSecondObjectPath string
diffCompareFiles bool
diffCommandCommand string
out textOutput
}
func (c *commandDiff) setup(svc appServices, parent commandParent) {
cmd := parent.Command("diff", "Displays differences between two repository objects (files or directories)").Alias("compare")
cmd.Arg("object-path1", "First object/path").Required().StringVar(&c.diffFirstObjectPath)
cmd.Arg("object-path2", "Second object/path").Required().StringVar(&c.diffSecondObjectPath)
cmd.Flag("files", "Compare files by launching diff command for all pairs of (old,new)").Short('f').BoolVar(&c.diffCompareFiles)
cmd.Flag("diff-command", "Displays differences between two repository objects (files or directories)").Default(defaultDiffCommand()).Envar("KOPIA_DIFF").StringVar(&c.diffCommandCommand)
cmd.Action(svc.repositoryReaderAction(c.run))
c.out.setup(svc)
}
func (c *commandDiff) run(ctx context.Context, rep repo.Repository) error {
ent1, err := snapshotfs.FilesystemEntryFromIDWithPath(ctx, rep, c.diffFirstObjectPath, false)
if err != nil {
return errors.Wrapf(err, "error getting filesystem entry for %v", c.diffFirstObjectPath)
}
ent2, err := snapshotfs.FilesystemEntryFromIDWithPath(ctx, rep, c.diffSecondObjectPath, false)
if err != nil {
return errors.Wrapf(err, "error getting filesystem entry for %v", c.diffSecondObjectPath)
}
_, isDir1 := ent1.(fs.Directory)
_, isDir2 := ent2.(fs.Directory)
if isDir1 != isDir2 {
return errors.New("arguments do diff must both be directories or both non-directories")
}
d, err := diff.NewComparer(c.out.stdout())
if err != nil {
return errors.Wrap(err, "error creating comparer")
}
defer d.Close() //nolint:errcheck
if c.diffCompareFiles {
parts := strings.Split(c.diffCommandCommand, " ")
d.DiffCommand = parts[0]
d.DiffArguments = parts[1:]
}
if isDir1 {
return d.Compare(ctx, ent1, ent2)
}
return errors.New("comparing files not implemented yet")
}
func defaultDiffCommand() string {
if isWindows() {
return "cmp"
}
return "diff -u"
}