-
Notifications
You must be signed in to change notification settings - Fork 1.2k
dvc: add support for dvc diff command #1778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 716a5ee
move print output from repo/diff.py to command/diff.py and minor fixe…
django-kz 8787995
dvc: fixes style issues with string ops
django-kz b525196
repo: replace custom _convert_size func to humanize lib
django-kz 8b50ef8
repo: replace custom plural func to inflect lib usage
django-kz 1c9d729
scm: add usage of os.path.normpath
django-kz 3499c27
dvc: fix comment style
django-kz 73b3b57
test: change some test logic for test_diff.py
django-kz f0225d8
dvc: change logic of getting dvc
django-kz f0a9255
tests: fix test_diff with new logic
django-kz 49dead0
tests: add CmdDiff command line parsing test
django-kz 23b4205
dvc: change help output of diff command and minor fixes
django-kz 44e2ff7
repo: change the returning value of repo.diff
django-kz 5f68b8e
dvc: replace keys in dictionary with macros in dvc/repo/diff.py
django-kz c235850
scm: change the order of dvc diff with one argument
django-kz e5d6218
tests: fix the test for dvc diff command
django-kz c3d70f2
repo: adjust repo.diff API to match `dvc diff` CLI
django-kz 7b3790e
repo: fix macros placement from `dvc/scm` to `dvc/repo/diff`
django-kz f7550c1
repo: change FileNotFoundError exception to IOError
django-kz fcca6ae
tests: add tests for CLI message output
django-kz ca2f4c5
repo: add checker for out.cheme != "local" case for `dvc diff`
django-kz 585babb
dvc: some refactoring of code
django-kz fe043ce
repo: refactoring of _get_diff_outs funciton
django-kz a5c04e9
command: refactoring of _show function in diff.py
django-kz 505b76a
repo: refactoring of dvc/repo/diff.py
django-kz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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," | ||
django-kz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"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" | ||
efiop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.