/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/index.py

  • Committer: Jelmer Vernooij
  • Date: 2009-01-14 18:24:38 UTC
  • mto: (0.222.3 dulwich)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090114182438-c0tn5eczyupi4ztn
Fix download url, add version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# index.py -- File parser/write for the git index file
 
2
# Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 
3
 
 
4
# This program is free software; you can redistribute it and/or
 
5
# modify it under the terms of the GNU General Public License
 
6
# as published by the Free Software Foundation; version 2
 
7
# of the License or (at your opinion) any later version of the license.
 
8
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
17
# MA  02110-1301, USA.
 
18
 
 
19
"""Parser for the git index file format."""
 
20
 
 
21
import struct
 
22
 
 
23
def read_cache_time(f):
 
24
    return struct.unpack(">LL", f.read(8))
 
25
 
 
26
def write_cache_time(f, t):
 
27
    f.write(struct.pack(">LL", *t))
 
28
 
 
29
def read_cache_entry(f):
 
30
    beginoffset = f.tell()
 
31
    ctime = read_cache_time(f)
 
32
    mtime = read_cache_time(f)
 
33
    (ino, dev, mode, uid, gid, size, sha, flags, ) = \
 
34
        struct.unpack(">LLLLLL20sH", f.read(20 + 4 * 6 + 2))
 
35
    name = ""
 
36
    char = ord(f.read(1))
 
37
    while char != 0:
 
38
        name += chr(char)
 
39
        char = ord(f.read(1))
 
40
    # Padding:
 
41
    real_size = ((f.tell() - beginoffset + 8) & ~7)
 
42
    f.seek(beginoffset + real_size)
 
43
    return (name, ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags)
 
44
 
 
45
 
 
46
def write_cache_entry(f, entry):
 
47
    beginoffset = f.tell()
 
48
    (name, ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = entry
 
49
    write_cache_time(f, ctime)
 
50
    write_cache_time(f, mtime)
 
51
    f.write(struct.pack(">LLLLLL20sH", ino, dev, mode, uid, gid, size, sha, flags))
 
52
    f.write(name)
 
53
    f.write(chr(0))
 
54
    real_size = ((f.tell() - beginoffset + 8) & ~7)
 
55
    f.write(chr(0) * (f.tell() - (beginoffset + real_size)))
 
56
    return 
 
57
 
 
58
def read_index(f):
 
59
    assert f.read(4) == "DIRC"
 
60
    (version, num_entries) = struct.unpack(">LL", f.read(4 * 2))
 
61
    assert version in (1, 2)
 
62
    for i in range(num_entries):
 
63
        yield read_cache_entry(f)
 
64
 
 
65
 
 
66
def write_index(f, entries):
 
67
    f.write("DIRC")
 
68
    f.write(struct.pack(">LL", 2, len(entries)))
 
69
    for x in entries:
 
70
        write_cache_entry(f, x)
 
71
 
 
72
 
 
73
class Index(object):
 
74
 
 
75
    def __init__(self, filename):
 
76
        self._entries = []
 
77
        f = open(filename, 'r')
 
78
        self._byname = {}
 
79
        try:
 
80
            for x in read_index(f):
 
81
                self._entries.append(x)
 
82
                self._byname[x[0]] = x
 
83
        finally:
 
84
            f.close()
 
85
 
 
86
    def __len__(self):
 
87
        return len(self._entries)
 
88
 
 
89
    def items(self):
 
90
        return list(self._entries)
 
91
 
 
92
    def __iter__(self):
 
93
        return iter(self._entries)
 
94
 
 
95
    def __getitem__(self, name):
 
96
        return self._byname[name]