Skip to content

Commit

Permalink
insert-license hook: switching to --comment-style + more doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Sep 14, 2017
1 parent 1e1c94c commit f1a1035
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -21,13 +21,25 @@ Hooks that require Python dependencies, or specific to a language, have been ext
- id: remove-tabs
args: [ --whitespaces-count, 2 ] # defaults to: 4
- id: insert-license
files: \.py$
files: \.groovy$
args:
- --license-filepath
- src/license_header.txt # defaults to: LICENSE.txt
- --comment-prefix
- --comment-style
- // # defaults to: #

### insert-license

For Java / Javascript / CSS, set `--comment-style /*| *| */`.

In case you want to remove the comment headers introduced by the `insert-license` hook,
e.g. because you want to change the wording of your `LICENSE.txt` and update the comments in your source files:

1. temporarily add the `--remove-header` arg in your `.pre-commit-config.yaml`
2. run the hook on all your files: `pre-commit run insert-license --all-files`
3. remove the `--remove-header` arg and update your `LICENSE.txt`
4. re-run the hook on all your files


## Other useful local hooks

Expand Down
21 changes: 13 additions & 8 deletions pre_commit_hooks/insert_license.py
Expand Up @@ -6,21 +6,26 @@ def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='filenames to check')
parser.add_argument('--license-filepath', default='LICENSE.txt')
parser.add_argument('--comment-start', default='/*\n')
parser.add_argument('--comment-prefix', default=' *')
parser.add_argument('--comment-end', default='\n */')
parser.add_argument('--comment-style', default='#',
help='Can be a single prefix or a triplet: <comment-sart>|<comment-prefix>|<comment-end>'
'E.g.: /*| *| */')
parser.add_argument('--detect-license-in-X-top-lines', type=int, default=5)
parser.add_argument('--remove-header', action='store_true')
args = parser.parse_args(argv)

if '|' in args.comment_style:
comment_start, comment_prefix, comment_end = args.comment_style.split('|')
else:
comment_start, comment_prefix, comment_end = None, args.comment_style, None

with open(args.license_filepath) as license_file:
prefixed_license = ['{}{}{}'.format(args.comment_prefix, ' ' if line.strip() else '', line)
prefixed_license = ['{}{}{}'.format(comment_prefix, ' ' if line.strip() else '', line)
for line in license_file.readlines()]
eol = '\r\n' if prefixed_license[0][-2:] == '\r\n' else '\n'
if args.comment_start:
prefixed_license = [args.comment_start] + prefixed_license
if args.comment_end:
prefixed_license = prefixed_license + [args.comment_end]
if comment_start:
prefixed_license = [comment_start+'\n'] + prefixed_license
if comment_end:
prefixed_license = prefixed_license + ['\n'+comment_end]

changes_made = False
for src_filepath in args.filenames:
Expand Down
7 changes: 4 additions & 3 deletions tests/insert_license_test.py
Expand Up @@ -18,11 +18,10 @@
),
)
def test_insert_license(src_file_path, comment_prefix, new_src_file_expected, tmpdir):
default_args = ['--comment-start', '', '--comment-end', '', '--comment-prefix']
with chdir_to_test_resources():
path = tmpdir.join('src_file_path')
shutil.copy(src_file_path, path.strpath)
assert insert_license(default_args + [comment_prefix, path.strpath]) == (1 if new_src_file_expected else 0)
assert insert_license(['--comment-style', comment_prefix, path.strpath]) == (1 if new_src_file_expected else 0)
if new_src_file_expected:
with open(new_src_file_expected) as expected_content_file:
expected_content = expected_content_file.read()
Expand Down Expand Up @@ -56,7 +55,9 @@ def test_remove_license(src_file_path, is_python, new_src_file_expected, tmpdir)
shutil.copy(src_file_path, path.strpath)
argv = ['--remove-header', path.strpath]
if is_python:
argv = ['--comment-start', '', '--comment-end', '', '--comment-prefix', '#'] + argv
argv = ['--comment-style', '#'] + argv
else:
argv = ['--comment-style', '/*| *| */'] + argv
assert insert_license(argv) == (1 if new_src_file_expected else 0)
if new_src_file_expected:
with open(new_src_file_expected) as expected_content_file:
Expand Down

0 comments on commit f1a1035

Please sign in to comment.