1 # objects.py -- Access to base git objects
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # of the License or (at your option) a later version of the License.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 """Access to base git objects."""
25 from cStringIO import (
34 from dulwich.errors import (
39 from dulwich.file import GitFile
40 from dulwich.misc import (
50 COMMITTER_ID = "committer"
54 ENCODING_ID = "encoding"
58 return (stat.S_IFMT(m) == S_IFGITLINK)
60 def _decompress(string):
61 dcomp = zlib.decompressobj()
62 dcomped = dcomp.decompress(string)
63 dcomped += dcomp.flush()
68 """Takes a string and returns the hex of the sha within"""
69 hexsha = binascii.hexlify(sha)
70 assert len(hexsha) == 40, "Incorrect length of sha1 string: %d" % hexsha
75 """Takes a hex sha and returns a binary sha"""
76 assert len(hex) == 40, "Incorrent length of hexsha: %s" % hex
77 return binascii.unhexlify(hex)
80 def serializable_property(name, docstring=None):
83 setattr(obj, "_"+name, value)
84 obj._needs_serialization = True
87 return getattr(obj, "_"+name)
88 return property(get, set, doc=docstring)
91 class ShaFile(object):
95 def _parse_legacy_object(cls, map):
96 """Parse a legacy object, creating it and setting object._text"""
97 text = _decompress(map)
99 for posstype in type_map.keys():
100 if text.startswith(posstype):
101 object = type_map[posstype]()
102 text = text[len(posstype):]
104 assert object is not None, "%s is not a known object type" % text[:9]
105 assert text[0] == ' ', "%s is not a space" % text[0]
109 while text[0] >= '0' and text[0] <= '9':
110 if i > 0 and size == 0:
111 raise AssertionError("Size is not in canonical format")
112 size = (size * 10) + int(text[0])
116 assert text[0] == "\0", "Size not followed by null"
118 object.set_raw_string(text)
121 def as_legacy_object(self):
122 text = self.as_raw_string()
123 return zlib.compress("%s %d\0%s" % (self._type, len(text), text))
125 def as_raw_string(self):
126 if self._needs_serialization:
131 return self.as_raw_string()
136 def as_pretty_string(self):
137 return self.as_raw_string()
139 def _ensure_parsed(self):
140 if self._needs_parsing:
143 def set_raw_string(self, text):
144 if type(text) != str:
145 raise TypeError(text)
148 self._needs_parsing = True
149 self._needs_serialization = False
152 def _parse_object(cls, map):
153 """Parse a new style object , creating it and setting object._text"""
155 byte = ord(map[used])
157 num_type = (byte >> 4) & 7
159 object = num_type_map[num_type]()
161 raise AssertionError("Not a known type: %d" % num_type)
162 while (byte & 0x80) != 0:
163 byte = ord(map[used])
166 object.set_raw_string(_decompress(raw))
170 def _parse_file(cls, map):
171 word = (ord(map[0]) << 8) + ord(map[1])
172 if ord(map[0]) == 0x78 and (word % 31) == 0:
173 return cls._parse_legacy_object(map)
175 return cls._parse_object(map)
178 """Don't call this directly"""
181 def _parse_text(self):
182 """For subclasses to do initialisation time parsing"""
185 def from_file(cls, filename):
186 """Get the contents of a SHA file on disk"""
187 size = os.path.getsize(filename)
188 f = GitFile(filename, 'rb')
190 map = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)
191 shafile = cls._parse_file(map)
197 def from_raw_string(cls, type, string):
198 """Creates an object of the indicated type from the raw string given.
200 Type is the numeric type of an object. String is the raw uncompressed
203 real_class = num_type_map[type]
206 obj.set_raw_string(string)
210 def from_string(cls, string):
211 """Create a blob from a string."""
213 shafile.set_raw_string(string)
216 def _raw_length(self):
217 """Returns the length of the raw string of this object."""
218 return len(self.as_raw_string())
221 return "%s %lu\0" % (self._type, self._raw_length())
224 """The SHA1 object that is the name of this object."""
225 if self._needs_serialization or self._sha is None:
226 self._sha = make_sha()
227 self._sha.update(self._header())
228 self._sha.update(self.as_raw_string())
233 return self.sha().hexdigest()
236 return self._num_type
238 def set_type(self, type):
239 self._num_type = type
241 type = property(get_type, set_type)
244 return "<%s %s>" % (self.__class__.__name__, self.id)
246 def __ne__(self, other):
247 return self.id != other.id
249 def __eq__(self, other):
250 """Return true id the sha of the two objects match.
252 The __le__ etc methods aren't overriden as they make no sense,
253 certainly at this level.
255 return self.id == other.id
259 """A Git Blob object."""
263 _needs_serialization = False
264 _needs_parsing = False
269 def set_data(self, data):
272 data = property(get_data, set_data,
273 "The text contained within the blob object.")
276 def from_file(cls, filename):
277 blob = ShaFile.from_file(filename)
278 if blob._type != cls._type:
279 raise NotBlobError(filename)
284 """A Git Tag object."""
290 super(Tag, self).__init__()
291 self._needs_parsing = False
292 self._needs_serialization = True
295 def from_file(cls, filename):
296 blob = ShaFile.from_file(filename)
297 if blob._type != cls._type:
298 raise NotBlobError(filename)
302 def from_string(cls, string):
303 """Create a blob from a string."""
305 shafile.set_raw_string(string)
310 f.write("%s %s\n" % (OBJECT_ID, self._object_sha))
311 f.write("%s %s\n" % (TYPE_ID, num_type_map[self._object_type]._type))
312 f.write("%s %s\n" % (TAG_ID, self._name))
314 if self._tag_time is None:
315 f.write("%s %s\n" % (TAGGER_ID, self._tagger))
317 f.write("%s %s %d %s\n" % (TAGGER_ID, self._tagger, self._tag_time, format_timezone(self._tag_timezone)))
318 f.write("\n") # To close headers
319 f.write(self._message)
320 self._text = f.getvalue()
321 self._needs_serialization = False
323 def _parse_text(self):
324 """Grab the metadata attached to the tag"""
326 f = StringIO(self._text)
330 break # empty line indicates end of headers
331 (field, value) = l.split(" ", 1)
332 if field == OBJECT_ID:
333 self._object_sha = value
334 elif field == TYPE_ID:
335 self._object_type = type_map[value]
336 elif field == TAG_ID:
338 elif field == TAGGER_ID:
340 sep = value.index("> ")
343 self._tag_time = None
344 self._tag_timezone = None
346 self._tagger = value[0:sep+1]
347 (timetext, timezonetext) = value[sep+2:].rsplit(" ", 1)
349 self._tag_time = int(timetext)
350 except ValueError: #Not a unix timestamp
351 self._tag_time = time.strptime(timetext)
352 self._tag_timezone = parse_timezone(timezonetext)
354 raise AssertionError("Unknown field %s" % field)
355 self._message = f.read()
356 self._needs_parsing = False
358 def get_object(self):
359 """Returns the object pointed by this tag, represented as a tuple(type, sha)"""
360 self._ensure_parsed()
361 return (self._object_type, self._object_sha)
363 def set_object(self, value):
364 self._ensure_parsed()
365 (self._object_type, self._object_sha) = value
366 self._needs_serialization = True
368 object = property(get_object, set_object)
370 name = serializable_property("name", "The name of this tag")
371 tagger = serializable_property("tagger",
372 "Returns the name of the person who created this tag")
373 tag_time = serializable_property("tag_time",
374 "The creation timestamp of the tag. As the number of seconds since the epoch")
375 tag_timezone = serializable_property("tag_timezone",
376 "The timezone that tag_time is in.")
377 message = serializable_property("message", "The message attached to this tag")
380 def parse_tree(text):
385 mode_end = text.index(' ', count)
386 mode = int(text[count:mode_end], 8)
388 name_end = text.index('\0', mode_end)
389 name = text[mode_end+1:name_end]
393 sha = text[name_end+1:count]
395 ret[name] = (mode, sha_to_hex(sha))
401 """A Git tree object"""
407 super(Tree, self).__init__()
409 self._needs_parsing = False
410 self._needs_serialization = True
413 def from_file(cls, filename):
414 tree = ShaFile.from_file(filename)
415 if tree._type != cls._type:
416 raise NotTreeError(filename)
419 def __contains__(self, name):
420 self._ensure_parsed()
421 return name in self._entries
423 def __getitem__(self, name):
424 self._ensure_parsed()
425 return self._entries[name]
427 def __setitem__(self, name, value):
428 assert isinstance(value, tuple)
429 assert len(value) == 2
430 self._ensure_parsed()
431 self._entries[name] = value
432 self._needs_serialization = True
434 def __delitem__(self, name):
435 self._ensure_parsed()
436 del self._entries[name]
437 self._needs_serialization = True
440 self._ensure_parsed()
441 return len(self._entries)
443 def add(self, mode, name, hexsha):
444 assert type(mode) == int
445 assert type(name) == str
446 assert type(hexsha) == str
447 self._ensure_parsed()
448 self._entries[name] = mode, hexsha
449 self._needs_serialization = True
452 """Return a list of tuples describing the tree entries"""
453 self._ensure_parsed()
454 # The order of this is different from iteritems() for historical reasons
455 return [(mode, name, hexsha) for (name, mode, hexsha) in self.iteritems()]
458 def cmp_entry((name1, value1), (name2, value2)):
459 if stat.S_ISDIR(value1[0]):
461 if stat.S_ISDIR(value2[0]):
463 return cmp(name1, name2)
464 self._ensure_parsed()
465 for name, entry in sorted(self._entries.iteritems(), cmp=cmp_entry):
466 yield name, entry[0], entry[1]
468 def _parse_text(self):
469 """Grab the entries in the tree"""
470 self._entries = parse_tree(self._text)
471 self._needs_parsing = False
475 for name, mode, hexsha in self.iteritems():
476 f.write("%04o %s\0%s" % (mode, name, hex_to_sha(hexsha)))
477 self._text = f.getvalue()
478 self._needs_serialization = False
480 def as_pretty_string(self):
482 for name, mode, hexsha in self.iteritems():
483 if mode & stat.S_IFDIR:
487 text += "%04o %s %s\t%s\n" % (mode, kind, hexsha, name)
491 def parse_timezone(text):
493 signum = (offset < 0) and -1 or 1
495 hours = int(offset / 100)
496 minutes = (offset % 100)
497 return signum * (hours * 3600 + minutes * 60)
500 def format_timezone(offset):
502 raise ValueError("Unable to handle non-minute offset.")
503 sign = (offset < 0) and '-' or '+'
505 return '%c%02d%02d' % (sign, offset / 3600, (offset / 60) % 60)
508 class Commit(ShaFile):
509 """A git commit object"""
515 super(Commit, self).__init__()
517 self._encoding = None
518 self._needs_parsing = False
519 self._needs_serialization = True
523 def from_file(cls, filename):
524 commit = ShaFile.from_file(filename)
525 if commit._type != cls._type:
526 raise NotCommitError(filename)
529 def _parse_text(self):
533 f = StringIO(self._text)
537 # Empty line indicates end of headers
539 (field, value) = l.split(" ", 1)
542 elif field == PARENT_ID:
543 self._parents.append(value)
544 elif field == AUTHOR_ID:
545 self._author, timetext, timezonetext = value.rsplit(" ", 2)
546 self._author_time = int(timetext)
547 self._author_timezone = parse_timezone(timezonetext)
548 elif field == COMMITTER_ID:
549 self._committer, timetext, timezonetext = value.rsplit(" ", 2)
550 self._commit_time = int(timetext)
551 self._commit_timezone = parse_timezone(timezonetext)
552 elif field == ENCODING_ID:
553 self._encoding = value
555 self._extra.append((field, value))
556 self._message = f.read()
557 self._needs_parsing = False
561 f.write("%s %s\n" % (TREE_ID, self._tree))
562 for p in self._parents:
563 f.write("%s %s\n" % (PARENT_ID, p))
564 f.write("%s %s %s %s\n" % (AUTHOR_ID, self._author, str(self._author_time), format_timezone(self._author_timezone)))
565 f.write("%s %s %s %s\n" % (COMMITTER_ID, self._committer, str(self._commit_time), format_timezone(self._commit_timezone)))
567 f.write("%s %s\n" % (ENCODING_ID, self.encoding))
568 for k, v in self.extra:
569 if "\n" in k or "\n" in v:
570 raise AssertionError("newline in extra data: %r -> %r" % (k, v))
571 f.write("%s %s\n" % (k, v))
572 f.write("\n") # There must be a new line after the headers
573 f.write(self._message)
574 self._text = f.getvalue()
575 self._needs_serialization = False
577 tree = serializable_property("tree", "Tree that is the state of this commit")
579 def get_parents(self):
580 """Return a list of parents of this commit."""
581 self._ensure_parsed()
584 def set_parents(self, value):
585 """Return a list of parents of this commit."""
586 self._ensure_parsed()
587 self._needs_serialization = True
588 self._parents = value
590 parents = property(get_parents, set_parents)
593 """Return extra settings of this commit."""
594 self._ensure_parsed()
597 extra = property(get_extra)
599 author = serializable_property("author",
600 "The name of the author of the commit")
602 committer = serializable_property("committer",
603 "The name of the committer of the commit")
605 message = serializable_property("message",
606 "The commit message")
608 commit_time = serializable_property("commit_time",
609 "The timestamp of the commit. As the number of seconds since the epoch.")
611 commit_timezone = serializable_property("commit_timezone",
612 "The zone the commit time is in")
614 author_time = serializable_property("author_time",
615 "The timestamp the commit was written. as the number of seconds since the epoch.")
617 author_timezone = serializable_property("author_timezone",
618 "Returns the zone the author time is in.")
620 encoding = serializable_property("encoding",
621 "Encoding of the commit message.")
637 # 5 Is reserved for further expansion
641 # Try to import C versions
642 from dulwich._objects import parse_tree