Guido Günther <a...@sigxcpu.org> writes:

[...]

>> I did it in gbp.deb.Changelog.create.
>> 
>> I was thinking about adding a "first=Fales" parameter to
>> Changelog.__init__ and make Changelog.create using True.
>> 
>> But this seems to be a "private" attribute, I don't like the idea of
>> exposing it in __init__ signature.
>
> Can you point me to your tree that uses the first attribute?

Here is the tag information, I includ patch against experimental after
for direct review[1].

This version suffer the direct gbp.script import.

Regards.

The following changes since commit c57d4af675910ec151cf982532db0f877aef413f:

  gbp.git.repository: Add a "git merge-base" wrapper (2012-05-14 13:16:15 +0200)

are available in the git repository at:

  git://git.baby-gnu.org/git-buildpackage 
tags/dad/create-inexistant-changelog/rebasable/on-c57d4af

for you to fetch changes up to f488942231ce317f11bce2e69fdaa313671c79a2:

  Create debian/changelog if it does not exist. (2012-05-14 22:37:55 +0200)

----------------------------------------------------------------
Consolidated patch rebased on latest experimental which integrate 
GitRepository.get_merge_base.

Fixes in test:

- use of self.assertEqual as suggested by Guido Günther
- remove sys.path manipulation in test, the caller handle it

----------------------------------------------------------------
Daniel Dehennin (1):
      Create debian/changelog if it does not exist.

 gbp/deb/changelog.py              |   40 ++++++++++
 gbp/scripts/dch.py                |   29 ++++++-
 tests/11_test_changelog_create.py |  159 +++++++++++++++++++++++++++++++++++++
 3 files changed, 225 insertions(+), 3 deletions(-)
 create mode 100644 tests/11_test_changelog_create.py


From f488942231ce317f11bce2e69fdaa313671c79a2 Mon Sep 17 00:00:00 2001
From: Daniel Dehennin <daniel.dehen...@baby-gnu.org>
Date: Thu, 3 May 2012 08:32:20 +0200
Subject: [PATCH] Create debian/changelog if it does not exist.

* tests/11_test_changelog_create.py: Test several senarios of
  debian/changelog creation.

* gbp/deb/changelog.py: Import scripts from gbp instead of dch to avoid
  include errors.
  (create): New class method to create debian/changelog with version from
  git-dch option "-N" or upstream tag.

* gbp/scripts/dch.py (guess_version_from_upstream): cp is None when
  called by ChangeLog.create().
  (main): Create a new changelog if it does not exist.
  Guess last time debian/changelog was touched if debian/changelog was
  just created.
---
 gbp/deb/changelog.py              |   40 ++++++++++
 gbp/scripts/dch.py                |   29 ++++++-
 tests/11_test_changelog_create.py |  159 +++++++++++++++++++++++++++++++++++++
 3 files changed, 225 insertions(+), 3 deletions(-)
 create mode 100644 tests/11_test_changelog_create.py

diff --git a/gbp/deb/changelog.py b/gbp/deb/changelog.py
index b8c10b8..b238baf 100644
--- a/gbp/deb/changelog.py
+++ b/gbp/deb/changelog.py
@@ -19,6 +19,7 @@
 import email
 import os
 import subprocess
+from gbp import scripts
 
 class NoChangeLogError(Exception):
     """No changelog found"""
@@ -148,3 +149,42 @@ class ChangeLog(object):
         """
         return self._cp['Date']
 
+    @classmethod
+    def create(klass, repo=None, package=None, version=None, 
upstream_tag_format=None):
+        """
+        Create an empty debian/changelog
+
+        @param repo: git repository object
+        @type repo: L{GitRepository}
+        @param package: name of the package, get it from debian/control if None
+        @type package: C{str}
+        @param upstream_tag_format: format of the upstream tag
+        @type upstream_tag_format: C{str}
+        @return: changelog object
+        @rtype: L{ChangeLog}
+        """
+
+        if package == None and os.path.isfile("debian/control"):
+            try:
+                control = open("debian/control", "r")
+                for line in control:
+                    if line.startswith("Source: "):
+                        package = line.split()[1]
+                        break
+            except Exception, e:
+                raise NoChangeLogError, "Error parsing debian/control: %s" % e
+        elif package == None:
+            raise NoChangeLogError, "No package name provided and no 
debian/control file"
+
+        if version == None:
+            version = scripts.dch.guess_version_from_upstream(repo, 
upstream_tag_format, None)
+        if version == None:
+            raise NoChangeLogError, "No suitable version found"
+
+        scripts.dch.spawn_dch(msg=["debian/changelog: generated by git-dch."],
+                              newversion=True, version={'version':version},
+                              dch_options="--create --package %s" % package)
+
+        cp = klass(filename="debian/changelog")
+        cp.first = True
+        return cp
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

Attachment: pgp0nmXtTkj66.pgp
Description: PGP signature

Reply via email to