PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/src/luarocks/fetch/hg.lua

http://github.com/keplerproject/luarocks
Lua | 65 lines | 45 code | 11 blank | 9 comment | 9 complexity | 13ef8c92ef727d615a72ec1031a315ac MD5 | raw file
 1
 2--- Fetch back-end for retrieving sources from HG.
 3local hg = {}
 4
 5local unpack = unpack or table.unpack
 6
 7local fs = require("luarocks.fs")
 8local dir = require("luarocks.dir")
 9local util = require("luarocks.util")
10
11--- Download sources for building a rock, using hg.
12-- @param rockspec table: The rockspec table
13-- @param extract boolean: Unused in this module (required for API purposes.)
14-- @param dest_dir string or nil: If set, will extract to the given directory.
15-- @return (string, string) or (nil, string): The absolute pathname of
16-- the fetched source tarball and the temporary directory created to
17-- store it; or nil and an error message.
18function hg.get_sources(rockspec, extract, dest_dir)
19   assert(rockspec:type() == "rockspec")
20   assert(type(dest_dir) == "string" or not dest_dir)
21
22   local hg_cmd = rockspec.variables.HG
23   local ok, err_msg = fs.is_tool_available(hg_cmd, "Mercurial")
24   if not ok then
25      return nil, err_msg
26   end
27
28   local name_version = rockspec.name .. "-" .. rockspec.version
29   -- Strip off special hg:// protocol type
30   local url = rockspec.source.url:gsub("^hg://", "")
31
32   local module = dir.base_name(url)
33
34   local command = {hg_cmd, "clone", url, module}
35   local tag_or_branch = rockspec.source.tag or rockspec.source.branch
36   if tag_or_branch then
37      command = {hg_cmd, "clone", "--rev", tag_or_branch, url, module}
38   end
39   local store_dir
40   if not dest_dir then
41      store_dir = fs.make_temp_dir(name_version)
42      if not store_dir then
43         return nil, "Failed creating temporary directory."
44      end
45      util.schedule_function(fs.delete, store_dir)
46   else
47      store_dir = dest_dir
48   end
49   local ok, err = fs.change_dir(store_dir)
50   if not ok then return nil, err end
51   if not fs.execute(unpack(command)) then
52      return nil, "Failed cloning hg repository."
53   end
54   ok, err = fs.change_dir(module)
55   if not ok then return nil, err end
56
57   fs.delete(dir.path(store_dir, module, ".hg"))
58   fs.delete(dir.path(store_dir, module, ".hgignore"))
59   fs.pop_dir()
60   fs.pop_dir()
61   return module, store_dir
62end
63
64
65return hg