From 8328c326f1fd40ecf9d6404faaee8658ec37f201 Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Sun, 8 Jan 2012 19:48:24 +0100 Subject: Allow to specify the upstream tree via --upstream-tree without the indirection to --upstream-branch. --- docs/manpages/git-buildpackage.sgml | 15 +++---- gbp/config.py | 2 +- gbp/scripts/buildpackage.py | 35 ++++++++++------ tests/10_test_get_upstream_tree.py | 81 +++++++++++++++++++++++++++++++++++++ tests/testutils.py | 41 +++++++++++++++++++ 5 files changed, 154 insertions(+), 20 deletions(-) create mode 100644 tests/10_test_get_upstream_tree.py create mode 100644 tests/testutils.py diff --git a/docs/manpages/git-buildpackage.sgml b/docs/manpages/git-buildpackage.sgml index 5e5ae73..b6f02e1 100644 --- a/docs/manpages/git-buildpackage.sgml +++ b/docs/manpages/git-buildpackage.sgml @@ -43,7 +43,7 @@ tag-format - [tag|branch] + [TAG|BRANCH|treeish] directory type level @@ -196,9 +196,8 @@ Branch to build the orig tarball from if - is set to branch. Default is - upstream. You can give any treeish object - here. + is set to BRANCH. Default is + upstream. @@ -334,10 +333,12 @@ How to find the upstream sources used to generate the tarball. - tag looks at a tag corresponding to the - version in the changelog. branch looks at + TAG looks at a tag corresponding to the + version in the changelog. BRANCH looks at the upstream branch given via the - option. This doesn't have any effect if + option. Other values are interpreted as treeishs. + + This doesn't have any effect if is being used. diff --git a/gbp/config.py b/gbp/config.py index 78c88ca..030f116 100644 --- a/gbp/config.py +++ b/gbp/config.py @@ -72,7 +72,7 @@ class GbpOptionParser(OptionParser): 'cleaner' : 'debuild -d clean', 'debian-branch' : 'master', 'upstream-branch' : 'upstream', - 'upstream-tree' : 'tag', + 'upstream-tree' : 'TAG', 'pristine-tar' : 'False', 'filter-pristine-tar' : 'False', 'sign-tags' : 'False', diff --git a/gbp/scripts/buildpackage.py b/gbp/scripts/buildpackage.py index 911552d..8510ed1 100644 --- a/gbp/scripts/buildpackage.py +++ b/gbp/scripts/buildpackage.py @@ -330,24 +330,35 @@ def pristine_tar_build_orig(repo, cp, output_dir, options): return False -def git_archive_build_orig(repo, cp, output_dir, options): - """build orig using git-archive""" - if options.upstream_tree == 'tag': - upstream_tree = repo.version_to_tag(options.upstream_tag, cp['Upstream-Version']) - elif options.upstream_tree == 'branch': +def get_upstream_tree(repo, cp, options): + """Determine the upstream tree from the given options""" + if options.upstream_tree.upper() == 'TAG': + upstream_tree = repo.version_to_tag(options.upstream_tag, + cp['Upstream-Version']) + elif options.upstream_tree.upper() == 'BRANCH': + if not repo.has_branch(options.upstream_branch): + raise GbpError("%s is not a valid branch" % options.upstream_branch) upstream_tree = options.upstream_branch else: - raise GbpError, "Unknown value %s" % options.upstream_tree + upstream_tree = options.upstream_tree + if not repo.has_treeish(upstream_tree): + raise GbpError # git-ls-tree printed an error message already + return upstream_tree + + +def git_archive_build_orig(repo, cp, output_dir, options): + """build orig using git-archive""" + upstream_tree = get_upstream_tree(repo, cp, options) gbp.log.info("%s does not exist, creating from '%s'" % (du.orig_file(cp, options.comp_type), upstream_tree)) - if not repo.has_treeish(upstream_tree): - raise GbpError # git-ls-tree printed an error message already - gbp.log.debug("Building upstream tarball with compression '%s -%s'" % (options.comp_type, - options.comp_level)) + gbp.log.debug("Building upstream tarball with compression '%s -%s'" % + (options.comp_type, options.comp_level)) if not git_archive(repo, cp, output_dir, upstream_tree, - options.comp_type, options.comp_level, options.with_submodules): - raise GbpError, "Cannot create upstream tarball at '%s'" % output_dir + options.comp_type, + options.comp_level, + options.with_submodules): + raise GbpError("Cannot create upstream tarball at '%s'" % output_dir) def guess_comp_type(repo, comp_type, cp, tarball_dir): diff --git a/tests/10_test_get_upstream_tree.py b/tests/10_test_get_upstream_tree.py new file mode 100644 index 0000000..3cd7eb8 --- /dev/null +++ b/tests/10_test_get_upstream_tree.py @@ -0,0 +1,81 @@ +# vim: set fileencoding=utf-8 : + +"""Test L{buildpackage}'s get_upstream_tree method""" + +import testutils + +import gbp.errors +import gbp.scripts.buildpackage as buildpackage + +class MockOptions(object): + def __init__(self, + upstream_branch=None, + upstream_tree=None, + upstream_tag=None): + self.upstream_branch = upstream_branch + self.upstream_tree = upstream_tree + self.upstream_tag = upstream_tag + +class TestGetUpstreamTree(testutils.DebianGitTestRepo): + def test_valid_upstream_branch(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + self.repo.create_branch('upstream') + options = MockOptions(upstream_tree='BRANCH', + upstream_branch='upstream') + t = buildpackage.get_upstream_tree(self.repo, None, options) + self.assertEqual(t, 'upstream') + + def test_invalid_upstream_branch(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + options = MockOptions(upstream_tree='BRANCH', + upstream_branch='upstream') + self.assertRaises(gbp.errors.GbpError, + buildpackage.get_upstream_tree, + self.repo, + None, + options) + + def test_valid_tree(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + tree = self.repo.rev_parse('master') + options = MockOptions(upstream_tree=tree) + t = buildpackage.get_upstream_tree(self.repo, None, options) + self.assertEqual(t, tree) + + def test_invalid_tree(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + options = MockOptions(upstream_tree='doesnotexist') + self.assertRaises(gbp.errors.GbpError, + buildpackage.get_upstream_tree, + self.repo, + None, + options) + + def test_valid_tag(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + tree = self.repo.rev_parse('master') + cp = { 'Upstream-Version': '1.0~rc3' } + self.repo.create_tag('upstream/1.0_rc3') + options = MockOptions(upstream_tree="TAG", + upstream_tag="upstream/%(version)s") + tag = buildpackage.get_upstream_tree(self.repo, cp, options) + self.assertEqual(tag, "upstream/1.0_rc3") + + def test_invalid_tag(self): + """Write out index file to nonexistant dir""" + self.add_file('foo') + cp = { 'Upstream-Version': '1.0~rc3' } + options = MockOptions(upstream_tree="TAG", + upstream_tag="upstream/%(version)s") + self.assertRaises(gbp.errors.GbpError, + buildpackage.get_upstream_tree, + self.repo, + cp, + options) + +# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: diff --git a/tests/testutils.py b/tests/testutils.py new file mode 100644 index 0000000..74b1067 --- /dev/null +++ b/tests/testutils.py @@ -0,0 +1,41 @@ +# vim: set fileencoding=utf-8 : + +import os +import shutil +import unittest + +import gbp.log +import gbp.deb.git +import gbp.errors +import gbp.scripts.buildpackage as buildpackage + +class DebianGitTestRepo(unittest.TestCase): + """Scratch repo for a single test""" + + def setUp(self): + gbp.log.setup(False, False) + top = os.path.abspath(os.path.curdir) + self.tmpdir = os.path.join(top, 'gbp_%s_repo' % __name__) + os.mkdir(self.tmpdir) + + repodir = os.path.join(self.tmpdir, 'test_repo') + self.repo = gbp.deb.git.DebianGitRepository.create(repodir) + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + def add_file(self, name, content=None): + """ + Add a single file with name I{name} and content I{content}. If + I{content} is C{none} the content of the file is undefined. + + @param name: the file's path relativ to the git repo + @type name: C{str} + @param content: the file's content + @type content: C{str} + """ + path = os.path.join(self.repo.path, name) + with file(path, 'w+') as f: + content == None or f.write(content) + self.repo.add_files(name, force=True) + self.repo.commit_files(path, "added %s" % name) -- cgit v1.2.3