Skip to content

Commit

Permalink
Some tweaks to merge
Browse files Browse the repository at this point in the history
Adds note about behavior, some edge case handling around other flags that might start with --file, permissions issues, etc.
  • Loading branch information
cpsauer committed Jan 25, 2023
1 parent 29bed89 commit 309746d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 309746d

Please sign in to comment.