From 1ff64e4c5d78d4f293675b8745dfb7bbb72f8d65 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 13:55:13 +0000
Subject: [PATCH 1/6] Correcting links to commits page from any 'commit page' if branch is not master


---

diff --git a/pagure/templates/commits.html b/pagure/templates/commits.html
index c824857..3bc23b7 100644
--- a/pagure/templates/commits.html
+++ b/pagure/templates/commits.html
@@ -110,7 +110,7 @@
       <div class="list-group m-t-1">
         {% for commit in last_commits %}
           <a href="{{ url_for('view_commit', username=username,
-                  repo=repo.name, commitid=commit.hex) }}"
+                  repo=repo.name, commitid=commit.hex, branch=branchname) }}"
               class="list-group-item {%
                 if diff_commits and commit.oid.hex in diff_commits %}p-l-3{%
                 endif %}" style="position:relative;">
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index 7979977..13312eb 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -600,6 +600,20 @@ def view_commit(repo, commitid, username=None):
 
     repo_obj = pygit2.Repository(reponame)
 
+    branchname = flask.request.args.get('branch', None)
+
+    if branchname and branchname not in repo_obj.listall_branches():
+        flask.abort(404, 'Branch no found')
+
+    if branchname:
+        branch = repo_obj.lookup_branch(branchname)
+    elif not repo_obj.is_empty and not repo_obj.head_is_unborn:
+        branch = repo_obj.lookup_branch(repo_obj.head.shorthand)
+        branchname = branch.branch_name
+    else:
+        branch = None
+        branchname = None
+
     try:
         commit = repo_obj.get(commitid)
     except ValueError:
@@ -621,6 +635,7 @@ def view_commit(repo, commitid, username=None):
         'commit.html',
         select='commits',
         repo=repo,
+        branchname=branchname,
         username=username,
         repo_admin=is_repo_admin(repo),
         commitid=commitid,

From a10b3fab74289dfbc5751436e0bb40c1ced76c6f Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 14:06:19 +0000
Subject: [PATCH 2/6] Add unit tests for view commit when branch name is provided


---

diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index 0adf303..fda50dd 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -1419,6 +1419,13 @@ class PagureFlaskRepotests(tests.Modeltests):
             '<span style="color: #800080; font-weight: bold">'
             '@@ -0,0 +1,3 @@</span>' in output.data)
 
+        #View the commit when branch name is provided
+        output = self.app.get('/test/c/%s?branch=master' % commit.oid.hex)
+        self.assertEqual(output.status_code, 200)
+        self.assertTrue(
+            '<a class="active nav-link" href="/test/commits/master">'
+            in output.data)
+
         # Add a fork of a fork
         item = pagure.lib.model.Project(
             user_id=1,  # pingou
@@ -1463,6 +1470,14 @@ class PagureFlaskRepotests(tests.Modeltests):
         self.assertEqual(output.status_code, 404)
         self.assertIn('<p>Project not found</p>', output.data)
 
+        #View the commit of the fork when branch name is provided
+        output = self.app.get('/fork/pingou/test3/c/%s?branch=master' % commit.oid.hex)
+        self.assertEqual(output.status_code, 200)
+        self.assertTrue(
+            '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
+            in output.data)
+
+
     def test_view_commit_patch(self):
         """ Test the view_commit_patch endpoint. """
 

From d3485c24f207938cad7eebeeb695742aeb05842c Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 14:28:58 +0000
Subject: [PATCH 3/6] Do not abort if branch name is wrong while viewing a commit


---

diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index 13312eb..8a4cbfb 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -603,7 +603,7 @@ def view_commit(repo, commitid, username=None):
     branchname = flask.request.args.get('branch', None)
 
     if branchname and branchname not in repo_obj.listall_branches():
-        flask.abort(404, 'Branch no found')
+        branchname = None
 
     if branchname:
         branch = repo_obj.lookup_branch(branchname)

From b854492c9b52f2ab0b5f41fc2e7bbd4928b5c5a7 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 14:37:38 +0000
Subject: [PATCH 4/6] Add unit tests for view commit in case the branch name is wrong


---

diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index fda50dd..9b29c91 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -1426,6 +1426,13 @@ class PagureFlaskRepotests(tests.Modeltests):
             '<a class="active nav-link" href="/test/commits/master">'
             in output.data)
 
+        #View the commit when branch name is wrong, show the commit
+        output = self.app.get('/test/c/%s?branch=abcxyz' % commit.oid.hex)
+        self.assertEqual(output.status_code, 200)
+        self.assertTrue(
+            '<a class="active nav-link" href="/test/commits/master">'
+            in output.data)
+
         # Add a fork of a fork
         item = pagure.lib.model.Project(
             user_id=1,  # pingou
@@ -1477,6 +1484,13 @@ class PagureFlaskRepotests(tests.Modeltests):
             '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
             in output.data)
 
+        #View the commit of the fork when branch name is wrong
+        output = self.app.get('/fork/pingou/test3/c/%s?branch=abcxyz' % commit.oid.hex)
+        self.assertEqual(output.status_code, 200)
+        self.assertTrue(
+            '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
+            in output.data)
+
 
     def test_view_commit_patch(self):
         """ Test the view_commit_patch endpoint. """

From 5910d2729eefb4c9b3347f823d01f454059b327e Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 15:04:35 +0000
Subject: [PATCH 5/6] Do not specifically go to default branch commits page, go to simple commits in general instead, if branch name is wrong


---

diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index 8a4cbfb..79aa0d4 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -605,15 +605,6 @@ def view_commit(repo, commitid, username=None):
     if branchname and branchname not in repo_obj.listall_branches():
         branchname = None
 
-    if branchname:
-        branch = repo_obj.lookup_branch(branchname)
-    elif not repo_obj.is_empty and not repo_obj.head_is_unborn:
-        branch = repo_obj.lookup_branch(repo_obj.head.shorthand)
-        branchname = branch.branch_name
-    else:
-        branch = None
-        branchname = None
-
     try:
         commit = repo_obj.get(commitid)
     except ValueError:

From 8e8c5a2fffb08f092e660188ae889367a5523ddd Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Jun 07 2016 15:05:57 +0000
Subject: [PATCH 6/6] modify test for unit test when branch name is wrong: go to commits page in general instead of default branch commits page


---

diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index 9b29c91..62f0462 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -1430,7 +1430,7 @@ class PagureFlaskRepotests(tests.Modeltests):
         output = self.app.get('/test/c/%s?branch=abcxyz' % commit.oid.hex)
         self.assertEqual(output.status_code, 200)
         self.assertTrue(
-            '<a class="active nav-link" href="/test/commits/master">'
+            '<a class="active nav-link" href="/test/commits">'
             in output.data)
 
         # Add a fork of a fork
@@ -1488,7 +1488,7 @@ class PagureFlaskRepotests(tests.Modeltests):
         output = self.app.get('/fork/pingou/test3/c/%s?branch=abcxyz' % commit.oid.hex)
         self.assertEqual(output.status_code, 200)
         self.assertTrue(
-            '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
+            '<a class="active nav-link" href="/fork/pingou/test3/commits">'
             in output.data)
 
 

