Skip to content

Instantly share code, notes, and snippets.

@StoneCypher
Created February 19, 2016 21:19
Show Gist options
  • Save StoneCypher/2df1da436c914bede241 to your computer and use it in GitHub Desktop.
Save StoneCypher/2df1da436c914bede241 to your computer and use it in GitHub Desktop.
-module(repo_metrics).
-export([
file_as_string/1,
nonws_lines/1,
nonws_linecount_string/1,
nonws_linecount_file/1,
git_churn/0,
git_churn/1,
git_churn/2,
histograph/1,
to_daterange/1,
churn/0,
churn/1,
churn/2
]).
file_as_string(Filename) ->
{ok, FaS} = file:read_file(Filename),
binary_to_list(FaS).
nonws_lines(String) -> string:tokens(String, "\r\n").
nonws_linecount_string(String) -> length(nonws_lines(String)).
nonws_linecount_file(Filename) -> nonws_linecount_string(file_as_string(Filename)).
to_daterange(none) -> "";
to_daterange(DR) -> " --since='" ++ DR ++ "'".
to_dir(none) -> "";
to_dir(Dir) -> " -C " ++ Dir.
git_churn() -> git_churn(none, none).
git_churn(DateRange) -> git_churn(none, DateRange).
git_churn(Dir, DateRange) ->
nonws_lines(os:cmd(
"git" ++ to_dir(Dir) ++ " log --all -M --name-only --format='format:'" ++ to_daterange(DateRange) ++ " | grep -v '^$'"
)).
histograph([]) -> [];
histograph(List) when is_list(List) ->
[Head|Tail] = lists:sort(List),
histo_count(Tail, Head, 1, []).
histo_count( [], Current, Count, Work) -> lists:reverse([{Current,Count}]++Work);
histo_count( [Current|Tail], Current, Count, Work) -> histo_count(Tail, Current, Count+1, Work);
histo_count( [New|Tail], Current, Count, Work) -> histo_count(Tail, New, 1, [{Current,Count}] ++ Work).
churn_row(File, ChurnCt) ->
NLF = nonws_linecount_file(File),
{File, ChurnCt, NLF, ChurnCt / NLF}.
churn() -> churn(none, none).
churn(DateRange) -> churn(none, DateRange).
churn(Dir, DateRange) ->
[ churn_row(File, ChurnCt) || {File, ChurnCt} <- histograph(git_churn(Dir, DateRange)) ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment