Skip to content

Commit 4d4138c

Browse files
committed
Refactored the working tree dependent code for add.
Adding to a bare repository is now possible because @git_working_dir is only used on the relevant methods.
1 parent f913337 commit 4d4138c

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

git/index/base.py

+44-40
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,48 @@ def _preprocess_add_items(self, items):
562562
# END for each item
563563
return (paths, entries)
564564

565+
566+
@git_working_dir
567+
def _store_path(self, filepath, fprogress):
568+
"""Store file at filepath in the database and return the base index entry"""
569+
st = os.lstat(filepath) # handles non-symlinks as well
570+
stream = None
571+
if S_ISLNK(st.st_mode):
572+
stream = StringIO(os.readlink(filepath))
573+
else:
574+
stream = open(filepath, 'rb')
575+
# END handle stream
576+
fprogress(filepath, False, filepath)
577+
istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream))
578+
fprogress(filepath, True, filepath)
579+
return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode),
580+
istream.binsha, 0, to_native_path_linux(filepath)))
581+
565582
@git_working_dir
583+
def _entries_for_paths(self, paths, path_rewriter, fprogress):
584+
entries_added = list()
585+
if path_rewriter:
586+
for path in paths:
587+
abspath = os.path.abspath(path)
588+
gitrelative_path = abspath[len(self.repo.working_tree_dir)+1:]
589+
blob = Blob(self.repo, Blob.NULL_BIN_SHA,
590+
stat_mode_to_index_mode(os.stat(abspath).st_mode),
591+
to_native_path_linux(gitrelative_path))
592+
entries.append(BaseIndexEntry.from_blob(blob))
593+
# END for each path
594+
del(paths[:])
595+
# END rewrite paths
596+
597+
# HANDLE PATHS
598+
assert len(entries_added) == 0
599+
added_files = list()
600+
for filepath in self._iter_expand_paths(paths):
601+
entries_added.append(self._store_path(filepath, fprogress))
602+
# END for each filepath
603+
# END path handling
604+
return entries_added
605+
606+
566607
def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=None,
567608
write=True):
568609
"""Add files from the working tree, specific blobs or BaseIndexEntries
@@ -651,47 +692,10 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
651692
# sort the entries into strings and Entries, Blobs are converted to entries
652693
# automatically
653694
# paths can be git-added, for everything else we use git-update-index
654-
entries_added = list()
655695
paths, entries = self._preprocess_add_items(items)
656-
if paths and path_rewriter:
657-
for path in paths:
658-
abspath = os.path.abspath(path)
659-
gitrelative_path = abspath[len(self.repo.working_tree_dir)+1:]
660-
blob = Blob(self.repo, Blob.NULL_BIN_SHA,
661-
stat_mode_to_index_mode(os.stat(abspath).st_mode),
662-
to_native_path_linux(gitrelative_path))
663-
entries.append(BaseIndexEntry.from_blob(blob))
664-
# END for each path
665-
del(paths[:])
666-
# END rewrite paths
667-
668-
669-
def store_path(filepath):
670-
"""Store file at filepath in the database and return the base index entry"""
671-
st = os.lstat(filepath) # handles non-symlinks as well
672-
stream = None
673-
if S_ISLNK(st.st_mode):
674-
stream = StringIO(os.readlink(filepath))
675-
else:
676-
stream = open(filepath, 'rb')
677-
# END handle stream
678-
fprogress(filepath, False, filepath)
679-
istream = self.repo.odb.store(IStream(Blob.type, st.st_size, stream))
680-
fprogress(filepath, True, filepath)
681-
return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode),
682-
istream.binsha, 0, to_native_path_linux(filepath)))
683-
# END utility method
684-
685-
686-
# HANDLE PATHS
696+
entries_added = list()
687697
if paths:
688-
assert len(entries_added) == 0
689-
added_files = list()
690-
for filepath in self._iter_expand_paths(paths):
691-
entries_added.append(store_path(filepath))
692-
# END for each filepath
693-
# END path handling
694-
698+
entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress))
695699

696700
# HANDLE ENTRIES
697701
if entries:
@@ -706,7 +710,7 @@ def store_path(filepath):
706710
if null_entries_indices:
707711
for ei in null_entries_indices:
708712
null_entry = entries[ei]
709-
new_entry = store_path(null_entry.path)
713+
new_entry = self._store_path(null_entry.path, fprogress)
710714

711715
# update null entry
712716
entries[ei] = BaseIndexEntry((null_entry.mode, new_entry.binsha, null_entry.stage, null_entry.path))

0 commit comments

Comments
 (0)