summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjörn-Egil Dahlberg <egil@erlang.org>2016-12-02 14:36:00 +0100
committerBjörn-Egil Dahlberg <psyeugenic@gmail.com>2016-12-07 22:26:05 +0100
commit7780933cfb78fde053ec7c69ba8a20ac14535cf0 (patch)
tree3e411bfc99507779d8fe14d38039bb8287c02872 /src
parent9f7e79d8157db06b3b03e033db87c2e811d938d2 (diff)
Use different git commands for different git versions
The option --single-branch was introduced in git version 1.7.10 and thus rebar3 cannot fetch git dependencies on systems where earlier git versions are install. This commit will select other git clone commands if an earlier git version is detected. If the git version cannot be determined rebar3 falls back on the previous behavior and uses --single-branch.
Diffstat (limited to 'src')
-rw-r--r--src/rebar_git_resource.erl72
1 files changed, 59 insertions, 13 deletions
diff --git a/src/rebar_git_resource.erl b/src/rebar_git_resource.erl
index acb9ec0..201b8b6 100644
--- a/src/rebar_git_resource.erl
+++ b/src/rebar_git_resource.erl
@@ -107,28 +107,50 @@ download(Dir, {git, Url, ""}, State) ->
download(Dir, {git, Url, {branch, "master"}}, State);
download(Dir, {git, Url, {branch, Branch}}, _State) ->
ok = filelib:ensure_dir(Dir),
- rebar_utils:sh(?FMT("git clone ~s ~s -b ~s --single-branch",
- [rebar_utils:escape_chars(Url),
- rebar_utils:escape_chars(filename:basename(Dir)),
- rebar_utils:escape_chars(Branch)]),
- [{cd, filename:dirname(Dir)}]);
+ git_clone(branch, git_vsn(), Url, Dir, Branch);
download(Dir, {git, Url, {tag, Tag}}, _State) ->
ok = filelib:ensure_dir(Dir),
- rebar_utils:sh(?FMT("git clone ~s ~s -b ~s --single-branch",
- [rebar_utils:escape_chars(Url),
- rebar_utils:escape_chars(filename:basename(Dir)),
- rebar_utils:escape_chars(Tag)]),
- [{cd, filename:dirname(Dir)}]);
+ git_clone(tag, git_vsn(), Url, Dir, Tag);
download(Dir, {git, Url, {ref, Ref}}, _State) ->
ok = filelib:ensure_dir(Dir),
+ git_clone(ref, git_vsn(), Url, Dir, Ref);
+download(Dir, {git, Url, Rev}, _State) ->
+ ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.", []),
+ ok = filelib:ensure_dir(Dir),
+ git_clone(rev, git_vsn(), Url, Dir, Rev).
+
+%% Use different git clone commands depending on git --version
+git_clone(branch,Vsn,Url,Dir,Branch) when Vsn >= {1,7,10}; Vsn =:= undefined ->
+ rebar_utils:sh(?FMT("git clone ~s ~s -b ~s --single-branch",
+ [rebar_utils:escape_chars(Url),
+ rebar_utils:escape_chars(filename:basename(Dir)),
+ rebar_utils:escape_chars(Branch)]),
+ [{cd, filename:dirname(Dir)}]);
+git_clone(branch,_Vsn,Url,Dir,Branch) ->
+ rebar_utils:sh(?FMT("git clone ~s ~s -b ~s",
+ [rebar_utils:escape_chars(Url),
+ rebar_utils:escape_chars(filename:basename(Dir)),
+ rebar_utils:escape_chars(Branch)]),
+ [{cd, filename:dirname(Dir)}]);
+git_clone(tag,Vsn,Url,Dir,Tag) when Vsn >= {1,7,10}; Vsn =:= undefined ->
+ rebar_utils:sh(?FMT("git clone ~s ~s -b ~s --single-branch",
+ [rebar_utils:escape_chars(Url),
+ rebar_utils:escape_chars(filename:basename(Dir)),
+ rebar_utils:escape_chars(Tag)]),
+ [{cd, filename:dirname(Dir)}]);
+git_clone(tag,_Vsn,Url,Dir,Tag) ->
+ rebar_utils:sh(?FMT("git clone ~s ~s -b ~s",
+ [rebar_utils:escape_chars(Url),
+ rebar_utils:escape_chars(filename:basename(Dir)),
+ rebar_utils:escape_chars(Tag)]),
+ [{cd, filename:dirname(Dir)}]);
+git_clone(ref,_Vsn,Url,Dir,Ref) ->
rebar_utils:sh(?FMT("git clone -n ~s ~s",
[rebar_utils:escape_chars(Url),
rebar_utils:escape_chars(filename:basename(Dir))]),
[{cd, filename:dirname(Dir)}]),
rebar_utils:sh(?FMT("git checkout -q ~s", [Ref]), [{cd, Dir}]);
-download(Dir, {git, Url, Rev}, _State) ->
- ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.", []),
- ok = filelib:ensure_dir(Dir),
+git_clone(rev,_Vsn,Url,Dir,Rev) ->
rebar_utils:sh(?FMT("git clone -n ~s ~s",
[rebar_utils:escape_chars(Url),
rebar_utils:escape_chars(filename:basename(Dir))]),
@@ -136,6 +158,30 @@ download(Dir, {git, Url, Rev}, _State) ->
rebar_utils:sh(?FMT("git checkout -q ~s", [rebar_utils:escape_chars(Rev)]),
[{cd, Dir}]).
+git_vsn() ->
+ case application:get_env(rebar, git_vsn) of
+ {ok, Vsn} -> Vsn;
+ undefined ->
+ Vsn = git_vsn_fetch(),
+ application:set_env(rebar, git_vsn, Vsn),
+ Vsn
+ end.
+
+git_vsn_fetch() ->
+ case rebar_utils:sh("git --version",[]) of
+ {ok, VsnStr} ->
+ case re:run(VsnStr, "git version\\h+(\\d)\\.(\\d)\\.(\\d).*",[{capture,[1,2,3],list}]) of
+ {match,[Maj,Min,Patch]} ->
+ {list_to_integer(Maj),
+ list_to_integer(Min),
+ list_to_integer(Patch)};
+ nomatch ->
+ undefined
+ end;
+ {error, _} ->
+ undefined
+ end.
+
make_vsn(Dir) ->
case collect_default_refcount(Dir) of
Vsn={plain, _} ->