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

Commitca4c1b8

Browse files
committed
Merge branch 'master' ofhttps://github.com/moshevds/GitPython into moshevds-master
Fixed an issue with path_rewriter code branch, but there is more to doConflicts:git/index/base.pygit/test/test_index.py
2 parentscebda2d +4d4138c commitca4c1b8

File tree

2 files changed

+90
-49
lines changed

2 files changed

+90
-49
lines changed

‎git/index/base.py

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171

7272
classIndexFile(LazyMixin,diff.Diffable,Serializable):
73-
7473
"""
7574
Implements an Index that can be manipulated using a native implementation in
7675
order to save git command function calls wherever possible.
@@ -561,8 +560,48 @@ def _preprocess_add_items(self, items):
561560
return (paths,entries)
562561

563562
@git_working_dir
564-
defadd(self,items,force=True,fprogress=lambda*args:None,path_rewriter=None,
565-
write=True):
563+
def_store_path(self,filepath,fprogress):
564+
"""Store file at filepath in the database and return the base index entry"""
565+
st=os.lstat(filepath)# handles non-symlinks as well
566+
stream=None
567+
ifS_ISLNK(st.st_mode):
568+
stream=StringIO(os.readlink(filepath))
569+
else:
570+
stream=open(filepath,'rb')
571+
# END handle stream
572+
fprogress(filepath,False,filepath)
573+
istream=self.repo.odb.store(IStream(Blob.type,st.st_size,stream))
574+
fprogress(filepath,True,filepath)
575+
returnBaseIndexEntry((stat_mode_to_index_mode(st.st_mode),
576+
istream.binsha,0,to_native_path_linux(filepath)))
577+
578+
@git_working_dir
579+
def_entries_for_paths(self,paths,path_rewriter,fprogress,entries):
580+
entries_added=list()
581+
ifpath_rewriter:
582+
forpathinpaths:
583+
abspath=os.path.abspath(path)
584+
gitrelative_path=abspath[len(self.repo.working_tree_dir)+1:]
585+
blob=Blob(self.repo,Blob.NULL_BIN_SHA,
586+
stat_mode_to_index_mode(os.stat(abspath).st_mode),
587+
to_native_path_linux(gitrelative_path))
588+
# TODO: variable undefined
589+
entries.append(BaseIndexEntry.from_blob(blob))
590+
# END for each path
591+
del(paths[:])
592+
# END rewrite paths
593+
594+
# HANDLE PATHS
595+
assertlen(entries_added)==0
596+
forfilepathinself._iter_expand_paths(paths):
597+
entries_added.append(self._store_path(filepath,fprogress))
598+
# END for each filepath
599+
# END path handling
600+
returnentries_added
601+
602+
603+
defadd(self,items,force=True,fprogress=lambda*args:None,path_rewriter=None,
604+
write=True):
566605
"""Add files from the working tree, specific blobs or BaseIndexEntries
567606
to the index.
568607
@@ -637,7 +676,7 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
637676
:param write:
638677
If True, the index will be written once it was altered. Otherwise
639678
the changes only exist in memory and are not available to git commands.
640-
679+
641680
:return:
642681
List(BaseIndexEntries) representing the entries just actually added.
643682
@@ -649,61 +688,29 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
649688
# sort the entries into strings and Entries, Blobs are converted to entries
650689
# automatically
651690
# paths can be git-added, for everything else we use git-update-index
652-
entries_added=list()
653691
paths,entries=self._preprocess_add_items(items)
654-
ifpathsandpath_rewriter:
655-
forpathinpaths:
656-
abspath=os.path.abspath(path)
657-
gitrelative_path=abspath[len(self.repo.working_tree_dir)+1:]
658-
blob=Blob(self.repo,Blob.NULL_BIN_SHA,
659-
stat_mode_to_index_mode(os.stat(abspath).st_mode),
660-
to_native_path_linux(gitrelative_path))
661-
entries.append(BaseIndexEntry.from_blob(blob))
662-
# END for each path
663-
del(paths[:])
664-
# END rewrite paths
665-
666-
defstore_path(filepath):
667-
"""Store file at filepath in the database and return the base index entry"""
668-
st=os.lstat(filepath)# handles non-symlinks as well
669-
stream=None
670-
ifS_ISLNK(st.st_mode):
671-
stream=StringIO(os.readlink(filepath))
672-
else:
673-
stream=open(filepath,'rb')
674-
# END handle stream
675-
fprogress(filepath,False,filepath)
676-
istream=self.repo.odb.store(IStream(Blob.type,st.st_size,stream))
677-
fprogress(filepath,True,filepath)
678-
returnBaseIndexEntry((stat_mode_to_index_mode(st.st_mode),
679-
istream.binsha,0,to_native_path_linux(filepath)))
680-
# END utility method
681-
682-
# HANDLE PATHS
692+
entries_added=list()
693+
# This code needs a working tree, therefore we try not to run it unless required.
694+
# That way, we are OK on a bare repository as well.
695+
# If there are no paths, the rewriter has nothing to do either
683696
ifpaths:
684-
assertlen(entries_added)==0
685-
added_files=list()
686-
forfilepathinself._iter_expand_paths(paths):
687-
entries_added.append(store_path(filepath))
688-
# END for each filepath
689-
# END path handling
697+
entries_added.extend(self._entries_for_paths(paths,path_rewriter,fprogress,entries))
690698

691699
# HANDLE ENTRIES
692700
ifentries:
693-
null_mode_entries= [eforeinentriesife.mode==0]
701+
null_mode_entries= [eforeinentriesife.mode==0]
694702
ifnull_mode_entries:
695-
raiseValueError(
696-
"At least one Entry has a null-mode - please use index.remove to remove files for clarity")
703+
raiseValueError("At least one Entry has a null-mode - please use index.remove to remove files for clarity")
697704
# END null mode should be remove
698705

699706
# HANLDE ENTRY OBJECT CREATION
700707
# create objects if required, otherwise go with the existing shas
701-
null_entries_indices= [ifori,einenumerate(entries)ife.binsha==Object.NULL_BIN_SHA]
708+
null_entries_indices= [ifori,einenumerate(entries)ife.binsha==Object.NULL_BIN_SHA]
702709
ifnull_entries_indices:
703710
foreiinnull_entries_indices:
704711
null_entry=entries[ei]
705-
new_entry=store_path(null_entry.path)
706-
712+
new_entry=self._store_path(null_entry.path,fprogress)
713+
707714
# update null entry
708715
entries[ei]=BaseIndexEntry((null_entry.mode,new_entry.binsha,null_entry.stage,null_entry.path))
709716
# END for each entry index
@@ -713,7 +720,7 @@ def store_path(filepath):
713720
# If we have to rewrite the entries, do so now, after we have generated
714721
# all object sha's
715722
ifpath_rewriter:
716-
fori,einenumerate(entries):
723+
fori,einenumerate(entries):
717724
entries[i]=BaseIndexEntry((e.mode,e.binsha,e.stage,path_rewriter(e)))
718725
# END for each entry
719726
# END handle path rewriting
@@ -733,11 +740,11 @@ def store_path(filepath):
733740
# add the new entries to this instance
734741
forentryinentries_added:
735742
self.entries[(entry.path,0)]=IndexEntry.from_base(entry)
736-
743+
737744
ifwrite:
738745
self.write()
739746
# END handle write
740-
747+
741748
returnentries_added
742749

743750
def_items_to_rela_paths(self,items):

‎git/test/test_index.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
importtime
2121
fromstatimport*
2222

23+
fromStringIOimportStringIO
24+
fromgitdb.baseimportIStream
25+
fromgit.objectsimportBlob
26+
fromgit.index.typimportBaseIndexEntry
27+
2328

2429
classTestIndex(TestBase):
2530

@@ -671,3 +676,32 @@ def test_index_new(self):
671676
index=IndexFile.new(self.rorepo,*args)
672677
assertisinstance(index,IndexFile)
673678
# END for each arg tuple
679+
680+
@with_rw_repo('HEAD',bare=True)
681+
deftest_index_bare_add(self,rw_bare_repo):
682+
# Something is wrong after cloning to a bare repo, reading the
683+
# property rw_bare_repo.working_tree_dir will return '/tmp'
684+
# instead of throwing the Exception we are expecting. This is
685+
# a quick hack to make this test fail when expected.
686+
rw_bare_repo._working_tree_dir=None
687+
contents='This is a StringIO file'
688+
filesize=len(contents)
689+
fileobj=StringIO(contents)
690+
filename='my-imaginary-file'
691+
istream=rw_bare_repo.odb.store(
692+
IStream(Blob.type,filesize,fileobj))
693+
entry=BaseIndexEntry((100644,istream.binsha,0,filename))
694+
try:
695+
rw_bare_repo.index.add([entry])
696+
exceptAssertionError,e:
697+
self.fail("Adding to the index of a bare repo is not allowed.")
698+
699+
# Adding using a path should still require a non-bare repository.
700+
asserted=False
701+
path=os.path.join('git','test','test_index.py')
702+
try:
703+
rw_bare_repo.index.add([path])
704+
exceptException,e:
705+
asserted="does not have a working tree"ine.message
706+
assertasserted,"Adding using a filename is not correctly asserted."
707+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp