Skip to content

Commit 862cbf9

Browse files
author
Przemyslaw Kudriawcew
committed
2 parents 317e174 + 84a2b29 commit 862cbf9

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ repos:
33
rev: v1.1.0
44
hooks:
55
- id: check-static-path
6-
args: ["-k=/home/tester"]
6+
args: ['--keywords=/home/tester']
77
verbose: true

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,27 @@ check static path........................................................Failed
5454
Specified keyword: /home/vtest detected in file pre_commit_hooks/sample.py, line 1 content: kualalumpur("/home/vtest")
5555
```
5656

57+
#### Pass multiple strings to keywords parameter
58+
59+
```
60+
repos:
61+
- repo: https://github.com/Kudrixon/pre-commit_hook_static_path
62+
rev: v1.1.0
63+
hooks:
64+
- id: check-static-path
65+
args: ['--keywords', 'for', 'if', '--nodiff', 'true', '--',]
66+
verbose: true
67+
```
68+
69+
***"--"*** needs to be added at the end when using this kind of syntax while providing args
5770

5871
### Parameters
5972

6073
***--filename***:
6174
By default this pre-commit dont need You to provide filename because diff mechanism gets it by itself. When executed pre-commit mechanism gets filename parameter anyway.
6275

63-
***--keyword, -k***:
64-
[REQUIRED] Provide this to find specific string existing in incoming commit, or to find it in file specified by You
76+
***--keywords, -k***:
77+
[REQUIRED] Provide this to find specific list of strings existing in incoming commit, or to find it in file specified by You
6578

6679
***--nodiff, -nd***:
6780
If You wish to use this pre-commit to check for string in whole file not just in incoming changes you can use this flag with parameters: "false" which is default setting for using script in diff mode, "true" sets script in no-diff mode which allows user to find phrase in whole file chosen previously

pre_commit_hooks/check_static_path.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ def extractMatches(text, pattern):
1717
result.append(match.group(0))
1818
return result
1919

20-
def findStringInRangeForFile(filename, keyword, changes_range):
21-
result = []
20+
def findStringInRangeForFile(filename, keywords, changes_range):
21+
results = []
2222
with open(filename, "r") as f:
2323
lines = f.readlines()
2424
for i, line in enumerate(lines):
2525
for start, end in changes_range:
26-
if start <= i+1 <= end and keyword in line:
27-
result.append((filename, i+1, line))
28-
return result
26+
if start <= i+1 <= end and any(keyword in line for keyword in keywords):
27+
results.append((filename, i+1, line))
28+
return results
2929

30-
def findStringInRange(filenames, keyword):
31-
result = []
30+
def findStringInRange(filenames, keywords):
31+
results = []
3232
for filename in filenames:
3333
text = subprocess.run(['git', 'diff', "--unified=0", 'HEAD', filename], capture_output=True, text=True).stdout
3434
matches = extractMatches(text, pattern)
@@ -42,23 +42,24 @@ def findStringInRange(filenames, keyword):
4242
changes_range.append((start, end))
4343
else:
4444
changes_range.append((start, end - 1))
45-
result += findStringInRangeForFile(filename, keyword, changes_range)
46-
return result
45+
results += findStringInRangeForFile(filename, keywords, changes_range)
46+
return results
4747

48-
def findStringInFile(filename, keyword, retval):
48+
def findStringInFile(filename, keywords, retval):
4949
with open(filename, 'r') as f:
5050
lines = f.readlines()
5151
for line_num, line in enumerate(lines, start=1):
52-
if keyword in line:
53-
print(f"Specified keyword: {keyword} detected in file {filename}, line {line_num} content: {line}")
54-
retval = 1
52+
for keyword in keywords:
53+
if keyword in line:
54+
print(f"Specified keyword: {keyword} detected in file {filename}, line {line_num} content: {line}")
55+
retval = 1
5556
return retval
5657

5758

5859
def main(argv: Union[Sequence[str], None] = None) -> int:
5960
parser = argparse.ArgumentParser()
6061
parser.add_argument('filenames', nargs='*')
61-
parser.add_argument('-k', '--keyword', help='Static string excluded in commiting', required=True)
62+
parser.add_argument('-k', '--keywords', nargs='+', help='Static string excluded in commiting', required=True)
6263
parser.add_argument('-nd', '--nodiff', default="false", choices=['true', 'false'], help='If you need to check just specified file select true')
6364

6465
args = parser.parse_args(argv)
@@ -70,15 +71,16 @@ def main(argv: Union[Sequence[str], None] = None) -> int:
7071
matches = extractMatches(text, pattern)
7172
filenames = [match.replace("+++ b/", "") for match in matches if match.startswith("+++ b/")]
7273
filenames = [filename for filename in filenames if filename is not None]
73-
results = findStringInRange(filenames, args.keyword)
74-
for r in results:
75-
print(f"Static path of {args.keyword} detected in file {r[0]}, line {r[1]} content: {r[2]}, change to dynamic needed")
76-
retval = 1
74+
results = findStringInRange(filenames, args.keywords)
75+
for keyword in args.keywords:
76+
for r in results:
77+
print(f"Static path of '{keyword}' detected in file {r[0]}, line {r[1]} content: {r[2]}, change to dynamic needed")
78+
retval = 1
7779

7880
else:
7981
for filename in args.filenames:
8082
try:
81-
retval = findStringInFile(filename, args.keyword, retval)
83+
retval = findStringInFile(filename, args.keywords, retval)
8284

8385
except SyntaxError:
8486
impl = platform.python_implementation()

0 commit comments

Comments
 (0)