@@ -29,6 +29,23 @@ def run_cmd(cmd, show_cmd=False):
2929 return res
3030
3131
32+ def try_find_clang_format (exec_path ):
33+ candidates = ['clang-format-15' ]
34+ if exec_path is not None :
35+ candidates .insert (0 , exec_path )
36+ for c in candidates :
37+ if which (c ) is not None :
38+ return c
39+ return candidates [- 1 ]
40+
41+
42+ def get_clang_format_version (clang_format_cmd ):
43+ proc = subprocess .run ([clang_format_cmd , '--version' ],
44+ stdout = subprocess .PIPE , check = True )
45+ clang_format_version = proc .stdout .decode ().strip ()
46+ return clang_format_version
47+
48+
3249def main ():
3350 default_suffix = ['.cpp' , '.h' , '.cc' , '.hpp' ]
3451 parser = argparse .ArgumentParser (description = 'TiFlash Code Format' ,
@@ -45,6 +62,8 @@ def main():
4562 help = 'exit -1 if NOT formatted' , action = 'store_true' )
4663 parser .add_argument ('--dump_diff_files_to' ,
4764 help = 'dump diff file names to specific path' , default = None )
65+ parser .add_argument ('--clang_format' ,
66+ help = 'path to clang-format' , default = None )
4867
4968 args = parser .parse_args ()
5069 default_suffix = args .suffix .strip ().split (' ' ) if args .suffix else []
@@ -82,10 +101,9 @@ def main():
82101 len (da ), args .dump_diff_files_to ))
83102
84103 if files_to_format :
104+ clang_format_cmd = try_find_clang_format (args .clang_format )
105+ print ('Using {}' .format (get_clang_format_version (clang_format_cmd )))
85106 print ('Files to format:\n {}' .format ('\n ' .join (files_to_format )))
86- clang_format_cmd = 'clang-format-13'
87- if which (clang_format_cmd ) is None :
88- clang_format_cmd = 'clang-format'
89107 for file in files_to_format :
90108 cmd = clang_format_cmd + ' -i {}' .format (file )
91109 if subprocess .Popen (cmd , shell = True , cwd = tiflash_repo_path ).wait ():
0 commit comments