Skip to content

Instantly share code, notes, and snippets.

@uwiger
Created March 28, 2013 09:23
Show Gist options
  • Save uwiger/5261892 to your computer and use it in GitHub Desktop.
Save uwiger/5261892 to your computer and use it in GitHub Desktop.
A simple escript for checking git repositories (on disk) against a rebar.config file
#!/usr/bin/env escript
%% License: http://www.mozilla.org/MPL/2.0/
%% Author: Ulf Wiger <ulf@feuerlabs.com>
-mode(compile).
main([]) ->
{ok, Fs} = file:list_dir("."),
{ok, Cwd} = file:get_cwd(),
[git_vsn(F, Cwd) || F <- Fs];
main([H]) when H=="-help"; H=="-h" ->
usage();
main(["-config", Cfg]) ->
{ok, Cwd} = file:get_cwd(),
case file:consult(Cfg) of
{ok, Terms} ->
case lists:keyfind(deps, 1, Terms) of
{_, Deps} ->
Apps = find_deps(Deps),
lists:foreach(
fun({A,AppVsn,Tag}) ->
io:fwrite("[~w - expected: ~s / ~s]~n",
[A,AppVsn,Tag]),
git_vsn(atom_to_list(A), Cwd)
end, Apps);
false ->
io:fwrite("Cannot find deps in ~s~n", [Cfg]),
halt(1)
end;
Error ->
io:fwrite("Error parsing ~p:~n~w~n", [Cfg, Error]),
halt(1)
end.
%% stolen & modified from rebar_deps.erl
find_deps([App | Rest]) when is_atom(App) ->
[{App, ".*", undefined}|find_deps(Rest)];
find_deps([{App, VsnRegex} | Rest]) when is_atom(App) ->
[{App, VsnRegex, undefined}|find_deps(Rest)];
find_deps([{App, VsnRegex, Source} | Rest]) ->
[{App, VsnRegex, get_vsn(Source)}|find_deps(Rest)];
find_deps([{App, VsnRegex, Source, Opts} | Rest]) when is_list(Opts) ->
[{App, VsnRegex, get_vsn(Source)}|find_deps(Rest)];
find_deps([_|Rest]) ->
find_deps(Rest);
find_deps([]) ->
[].
get_vsn({hg, _Url, Rev}) -> Rev;
get_vsn({git, _Url}) -> "HEAD";
get_vsn({git, _Url, ""}) -> "HEAD";
get_vsn({git, _Url, Rev}) -> Rev;
get_vsn({bzr, _Url, Rev}) -> Rev;
get_vsn({svn, _Url, Rev}) -> Rev;
get_vsn({rsync, _Url}) -> "latest";
get_vsn({fossil, _Url}) -> "latest";
get_vsn({fossil, _Url, latest}) -> "latest";
get_vsn({fossil, _Url, Rev}) -> Rev;
get_vsn(_) -> "unknown".
usage() ->
io:fwrite(
escript:script_name() ++ " [-config PathToRebarConfig]
This utility checks each subdirectory of the current working dir, and
lists the git version of the git repository (assuming it is in fact a
git repos).
Options:
-config PathToRebarConfig reads a given rebar.config, and lists the
corresponding git versions for each dependent app - both the version
expected by the rebar.config and the version derived from the git repository.
"), halt().
git_vsn(D, Cwd) ->
case file:set_cwd(D) of
ok ->
Res = os:cmd("git describe --always --tags "
"`git log -n 1 --pretty=format:%h .`"),
io:fwrite("~s: ~s / ~s~n", [D, try_app_vsn(), Res]),
file:set_cwd(Cwd);
_ ->
ignore
end.
try_app_vsn() ->
%% we are in the app directory
case filelib:wildcard("ebin/*.app") of
[AppF] ->
case file:consult(AppF) of
{ok, [{application, _, Attrs}]} ->
proplists:get_value(vsn, Attrs, "?");
_ ->
"?"
end;
_ ->
"?"
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment