This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Allow using various git diff options for comparisons #126
Closed
Closed
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
135a677
Allow using various git diff options for comparisons
timmartin19 f56f852
Disable pylint line length check for docopt
timmartin19 10282a5
Apparently we need to disable two types for line-too-long
timmartin19 bab9acf
Revert to multi-line usage
timmartin19 2c25861
Update --diff option to more simply compare the to HEAD
timmartin19 6baaf08
Fixing internal code styling and broken test cleanup
timmartin19 90c9d5b
Fix docopt error
timmartin19 6405631
forgot to run yapf
timmartin19 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 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 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 |
---|---|---|
|
@@ -50,14 +50,15 @@ def _remove_filename_quotes(filename): | |
return filename | ||
|
||
|
||
def modified_files(root, tracked_only=False, commit=None): | ||
def modified_files(root, tracked_only=False, commit=None, diff=None): | ||
"""Returns a list of files that has been modified since the last commit. | ||
|
||
Args: | ||
root: the root of the repository, it has to be an absolute path. | ||
tracked_only: exclude untracked files when True. | ||
commit: SHA1 of the commit. If None, it will get the modified files in the | ||
working copy. | ||
diff: A git style diff. Like dev..master, 1b2ec4..123bade, dev ^master | ||
|
||
Returns: a dictionary with the modified files as keys, and additional | ||
information as value. In this case it adds the status returned by | ||
|
@@ -68,6 +69,9 @@ def modified_files(root, tracked_only=False, commit=None): | |
if commit: | ||
return _modified_files_with_commit(root, commit) | ||
|
||
if diff: | ||
return _modified_files_with_commit(root, diff) | ||
|
||
# Convert to unicode and split | ||
status_lines = subprocess.check_output([ | ||
'git', 'status', '--porcelain', '--untracked-files=all', | ||
|
@@ -88,12 +92,14 @@ def modified_files(root, tracked_only=False, commit=None): | |
for filename, mode in modified_file_status) | ||
|
||
|
||
def _modified_files_with_commit(root, commit): | ||
def _modified_files_with_commit(root, diff_target): | ||
# Convert to unicode and split | ||
status_lines = subprocess.check_output([ | ||
'git', 'diff-tree', '-r', '--root', '--no-commit-id', '--name-status', | ||
commit | ||
]).decode('utf-8').split(os.linesep) | ||
command = [ | ||
'git', 'diff-tree', '-r', '--root', '--no-commit-id', '--name-status' | ||
] | ||
command.extend(diff_target.split(' ')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which cases is the argument going to have spaces, could you give some examples. |
||
status_lines = subprocess.check_output(command).decode('utf-8').split( | ||
os.linesep) | ||
|
||
modified_file_status = utils.filter_lines( | ||
status_lines, | ||
|
@@ -106,14 +112,14 @@ def _modified_files_with_commit(root, commit): | |
mode + ' ') for filename, mode in modified_file_status) | ||
|
||
|
||
def modified_lines(filename, extra_data, commit=None): | ||
def modified_lines(filename, extra_data, commits=None): | ||
"""Returns the lines that have been modifed for this file. | ||
|
||
Args: | ||
filename: the file to check. | ||
extra_data: is the extra_data returned by modified_files. Additionally, a | ||
value of None means that the file was not modified. | ||
commit: the complete sha1 (40 chars) of the commit. Note that specifying | ||
commits: the complete sha1 (40 chars) of the commit. Note that specifying | ||
this value will only work (100%) when commit == last_commit (with | ||
respect to the currently checked out revision), otherwise, we could miss | ||
some lines. | ||
|
@@ -126,15 +132,30 @@ def modified_lines(filename, extra_data, commit=None): | |
if extra_data not in ('M ', ' M', 'MM'): | ||
return None | ||
|
||
if commit is None: | ||
commit = '0' * 40 | ||
commit = commit.encode('utf-8') | ||
if commits is None: | ||
commits = ['0' * 40] | ||
# commit = commit.encode('utf-8') | ||
|
||
# Split as bytes, as the output may have some non unicode characters. | ||
blame_lines = subprocess.check_output( | ||
['git', 'blame', '--porcelain', filename]).split( | ||
os.linesep.encode('utf-8')) | ||
regex_pattern = r'^({}) (?P<line>\d+) (\d+)'.format( | ||
'|'.join(commits)).encode('utf-8') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For safety I'd prefer to escape the commits. |
||
modified_line_numbers = utils.filter_lines( | ||
blame_lines, commit + br' (?P<line>\d+) (\d+)', groups=('line', )) | ||
blame_lines, regex_pattern, groups=('line', )) | ||
|
||
return list(map(int, modified_line_numbers)) | ||
|
||
|
||
def commit_diff(diff): | ||
"""Returns a list of commits corresponding to the provided diff | ||
|
||
Args: | ||
diff: the diff like branch-a..branch-b or HEAD ^master | ||
|
||
Returns: A list of commit shas associated with the provided diff | ||
""" | ||
command = ['git', 'rev-list'] | ||
command.extend(diff.split(' ')) | ||
return subprocess.check_output(command).decode('utf-8').split(os.linesep) |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could do
vcs != gitlint.git