-
Notifications
You must be signed in to change notification settings - Fork 76
Allow using various git diff options for comparisons #126
Conversation
Instead of having to do funky stuff with soft git resets, this allows the user to pass a `--diff` option. This option can be anything that works with both `git diff-tree` and `git rev-list`. This option does not work with mercurial yet. This is particularly helpful in CI pipelines that rely on PRs. Effectively, you run `git lint --diff 'HEAD ^<target-branch>'` The change basically works by looking at all the commits that are different instead of just the `--last-commit`. It uses the `git rev-list` command to determine which commits are different. It then uses the same `modified_lines` function with a slight modification. `modified lines` now uses a regex OR condition for all of the commits instead of just one commit.
It wanted to break the arguments onto multiple line in the usage. This made it less readable IMO so I disabled the linter for that section
Can't satisfy pycodestyle and docopt at the same time without breaking it onto multiple lines. Doesn't read as well in my opinion.
Thanks @timmartin19 for the PR, I like the idea, but would like to clarify and simplify some things.
|
gitlint/__init__.py
Outdated
last_commit = vcs.last_commit() | ||
commits = [last_commit] | ||
|
||
if arguments['--diff'] and vcs.__name__ != 'gitlint.git': |
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
gitlint/git.py
Outdated
|
||
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
For safety I'd prefer to escape the commits.
test/unittest/test_gitlint.py
Outdated
@@ -17,6 +17,7 @@ | |||
import os | |||
import sys | |||
|
|||
from docopt import DocoptExit |
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 follow the Google style guide, so we only import modules.
gitlint/git.py
Outdated
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 comment
The 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.
@sk- I think you're absolutely right about always comparing against I don't think a detached HEAD will cause any problems since we aren't making any changes nor committing. That being said, I'll play with it in a branch in my fork on travis and play with it in Jenkins. |
It gets pretty hard to compare arbitrarily. Instead, it now simply compares a target branch or commit against the current head. This also simplifies `--last-commit` since it's effectively the same as `--diff HEAD^`. This did require some refactoring to keep the code base clean. Also, adding an e2e test for comparing a branch multiple commits ahead of master against master.
I really need to set up the local commit hooks
I checked whether it worked with travis. Do to their use of
|
@sk- Is there anything else you need for this PR? |
@timmartin19 Hi Tim, the fact that you still need to wrangle with the git settings in order to be able to run it in Travis makes me doubt about the general usefulness of the PR, as the motivating case was exactly to support CI (hence the mention of git reset). |
@sk- That's a quirk of travis as far as I can tell. We're running it in Jenkins now with no special adjustments and it's working just fine. Also, this does not break (like the current CI mechanism) when you force push. The soft reset mechanism being used in Travis is not really workable across different CI tools. |
Instead of having to do funky stuff with soft git resets, this allows the user
to pass a
--diff
option. This option can be anything that works with bothgit diff-tree
andgit rev-list
. This option does not work with mercurialyet. This is particularly helpful in CI pipelines that rely on PRs.
Effectively, you run
git lint --diff 'HEAD ^<target-branch>'
The change basically works by looking at all the commits that are different
instead of just the
--last-commit
. It uses thegit rev-list
command todetermine which commits are different. It then uses the same
modified_lines
function with a slight modification.
modified lines
now uses a regex ORcondition for all of the commits instead of just one commit.