From 8d2002b02a6d55e77e6859e9a9b6669f0ecf8056 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 20 Jan 2017 18:23:57 +0100 Subject: [PATCH] move check_workingcopy() into the source classes Signed-off-by: Tobias Ulmer --- local/scm.lua | 3 +- local/source.lua | 13 +++- plugins/cvs.lua | 10 ++- plugins/files.lua | 15 ++-- plugins/git.lua | 184 ++++++++++++++++++++++---------------------- plugins/gitrepo.lua | 178 +++++++++++++++++++++--------------------- plugins/svn.lua | 49 ++++++------ 7 files changed, 231 insertions(+), 221 deletions(-) diff --git a/local/scm.lua b/local/scm.lua index d1a1134..2232b07 100644 --- a/local/scm.lua +++ b/local/scm.lua @@ -147,7 +147,7 @@ function scm.generic_source_check(info, sourcename, require_workingcopy) if (not rc) and require_workingcopy then return false, err.new("working copy is not available") end - rc, re = scm.check_workingcopy(info, sourcename) + rc, re = src:check_workingcopy() if not rc then return false, re end @@ -158,7 +158,6 @@ scm.register_interface("toresult") scm.register_interface("prepare_source") scm.register_interface("fetch_source") scm.register_interface("update") -scm.register_interface("check_workingcopy") return strict.lock(scm) diff --git a/local/source.lua b/local/source.lua index 15228de..849c2a3 100644 --- a/local/source.lua +++ b/local/source.lua @@ -142,12 +142,23 @@ end -- @return True or false. -- @return Error object on false function source.basic_source:working_copy_available() - error(err.new("called working_copy_availabley() of source base class, type %s name %s", + error(err.new("called working_copy_available() of source base class, type %s name %s", + self._type, self._name)) +end + +--- Verify the consistency of the working copy, for example when building +-- a release. +-- @return True on success, false on error. +-- Error object on failure. +function source.basic_source:check_workingcopy() + error(err.new("called check_workingcopy() of source base class, type %s name %s", self._type, self._name)) end --- @section end +-------------------------------------------------------------------------------- + --- Dictionary holding all source objects indexed by their name. source.sources = {} diff --git a/plugins/cvs.lua b/plugins/cvs.lua index 9ad22e5..a7c98b5 100644 --- a/plugins/cvs.lua +++ b/plugins/cvs.lua @@ -289,6 +289,12 @@ function cvs.cvs_source:working_copy_available() return true end +function cvs.cvs_source:check_workingcopy() + return true +end + +-------------------------------------------------------------------------------- + --- Build the cvsroot string. -- @param info Info table. -- @param sourcename Source name. @@ -500,10 +506,6 @@ function cvs.toresult(info, sourcename, sourceset, directory) return true, nil end -function cvs.check_workingcopy(info, sourcename) - return true -end - strict.lock(cvs) -- vim:sw=4:sts=4:et: diff --git a/plugins/files.lua b/plugins/files.lua index 8be0247..4334a38 100644 --- a/plugins/files.lua +++ b/plugins/files.lua @@ -297,6 +297,12 @@ function files.files_source:working_copy_available() return false, err.new("source %s doesn't require a working copy", self._name) end +function files.files_source:check_workingcopy() + return true +end + +-------------------------------------------------------------------------------- + function files.fetch_source(info, sourcename) local rc, re local src = source.sources[sourcename] @@ -702,15 +708,6 @@ function files.toresult(info, sourcename, sourceset, directory) return true end ---- Check for working copy availability. --- @param info The info table. --- @param sourcename The name of the source (string). --- @return Boolean, true on success. --- @return An error object on failure. -function files.check_workingcopy(info, sourcename) - return true, nil -end - --- Update the source. -- @param info The info table. -- @param sourcename The name of the source (string). diff --git a/plugins/git.lua b/plugins/git.lua index 4d4079b..5ddac52 100644 --- a/plugins/git.lua +++ b/plugins/git.lua @@ -76,6 +76,8 @@ plugin_descriptor = { exit = function (ctx) return true end, } +-------------------------------------------------------------------------------- + git.git_source = class("git_source", source.basic_source) function git.git_source.static:is_scm_source_class() @@ -284,6 +286,96 @@ function git.git_source:working_copy_available() return true end +function git.git_source:check_workingcopy() + + --- turn server:location into a git-style url + -- @param c table: a cache + -- @param server string: server name + -- @param location string: location + -- @return string: the git url, or nil + -- @return an error object on failure + local function git_url(c, server, location) + local e = err.new("translating server:location to git url") + local rurl, re = cache.remote_url(c, server, location) + if not rurl then + return nil, e:cat(re) + end + local u, re = url.parse(rurl) + if not u then + return nil, e:cat(re) + end + local g, re = generic_git.git_url1(u) + if not g then + return nil, e:cat(re) + end + return g, nil + end + + local rc, re + local e = err.new("checking working copy of source %s failed", self._name) + + -- check if branch exists + local gitdir = e2lib.join(e2tool.root(), self:get_working(), ".git") + local ref = string.format("refs/heads/%s", self._branch) + local id + + rc = self:working_copy_available() + if not rc then + e2lib.warnf("WOTHER", "in source %s: ", self._name) + e2lib.warnf("WOTHER", " working copy is not available") + return true, nil + end + + + rc, re, id = generic_git.lookup_id(gitdir, false, ref) + if not rc then + return false, e:cat(re) + elseif not id then + return false, e:cat(err.new("branch %q does not exist", self._branch)) + end + + -- git config branch..remote == "origin" + local query, expect, res + query = string.format("branch.%s.remote", self._branch) + res, re = generic_git.git_config(gitdir, query) + if not res then + e:append("remote is not configured for branch \"%s\"", self._branch) + return false, e + elseif res ~= "origin" then + e:append("%s is not \"origin\"", query) + return false, e + end + + -- git config remote.origin.url == server:location + query = string.format("remote.origin.url") + expect, re = git_url(e2tool.info().cache, self._server, self._location) + if not expect then + return false, e:cat(re) + end + res, re = generic_git.git_config(gitdir, query) + if not res then + return false, e:cat(re) + end + + local function remove_trailing_slashes(s) + while s:sub(#s) == "/" do + s = s:sub(1, #s-1) + end + return s + end + + res = remove_trailing_slashes(res) + expect = remove_trailing_slashes(expect) + if res ~= expect then + e:append('git variable "%s" does not match e2 source configuration.', + query) + e:append('expected "%s" but got "%s" instead.', expect, res) + return false, e + end + + return true +end + -------------------------------------------------------------------------------- --- Return the git commit ID of the specified source configuration. Specific to @@ -306,7 +398,7 @@ function git.git_commit_id(info, sourcename, sourceset, check_remote) return false, e:cat(re) end - rc, re = scm.check_workingcopy(info, sourcename) + rc, re = src:check_workingcopy() if not rc then return false, e:cat(re) end @@ -631,29 +723,6 @@ function git.prepare_source(info, sourcename, sourceset, buildpath) return true end ---- turn server:location into a git-style url --- @param c table: a cache --- @param server string: server name --- @param location string: location --- @return string: the git url, or nil --- @return an error object on failure -local function git_url(c, server, location) - local e = err.new("translating server:location to git url") - local rurl, re = cache.remote_url(c, server, location) - if not rurl then - return nil, e:cat(re) - end - local u, re = url.parse(rurl) - if not u then - return nil, e:cat(re) - end - local g, re = generic_git.git_url1(u) - if not g then - return nil, e:cat(re) - end - return g, nil -end - function git.toresult(info, sourcename, sourceset, directory) local rc, re, argv local e = err.new("converting result") @@ -751,73 +820,6 @@ function git.toresult(info, sourcename, sourceset, directory) return true, nil end -function git.check_workingcopy(info, sourcename) - local rc, re - local e = err.new("checking working copy of source %s failed", sourcename) - - -- check if branch exists - local src = source.sources[sourcename] - local gitdir = e2lib.join(e2tool.root(), src:get_working(), ".git") - local ref = string.format("refs/heads/%s", src:get_branch()) - local id - - rc = src:working_copy_available() - if not rc then - e2lib.warnf("WOTHER", "in source %s: ", sourcename) - e2lib.warnf("WOTHER", " working copy is not available") - return true, nil - end - - - rc, re, id = generic_git.lookup_id(gitdir, false, ref) - if not rc then - return false, e:cat(re) - elseif not id then - return false, e:cat(err.new("branch %q does not exist", src:get_branch())) - end - - -- git config branch..remote == "origin" - local query, expect, res - query = string.format("branch.%s.remote", src:get_branch()) - res, re = generic_git.git_config(gitdir, query) - if not res then - e:append("remote is not configured for branch \"%s\"", src:get_branch()) - return false, e - elseif res ~= "origin" then - e:append("%s is not \"origin\"", query) - return false, e - end - - -- git config remote.origin.url == server:location - query = string.format("remote.origin.url") - expect, re = git_url(info.cache, src:get_server(), src:get_location()) - if not expect then - return false, e:cat(re) - end - res, re = generic_git.git_config(gitdir, query) - if not res then - return false, e:cat(re) - end - - local function remove_trailing_slashes(s) - while s:sub(#s) == "/" do - s = s:sub(1, #s-1) - end - return s - end - - res = remove_trailing_slashes(res) - expect = remove_trailing_slashes(expect) - if res ~= expect then - e:append('git variable "%s" does not match e2 source configuration.', - query) - e:append('expected "%s" but got "%s" instead.', expect, res) - return false, e - end - - return true -end - strict.lock(git) -- vim:sw=4:sts=4:et: diff --git a/plugins/gitrepo.lua b/plugins/gitrepo.lua index d4bf261..22b98e9 100644 --- a/plugins/gitrepo.lua +++ b/plugins/gitrepo.lua @@ -230,6 +230,95 @@ function gitrepo_source:working_copy_available() return true end +function gitrepo_source:check_workingcopy() + + --- turn server:location into a git-style url + -- @param c table: a cache + -- @param server string: server name + -- @param location string: location + -- @return string: the git url, or nil + -- @return an error object on failure + local function git_url(c, server, location) + local e = err.new("translating server:location to git url") + local rurl, re = cache.remote_url(c, server, location) + if not rurl then + return nil, e:cat(re) + end + local u, re = url.parse(rurl) + if not u then + return nil, e:cat(re) + end + local g, re = generic_git.git_url1(u) + if not g then + return nil, e:cat(re) + end + return g, nil + end + + local rc, re + local e = err.new("checking working copy of source %s failed", self._name) + + -- check if branch exists + local gitdir = e2lib.join(e2tool.root(), self:get_working(), ".git") + local ref = string.format("refs/heads/%s", self._branch) + local id + + rc = self:working_copy_available() + if not rc then + e2lib.warnf("WOTHER", "in source %s: ", self._name) + e2lib.warnf("WOTHER", " working copy is not available") + return true, nil + end + + rc, re, id = generic_git.lookup_id(gitdir, false, ref) + if not rc then + return false, e:cat(re) + elseif not id then + return false, e:cat(err.new("branch %q does not exist", self._branch)) + end + + -- git config branch..remote == "origin" + local query, expect, res + query = string.format("branch.%s.remote", self._branch) + res, re = generic_git.git_config(gitdir, query) + if not res then + e:append("remote is not configured for branch \"%s\"", self._branch) + return false, e + elseif res ~= "origin" then + e:append("%s is not \"origin\"", query) + return false, e + end + + -- git config remote.origin.url == server:location + query = string.format("remote.origin.url") + expect, re = git_url(e2tool.info().cache, self._server, self._location) + if not expect then + return false, e:cat(re) + end + res, re = generic_git.git_config(gitdir, query) + if not res then + return false, e:cat(re) + end + + local function remove_trailing_slashes(s) + while s:sub(#s) == "/" do + s = s:sub(1, #s-1) + end + return s + end + + res = remove_trailing_slashes(res) + expect = remove_trailing_slashes(expect) + if res ~= expect then + e:append('git variable "%s" does not match e2 source configuration.', + query) + e:append('expected "%s" but got "%s" instead.', expect, res) + return false, e + end + + return true +end + -------------------------------------------------------------------------------- --- Fetch a gitrepo source. Adapted from git plugin. @@ -430,95 +519,6 @@ function gitrepo.update(info, sourcename) return true end ---- turn server:location into a git-style url --- @param c table: a cache --- @param server string: server name --- @param location string: location --- @return string: the git url, or nil --- @return an error object on failure -local function git_url(c, server, location) - local e = err.new("translating server:location to git url") - local rurl, re = cache.remote_url(c, server, location) - if not rurl then - return nil, e:cat(re) - end - local u, re = url.parse(rurl) - if not u then - return nil, e:cat(re) - end - local g, re = generic_git.git_url1(u) - if not g then - return nil, e:cat(re) - end - return g, nil -end - -function gitrepo.check_workingcopy(info, sourcename) - local rc, re - local e = err.new("checking working copy of source %s failed", sourcename) - - -- check if branch exists - local src = source.sources[sourcename] - local gitdir = e2lib.join(e2tool.root(), src:get_working(), ".git") - local ref = string.format("refs/heads/%s", src:get_branch()) - local id - - rc = src:working_copy_available() - if not rc then - e2lib.warnf("WOTHER", "in source %s: ", sourcename) - e2lib.warnf("WOTHER", " working copy is not available") - return true, nil - end - - rc, re, id = generic_git.lookup_id(gitdir, false, ref) - if not rc then - return false, e:cat(re) - elseif not id then - return false, e:cat(err.new("branch %q does not exist", src:get_branch())) - end - - -- git config branch..remote == "origin" - local query, expect, res - query = string.format("branch.%s.remote", src:get_branch()) - res, re = generic_git.git_config(gitdir, query) - if not res then - e:append("remote is not configured for branch \"%s\"", src:get_branch()) - return false, e - elseif res ~= "origin" then - e:append("%s is not \"origin\"", query) - return false, e - end - - -- git config remote.origin.url == server:location - query = string.format("remote.origin.url") - expect, re = git_url(info.cache, src:get_server(), src:get_location()) - if not expect then - return false, e:cat(re) - end - res, re = generic_git.git_config(gitdir, query) - if not res then - return false, e:cat(re) - end - - local function remove_trailing_slashes(s) - while s:sub(#s) == "/" do - s = s:sub(1, #s-1) - end - return s - end - - res = remove_trailing_slashes(res) - expect = remove_trailing_slashes(expect) - if res ~= expect then - e:append('git variable "%s" does not match e2 source configuration.', - query) - e:append('expected "%s" but got "%s" instead.', expect, res) - return false, e - end - - return true -end - --- Archives the source and prepares the necessary files outside the archive -- @param info the info structure -- @param sourcename string diff --git a/plugins/svn.lua b/plugins/svn.lua index 56711df..85fd096 100644 --- a/plugins/svn.lua +++ b/plugins/svn.lua @@ -376,6 +376,30 @@ function svn.svn_source:working_copy_available() return true end +function svn.svn_source:check_workingcopy() + local rc, re + local e = err.new("checking working copy failed") + e:append("in source %s (svn configuration):", self._name) + e:setcount(0) + if e:getcount() > 0 then + return false, e + end + -- check if the configured branch and tag exist + local d + d = e2lib.join(e2tool.root(), self:get_working(), self._branch) + if not e2lib.isdir(d) then + e:append("branch does not exist: %s", self._branch) + end + d = e2lib.join(e2tool.root(), self:get_working(), self._tag) + if not e2lib.isdir(d) then + e:append("tag does not exist: %s", self._tag) + end + if e:getcount() > 0 then + return false, e + end + return true +end + -------------------------------------------------------------------------------- function svn.fetch_source(info, sourcename) @@ -448,31 +472,6 @@ function svn.prepare_source(info, sourcename, sourceset, build_path) return true, nil end -function svn.check_workingcopy(info, sourcename) - local rc, re - local e = err.new("checking working copy failed") - e:append("in source %s (svn configuration):", sourcename) - e:setcount(0) - local src = source.sources[sourcename] - if e:getcount() > 0 then - return false, e - end - -- check if the configured branch and tag exist - local d - d = e2lib.join(e2tool.root(), src:get_working(), src:get_branch()) - if not e2lib.isdir(d) then - e:append("branch does not exist: %s", src:get_branch()) - end - d = e2lib.join(e2tool.root(), src:get_working(), src:get_tag()) - if not e2lib.isdir(d) then - e:append("tag does not exist: %s", src:get_tag()) - end - if e:getcount() > 0 then - return false, e - end - return true -end - function svn.toresult(info, sourcename, sourceset, directory) -- /source/.tar.gz -- /makefile -- 2.39.5