diff options
author | David Leach <dleach@wetafx.co.nz> | 2015-08-10 20:33:23 +1200 |
---|---|---|
committer | David Leach <dleach@wetafx.co.nz> | 2015-08-11 13:49:06 +1200 |
commit | da30a6e8c9c62bd47cca44322320c162bf4432b8 (patch) | |
tree | 8f69edb92fc515dda8e466d0a0a122a72b80caa6 | |
parent | 1001eefc2b0f0979c1a07fc309625ee04df60910 (diff) |
Modify parse_git_url to be more generic
Changes parse_git url function to use Using RFC3986 standard to validate
git uri instead of matching strings in function head. Also accepts scp
style syntax for parsing.
-rw-r--r-- | src/rebar_git_resource.erl | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/rebar_git_resource.erl b/src/rebar_git_resource.erl index aec1535..2fc1ba9 100644 --- a/src/rebar_git_resource.erl +++ b/src/rebar_git_resource.erl @@ -11,6 +11,9 @@ -include("rebar.hrl"). +%% Regex used for parsing scp style remote url +-define(SCP_PATTERN, "\\A(?<username>[^@]+)@(?<host>[^:]+):(?<path>.+)\\z"). + lock(AppDir, {git, Url, _}) -> lock(AppDir, {git, Url}); lock(AppDir, {git, Url}) -> @@ -67,23 +70,27 @@ compare_url(Dir, Url) -> {ok, CurrentUrl} = rebar_utils:sh(?FMT("git config --get remote.origin.url", []), [{cd, Dir}]), CurrentUrl1 = string:strip(string:strip(CurrentUrl, both, $\n), both, $\r), - ParsedUrl = parse_git_url(Url), - ParsedCurrentUrl = parse_git_url(CurrentUrl1), + {ok, ParsedUrl} = parse_git_url(Url), + {ok, ParsedCurrentUrl} = parse_git_url(CurrentUrl1), ?DEBUG("Comparing git url ~p with ~p", [ParsedUrl, ParsedCurrentUrl]), ParsedCurrentUrl =:= ParsedUrl. -parse_git_url("git@" ++ HostPath) -> - [Host, Path] = string:tokens(HostPath, ":"), - {Host, filename:rootname(Path, ".git")}; -parse_git_url("git://" ++ HostPath) -> - [Host | Path] = string:tokens(HostPath, "/"), - {Host, filename:rootname(filename:join(Path), ".git")}; -parse_git_url("http://" ++ HostPath) -> - [Host | Path] = string:tokens(HostPath, "/"), - {Host, filename:rootname(filename:join(Path), ".git")}; -parse_git_url("https://" ++ HostPath) -> - [Host | Path] = string:tokens(HostPath, "/"), - {Host, filename:rootname(filename:join(Path), ".git")}. +parse_git_url(Url) -> + %% Checks for standard scp style git remote + case re:run(Url, ?SCP_PATTERN, [{capture, [host, path], list}]) of + {match, [Host, Path]} -> + {ok, {Host, filename:rootname(Path, ".git")}}; + nomatch -> + parse_git_url(not_scp, Url) + end. +parse_git_url(not_scp, Url) -> + UriOpts = [{scheme_defaults, [{git, 9418} | http_uri:scheme_defaults()]}], + case http_uri:parse(Url, UriOpts) of + {ok, {_Scheme, _User, Host, _Port, Path, _Query}} -> + {ok, {Host, filename:rootname(Path, ".git")}}; + {error, Reason} -> + {error, Reason} + end. download(Dir, {git, Url}, State) -> ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.", []), |