Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Add ability to fetch merge commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Kalnitsky committed Oct 17, 2015
1 parent 1459010 commit 5119405
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Expand Up @@ -5,6 +5,13 @@
Here you can see the list of changes between each git-pr release.


0.2.0 (YYYY-MM-DD)
==================

- New argument: ``-m``, ``--merge``. The flag is used to fetch a merge
commit of a given pull request and ``master`` branch.


0.1.0 (2015-10-17)
==================

Expand Down
2 changes: 1 addition & 1 deletion git_pr/__init__.py
Expand Up @@ -9,5 +9,5 @@
:license: MIT, see LICENSE for details
"""

__version__ = '0.1.0'
__version__ = '0.2.0'
__license__ = 'MIT'
6 changes: 5 additions & 1 deletion git_pr/__main__.py
Expand Up @@ -33,11 +33,15 @@ def main(args=sys.argv[1:]):
'-b', '--branch',
help='create a branch if passed')

parser.add_argument(
'-m', '--merge-commit', action='store_true',
help='fetch merge commit if passed')

arguments = parser.parse_args(args)

try:
pr = PullRequest(arguments.repository)
pr.get(arguments.pull_request, arguments.branch)
pr.get(arguments.pull_request, arguments.branch, arguments.merge_commit)
except subprocess.CalledProcessError:
# do nothing, just because that mostly means we do have an error
# from git binary on stderr and it makes no sense to continue or
Expand Down
23 changes: 21 additions & 2 deletions git_pr/core.py
Expand Up @@ -14,12 +14,31 @@


class PullRequest(object):
"""
A simple wrapper around Git to manage GitHub's pull requests.
The class allows you:
* to retrieve a pull request
* to retrieve a pull request's "merge" commit against "master" branch
:param repository: a Git URL or remote that points to some GitHub repo
"""

def __init__(self, repository):
self._repository = repository

def get(self, pr, branch=None):
refspec = 'pull/{0}/head'.format(pr)
def get(self, pr, branch=None, merge_commit=False):
"""
Retrieves pull request with ID = ``pr``.
:param pr: a pull request ID
:param branch: a branch to fetch into (if passed)
:param merge_commit: fetch merge commit, not pull request's head
"""
pointer = 'head' if not merge_commit else 'merge'
refspec = 'refs/pull/{0}/{1}'.format(pr, pointer)

if branch is not None:
refspec = '{0}:{1}'.format(refspec, branch)
subprocess.check_call(['git', 'fetch', self._repository, refspec])
Expand Down
5 changes: 5 additions & 0 deletions man/git-pr.1
Expand Up @@ -36,6 +36,11 @@ A pull request ID to be fetched.
If passed, a pull request will be fetched into a new branch with a given
\fIname\fR.

.TP
.B \-m, \-\-merge-commit
If passed, a merge commit of pull request and master will be fetched.
Please note, it's possible to fetch only a merge commit of open pull requests.


.SH BUGS
Bug reports can be submitted to <https://github.com/ikalnitsky/git\-pr/issues>
Expand Down
22 changes: 19 additions & 3 deletions tests/test_core.py
Expand Up @@ -19,20 +19,36 @@
class TestPullRequest(unittest.TestCase):

def setUp(self):
self.pr = PullRequest('test-repo')
self.pr = PullRequest('repo')

def test_get(self, sp_call):
self.pr.get(42)

self.assertEqual([
mock.call(['git', 'fetch', 'test-repo', 'pull/42/head']),
mock.call(['git', 'fetch', 'repo', 'refs/pull/42/head']),
mock.call(['git', 'checkout', 'FETCH_HEAD']),
], sp_call.call_args_list)

def test_get_create_branch(self, sp_call):
self.pr.get(42, 'pr/42')

self.assertEqual([
mock.call(['git', 'fetch', 'test-repo', 'pull/42/head:pr/42']),
mock.call(['git', 'fetch', 'repo', 'refs/pull/42/head:pr/42']),
mock.call(['git', 'checkout', 'pr/42']),
], sp_call.call_args_list)

def test_get_merge_commit(self, sp_call):
self.pr.get(42, merge_commit=True)

self.assertEqual([
mock.call(['git', 'fetch', 'repo', 'refs/pull/42/merge']),
mock.call(['git', 'checkout', 'FETCH_HEAD']),
], sp_call.call_args_list)

def test_get_create_branch_and_merge_commit(self, sp_call):
self.pr.get(42, 'pr/42', merge_commit=True)

self.assertEqual([
mock.call(['git', 'fetch', 'repo', 'refs/pull/42/merge:pr/42']),
mock.call(['git', 'checkout', 'pr/42']),
], sp_call.call_args_list)
12 changes: 9 additions & 3 deletions tests/test_main.py
Expand Up @@ -22,16 +22,22 @@ def test_one_argument(self, PullRequest):
main(['42'])

PullRequest.assert_called_once_with('origin')
PullRequest('origin').get.assert_called_once_with(42, None)
PullRequest('origin').get.assert_called_once_with(42, None, False)

def test_two_arguments(self, PullRequest):
main(['github', '42'])

PullRequest.assert_called_once_with('github')
PullRequest('github').get.assert_called_once_with(42, None)
PullRequest('github').get.assert_called_once_with(42, None, False)

def test_branch_argument(self, PullRequest):
main(['42', '-b', 'pr/42'])

PullRequest.assert_called_once_with('origin')
PullRequest('origin').get.assert_called_once_with(42, 'pr/42')
PullRequest('origin').get.assert_called_once_with(42, 'pr/42', False)

def test_merge_commit_argument(self, PullRequest):
main(['42', '-m'])

PullRequest.assert_called_once_with('origin')
PullRequest('origin').get.assert_called_once_with(42, None, True)

0 comments on commit 5119405

Please sign in to comment.