|
| 1 | +#!/usr/bin/env elixir |
| 2 | + |
| 3 | +defmodule FuncsDiffAnalyzer do |
| 4 | + @moduledoc """ |
| 5 | + Analyzes git diff output for priv/funcs.txt to identify actual content changes. |
| 6 | +
|
| 7 | + This script distinguishes between functions that are genuinely added/removed |
| 8 | + versus functions that are simply reordered due to sorting changes. |
| 9 | + It provides a mathematical consistency check between git diff statistics |
| 10 | + and actual content changes. |
| 11 | + """ |
| 12 | + |
| 13 | + @funcs_txt_path "priv/funcs.txt" |
| 14 | + def run do |
| 15 | + case System.cmd("git", ["diff", @funcs_txt_path], stderr_to_stdout: true) do |
| 16 | + {output, 0} when output != "" -> output |> parse_and_analyze() |> display_results() |
| 17 | + {_, 0} -> IO.puts("\nNo changes detected") |
| 18 | + {error, _} -> IO.puts("Error: #{error}") |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + defp parse_and_analyze(diff_output) do |
| 23 | + {added, removed} = |
| 24 | + diff_output |
| 25 | + |> String.split("\n") |
| 26 | + |> Enum.reduce({[], []}, fn |
| 27 | + "+" <> line, {adds, removes} -> |
| 28 | + if String.starts_with?(line, "++"), do: {adds, removes}, else: {[line | adds], removes} |
| 29 | + |
| 30 | + "-" <> line, {adds, removes} -> |
| 31 | + if String.starts_with?(line, "--"), do: {adds, removes}, else: {adds, [line | removes]} |
| 32 | + |
| 33 | + _, acc -> |
| 34 | + acc |
| 35 | + end) |
| 36 | + |
| 37 | + added_set = MapSet.new(added) |
| 38 | + removed_set = MapSet.new(removed) |
| 39 | + reordered = MapSet.intersection(added_set, removed_set) |
| 40 | + |
| 41 | + {MapSet.difference(added_set, reordered) |> Enum.sort(), |
| 42 | + MapSet.difference(removed_set, reordered) |> Enum.sort(), length(added), length(removed)} |
| 43 | + end |
| 44 | + |
| 45 | + defp display_results({actual_added, actual_removed, total_added, total_removed}) do |
| 46 | + git_net = total_added - total_removed |
| 47 | + content_net = length(actual_added) - length(actual_removed) |
| 48 | + |
| 49 | + IO.puts("\nGit diff: +#{total_added} -#{total_removed} = #{git_net} net lines") |
| 50 | + |
| 51 | + IO.puts( |
| 52 | + "Content: +#{length(actual_added)} -#{length(actual_removed)} = #{content_net} net functions" |
| 53 | + ) |
| 54 | + |
| 55 | + IO.puts( |
| 56 | + "Math check: #{if git_net == content_net, do: "✅ CONSISTENT", else: "❌ INCONSISTENT"}" |
| 57 | + ) |
| 58 | + |
| 59 | + case {actual_added, actual_removed} do |
| 60 | + {[], []} -> |
| 61 | + IO.puts("\n🔄 All changes are due to reordering") |
| 62 | + |
| 63 | + {funcs, []} -> |
| 64 | + show_functions("📈 NEW", funcs) |
| 65 | + |
| 66 | + {[], funcs} -> |
| 67 | + show_functions("📉 REMOVED", funcs) |
| 68 | + |
| 69 | + {new_funcs, old_funcs} -> |
| 70 | + [show_functions("📈 NEW", new_funcs), show_functions("📉 REMOVED", old_funcs)] |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + defp show_functions(label, funcs) do |
| 75 | + IO.puts("\n#{label} FUNCTIONS (#{length(funcs)}):") |
| 76 | + |
| 77 | + funcs |
| 78 | + |> Enum.each(&IO.puts(" #{if String.contains?(label, "NEW"), do: "+", else: "-"} #{&1}")) |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +# Run the analysis |
| 83 | +FuncsDiffAnalyzer.run() |
0 commit comments