From 309746df5ec57540df23c89747b1f6779fcdf050 Mon Sep 17 00:00:00 2001 From: Christopher Peterson Sauer Date: Tue, 24 Jan 2023 18:43:28 -0800 Subject: [PATCH] Some tweaks to merge Adds note about behavior, some edge case handling around other flags that might start with --file, permissions issues, etc. --- refresh.template.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/refresh.template.py b/refresh.template.py index 72c4c03..91c5686 100644 --- a/refresh.template.py +++ b/refresh.template.py @@ -1065,16 +1065,17 @@ def _ensure_cwd_is_workspace_root(): for (target, flags) in target_flag_pairs: compile_command_entries.extend(_get_commands(target, flags)) - should_merge = any(arg.startswith('--file') for arg in sys.argv[1:]) - if should_merge and os.path.isfile('compile_commands.json'): - origin_compile_command_entries = [] - with open('compile_commands.json') as fd: - try: - origin_compile_command_entries = json.load(fd) - except: - log_warning(">>> Failed to merge compile_commands.json the file seems has broken") - files_index = set(entry['file'] for entry in compile_command_entries) - compile_command_entries += [entry for entry in origin_compile_command_entries if entry['file'] not in files_index] + # --file triggers incremental update of compile_commands.json + if any(arg.startswith('--file=') for arg in sys.argv[1:]) and os.path.isfile('compile_commands.json'): + previous_compile_command_entries = [] + try: + with open('compile_commands.json') as compile_commands_file: + previous_compile_command_entries = json.load(compile_commands_file) + except: + log_warning(">>> Couldn't read previous compile_commands.json. Overwriting instead of merging...") + else: + updated_files = set(entry['file'] for entry in compile_command_entries) + compile_command_entries += [entry for entry in previous_compile_command_entries if entry['file'] not in updated_files] # Chain output into compile_commands.json with open('compile_commands.json', 'w') as output_file: