[damned-lies] Refs #245 - Implement Git branch renaming



commit 7860ef2e294aace56d8c53e70c17ccfcefa39c31
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Sep 18 12:07:04 2021 +0200

    Refs #245 - Implement Git branch renaming

 stats/models.py      |  4 ++++
 stats/repos.py       | 11 +++++++++++
 stats/tests/tests.py | 12 ++++++++++++
 3 files changed, 27 insertions(+)
---
diff --git a/stats/models.py b/stats/models.py
index 89162728..1d3197fc 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -265,7 +265,11 @@ class Branch(models.Model):
                 )
 
     def save(self, update_statistics=True, **kwargs):
+        old_name = Branch.objects.get(pk=self.pk).name if self.pk else None
         super().save(**kwargs)
+        if old_name and old_name != self.name:
+            with ModuleLock(self.module):
+                self._repo.rename(old_name, self.name)
         if update_statistics and not self.module.archived:
             # The update command is launched asynchronously in a separate thread
             upd_thread = threading.Thread(target=self.update_stats, kwargs={'force': True})
diff --git a/stats/repos.py b/stats/repos.py
index 8bbe1726..094e2de7 100644
--- a/stats/repos.py
+++ b/stats/repos.py
@@ -35,6 +35,9 @@ class RepoBase:
     def cherry_pick(self, commit_hash):
         raise NotImplementedError
 
+    def rename(self, old_name, new_name):
+        raise NotImplementedError
+
     def remove(self):
         raise NotImplementedError
 
@@ -114,6 +117,14 @@ class GitRepo(RepoBase):
             run_shell_command(['git', 'cherry-pick', '--abort'], cwd=commit_dir)
             return False
 
+    def rename(self, old_name, new_name):
+        commit_dir = self.branch.co_path
+        run_shell_command(['git', 'checkout', old_name], cwd=commit_dir)
+        run_shell_command(['git', 'branch', '-m', old_name, new_name], cwd=commit_dir)
+        run_shell_command(['git', 'fetch'], cwd=commit_dir)
+        run_shell_command(['git', 'branch', '--unset-upstream'], cwd=commit_dir)
+        run_shell_command(['git', 'branch', '--set-upstream-to', f'origin/{new_name}'], cwd=commit_dir)
+
     def remove(self):
         wdir = str(self.branch.co_path)
         if os.access(wdir, os.W_OK):
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index aeceaebf..c6fa929f 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -223,6 +223,18 @@ class ModuleTestCase(TestCase):
         branch = Branch.objects.get(name='master', module__name='gnome-hello')
         self.assertTrue(branch._repo.exists())
 
+    @test_scratchdir
+    def test_rename_branch(self):
+        branch = Branch.objects.get(name='master', module__name='gnome-hello')
+        _, out, _ = run_shell_command(['git', 'branch', '--list'], cwd=branch.co_path)
+        self.assertNotIn('gnome-hello-1-4', out)
+        branch.name = 'gnome-hello-1-4'
+        import pdb; pdb.set_trace()
+        branch.save(update_statistics=False)
+        self.branch.co_path
+        _, out, _ = run_shell_command(['git', 'branch', '--list'], cwd=branch.co_path)
+        self.assertIn('gnome-hello-1-4', out)
+
     @test_scratchdir
     def test_delete_branch(self):
         """ Deleting the master branch of a git module deletes the checkout dir """


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