#!/bin/sh # Lists the Rust definitions that a branch adds or removes: fn, struct, enum, union, # trait, type, const, static and macro_rules. A name counts as new when the diff adds # a definition with that name more times than it removes one. Code that only moves is # not listed, because a move removes and adds the same name. # # Usage: scripts/new-definitions.sh [base] [head] # base defaults to the merge base of origin/main and head # head defaults to HEAD (committed code only) # # Files under tests/ directories and files named tests.rs are skipped. # Definitions inside #[cfg(test)] modules of other files are listed. set -eu visibility='(pub([(][^)]*[)])?[[:space:]]+)?' name='(r#)?[A-Za-z_][A-Za-z0-9_]*' qualifiers='((async|const|unsafe|safe)[[:space:]]+)*(extern[[:space:]]+("[^"]*"[[:space:]]+)?)?' item="^${visibility}${qualifiers}(fn|struct|enum|union|trait|type)[[:space:]]+${name}" value="^${visibility}(const|static([[:space:]]+mut)?)[[:space:]]+${name}[[:space:]]*:" macro="^macro_rules![[:space:]]*${name}" main() { cd "$(git rev-parse --show-toplevel)" head=${2:-HEAD} base=${1:-$(git merge-base origin/main "$head")} tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT definitions_in_diff + > "$tmp/added" definitions_in_diff - > "$tmp/removed" new=$(lines_whose_name_is_more_common "$tmp/added" "$tmp/removed") removed=$(lines_whose_name_is_more_common "$tmp/removed" "$tmp/added") echo "Comparing $(git rev-parse --short "$base")..$(git rev-parse --short "$head")" echo "New definitions: $(count "$new")" [ -n "$new" ] && printf '%s\n' "$new" echo "Removed definitions: $(count "$removed")" [ -n "$removed" ] && printf '%s\n' "$removed" exit 0 } # Prints "pathname" for each definition on the lines that the diff adds ("+") or # removes ("-"). The flags make the output the same whatever the user's git config is. definitions_in_diff() { git diff --no-color --no-ext-diff --no-textconv --no-relative --no-renames \ --src-prefix=a/ --dst-prefix=b/ "$base" "$head" -- \ '*.rs' ':(glob,exclude)**/tests/**' ':(glob,exclude)**/tests.rs' | awk -v sign="$1" -v item="$item" -v value="$value" -v macro="$macro" ' # The "---" and "+++" file names come between "diff --git" and the first "@@". # Inside a hunk, a removed line "-- x" also starts with "--- ". /^diff --git / { in_header = 1; next } in_header && /^@@/ { in_header = 0; next } in_header { if ($0 ~ /^(---|\+\+\+) [ab]\//) { file = substr($0, 7) sub(/\t$/, "", file) } next } substr($0, 1, 1) != sign { next } { line = substr($0, 2) sub(/^[[:space:]]+/, "", line) while (match(line, /^#!?\[[^]]*\][[:space:]]*/)) line = substr(line, RLENGTH + 1) if (match(line, item) || match(line, value) || match(line, macro)) { text = substr(line, RSTART, RLENGTH) sub(/[[:space:]]*:$/, "", text) n = split(text, words, /[[:space:]!]+/) if (words[n] != "_") print file "\t" words[n] } } ' } # Prints, once each, the "pathname" lines of file $1 whose name is on more lines # of file $1 than of file $2. lines_whose_name_is_more_common() { awk -F '\t' ' FILENAME == ARGV[1] { other[$2]++; next } { count[$2]++; lines[++n] = $0; names[n] = $2; files[n] = $1 } END { for (i = 1; i <= n; i++) if (count[names[i]] > other[names[i]] && !printed[lines[i]]++) printf " %s: %s\n", files[i], names[i] } ' "$2" "$1" | sort } count() { [ -z "$1" ] && echo 0 || printf '%s\n' "$1" | wc -l | tr -d ' '; } main "$@"