#4790 Three different bug fixes
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

file modified
+22 -3
@@ -322,6 +322,14 @@ 

          if isinstance(commit, pygit2.Tag):

              commit = commit.peel(pygit2.Commit)

              branchname = commit.oid.hex

+         elif isinstance(commit, pygit2.Blob):

+             try:

+                 commit = commit.peel(pygit2.Commit)

+                 branchname = commit.oid.hex

+             except Exception:

+                 flask.abort(

+                     404, description="Invalid branch/identifier provided"

+                 )

      elif not repo_obj.is_empty and not repo_obj.head_is_unborn:

          branch = repo_obj.lookup_branch(repo_obj.head.shorthand)

          commit = branch.peel(pygit2.Commit)
@@ -768,8 +776,13 @@ 

                      404, description="Cannot find specified identifier"

                  )

  

-     if isinstance(commit, pygit2.Tag):

-         commit = commit.peel(pygit2.Commit)

+     try:

+         if isinstance(commit, pygit2.Tag):

+             commit = commit.peel(pygit2.Commit)

+         elif isinstance(commit, pygit2.Blob):

+             commit = commit.peel(pygit2.Commit)

+     except Exception:

+         flask.abort(404, description="Invalid identified provided")

  

      content = __get_file_in_tree(

          repo_obj, commit.tree, filename.split("/"), bail_on_tree=True
@@ -816,10 +829,16 @@ 

      repo_obj = flask.g.repo_obj

  

      branchname = flask.request.args.get("identifier")

- 

      if repo_obj.is_empty:

          flask.abort(404, description="Empty repo cannot have a file")

  

+     if not branchname:

+         try:

+             branch = repo_obj.lookup_branch(repo_obj.head.shorthand)

+             branchname = branch.branch_name

+         except pygit2.GitError:

+             flask.abort(400, description="Invalid repository")

+ 

      try:

          log = pagure.lib.repo.PagureRepo.log(

              flask.g.reponame,

file modified
+8 -3
@@ -1000,6 +1000,7 @@ 

  def add_content_to_git(

      folder,

      branch="master",

+     folders=None,

      filename="sources",

      content="foo",

      message=None,
@@ -1012,9 +1013,13 @@ 

      )

  

      # Create a file in that git repo

-     with open(

-         os.path.join(newfolder, filename), "a", encoding="utf-8"

-     ) as stream:

+     if folders:

+         if not os.path.exists(os.path.join(newfolder, folders)):

+             os.makedirs(os.path.join(newfolder, folders))

+         filename = os.path.join(folders, filename)

+ 

+     filepath = os.path.join(newfolder, filename)

+     with open(filepath, "a", encoding="utf-8") as stream:

          stream.write("%s\n" % content)

      repo.index.add(filename)

      repo.index.write()

@@ -31,6 +31,7 @@ 

  import pagure.lib.query

  import tests

  from pagure.lib.repo import PagureRepo

+ from pagure.utils import __get_file_in_tree as get_file_in_tree

  

  

  class PagureFlaskRepotests(tests.Modeltests):
@@ -2282,6 +2283,28 @@ 

          self.assertIn("<title>Commits - test - Pagure</title>", output_text)

          self.assertEqual(output_text.count('<span id="commit-actions">'), 1)

  

+     def test_view_commits_from_blob(self):

+         """ Test the view_commits endpoint given a blob. """

+ 

+         tests.create_projects(self.session)

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

+ 

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

+ 

+         # Retrieve the blob of the `sources` file in head

+         repo_obj = pygit2.Repository(

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

+         )

+         commit = repo_obj[repo_obj.head.target]

+         content = get_file_in_tree(

+             repo_obj, commit.tree, ["sources"], bail_on_tree=True

+         )

+ 

+         output = self.app.get("/test/commits/%s" % content.oid.hex)

+         self.assertEqual(output.status_code, 404)

+         output_text = output.get_data(as_text=True)

+         self.assertIn("Invalid branch/identifier provided", output_text)

+ 

      def test_view_commit_from_tag(self):

          """ Test the view_commit endpoint given a tag. """

  

@@ -20,6 +20,7 @@ 

  

  import tests

  import pagure.lib.model

+ from pagure.utils import __get_file_in_tree as get_file_in_tree

  

  

  class PagureFlaskRepoViewBlameFileSimpletests(tests.Modeltests):
@@ -204,6 +205,24 @@ 

          data = self.regex.findall(output_text)

          self.assertEqual(len(data), 1)

  

+     def test_view_blame_file_on_blob(self):

+         """ Test the view_blame_file endpoint """

+         # Retrieve the blob of the `sources` file in head

+         repo_obj = pygit2.Repository(

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

+         )

+         commit = repo_obj[repo_obj.head.target]

+         content = get_file_in_tree(

+             repo_obj, commit.tree, ["sources"], bail_on_tree=True

+         )

+ 

+         output = self.app.get(

+             "/test/blame/sources?identifier=%s" % content.oid.hex

+         )

+         self.assertEqual(output.status_code, 404)

+         output_text = output.get_data(as_text=True)

+         self.assertIn("Invalid identified provided", output_text)

+ 

      def test_view_blame_file_binary(self):

          """ Test the view_blame_file endpoint """

          # Add binary content

@@ -234,6 +234,21 @@ 

          output_text = output.get_data(as_text=True)

          self.assertIn("No history could be found for this file", output_text)

  

+     def test_view_history_file_existing_folder(self):

+         """ Test the view_history_file endpoint """

+         tests.add_content_to_git(

+             os.path.join(self.path, "repos", "test.git"), folders="foo/bar"

+         )

+ 

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

+         self.assertEqual(output.status_code, 200)

+         output_text = output.get_data(as_text=True)

+         self.assertIn(

+             "<strong>Add content to file foo/bar/sources</strong>", output_text

+         )

+         data = self.regex.findall(output_text)

+         self.assertEqual(len(data), 1)

+ 

      def test_view_history_file_unborn_head_no_identifier(self):

          repo_obj = pygit2.Repository(

              os.path.join(self.path, "repos", "test.git")
@@ -243,4 +258,4 @@ 

          output = self.app.get("/test/history/sources")

          self.assertEqual(output.status_code, 400)

          output_text = output.get_data(as_text=True)

-         self.assertIn("No history could be found for this file", output_text)

+         self.assertIn("Invalid repository", output_text)