From b5ee7f79b2d0e54209d714a5eca640d338e17ac6 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sun, 22 May 2016 19:45:57 +0300 Subject: write-rockspec: infer git and hg urls from local repos --- src/luarocks/write_rockspec.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/luarocks/write_rockspec.lua b/src/luarocks/write_rockspec.lua index abfca322..9b6593b6 100644 --- a/src/luarocks/write_rockspec.lua +++ b/src/luarocks/write_rockspec.lua @@ -3,6 +3,7 @@ local write_rockspec = {} package.loaded["luarocks.write_rockspec"] = write_rockspec +local cfg = require("luarocks.cfg") local dir = require("luarocks.dir") local fetch = require("luarocks.fetch") local fs = require("luarocks.fs") @@ -111,6 +112,32 @@ local function detect_mit_license(data) return sum == 78656 end +local simple_scm_protocols = { + git = true, ["git+http"] = true, ["git+https"] = true, + hg = true, ["hg+http"] = true, ["hg+https"] = true +} + +local function detect_url_from_command(program, args, directory) + local command = fs.Q(cfg.variables[program:upper()]).. " "..args + local pipe = io.popen(fs.command_at(directory, fs.quiet_stderr(command))) + if not pipe then return nil end + local url = pipe:read("*a"):match("^([^\r\n]+)") + pipe:close() + if not url then return nil end + if not util.starts_with(url, program.."://") then + url = program.."+"..url + end + + if simple_scm_protocols[dir.split_url(url)] then + return url + end +end + +local function detect_scm_url(directory) + return detect_url_from_command("git", "config --get remote.origin.url", directory) or + detect_url_from_command("hg", "paths default", directory) +end + local function show_license(rockspec) local fd = open_file("COPYING") or open_file("LICENSE") or open_file("MIT-LICENSE.txt") if not fd then return nil end @@ -290,6 +317,8 @@ function write_rockspec.run(...) else local_dir = nil end + else + rockspec.source.url = detect_scm_url(local_dir) or rockspec.source.url end if not local_dir then -- cgit v1.2.3-55-g6feb