ml-hgext / 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
# 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 changes 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

== 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 .aspx .asp .bat .cmd .glsl

# default is .sln .suo .vcproj .csproj .ui
ignored_exts = .xslt
ignored_files = foo/contains_tabs.txt bar/contains_trailing_ws.txt
tab_size = 4

# to examine only modified lines from check_hook (no effect on fixup_hook or command), use:
# check_diffs = True

# to replace spaces with tabs (instead of tabs -> spaces), set use_spaces to False
use_spaces = True

'''

from mercurial.i18n import _
from mercurial import cmdutil, patch
import re

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',
            default='.sln .suo .vcproj .csproj .ui') # some common autogenerated filetypes
        self.ignored_files = ui.configlist('checkfiles', 'ignored_files')
        self.check_diffs = ui.configbool('checkfiles', 'check_diffs')
        self.tab_size = int(ui.config('checkfiles', 'tab_size', default='4'))
        self.use_spaces = ui.configbool('checkfiles', 'use_spaces', True)

        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.opt_all = opts.get('all', False)

        if self.opt_all:
            self.check_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: check diffs only: %r\n' % self.check_diffs)
        self.ui.debug('checkfiles: use spaces: %r\n' % self.use_spaces)

        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)
            self.files = modified + added + clean # we can't get filecontext for unknown files
        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

        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:
            data = 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:
                # XXX would be nicer if checked_exts were a proper pattern;
                # then cmdutil.match would work naturally with it
                try:
                    from mercurial.scmutil import match
                    ctx = self.repo[None]
                except ImportError:
                    from mercurial.cmdutil import match
                    ctx = self.repo

                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':
                        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():
                        state.found_all_ws()
                        self.ui.note('%s (%i): all whitespace\n' % (file, num))

                    elif line.endswith((' ', '\t')):
                        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(self):
        import os.path

        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, node, **kwargs):
    '''blocks commits/changesets containing tabs or trailing whitespace'''

    if hooktype == 'pretxncommit':
        ui.note('checkfiles: checking commit for tabs or trailing whitespace...\n')
        cf = CheckFiles(ui, repo, repo.changectx(node))
        return cf.check()

    elif hooktype == 'pretxnchangegroup':
        try:
            from mercurial.scmutil import revrange
        except ImportError:
            # 1.8 and earlier
            from mercurial.cmdutil import revrange

        ui.note('checkfiles: checking incoming changes for tabs or trailing whitespace...\n')
        cf = CheckFiles(ui, repo, repo[None])
        fail = False

        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:
        from mercurial import util
        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)')],
                      'hg fixwhitespace [options]')
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.