1
# index.py -- File parser/write for the git index file
2
# Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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,
19
"""Parser for the git index file format."""
23
def read_cache_time(f):
24
return struct.unpack(">LL", f.read(8))
26
def write_cache_time(f, t):
27
f.write(struct.pack(">LL", *t))
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))
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)
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))
54
real_size = ((f.tell() - beginoffset + 8) & ~7)
55
f.write(chr(0) * (f.tell() - (beginoffset + real_size)))
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)
66
def write_index(f, entries):
68
f.write(struct.pack(">LL", 2, len(entries)))
70
write_cache_entry(f, x)
75
def __init__(self, filename):
77
f = open(filename, 'r')
80
for x in read_index(f):
81
self._entries.append(x)
82
self._byname[x[0]] = x
87
return len(self._entries)
90
return list(self._entries)
93
return iter(self._entries)
95
def __getitem__(self, name):
96
return self._byname[name]