c70110c
-- Lua code used by macros.forge and derivatives
c70110c
c70110c
-- Computes the suffix of a version string, removing vprefix if it matches
c70110c
-- For example with vprefix 1.2.3: 1.2.3.rc2 → .rc2 but 1.2.30 → 1.2.30 not 0
c70110c
local function getversionsuffix(vstring,vprefix)
c70110c
  if (string.sub(vstring, 1, #vprefix) == vprefix) and
c70110c
     (not string.match(string.sub(vstring, #vprefix + 1), "^%.?%d")) then
c70110c
    return string.sub(vstring, #vprefix + 1)
c70110c
  else
c70110c
    return vstring
c70110c
  end
c70110c
end
c70110c
c70110c
-- Check if an identified url is sane
c70110c
local function checkforgeurl(url, id, silent)
c70110c
  local checkedurl  = nil
c70110c
  local checkedid   = nil
c70110c
  local urlpatterns = {
c70110c
    gitlab = {
c70110c
      pattern     = 'https://[^/]+/[^/]+/[^/#?]+',
c70110c
      description = 'https://(…[-.])gitlab[-.]…/owner/repo'},
61ec2ef
    pagure = {
61ec2ef
      pattern     = 'https://[^/]+/[^/#?]+',
61ec2ef
      description = 'https://pagure.io/repo'},
61ec2ef
    pagure_ns = {
61ec2ef
      pattern     = 'https://[^/]+/[^/]+/[^/#?]+',
61ec2ef
      description = 'https://pagure.io/namespace/repo'},
61ec2ef
    pagure_fork = {
61ec2ef
      pattern     = 'https://[^/]+/fork/[^/]+/[^/#?]+',
61ec2ef
      description = 'https://pagure.io/fork/owner/repo'},
61ec2ef
    pagure_ns_fork = {
61ec2ef
      pattern     = 'https://[^/]+/fork/[^/]+/[^/]+/[^/#?]+',
61ec2ef
      description = 'https://pagure.io/fork/owner/namespace/repo'},
c70110c
    github = {
c70110c
      pattern     = 'https://[^/]+/[^/]+/[^/#?]+',
c70110c
      description = 'https://(…[-.])github[-.]…/owner/repo'},
c70110c
    ["code.googlesource.com"] = {
c70110c
      pattern     = 'https://code.googlesource.com/[^#?]*[^/#?]+',
c70110c
      description = 'https://code.googlesource.com/…/repo'},
c70110c
    ["bitbucket.org"] = {
c70110c
      pattern     = 'https://[^/]+/[^/]+/[^/#?]+',
c70110c
      description = 'https://bitbucket.org/owner/repo'}}
c70110c
  if (urlpatterns[id] ~= nil) then
c70110c
    checkedurl = string.match(url,urlpatterns[id]["pattern"])
c70110c
    if (checkedurl == nil) then
c70110c
      if not silent then
c70110c
        rpm.expand("%{error:" .. id .. " URLs must match " .. urlpatterns[id]["description"] .. " !}")
c70110c
      end
c70110c
    else
c70110c
      checkedid = id
c70110c
    end
c70110c
  end
c70110c
  return checkedurl, checkedid
c70110c
end
c70110c
c70110c
-- Check if an url matches a known forge
c70110c
local function idforge(url, silent)
c70110c
  local forgeurl = nil
c70110c
  local forge    = nil
c70110c
  if (url ~= "") then
c70110c
    forge = string.match(url, "^[^:]+://([^/]+)/")
c70110c
    if (forge == nil) then
c70110c
      if not silent then
c70110c
        rpm.expand("%{error:URLs must include a protocol such as https:// and a path starting with / !}")
c70110c
      end
c70110c
    else
61ec2ef
      if (forge == "pagure.io") then
61ec2ef
        if     string.match(url, "[^:]+://pagure.io/fork/[^/]+/[^/]+/[^/]+") then
61ec2ef
          forge = "pagure_ns_fork"
61ec2ef
        elseif string.match(url, "[^:]+://pagure.io/fork/[^/]+/[^/]+") then
61ec2ef
          forge = "pagure_fork"
61ec2ef
        elseif  string.match(url, "[^:]+://pagure.io/[^/]+/[^/]+") then
61ec2ef
          forge = "pagure_ns"
61ec2ef
        elseif  string.match(url, "[^:]+://pagure.io/[^/]+") then
61ec2ef
          forge = "pagure"
61ec2ef
        end
61ec2ef
      elseif (string.match(forge, "^gitlab[%.-]") or string.match(forge, "[%.-]gitlab[%.]")) then
c70110c
        forge = "gitlab"
c70110c
      elseif (string.match(forge, "^github[%.-]") or string.match(forge, "[%.-]github[%.]")) then
c70110c
        forge = "github"
c70110c
      end
c70110c
      forgeurl, forge = checkforgeurl(url, forge, silent)
c70110c
    end
c70110c
  end
c70110c
  return forgeurl, forge
c70110c
end
c70110c
c70110c
-- The forgemeta macro main processing function
c70110c
-- See the documentation in the macros.forge file for argument description
c70110c
-- Also called directly by gometa
353f8b9
local function meta(suffix, verbose, informative, silent)
c70110c
  local fedora = require "fedora.common"
c70110c
  local ismain = (suffix == "") or (suffix == "0")
c70110c
  if ismain then
c70110c
    fedora.zalias({"forgeurl", "forgesource", "forgesetupargs",
c70110c
                      "archivename", "archiveext", "archiveurl",
61ec2ef
                      "topdir", "extractdir", "repo", "owner", "namespace",
c70110c
                      "scm", "tag", "commit", "shortcommit", "branch", "version",
c70110c
                      "date", "distprefix"}, verbose)
c70110c
  end
c70110c
  local variables = {
c70110c
    default = {
c70110c
      scm         = "git",
c70110c
      archiveext  = "tar.bz2",
61ec2ef
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://[^/]+/[^/]+/([^/?#]+)"))}',
c70110c
      archivename = "%{repo"         .. suffix .. "}-%{ref"           .. suffix .. "}",
c70110c
      topdir      = "%{archivename"  .. suffix .. "}" },
c70110c
    gitlab = {
c70110c
      archiveurl  = "%{forgeurl"     .. suffix .. "}/-/archive/%{ref" .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
61ec2ef
    pagure = {
61ec2ef
      archiveext  = "tar.gz",
61ec2ef
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://[^/]+/([^/?#]+)"))}',
61ec2ef
      archiveurl  = "%{forgeurl"     .. suffix .. "}/archive/%{ref"   .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
61ec2ef
    pagure_ns = {
61ec2ef
      archiveext  = "tar.gz",
61ec2ef
      namespace   = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://[^/]+/([^/]+)/[^/?#]+"))}',
61ec2ef
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://[^/]+/[^/]+/([^/?#]+)"))}',
61ec2ef
      archivename = "%{namespace"    .. suffix .. "}-%{repo"          .. suffix .. "}-%{ref"         .. suffix .. "}",
61ec2ef
      archiveurl  = "%{forgeurl"     .. suffix .. "}/archive/%{ref"   .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
61ec2ef
    pagure_fork = {
61ec2ef
      archiveext  = "tar.gz",
61ec2ef
      owner       = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "https://[^/]+/fork/([^/]+)/[^/?#]+"))}',
61ec2ef
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "https://[^/]+/fork/[^/]+/([^/?#]+)"))}',
61ec2ef
      archivename = "%{owner"        .. suffix .. "}-%{repo"          .. suffix .. "}-%{ref"         .. suffix .. "}",
61ec2ef
      archiveurl  = "%{forgeurl"     .. suffix .. "}/archive/%{ref"   .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
61ec2ef
    pagure_ns_fork = {
61ec2ef
      owner       = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "https://[^/]+/fork/([^/]+)/[^/]+/[^/?#]+"))}',
61ec2ef
      namespace   = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "https://[^/]+/fork/[^/]+/([^/]+)/[^/?#]+")}',
61ec2ef
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "https://[^/]+/fork/[^/]+/[^/]+/([^/?#]+)")}',
61ec2ef
      archivename = "%{owner"        .. suffix .. "}-%{namespace"     .. suffix .. "}-%{repo"        .. suffix .. "}-%{ref"        .. suffix .. "}",
61ec2ef
      archiveurl  = "%{forgeurl"     .. suffix .. "}/archive/%{ref"   .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
c70110c
    github = {
c70110c
      archiveext  = "tar.gz",
c70110c
      archivename = "%{repo"         .. suffix .. "}-%{fileref"       .. suffix .. "}",
c70110c
      archiveurl  = "%{forgeurl"     .. suffix .. "}/archive/%{ref"   .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}" },
c70110c
    ["code.googlesource.com"] = {
c70110c
      archiveext  = "tar.gz",
c70110c
      repo        = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://.+/([^/?#]+)"))}',
c70110c
      archiveurl  = "%{forgeurl"     .. suffix .. "}/+archive/%{ref"  .. suffix .. "}.%{archiveext"  .. suffix .. "}",
c70110c
      topdir      = "" },
c70110c
    ["bitbucket.org"] = {
c70110c
      shortcommit = '%{lua:print(string.sub(rpm.expand("%{commit'     .. suffix .. '}"), 1, 12))}',
61ec2ef
      owner       = '%{lua:print(string.match(rpm.expand("%{forgeurl' .. suffix .. '}"), "^[^:]+://[^/]+/([^/?#]+)"))}',
c70110c
      archivename = "%{owner"        .. suffix .. "}-%{repo"          .. suffix .. "}-%{shortcommit" .. suffix .. "}",
c70110c
      archiveurl  = "%{forgeurl"     .. suffix .. "}/get/%{ref"       .. suffix .. "}.%{archiveext"  .. suffix .. "}" } }
c70110c
  -- Packaging a moving branch is quite a bad idea, but since at least Gitlab
c70110c
  -- will treat branches and tags the same way better support branches explicitly
c70110c
  -- than have packagers hijack %{tag} to download branch states
c70110c
  local spec = {}
c70110c
  for _, v in ipairs({'forgeurl','tag','commit','branch','version'}) do
c70110c
    spec[v] = rpm.expand("%{?" .. v .. suffix .. "}")
c70110c
  end
c70110c
  -- Compute the reference of the object to fetch
c70110c
  local isrelease = false
c70110c
  if     (spec["tag"]     ~= "") then       ref = "%{?tag"     .. suffix .. "}"
c70110c
  elseif (spec["commit"]  ~= "") then       ref = "%{?commit"  .. suffix .. "}"
c70110c
  elseif (spec["branch"]  ~= "") then       ref = "%{?branch"  .. suffix .. "}"
c70110c
  else                                      ref = "%{?version" .. suffix .. "}"
c70110c
                                      isrelease = true
c70110c
  end
c70110c
  if (rpm.expand(ref) == "") then
c70110c
    if (suffix == "") then
c70110c
      rpm.expand("%{error:You need to define Version:, %{commit} or %{tag} before the macro invocation !}")
c70110c
    else
c70110c
      rpm.expand("%{error:You need to define %{version" .. suffix .. "}, %{commit" .. suffix .. "} or %{tag" .. suffix .. "} before the macro invocation !}")
c70110c
    end
c70110c
  end
c70110c
  local    forgeurl = spec["forgeurl"]
c70110c
  -- For backwards compatibility only
c70110c
  local expliciturl = rpm.expand("%{?-u*}")
c70110c
  if   (expliciturl ~= "") then
c70110c
    rpm.expand("%{warn:-u use in %%forgemeta is deprecated, use -z instead to select a separate set of rpm variables!}")
c70110c
           forgeurl = expliciturl
c70110c
  end
c70110c
  local forge
c70110c
  forgeurl,   forge = idforge(forgeurl, silent)
c70110c
  if (forge ~= nil) then
c70110c
    fedora.explicitset("forgeurl" .. suffix, forgeurl, verbose)
c70110c
    -- Custom processing of quirky forges that can not be handled with simple variables
c70110c
    if (forge == "github") then
c70110c
      -- Workaround the way GitHub injects "v"s before some version strings (but not all!)
c70110c
      -- To package one of the minority of sane GitHub projects that do not munge their version
c70110c
      -- strings set tag to %{version} in your spec
c70110c
      local fileref = ref
c70110c
      if (ref == "%{?version"  .. suffix .. "}") then
c70110c
        ref = "v" .. ref
c70110c
      elseif (fileref ~= "%{?commit" .. suffix .. "}") and
c70110c
             string.match(rpm.expand(fileref), "^v[%d]") then
c70110c
        fileref = string.gsub(rpm.expand(fileref), "^v", "")
7b6ec46
      elseif (string.match(rpm.expand(fileref), "/")) then
7b6ec46
        fileref = string.gsub(rpm.expand(fileref), "/", "-")
c70110c
      end
c70110c
      fedora.safeset("fileref" .. suffix, fileref, verbose)
c70110c
    elseif (forge == "code.googlesource.com") then
c70110c
      if (ref == "%{?version"  .. suffix .. "}") then
c70110c
        ref = "v" .. ref
c70110c
      end
c70110c
    elseif (forge == "bitbucket.org") then
c70110c
      if (spec["commit"] == "") then
c70110c
        rpm.expand("%{error:All BitBucket URLs require commit value knowledge: you need to define %{commit}!}")
c70110c
      end
c70110c
    end
c70110c
    fedora.safeset("ref" .. suffix, ref, verbose)
c70110c
    -- Mass setting of the remaining variables
c70110c
    for k,v in pairs(variables[forge]) do
c70110c
      fedora.safeset(k .. suffix, variables[forge][k], verbose)
c70110c
    end
c70110c
    for k,v in pairs(variables["default"]) do
c70110c
      if (variables[forge][k] == nil) then
c70110c
        fedora.safeset(k .. suffix, variables["default"][k], verbose)
c70110c
      end
c70110c
    end
c70110c
  end
c70110c
  -- Generic rules
c70110c
  for _, v in ipairs({'archiveurl','archivename','archiveext','topdir'}) do
c70110c
    spec[v] = rpm.expand("%{?" .. v .. suffix .. "}")
c70110c
  end
c70110c
  -- Source URL processing (computing the forgesource spec variable)
c70110c
  local forgesource = "%{archiveurl" .. suffix .. "}"
c70110c
  if (string.match(spec["archiveurl"], "/([^/]+)$") ~= spec["archivename"] .. "." .. spec["archiveext"]) then
c70110c
    forgesource     = "%{?archiveurl" .. suffix .. "}#/%{?archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}"
c70110c
  end
c70110c
  fedora.safeset("forgesource" .. suffix, forgesource, verbose)
c70110c
  -- Setup processing      (computing the forgesetup and extractdir variables)
c70110c
  local forgesetupargs = "-n %{extractdir" .. suffix .. "}"
c70110c
  local extractdir     = "%{topdir"        .. suffix .. "}"
c70110c
  if (spec["topdir"] == "") then
c70110c
    forgesetupargs     = "-c " .. forgesetupargs
c70110c
    extractdir         = "%{archivename"   .. suffix .. "}"
c70110c
  end
c70110c
  if not ismain then
c70110c
    if (spec["topdir"] ~= "") then
c70110c
      forgesetupargs = "-T -D -b " .. suffix .. " " .. forgesetupargs
c70110c
    else
c70110c
      forgesetupargs = "-T -D -a " .. suffix .. " " .. forgesetupargs
c70110c
    end
c70110c
  end
c70110c
  fedora.safeset("forgesetupargs" .. suffix, forgesetupargs, verbose)
c70110c
  fedora.safeset("extractdir"     .. suffix, extractdir, verbose)
c70110c
  -- dist processing       (computing the correct prefix for snapshots)
c70110c
  local distprefix = ""
c70110c
  if not isrelease then
455084c
    distprefix = string.lower(rpm.expand(ref))
c70110c
    if     (ref == "%{?commit" .. suffix .. "}") then
c70110c
      distprefix = string.sub(distprefix, 1, 7)
c70110c
    elseif (ref ~= "%{?branch" .. suffix .. "}") then
c70110c
      distprefix = string.gsub(distprefix,      "[%p%s]+", ".")
455084c
      distprefix = string.gsub(distprefix, "^" .. string.lower(rpm.expand("%{?repo}")) .. "%.?", "")
455084c
      local    v = string.gsub(rpm.expand("%{version}"), "[%p%s]+", ".")
7b6ec46
      for _, p in ipairs({'','v','v.','version','version.','tags.v', 'tags.v.'}) do
c70110c
        distprefix = getversionsuffix(distprefix, p .. v)
c70110c
      end
c70110c
      distprefix = string.gsub(distprefix, "^%.", "")
c70110c
    end
c70110c
    if (distprefix ~= "") then
c70110c
      distprefix = "%{scm"     .. suffix .. "}" .. distprefix
c70110c
      date = rpm.expand("%{?date" .. suffix .. "}")
c70110c
      if (date ~= "") then
c70110c
        distprefix = date .. distprefix
c70110c
      else
c70110c
        distprefix = "%([ -r %{_sourcedir}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "} ] && date +%Y%m%d -u -r %{_sourcedir}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "})" .. distprefix
c70110c
      end
c70110c
      distprefix = "." .. distprefix
c70110c
    end
c70110c
  end
c70110c
  if (spec["version"] ~= "") and
c70110c
     (spec["version"] ~= "0") and
c70110c
     (spec["version"] ~= rpm.expand("%{?version}")) then
c70110c
    distprefix = ".%{version" .. suffix .. "}" .. distprefix
c70110c
  end
c70110c
  if (rpm.expand(distprefix) ~= "") then
c70110c
    if not ismain then
d6f7114
      distprefix = string.gsub(distprefix, "^%.", ".s")
c70110c
    end
c70110c
    fedora.safeset ("distprefix"    .. suffix, distprefix, verbose)
c70110c
  end
c70110c
  if ismain then
c70110c
    fedora.zalias({"forgeurl", "forgesource", "forgesetupargs",
c70110c
                      "archivename", "archiveext", "archiveurl",
61ec2ef
                      "topdir", "extractdir", "repo", "owner", "namespace",
c70110c
                      "scm", "shortcommit", "distprefix"}, verbose)
c70110c
  end
c70110c
  -- Final spec variable summary if the macro was called with -i
c70110c
  if informative then
c70110c
    rpm.expand("%{echo:Packaging variables read or set by %%forgemeta}")
c70110c
    fedora.echovars({"forgeurl", "forgesource", "forgesetupargs",
c70110c
                        "archivename", "archiveext", "archiveurl",
61ec2ef
                        "topdir", "extractdir", "repo", "owner", "namespace",
c70110c
                        "scm", "tag", "commit", "shortcommit", "branch", "version",
c70110c
                        "date", "distprefix"}, suffix)
c70110c
    fedora.echovars({"dist"},"")
c70110c
    rpm.expand("%{echo:  (snapshot date is either manually supplied or computed once %%{_sourcedir}/%%{archivename" .. suffix .. "}.%%{archiveext" .. suffix .. "} is available)}")
c70110c
  end
c70110c
end
c70110c
c70110c
return {
353f8b9
  meta = meta,
c70110c
}
c70110c