Skip to content

Instantly share code, notes, and snippets.

@acdha
Last active April 18, 2024 02:22
Show Gist options
  • Select an option

  • Save acdha/8717683 to your computer and use it in GitHub Desktop.

Select an option

Save acdha/8717683 to your computer and use it in GitHub Desktop.

Revisions

  1. acdha revised this gist Oct 12, 2016. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,17 @@ def lint_files(changed_files):
    check_linter(['isort', '-c'], py_files)

    if '.js' in changed_extensions:
    check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))
    check_linter(['eslint'], filter_ext('.js', changed_files, exclude='.min.'))

    if '.scss' in changed_extensions:
    check_linter(['sass', '--compass', '--no-cache', '--check'], filter_ext('.scss', changed_files))
    try:
    check_linter(['scss-lint'], filter_ext('.scss', changed_files))
    except subprocess.CalledProcessError as exc:
    if exc.returncode == 1:
    # scss-lint rc=1 means no message more severe than a warning
    pass
    else:
    raise

    if '.css' in changed_extensions:
    check_linter(['csslint'], filter_ext('.css', changed_files, exclude='.min.'))
  2. acdha revised this gist Mar 17, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,14 @@ import os
    import subprocess
    import sys

    FS_ENCODING = sys.getfilesystemencoding()


    def check_linter(cmd, files, **kwargs):
    if not files:
    return
    print('Running %s' % cmd[0])
    return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs)
    return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs).decode(FS_ENCODING)


    def filter_ext(extension, files, exclude=None):
    @@ -46,12 +48,14 @@ def lint_files(changed_files):
    if __name__ == "__main__":
    os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
    changed_files = subprocess.check_output('git diff --cached --name-only --diff-filter=ACM'.split())
    changed_files = changed_files.decode(FS_ENCODING)

    try:
    lint_files(changed_files)
    except subprocess.CalledProcessError as exc:
    print('Quality check failed:', file=sys.stderr)
    print(' '.join(exc.cmd), file=sys.stderr)
    if exc.output:
    print('\t', '\n\t'.join(exc.output.splitlines()), sep='', file=sys.stderr)
    output = exc.output.decode(FS_ENCODING)
    print('\t', '\n\t'.join(output.splitlines()), sep='', file=sys.stderr)
    sys.exit(1)
  3. acdha revised this gist Mar 4, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ def lint_files(changed_files):
    check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))

    if '.scss' in changed_extensions:
    check_linter(['sass', '--no-cache', '--check'], filter_ext('.scss', changed_files))
    check_linter(['sass', '--compass', '--no-cache', '--check'], filter_ext('.scss', changed_files))

    if '.css' in changed_extensions:
    check_linter(['csslint'], filter_ext('.css', changed_files, exclude='.min.'))
  4. acdha revised this gist Feb 6, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ def lint_files(changed_files):

    if __name__ == "__main__":
    os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
    changed_files = subprocess.check_output('git diff --cached --name-only'.split())
    changed_files = subprocess.check_output('git diff --cached --name-only --diff-filter=ACM'.split())

    try:
    lint_files(changed_files)
  5. acdha revised this gist Feb 5, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,7 @@ import sys
    def check_linter(cmd, files, **kwargs):
    if not files:
    return
    print('Running %s' % cmd[0])
    return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs)


    @@ -28,21 +29,17 @@ def lint_files(changed_files):
    changed_extensions = {ext for root, ext in map(os.path.splitext, changed_files)}

    if '.py' in changed_extensions:
    print('Linting Python…')
    py_files = filter_ext('.py', changed_files)
    check_linter(['frosted'], py_files)
    check_linter(['isort', '-c'], py_files)

    if '.js' in changed_extensions:
    print('Linting JavaScript…')
    check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))

    if '.scss' in changed_extensions:
    print('Linting SASS…')
    check_linter(['sass', '--no-cache', '--check'], filter_ext('.scss', changed_files))

    if '.css' in changed_extensions:
    print('Linting CSS…')
    check_linter(['csslint'], filter_ext('.css', changed_files, exclude='.min.'))


  6. acdha revised this gist Feb 4, 2014. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,19 @@ import sys

    def check_linter(cmd, files, **kwargs):
    if not files:
    raise ValueError('Linter %s called with an empty file list!' % cmd)
    return
    return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs)


    def filter_ext(extension, files):
    return [f for f in files if f.endswith(extension)]
    def filter_ext(extension, files, exclude=None):
    files = [f for f in files if f.endswith(extension)]
    if exclude is not None:
    files = [i for i in files if exclude not in i]
    return files


    def lint_files(changed_files):
    changed_files = [i.strip() for i in changed_files.splitlines()]
    changed_files = [i.strip() for i in changed_files.splitlines() if '/external/' not in i]

    changed_extensions = {ext for root, ext in map(os.path.splitext, changed_files)}

    @@ -32,15 +35,15 @@ def lint_files(changed_files):

    if '.js' in changed_extensions:
    print('Linting JavaScript…')
    check_linter(['jshint'], filter_ext('.js', changed_files))
    check_linter(['jshint'], filter_ext('.js', changed_files, exclude='.min.'))

    if '.scss' in changed_extensions:
    print('Linting SASS…')
    check_linter(['sass', '--no-cache', '--check'], filter_ext('.scss', changed_files))

    if '.css' in changed_extensions:
    print('Linting CSS…')
    check_linter(['csslint'], filter_ext('.css', changed_files))
    check_linter(['csslint'], filter_ext('.css', changed_files, exclude='.min.'))


    if __name__ == "__main__":
  7. acdha revised this gist Jan 30, 2014. 1 changed file with 25 additions and 6 deletions.
    31 changes: 25 additions & 6 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/usr/bin/env PYTHONIOENCODING=utf-8 python
    # encoding: utf-8
    """Git pre-commit hook which lints Python, JavaScript, SASS and CSS"""

    from __future__ import absolute_import, print_function, unicode_literals

    @@ -8,20 +9,38 @@ import subprocess
    import sys


    def check_linter(cmd, files, **kwargs):
    if not files:
    raise ValueError('Linter %s called with an empty file list!' % cmd)
    return subprocess.check_output(cmd + files, stderr=subprocess.STDOUT, **kwargs)


    def filter_ext(extension, files):
    return [f for f in files if f.endswith(extension)]


    def lint_files(changed_files):
    changed_files = [i.strip() for i in changed_files.splitlines()]

    changed_extensions = {ext for root, ext in map(os.path.splitext, changed_files)}

    if '.py' in changed_extensions:
    print('Linting Python…')
    py_files = [f for f in changed_files if f.endswith('.py')]
    subprocess.check_output(['frosted'] + py_files, stderr=subprocess.STDOUT)
    subprocess.check_output(['isort', '-c'] + py_files, stderr=subprocess.STDOUT)
    py_files = filter_ext('.py', changed_files)
    check_linter(['frosted'], py_files)
    check_linter(['isort', '-c'], py_files)

    if '.js' in changed_extensions:
    print('Linting JavaScript…')
    check_linter(['jshint'], filter_ext('.js', changed_files))

    if '.scss' in changed_extensions:
    print('Linting SASS…')
    check_linter(['sass', '--no-cache', '--check'], filter_ext('.scss', changed_files))

    if '.css' in changed_extensions or '.js' in changed_extensions:
    print('Compiling static media…')
    subprocess.check_call('make')
    if '.css' in changed_extensions:
    print('Linting CSS…')
    check_linter(['csslint'], filter_ext('.css', changed_files))


    if __name__ == "__main__":
  8. acdha revised this gist Jan 30, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/env python
    #!/usr/bin/env PYTHONIOENCODING=utf-8 python
    # encoding: utf-8

    from __future__ import absolute_import, print_function, unicode_literals
    @@ -23,6 +23,7 @@ def lint_files(changed_files):
    print('Compiling static media…')
    subprocess.check_call('make')


    if __name__ == "__main__":
    os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
    changed_files = subprocess.check_output('git diff --cached --name-only'.split())
  9. acdha revised this gist Jan 30, 2014. No changes.
  10. acdha created this gist Jan 30, 2014.
    37 changes: 37 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/usr/bin/env python
    # encoding: utf-8

    from __future__ import absolute_import, print_function, unicode_literals

    import os
    import subprocess
    import sys


    def lint_files(changed_files):
    changed_files = [i.strip() for i in changed_files.splitlines()]

    changed_extensions = {ext for root, ext in map(os.path.splitext, changed_files)}

    if '.py' in changed_extensions:
    print('Linting Python…')
    py_files = [f for f in changed_files if f.endswith('.py')]
    subprocess.check_output(['frosted'] + py_files, stderr=subprocess.STDOUT)
    subprocess.check_output(['isort', '-c'] + py_files, stderr=subprocess.STDOUT)

    if '.css' in changed_extensions or '.js' in changed_extensions:
    print('Compiling static media…')
    subprocess.check_call('make')

    if __name__ == "__main__":
    os.chdir(os.path.join(os.path.dirname(__file__), '..', '..'))
    changed_files = subprocess.check_output('git diff --cached --name-only'.split())

    try:
    lint_files(changed_files)
    except subprocess.CalledProcessError as exc:
    print('Quality check failed:', file=sys.stderr)
    print(' '.join(exc.cmd), file=sys.stderr)
    if exc.output:
    print('\t', '\n\t'.join(exc.output.splitlines()), sep='', file=sys.stderr)
    sys.exit(1)