Skip to content

Commit 4375386

Browse files
committed
IndexFile.write: Added special handling flag allowing to skip TREE extension data, which becomes important if git-write-tree is supposed to be used
1 parent f96ee74 commit 4375386

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

lib/git/index.py

+27-17
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def _write_cache_entry(cls, stream, entry):
416416
real_size = ((stream.tell() - beginoffset + 8) & ~7)
417417
stream.write("\0" * ((beginoffset + real_size) - stream.tell()))
418418

419-
def write(self, file_path = None):
419+
def write(self, file_path = None, ignore_tree_extension_data=False):
420420
"""
421421
Write the current state to our file path or to the given one
422422
@@ -426,6 +426,16 @@ def write(self, file_path = None):
426426
Please note that this will change the file_path of this index to
427427
the one you gave.
428428
429+
``ignore_tree_extension_data``
430+
If True, the TREE type extension data read in the index will not
431+
be written to disk. Use this if you have altered the index and
432+
would like to use git-write-tree afterwards to create a tree
433+
representing your written changes.
434+
If this data is present in the written index, git-write-tree
435+
will instead write the stored/cached tree.
436+
Alternatively, use IndexFile.write_tree() to handle this case
437+
automatically
438+
429439
Returns
430440
self
431441
@@ -448,9 +458,19 @@ def write(self, file_path = None):
448458
self._write_cache_entry(stream, entry)
449459
# END for each entry
450460

461+
stored_ext_data = None
462+
if ignore_tree_extension_data and self._extension_data and self._extension_data[:4] == 'TREE':
463+
stored_ext_data = self._extension_data
464+
self._extension_data = ''
465+
# END extension data special handling
466+
451467
# write previously cached extensions data
452468
stream.write(self._extension_data)
453469

470+
if stored_ext_data:
471+
self._extension_data = stored_ext_data
472+
# END reset previous ext data
473+
454474
# write the sha over the content
455475
stream.write_sha()
456476
write_op._end_writing()
@@ -770,28 +790,14 @@ def write_tree(self, missing_ok=False):
770790
Returns
771791
Tree object representing this index
772792
"""
773-
# IMPORTANT: If we have TREE extension data, it will actually
774-
# ignore the index and write the stored tree instead. Hence we
775-
# temporarily forget about it, and in fact I don't know what git
776-
# uses it for
777-
stored_ext_data = None
778-
if self._extension_data and self._extension_data[:4] == 'TREE':
779-
stored_ext_data = self._extension_data
780-
self._extension_data = ''
781-
# END extension data special handling
782-
783793
index_path = self._index_path()
784794
tmp_index_mover = _TemporaryFileSwap(index_path)
785795

786-
self.write(index_path)
796+
self.write(index_path, ignore_tree_extension_data=True)
787797
tree_sha = self.repo.git.write_tree(missing_ok=missing_ok)
788798

789799
del(tmp_index_mover) # as soon as possible
790800

791-
if stored_ext_data:
792-
self._extension_data = stored_ext_data
793-
# END reset stored exstension data
794-
795801
return Tree(self.repo, tree_sha, 0, '')
796802

797803
def _process_diff_args(self, args):
@@ -1127,10 +1133,14 @@ def move(self, items, skip_errors=False, **kwargs):
11271133
@default_index
11281134
def commit(self, message, parent_commits=None, head=True):
11291135
"""
1130-
Commit the current index, creating a commit object.
1136+
Commit the current default index file, creating a commit object.
11311137
11321138
For more information on the arguments, see tree.commit.
11331139
1140+
``NOTE``:
1141+
If you have manually altered the .entries member of this instance,
1142+
don't forget to write() your changes to disk beforehand.
1143+
11341144
Returns
11351145
Commit object representing the new commit
11361146
"""

0 commit comments

Comments
 (0)