From 24d031707e4cbfc5e585f5a9b63c37faea1ee450 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Dec 03 2020 19:46:43 +0000 Subject: Ensure a fork project has the same default branch as its parent Otherwise we end up in weird situations where the default branch differs between the two projects which ends up being quite confusing to the users. Fixes https://pagure.io/pagure/issue/5052 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/tasks.py b/pagure/lib/tasks.py index b3a6f6d..8be0769 100644 --- a/pagure/lib/tasks.py +++ b/pagure/lib/tasks.py @@ -514,6 +514,11 @@ def fork( with open(http_clone_file, "w"): pass + # Finally set the default branch to be the same as the parent + repo_from_obj = pygit2.Repository(repo_from.repopath("main")) + repo_to_obj = pygit2.Repository(repo_to.repopath("main")) + repo_to_obj.set_head(repo_from_obj.lookup_reference("HEAD").target) + pagure.lib.notify.log( repo_to, topic="project.forked", diff --git a/tests/test_pagure_flask_ui_fork.py b/tests/test_pagure_flask_ui_fork.py index e9b91f3..6cabfaa 100644 --- a/tests/test_pagure_flask_ui_fork.py +++ b/tests/test_pagure_flask_ui_fork.py @@ -2798,6 +2798,58 @@ index 0000000..2a552bb ) self.assertEqual(output.status_code, 200) + @patch("pagure.lib.notify.send_email", MagicMock(return_value=True)) + def test_fork_project_non_master_default(self): + """ Test the fork_project endpoint with a project whose default branch + is not master. """ + + tests.create_projects(self.session) + for folder in ["docs", "tickets", "requests", "repos"]: + tests.create_projects_git( + os.path.join(self.path, folder), bare=True + ) + path = os.path.join(self.path, "repos", "test.git") + tests.add_content_git_repo(path) + project = pagure.lib.query.get_authorized_project(self.session, "test") + + # Check before that the master branch is the default one - shown in the + # default page + output = self.app.get("/test") + self.assertEqual(output.status_code, 200) + output_text = output.get_data(as_text=True) + self.assertIn( + 'mastermainmain