#!/bin/bash # Create schema for this report. Its name is based on the current git commit. # We set search_path for this user for the below psql calls, which we'll reset # at the end. hash=$(git show --format=format:%h --no-patch) psql -c "create schema git_$hash" psql -c "alter user current_user set search_path to git_$hash" psql -c "create table if not exists includes (type text, file text, header text); truncate table includes;" IFS=$'\n' # Grep all the backend .c files for the "#include" lines and insert them into the # includes table. for i in $(find src/backend \( -path "*libstemmer*" -prune \) -o -name \*.c -print); do insert_lines="" for line in $(git grep -h '^#include ".*\.h"' $i); do file=$(echo $i | sed -e 's/src.backend.//') line=$(echo $line | sed -e 's/#include "\([^"]*\)".*/\1/') if [ ! -z "$insert_lines" ]; then insert_lines=$insert_lines, fi insert_lines="$insert_lines ('source', '$file', '$line')" done # Insert in batches; too slow otherwise if [ ! -z "$insert_lines" ]; then psql -c "insert into includes values $insert_lines" fi done # Grep the .h files for the #include lines. This code is almost identical to # above, could stand refactoring. for i in $(find src/include \( -path "*libstemmer*" -prune \) -o -name \*.h -print); do insert_lines="" for line in $(git grep -h '^#include ".*\.h"' $i); do file=$(echo $i | sed -e 's/src.include.//') line=$(echo $line | sed -e 's/#include "\([^"]*\)".*/\1/') if [ ! -z "$insert_lines" ]; then insert_lines=$insert_lines, fi insert_lines="$insert_lines ('header', '$file', '$line')" done if [ ! -z "$insert_lines" ]; then psql -c "insert into includes values $insert_lines" fi done # Some header files are not interesting psql -c "delete from includes where header in ('c.h', 'postgres.h', 'postgres_fe.h', 'utils/elog.h')" # Create a table with all the direct and indirect dependencies between header # files psql -c "create table allheaderdeps as with recursive headertree(file, include, depth) as ( select file, header as include, 1 from includes where type = 'header' union select headertree.file, header, depth+1 from headertree, includes where includes.file = headertree.include ) select * from headertree" # Create a table with all the dependencies -- as above, but also consider all # .c files. psql -c "create table alldeps as with recursive headertree(file, include, depth) as ( select file, header as include, 1 from includes union select headertree.file, header, depth+1 from headertree, includes where includes.file = headertree.include ) select * from headertree" # Create a view to show the includes most used altogether. # XXX I think this is slightly bogus ... perhaps the `alldeps` table should # really include only .c files? Not sure. psql -c "create view topheaders as select a.*, b.numincludes from (select include, count(*) as used_by_source from alldeps group by include) a join (select file, count(*) as numincludes from allheaderdeps group by file) b on (b.file = a.include) /* where used_by_source >= 0 */ order by 2 desc" # reset search_path psql -c "alter user current_user reset search_path"