#5051 Allow deleting the master branch when it is not the default branch
Merged 3 years ago by pingou. Opened 3 years ago by pingou.

file modified
+8 -2
@@ -2694,9 +2694,15 @@ 

      if six.PY2:

          branchname = branchname.encode("utf-8")

  

-     if branchname == "master":

+     default_branchname = "master"

Future note: we should make the default implicit branch name globally configurable, like other forges have done.

+     if not repo_obj.is_empty and not repo_obj.head_is_unborn:

+         default_branchname = repo_obj.head.shorthand

+ 

+     if branchname == default_branchname:

          flask.abort(

-             403, description="You are not allowed to delete the master branch"

+             403,

+             description="You are not allowed to delete the default "

+             "branch: %s" % default_branchname,

          )

  

      if branchname not in repo_obj.listall_branches():

@@ -5770,7 +5770,7 @@ 

              self.assertEqual(output.status_code, 403)

              output_text = output.get_data(as_text=True)

              self.assertIn(

-                 "<p>You are not allowed to delete the master branch</p>",

+                 "<p>You are not allowed to delete the default branch: master</p>",

                  output_text,

              )

  
@@ -5864,6 +5864,49 @@ 

              output_text = output.get_data(as_text=True)

              self.assertNotIn('<form id="delete_branch_form-☃️"', output_text)

  

+     def test_delete_branch_master(self):

+         """ Test the delete_branch endpoint with the master branch. """

+ 

+         tests.create_projects(self.session)

+         tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)

+         project = pagure.lib.query._get_project(self.session, "test")

+ 

+         user = tests.FakeUser(username="pingou")

+         with tests.user_set(self.app.application, user):

+ 

+             # Add a branch that we can delete

+             path = os.path.join(self.path, "repos", "test.git")

+             tests.add_content_git_repo(path)

+             repo = pygit2.Repository(path)

+             branchname = "main"

+             repo.create_branch(branchname, repo.head.peel())

+ 

+             # Make that branch be the default one:

+             pagure.lib.git.git_set_ref_head(project=project, branch=branchname)

+ 

+             # Check before deletion

+             output = self.app.get("/test")

+             self.assertEqual(output.status_code, 200)

+ 

+             output = self.app.get("/test/branches")

+             output_text = output.get_data(as_text=True)

+             self.assertIn('<form id="delete_branch_form-master"', output_text)

+             self.assertNotIn('<form id="delete_branch_form-main"', output_text)

+ 

+             # Delete the branch

+             output = self.app.post(

+                 "/test/b/master/delete", follow_redirects=True

+             )

+             self.assertEqual(output.status_code, 200)

+ 

+             # Check after deletion

+             output = self.app.get("/test/branches")

+             output_text = output.get_data(as_text=True)

+             self.assertNotIn(

+                 '<form id="delete_branch_form-master"', output_text

+             )

+             self.assertNotIn('<form id="delete_branch_form-main"', output_text)

+ 

      @patch.dict("pagure.config.config", {"ALLOW_DELETE_BRANCH": False})

      def test_delete_branch_disabled_in_ui(self):

          """ Test that the delete branch button doesn't show when the feature

rebased onto 021b6099e5e5993bd6da2671a8ba5137e4bbe382

3 years ago

rebased onto 9d619d28b5c88634ba443a904a14e411ee127b82

3 years ago

rebased onto 24efe4d

3 years ago

Future note: we should make the default implicit branch name globally configurable, like other forges have done.

Thanks for the review :)

Pull-Request has been merged by pingou

3 years ago