@@ -416,7 +416,7 @@ def _write_cache_entry(cls, stream, entry):
416
416
real_size = ((stream .tell () - beginoffset + 8 ) & ~ 7 )
417
417
stream .write ("\0 " * ((beginoffset + real_size ) - stream .tell ()))
418
418
419
- def write (self , file_path = None ):
419
+ def write (self , file_path = None , ignore_tree_extension_data = False ):
420
420
"""
421
421
Write the current state to our file path or to the given one
422
422
@@ -426,6 +426,16 @@ def write(self, file_path = None):
426
426
Please note that this will change the file_path of this index to
427
427
the one you gave.
428
428
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
+
429
439
Returns
430
440
self
431
441
@@ -448,9 +458,19 @@ def write(self, file_path = None):
448
458
self ._write_cache_entry (stream , entry )
449
459
# END for each entry
450
460
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
+
451
467
# write previously cached extensions data
452
468
stream .write (self ._extension_data )
453
469
470
+ if stored_ext_data :
471
+ self ._extension_data = stored_ext_data
472
+ # END reset previous ext data
473
+
454
474
# write the sha over the content
455
475
stream .write_sha ()
456
476
write_op ._end_writing ()
@@ -770,28 +790,14 @@ def write_tree(self, missing_ok=False):
770
790
Returns
771
791
Tree object representing this index
772
792
"""
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
-
783
793
index_path = self ._index_path ()
784
794
tmp_index_mover = _TemporaryFileSwap (index_path )
785
795
786
- self .write (index_path )
796
+ self .write (index_path , ignore_tree_extension_data = True )
787
797
tree_sha = self .repo .git .write_tree (missing_ok = missing_ok )
788
798
789
799
del (tmp_index_mover ) # as soon as possible
790
800
791
- if stored_ext_data :
792
- self ._extension_data = stored_ext_data
793
- # END reset stored exstension data
794
-
795
801
return Tree (self .repo , tree_sha , 0 , '' )
796
802
797
803
def _process_diff_args (self , args ):
@@ -1127,10 +1133,14 @@ def move(self, items, skip_errors=False, **kwargs):
1127
1133
@default_index
1128
1134
def commit (self , message , parent_commits = None , head = True ):
1129
1135
"""
1130
- Commit the current index, creating a commit object.
1136
+ Commit the current default index file , creating a commit object.
1131
1137
1132
1138
For more information on the arguments, see tree.commit.
1133
1139
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
+
1134
1144
Returns
1135
1145
Commit object representing the new commit
1136
1146
"""
0 commit comments