-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkfiles.py
602 lines (488 loc) · 23.7 KB
/
checkfiles.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# checkfiles.py detect and fix tabs and/or trailing whitespace in commits
#
# Copyright: Marcus Lindblom 2010
# License: GPLv2+
""" Detects (and optionally fixes) tabs and trailing whitespace in committed
files
== The hooks ==
check_hook: prevents commits containing tabs or trailing whitespace, without
actually touching any files. Use as pretxncommit locally or
pretxnchangegroup on a central repo.
fixup_hook: automatically fixes any problematic files before commit. Useful in
pre-commit. (As this hook may change files on disc, you will need
to recompile your project after committing if any files needed
fixing.)
If you're not comfortable with this kind of magic, use the
check_hook and manually run the command with the --fixup option.
== The commands ==
hg checkfiles [options]
checks changed files in the working directory for tabs or trailing whitespace
- --verbose shows the location of offending characters in each line
- --quiet hides filenames and only reports summary information
- --debug shows settings and details about each file considered for
checking
If problems are found, the command returns 1, otherwise 0.
hg fixwhitespace [options]
Replaces tabs -> spaces and removes trailing whitespace
If --fixup is given, the return value is always 0 (unless an error occurs).
options:
-t --tabsize VALUE set the tab length (default: 4)
--all fix all files in working directory, not just the changes
ones
--diff fix only modified lines!
== Example usage ==
[extensions]
checkfiles = /path/to/checkfiles.py enable command
[hooks]
pretxnchangegroup.checkfiles = python:/path/to/checkfiles.py:check_hook
pretxncommit.checkfiles = python:/path/to/checkfiles.py:check_hook
precommit.checkfiles = python:/path/to/checkfiles.py:fixup_hook
[checkfiles]
# default is all text files
checked_exts = .c .h .cpp .xml .cs .html .js .css .txt .py .nsi .java
# default is .sln .suo .vcproj .csproj .ui
ignored_exts = .xslt
ignored_files = foo/contains_tabs.txt bar/contains_trailing_ws.txt
# ignore everything in the /res directory, including those files in
# subdirectories.
ignored_patterns = res/.*
tab_size = 4
# For check_hook to only check for tabs (and ignore trailing whitespace) set
# to True. Default, if not present, is False.
check_ignores_trailing_ws = False
# to examine only modified lines from check_hook, use:
# check_diffs = True
# to fix only modified lines from fixup_hook, use:
# fixup_diff = True
# to replace spaces with tabs (instead of tabs -> spaces), set use_spaces
# to False.
use_spaces = True
"""
import re
from mercurial.i18n import _
from mercurial import patch, util
from mercurial.scmutil import match, revrange
hunk_re = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
class CheckFiles(object):
def __init__(self, ui, repo, ctx, opts = {}):
self.ui = ui
self.repo = repo
self.checked_exts = ui.configlist('checkfiles', 'checked_exts',
default='""')
self.ignored_exts = ui.configlist('checkfiles', 'ignored_exts',
# some common autogenerated filetypes
default='.sln .suo .vcproj .csproj .ui')
self.ignored_files = ui.configlist('checkfiles', 'ignored_files')
self.ignored_patterns = ui.configlist('checkfiles', 'ignored_patterns')
self.check_diffs = ui.configbool('checkfiles', 'check_diffs')
self.fixup_diffs = ui.configbool('checkfiles', 'fixup_diffs')
self.tab_size = int(ui.config('checkfiles', 'tab_size', default='4'))
self.use_spaces = ui.configbool('checkfiles', 'use_spaces', True)
self.check_ignores_trailing_ws = ui.configbool('checkfiles',
'check_ignores_trailing_ws', False)
if 'tabsize' in opts:
self.tab_size = int(opts['tabsize'])
if opts.get('all', False) and opts.get('diff', False):
raise util.Abort("--all and --diff options are mutually exclusive")
self.check_diffs = opts.get('diff', self.check_diffs)
self.fixup_diffs = opts.get('diff', self.fixup_diffs)
self.opt_all = opts.get('all', False)
if self.opt_all:
self.check_diffs = False
self.fixup_diffs = False
if self.checked_exts == '""':
self.ui.debug('checkfiles: checked extensions: (all text files)\n')
else:
self.ui.debug('checkfiles: checked extensions: %s\n' % ' '.join(
self.checked_exts))
self.ui.debug('checkfiles: ignored extensions: %s\n' % ' '.join(
self.ignored_exts))
self.ui.debug('checkfiles: ignored files: %s\n' % ' '.join(
self.ignored_files))
self.ui.debug('checkfiles: ignored patterns: %s\n' % ' '.join(
self.ignored_patterns))
self.ui.debug('checkfiles: check diffs only: %r\n' % self.check_diffs)
self.ui.debug('checkfiles: use spaces: %r\n' % self.use_spaces)
self.ui.debug('checkfiles: tab size: %r\n' % self.tab_size)
self.ui.debug('checkfiles: ignore trailing ws: %r\n' %
self.check_ignores_trailing_ws)
if ctx:
self.set_changectx(ctx)
def set_changectx(self, ctx):
self.ctx = ctx
if self.opt_all:
modified, added, removed, deleted, unknown, ignored, clean = \
self.repo.status(clean=True)
# We can't get filecontext for unknown files
self.files = modified + added + clean
else:
self.files = ctx.files() if ctx else []
def is_relevant(self, file):
if file in self.ignored_files:
self.ui.debug('checkfiles: ignoring %s (explicit ignore)\n' % file)
return False
if any(map(lambda e: file.endswith(e), self.ignored_exts)):
self.ui.debug('checkfiles: ignoring %s (ignored extension)\n'
% file)
return False
if not any(map(lambda e: file.endswith(e), self.checked_exts)):
self.ui.debug('checkfiles: ignoring %s (non-checked extension)\n'
% file)
return False
if any(map(lambda e: re.search(e, file) is not None,
self.ignored_patterns)):
self.ui.debug('checkfiles: ignoring %s (ignored pattern)\n' % file)
return False
try:
fctx = self.ctx[file]
except LookupError:
self.ui.debug('checkfiles: skipping %s (deleted)\n' % file)
return False
if fctx == None:
self.ui.debug('checkfiles: skipping %s (deleted)\n' % file)
return False
try:
fctx.data()
except:
self.ui.debug('checkfiles: skipping %s (deleted)\n' % file)
return False
if '\0' in fctx.data():
self.ui.debug('checkfiles: skipping %s (binary)\n' % file)
return False
return True
def check(self):
self.ui.debug('checkfiles: considering files:\n %s\n'
% '\n '.join(self.files))
if self.use_spaces:
indicator = '^' * self.tab_size
else:
indicator = '^'
class State:
def __init__(self, ui):
self.ui = ui
self.ws_begin = False
self.ws_end = False
self.all_ws = False
self.filecount = 0
self.probcount = 0
def endfile(self, file):
if file is None:
return
if self.ws_begin or self.ws_end or self.all_ws:
self.filecount += 1
self.ui.status('checkfiles: %s:%s%s%s\n' %
(file, ' whitespace_begin' if self.ws_begin else '',
' whitespace_end' if self.ws_end else '',
' all_whitespace' if self.all_ws else ''))
self.ws_begin = False
self.ws_end = False
self.all_ws = False
else:
self.ui.note('checkfiles: %s: ok\n' % file)
def found_all_ws(self):
self.all_ws = True
self.probcount += 1
def found_ws_end(self):
self.ws_end = True
self.probcount += 1
def found_ws_begin(self):
self.ws_begin = True
self.probcount += 1
state = State(self.ui)
if self.check_diffs:
if len(self.ctx.parents()) == 1:
ctx = self.repo[None]
file = None
hunk = None
lastlabel = None
for chunk, label in patch.diffui(self.repo,
self.ctx.p1().node(),
self.ctx.node(),
match(ctx)):
if len(label) > 0 or chunk != '\n':
self.ui.debug('checkfiles: %s="%s"\n' % (label, chunk))
if label == 'diff.file_b':
state.endfile(file)
file = re.sub(r'^[+][+][+] b/(.+)\t.+$', r'\1', chunk)
if self.is_relevant(file):
self.ui.debug('checkfiles: checking %s ...\n'
% file)
else:
file = None
elif label == 'diff.hunk':
hunk = chunk
elif file and label == 'diff.trailingwhitespace' and \
lastlabel == 'diff.inserted' and chunk != '\r':
if self.check_ignores_trailing_ws:
# still check for presence of \t tough
if self.use_spaces and ('\t' in chunk):
state.found_ws_end()
self.ui.note('%s: trailing tab in %s\n'
% (file, hunk))
else:
state.found_ws_end()
self.ui.note('%s: trailing whitespace in %s\n'
% (file, hunk))
elif file and label == 'diff.inserted' and \
self.is_ws_before_text(chunk[1:]):
state.found_ws_begin()
if self.use_spaces:
self.ui.note('%s: tab character(s) in %s\n'
% (file, hunk))
else:
self.ui.note('%s: space(s) before text in %s\n'
% (file, hunk))
lastlabel = label
state.endfile(file)
else:
self.ui.note('checkfiles: skipping merge changeset\n')
else:
for file in filter(self.is_relevant, self.files):
self.ui.debug('checkfiles: checking %s ...\n' % file)
fctx = self.ctx[file]
for num, line in enumerate(fctx.data().splitlines(), 1):
if line.isspace():
if self.check_ignores_trailing_ws:
# still check for presence of \t
if self.use_spaces and ('\t' in line):
state.found_all_ws()
self.ui.note('%s (%i): tabs in all ' \
'whitespace line\n' % (file, num))
else:
state.found_all_ws()
self.ui.note('%s (%i): all whitespace\n'
% (file, num))
elif line.endswith((' ', '\t')):
if self.check_ignores_trailing_ws:
# still check for presence of \t
if self.use_spaces and ('\t' in line):
state.found_ws_end()
self.ui.note('%s (%i): trailing tabs\n'
% (file, num))
else:
state.found_ws_end()
self.ui.note('%s (%i): trailing whitespace\n'
% (file, num))
line = line.expandtabs(self.tab_size)
non_ws_len = len(line.rstrip())
line_show = ' ' * non_ws_len + \
'^' * (len(line) - non_ws_len)
self.ui.note(' %s\n %s\n' % (line, line_show))
self.detect_ws_before_text(file, num, line, indicator,
state)
state.endfile(file)
if state.filecount > 0:
from mercurial.node import short
self.ui.warn('checkfiles: %i issues(s) found in %i file(s) in %s\n'
% (state.probcount, state.filecount,
short(self.ctx.node()) if self.ctx.node() \
else 'working directory'))
return state.filecount > 0
def fixup_line_num_in_file(self, file, line_num):
import os.path
data = self.ctx[file].data()
lines = data.splitlines()
nl_at_eof = data.endswith('\n')
self.ui.status('checkfiles: fixing in %s:%d\n' % (file, line_num))
with open(os.path.join(self.repo.root, file), 'w') as fileobj:
def fixline():
if self.use_spaces:
for num, line in enumerate(lines, 1):
if num == line_num:
yield line.rstrip().expandtabs(self.tab_size)
else:
yield line
else:
for num, line in enumerate(lines, 1):
if num == line_num:
match = re.match(r'^(\t*( \t*)+)[^ \t]', line)
if match:
ws = match.group(1)
text = line[len(ws):]
yield ws.expandtabs(self.tab_size).replace(
' ' * self.tab_size, '\t') + text.rstrip()
else:
yield line.rstrip()
else:
yield line
fileobj.writelines('\n'.join(fixline()))
if nl_at_eof:
fileobj.write('\n')
def fixup(self):
import os.path
if self.fixup_diffs:
if len(self.ctx.parents()) == 1:
ctx = self.repo[None]
file = None
hunk = None
lastlabel = None
line_num = None
for chunk, label in patch.diffui(self.repo,
self.ctx.p1().node(),
self.ctx.node(),
match(ctx)):
if (len(label) == 0 and chunk != '\n') or \
label == 'diff.inserted':
line_num += 1
if len(label) > 0 or chunk != '\n':
self.ui.debug('checkfiles: %s="%s"\n' % (label, chunk))
if label == 'diff.file_b':
file = re.sub(r'^[+][+][+] b/(.+)\t.+$', r'\1', chunk)
if self.is_relevant(file):
self.ui.debug('checkfiles: checking %s ...\n'
% file)
else:
file = None
elif label == 'diff.hunk':
hunk = chunk
m = re.match(hunk_re, hunk)
(start_a, len_a, start_b, len_b) = m.groups()
self.ui.debug('checkfiles: parsed: %s, %s, %s, %s\n'
% m.groups())
line_num = int(start_b) - 1
elif file and label == 'diff.trailingwhitespace' and \
lastlabel == 'diff.inserted' and chunk != '\r':
self.ui.note('%s:%d: trailing whitespace in %s\n'
% (file, line_num, hunk))
self.fixup_line_num_in_file(file, line_num)
elif file and label == 'diff.inserted' and \
self.is_ws_before_text(chunk[1:]):
if self.use_spaces:
self.ui.note('%s:%d: tab character(s) in %s\n'
% (file, line_num, hunk))
else:
self.ui.note('%s:%d: space(s) before text in %s\n'
% (file, line_num, hunk))
self.fixup_line_num_in_file(file, line_num)
lastlabel = label
else:
self.ui.note('checkfiles: skipping merge changeset\n')
else:
for file in filter(self.is_relevant, self.files):
data = self.ctx[file].data()
lines = data.splitlines()
nl_at_eof = data.endswith('\n')
if not any(line.isspace() or self.is_ws_before_text(line) or \
line.endswith((' ', '\t')) for line in lines):
self.ui.note('checkfiles: %s ok\n' % file)
continue
self.ui.status('checkfiles: fixing %s\n' % file)
with open(os.path.join(self.repo.root, file), 'w') as fileobj:
def fixline():
if self.use_spaces:
for line in lines:
yield line.rstrip().expandtabs(self.tab_size)
else:
for line in lines:
match = re.match(r'^(\t*( \t*)+)[^ \t]', line)
if match:
ws = match.group(1)
text = line[len(ws):]
yield ws.expandtabs(self.tab_size).replace(
' ' * self.tab_size, '\t') + text.rstrip()
else:
yield line.rstrip()
fileobj.writelines('\n'.join(fixline()))
if nl_at_eof:
fileobj.write('\n')
def match_spaces_before_text(self, line):
return re.match(r'^\t*( \t*)+[^ \t]', line)
def is_ws_before_text(self, line):
if self.use_spaces:
return '\t' in line
else:
return self.match_spaces_before_text(line)
def detect_ws_before_text(self, file, num, line, indicator, state):
if self.use_spaces:
if self.is_ws_before_text(line):
state.found_ws_begin()
self.ui.note('%s (%i): tab character(s)\n' % (file, num))
line_show = ''.join(indicator if c == '\t' else \
' ' for c in line)
line = line.expandtabs(self.tab_size)
self.ui.note(' %s\n %s\n' % (line, line_show))
else:
match = self.match_spaces_before_text(line)
if match:
state.found_ws_begin()
self.ui.note('%s (%i): space(s) before text\n' % (file, num))
line = line.expandtabs(self.tab_size)
line_show = match.group()[0:-1].replace(' ',
indicator).expandtabs(self.tab_size)
self.ui.note(' %s\n %s\n' % (line, line_show))
###############################################################################
def check_hook(ui, repo, hooktype, **kwargs):
"""blocks commits/changesets containing tabs or trailing whitespace"""
if hooktype == 'precommit':
ui.note('checkfiles: checking for tabs and/or trailing whitespace ' \
'in changed files...\n')
cf = CheckFiles(ui, repo, repo[None])
return cf.check()
if hooktype == 'pretxncommit':
ui.note('checkfiles: checking commit for tabs or trailing ' \
'whitespace...\n')
node = kwargs['node']
cf = CheckFiles(ui, repo, repo.changectx(node))
return cf.check()
elif hooktype == 'pretxnchangegroup':
ui.note('checkfiles: checking incoming changes for tabs or trailing ' \
'whitespace...\n')
cf = CheckFiles(ui, repo, repo[None])
fail = False
node = kwargs['node']
for rev in revrange(repo, ['%s::' % node]):
cf.set_changectx(repo.changectx(rev))
cf.files = cf.ctx.files()
fail = cf.check() or fail
return fail
else:
raise util.Abort(_('checkfiles: check_hook installed as unsupported ' \
'hooktype: %s') % hooktype)
def fixup_hook(ui, repo, hooktype, **kwargs):
""" Removes tabs and/or trailing whitespace from modified files in the
working directory
"""
ui.note('checkfiles: removing tabs and/or trailing whitespace in ' \
'changed files...\n')
cf = CheckFiles(ui, repo, repo[None])
cf.fixup()
return False
def check_cmd(ui, repo, **opts):
""" Checks changed files in the working directory for tabs or trailing
whitespace
- --verbose shows the location of offending characters in each line
- --quiet hides filenames and only reports summary information
- --debug shows settings and details about each file considered for
checking
If problems are found, the command returns 1, otherwise 0.
"""
ui.note('checkfiles: checking %s files for tabs or trailing ' \
'whitespace...\n' % ('all' if opts['all'] else 'modified'))
cf = CheckFiles(ui, repo, repo[None], opts)
return cf.check()
def fixup_cmd(ui, repo, **opts):
""" Replaces tabs with spaces and removes trailing whitespace from changed
files
"""
ui.note('checkfiles: removing tabs and/or trailing whitespace in %s ' \
'files...\n' % ('all' if opts['all'] else 'modified'))
cf = CheckFiles(ui, repo, repo[None], opts)
cf.fixup()
return 0
###############################################################################
cmdtable = {
'checkfiles': (check_cmd,
[('t', 'tabsize', 4, 'set the tab length'),
('', 'all', None, 'check all tracked files (not just ' \
'changed)'),
('', 'diff', None, 'check only diff lines (not entire ' \
'file)')],
'hg checkfiles [options]'),
'fixwhitespace': (fixup_cmd,
[('t', 'tabsize', 4, 'set the tab length'),
('', 'all', None, 'fix all tracked files (not just ' \
'changed)'),
('', 'diff', None, 'fix only diff lines (not entire ' \
'file)')],
'hg fixwhitespace [options]'),
}