Browse Source

When viewing file's history, use the default branch if needed

If the user does not specify a branch, we will show the history
in the default branch so define the branchname variable so it
can be used in the templates

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Pierre-Yves Chibon 4 years ago
parent
commit
9982ed8cbe
3 changed files with 31 additions and 5 deletions
  1. 7 1
      pagure/ui/repo.py
  2. 8 3
      tests/__init__.py
  3. 16 1
      tests/test_pagure_flask_ui_repo_view_history.py

+ 7 - 1
pagure/ui/repo.py

@@ -829,10 +829,16 @@ def view_history_file(repo, filename, username=None, namespace=None):
     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,

+ 8 - 3
tests/__init__.py

@@ -1000,6 +1000,7 @@ def add_tag_git_repo(folder, tagname, obj_hash, message):
 def add_content_to_git(
     folder,
     branch="master",
+    folders=None,
     filename="sources",
     content="foo",
     message=None,
@@ -1012,9 +1013,13 @@ def add_content_to_git(
     )
 
     # 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()

+ 16 - 1
tests/test_pagure_flask_ui_repo_view_history.py

@@ -234,6 +234,21 @@ class PagureFlaskRepoViewHistoryFiletests(tests.Modeltests):
         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 @@ class PagureFlaskRepoViewHistoryFiletests(tests.Modeltests):
         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)