/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to dulwich/objects.py

  • Committer: Jelmer Vernooij
  • Date: 2008-12-12 15:37:07 UTC
  • mto: (0.215.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20081212153707-usxaq2v8agvmp91k
Change README to be about Dulwich rather than Python-git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
                    NotBlobError,
30
30
                    )
31
31
 
32
 
BLOB_ID = "blob"
33
 
TAG_ID = "tag"
34
 
TREE_ID = "tree"
35
 
COMMIT_ID = "commit"
36
 
PARENT_ID = "parent"
37
 
AUTHOR_ID = "author"
38
 
COMMITTER_ID = "committer"
 
32
blob_id = "blob"
 
33
tag_id = "tag"
 
34
tree_id = "tree"
 
35
commit_id = "commit"
 
36
parent_id = "parent"
 
37
author_id = "author"
 
38
committer_id = "committer"
39
39
 
40
40
def _decompress(string):
41
41
    dcomp = zlib.decompressobj()
56
56
class ShaFile(object):
57
57
  """A git SHA file."""
58
58
 
 
59
  def _update_contents(self):
 
60
    """Update the _contents from the _text"""
 
61
    self._contents = [ord(c) for c in self._text]
 
62
 
59
63
  @classmethod
60
64
  def _parse_legacy_object(cls, map):
61
65
    """Parse a legacy object, creating it and setting object._text"""
81
85
    assert text[0] == "\0", "Size not followed by null"
82
86
    text = text[1:]
83
87
    object._text = text
 
88
    object._update_contents()
84
89
    return object
85
90
 
86
 
  def as_raw_string(self):
87
 
    return self._num_type, self._text
88
 
 
89
91
  @classmethod
90
92
  def _parse_object(cls, map):
91
93
    """Parse a new style object , creating it and setting object._text"""
102
104
      used += 1
103
105
    raw = map[used:]
104
106
    object._text = _decompress(raw)
 
107
    object._update_contents()
105
108
    return object
106
109
 
107
110
  @classmethod
116
119
    """Don't call this directly"""
117
120
 
118
121
  def _parse_text(self):
119
 
    """For subclasses to do initialisation time parsing"""
 
122
    """For subclasses to do initialistion time parsing"""
120
123
 
121
124
  @classmethod
122
125
  def from_file(cls, filename):
140
143
    """
141
144
    real_class = num_type_map[type]
142
145
    obj = real_class()
143
 
    obj._num_type = type
144
146
    obj._text = string
145
 
    obj._parse_text()
 
147
    obj._update_contents()
146
148
    return obj
147
149
 
148
150
  def _header(self):
149
 
    return "%s %lu\0" % (self._type, len(self._text))
 
151
    return "%s %lu\0" % (self._type, len(self._contents))
 
152
 
 
153
  def contents(self):
 
154
    """The raw bytes of this object"""
 
155
    return self._contents
150
156
 
151
157
  def crc32(self):
152
158
    return zlib.crc32(self._text)
158
164
    ressha.update(self._text)
159
165
    return ressha
160
166
 
161
 
  @property
162
 
  def id(self):
163
 
      return self.sha().hexdigest()
164
 
 
165
 
  def __repr__(self):
166
 
    return "<%s %s>" % (self.__class__.__name__, self.id)
167
 
 
168
167
  def __eq__(self, other):
169
168
    """Return true id the sha of the two objects match.
170
169
 
177
176
class Blob(ShaFile):
178
177
  """A Git Blob object."""
179
178
 
180
 
  _type = BLOB_ID
 
179
  _type = blob_id
181
180
 
182
 
  @property
183
 
  def data(self):
 
181
  def text(self):
184
182
    """The text contained within the blob object."""
185
183
    return self._text
186
184
 
196
194
    """Create a blob from a string."""
197
195
    shafile = cls()
198
196
    shafile._text = string
 
197
    shafile._update_contents()
199
198
    return shafile
200
199
 
201
200
 
202
201
class Tag(ShaFile):
203
202
  """A Git Tag object."""
204
203
 
205
 
  _type = TAG_ID
 
204
  _type = tag_id
206
205
 
207
206
  @classmethod
208
207
  def from_file(cls, filename):
216
215
    """Create a blob from a string."""
217
216
    shafile = cls()
218
217
    shafile._text = string
 
218
    shafile._update_contents()
219
219
    return shafile
220
220
 
221
221
 
222
222
class Tree(ShaFile):
223
223
  """A Git tree object"""
224
224
 
225
 
  _type = TREE_ID
 
225
  _type = tree_id
226
226
 
227
227
  @classmethod
228
228
  def from_file(cls, filename):
264
264
class Commit(ShaFile):
265
265
  """A git commit object"""
266
266
 
267
 
  _type = COMMIT_ID
 
267
  _type = commit_id
268
268
 
269
269
  @classmethod
270
270
  def from_file(cls, filename):
276
276
  def _parse_text(self):
277
277
    text = self._text
278
278
    count = 0
279
 
    assert text.startswith(TREE_ID), "Invalid commit object, " \
280
 
         "must start with %s" % TREE_ID
281
 
    count += len(TREE_ID)
 
279
    assert text.startswith(tree_id), "Invlid commit object, " \
 
280
         "must start with %s" % tree_id
 
281
    count += len(tree_id)
282
282
    assert text[count] == ' ', "Invalid commit object, " \
283
 
         "%s must be followed by space not %s" % (TREE_ID, text[count])
 
283
         "%s must be followed by space not %s" % (tree_id, text[count])
284
284
    count += 1
285
285
    self._tree = text[count:count+40]
286
286
    count = count + 40
288
288
         "tree sha must be followed by newline"
289
289
    count += 1
290
290
    self._parents = []
291
 
    while text[count:].startswith(PARENT_ID):
292
 
      count += len(PARENT_ID)
 
291
    while text[count:].startswith(parent_id):
 
292
      count += len(parent_id)
293
293
      assert text[count] == ' ', "Invalid commit object, " \
294
 
           "%s must be followed by space not %s" % (PARENT_ID, text[count])
 
294
           "%s must be followed by space not %s" % (parent_id, text[count])
295
295
      count += 1
296
296
      self._parents.append(text[count:count+40])
297
297
      count += 40
299
299
           "parent sha must be followed by newline"
300
300
      count += 1
301
301
    self._author = None
302
 
    if text[count:].startswith(AUTHOR_ID):
303
 
      count += len(AUTHOR_ID)
 
302
    if text[count:].startswith(author_id):
 
303
      count += len(author_id)
304
304
      assert text[count] == ' ', "Invalid commit object, " \
305
 
           "%s must be followed by space not %s" % (AUTHOR_ID, text[count])
 
305
           "%s must be followed by space not %s" % (author_id, text[count])
306
306
      count += 1
307
307
      self._author = ''
308
308
      while text[count] != '>':
315
315
        count += 1
316
316
      count += 1
317
317
    self._committer = None
318
 
    if text[count:].startswith(COMMITTER_ID):
319
 
      count += len(COMMITTER_ID)
 
318
    if text[count:].startswith(committer_id):
 
319
      count += len(committer_id)
320
320
      assert text[count] == ' ', "Invalid commit object, " \
321
 
           "%s must be followed by space not %s" % (COMMITTER_ID, text[count])
 
321
           "%s must be followed by space not %s" % (committer_id, text[count])
322
322
      count += 1
323
323
      self._committer = ''
324
324
      while text[count] != '>':
339
339
    # XXX: There can be an encoding field.
340
340
    self._message = text[count:]
341
341
 
342
 
  @property
343
342
  def tree(self):
344
343
    """Returns the tree that is the state of this commit"""
345
344
    return self._tree
346
345
 
347
 
  @property
348
346
  def parents(self):
349
347
    """Return a list of parents of this commit."""
350
348
    return self._parents
351
349
 
352
 
  @property
353
350
  def author(self):
354
351
    """Returns the name of the author of the commit"""
355
352
    return self._author
356
353
 
357
 
  @property
358
354
  def committer(self):
359
355
    """Returns the name of the committer of the commit"""
360
356
    return self._committer
361
357
 
362
 
  @property
363
358
  def message(self):
364
359
    """Returns the commit message"""
365
360
    return self._message
366
361
 
367
 
  @property
368
362
  def commit_time(self):
369
363
    """Returns the timestamp of the commit.
370
364
    
373
367
    return self._commit_time
374
368
 
375
369
type_map = {
376
 
  BLOB_ID : Blob,
377
 
  TREE_ID : Tree,
378
 
  COMMIT_ID : Commit,
379
 
  TAG_ID: Tag,
 
370
  blob_id : Blob,
 
371
  tree_id : Tree,
 
372
  commit_id : Commit,
 
373
  tag_id: Tag,
380
374
}
381
375
 
382
376
num_type_map = {