This is an automated email from the ASF dual-hosted git repository.

gcruz pushed a commit to branch gc/8518
in repository https://gitbox.apache.org/repos/asf/allura.git


The following commit(s) were added to refs/heads/gc/8518 by this push:
     new a6b94ae6d [#8518] added test and updated code
a6b94ae6d is described below

commit a6b94ae6d01431a641e6ec7ab1e720432b2dfaf2
Author: Guillermo Cruz <[email protected]>
AuthorDate: Tue Aug 15 13:25:37 2023 -0600

    [#8518] added test and updated code
---
 Allura/allura/tasks/repo_tasks.py                | 15 +++++++++++++--
 ForgeGit/forgegit/tests/model/test_repository.py | 14 +++++++++++++-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/Allura/allura/tasks/repo_tasks.py 
b/Allura/allura/tasks/repo_tasks.py
index 3177b50b8..296358fde 100644
--- a/Allura/allura/tasks/repo_tasks.py
+++ b/Allura/allura/tasks/repo_tasks.py
@@ -26,6 +26,7 @@ from allura.lib.decorators import task
 from allura.lib.repository import RepositoryApp
 from allura.lib.utils import skip_mod_date
 import git
+from git import SymbolicReference
 
 
 @task
@@ -183,6 +184,16 @@ def determine_mr_commits(merge_request_id):
 
 @task
 def update_head_reference(fs_path, branch_name):
-    _git = git.Repo(fs_path, odbt=git.GitCmdObjectDB)
-    _git.head.reference = branch_name
+    repo = git.Repo(fs_path, odbt=git.GitCmdObjectDB)
+    if not repo.head.is_detached:
+        new_branch = [ref for ref in repo.refs if ref.name == branch_name]
+        _ref = SymbolicReference.create(repo, 'HEAD', repo.head.reference)
+        _ref.reference = new_branch[0]
+    else:
+        # it is detached there's no refs in repo.refs default to first
+        repo.head.reference = repo.refs[0]
+        #lookup for new default branch
+        new_branch = [ref for ref in repo.refs if ref.name == branch_name]
+        _ref = SymbolicReference.create(repo, 'HEAD', repo.head.reference)
+        _ref.reference = new_branch[0]
 
diff --git a/ForgeGit/forgegit/tests/model/test_repository.py 
b/ForgeGit/forgegit/tests/model/test_repository.py
index 0dd8a9fc0..ce2118f7e 100644
--- a/ForgeGit/forgegit/tests/model/test_repository.py
+++ b/ForgeGit/forgegit/tests/model/test_repository.py
@@ -33,7 +33,7 @@ from testfixtures import TempDirectory
 
 from alluratest.controller import setup_basic_test, setup_global_objects
 from allura.lib import helpers as h
-from allura.tasks.repo_tasks import tarball
+from allura.tasks.repo_tasks import tarball, update_head_reference
 from allura.tests import decorators as td
 from allura.tests.model.test_repo import RepoImplTestBase
 from allura import model as M
@@ -605,6 +605,18 @@ By Dave Brondsema''' in text_body
         self.repo.default_branch_name = 'zz'
         assert self.repo.get_default_branch(('main', 'master')) == 'zz'
 
+    def test_update_default_branch(self):
+        repo_dir = pkg_resources.resource_filename('forgegit', 
'tests/data/testgit.git')
+        repo = mock.Mock(full_fs_path=repo_dir)
+        repo.__ming__ = mock.Mock()
+        impl = GM.git_repo.GitImplementation(repo)
+        try:
+            update_head_reference(self.repo.full_fs_path, 'zz')
+            assert impl._git.head.reference.name == 'zz'
+        finally:
+            update_head_reference(self.repo.full_fs_path, 'master')
+            assert impl._git.head.reference.name == 'master'
+
     def test_default_branch_non_standard_unset(self):
         with mock.patch.object(self.repo, 'get_branches') as gb,\
                 mock.patch.object(self.repo, 'set_default_branch') as set_db:

Reply via email to