Skip to content

Commit

Permalink
port format-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
xzhangxian1008 committed Sep 19, 2022
1 parent ac4aef1 commit bd6130f
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions format-diff.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
#!/usr/bin/python3

#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import subprocess
from os import path

import json


def run_cmd(cmd, show_cmd=False):
res = os.popen(cmd).readlines()
if show_cmd:
print("RUN CMD: {}".format(cmd))
return res


def main():
default_suffix = ['.cpp', '.h', '.cc', '.hpp']
parser = argparse.ArgumentParser(description='TiFlash Code Format',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--repo_path', help='path of tics repository',
parser.add_argument('--repo_path', help='path of tiflash repository',
default=os.path.dirname(os.path.abspath(__file__)))
parser.add_argument('--suffix',
help='suffix of files to format, split by space', default=' '.join(default_suffix))
parser.add_argument('--ignore_suffix', help='ignore files with suffix, split by space')
parser.add_argument('--diff_from', help='commit hash/branch to check git diff', default='HEAD')
parser.add_argument('--check_formatted', help='exit -1 if NOT formatted', action='store_true')
parser.add_argument('--dump_diff_files_to', help='dump diff file names to specific path', default=None)

parser.add_argument('--ignore_suffix',
help='ignore files with suffix, split by space')
parser.add_argument(
'--diff_from', help='commit hash/branch to check git diff', default='HEAD')
parser.add_argument('--check_formatted',
help='exit -1 if NOT formatted', action='store_true')
parser.add_argument('--dump_diff_files_to',
help='dump diff file names to specific path', default=None)
args = parser.parse_args()
default_suffix = args.suffix.strip().split(' ') if args.suffix else []
ignore_suffix = args.ignore_suffix.strip().split(' ') if args.ignore_suffix else []
tics_repo_path = args.repo_path
if not os.path.isabs(tics_repo_path):
ignore_suffix = args.ignore_suffix.strip().split(
' ') if args.ignore_suffix else []
tiflash_repo_path = args.repo_path
if not os.path.isabs(tiflash_repo_path):
raise Exception("path of repo should be absolute")
assert tics_repo_path[-1] != '/'

os.chdir(tics_repo_path)
assert tiflash_repo_path[-1] != '/'
os.chdir(tiflash_repo_path)
files_to_check = run_cmd('git diff HEAD --name-only') if args.diff_from == 'HEAD' else run_cmd(
'git diff {} --name-only'.format(args.diff_from))
files_to_check = [os.path.join(tics_repo_path, s.strip()) for s in files_to_check]
files_to_check = [os.path.join(tiflash_repo_path, s.strip())
for s in files_to_check]
files_to_format = []
for f in files_to_check:
if not any([f.endswith(e) for e in default_suffix]):
Expand All @@ -52,33 +57,39 @@ def main():
print('file {} can not be formatted'.format(file_path))
continue
files_to_format.append(file_path)

if args.dump_diff_files_to:
da = [e[len(tics_repo_path):] for e in files_to_format]
json.dump({'files': da, 'repo': tics_repo_path}, open(args.dump_diff_files_to, 'w'))
print('dump {} modified files info to {}'.format(len(da), args.dump_diff_files_to))

da = [e[len(tiflash_repo_path):] for e in files_to_format]
json.dump({'files': da, 'repo': tiflash_repo_path},
open(args.dump_diff_files_to, 'w'))
print('dump {} modified files info to {}'.format(
len(da), args.dump_diff_files_to))
if files_to_format:
print('Files to format:\n {}'.format('\n '.join(files_to_format)))
for file in files_to_format:
cmd = 'clang-format -i {}'.format(file)
if subprocess.Popen(cmd, shell=True, cwd=tiflash_repo_path).wait():
exit(-1)

if args.check_formatted:
cmd = 'clang-format -i {}'.format(' '.join(files_to_format))
if subprocess.Popen(cmd, shell=True, cwd=tics_repo_path).wait():
exit(-1)
diff_res = run_cmd('git diff --name-only')
if diff_res:
files_not_in_contrib = [f for f in diff_res if not f.startswith('contrib')]
files_contrib = [f for f in diff_res if f.startswith('contrib')]
if files_not_in_contrib:
print('')
print('Error: found files NOT formatted')
print(''.join(diff_res))
print(''.join(files_not_in_contrib))
exit(-1)
elif files_contrib:
print('')
print('Warn: found contrib changed')
print(''.join(files_contrib))
print('')
print(''.join(run_cmd('git status')))
else:
print("Format check passed")
else:
cmd = 'clang-format -i {}'.format(' '.join(files_to_format))
subprocess.Popen(cmd, shell=True, cwd=tics_repo_path).wait()
print("Finish code format")
else:
print('No file to format')


if __name__ == '__main__':
main()
main()

0 comments on commit bd6130f

Please sign in to comment.