[damned-lies] Sort branch names so as gnome-3-2 is lower than gnome-3-10



commit 86757b3c2cf5f7275dcdedfaf69d18521b7f5ba7
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Feb 4 23:23:40 2016 +0100

    Sort branch names so as gnome-3-2 is lower than gnome-3-10

 stats/models.py      |   14 +++++++++++++-
 stats/tests/tests.py |    9 +++++++++
 2 files changed, 22 insertions(+), 1 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index cb90c30..de79c35 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -272,7 +272,8 @@ class Branch(models.Model):
             return True
         elif other.name in BRANCH_HEAD_NAMES:
             return False
-        return (-self.weight, self.name) > (-other.weight, other.name)
+        # Splitting so gnome-3-2 < gnome-3-10
+        return ((-self.weight,) + split_name(self.name)) > ((-other.weight,) + split_name(other.name))
 
     @property
     def img_url_prefix(self):
@@ -1900,3 +1901,14 @@ class InformationArchived(models.Model):
 
     class Meta:
         db_table = 'information_archived'
+
+
+# Utilities to properly sort branch names like gnome-3-2 < gnome-3-10
+def try_int(value):
+    try:
+        return int(value)
+    except ValueError:
+        return value
+
+def split_name(value):
+    return tuple(try_int(part) for part in re.split("[-\.]", value))
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 06b9b84..f9c616c 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -237,6 +237,15 @@ class ModuleTestCase(TestCase):
             [b.name for b in self.mod.get_branches(reverse=True)],
             ['p-branch', 'a-branch', 'master']
         )
+        # "Clever" sorting of alphanumeric names
+        b3 = Branch(name='gnome-3-2', module=self.mod)
+        b3.save(update_statistics=False)
+        b4 = Branch(name='gnome-3-10', module=self.mod)
+        b4.save(update_statistics=False)
+        self.assertEqual(
+            [b.name for b in sorted(self.mod.branch_set.all())],
+            ['master', 'a-branch', 'p-branch', 'gnome-3-10', 'gnome-3-2']
+        )
 
     @test_scratchdir
     def test_string_frozen_mail(self):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]