MCPcopy Create free account
hub / github.com/ElementsProject/elements / main

Function main

contrib/devtools/clang-format-diff.py:82–163  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

80
81
82def main():
83 parser = argparse.ArgumentParser(description=
84 'Reformat changed lines in diff. Without -i '
85 'option just output the diff that would be '
86 'introduced.')
87 parser.add_argument('-i', action='store_true', default=False,
88 help='apply edits to files instead of displaying a diff')
89 parser.add_argument('-p', metavar='NUM', default=0,
90 help='strip the smallest prefix containing P slashes')
91 parser.add_argument('-regex', metavar='PATTERN', default=None,
92 help='custom pattern selecting file paths to reformat '
93 '(case sensitive, overrides -iregex)')
94 parser.add_argument('-iregex', metavar='PATTERN', default=
95 r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|ts|proto'
96 r'|protodevel|java)',
97 help='custom pattern selecting file paths to reformat '
98 '(case insensitive, overridden by -regex)')
99 parser.add_argument('-sort-includes', action='store_true', default=False,
100 help='let clang-format sort include blocks')
101 parser.add_argument('-v', '--verbose', action='store_true',
102 help='be more verbose, ineffective without -i')
103 args = parser.parse_args()
104
105 # Extract changed lines for each file.
106 filename = None
107 lines_by_file = {}
108 for line in sys.stdin:
109 match = re.search(r'^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
110 if match:
111 filename = match.group(2)
112 if filename is None:
113 continue
114
115 if args.regex is not None:
116 if not re.match('^%s$' % args.regex, filename):
117 continue
118 else:
119 if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
120 continue
121
122 match = re.search(r'^@@.*\+(\d+)(,(\d+))?', line)
123 if match:
124 start_line = int(match.group(1))
125 line_count = 1
126 if match.group(3):
127 line_count = int(match.group(3))
128 if line_count == 0:
129 continue
130 end_line = start_line + line_count - 1
131 lines_by_file.setdefault(filename, []).extend(
132 ['-lines', str(start_line) + ':' + str(end_line)])
133
134 # Reformat files containing changes in place.
135 for filename, lines in lines_by_file.items():
136 if args.i and args.verbose:
137 print('Formatting {}'.format(filename))
138 command = [binary, filename]
139 if args.i:

Callers 1

Calls 5

parse_argsMethod · 0.80
formatMethod · 0.80
itemsMethod · 0.45
joinMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected