style.py (5309:4d3a6e086488) | style.py (5465:4cff095bbf2b) |
---|---|
1#! /usr/bin/env python | 1#! /usr/bin/env python |
2# Copyright (c) 2007 The Regents of The University of Michigan | 2# Copyright (c) 2006 The Regents of The University of Michigan 3# Copyright (c) 2007 The Hewlett-Packard Development Company |
3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer; 9# redistributions in binary form must reproduce the above copyright 10# notice, this list of conditions and the following disclaimer in the --- 244 unchanged lines hidden (view full) --- 255 if i < fbeg: 256 modified.add(i) 257 elif i + 1 >= fend: 258 break 259 elif i > max_lines: 260 break 261 return modified 262 | 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer; 10# redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the --- 244 unchanged lines hidden (view full) --- 256 if i < fbeg: 257 modified.add(i) 258 elif i + 1 >= fend: 259 break 260 elif i > max_lines: 261 break 262 return modified 263 |
263def check_whitespace(ui, repo, hooktype, node, parent1, parent2): 264 from mercurial import mdiff | 264def do_check_whitespace(ui, repo, *files, **args): 265 """check files for proper m5 style guidelines""" 266 from mercurial import mdiff, util |
265 | 267 |
266 if hooktype != 'pretxncommit': 267 raise AttributeError, \ 268 "This hook is only meant for pretxncommit, not %s" % hooktype | 268 if files: 269 files = frozenset(files) |
269 | 270 |
270 tabsize = 8 271 verbose = ui.configbool('style', 'verbose', False) | 271 def skip(name): 272 return files and name in files 273 |
272 def prompt(name, fixonly=None): | 274 def prompt(name, fixonly=None): |
273 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", "^[aif]$", "a") | 275 if args.get('auto', False): 276 result = 'f' 277 else: 278 result = ui.prompt("(a)bort, (i)gnore, or (f)ix?", "^[aif]$", "a") |
274 if result == 'a': 275 return True 276 elif result == 'i': 277 pass 278 elif result == 'f': | 279 if result == 'a': 280 return True 281 elif result == 'i': 282 pass 283 elif result == 'f': |
279 fixwhite(repo.wjoin(name), tabsize, fixonly) | 284 fixwhite(repo.wjoin(name), args['tabsize'], fixonly) |
280 else: | 285 else: |
281 raise RepoError, "Invalid response: '%s'" % result | 286 raise util.Abort(_("Invalid response: '%s'") % result) |
282 283 return False 284 285 modified, added, removed, deleted, unknown, ignore, clean = repo.status() 286 287 for fname in added: | 287 288 return False 289 290 modified, added, removed, deleted, unknown, ignore, clean = repo.status() 291 292 for fname in added: |
293 if skip(fname): 294 continue 295 |
|
288 ok = True 289 for line,num in checkwhite(repo.wjoin(fname)): 290 ui.write("invalid whitespace in %s:%d\n" % (fname, num)) | 296 ok = True 297 for line,num in checkwhite(repo.wjoin(fname)): 298 ui.write("invalid whitespace in %s:%d\n" % (fname, num)) |
291 if verbose: | 299 if ui.verbose: |
292 ui.write(">>%s<<\n" % line[-1]) 293 ok = False 294 295 if not ok: 296 if prompt(fname): 297 return True 298 299 wctx = repo.workingctx() 300 for fname in modified: | 300 ui.write(">>%s<<\n" % line[-1]) 301 ok = False 302 303 if not ok: 304 if prompt(fname): 305 return True 306 307 wctx = repo.workingctx() 308 for fname in modified: |
309 if skip(fname): 310 continue 311 |
|
301 if not whitespace_file(fname): 302 continue 303 304 fctx = wctx.filectx(fname) 305 pctx = fctx.parents() 306 assert len(pctx) in (1, 2) 307 308 file_data = fctx.data() --- 7 unchanged lines hidden (view full) --- 316 for i,line in enumerate(lines): 317 if i not in mod_lines: 318 continue 319 320 if checkwhite_line(line): 321 continue 322 323 ui.write("invalid whitespace: %s:%d\n" % (fname, i+1)) | 312 if not whitespace_file(fname): 313 continue 314 315 fctx = wctx.filectx(fname) 316 pctx = fctx.parents() 317 assert len(pctx) in (1, 2) 318 319 file_data = fctx.data() --- 7 unchanged lines hidden (view full) --- 327 for i,line in enumerate(lines): 328 if i not in mod_lines: 329 continue 330 331 if checkwhite_line(line): 332 continue 333 334 ui.write("invalid whitespace: %s:%d\n" % (fname, i+1)) |
324 if verbose: | 335 if ui.verbose: |
325 ui.write(">>%s<<\n" % line[:-1]) 326 fixonly.add(i) 327 328 if fixonly: 329 if prompt(fname, fixonly): 330 return True 331 | 336 ui.write(">>%s<<\n" % line[:-1]) 337 fixonly.add(i) 338 339 if fixonly: 340 if prompt(fname, fixonly): 341 return True 342 |
343def check_whitespace(ui, repo, hooktype, node, parent1, parent2): 344 if hooktype != 'pretxncommit': 345 raise AttributeError, \ 346 "This hook is only meant for pretxncommit, not %s" % hooktype 347 348 args = { 'tabsize' : 8 } 349 do_check_whitespace(ui, repo, **args) 350 |
|
332def check_format(ui, repo, hooktype, node, parent1, parent2): 333 if hooktype != 'pretxncommit': 334 raise AttributeError, \ 335 "This hook is only meant for pretxncommit, not %s" % hooktype 336 337 modified, added, removed, deleted, unknown, ignore, clean = repo.status() 338 339 verbose = 0 --- 5 unchanged lines hidden (view full) --- 345 stats.dump() 346 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?", 347 "^[ia]$", "a") 348 if result.startswith('i'): 349 pass 350 elif result.startswith('a'): 351 return True 352 else: | 351def check_format(ui, repo, hooktype, node, parent1, parent2): 352 if hooktype != 'pretxncommit': 353 raise AttributeError, \ 354 "This hook is only meant for pretxncommit, not %s" % hooktype 355 356 modified, added, removed, deleted, unknown, ignore, clean = repo.status() 357 358 verbose = 0 --- 5 unchanged lines hidden (view full) --- 364 stats.dump() 365 result = ui.prompt("invalid formatting\n(i)gnore or (a)bort?", 366 "^[ia]$", "a") 367 if result.startswith('i'): 368 pass 369 elif result.startswith('a'): 370 return True 371 else: |
353 raise RepoError, "Invalid response: '%s'" % result | 372 raise util.Abort(_("Invalid response: '%s'") % result) |
354 355 return False 356 | 373 374 return False 375 |
376try: 377 from mercurial.i18n import _ 378except ImportError: 379 def _(arg): 380 return arg 381 382cmdtable = { 383 '^m5style' : 384 ( do_check_whitespace, 385 [ ('a', 'auto', False, _("automatically fix whitespace")), 386 ('t', 'tabsize', 8, _("Number of spaces TAB indents")) ], 387 _('hg m5check [-t <tabsize>] [FILE]...')), 388} |
|
357if __name__ == '__main__': 358 import getopt 359 360 progname = sys.argv[0] 361 if len(sys.argv) < 2: 362 sys.exit('usage: %s <command> [<command args>]' % progname) 363 364 fixwhite_usage = '%s fixwhite [-t <tabsize> ] <path> [...] \n' % progname --- 47 unchanged lines hidden --- | 389if __name__ == '__main__': 390 import getopt 391 392 progname = sys.argv[0] 393 if len(sys.argv) < 2: 394 sys.exit('usage: %s <command> [<command args>]' % progname) 395 396 fixwhite_usage = '%s fixwhite [-t <tabsize> ] <path> [...] \n' % progname --- 47 unchanged lines hidden --- |