From 2afbcda3cfbb755da0b6413a795e893eb34f528c Mon Sep 17 00:00:00 2001 From: foxcam <118768446+thefoxcam@users.noreply.github.com> Date: Mon, 29 Jul 2024 21:51:01 +0000 Subject: [PATCH] Rewrite diff workflow to make it compare properly (#1405) * change workflow * simplify return values * exit properly oops * Update .github/workflows/build.yml * Fix result check * Run `black` --------- Co-authored-by: Robin Avery --- .github/workflows/build.yml | 19 ++++-------- tools/diff_changes.py | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) create mode 100755 tools/diff_changes.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f9ba392bad..99b358061b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -120,6 +120,7 @@ jobs: expected="$root/expected-report.json" actual="$root/actual-report.json" changes="$root/changes-report.json" + head=$(git rev-parse HEAD) ninja all_source objdiff-cli report generate -o "$actual" @@ -134,24 +135,14 @@ jobs: ;; *) exit 1 ;; esac + ninja all_source objdiff-cli report generate -o "$expected" + git checkout "$head" objdiff-cli report changes -o "$changes" "$expected" "$actual" - results="$(jq ' - .units[] - | select(.functions[] - | { from: .from.fuzzy_match_percent, - to: .to.fuzzy_match_percent } - | .from == 100 and .to < .from) - | { (.name): ([.functions[] | .name]) }' \ - "$changes")" - status="$(echo "$results" | jq -s 'if . == [] then 0 else 1 end')" - - if [ "$status" -ne 0 ]; then - echo "::error::One or more functions were broken!" - echo "$results" - fi + python tools/diff_changes.py "$changes" + status="$?" exit "$status" ;; *) exit 1 ;; diff --git a/tools/diff_changes.py b/tools/diff_changes.py new file mode 100755 index 0000000000..42aa97e39c --- /dev/null +++ b/tools/diff_changes.py @@ -0,0 +1,59 @@ +import argparse +import json +import sys +from typing import Optional + + +def process_json(data): + result = {} + for unit in data.get("units", []): + matching_functions = [] + for function in unit.get("functions", []): + from_fuzzy = function.get("from", {}).get("fuzzy_match_percent") + to_fuzzy = function.get("to", {}).get("fuzzy_match_percent") + if from_fuzzy == 100 and to_fuzzy < from_fuzzy: + func_name = function.get("name") + matching_functions.append(f"{func_name} ({from_fuzzy}% -> {to_fuzzy})") + + if matching_functions: + result[unit.get("name")] = matching_functions + + return result + + +def process_input(input_file: Optional[str]) -> None: + content = "" + if input_file == "-" or not input_file: + content = json.load(sys.stdin) + else: + with open(input_file, "r") as f: + content = json.load(f) + + result = process_json(content) + if not result: + print("No functions broken.") + sys.exit(0) + else: + print("::error::One or more functions were broken!") + sys.stdout.write(json.dumps(result, indent=2)) + sys.exit(1) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="List broken functions from the output of objdiff's `changes` command." + ) + parser.add_argument( + "input_file", + metavar="INPUT_FILE", + nargs="?", + default="", + help='input file to process (use "-" for stdin)', + ) + args = parser.parse_args() + + if args.input_file != "": + process_input(args.input_file) + else: + print("No argument provided to diff_changes.py.") + sys.exit(1)