From 9982ed8cbe7b383f2200443e91ba791a861c4e48 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Mar 29 2020 11:45:16 +0000 Subject: 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 --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index cf95f80..aa1201b 100644 --- a/pagure/ui/repo.py +++ b/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, diff --git a/tests/__init__.py b/tests/__init__.py index 165c37d..8048fee 100644 --- a/tests/__init__.py +++ b/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() diff --git a/tests/test_pagure_flask_ui_repo_view_history.py b/tests/test_pagure_flask_ui_repo_view_history.py index 1c53f4c..36e7f11 100644 --- a/tests/test_pagure_flask_ui_repo_view_history.py +++ b/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( + "Add content to file foo/bar/sources", 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)