Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84be126

Browse files
committed
index: Added IndexEntry.from_base method including test in conjunction with symlinks.
Added index.get_entries_key method to allow direct access to anyone willing to do so
1 parent 59b05d0 commit 84be126

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/git/index.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ def size(self):
159159
"""
160160
return self[10]
161161

162+
@classmethod
163+
def from_base(cls, base):
164+
"""
165+
Returns
166+
Minimal entry as created from the given BaseIndexEntry instance.
167+
Missing values will be set to null-like values
168+
169+
``base``
170+
Instance of type BaseIndexEntry
171+
"""
172+
time = struct.pack(">LL", 0, 0)
173+
return IndexEntry((base.mode, base.sha, base.stage, base.path, time, time, 1, 1, 1, 1, 0))
174+
162175
@classmethod
163176
def from_blob(cls, blob):
164177
"""
@@ -219,7 +232,8 @@ class IndexFile(LazyMixin, diff.Diffable):
219232
The index contains an entries dict whose keys are tuples of type IndexEntry
220233
to facilitate access.
221234
222-
You may only read the entries dict or manipulate it through designated methods.
235+
You may read the entries dict or manipulate it using IndexEntry instance, i.e.::
236+
index.entries[index.get_entries_key(index_entry_instance)] = index_entry_instance
223237
Otherwise changes to it will be lost when changing the index using its methods.
224238
"""
225239
__slots__ = ( "repo", "version", "entries", "_extension_data", "_file_path" )
@@ -311,7 +325,7 @@ def _read_from_stream(self, stream):
311325
self.entries = dict()
312326
while count < num_entries:
313327
entry = self._read_entry(stream)
314-
self.entries[(entry.path, entry.stage)] = entry
328+
self.entries[self.get_entries_key(entry)] = entry
315329
count += 1
316330
# END for each entry
317331

@@ -521,6 +535,18 @@ def unmerged_blobs(self):
521535

522536
return path_map
523537

538+
@classmethod
539+
def get_entries_key(cls, entry):
540+
"""
541+
Returns
542+
Key suitable to be used for the index.entries dictionary
543+
544+
``entry``
545+
Instance of type BaseIndexEntry
546+
"""
547+
return (entry.path, entry.stage)
548+
549+
524550
def resolve_blobs(self, iter_blobs):
525551
"""
526552
Resolve the blobs given in blob iterator. This will effectively remove the

test/git/test_index.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def test_index_file_diffing(self, rw_repo):
192192
index.checkout(test_file)
193193
assert os.path.exists(test_file)
194194

195-
195+
# checking out non-existing file is ignored/doesn't raise
196+
index.checkout("doesnt_exist_ever.txt.that")
196197

197198
# currently it ignore non-existing paths
198199
index.checkout(paths=["doesnt/exist"])
@@ -336,8 +337,20 @@ def mixed_iterator():
336337
fake_symlink_path = self._make_file(fake_symlink_relapath, link_target, rw_repo)
337338
fake_entry = BaseIndexEntry((0120000, null_sha, 0, fake_symlink_relapath))
338339
entries = index.reset(new_commit).add([fake_entry])
340+
assert entries[0].sha != null_sha
339341
assert len(entries) == 1 and S_ISLNK(entries[0].mode)
340342

343+
# assure this also works with an alternate method
344+
full_index_entry = IndexEntry.from_base(BaseIndexEntry((0120000, entries[0].sha, 0, entries[0].path)))
345+
entry_key = index.get_entries_key(full_index_entry)
346+
index.reset(new_commit)
347+
assert entry_key not in index.entries
348+
index.entries[entry_key] = full_index_entry
349+
index.write()
350+
index.update() # force reread of entries
351+
new_entry = index.entries[entry_key]
352+
assert S_ISLNK(new_entry.mode)
353+
341354
# checkout the fakelink, should be a link then
342355
assert not S_ISLNK(os.stat(fake_symlink_path)[ST_MODE])
343356
os.remove(fake_symlink_path)

0 commit comments

Comments
 (0)