Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8f043d8

Browse files
committed
fix(index): don't write extension data by default
It turned out that the index is not actually corrupted, which is goodnews. What happens is that `git` writes `TREE` extension data into theindex, which causes it to write out the given tree *as is* next timea `git commit` is executed. When using `git add`, this extension datais maintained automatically. However, GitPython doesn't do that ... .Usually this is no problem at all, as you are supposed to use`IndexFile.commit(...)` along with `IndexFile.add(...)`.Thanks to a shortcoming in the GitPython API, the index wasautomatically written out whenever files have been added, withoutproviding control over whether or not *extension data* will bewritten along with it.My fix consists of an additional flag in `IndexFile.add(...)`, whichcauses extension data not to be written by default, so commits can besafely done via `git commit` or `IndexFile.commit(...)`.However, this might introduce new subtle bugs in case someone isrelying on extension data to be written. As this can be controlledthrough the said flag though, a fix is easily done in that case.Fixesgitpython-developers#265
1 parentab7c322 commit8f043d8

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

‎doc/source/changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Changelog
33
=========
44

5+
0.3.7 - Fixes
6+
=============
7+
* `IndexFile.add()` will now write the index without any extension data by default. However, you may override this behaviour with the new `write_extension_data` keyword argument.
8+
9+
- Renamed `ignore_tree_extension_data` keyword argument in `IndexFile.write(...)` to `ignore_extension_data`
10+
511
0.3.6 - Features
612
================
713
* **DOCS**

‎git/index/base.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,17 @@ def _entries_sorted(self):
172172
""":return: list of entries, in a sorted fashion, first by path, then by stage"""
173173
returnsorted(self.entries.values(),key=lambdae: (e.path,e.stage))
174174

175-
def_serialize(self,stream,ignore_tree_extension_data=False):
175+
def_serialize(self,stream,ignore_extension_data=False):
176176
entries=self._entries_sorted()
177-
write_cache(entries,
178-
stream,
179-
(ignore_tree_extension_dataandNone)orself._extension_data)
177+
extension_data=self._extension_data
178+
ifignore_extension_data:
179+
extension_data=None
180+
write_cache(entries,stream,extension_data)
180181
returnself
181182

182183
#} END serializable interface
183184

184-
defwrite(self,file_path=None,ignore_tree_extension_data=False):
185+
defwrite(self,file_path=None,ignore_extension_data=False):
185186
"""Write the current state to our file path or to the given one
186187
187188
:param file_path:
@@ -190,9 +191,10 @@ def write(self, file_path=None, ignore_tree_extension_data=False):
190191
Please note that this will change the file_path of this index to
191192
the one you gave.
192193
193-
:paramignore_tree_extension_data:
194+
:paramignore_extension_data:
194195
If True, the TREE type extension data read in the index will not
195-
be written to disk. Use this if you have altered the index and
196+
be written to disk. NOTE that no extension data is actually written.
197+
Use this if you have altered the index and
196198
would like to use git-write-tree afterwards to create a tree
197199
representing your written changes.
198200
If this data is present in the written index, git-write-tree
@@ -208,7 +210,7 @@ def write(self, file_path=None, ignore_tree_extension_data=False):
208210
lfd=LockedFD(file_pathorself._file_path)
209211
stream=lfd.open(write=True,stream=True)
210212

211-
self._serialize(stream,ignore_tree_extension_data)
213+
self._serialize(stream,ignore_extension_data)
212214

213215
lfd.commit()
214216

@@ -612,7 +614,7 @@ def _entries_for_paths(self, paths, path_rewriter, fprogress, entries):
612614
returnentries_added
613615

614616
defadd(self,items,force=True,fprogress=lambda*args:None,path_rewriter=None,
615-
write=True):
617+
write=True,write_extension_data=False):
616618
"""Add files from the working tree, specific blobs or BaseIndexEntries
617619
to the index.
618620
@@ -689,8 +691,19 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
689691
Please note that entry.path is relative to the git repository.
690692
691693
:param write:
692-
If True, the index will be written once it was altered. Otherwise
693-
the changes only exist in memory and are not available to git commands.
694+
If True, the index will be written once it was altered. Otherwise
695+
the changes only exist in memory and are not available to git commands.
696+
697+
:param write_extension_data:
698+
If True, extension data will be written back to the index. This can lead to issues in case
699+
it is containing the 'TREE' extension, which will cause the `git commit` command to write an
700+
old tree, instead of a new one representing the now changed index.
701+
This doesn't matter if you use `IndexFile.commit()`, which ignores the `TREE` extension altogether.
702+
You should set it to True if you intend to use `IndexFile.commit()` exclusively while maintaining
703+
support for third-party extensions. Besides that, you can usually safely ignore the built-in
704+
extensions when using GitPython on repositories that are not handled manually at all.
705+
All current built-in extensions are listed here:
706+
http://opensource.apple.com/source/Git/Git-26/src/git-htmldocs/technical/index-format.txt
694707
695708
:return:
696709
List(BaseIndexEntries) representing the entries just actually added.
@@ -763,7 +776,7 @@ def handle_null_entries(self):
763776
self.entries[(entry.path,0)]=IndexEntry.from_base(entry)
764777

765778
ifwrite:
766-
self.write()
779+
self.write(ignore_extension_data=notwrite_extension_data)
767780
# END handle write
768781

769782
returnentries_added

‎git/test/test_index.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@
4444
BaseIndexEntry,
4545
IndexEntry
4646
)
47-
fromgitdb.test.libimportwith_rw_directory
4847
fromgit.index.funimporthook_path
4948

50-
importgit
51-
5249

5350
classTestIndex(TestBase):
5451

@@ -768,15 +765,3 @@ def test_index_bare_add(self, rw_bare_repo):
768765
exceptInvalidGitRepositoryError:
769766
asserted=True
770767
assertasserted,"Adding using a filename is not correctly asserted."
771-
772-
@with_rw_directory
773-
deftest_index_add_corruption(self,rw_dir):
774-
# Test for https://github.com/gitpython-developers/GitPython/issues/265
775-
repo=git.Repo.clone_from("git://pkgs.fedoraproject.org/GitPython",rw_dir)
776-
assertnotrepo.is_dirty()
777-
file_path=os.path.join(rw_dir,"GitPython.spec")
778-
open(file_path,'wb').close()
779-
assertrepo.is_dirty()
780-
repo.index.add(['0001-GPG-signature-support-on-commit-object.patch','GitPython.spec','.gitignore','sources'])
781-
repo.git.commit(m="committing file")
782-
assertnotrepo.is_dirty()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp