1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# index.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
"""
Module containing Index implementation, allowing to perform all kinds of index
manipulations such as querying and merging.
"""
import struct
import binascii
import mmap
import objects
import tempfile
import os
class IndexEntry(tuple):
"""
Allows convenient access to IndexEntry data without completely unpacking it.
Attributes usully accessed often are cached in the tuple whereas others are
unpacked on demand.
See the properties for a mapping between names and tuple indices.
"""
@property
def ctime(self):
"""
Returns
Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the
file's creation time
"""
return struct.unpack(">LL", self[0])
@property
def mtime(self):
"""
See ctime property, but returns modification time
"""
return struct.unpack(">LL", self[1])
@property
def dev(self):
"""
Device ID
"""
return self[2]
@property
def inode(self):
"""
Inode ID
"""
return self[3]
@property
def mode(self):
"""
File Mode, compatible to stat module constants
"""
return self[4]
@property
def uid(self):
"""
User ID
"""
return self[5]
@property
def gid(self):
"""
Group ID
"""
return self[6]
@property
def size(self):
"""
Uncompressed size of the blob
Note
Will be 0 if the stage is not 0 ( hence it is an unmerged entry )
"""
return self[7]
@property
def sha(self):
"""
hex sha of the blob
"""
return self[8]
@property
def stage(self):
"""
Stage of the entry, either:
0 = default stage
1 = stage before a merge or common ancestor entry in case of a 3 way merge
2 = stage of entries from the 'left' side of the merge
3 = stage of entries from the right side of the merge
Note:
For more information, see http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html
"""
return self[9]
@property
def path(self):
return self[10]
class Index(object):
"""
Implements an Index that can be manipulated using a native implementation in
order to save git command function calls wherever possible.
It provides custom merging facilities and to create custom commits.
``Entries``
The index contains an entries dict whose keys are tuples of type IndexEntry
to facilitate access.
"""
__slots__ = ( "version", "entries", "_extension_data" )
_VERSION = 2 # latest version we support
def __init__(self, stream = None):
"""
Initialize this Index instance, optionally from the given ``stream``
"""
self.entries = dict()
self.version = self._VERSION
self._extension_data = ''
if stream is not None:
self._read_from_stream(stream)
@classmethod
def _read_entry(cls, stream):
"""Return: One entry of the given stream"""
beginoffset = stream.tell()
ctime = struct.unpack(">8s", stream.read(8))[0]
mtime = struct.unpack(">8s", stream.read(8))[0]
(dev, ino, mode, uid, gid, size, sha, flags) = \
struct.unpack(">LLLLLL20sH", stream.read(20 + 4 * 6 + 2))
path_size = flags & 0x0fff
path = stream.read(path_size)
real_size = ((stream.tell() - beginoffset + 8) & ~7)
data = stream.read((beginoffset + real_size) - stream.tell())
return IndexEntry((ctime, mtime, dev, ino, mode, uid, gid, size,
binascii.hexlify(sha), flags >> 12, path))
@classmethod
def _read_header(cls, stream):
"""Return tuple(version_long, num_entries) from the given stream"""
type_id = stream.read(4)
if type_id != "DIRC":
raise AssertionError("Invalid index file header: %r" % type_id)
version, num_entries = struct.unpack(">LL", stream.read(4 * 2))
assert version in (1, 2)
return version, num_entries
def _read_from_stream(self, stream):
"""
Initialize this instance with index values read from the given stream
Note
We explicitly do not clear the entries dict here to allow for reading
multiple chunks from multiple streams into the same Index instance
"""
self.version, num_entries = self._read_header(stream)
count = 0
while count < num_entries:
entry = self._read_entry(stream)
self.entries[(entry.path,entry.stage)] = entry
count += 1
# END for each entry
# this data chunk is the footer of the index, don't yet know what it is for
self._extension_data = stream.read(~0)
@classmethod
def from_file(cls, file_path):
"""
Returns
Index instance as recreated from the given stream.
``file_pa ``
File path pointing to git index file
Note
Reading is based on the dulwich project.
"""
fp = open(file_path, "r")
# try memory map for speed
stream = fp
try:
stream = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
except Exception:
pass
# END memory mapping
try:
return cls(stream)
finally:
fp.close()
@classmethod
def to_file(cls, index, file_path):
"""
Write the index data to the given file path.
``index``
Index you wish to write.
``file_path``
Path at which to write the index data. Please note that missing directories
will lead to an exception to be thrown.
Raise
IOError if the file could not be written
"""
fp = open(file_path, "w")
try:
return index.write(fp)
finally:
fp.close()
# END exception handling
@classmethod
def _write_cache_entry(cls, stream, entry):
"""
Write an IndexEntry to a stream
"""
beginoffset = stream.tell()
stream.write(entry[0]) # ctime
stream.write(entry[1]) # mtime
path = entry[10]
plen = len(path) & 0x0fff # path length
assert plen == len(path), "Path %s too long to fit into index" % entry[10]
flags = plen | (entry[9] << 12)# stage and path length are 2 byte flags
stream.write(struct.pack(">LLLLLL20sH", entry[2], entry[3], entry[4],
entry[5], entry[6], entry[7], binascii.unhexlify(entry[8]), flags))
stream.write(path)
real_size = ((stream.tell() - beginoffset + 8) & ~7)
stream.write("\0" * ((beginoffset + real_size) - stream.tell()))
def write(self, stream):
"""
Write the current state to the given stream
``stream``
File-like object
Returns
self
Note
Index writing based on the dulwich implementation
"""
# header
stream.write("DIRC")
stream.write(struct.pack(">LL", self.version, len(self.entries)))
# body
entries_sorted = self.entries.values()
entries_sorted.sort(key=lambda e: (e[10], e[9])) # use path/stage as sort key
for entry in entries_sorted:
self._write_cache_entry(stream, entry)
# END for each entry
# write extension_data which we currently cannot interprete
stream.write(self._extension_data)
@classmethod
def from_tree(cls, repo, *treeish, **kwargs):
"""
Merge the given treeish revisions into a new index which is returned.
The original index will remain unaltered
``repo``
The repository treeish are located in.
``*treeish``
One, two or three Tree Objects or Commits. The result changes according to the
amount of trees.
If 1 Tree is given, it will just be read into a new index
If 2 Trees are given, they will be merged into a new index using a
two way merge algorithm. Tree 1 is the 'current' tree, tree 2 is the 'other'
one. It behaves like a fast-forward.
If 3 Trees are given, a 3-way merge will be performed with the first tree
being the common ancestor of tree 2 and tree 3. Tree 2 is the 'current' tree,
tree 3 is the 'other' one
``**kwargs``
Additional arguments passed to git-read-tree
Note:
In the three-way merge case, --aggressive will be specified to automatically
resolve more cases in a commonly correct manner. Specify trivial=True as kwarg
to override that.
As the underlying git-read-tree command takes into account the current index,
it will be temporarily moved out of the way to assure there are no unsuspected
interferences.
"""
if len(treeish) == 0 or len(treeish) > 3:
raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish))
arg_list = list()
# ignore that working tree and index possibly are out of date
if len(treeish)>1:
# drop unmerged entries when reading our index and merging
arg_list.append("--reset")
# handle non-trivial cases the way a real merge does
arg_list.append("--aggressive")
# END merge handling
# tmp file created in git home directory to be sure renaming
# works - /tmp/ dirs could be on another device
tmp_index = tempfile.mktemp('','',repo.path)
arg_list.append("--index-output=%s" % tmp_index)
arg_list.extend(treeish)
# move current index out of the way - otherwise the merge may fail
# as it considers existing entries. moving it essentially clears the index.
# Unfortunately there is no 'soft' way to do it
cur_index = os.path.join(repo.path, 'index')
moved_index = os.path.join(repo.path, 'index_moved'+tempfile.mktemp('','',''))
try:
os.rename(cur_index, moved_index)
repo.git.read_tree(*arg_list, **kwargs)
index = cls.from_file(tmp_index)
finally:
# put back the original index first !
if os.path.exists(moved_index):
os.rename(moved_index, cur_index)
if os.path.exists(tmp_index):
os.remove(tmp_index)
# END index merge handling
return index
def write_tree(self, stream):
"""
Writes the
"""
raise NotImplementedError("TODO")
|