Mercurial > hg-git
annotate dulwich/object_store.py @ 88:52b4be85151d
fixed some small compatability issues with the dulwich update
author | Scott Chacon <schacon@gmail.com> |
---|---|
date | Fri, 08 May 2009 20:54:33 -0700 |
parents | babc85201dc4 |
children | b51381c6fab8 |
rev | line source |
---|---|
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
1 # object_store.py -- Object store for git objects |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org> |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
3 # |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
4 # This program is free software; you can redistribute it and/or |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
5 # modify it under the terms of the GNU General Public License |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
6 # as published by the Free Software Foundation; either version 2 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
7 # or (at your option) a later version of the License. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
8 # |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
9 # This program is distributed in the hope that it will be useful, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
12 # GNU General Public License for more details. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
13 # |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
14 # You should have received a copy of the GNU General Public License |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
15 # along with this program; if not, write to the Free Software |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
17 # MA 02110-1301, USA. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
18 |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
19 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
20 """Git object store interfaces and implementation.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
21 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
22 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
23 import itertools |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
24 import os |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
25 import stat |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
26 import tempfile |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
27 import urllib2 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
28 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
29 from errors import ( |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
30 NotTreeError, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
31 ) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
32 from objects import ( |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
33 ShaFile, |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
34 Tag, |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
35 Tree, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
36 hex_to_sha, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
37 sha_to_hex, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
38 ) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
39 from pack import ( |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
40 Pack, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
41 PackData, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
42 iter_sha1, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
43 load_packs, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
44 load_pack_index, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
45 write_pack, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
46 write_pack_data, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
47 write_pack_index_v2, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
48 ) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
49 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
50 PACKDIR = 'pack' |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
51 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
52 |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
53 class BaseObjectStore(object): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
54 """Object store interface.""" |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
55 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
56 def determine_wants_all(self, refs): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
57 return [sha for (ref, sha) in refs.iteritems() if not sha in self and not ref.endswith("^{}")] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
58 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
59 def iter_shas(self, shas): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
60 """Iterate over the objects for the specified shas. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
61 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
62 :param shas: Iterable object with SHAs |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
63 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
64 return ObjectStoreIterator(self, shas) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
65 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
66 def __contains__(self, sha): |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
67 """Check if a particular object is present by SHA1.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
68 raise NotImplementedError(self.__contains__) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
69 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
70 def get_raw(self, name): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
71 """Obtain the raw text for an object. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
72 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
73 :param name: sha for the object. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
74 :return: tuple with object type and object contents. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
75 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
76 raise NotImplementedError(self.get_raw) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
77 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
78 def __getitem__(self, sha): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
79 """Obtain an object by SHA1.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
80 type, uncomp = self.get_raw(sha) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
81 return ShaFile.from_raw_string(type, uncomp) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
82 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
83 def __iter__(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
84 """Iterate over the SHAs that are present in this store.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
85 raise NotImplementedError(self.__iter__) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
86 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
87 def add_object(self, obj): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
88 """Add a single object to this object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
89 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
90 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
91 raise NotImplementedError(self.add_object) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
92 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
93 def add_objects(self, objects): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
94 """Add a set of objects to this object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
95 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
96 :param objects: Iterable over a list of objects. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
97 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
98 raise NotImplementedError(self.add_objects) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
99 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
100 def find_missing_objects(self, wants, graph_walker, progress=None): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
101 """Find the missing objects required for a set of revisions. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
102 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
103 :param wants: Iterable over SHAs of objects to fetch. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
104 :param graph_walker: Object that can iterate over the list of revisions |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
105 to fetch and has an "ack" method that will be called to acknowledge |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
106 that a revision is present. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
107 :param progress: Simple progress function that will be called with |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
108 updated progress strings. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
109 :return: Iterator over (sha, path) pairs. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
110 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
111 return iter(MissingObjectFinder(self, wants, graph_walker, progress).next, None) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
112 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
113 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
114 class DiskObjectStore(BaseObjectStore): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
115 """Git-style object store that exists on disk.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
116 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
117 def __init__(self, path): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
118 """Open an object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
119 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
120 :param path: Path of the object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
121 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
122 self.path = path |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
123 self._pack_cache = None |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
124 self.pack_dir = os.path.join(self.path, PACKDIR) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
125 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
126 def __contains__(self, sha): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
127 """Check if a particular object is present by SHA1.""" |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
128 for pack in self.packs: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
129 if sha in pack: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
130 return True |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
131 ret = self._get_shafile(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
132 if ret is not None: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
133 return True |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
134 return False |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
135 |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
136 def __iter__(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
137 """Iterate over the SHAs that are present in this store.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
138 iterables = self.packs + [self._iter_shafile_shas()] |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
139 return itertools.chain(*iterables) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
140 |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
141 @property |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
142 def packs(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
143 """List with pack objects.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
144 if self._pack_cache is None: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
145 self._pack_cache = list(load_packs(self.pack_dir)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
146 return self._pack_cache |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
147 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
148 def _add_known_pack(self, path): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
149 """Add a newly appeared pack to the cache by path. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
150 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
151 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
152 if self._pack_cache is not None: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
153 self._pack_cache.append(Pack(path)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
154 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
155 def _get_shafile_path(self, sha): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
156 dir = sha[:2] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
157 file = sha[2:] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
158 # Check from object dir |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
159 return os.path.join(self.path, dir, file) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
160 |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
161 def _iter_shafile_shas(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
162 for base in os.listdir(self.path): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
163 if len(base) != 2: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
164 continue |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
165 for rest in os.listdir(os.path.join(self.path, base)): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
166 yield base+rest |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
167 |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
168 def _get_shafile(self, sha): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
169 path = self._get_shafile_path(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
170 if os.path.exists(path): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
171 return ShaFile.from_file(path) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
172 return None |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
173 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
174 def _add_shafile(self, sha, o): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
175 dir = os.path.join(self.path, sha[:2]) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
176 if not os.path.isdir(dir): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
177 os.mkdir(dir) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
178 path = os.path.join(dir, sha[2:]) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
179 f = open(path, 'w+') |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
180 try: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
181 f.write(o.as_legacy_object()) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
182 finally: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
183 f.close() |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
184 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
185 def get_raw(self, name): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
186 """Obtain the raw text for an object. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
187 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
188 :param name: sha for the object. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
189 :return: tuple with object type and object contents. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
190 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
191 if len(name) == 40: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
192 sha = hex_to_sha(name) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
193 hexsha = name |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
194 elif len(name) == 20: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
195 sha = name |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
196 hexsha = None |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
197 else: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
198 raise AssertionError |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
199 for pack in self.packs: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
200 try: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
201 return pack.get_raw(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
202 except KeyError: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
203 pass |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
204 if hexsha is None: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
205 hexsha = sha_to_hex(name) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
206 ret = self._get_shafile(hexsha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
207 if ret is not None: |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
208 return ret.type, ret.as_raw_string() |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
209 raise KeyError(hexsha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
210 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
211 def move_in_thin_pack(self, path): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
212 """Move a specific file containing a pack into the pack directory. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
213 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
214 :note: The file should be on the same file system as the |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
215 packs directory. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
216 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
217 :param path: Path to the pack file. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
218 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
219 data = PackData(path) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
220 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
221 # Write index for the thin pack (do we really need this?) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
222 temppath = os.path.join(self.pack_dir, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
223 sha_to_hex(urllib2.randombytes(20))+".tempidx") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
224 data.create_index_v2(temppath, self.get_raw) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
225 p = Pack.from_objects(data, load_pack_index(temppath)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
226 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
227 # Write a full pack version |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
228 temppath = os.path.join(self.pack_dir, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
229 sha_to_hex(urllib2.randombytes(20))+".temppack") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
230 write_pack(temppath, ((o, None) for o in p.iterobjects(self.get_raw)), |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
231 len(p)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
232 pack_sha = load_pack_index(temppath+".idx").objects_sha1() |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
233 newbasename = os.path.join(self.pack_dir, "pack-%s" % pack_sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
234 os.rename(temppath+".pack", newbasename+".pack") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
235 os.rename(temppath+".idx", newbasename+".idx") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
236 self._add_known_pack(newbasename) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
237 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
238 def move_in_pack(self, path): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
239 """Move a specific file containing a pack into the pack directory. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
240 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
241 :note: The file should be on the same file system as the |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
242 packs directory. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
243 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
244 :param path: Path to the pack file. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
245 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
246 p = PackData(path) |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
247 entries = p.sorted_entries() |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
248 basename = os.path.join(self.pack_dir, |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
249 "pack-%s" % iter_sha1(entry[0] for entry in entries)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
250 write_pack_index_v2(basename+".idx", entries, p.get_stored_checksum()) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
251 os.rename(path, basename + ".pack") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
252 self._add_known_pack(basename) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
253 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
254 def add_thin_pack(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
255 """Add a new thin pack to this object store. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
256 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
257 Thin packs are packs that contain deltas with parents that exist |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
258 in a different pack. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
259 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
260 fd, path = tempfile.mkstemp(dir=self.pack_dir, suffix=".pack") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
261 f = os.fdopen(fd, 'w') |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
262 def commit(): |
88
52b4be85151d
fixed some small compatability issues with the dulwich update
Scott Chacon <schacon@gmail.com>
parents:
87
diff
changeset
|
263 #os.fsync(fd) |
52b4be85151d
fixed some small compatability issues with the dulwich update
Scott Chacon <schacon@gmail.com>
parents:
87
diff
changeset
|
264 #f.close() |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
265 if os.path.getsize(path) > 0: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
266 self.move_in_thin_pack(path) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
267 return f, commit |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
268 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
269 def add_pack(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
270 """Add a new pack to this object store. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
271 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
272 :return: Fileobject to write to and a commit function to |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
273 call when the pack is finished. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
274 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
275 fd, path = tempfile.mkstemp(dir=self.pack_dir, suffix=".pack") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
276 f = os.fdopen(fd, 'w') |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
277 def commit(): |
88
52b4be85151d
fixed some small compatability issues with the dulwich update
Scott Chacon <schacon@gmail.com>
parents:
87
diff
changeset
|
278 #os.fsync(fd) |
52b4be85151d
fixed some small compatability issues with the dulwich update
Scott Chacon <schacon@gmail.com>
parents:
87
diff
changeset
|
279 #f.close() |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
280 if os.path.getsize(path) > 0: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
281 self.move_in_pack(path) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
282 return f, commit |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
283 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
284 def add_object(self, obj): |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
285 """Add a single object to this object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
286 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
287 """ |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
288 self._add_shafile(obj.id, obj) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
289 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
290 def add_objects(self, objects): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
291 """Add a set of objects to this object store. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
292 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
293 :param objects: Iterable over a list of objects. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
294 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
295 if len(objects) == 0: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
296 return |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
297 f, commit = self.add_pack() |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
298 write_pack_data(f, objects, len(objects)) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
299 commit() |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
300 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
301 |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
302 class MemoryObjectStore(BaseObjectStore): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
303 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
304 def __init__(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
305 super(MemoryObjectStore, self).__init__() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
306 self._data = {} |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
307 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
308 def __contains__(self, sha): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
309 return sha in self._data |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
310 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
311 def __iter__(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
312 """Iterate over the SHAs that are present in this store.""" |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
313 return self._data.iterkeys() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
314 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
315 def get_raw(self, name): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
316 """Obtain the raw text for an object. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
317 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
318 :param name: sha for the object. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
319 :return: tuple with object type and object contents. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
320 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
321 return self[name].as_raw_string() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
322 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
323 def __getitem__(self, name): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
324 return self._data[name] |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
325 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
326 def add_object(self, obj): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
327 """Add a single object to this object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
328 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
329 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
330 self._data[obj.id] = obj |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
331 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
332 def add_objects(self, objects): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
333 """Add a set of objects to this object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
334 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
335 :param objects: Iterable over a list of objects. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
336 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
337 for obj in objects: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
338 self._data[obj.id] = obj |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
339 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
340 |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
341 class ObjectImporter(object): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
342 """Interface for importing objects.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
343 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
344 def __init__(self, count): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
345 """Create a new ObjectImporter. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
346 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
347 :param count: Number of objects that's going to be imported. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
348 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
349 self.count = count |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
350 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
351 def add_object(self, object): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
352 """Add an object.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
353 raise NotImplementedError(self.add_object) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
354 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
355 def finish(self, object): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
356 """Finish the imoprt and write objects to disk.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
357 raise NotImplementedError(self.finish) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
358 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
359 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
360 class ObjectIterator(object): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
361 """Interface for iterating over objects.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
362 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
363 def iterobjects(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
364 raise NotImplementedError(self.iterobjects) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
365 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
366 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
367 class ObjectStoreIterator(ObjectIterator): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
368 """ObjectIterator that works on top of an ObjectStore.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
369 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
370 def __init__(self, store, sha_iter): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
371 self.store = store |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
372 self.sha_iter = sha_iter |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
373 self._shas = [] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
374 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
375 def __iter__(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
376 for sha, path in self.itershas(): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
377 yield self.store[sha], path |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
378 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
379 def iterobjects(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
380 for o, path in self: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
381 yield o |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
382 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
383 def itershas(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
384 for sha in self._shas: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
385 yield sha |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
386 for sha in self.sha_iter: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
387 self._shas.append(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
388 yield sha |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
389 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
390 def __contains__(self, needle): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
391 """Check if an object is present. |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
392 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
393 :param needle: SHA1 of the object to check for |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
394 """ |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
395 return needle in self.store |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
396 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
397 def __getitem__(self, key): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
398 """Find an object by SHA1.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
399 return self.store[key] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
400 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
401 def __len__(self): |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
402 """Return the number of objects.""" |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
403 return len(list(self.itershas())) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
404 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
405 |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
406 def tree_lookup_path(lookup_obj, root_sha, path): |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
407 """Lookup an object in a Git tree. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
408 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
409 :param lookup_obj: Callback for retrieving object by SHA1 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
410 :param root_sha: SHA1 of the root tree |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
411 :param path: Path to lookup |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
412 """ |
2
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
413 parts = path.split("/") |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
414 sha = root_sha |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
415 for p in parts: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
416 obj = lookup_obj(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
417 if type(obj) is not Tree: |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
418 raise NotTreeError(sha) |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
419 if p == '': |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
420 continue |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
421 mode, sha = obj[p] |
c43c02cc803a
added dulwich library and got the script to call it for clone
Scott Chacon <schacon@gmail.com>
parents:
diff
changeset
|
422 return lookup_obj(sha) |
87
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
423 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
424 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
425 class MissingObjectFinder(object): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
426 """Find the objects missing from another object store. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
427 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
428 :param object_store: Object store containing at least all objects to be |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
429 sent |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
430 :param wants: SHA1s of commits to send |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
431 :param graph_walker: graph walker object used to see what the remote |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
432 repo has and misses |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
433 :param progress: Optional function to report progress to. |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
434 """ |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
435 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
436 def __init__(self, object_store, wants, graph_walker, progress=None): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
437 self.sha_done = set() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
438 self.objects_to_send = set([(w, None, False) for w in wants]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
439 self.object_store = object_store |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
440 if progress is None: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
441 self.progress = lambda x: None |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
442 else: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
443 self.progress = progress |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
444 ref = graph_walker.next() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
445 while ref: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
446 if ref in self.object_store: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
447 graph_walker.ack(ref) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
448 ref = graph_walker.next() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
449 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
450 def add_todo(self, entries): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
451 self.objects_to_send.update([e for e in entries if not e[0] in self.sha_done]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
452 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
453 def parse_tree(self, tree): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
454 self.add_todo([(sha, name, not stat.S_ISDIR(mode)) for (mode, name, sha) in tree.entries()]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
455 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
456 def parse_commit(self, commit): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
457 self.add_todo([(commit.tree, "", False)]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
458 self.add_todo([(p, None, False) for p in commit.parents]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
459 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
460 def parse_tag(self, tag): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
461 self.add_todo([(tag.object[1], None, False)]) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
462 |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
463 def next(self): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
464 if not self.objects_to_send: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
465 return None |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
466 (sha, name, leaf) = self.objects_to_send.pop() |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
467 if not leaf: |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
468 o = self.object_store[sha] |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
469 if isinstance(o, Commit): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
470 self.parse_commit(o) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
471 elif isinstance(o, Tree): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
472 self.parse_tree(o) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
473 elif isinstance(o, Tag): |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
474 self.parse_tag(o) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
475 self.sha_done.add(sha) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
476 self.progress("counting objects: %d\r" % len(self.sha_done)) |
babc85201dc4
merge of upstream work from dulwich project
Scott Chacon <schacon@gmail.com>
parents:
57
diff
changeset
|
477 return (sha, name) |