From a58725822651f791b2e74fe40a6e85b3b7e72aca Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Mon, 10 Aug 2020 11:01:37 +0300
Subject: [PATCH] Fix changelog trimming to work relative to newest
existing
 entry (#1301)

%_changelog_trimtime is an absolute timestamp which needs to be
%constantly
pushed forward to preserve the same relative age, and will start
trimming
entries from unchanged packages until none are left, leading to
unexpected
and confusing behavior (RhBug:1722806, ...)

It's better to trim by age relative to newest changelog entry. This way
the
number of trimmed entries will not change unless the spec changes, and
at
least one entry is always preserved. Introduce a new %_changelog_trimage
macro for this and mark the broken by design %_changelog_trimtime as
deprecated, but autoconvert an existing trimtime into relative for now.

As a seemingly unrelated change, move the "time" variable declaration
to a narrower scope to unmask the time() function for use on entry.

Fixes: #1301
---
 build/parseChangelog.c | 16 +++++++++++++++-
 macros.in              |  6 +++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/build/parseChangelog.c b/build/parseChangelog.c
index ad6d834..22f445e 100644
--- a/build/parseChangelog.c
+++ b/build/parseChangelog.c
@@ -200,18 +200,26 @@ static rpmRC addChangelog(Header h, ARGV_const_t sb)
     rpmRC rc = RPMRC_FAIL; /* assume failure */
     char *s, *sp;
     int i;
-    time_t time;
+    time_t firstTime = 0;
     time_t lastTime = 0;
     time_t trimtime = rpmExpandNumeric("%{?_changelog_trimtime}");
+    time_t trimage = rpmExpandNumeric("%{?_changelog_trimage}");
     char *date, *name, *text, *next;
     int date_words;      /* number of words in date string */
 
+    /* Convert _changelog_trimtime to age for backwards compatibility */
+    if (trimtime && !trimage) {
+	trimage = time(NULL) - trimtime;
+	trimtime = 0;
+    }
+
     s = sp = argvJoin(sb, "");
 
     /* skip space */
     SKIPSPACE(s);
 
     while (*s != '\0') {
+	time_t time;
 	if (*s != '*') {
 	    rpmlog(RPMLOG_ERR, _("%%changelog entries must start with *\n"));
 	    goto exit;
@@ -235,6 +243,12 @@ static rpmRC addChangelog(Header h, ARGV_const_t sb)
 	    rpmlog(RPMLOG_ERR, _("bad date in %%changelog: %s\n"), date);
 	    goto exit;
 	}
+	/* Changelog trimming is always relative to first entry */
+	if (!firstTime) {
+	    firstTime = time;
+	    if (trimage)
+		trimtime = firstTime - trimage;
+	}
 	if (lastTime && lastTime < time) {
 	    rpmlog(RPMLOG_ERR,
 		     _("%%changelog not in descending chronological order\n"));
diff --git a/macros.in b/macros.in
index 8619c13..5b45d73 100644
--- a/macros.in
+++ b/macros.in
@@ -230,8 +230,12 @@ package or when debugging this package.\
 #	The path to the gzip executable (legacy, use %{__gzip} instead).
 %_gzipbin		%{__gzip}
 
+#	Maximum age of preserved changelog entries in binary packages,
+#	relative to newest existing entry. Unix timestamp format.
+%_changelog_trimage	0
+
 #	The Unix time of the latest kept changelog entry in binary packages.
-#	Any older entry is not packaged in binary packages.
+#	DEPRACATED, use %_changelog_trimage instead.
 %_changelog_trimtime	0
 
 #	If true, set the SOURCE_DATE_EPOCH environment variable
-- 
2.27.0

