You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
6.2 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_hg_resource).
  4. -behaviour(rebar_resource).
  5. -export([lock/2
  6. ,download/3
  7. ,needs_update/2
  8. ,make_vsn/1]).
  9. -include("rebar.hrl").
  10. lock(AppDir, {hg, Url, _}) ->
  11. lock(AppDir, {hg, Url});
  12. lock(AppDir, {hg, Url}) ->
  13. Ref = get_ref(AppDir),
  14. {hg, Url, {ref, Ref}}.
  15. %% Return `true' if either the hg url or tag/branch/ref is not the same as
  16. %% the currently checked out repo for the dep
  17. needs_update(Dir, {hg, Url, {tag, Tag}}) ->
  18. Ref = get_ref(Dir),
  19. {ClosestTag, Distance} = get_tag_distance(Dir, Ref),
  20. ?DEBUG("Comparing hg tag ~s with ref ~s (closest tag is ~s at distance ~s)",
  21. [Tag, Ref, ClosestTag, Distance]),
  22. not ((Distance =:= "0") andalso (Tag =:= ClosestTag)
  23. andalso compare_url(Dir, Url));
  24. needs_update(Dir, {hg, Url, {branch, Branch}}) ->
  25. Ref = get_ref(Dir),
  26. BRef = get_branch_ref(Dir, Branch),
  27. not ((Ref =:= BRef) andalso compare_url(Dir, Url));
  28. needs_update(Dir, {hg, Url, "default"}) ->
  29. Ref = get_ref(Dir),
  30. BRef = get_branch_ref(Dir, "default"),
  31. not ((Ref =:= BRef) andalso compare_url(Dir, Url));
  32. needs_update(Dir, {hg, Url, Ref}) ->
  33. LocalRef = get_ref(Dir),
  34. TargetRef = case Ref of
  35. {ref, Ref1} ->
  36. Length = length(LocalRef),
  37. if Length >= 7 -> lists:sublist(Ref1, Length);
  38. Length < 7 -> Ref1
  39. end;
  40. Ref1 ->
  41. Ref1
  42. end,
  43. ?DEBUG("Comparing hg ref ~s with ~s", [Ref1, LocalRef]),
  44. not ((LocalRef =:= TargetRef) andalso compare_url(Dir, Url)).
  45. download(Dir, {hg, Url}, State) ->
  46. ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.", []),
  47. download(Dir, {hg, Url, {branch, "default"}}, State);
  48. download(Dir, {hg, Url, ""}, State) ->
  49. ?WARN("WARNING: It is recommended to use {branch, Name}, {tag, Tag} or {ref, Ref}, otherwise updating the dep may not work as expected.", []),
  50. download(Dir, {hg, Url, {branch, "default"}}, State);
  51. download(Dir, {hg, Url, {branch, Branch}}, _State) ->
  52. ok = filelib:ensure_dir(Dir),
  53. rebar_utils:sh(?FMT("hg clone -q -b ~s ~s ~s",
  54. [rebar_utils:escape_chars(Branch),
  55. rebar_utils:escape_chars(Url),
  56. rebar_utils:escape_chars(filename:basename(Dir))]),
  57. [{cd, filename:dirname(Dir)}]);
  58. download(Dir, {hg, Url, {tag, Tag}}, _State) ->
  59. ok = filelib:ensure_dir(Dir),
  60. rebar_utils:sh(?FMT("hg clone -q -u ~s ~s ~s",
  61. [rebar_utils:escape_chars(Tag),
  62. rebar_utils:escape_chars(Url),
  63. rebar_utils:escape_chars(filename:basename(Dir))]),
  64. [{cd, filename:dirname(Dir)}]);
  65. download(Dir, {hg, Url, {ref, Ref}}, _State) ->
  66. ok = filelib:ensure_dir(Dir),
  67. rebar_utils:sh(?FMT("hg clone -q -r ~s ~s ~s",
  68. [rebar_utils:escape_chars(Ref),
  69. rebar_utils:escape_chars(Url),
  70. rebar_utils:escape_chars(filename:basename(Dir))]),
  71. [{cd, filename:dirname(Dir)}]);
  72. download(Dir, {hg, Url, Rev}, _State) ->
  73. ok = filelib:ensure_dir(Dir),
  74. rebar_utils:sh(?FMT("hg clone -q -r ~s ~s ~s",
  75. [rebar_utils:escape_chars(Rev),
  76. rebar_utils:escape_chars(Url),
  77. rebar_utils:escape_chars(filename:basename(Dir))]),
  78. [{cd, filename:dirname(Dir)}]).
  79. make_vsn(Dir) ->
  80. BaseHg = "hg -R \"" ++ rebar_utils:escape_double_quotes(Dir) ++ "\" ",
  81. Ref = get_ref(Dir),
  82. Cmd = BaseHg ++ "log --template \"{latesttag}+build.{latesttagdistance}.rev.{node|short}\""
  83. " --rev " ++ Ref,
  84. AbortMsg = io_lib:format("Version resolution of hg dependency failed in ~s", [Dir]),
  85. {ok, VsnString} =
  86. rebar_utils:sh(Cmd,
  87. [{use_stdout, false}, {debug_abort_on_error, AbortMsg}]),
  88. RawVsn = string:strip(VsnString, both, $\n),
  89. Vsn = case RawVsn of
  90. "null+" ++ Rest -> "0.0.0+" ++ Rest;
  91. _ -> RawVsn
  92. end,
  93. {plain, Vsn}.
  94. %%% Internal functions
  95. compare_url(Dir, Url) ->
  96. CurrentUrl = string:strip(os:cmd("hg -R \"" ++ rebar_utils:escape_double_quotes(Dir) ++"\" paths default"), both, $\n),
  97. CurrentUrl1 = string:strip(CurrentUrl, both, $\r),
  98. parse_hg_url(CurrentUrl1) =:= parse_hg_url(Url).
  99. get_ref(Dir) ->
  100. AbortMsg = io_lib:format("Get ref of hg dependency failed in ~s", [Dir]),
  101. {ok, RefString} =
  102. rebar_utils:sh("hg -R \"" ++ rebar_utils:escape_double_quotes(Dir) ++ "\" --debug id -i",
  103. [{use_stdout, false}, {debug_abort_on_error, AbortMsg}]),
  104. string:strip(RefString, both, $\n).
  105. get_tag_distance(Dir, Ref) ->
  106. AbortMsg = io_lib:format("Get tag distance of hg dependency failed in ~s", [Dir]),
  107. {ok, LogString} =
  108. rebar_utils:sh("hg -R \"" ++ rebar_utils:escape_double_quotes(Dir) ++ "\" "
  109. "log --template \"{latesttag}-{latesttagdistance}\n\" "
  110. "--rev " ++ rebar_utils:escape_chars(Ref),
  111. [{use_stdout, false}, {debug_abort_on_error, AbortMsg}]),
  112. Log = string:strip(LogString,
  113. both, $\n),
  114. [Tag, Distance] = re:split(Log, "-([0-9]+)$", [{parts,0}, {return, list}]),
  115. {Tag, Distance}.
  116. get_branch_ref(Dir, Branch) ->
  117. AbortMsg = io_lib:format("Get branch ref of hg dependency failed in ~s", [Dir]),
  118. {ok, BranchRefString} =
  119. rebar_utils:sh("hg -R \"" ++ rebar_utils:escape_double_quotes(Dir) ++
  120. "\" log --template \"{node}\n\" --rev " ++ rebar_utils:escape_chars(Branch),
  121. [{use_stdout, false}, {debug_abort_on_error, AbortMsg}]),
  122. string:strip(BranchRefString, both, $\n).
  123. parse_hg_url("ssh://" ++ HostPath) ->
  124. [Host | Path] = string:tokens(HostPath, "/"),
  125. {Host, filename:rootname(filename:join(Path), ".hg")};
  126. parse_hg_url("http://" ++ HostPath) ->
  127. [Host | Path] = string:tokens(HostPath, "/"),
  128. {Host, filename:rootname(filename:join(Path), ".hg")};
  129. parse_hg_url("https://" ++ HostPath) ->
  130. [Host | Path] = string:tokens(HostPath, "/"),
  131. {Host, filename:rootname(filename:join(Path), ".hg")}.