From 5189abf52d114ce839f1e374756c21b109cbc5a6 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 20 Jan 2017 16:59:03 +0100 Subject: [PATCH] move working_copy_available() into the source classes Signed-off-by: Tobias Ulmer --- local/scm.lua | 10 ++++----- local/source.lua | 9 ++++++++ plugins/cvs.lua | 26 ++++++++++------------ plugins/files.lua | 8 +++---- plugins/git.lua | 53 +++++++++++++++++++++------------------------ plugins/gitrepo.lua | 43 +++++++++++++++--------------------- plugins/svn.lua | 22 +++++++++---------- 7 files changed, 83 insertions(+), 88 deletions(-) diff --git a/local/scm.lua b/local/scm.lua index ab0171d..d1a1134 100644 --- a/local/scm.lua +++ b/local/scm.lua @@ -1,7 +1,7 @@ --- SCM Interface -- @module local.scm --- Copyright (C) 2007-2016 emlix GmbH, see file AUTHORS +-- Copyright (C) 2007-2017 emlix GmbH, see file AUTHORS -- -- This file is part of e2factory, the emlix embedded build system. -- For more information see http://www.e2factory.org @@ -140,9 +140,10 @@ end -- @return bool -- @return an error object on failure function scm.generic_source_check(info, sourcename, require_workingcopy) - local rc, re + local rc, re, src + src = source.sources[sourcename] - rc, re = scm.working_copy_available(info, sourcename) + rc, re = src:working_copy_available() if (not rc) and require_workingcopy then return false, err.new("working copy is not available") end @@ -150,7 +151,7 @@ function scm.generic_source_check(info, sourcename, require_workingcopy) if not rc then return false, re end - return true, nil + return true end scm.register_interface("toresult") @@ -158,7 +159,6 @@ scm.register_interface("prepare_source") scm.register_interface("fetch_source") scm.register_interface("update") scm.register_interface("check_workingcopy") -scm.register_interface("working_copy_available") return strict.lock(scm) diff --git a/local/source.lua b/local/source.lua index 0f4fd0b..15228de 100644 --- a/local/source.lua +++ b/local/source.lua @@ -137,6 +137,15 @@ function source.basic_source:display() self._type, self._name)) end +--- Check whether the working copy is available. Sources that don't have a +-- working copy should return false and an error object. Abstract method. +-- @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", + self._type, self._name)) +end + --- @section end --- Dictionary holding all source objects indexed by their name. diff --git a/plugins/cvs.lua b/plugins/cvs.lua index 67c53a9..9ad22e5 100644 --- a/plugins/cvs.lua +++ b/plugins/cvs.lua @@ -282,6 +282,13 @@ function cvs.cvs_source:display() return d end +function cvs.cvs_source:working_copy_available() + if not e2lib.isdir(e2lib.join(e2tool.root(), self._working)) then + return false, err.new("working copy for %s is not available", self._name) + end + return true +end + --- Build the cvsroot string. -- @param info Info table. -- @param sourcename Source name. @@ -319,13 +326,13 @@ end function cvs.fetch_source(info, sourcename) local rc, re, e, src, cvsroot, workdir, argv - if scm.working_copy_available(info, sourcename) then - return true - end - e = err.new("fetching source failed: %s", sourcename) src = source.sources[sourcename] + if src:working_copy_available() then + return true + end + cvsroot, re = mkcvsroot(info, sourcename) if not cvsroot then return false, e:cat(re) @@ -411,7 +418,7 @@ function cvs.update(info, sourcename) e = err.new("updating source '%s' failed", sourcename) src = source.sources[sourcename] - rc, re = scm.working_copy_available(info, sourcename) + rc, re = src:working_copy_available() if not rc then return false, e:cat(re) end @@ -427,15 +434,6 @@ function cvs.update(info, sourcename) return true end -function cvs.working_copy_available(info, sourcename) - local src = source.sources[sourcename] - local dir = e2lib.join(e2tool.root(), src:get_working()) - if not e2lib.isdir(dir) then - return false, err.new("working copy for %s is not available", sourcename) - end - return true -end - function cvs.toresult(info, sourcename, sourceset, directory) -- /source/.tar.gz -- /makefile diff --git a/plugins/files.lua b/plugins/files.lua index 2b0b370..8be0247 100644 --- a/plugins/files.lua +++ b/plugins/files.lua @@ -293,6 +293,10 @@ function files.files_source:display() return d end +function files.files_source:working_copy_available() + return false, err.new("source %s doesn't require a working copy", self._name) +end + function files.fetch_source(info, sourcename) local rc, re local src = source.sources[sourcename] @@ -313,10 +317,6 @@ function files.fetch_source(info, sourcename) return true end -function files.working_copy_available(info, sourcename) - return false, err.new("source %s doesn't require a working copy", sourcename) -end - --- Handle file:copy() in a way that appears intuitive to the user. Returns -- a directory and filename that can be passed to eg. mkdir -p and cp. -- @param buildpath Base build path (string). diff --git a/plugins/git.lua b/plugins/git.lua index 5ef7475..4d4079b 100644 --- a/plugins/git.lua +++ b/plugins/git.lua @@ -274,6 +274,18 @@ function git.git_source:display() return d end +--- Check if a working copy for a git repository is available +-- @return True if available, false otherwise. +-- @return Error object if no directory. +function git.git_source:working_copy_available() + if not e2lib.isdir(e2lib.join(e2tool.root(), self._working)) then + return false, err.new("working copy for %s is not available", self._name) + end + return true +end + +-------------------------------------------------------------------------------- + --- Return the git commit ID of the specified source configuration. Specific to -- sources of type git, useful for writing plugins. -- @param info Info table. @@ -289,7 +301,7 @@ function git.git_commit_id(info, sourcename, sourceset, check_remote) e = err.new("getting commit ID failed for source: %s", sourcename) src = source.sources[sourcename] - rc, re = scm.working_copy_available(info, sourcename) + rc, re = src:working_copy_available() if not rc then return false, e:cat(re) end @@ -346,7 +358,7 @@ function git.update(info, sourcename) src = source.sources[sourcename] e = err.new("updating source '%s' failed", sourcename) - rc, re = scm.working_copy_available(info, sourcename) + rc, re = src:working_copy_available() if not rc then return false, e:cat(re) end @@ -415,13 +427,13 @@ end function git.fetch_source(info, sourcename) local e, rc, re, src, git_dir, work_tree, id - if scm.working_copy_available(info, sourcename) then - return true - end - src = source.sources[sourcename] e = err.new("fetching source failed: %s", sourcename) + if src:working_copy_available() then + return true + end + work_tree = e2lib.join(e2tool.root(), src:get_working()) git_dir = e2lib.join(work_tree, ".git") @@ -619,22 +631,6 @@ function git.prepare_source(info, sourcename, sourceset, buildpath) return true end ---- Check if a working copy for a git repository is available --- @param info the info structure --- @param sourcename string --- @return True if available, false otherwise. --- @return Error object if no directory. -function git.working_copy_available(info, sourcename) - local rc - local src = source.sources[sourcename] - local gitwc = e2lib.join(e2tool.root(), src:get_working()) - - if not e2lib.isdir(gitwc) then - return false, err.new("working copy for %s is not available", sourcename) - end - return true -end - --- turn server:location into a git-style url -- @param c table: a cache -- @param server string: server name @@ -759,18 +755,19 @@ function git.check_workingcopy(info, sourcename) local rc, re local e = err.new("checking working copy of source %s failed", sourcename) - rc = scm.working_copy_available(info, 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 - -- 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, re, id = generic_git.lookup_id(gitdir, false, ref) if not rc then diff --git a/plugins/gitrepo.lua b/plugins/gitrepo.lua index 96e37a4..d4bf261 100644 --- a/plugins/gitrepo.lua +++ b/plugins/gitrepo.lua @@ -220,25 +220,18 @@ function gitrepo_source:display() return d end --------------------------------------------------------------------------------- - --- Check if a working copy for a git repository is available --- @param info the info structure --- @param sourcename string --- @return True if working copy is available, false otherwise. --- @return Informative error object if directory does not exist -function gitrepo.working_copy_available(info, sourcename) - assertIsTable(info) - assertIsStringN(sourcename) - - local src = source.sources[sourcename] - - if not e2lib.isdir(e2lib.join(e2tool.root(), src:get_working())) then - return false, err.new("working copy for %s is not available", sourcename) +-- @return True if available, false otherwise. +-- @return Error object if no directory. +function gitrepo_source:working_copy_available() + if not e2lib.isdir(e2lib.join(e2tool.root(), self._working)) then + return false, err.new("working copy for %s is not available", self._name) end return true end +-------------------------------------------------------------------------------- + --- Fetch a gitrepo source. Adapted from git plugin. -- @param info the info structure -- @param sourcename string @@ -250,13 +243,13 @@ function gitrepo.fetch_source(info, sourcename) local e, rc, re, src, git_dir, work_tree, id - if scm.working_copy_available(info, sourcename) then - return true - end - src = source.sources[sourcename] e = err.new("fetching source failed: %s", sourcename) + if src:working_copy_available() then + return true + end + work_tree = e2lib.join(e2tool.root(), src:get_working()) git_dir = e2lib.join(work_tree, ".git") @@ -464,19 +457,19 @@ function gitrepo.check_workingcopy(info, sourcename) local rc, re local e = err.new("checking working copy of source %s failed", sourcename) - rc = scm.working_copy_available(info, sourcename) - if not rc then - e2lib.warnf("WOTHER", "in source %s: ", sourcename) - e2lib.warnf("WOTHER", " working copy is not available") - return true, nil - end - -- 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) diff --git a/plugins/svn.lua b/plugins/svn.lua index a54010e..56711df 100644 --- a/plugins/svn.lua +++ b/plugins/svn.lua @@ -369,6 +369,15 @@ function svn.svn_source:display() return d end +function svn.svn_source:working_copy_available() + if not e2lib.isdir(e2lib.join(e2tool.root(), self._working)) then + return false, err.new("working copy for %s is not available", self._name) + end + return true +end + +-------------------------------------------------------------------------------- + function svn.fetch_source(info, sourcename) local rc, re local e = err.new("fetching source failed: %s", sourcename) @@ -384,7 +393,7 @@ function svn.fetch_source(info, sourcename) return false, e:cat(re) end - if scm.working_copy_available(info, sourcename) then + if src:working_copy_available() then return true end @@ -439,17 +448,6 @@ function svn.prepare_source(info, sourcename, sourceset, build_path) return true, nil end -function svn.working_copy_available(info, sourcename) - local rc, re - local src = source.sources[sourcename] - - local dir = e2lib.join(e2tool.root(), src:get_working()) - if not e2lib.isdir(dir) then - return false, err.new("working copy for %s is not available", sourcename) - end - return true -end - function svn.check_workingcopy(info, sourcename) local rc, re local e = err.new("checking working copy failed") -- 2.39.5