Bug 1369787 - [mozlint] Fix up version control commands, r=standard8

The underlying commands to mozlint's vcs.py had a few problems. This:

    1. Ensures deleted files aren't considered in both hg and git
    2. Automatically determines the default remote repo and branch when using --outgoing with git
    3. Excludes metadata from output of the git command used with --outgoing
    4. Adds --cached to the git command for --workdir, which lints all staged files

MozReview-Commit-ID: EBtM3MCiCDs

--HG--
extra : rebase_source : 982b24acd81c86e383b28b8a71bcd51a041e60f4
This commit is contained in:
Andrew Halberstadt 2017-06-08 23:31:16 -04:00
parent 5fde640164
commit 4761330083
2 changed files with 34 additions and 10 deletions

View File

@ -43,9 +43,10 @@ class MozlintParser(ArgumentParser):
[['-o', '--outgoing'],
{'const': 'default',
'nargs': '?',
'help': "Lint files touched by commits that are not on the remote repository."
"If you are using git please specify which remote you want to compare to."
"Works with mercurial or git."
'help': "Lint files touched by commits that are not on the remote repository. "
"Without arguments, finds the default remote that would be pushed to. "
"The remote branch can also be specified manually. Works with "
"mercurial or git."
}],
[['-w', '--workdir'],
{'default': False,

View File

@ -54,8 +54,8 @@ class HgHelper(VCSHelper):
"""A helper to find files to lint from Mercurial."""
def by_outgoing(self, dest='default'):
return self.run(['hg', 'outgoing', '--quiet', '-r .',
dest, '--template', '{files % "\n{file}"}'])
return self.run(['hg', 'outgoing', '--quiet', '--template',
"{file_mods % '\\n{file}'}{file_adds % '\\n{file}'}", '-r', '.', dest])
def by_workdir(self):
return self.run(['hg', 'status', '-amn'])
@ -63,16 +63,39 @@ class HgHelper(VCSHelper):
class GitHelper(VCSHelper):
"""A helper to find files to lint from Git."""
_default = None
@property
def default(self):
if self._default:
return self._default
ref = subprocess.check_output(['git', 'symbolic-ref', '-q', 'HEAD']).strip()
dest = subprocess.check_output(
['git', 'for-each-ref', '--format=%(upstream:short)', ref]).strip()
if not dest:
branches = subprocess.check_output(['git', 'branch', '--list'])
for b in ('master', 'central', 'default'):
if b in branches and not ref.endswith(b):
dest = b
break
self._default = dest
return self._default
def by_outgoing(self, dest='default'):
if dest == 'default':
comparing = 'origin/master..HEAD'
else:
comparing = '{}..HEAD'.format(dest)
return self.run(['git', 'log', '--name-only', comparing])
if not self.default:
print("warning: could not find default push, specify a remote for --outgoing")
return []
dest = self.default
comparing = '{}..HEAD'.format(self.default)
return self.run(['git', 'log', '--name-only', '--diff-filter=AM',
'--oneline', '--pretty=format:', comparing])
def by_workdir(self):
return self.run(['git', 'diff', '--name-only'])
return self.run(['git', 'diff', '--name-only', '--diff-filter=AM', 'HEAD'])
vcs_class = {