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