On Mon, May 14, 2012 at 10:46:52PM +0200, Daniel Dehennin wrote:

> +        cp = klass(filename="debian/changelog")
> +        cp.first = True
> +        return cp

It's better to have a:

cp.is_empty()

method here. You can simply stat the file and check it's length to
implement this until we have the ChangeLog class parsing the changelog
for real.
Cheers,
 -- Guido


> diff --git a/gbp/scripts/dch.py b/gbp/scripts/dch.py
> index 14dff29..0309aa4 100644
> --- a/gbp/scripts/dch.py
> +++ b/gbp/scripts/dch.py
> @@ -112,6 +112,8 @@ def guess_version_from_upstream(repo, 
> upstream_tag_format, cp):
>          version = repo.tag_to_version(tag, upstream_tag_format)
>          if version:
>              gbp.log.debug("Found upstream version %s." % version)
> +            if cp == None:
> +                return "%s-1" % version
>              if cp.has_epoch():
>                  version = "%s:%s" % (cp.epoch, version)
>              if compare_versions(version, cp.version) > 0:
> @@ -357,6 +359,8 @@ def main(argv):
>      parser.add_option_group(custom_group)
>  
>      parser.add_boolean_config_file_option(option_name = "ignore-branch", 
> dest="ignore_branch")
> +    naming_group.add_config_file_option(option_name="upstream-tree", 
> dest="upstream_tree")
> +    naming_group.add_config_file_option(option_name="upstream-branch", 
> dest="upstream_branch")
>      naming_group.add_config_file_option(option_name="debian-branch", 
> dest="debian_branch")
>      naming_group.add_config_file_option(option_name="upstream-tag", 
> dest="upstream_tag")
>      naming_group.add_config_file_option(option_name="debian-tag", 
> dest="debian_tag")
> @@ -423,13 +427,17 @@ def main(argv):
>              gbp.log.err("You are not on branch '%s' but on '%s'" % 
> (options.debian_branch, branch))
>              raise GbpError, "Use --ignore-branch to ignore or 
> --debian-branch to set the branch name."
>  
> -        cp = ChangeLog(filename=changelog)
> +        try:
> +            cp = ChangeLog(filename=changelog)
> +        except NoChangeLogError, e:
> +            gbp.log.debug("No debian/changelog: create a new one")
> +            cp = ChangeLog.create(repo, version=options.new_version, 
> upstream_tag_format=options.upstream_tag)
>  
>          if options.since:
>              since = options.since
>          else:
>              since = ''
> -            if options.auto:
> +            if options.auto or hasattr(cp, 'first') and cp.first:
>                  since = guess_snapshot_commit(cp, repo, options)
>                  if since:
>                      gbp.log.info("Continuing from commit '%s'" % since)
> @@ -437,7 +445,22 @@ def main(argv):
>                  else:
>                      gbp.log.info("Couldn't find snapshot header, using 
> version info")
>              if not since:
> -                since = repo.find_version(options.debian_tag, cp['Version'])
> +                # Take care of newly created debian/changelog
> +                if hasattr(cp, 'first') and cp.first:
> +                    pattern = options.upstream_tag % dict(version='*')
> +                    try:
> +                        upstream = repo.find_tag('HEAD', pattern=pattern)
> +                    except GitRepositoryError:
> +                        gbp.debug('No upstream tag found')
> +                        upstream = options.upstream_branch
> +                    if options.upstream_tree == 'branch':
> +                        upstream = options.upstream_branch
> +                    since = repo.get_merge_base('HEAD', upstream)
> +                    if since and options.snapshot:
> +                        # Snapshot can not be guessed
> +                        found_snapshot_header = True
> +                else:
> +                    since = repo.find_version(options.debian_tag, 
> cp['Version'])
>                  if not since:
>                      raise GbpError, "Version %s not found" % cp['Version']
>  
> diff --git a/tests/11_test_changelog_create.py 
> b/tests/11_test_changelog_create.py
> new file mode 100644
> index 0000000..270bdf1
> --- /dev/null
> +++ b/tests/11_test_changelog_create.py
> @@ -0,0 +1,159 @@
> +# vim: set fileencoding=utf-8 :
> +
> +"""Test L{Changelog}'s create"""
> +
> +import unittest
> +
> +from testutils import DebianGitTestRepo
> +
> +from gbp.scripts import dch
> +from gbp.deb.changelog import ChangeLog
> +
> +import os
> +import re
> +
> +snap_header = 
> r'^test-package\s\(1.0-1~1\.gbp([0-9a-f]{6})\)\sUNRELEASED;\surgency=low'
> +snap_mark = r'\s{2}\*{2}\sSNAPSHOT\sbuild\s@'
> +
> +class TestScriptDch(DebianGitTestRepo):
> +    """Test git-dch"""
> +
> +    def setUp(self):
> +        DebianGitTestRepo.setUp(self)
> +        self.add_file("foo", "bar")
> +        self.repo.create_tag("upstream/1.0", msg="upstream version 1.0")
> +        self.repo.create_branch("debian")
> +        self.repo.set_branch("debian")
> +        self.upstream_tag = "upstream/%(version)s"
> +        self.top = os.path.abspath(os.path.curdir)
> +        os.mkdir(os.path.join(self.repo.path, "debian"))
> +        os.chdir(self.repo.path)
> +        self.add_file("debian/control", """Source: test-package\nSection: 
> test\n""")
> +
> +    def tearDown(self):
> +        os.chdir(self.top)
> +        DebianGitTestRepo.tearDown(self)
> +
> +    def test_create(self):
> +        """Test direct call to ChangeLog.create(): guess package name from 
> debian/control"""
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +
> +    def test_create_without_control(self):
> +        """Test direct call to ChangeLog.create()"""
> +        ChangeLog.create(self.repo, package="test-package", 
> upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +
> +    def test_create_without_upstream_log(self):
> +        """Test direct call to ChangeLog.create(): must not include upstream 
> log in debian/changelog"""
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * added foo\n""" not in lines
> +
> +    def test_create_after_delete(self):
> +        """Test direct call to ChangeLog.create(): debian/changelog 
> deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ChangeLog.create(self.repo, upstream_tag_format=self.upstream_tag)
> +        lines = file("debian/changelog").readlines()
> +        assert """test-package (1.0-1) UNRELEASED; urgency=low\n""" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog\n""" not in lines
> +
> +    def test_create_from_dch_main(self):
> +        """Test dch.py like git-dch script does: must not include upstream 
> log in debian/changelog"""
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added debian/control\n""" in lines
> +
> +    def test_create_from_dch_main_with_auto(self):
> +        """Test dch.py like git-dch script does: guess last commit"""
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-a"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added debian/control\n""" in lines
> +
> +    def test_create_from_dch_main_with_snapshot(self):
> +        """Test dch.py like git-dch script does: snashot mode"""
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-S"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        header = re.search(snap_header, lines[0])
> +        self.assertEqual(header.lastindex, 1)
> +        assert re.search(snap_mark + header.group(1), lines[2])
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added debian/control\n""" in lines
> +
> +    def test_create_from_dch_main_after_delete(self):
> +        """Test dch.py like git-dch script does: debian/changelog was 
> deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog\n""" not in lines
> +
> +    def test_create_from_dch_main_with_auto_snapshot(self):
> +        """Test dch.py like git-dch script does: guess last commit, 
> snashot"""
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-a", "-S"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        header = re.search(snap_header, lines[0])
> +        self.assertEqual(header.lastindex, 1)
> +        assert re.search(snap_mark + header.group(1), lines[2])
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * added debian/control\n""" in lines
> +
> +    def test_create_from_dch_main_with_auto_after_delete(self):
> +        """Test dch.py like git-dch script does: guess last commit, 
> debian/changelog was deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-a"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        assert "test-package (1.0-1) UNRELEASED; urgency=low\n" in lines
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog\n""" not in lines
> +
> +    def test_create_from_dch_main_with_snapshot_after_delete(self):
> +        """Test dch.py like git-dch script does: snapshot, debian/changelog 
> was deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-S"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        header = re.search(snap_header, lines[0])
> +        self.assertEqual(header.lastindex, 1)
> +        assert re.search(snap_mark + header.group(1), lines[2])
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog\n""" not in lines
> +
> +    def test_create_from_dch_main_with_auto_snapshot_after_delete(self):
> +        """Test dch.py like git-dch script does: guess last commit, 
> snapshot, debian/changelog was deleted"""
> +        self.add_file("debian/changelog", "fake")
> +        self.repo.remove_files("debian/changelog")
> +        self.repo.commit_files(self.repo.path, "removed debian/changelog")
> +        ret = dch.main(["--upstream-tag=%s" % self.upstream_tag, 
> "--debian-branch=debian", "-a", "-S"])
> +        self.assertEqual(ret, 0)
> +        lines = file("debian/changelog").readlines()
> +        header = re.search(snap_header, lines[0])
> +        self.assertEqual(header.lastindex, 1)
> +        assert re.search(snap_mark + header.group(1), lines[2])
> +        assert """  * debian/changelog: generated by git-dch.\n""" in lines
> +        assert """  * removed debian/changelog\n""" not in lines
> -- 
> 1.7.10
> 
> 
> 
> Footnotes: 
> [1]  This make longer mail but as the rebasable signed tags are not
>      meant to stay for ever, this make history available on the BTS.
> 
> -- 
> Daniel Dehennin
> Récupérer ma clef GPG:
> gpg --keyserver pgp.mit.edu --recv-keys 0x7A6FE2DF





--
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to