Skip to content

Commit 402a6c2

Browse files
committed
IndexFile.add: writing of the index file can now optionally be turned off. The default is to write the physical index, which is the behaviour you would expect
1 parent feb1ea0 commit 402a6c2

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/git/index/base.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ def _preprocess_add_items(self, items):
581581
return (paths, entries)
582582

583583
@git_working_dir
584-
def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None):
584+
def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None,
585+
write=True):
585586
"""Add files from the working tree, specific blobs or BaseIndexEntries
586-
to the index. The underlying index file will be written immediately, hence
587-
you should provide as many items as possible to minimize the amounts of writes
587+
to the index.
588588
589589
:param items:
590590
Multiple types of items are supported, types can be mixed within one call.
@@ -653,6 +653,10 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
653653
converted to Entries beforehand and passed to the path_rewriter.
654654
Please note that entry.path is relative to the git repository.
655655
656+
:param write:
657+
If True, the index will be written once it was altered. Otherwise
658+
the changes only exist in memory and are not available to git commands.
659+
656660
:return:
657661
List(BaseIndexEntries) representing the entries just actually added.
658662
@@ -748,11 +752,13 @@ def store_path(filepath):
748752
# END if there are base entries
749753

750754
# FINALIZE
751-
# add the new entries to this instance, and write it
755+
# add the new entries to this instance
752756
for entry in entries_added:
753757
self.entries[(entry.path, 0)] = IndexEntry.from_base(entry)
754-
755-
self.write()
758+
759+
if write:
760+
self.write()
761+
# END handle write
756762

757763
return entries_added
758764

test/git/test_index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def test_index_merge_tree(self, rw_repo):
171171
# pretend there was a change, but we do not even bother adding a proper
172172
# sha for it ( which makes things faster of course )
173173
manifest_fake_entry = BaseIndexEntry((manifest_entry[0], "\0"*20, 0, manifest_entry[3]))
174-
rw_repo.index.add([manifest_fake_entry])
174+
# try write flag
175+
rw_repo.index.add([manifest_fake_entry], write=False)
175176
# add actually resolves the null-hex-sha for us as a feature, but we can
176177
# edit the index manually
177178
assert rw_repo.index.entries[manifest_key].binsha != Object.NULL_BIN_SHA

0 commit comments

Comments
 (0)