From 46abcf94e22a23b0c9c3b665a67c83e4a54bd259 Mon Sep 17 00:00:00 2001
From: Michael Watters <michael.watters@dart.biz>
Date: Apr 19 2018 12:34:21 +0000
Subject: [PATCH 1/2] Bug - Fix exceptions caused by missing merge object


Updated get_branches_of_commit function to avoid sending out
pagure exceptions whenever a commit with missing merge objects
is viewed.

Fixes #3163.

---

diff --git a/pagure/internal/__init__.py b/pagure/internal/__init__.py
index f8de97e..0cc64c2 100644
--- a/pagure/internal/__init__.py
+++ b/pagure/internal/__init__.py
@@ -491,10 +491,13 @@ def get_branches_of_commit():
             merge_commit = None
 
             if compare_branch:
-                merge_commit = repo_obj.merge_base(
+                merge_commit_obj = repo_obj.merge_base(
                     compare_branch.get_object().hex,
                     branch.get_object().hex
-                ).hex
+                )
+
+                if merge_commit_obj:
+                    merge_commit = merge_commit_obj.hex
 
             repo_commit = repo_obj[branch.get_object().hex]
 

From d19655abc4c30d94e1384c65bf6904287b02f1c3 Mon Sep 17 00:00:00 2001
From: Michael Watters <michael.watters@dart.biz>
Date: Apr 19 2018 12:35:30 +0000
Subject: [PATCH 2/2] [PATCH] Add tests for get_branches_of_commit of unrelated branches


---

diff --git a/tests/test_pagure_flask_internal.py b/tests/test_pagure_flask_internal.py
index c654989..d8b2969 100644
--- a/tests/test_pagure_flask_internal.py
+++ b/tests/test_pagure_flask_internal.py
@@ -1181,6 +1181,87 @@ class PagureFlaskInternaltests(tests.Modeltests):
             }
         )
 
+    def test_get_branches_of_commit_with_unrelated_branches(self):
+        ''' Test the get_branches_of_commit from the internal API. '''
+        tests.create_projects(self.session)
+        tests.create_projects_git(os.path.join(self.path, 'repos'))
+
+        user = tests.FakeUser(username='pingou')
+        with tests.user_set(self.app.application, user):
+            csrf_token = self.get_csrf()
+
+        # Create a git repo to play with
+        gitrepo = os.path.join(self.path, 'repos', 'test.git')
+        self.assertTrue(os.path.exists(gitrepo))
+        repo = pygit2.Repository(gitrepo)
+
+        # Create a file in that git repo
+        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
+            stream.write('foo\n bar')
+        repo.index.add('sources')
+        repo.index.write()
+
+        # Commits the files added
+        tree = repo.index.write_tree()
+        author = pygit2.Signature(
+            'Alice Author', 'alice@authors.tld')
+        committer = pygit2.Signature(
+            'Cecil Committer', 'cecil@committers.tld')
+        repo.create_commit(
+            'refs/heads/master',  # the name of the reference to update
+            author,
+            committer,
+            'Add sources file for testing',
+            # binary string representing the tree object ID
+            tree,
+            # list of binary strings representing parents of the new commit
+            []
+        )
+
+        first_commit = repo.revparse_single('HEAD')
+
+        # Edit the sources file again
+        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
+            stream.write('foo\n bar\nbaz\n boose')
+        repo.index.add('sources')
+        repo.index.write()
+
+        # Commits the files added, but unrelated with the first commit
+        tree = repo.index.write_tree()
+        author = pygit2.Signature(
+            'Alice Author', 'alice@authors.tld')
+        committer = pygit2.Signature(
+            'Cecil Committer', 'cecil@committers.tld')
+        commit = repo.create_commit(
+            'refs/heads/feature',  # the name of the reference to update
+            author,
+            committer,
+            'Add baz and boose to the sources\n\n There are more objects to '
+            'consider',
+            # binary string representing the tree object ID
+            tree,
+            # list of binary strings representing parents of the new commit
+            []
+        )
+        commit_hash = commit.hex
+
+        # All good
+        data = {
+            'repo': 'test',
+            'commit_id': commit_hash,
+            'csrf_token': csrf_token,
+        }
+        output = self.app.post('/pv/branches/commit/', data=data)
+        self.assertEqual(output.status_code, 200)
+        js_data = json.loads(output.data.decode('utf-8'))
+        self.assertDictEqual(
+            js_data,
+            {
+                u'code': u'OK',
+                u'branches': ['feature'],
+            }
+        )
+
     def test_get_branches_head(self):
         ''' Test the get_branches_head from the internal API. '''
         tests.create_projects(self.session)

