From 6945939180fd8ef453978ead733589d4aa8c1f05 Mon Sep 17 00:00:00 2001
From: Andrej Shadura <andrew.shadura@collabora.co.uk>
Date: Mon, 1 Feb 2021 12:39:15 +0100
Subject: [PATCH] Add a version suffix even when there are no local changes

We always add some extra files, at least the Apertis component,
plus copyright scanning results. Since those are intended to end up
in the source package, we should append a version suffix regardless
of any other actual changes we add on top.

Since these changes are an addition of metadata only, co0 seems a
good enough starting version.

Instead of dch, we use a custom changelog modification implementation.
This is because dch insists on launching an interactive editor when
a changelog is finalised.

Apertis: T7556

Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
---
 .../overlay/usr/bin/apertis-pkg-merge-updates | 43 ++++++++++++++++---
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/package-source-builder/overlay/usr/bin/apertis-pkg-merge-updates b/package-source-builder/overlay/usr/bin/apertis-pkg-merge-updates
index a57c933..612c7da 100755
--- a/package-source-builder/overlay/usr/bin/apertis-pkg-merge-updates
+++ b/package-source-builder/overlay/usr/bin/apertis-pkg-merge-updates
@@ -16,13 +16,12 @@ import sys
 import tempfile
 import urllib.request
 
-from sh import dch
 from sh.contrib import git
 
 from pathlib import Path
 
 from debian.debian_support import Version
-from debian.changelog import Changelog
+from debian.changelog import Changelog, VersionError, format_date
 
 APERTIS_CI_NAME = 'Apertis CI'
 APERTIS_CI_EMAIL = 'devel@lists.apertis.org'
@@ -72,6 +71,24 @@ def get_current_branch_name():
     branch = git('rev-parse', '-q', '--verify', '--symbolic-full-name', 'HEAD', _ok_code=[0, 1]).strip('\n')
     return branch.replace('refs/heads/', '', 1)
 
+def bump_version(version: Version, changes: list, release: bool = False):
+    with Path("debian/changelog") as f:
+        ch = Changelog(f.read_text())
+        if version <= ch.version:
+            raise VersionError("The new version must be greater than the last one.")
+        ch.new_block(package=ch.package,
+                     version=version,
+                     distributions='apertis' if release else 'UNRELEASED',
+                     urgency='medium',
+                     author=('%s <%s>' % (APERTIS_CI_NAME, APERTIS_CI_EMAIL)),
+                     date=format_date())
+        ch.add_change('')
+        for change in changes:
+            ch.add_change(f'  * {change}')
+        ch.add_change('')
+        f.write_text(str(ch))
+        git.add('-f', f)
+
 def main():
   parser = argparse.ArgumentParser(description='Merge updates from the upstream repositories to the derivative branch')
   parser.add_argument('--package', dest='package', type=str, help='the package name (e.g. glib2.0)') # TODO: figure this out from the repo
@@ -99,14 +116,26 @@ def main():
     print(e)
     sys.exit(1)
 
-  o = git('diff', '--exit-code', args.upstream, 'debian/changelog', _ok_code=[0,1])
+  o = git('diff', '--exit-code', args.upstream, ':!debian/changelog', ':!debian/apertis/*', _ok_code=[0,1])
   if o.exit_code == 1:
-    # debian/changelog isn't as is from debian, so someone should
+    # we carry some changes in addition to changelog entries
+    # and metadata under debian/apertis, so someone should
     # re-summarize the remaining changes
     version = upstream_version.full_version + 'co1'
-    dch('--no-auto-nmu', f"--newversion={version}",
-        'PLEASE SUMMARIZE remaining Apertis changes')
-    git('commit', '--amend', '--no-edit', 'debian/changelog')
+    bump_version(
+        version,
+        ['PLEASE SUMMARIZE remaining Apertis changes'],
+        release=False
+    )
+  else:
+    # no changes, but we add a suffix anyway
+    version = upstream_version.full_version + 'co0'
+    bump_version(
+        version,
+        [f'Sync from Debian {args.upstream}.'],
+        release=True
+    )
+  git('commit', '--amend', '--no-edit', 'debian/changelog')
 
 if __name__ == '__main__':
   main()
-- 
GitLab