From 2a58ed3e8cf6e97b15d467894c6d1e9ceac8d0a3 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: May 08 2016 08:21:42 +0000 Subject: [PATCH 1/4] rename get_revs_between arguments to be consistent with the function call. --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 0147e3f..1b343a7 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -840,18 +840,18 @@ def read_git_lines(args, abspath, keepends=False, **kw): ).splitlines(keepends) -def get_revs_between(torev, fromrev, abspath, forced=False): +def get_revs_between(oldrev, newrev, abspath, forced=False): """ Yield revisions between HEAD and BASE. """ - cmd = ['rev-list', '%s...%s' % (torev, fromrev)] + cmd = ['rev-list', '%s...%s' % (oldrev, newrev)] if forced: head = get_default_branch(abspath) cmd.append('^%s' % head) - if set(fromrev) == set('0'): - cmd = ['rev-list', '%s' % torev] - elif set(torev) == set('0') or set(torev) == set('^0'): + if set(newrev) == set('0'): + cmd = ['rev-list', '%s' % oldrev] + elif set(oldrev) == set('0') or set(oldrev) == set('^0'): head = get_default_branch(abspath) - cmd = ['rev-list', '%s' % fromrev, '^%s' % head] + cmd = ['rev-list', '%s' % newrev, '^%s' % head] return pagure.lib.git.read_git_lines(cmd, abspath) From 439a2dc34c3ad26d7d575613ecad785ed73dfb6f Mon Sep 17 00:00:00 2001 From: Clement Verna Date: May 08 2016 08:21:43 +0000 Subject: [PATCH 2/4] Switched newrev and oldrev in logic in order to fix the function --- diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 1b343a7..6745c63 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -847,11 +847,11 @@ def get_revs_between(oldrev, newrev, abspath, forced=False): if forced: head = get_default_branch(abspath) cmd.append('^%s' % head) - if set(newrev) == set('0'): - cmd = ['rev-list', '%s' % oldrev] - elif set(oldrev) == set('0') or set(oldrev) == set('^0'): + if set(oldrev) == set('0'): + cmd = ['rev-list', '%s' % newrev] + elif set(newrev) == set('0') or set(newrev) == set('^0'): head = get_default_branch(abspath) - cmd = ['rev-list', '%s' % newrev, '^%s' % head] + cmd = ['rev-list', '%s' % oldrev, '^%s' % head] return pagure.lib.git.read_git_lines(cmd, abspath) From 1901d549a055551c36120ef063ef7165118ab256 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: May 08 2016 08:21:43 +0000 Subject: [PATCH 3/4] Added test case for repo BASE is null --- diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 7284356..16f1416 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -1253,17 +1253,25 @@ index 0000000..60f7480 output = pagure.lib.git.read_git_lines( ['log', '-3', "--pretty='%H'"], gitrepo) self.assertEqual(len(output), 2) - to_hash = output[0].replace("'", '') from_hash = output[1].replace("'", '') + # Case 1, repo BASE is null and HEAD is equal to from_hash + to_hash = '0' output1 = pagure.lib.git.get_revs_between( to_hash, from_hash, gitrepo) - self.assertEqual(output1, [to_hash]) + self.assertEqual(output1, [from_hash]) + # Case 2, get revs between two commits (to_hash, from_hash) + to_hash = output[0].replace("'", '') output2 = pagure.lib.git.get_revs_between( - from_hash, to_hash, gitrepo) + to_hash, from_hash, gitrepo) self.assertEqual(output2, [to_hash]) + # Case 3, get revs between two commits (from_hash, to_hash) + output3 = pagure.lib.git.get_revs_between( + from_hash, to_hash, gitrepo) + self.assertEqual(output3, [to_hash]) + def test_get_author(self): """ Test the get_author method of pagure.lib.git. """ From 469e2c337aebb80c27c48be911fc1121be6db75e Mon Sep 17 00:00:00 2001 From: Clement Verna Date: May 08 2016 20:34:45 +0000 Subject: [PATCH 4/4] Add test case for get revs between 2 branches --- diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index 16f1416..b7a6ae6 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -16,9 +16,10 @@ import unittest import shutil import sys import os - +import tempfile import pygit2 from mock import patch +from pagure.lib.repo import PagureRepo sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -1272,6 +1273,45 @@ index 0000000..60f7480 from_hash, to_hash, gitrepo) self.assertEqual(output3, [to_hash]) + # Case 4, get revs between two commits on two different branches + newgitrepo = tempfile.mkdtemp(prefix='pagure-') + newrepo = pygit2.clone_repository(gitrepo, newgitrepo) + newrepo.create_branch('feature', newrepo.head.get_object()) + + with open(os.path.join(newgitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + newrepo.index.add('sources') + newrepo.index.write() + + # Commits the files added + tree = newrepo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + newrepo.create_commit( + 'refs/heads/feature', # 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 + [to_hash] + ) + branch_commit = newrepo.revparse_single('refs/heads/feature') + + # Push to origin + ori_remote = newrepo.remotes[0] + PagureRepo.push(ori_remote, 'refs/heads/feature') + + # Remove the clone + shutil.rmtree(newgitrepo) + + output4 = pagure.lib.git.get_revs_between( + 'feature', '0', gitrepo) + self.assertEqual(output4, [branch_commit.oid.hex]) + def test_get_author(self): """ Test the get_author method of pagure.lib.git. """