Mercurial > public > mercurial-scm > hg-stable
comparison contrib/check-code.py @ 41830:6d6bd9039ecd
contrib: add line offset information to file check function of check-code.py
This is a part of preparation to apply checking with check-code.py on
code fragments embedded in *.t test scripts.
This information will be useful to show correct line number in an
actual file for errors detected in code fragments embedded in *.t test
scripts.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Fri, 01 Mar 2019 02:53:09 +0900 |
parents | 519b2faea261 |
children | 867883d454ea |
comparison
equal
deleted
inserted
replaced
41829:519b2faea261 | 41830:6d6bd9039ecd |
---|---|
675 result = False | 675 result = False |
676 | 676 |
677 return result | 677 return result |
678 | 678 |
679 def _checkfiledata(name, f, filedata, filters, pats, context, | 679 def _checkfiledata(name, f, filedata, filters, pats, context, |
680 logfunc, maxerr, warnings, blame, debug, lineno): | 680 logfunc, maxerr, warnings, blame, debug, lineno, |
681 offset=None): | |
681 """Execute actual error check for file data | 682 """Execute actual error check for file data |
682 | 683 |
683 :name: of the checking category | 684 :name: of the checking category |
684 :f: filepath | 685 :f: filepath |
685 :filedata: content of a file | 686 :filedata: content of a file |
693 report all errors | 694 report all errors |
694 :warnings: whether warning level checks should be applied | 695 :warnings: whether warning level checks should be applied |
695 :blame: whether blame information should be displayed at error reporting | 696 :blame: whether blame information should be displayed at error reporting |
696 :debug: whether debug information should be displayed | 697 :debug: whether debug information should be displayed |
697 :lineno: whether lineno should be displayed at error reporting | 698 :lineno: whether lineno should be displayed at error reporting |
699 :offset: line number offset of 'filedata' in 'f' for checking | |
700 an embedded code fragment, or None (offset=0 is different | |
701 from offset=None) | |
698 | 702 |
699 returns number of detected errors. | 703 returns number of detected errors. |
700 """ | 704 """ |
701 blamecache = context['blamecache'] | 705 blamecache = context['blamecache'] |
706 if offset is None: | |
707 lineoffset = 0 | |
708 else: | |
709 lineoffset = offset | |
702 | 710 |
703 fc = 0 | 711 fc = 0 |
704 pre = post = filedata | 712 pre = post = filedata |
705 | 713 |
706 if True: # TODO: get rid of this redundant 'if' block | 714 if True: # TODO: get rid of this redundant 'if' block |
744 l = prelines[n] | 752 l = prelines[n] |
745 | 753 |
746 if ignore and re.search(ignore, l, re.MULTILINE): | 754 if ignore and re.search(ignore, l, re.MULTILINE): |
747 if debug: | 755 if debug: |
748 print("Skipping %s for %s:%s (ignore pattern)" % ( | 756 print("Skipping %s for %s:%s (ignore pattern)" % ( |
749 name, f, n)) | 757 name, f, (n + lineoffset))) |
750 continue | 758 continue |
751 bd = "" | 759 bd = "" |
752 if blame: | 760 if blame: |
753 bd = 'working directory' | 761 bd = 'working directory' |
754 if blamecache is None: | 762 if blamecache is None: |
755 blamecache = getblame(f) | 763 blamecache = getblame(f) |
756 context['blamecache'] = blamecache | 764 context['blamecache'] = blamecache |
757 if n < len(blamecache): | 765 if (n + lineoffset) < len(blamecache): |
758 bl, bu, br = blamecache[n] | 766 bl, bu, br = blamecache[(n + lineoffset)] |
759 if bl == l: | 767 if offset is None and bl == l: |
760 bd = '%s@%s' % (bu, br) | 768 bd = '%s@%s' % (bu, br) |
761 | 769 elif offset is not None and bl.endswith(l): |
762 errors.append((f, lineno and n + 1, l, msg, bd)) | 770 # "offset is not None" means "checking |
771 # embedded code fragment". In this case, | |
772 # "l" does not have information about the | |
773 # beginning of an *original* line in the | |
774 # file (e.g. ' > '). | |
775 # Therefore, use "str.endswith()", and | |
776 # show "maybe" for a little loose | |
777 # examination. | |
778 bd = '%s@%s, maybe' % (bu, br) | |
779 | |
780 errors.append((f, lineno and (n + lineoffset + 1), l, msg, bd)) | |
763 | 781 |
764 errors.sort() | 782 errors.sort() |
765 for e in errors: | 783 for e in errors: |
766 logfunc(*e) | 784 logfunc(*e) |
767 fc += 1 | 785 fc += 1 |