From a4c07ec7ae8a384e9514370c37e533028bc7f2dc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jul 21 2015 10:09:36 +0000 Subject: Add a method to pull changes from a remote repo into the local one --- diff --git a/pagure/lib/repo.py b/pagure/lib/repo.py index 12dbd66..105b5b9 100644 --- a/pagure/lib/repo.py +++ b/pagure/lib/repo.py @@ -11,6 +11,8 @@ import pygit2 +import pagure.exceptions + class PagureRepo(pygit2.Repository): """ An utility class allowing to go around pygit2's inability to be @@ -25,3 +27,34 @@ class PagureRepo(pygit2.Repository): remote.push([refname]) else: remote.push(refname) + + def pull(self, remote_name='origin', branch='master'): + ''' pull changes for the specified remote (defaults to origin). + + Code from MichaelBoselowitz at: + https://github.com/MichaelBoselowitz/pygit2-examples/blob/ + 68e889e50a592d30ab4105a2e7b9f28fac7324c8/examples.py#L58 + licensed under the MIT license. + ''' + + for remote in self.remotes: + if remote.name == remote_name: + remote.fetch() + remote_master_id = self.lookup_reference( + 'refs/remotes/origin/%s' % branch).target + merge_result, _ = self.merge_analysis(remote_master_id) + # Up to date, do nothing + if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE: + return + # We can just fastforward + elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD: + self.checkout_tree(self.get(remote_master_id)) + master_ref = self.lookup_reference( + 'refs/heads/%s' % branch) + master_ref.set_target(remote_master_id) + self.head.set_target(remote_master_id) + elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL: + raise pagure.exceptions.PagureException( + 'Pulling remote changes leads to a conflict') + else: + raise AssertionError('Unknown merge analysis result')