[damned-lies] Return latest branch if no head branch found



commit 1eb97f695113c4543c0e721589823aeeadee638c
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Sep 11 12:40:34 2021 +0200

    Return latest branch if no head branch found

 stats/models.py      |  9 ++++-----
 stats/tests/tests.py | 10 ++++++++++
 2 files changed, 14 insertions(+), 5 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index 27d7e502..89162728 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -165,11 +165,10 @@ class Module(models.Model):
 
     def get_head_branch(self):
         """ Returns the HEAD (trunk, master, ...) branch of the module """
-        branch = self.branch_set.filter(name__in=BRANCH_HEAD_NAMES)
-        if branch:
-            # First one (if many something is wrong :-/)
-            return branch[0]
-        return None
+        branch = self.branch_set.filter(name__in=BRANCH_HEAD_NAMES).first()
+        if not branch:
+            branch = self.get_branches(reverse=False)[0]
+        return branch
 
     def can_edit_branches(self, user):
         """ Returns True for superusers, users with adequate permissions or maintainers of the module """
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index d5feab6d..0c0a56b1 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -116,6 +116,16 @@ class ModuleTestCase(TestCase):
             "%s is not the expected 'git clone' command." % cmds[0]
         )
 
+    def test_head_branch(self):
+        self.assertEqual(self.mod.get_head_branch().name, 'master')
+        Branch.objects.filter(module=self.mod, name='master').delete()
+        Branch.objects.bulk_create([
+            Branch(module=self.mod, name='gnome-3-18'),
+            Branch(module=self.mod, name='gnome-41'),
+            Branch(module=self.mod, name='gnome-2-1'),
+        ])
+        self.assertEqual(self.mod.get_head_branch().name, 'gnome-41')
+
     def test_branch_methods(self):
         branch = Branch(module=self.mod, name='master')
         self.assertTrue(branch.is_head())


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