@@ -29,6 +29,22 @@ def run_cmd(cmd, show_cmd=False):
2929 return res
3030
3131
32+ def try_find_clang_format (exec_path ):
33+ candidates = ['clang-format-13' ]
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' ], capture_output = True , text = True , check = True )
44+ clang_format_version = proc .stdout .strip ()
45+ return clang_format_version
46+
47+
3248def main ():
3349 default_suffix = ['.cpp' , '.h' , '.cc' , '.hpp' ]
3450 parser = argparse .ArgumentParser (description = 'TiFlash Code Format' ,
@@ -45,6 +61,8 @@ def main():
4561 help = 'exit -1 if NOT formatted' , action = 'store_true' )
4662 parser .add_argument ('--dump_diff_files_to' ,
4763 help = 'dump diff file names to specific path' , default = None )
64+ parser .add_argument ('--clang_format' ,
65+ help = 'path to clang-format' , default = None )
4866
4967 args = parser .parse_args ()
5068 default_suffix = args .suffix .strip ().split (' ' ) if args .suffix else []
@@ -82,10 +100,10 @@ def main():
82100 len (da ), args .dump_diff_files_to ))
83101
84102 if files_to_format :
103+ clang_format_cmd = try_find_clang_format (args .clang_format )
104+ clang_format_version = get_clang_format_version (clang_format_cmd )
105+ print ('Using {}' .format (clang_format_version ))
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