From 8c50d0a446c646f93a44724078044ea1a2b0fe75 Mon Sep 17 00:00:00 2001
From: Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Date: Mon, 4 Jan 2010 12:18:24 -0200
Subject: [PATCH] Introduce the --reverse option to list entries in the reverse order. Closes: #172113

---
 apt-listchanges.py             |    2 +-
 apt-listchanges/ALCConfig.py   |    7 +++++--
 apt-listchanges/DebianFiles.py |   17 ++++++++++++-----
 debian/changelog               |    7 ++++++-
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/apt-listchanges.py b/apt-listchanges.py
index bfca1da..bf734a8 100755
--- a/apt-listchanges.py
+++ b/apt-listchanges.py
@@ -132,7 +132,7 @@ def main():
                                                                       srcversion))
             continue
 
-        (news, changelog) = pkg.extract_changes(config.which, fromversion)
+        (news, changelog) = pkg.extract_changes(config.which, fromversion, config.reverse)
 
         if news or changelog:
             found[srcpackage] = 1
diff --git a/apt-listchanges/ALCConfig.py b/apt-listchanges/ALCConfig.py
index 221909e..1f490a8 100644
--- a/apt-listchanges/ALCConfig.py
+++ b/apt-listchanges/ALCConfig.py
@@ -47,6 +47,7 @@ class ALCConfig:
         self.which = 'both'
         self.allowed_which = ('both', 'news', 'changelogs')
         self.since = None
+        self.reverse = False
 
     def read(self, file):
         self.parser = ConfigParser.ConfigParser()
@@ -57,7 +58,7 @@ class ALCConfig:
             for option in self.parser.options(self.profile):
                 value = None
                 if self.parser.has_option(self.profile, option):
-                    if option in ('confirm', 'run', 'show_all', 'headers', 'verbose'):
+                    if option in ('confirm', 'run', 'show_all', 'headers', 'verbose', 'reverse'):
                         value = self.parser.getboolean(self.profile, option)
                     else:
                         value = self.parser.get(self.profile, option)
@@ -81,7 +82,7 @@ class ALCConfig:
             (optlist, args) = getopt.getopt(argv[1:], 'vf:s:cah', [
                 "apt", "verbose", "frontend=", "email-address=", "confirm",
                 "all", "headers", "save_seen=", "since=", "debug", "which=",
-                "help", "profile="])
+                "help", "profile=", "reverse"])
         except getopt.GetoptError:
             return None
 
@@ -134,6 +135,8 @@ class ALCConfig:
                     sys.exit(1)
             elif opt == '--debug':
                 self.debug = 1
+            elif opt == '--reverse':
+                self.reverse = 1
 
         if self.email_address == 'none':
             self.email_address = None
diff --git a/apt-listchanges/DebianFiles.py b/apt-listchanges/DebianFiles.py
index 4bc9c58..4208f28 100644
--- a/apt-listchanges/DebianFiles.py
+++ b/apt-listchanges/DebianFiles.py
@@ -110,7 +110,7 @@ class Package:
         self.source  = pkgdata.source()
         self.Version = pkgdata.Version
 
-    def extract_changes(self, which, since_version=None):
+    def extract_changes(self, which, since_version=None, reverse=None):
         '''Extract changelog entries, news or both from the package.
         If since_version is specified, only return entries later than the specified version.
         returns a sequence of Changes objects.'''
@@ -128,7 +128,7 @@ class Package:
 
         tempdir = self.extract_contents(filenames)
 
-        find_first = lambda acc, fname: acc or self.read_changelog(os.path.join(tempdir, fname), since_version)
+        find_first = lambda acc, fname: acc or self.read_changelog(os.path.join(tempdir, fname), since_version, reverse)
 
         news       = reduce(find_first, news_filenames, None)
         changelog  = reduce(find_first, changelog_filenames + changelog_filenames_native, None)
@@ -151,7 +151,7 @@ class Package:
 
         return tempdir
 
-    def read_changelog(self, filename, since_version):
+    def read_changelog(self, filename, since_version, reverse=False):
         filenames = glob.glob(filename)
 
         fd = None
@@ -174,11 +174,14 @@ class Package:
             return None
 
         urgency = numeric_urgency('low')
-        changes = ''
+        entry = ''
+        entries = []
         is_debian_changelog = 0
         for line in fd.readlines():
             match = self.changelog_header.match(line)
             if match:
+                entries += [entry]
+                entry = ''
                 is_debian_changelog = 1
                 if since_version:
                     if apt_pkg.VersionCompare(match.group('version'),
@@ -187,11 +190,15 @@ class Package:
                                       urgency)
                     else:
                         break
-            changes += line
+            entry += line
 
         if not is_debian_changelog:
             return None
 
+        if reverse:
+            entries.reverse()
+        changes = "".join(entries)
+
         return Changes(self.source, changes, urgency)
 
     def _changelog_variations(self, filename):
diff --git a/debian/changelog b/debian/changelog
index f1e72e8..675790c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,13 @@
 apt-listchanges (2.84) UNRELEASED; urgency=low
 
+  [ Christian Perrier ]
   * Fix typo in Polish translation. Closes: #510618
 
- -- Christian Perrier <bubulle@debian.org>  Mon, 19 Oct 2009 22:59:10 +0200
+  [ Thadeu Lima de Souza Cascardo ]
+  * Introduce the --reverse option to list entries in the reverse order.
+    Closes: #172113:
+
+ -- Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>  Mon, 04 Jan 2010 12:17:21 -0200
 
 apt-listchanges (2.83+nmu1) unstable; urgency=low
 
-- 
1.6.5.7

