This repository was archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhgjsonhook.py
More file actions
67 lines (51 loc) · 2.01 KB
/
Copy pathhgjsonhook.py
File metadata and controls
67 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
import os
import traceback
try:
import simplejson as json
except ImportError:
import json
check_extension = 'json'
trailing_comma = re.compile('(,)(\s*[\]|}])')
# colors
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def run(ui, repo, hooktype, **kwargs):
if hooktype not in ['precommit']:
ui.write('Hook should be "precommit" not "%s".\n' % hooktype)
return True
files = filter(lambda x: x.endswith('.%s' % check_extension), repo[None].files())
for f in files:
data = repo[None][f].data()
# Check for trailing commas
matches = trailing_comma.finditer(data)
fix_stack = []
for match in matches:
line_num = data.count('\n', 0, match.start(1)) + 1
# catches not found case accidentally, because -1 turns into 0
line_start = data.rfind('\n', 0, match.start(1)) + 1
line_end = max(data.index('\n', match.end(2)), match.end(2))
ui.write('Trailing comma in ' + GREEN + f + ENDC + \
' at line ' + GREEN + str(line_num) + ENDC + \
' char ' + GREEN + str(match.start(1) - line_start + 1) + \
ENDC + ':\n')
ui.write(BLUE + data[line_start:match.start(1)] + \
FAIL + ',' + BLUE + data[match.end(1):line_end] + ENDC + '\n')
if ui.promptchoice('Want me to fix it? [Yn]: $$ &no $$ &yes', default = 1):
fix_stack.append(match)
# Fix in reverse order so that indexes are correct.
while fix_stack:
m = fix_stack.pop()
data = data[:m.start(1)] + data[m.end(1):]
with open(os.path.join(repo.root, f), 'wb') as fileobj:
fileobj.write(data)
try:
json_object = json.loads(data)
except Exception, e:
ui.write('JSON parsing of ' + GREEN + f + FAIL + ' FAILED:' + ENDC + '\n')
ui.write(e.message + '\n')
return True
return False