Skip to content

Instantly share code, notes, and snippets.

@isaacl
Last active March 29, 2016 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacl/3451f9e048b5cc51c9e4 to your computer and use it in GitHub Desktop.
Save isaacl/3451f9e048b5cc51c9e4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import os
import pipes
import subprocess
import sys
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__));
os.chdir(subprocess.check_output(['git', 'rev-parse', '--show-toplevel'],
cwd=SCRIPT_DIR).strip())
def capture(cmd, **args):
return subprocess.Popen(cmd, stdout=subprocess.PIPE, **args).communicate()[0]
def csplit(nstr, char='\x00'):
if not nstr:
return []
return [l for l in nstr.split(char) if l]
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-w', '--allow-warnings', action='store_true',
help="Only fail on eslint errors.")
parser.add_argument('-f', '--fast', action='store_true',
help='"fast" mode. Run only on changed files.')
parser.add_argument('--git-remote', default='refs/remotes/origin/master',
help="(fast mode only) branch to look for changed files against")
parser.add_argument('-u', '--ignore-untracked', action='store_true',
help="(fast mode only) ignore untracked files.");
opts = parser.parse_args()
eslint_args = ['--ext', '.jsx,.js']
if not opts.allow_warnings:
eslint_args.append('--max-warnings=0')
if opts.fast:
eslint_args.append('--cache')
ancestor = capture(['git', 'merge-base', 'HEAD', opts.git_remote]).strip()
paths = csplit(capture(['git', 'diff', '--diff-filter=AM', '--name-only', '-z',
'--no-renames', '--no-color', ancestor,
'--', '*.js', '*.jsx']))
if not opts.ignore_untracked:
paths.extend(csplit(capture(['git', 'ls-files', '--others', '--exclude-standard',
'-z', '--', '*.js', '*.jsx'])))
else:
paths = ['.']
if not paths:
print('No js files to lint -- skipping')
sys.exit(0)
eslint_args.extend(p for p in paths if p)
# prefer local eslint.
eslint = subprocess.check_output(
['which', 'eslint'],
env={'PATH': './node_modules/.bin:' + os.environ['PATH']}).strip()
if opts.verbose:
os.environ['DEBUG'] = 'eslint:cli-engine'
print '>', eslint, ' '.join(pipes.quote(c) for c in eslint_args)
os.execv(eslint, [eslint] + eslint_args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment