diff options
author | Tristan Sloughter <t@crashfast.com> | 2014-11-01 13:17:52 -0500 |
---|---|---|
committer | Tristan Sloughter <t@crashfast.com> | 2014-11-01 13:17:52 -0500 |
commit | 35d1afd2f69aa604ec1a44ac511092dc5a40e12c (patch) | |
tree | 25e261845a06234fd59db8df0e262872182f0422 | |
parent | 6c1201f1ace6870adb5547a8e4f2fb1f22106cc2 (diff) |
verify checked out dep is the same as specified in the config and update if not
-rw-r--r-- | src/rebar_fetch.erl | 20 | ||||
-rw-r--r-- | src/rebar_git_resource.erl | 37 | ||||
-rw-r--r-- | src/rebar_prv_install_deps.erl | 27 | ||||
-rw-r--r-- | src/rebar_resource.erl | 5 | ||||
-rw-r--r-- | src/rebar_utils.erl | 2 |
5 files changed, 79 insertions, 12 deletions
diff --git a/src/rebar_fetch.erl b/src/rebar_fetch.erl index 4c1713c..bba7c1c 100644 --- a/src/rebar_fetch.erl +++ b/src/rebar_fetch.erl @@ -8,7 +8,8 @@ -module(rebar_fetch). -export([lock_source/2, - download_source/2]). + download_source/2, + needs_update/2]). -include("rebar.hrl"). @@ -36,6 +37,8 @@ download_source(AppDir, Source) -> ec_file:mkdir_p(AppDir1), case Module:download(TmpDir, Source) of {ok, _} -> + code:del_path(filename:absname(filename:join(AppDir1, "ebin"))), + ec_file:remove(filename:absname(AppDir1), [recursive]), ok = ec_file:copy(TmpDir, filename:absname(AppDir1), [recursive]), true; {tarball, File} -> @@ -43,17 +46,28 @@ download_source(AppDir, Source) -> ,compressed]), BaseName = filename:basename(AppDir1), [FromDir] = filelib:wildcard(filename:join(TmpDir, BaseName++"-*")), - ok = ec_file:copy(FromDir, AppDir1, [recursive]), + code:del_path(filename:absname(filename:join(AppDir1, "ebin"))), + ec_file:remove(filename:absname(AppDir1), [recursive]), + ok = ec_file:copy(FromDir, filename:absname(AppDir1), [recursive]), true end end. +-spec needs_update(file:filename_all(), rebar_resource:resource()) -> boolean() | {error, string()}. +needs_update(AppDir, Source) -> + case get_resource_type(Source) of + {error, _}=Error -> + Error; + Module -> + Module:needs_update(AppDir, Source) + end. + get_resource_type({Type, Location, _}) -> case lists:keyfind(Type, 1, ?RESOURCES) of false -> case code:which(Type) of non_existing -> - {error, io_lib:format("Cannot fetch dependency ~s.~n" + {error, io_lib:format("Cannot handle dependency ~s.~n" " No module for resource type ~p", [Location, Type])}; _ -> Type diff --git a/src/rebar_git_resource.erl b/src/rebar_git_resource.erl index b2b8ed5..1dfa1bf 100644 --- a/src/rebar_git_resource.erl +++ b/src/rebar_git_resource.erl @@ -5,7 +5,8 @@ -behaviour(rebar_resource). -export([lock/2 - ,download/2]). + ,download/2 + ,needs_update/2]). -include("rebar.hrl"). @@ -15,6 +16,40 @@ lock(AppDir, {git, Url, _}) -> ,both, $\n), {git, Url, {ref, Ref}}. +needs_update(Dir, {git, Url, {tag, Tag}}) -> + {ok, CurrentUrl} = rebar_utils:sh(?FMT("git config --get remote.origin.url", []), + [{cd, Dir}]), + {ok, Current} = rebar_utils:sh(?FMT("git describe --tags --exact-match", []), + [{cd, Dir}]), + Current1 = string:strip(string:strip(Current, both, $\n), both, $\r), + CurrentUrl1 = string:strip(string:strip(CurrentUrl, both, $\n), both, $\r), + ?DEBUG("Comparing git tag ~s with ~s and url ~s with ~s~n", [Tag, Current1, Url, CurrentUrl1]), + not ((Current1 =:= Tag) andalso (CurrentUrl1 =:= Url)); +needs_update(Dir, {git, Url, {branch, Branch}}) -> + {ok, CurrentUrl} = rebar_utils:sh(?FMT("git config --get remote.origin.url", []), + [{cd, Dir}]), + {ok, Current} = rebar_utils:sh(?FMT("git symbolic-ref -q --short HEAD", []), + [{cd, Dir}]), + Current1 = string:strip(string:strip(Current, both, $\n), both, $\r), + CurrentUrl1 = string:strip(string:strip(CurrentUrl, both, $\n), both, $\r), + ?DEBUG("Comparing git branch ~s with ~s and url ~s with ~s~n", [Branch, Current1, Url, CurrentUrl1]), + not ((Current1 =:= Branch) andalso (CurrentUrl1 =:= Url)); +needs_update(Dir, {git, Url, Ref}) -> + case Ref of + {ref, Ref1} -> + Ref1; + Ref1 -> + Ref1 + end, + {ok, CurrentUrl} = rebar_utils:sh(?FMT("git config --get remote.origin.url", []), + [{cd, Dir}]), + {ok, Current} = rebar_utils:sh(?FMT("git rev-parse -q HEAD", []), + [{cd, Dir}]), + Current1 = string:strip(string:strip(Current, both, $\n), both, $\r), + CurrentUrl1 = string:strip(string:strip(CurrentUrl, both, $\n), both, $\r), + ?DEBUG("Comparing git ref ~s with ~s and url ~s with ~s~n", [Ref1, Current1, Url, CurrentUrl1]), + not ((Current1 =:= Ref1) andalso (CurrentUrl1 =:= Url)). + download(Dir, {git, Url}) -> ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.~n", []), download(Dir, {git, Url, {branch, "HEAD"}}); diff --git a/src/rebar_prv_install_deps.erl b/src/rebar_prv_install_deps.erl index a4be809..6ae3042 100644 --- a/src/rebar_prv_install_deps.erl +++ b/src/rebar_prv_install_deps.erl @@ -86,7 +86,7 @@ do(State) -> end catch _:Reason -> - Reason + {error, Reason} end. -spec format_error(any(), rebar_state:t()) -> {iolist(), rebar_state:t()}. @@ -267,15 +267,30 @@ maybe_fetch(AppInfo, Update, Seen) -> ?INFO("Fetching ~s~n", [rebar_app_info:name(AppInfo)]), Source = rebar_app_info:source(AppInfo), case rebar_fetch:download_source(AppDir, Source) of - {error, _}=Error -> - throw(Error); + {error, Reason} -> + throw(Reason); Result -> Result end; _ -> - io:format("Was ~p seen: ~p~n", [rebar_app_info:name(AppInfo) - ,sets:is_element(rebar_app_info:name(AppInfo), Seen)]), - false + case sets:is_element(rebar_app_info:name(AppInfo), Seen) of + true -> + false; + false -> + Source = rebar_app_info:source(AppInfo), + case rebar_fetch:needs_update(AppDir, Source) of + true -> + ?INFO("Updating ~s~n", [rebar_app_info:name(AppInfo)]), + case rebar_fetch:download_source(AppDir, Source) of + {error, Reason} -> + throw(Reason); + Result -> + Result + end; + false -> + false + end + end end end. diff --git a/src/rebar_resource.erl b/src/rebar_resource.erl index 17db13b..f0490f8 100644 --- a/src/rebar_resource.erl +++ b/src/rebar_resource.erl @@ -23,7 +23,8 @@ -spec behaviour_info(atom()) -> [{atom(), arity()}] | undefined. behaviour_info(callbacks) -> [{lock, 2}, - {download, 2}]; + {download, 2}, + {needs_update,2}]; behaviour_info(_) -> undefined. @@ -32,5 +33,7 @@ behaviour_info(_) -> -callback lock(file:filename_all(), tuple()) -> rebar_resource:resource(). -callback download(file:filename_all(), tuple()) -> {tarball, file:filename_all()} | {ok, any()} | {error, any()}. +-callback needs_update(file:filename_all(), tuple()) -> + {tarball, file:filename_all()} | {ok, any()} | {error, any()}. -endif. diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl index 87387be..347b02a 100644 --- a/src/rebar_utils.erl +++ b/src/rebar_utils.erl @@ -135,7 +135,7 @@ sh(Command0, Options0) -> ?DEBUG("sh info:\n\tcwd: ~p\n\tcmd: ~s\n", [get_cwd(), Command0]), ?DEBUG("\topts: ~p\n", [Options0]), - DefaultOptions = [use_stdout, abort_on_error], + DefaultOptions = [{use_stdout, false}, abort_on_error], Options = [expand_sh_flag(V) || V <- proplists:compact(Options0 ++ DefaultOptions)], |