##// END OF EJS Templates
libgit2: always cleanup after opening a repository, this way we don't lock the repo in multi-worker env
marcink -
r731:34168bd1 default
parent child
Show More
@@ -158,32 +158,36 class GitRemote(object):
158
158
159 @reraise_safe_exceptions
159 @reraise_safe_exceptions
160 def is_empty(self, wire):
160 def is_empty(self, wire):
161 repo = self._factory.repo_libgit2(wire)
161 repo_init = self._factory.repo_libgit2(wire)
162
162 with repo_init as repo:
163 try:
164 has_head = repo.head.name
165 if has_head:
166 return False
167
163
168 # NOTE(marcink): check again using more expensive method
164 try:
169 return repo.is_empty
165 has_head = repo.head.name
170 except Exception:
166 if has_head:
171 pass
167 return False
172
168
173 return True
169 # NOTE(marcink): check again using more expensive method
170 return repo.is_empty
171 except Exception:
172 pass
173
174 return True
174
175
175 @reraise_safe_exceptions
176 @reraise_safe_exceptions
176 def add_object(self, wire, content):
177 def add_object(self, wire, content):
177 repo = self._factory.repo(wire)
178 repo_init = self._factory.repo_libgit2(wire)
178 blob = objects.Blob()
179 with repo_init as repo:
179 blob.set_raw_string(content)
180 blob = objects.Blob()
180 repo.object_store.add_object(blob)
181 blob.set_raw_string(content)
181 return blob.id
182 repo.object_store.add_object(blob)
183 return blob.id
182
184
183 @reraise_safe_exceptions
185 @reraise_safe_exceptions
184 def assert_correct_path(self, wire):
186 def assert_correct_path(self, wire):
185 try:
187 try:
186 self._factory.repo_libgit2(wire)
188 repo_init = self._factory.repo_libgit2(wire)
189 with repo_init as repo:
190 pass
187 except pygit2.GitError:
191 except pygit2.GitError:
188 path = wire.get('path')
192 path = wire.get('path')
189 tb = traceback.format_exc()
193 tb = traceback.format_exc()
@@ -194,8 +198,9 class GitRemote(object):
194
198
195 @reraise_safe_exceptions
199 @reraise_safe_exceptions
196 def bare(self, wire):
200 def bare(self, wire):
197 repo = self._factory.repo_libgit2(wire)
201 repo_init = self._factory.repo_libgit2(wire)
198 return repo.is_bare
202 with repo_init as repo:
203 return repo.is_bare
199
204
200 @reraise_safe_exceptions
205 @reraise_safe_exceptions
201 def blob_as_pretty_string(self, wire, sha):
206 def blob_as_pretty_string(self, wire, sha):
@@ -232,7 +237,6 class GitRemote(object):
232 @reraise_safe_exceptions
237 @reraise_safe_exceptions
233 def is_large_file(self, wire, sha):
238 def is_large_file(self, wire, sha):
234 repo_init = self._factory.repo_libgit2(wire)
239 repo_init = self._factory.repo_libgit2(wire)
235
236 with repo_init as repo:
240 with repo_init as repo:
237 blob = repo[sha]
241 blob = repo[sha]
238 if blob.is_binary:
242 if blob.is_binary:
@@ -242,12 +246,14 class GitRemote(object):
242
246
243 @reraise_safe_exceptions
247 @reraise_safe_exceptions
244 def in_largefiles_store(self, wire, oid):
248 def in_largefiles_store(self, wire, oid):
245 repo = self._factory.repo_libgit2(wire)
246 conf = self._wire_to_config(wire)
249 conf = self._wire_to_config(wire)
250 repo_init = self._factory.repo_libgit2(wire)
251 with repo_init as repo:
252 repo_name = repo.path
247
253
248 store_location = conf.get('vcs_git_lfs_store_location')
254 store_location = conf.get('vcs_git_lfs_store_location')
249 if store_location:
255 if store_location:
250 repo_name = repo.path
256
251 store = LFSOidStore(
257 store = LFSOidStore(
252 oid=oid, repo=repo_name, store_location=store_location)
258 oid=oid, repo=repo_name, store_location=store_location)
253 return store.has_oid()
259 return store.has_oid()
@@ -256,12 +262,13 class GitRemote(object):
256
262
257 @reraise_safe_exceptions
263 @reraise_safe_exceptions
258 def store_path(self, wire, oid):
264 def store_path(self, wire, oid):
259 repo = self._factory.repo_libgit2(wire)
260 conf = self._wire_to_config(wire)
265 conf = self._wire_to_config(wire)
266 repo_init = self._factory.repo_libgit2(wire)
267 with repo_init as repo:
268 repo_name = repo.path
261
269
262 store_location = conf.get('vcs_git_lfs_store_location')
270 store_location = conf.get('vcs_git_lfs_store_location')
263 if store_location:
271 if store_location:
264 repo_name = repo.path
265 store = LFSOidStore(
272 store = LFSOidStore(
266 oid=oid, repo=repo_name, store_location=store_location)
273 oid=oid, repo=repo_name, store_location=store_location)
267 return store.oid_path
274 return store.oid_path
@@ -605,51 +612,53 class GitRemote(object):
605
612
606 @reraise_safe_exceptions
613 @reraise_safe_exceptions
607 def get_object(self, wire, sha):
614 def get_object(self, wire, sha):
608 repo = self._factory.repo_libgit2(wire)
615 repo_init = self._factory.repo_libgit2(wire)
616 with repo_init as repo:
609
617
610 missing_commit_err = 'Commit {} does not exist for `{}`'.format(sha, wire['path'])
618 missing_commit_err = 'Commit {} does not exist for `{}`'.format(sha, wire['path'])
611 try:
619 try:
612 commit = repo.revparse_single(sha)
620 commit = repo.revparse_single(sha)
613 except (KeyError, ValueError) as e:
621 except (KeyError, ValueError) as e:
614 raise exceptions.LookupException(e)(missing_commit_err)
622 raise exceptions.LookupException(e)(missing_commit_err)
615
623
616 if isinstance(commit, pygit2.Tag):
624 if isinstance(commit, pygit2.Tag):
617 commit = repo.get(commit.target)
625 commit = repo.get(commit.target)
618
626
619 # check for dangling commit
627 # check for dangling commit
620 branches = [x for x in repo.branches.with_commit(commit.hex)]
628 branches = [x for x in repo.branches.with_commit(commit.hex)]
621 if not branches:
629 if not branches:
622 raise exceptions.LookupException(None)(missing_commit_err)
630 raise exceptions.LookupException(None)(missing_commit_err)
623
631
624 commit_id = commit.hex
632 commit_id = commit.hex
625 type_id = commit.type
633 type_id = commit.type
626
634
627 return {
635 return {
628 'id': commit_id,
636 'id': commit_id,
629 'type': self._type_id_to_name(type_id),
637 'type': self._type_id_to_name(type_id),
630 'commit_id': commit_id,
638 'commit_id': commit_id,
631 'idx': 0
639 'idx': 0
632 }
640 }
633
641
634 @reraise_safe_exceptions
642 @reraise_safe_exceptions
635 def get_refs(self, wire):
643 def get_refs(self, wire):
636 repo = self._factory.repo_libgit2(wire)
644 repo_init = self._factory.repo_libgit2(wire)
645 with repo_init as repo:
646 result = {}
647 for ref in repo.references:
648 peeled_sha = repo.lookup_reference(ref).peel()
649 result[ref] = peeled_sha.hex
637
650
638 result = {}
651 return result
639 for ref in repo.references:
640 peeled_sha = repo.lookup_reference(ref).peel()
641 result[ref] = peeled_sha.hex
642
643 return result
644
652
645 @reraise_safe_exceptions
653 @reraise_safe_exceptions
646 def head(self, wire, show_exc=True):
654 def head(self, wire, show_exc=True):
647 repo = self._factory.repo_libgit2(wire)
655 repo_init = self._factory.repo_libgit2(wire)
648 try:
656 with repo_init as repo:
649 return repo.head.peel().hex
657 try:
650 except Exception:
658 return repo.head.peel().hex
651 if show_exc:
659 except Exception:
652 raise
660 if show_exc:
661 raise
653
662
654 @reraise_safe_exceptions
663 @reraise_safe_exceptions
655 def init(self, wire):
664 def init(self, wire):
@@ -663,71 +672,80 class GitRemote(object):
663
672
664 @reraise_safe_exceptions
673 @reraise_safe_exceptions
665 def revision(self, wire, rev):
674 def revision(self, wire, rev):
666 repo = self._factory.repo_libgit2(wire)
675 repo_init = self._factory.repo_libgit2(wire)
667 commit = repo[rev]
676 with repo_init as repo:
668 obj_data = {
677 commit = repo[rev]
669 'id': commit.id.hex,
678 obj_data = {
670 }
679 'id': commit.id.hex,
671 # tree objects itself don't have tree_id attribute
680 }
672 if hasattr(commit, 'tree_id'):
681 # tree objects itself don't have tree_id attribute
673 obj_data['tree'] = commit.tree_id.hex
682 if hasattr(commit, 'tree_id'):
683 obj_data['tree'] = commit.tree_id.hex
674
684
675 return obj_data
685 return obj_data
676
686
677 @reraise_safe_exceptions
687 @reraise_safe_exceptions
678 def date(self, wire, rev):
688 def date(self, wire, rev):
679 repo = self._factory.repo_libgit2(wire)
689 repo_init = self._factory.repo_libgit2(wire)
680 commit = repo[rev]
690 with repo_init as repo:
681 # TODO(marcink): check dulwich difference of offset vs timezone
691 commit = repo[rev]
682 return [commit.commit_time, commit.commit_time_offset]
692 # TODO(marcink): check dulwich difference of offset vs timezone
693 return [commit.commit_time, commit.commit_time_offset]
683
694
684 @reraise_safe_exceptions
695 @reraise_safe_exceptions
685 def author(self, wire, rev):
696 def author(self, wire, rev):
686 repo = self._factory.repo_libgit2(wire)
697 repo_init = self._factory.repo_libgit2(wire)
687 commit = repo[rev]
698 with repo_init as repo:
688 if commit.author.email:
699 commit = repo[rev]
689 return u"{} <{}>".format(commit.author.name, commit.author.email)
700 if commit.author.email:
701 return u"{} <{}>".format(commit.author.name, commit.author.email)
690
702
691 return u"{}".format(commit.author.raw_name)
703 return u"{}".format(commit.author.raw_name)
692
704
693 @reraise_safe_exceptions
705 @reraise_safe_exceptions
694 def message(self, wire, rev):
706 def message(self, wire, rev):
695 repo = self._factory.repo_libgit2(wire)
707 repo_init = self._factory.repo_libgit2(wire)
696 commit = repo[rev]
708 with repo_init as repo:
697 return commit.message
709 commit = repo[rev]
710 return commit.message
698
711
699 @reraise_safe_exceptions
712 @reraise_safe_exceptions
700 def parents(self, wire, rev):
713 def parents(self, wire, rev):
701 repo = self._factory.repo_libgit2(wire)
714 repo_init = self._factory.repo_libgit2(wire)
702 commit = repo[rev]
715 with repo_init as repo:
703 return [x.hex for x in commit.parent_ids]
716 commit = repo[rev]
717 return [x.hex for x in commit.parent_ids]
704
718
705 @reraise_safe_exceptions
719 @reraise_safe_exceptions
706 def set_refs(self, wire, key, value):
720 def set_refs(self, wire, key, value):
707 repo = self._factory.repo_libgit2(wire)
721 repo_init = self._factory.repo_libgit2(wire)
708 repo.references.create(key, value, force=True)
722 with repo_init as repo:
723 repo.references.create(key, value, force=True)
709
724
710 @reraise_safe_exceptions
725 @reraise_safe_exceptions
711 def create_branch(self, wire, branch_name, commit_id, force=False):
726 def create_branch(self, wire, branch_name, commit_id, force=False):
712 repo = self._factory.repo_libgit2(wire)
727 repo_init = self._factory.repo_libgit2(wire)
713 commit = repo[commit_id]
728 with repo_init as repo:
729 commit = repo[commit_id]
714
730
715 if force:
731 if force:
716 repo.branches.local.create(branch_name, commit, force=force)
732 repo.branches.local.create(branch_name, commit, force=force)
717 elif not repo.branches.get(branch_name):
733 elif not repo.branches.get(branch_name):
718 # create only if that branch isn't existing
734 # create only if that branch isn't existing
719 repo.branches.local.create(branch_name, commit, force=force)
735 repo.branches.local.create(branch_name, commit, force=force)
720
736
721 @reraise_safe_exceptions
737 @reraise_safe_exceptions
722 def remove_ref(self, wire, key):
738 def remove_ref(self, wire, key):
723 repo = self._factory.repo_libgit2(wire)
739 repo_init = self._factory.repo_libgit2(wire)
724 repo.references.delete(key)
740 with repo_init as repo:
741 repo.references.delete(key)
725
742
726 @reraise_safe_exceptions
743 @reraise_safe_exceptions
727 def tag_remove(self, wire, tag_name):
744 def tag_remove(self, wire, tag_name):
728 repo = self._factory.repo_libgit2(wire)
745 repo_init = self._factory.repo_libgit2(wire)
729 key = 'refs/tags/{}'.format(tag_name)
746 with repo_init as repo:
730 repo.references.delete(key)
747 key = 'refs/tags/{}'.format(tag_name)
748 repo.references.delete(key)
731
749
732 @reraise_safe_exceptions
750 @reraise_safe_exceptions
733 def tree_changes(self, wire, source_id, target_id):
751 def tree_changes(self, wire, source_id, target_id):
General Comments 0
You need to be logged in to leave comments. Login now