Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c2639cf
dvc: add support for dvc diff command
django-kz Mar 23, 2019
716a5ee
move print output from repo/diff.py to command/diff.py and minor fixe…
django-kz Mar 25, 2019
8787995
dvc: fixes style issues with string ops
django-kz Mar 25, 2019
b525196
repo: replace custom _convert_size func to humanize lib
django-kz Mar 25, 2019
8b50ef8
repo: replace custom plural func to inflect lib usage
django-kz Mar 25, 2019
1c9d729
scm: add usage of os.path.normpath
django-kz Mar 25, 2019
3499c27
dvc: fix comment style
django-kz Mar 25, 2019
73b3b57
test: change some test logic for test_diff.py
django-kz Mar 25, 2019
f0225d8
dvc: change logic of getting dvc
django-kz Mar 26, 2019
f0a9255
tests: fix test_diff with new logic
django-kz Mar 27, 2019
49dead0
tests: add CmdDiff command line parsing test
django-kz Mar 27, 2019
23b4205
dvc: change help output of diff command and minor fixes
django-kz Mar 27, 2019
44e2ff7
repo: change the returning value of repo.diff
django-kz Mar 27, 2019
5f68b8e
dvc: replace keys in dictionary with macros in dvc/repo/diff.py
django-kz Apr 1, 2019
c235850
scm: change the order of dvc diff with one argument
django-kz Apr 1, 2019
e5d6218
tests: fix the test for dvc diff command
django-kz Apr 1, 2019
c3d70f2
repo: adjust repo.diff API to match `dvc diff` CLI
django-kz Apr 1, 2019
7b3790e
repo: fix macros placement from `dvc/scm` to `dvc/repo/diff`
django-kz Apr 1, 2019
f7550c1
repo: change FileNotFoundError exception to IOError
django-kz Apr 2, 2019
fcca6ae
tests: add tests for CLI message output
django-kz Apr 3, 2019
ca2f4c5
repo: add checker for out.cheme != "local" case for `dvc diff`
django-kz Apr 3, 2019
585babb
dvc: some refactoring of code
django-kz Apr 4, 2019
fe043ce
repo: refactoring of _get_diff_outs funciton
django-kz Apr 4, 2019
a5c04e9
command: refactoring of _show function in diff.py
django-kz Apr 5, 2019
505b76a
repo: refactoring of dvc/repo/diff.py
django-kz Apr 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dvc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dvc.command.daemon as daemon
import dvc.command.commit as commit
import dvc.command.tag as tag
import dvc.command.diff as diff
from dvc.exceptions import DvcParserError
from dvc import VERSION

Expand Down Expand Up @@ -62,6 +63,7 @@
daemon,
commit,
tag,
diff,
]


Expand Down
153 changes: 153 additions & 0 deletions dvc/command/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from __future__ import unicode_literals

import humanize
import inflect


import dvc.logger as logger
from dvc.exceptions import DvcException
from dvc.command.base import CmdBase
from dvc.utils.collections import compact
import dvc.repo.diff as diff


class CmdDiff(CmdBase):
def _print_size(self, size):
if size < 0:
change = "decreased by {}"
elif size > 0:
change = "increased by {}"
else:
change = "not changed"
natur_size = humanize.naturalsize(abs(size))
return change.format(natur_size)

def _get_md5_string(self, sign, file_name, checksum):
sample_msg = ""
if file_name:
sample_msg = "{}{} with md5 {}\n"
sample_msg = sample_msg.format(sign, file_name, checksum)
return sample_msg

def _get_dir_changes(self, dct):
engine = inflect.engine()
changes_msg = (
"{} {} not changed, {} {} modified, {} {} added, "
"{} {} deleted, size was {}"
)
changes_msg = changes_msg.format(
dct[diff.DIFF_IDENT],
engine.plural("file", dct[diff.DIFF_IDENT]),
dct[diff.DIFF_CHANGE],
engine.plural("file", dct[diff.DIFF_CHANGE]),
dct[diff.DIFF_NEW],
engine.plural("file", dct[diff.DIFF_NEW]),
dct[diff.DIFF_DEL],
engine.plural("file", dct[diff.DIFF_DEL]),
self._print_size(dct[diff.DIFF_SIZE]),
)
return changes_msg

def _get_file_changes(self, dct):
if (
dct.get(diff.DIFF_OLD_FILE)
and dct.get(diff.DIFF_NEW_FILE)
and dct[diff.DIFF_SIZE] == 0
):
msg = "file size was not changed"
elif dct.get(diff.DIFF_NEW_FILE):
msg = "added file with size {}".format(
humanize.naturalsize(dct[diff.DIFF_SIZE])
)
elif dct.get(diff.DIFF_OLD_FILE):
msg = "deleted file with size {}".format(
humanize.naturalsize(abs(dct[diff.DIFF_SIZE]))
)
else:
msg = "file was modified, file size {}".format(
self._print_size(dct[diff.DIFF_SIZE])
)
return msg

def _get_royal_changes(self, dct):
if dct[diff.DIFF_SIZE] != diff.DIFF_SIZE_UNKNOWN:
if dct.get("is_dir"):
return self._get_dir_changes(dct)
else:
return self._get_file_changes(dct)
return "size is ?"

def _show(self, diff_dct):
msg = "dvc diff from {} to {}".format(
diff_dct[diff.DIFF_A_REF], diff_dct[diff.DIFF_B_REF]
)
if diff_dct.get(diff.DIFF_EQUAL):
logger.info(msg)
return
for dct in diff_dct[diff.DIFF_LIST]:
msg += "\n\ndiff for '{}'\n".format(dct[diff.DIFF_TARGET])
msg += self._get_md5_string(
"-",
dct.get(diff.DIFF_OLD_FILE),
dct.get(diff.DIFF_OLD_CHECKSUM),
)
msg += self._get_md5_string(
"+",
dct.get(diff.DIFF_NEW_FILE),
dct.get(diff.DIFF_NEW_CHECKSUM),
)
msg += "\n"
msg += self._get_royal_changes(dct)
logger.info(msg)
return msg

def run(self):
try:
msg = self.repo.diff(
self.args.a_ref, target=self.args.target, b_ref=self.args.b_ref
)
self._show(msg)
except DvcException:
msg = "failed to get 'diff {}'"
args = " ".join(
compact([self.args.target, self.args.a_ref, self.args.b_ref])
)
msg = msg.format(args)
logger.error(msg)
return 1
return 0


def add_parser(subparsers, parent_parser):
description = (
"Show diff of a data file or a directory that "
"is under DVC control. Some basic statistics "
"summary, how many files were deleted/changed."
)
help = "Show a diff of a DVC controlled data file or a directory."
diff_parser = subparsers.add_parser(
"diff", parents=[parent_parser], description=description, help=help
)
diff_parser.add_argument(
"-t",
"--target",
default=None,
help=(
"Source path to a data file or directory. Default None,"
"If not specified, compares all files and directories "
"that are under DVC control in the current working space."
),
)
diff_parser.add_argument(
"a_ref", help="Git reference from which diff calculates"
)
diff_parser.add_argument(
"b_ref",
help=(
"Git reference till which diff calculates, if omitted "
"diff shows the difference between current HEAD and a_ref"
),
nargs="?",
default=None,
)
diff_parser.set_defaults(func=CmdDiff)
1 change: 1 addition & 0 deletions dvc/repo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Repo(object):
from dvc.repo.gc import gc
from dvc.repo.commit import commit
from dvc.repo.pkg import install_pkg
from dvc.repo.diff import diff
from dvc.repo.brancher import brancher

def __init__(self, root_dir=None):
Expand Down
Loading