Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. defmodule Mix.SCM.Mercurial do
  2. @behaviour Mix.SCM
  3. @moduledoc false
  4.  
  5. def fetchable? do
  6. true
  7. end
  8.  
  9. def format(opts) do
  10. opts[:hg]
  11. end
  12.  
  13. def format_lock(opts) do
  14. case opts[:lock] do
  15. {:hg, _, lock_rev, lock_opts} ->
  16. lock = String.slice(lock_rev, 0, 7)
  17. case Enum.find_value [:branch, :ref, :tag], &List.keyfind(lock_opts, &1, 0) do
  18. {:ref, _} -> lock <> " (ref)"
  19. {key, val} -> lock <> " (#{key}: #{val})"
  20. nil -> lock
  21. end
  22. _ ->
  23. nil
  24. end
  25. end
  26.  
  27. def accepts_options(_app, opts) do
  28. cond do
  29. gh = opts[:hghub] ->
  30. opts |> Keyword.delete(:hghub) |> Keyword.put(:hg, "hg://hghub.com/#{gh}.hg")
  31. opts[:hg] ->
  32. opts
  33. true ->
  34. nil
  35. end
  36. end
  37.  
  38. def checked_out?(opts) do
  39. # Are we inside a hg repository?
  40. File.regular?(Path.join(opts[:dest], ".hg/requires"))
  41. end
  42.  
  43. def lock_status(opts) do
  44. assert_hg
  45.  
  46. case opts[:lock] do
  47. {:hg, lock_repo, lock_rev, lock_opts} ->
  48. File.cd!(opts[:dest], fn ->
  49. rev_info = get_rev_info
  50. cond do
  51. lock_repo != opts[:hg] -> :outdated
  52. lock_opts != get_lock_opts(opts) -> :outdated
  53. lock_rev != rev_info[:rev] -> :mismatch
  54. lock_repo != rev_info[:origin] -> :outdated
  55. true -> :ok
  56. end
  57. end)
  58. nil ->
  59. :mismatch
  60. _ ->
  61. :outdated
  62. end
  63. end
  64.  
  65. def equal?(opts1, opts2) do
  66. opts1[:hg] == opts2[:hg] &&
  67. get_lock_opts(opts1) == get_lock_opts(opts2)
  68. end
  69.  
  70. def checkout(opts) do
  71. assert_hg
  72.  
  73. path = opts[:dest]
  74. location = opts[:hg]
  75.  
  76. _ = File.rm_rf!(path)
  77. hg!(["clone", location, path])
  78.  
  79. File.cd! path, fn -> do_checkout(opts) end
  80. end
  81.  
  82. def update(opts) do
  83. assert_hg
  84.  
  85. File.cd! opts[:dest], fn ->
  86. # Ensures origin is set the lock repo
  87. location = opts[:hg]
  88. # update_origin(location)
  89. args = []
  90.  
  91. if opts[:tag] do
  92. args = ["--tags"|args]
  93. end
  94.  
  95. hg!(["pull" , "-u"|args])
  96. do_checkout(opts)
  97. end
  98. end
  99.  
  100. ## Helpers
  101.  
  102. defp do_checkout(opts) do
  103. ref = get_lock_rev(opts[:lock]) || get_opts_rev(opts)
  104. hg!(["update", ref])
  105.  
  106. get_lock(opts)
  107. end
  108.  
  109. defp get_lock(opts) do
  110. rev_info = get_rev_info()
  111. {:hg, opts[:hg], rev_info[:rev], get_lock_opts(opts)}
  112. end
  113.  
  114. defp get_lock_rev({:hg, _repo, lock, _opts}) when is_binary(lock), do: lock
  115. defp get_lock_rev(_), do: nil
  116.  
  117. defp get_lock_opts(opts) do
  118. lock_opts = Enum.find_value [:branch, :ref, :tag], &List.keyfind(opts, &1, 0)
  119. lock_opts = List.wrap(lock_opts)
  120. if opts[:submodules] do
  121. lock_opts ++ [submodules: true]
  122. else
  123. lock_opts
  124. end
  125. end
  126.  
  127. defp get_opts_rev(opts) do
  128. if branch = opts[:branch] do
  129. "origin/#{branch}"
  130. else
  131. opts[:ref] || opts[:tag] || "default"
  132. end
  133. end
  134.  
  135. defp get_rev_info do
  136. destructure [origin, rev],
  137. :os.cmd('hg --hg-dir=.hg config remote.origin.url && hg --hg-dir=.hg rev-parse --verify --quiet HEAD')
  138. |> IO.iodata_to_binary
  139. |> String.split("\n", trim: true)
  140. [origin: origin, rev: rev]
  141. end
  142.  
  143. # defp update_origin(location) do
  144. # hg!(["--hg-dir=.hg", "config" , "remote.origin.url", location])
  145. # :ok
  146. # end
  147.  
  148. defp hg!(args) do
  149. {output, status} = System.cmd("hg", args, stderr_to_stdout: true)
  150. if status != 0 do
  151. Mix.raise "Command `hg #{Enum.join(args, " ")}` failed. Output:\n#{output}"
  152. end
  153. true
  154. end
  155.  
  156. defp assert_hg do
  157. case Mix.State.fetch(:hg_available) do
  158. {:ok, true} ->
  159. :ok
  160. :error ->
  161. if System.find_executable("hg") do
  162. Mix.State.put(:hg_available, true)
  163. else
  164. Mix.raise "Error fetching/updating Mercurial repository: the `hg` " <>
  165. "executable is not available in your PATH. Please install " <>
  166. "Mercurial on this machine or pass --no-deps-check if you want to " <>
  167. "run a previously built application on a system without Mercurial."
  168. end
  169. end
  170. end
  171.  
  172. defp to_integer(string) do
  173. {int, _} = Integer.parse(string)
  174. int
  175. end
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement