604
+ write = True ):
566
605
"""Add files from the working tree, specific blobs or BaseIndexEntries
567
606
to the index.
568
607
@@ -637,7 +676,7 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
637
676
:param write:
638
677
If True, the index will be written once it was altered. Otherwise
639
678
the changes only exist in memory and are not available to git commands.
640
-
679
+
641
680
:return:
642
681
List(BaseIndexEntries) representing the entries just actually added.
643
682
@@ -649,71 +688,43 @@ def add(self, items, force=True, fprogress=lambda *args: None, path_rewriter=Non
649
688
# sort the entries into strings and Entries, Blobs are converted to entries
650
689
# automatically
651
690
# paths can be git-added, for everything else we use git-update-index
652
- entries_added = list ()
653
691
paths , entries = self ._preprocess_add_items (items )
654
- if paths and path_rewriter :
655
- for path in paths :
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
- def store_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
- if S_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
- return BaseIndexEntry ((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
683
696
if paths :
684
- assert len (entries_added ) == 0
685
- added_files = list ()
686
- for filepath in self ._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 ))
690
698
691
699
# HANDLE ENTRIES
692
700
if entries :
693
- null_mode_entries = [e for e in entries if e .mode == 0 ]
701
+ null_mode_entries = [ e for e in entries if e .mode == 0 ]
694
702
if null_mode_entries :
695
- raise ValueError (
696
- "At least one Entry has a null-mode - please use index.remove to remove files for clarity" )
703
+ raise ValueError ("At least one Entry has a null-mode - please use index.remove to remove files for clarity" )
697
704
# END null mode should be remove
698
705
699
706
# HANLDE ENTRY OBJECT CREATION
700
707
# create objects if required, otherwise go with the existing shas
701
- null_entries_indices = [i for i , e in enumerate (entries ) if e .binsha == Object .NULL_BIN_SHA ]
708
+ null_entries_indices = [ i for i ,e in enumerate (entries ) if e .binsha == Object .NULL_BIN_SHA ]
702
709
if null_entries_indices :
703
- for ei in null_entries_indices :
704
- null_entry = entries [ei ]
705
- new_entry = store_path (null_entry .path )
706
-
707
- # update null entry
708
- entries [ei ] = BaseIndexEntry ((null_entry .mode , new_entry .binsha , null_entry .stage , null_entry .path ))
709
- # END for each entry index
710
+ @git_working_dir
711
+ def handle_null_entries (self ):
712
+ for ei in null_entries_indices :
713
+ null_entry = entries [ei ]
714
+ new_entry = self ._store_path (null_entry .path , fprogress )
715
+
716
+ # update null entry
717
+ entries [ei ] = BaseIndexEntry ((null_entry .mode , new_entry .binsha , null_entry .stage , null_entry .path ))
718
+ # END for each entry index
719
+ # end closure
720
+ handle_null_entries (self )
710
721
# END null_entry handling
711
722
712
723
# REWRITE PATHS
713
724
# If we have to rewrite the entries, do so now, after we have generated
714
725
# all object sha's
715
726
if path_rewriter :
716
- for i , e in enumerate (entries ):
727
+ for i ,e in enumerate (entries ):
717
728
entries [i ] = BaseIndexEntry ((e .mode , e .binsha , e .stage , path_rewriter (e )))
718
729
# END for each entry
719
730
# END handle path rewriting
@@ -733,11 +744,11 @@ def store_path(filepath):
733
744
# add the new entries to this instance
734
745
for entry in entries_added :
735
746
self .entries [(entry .path , 0 )] = IndexEntry .from_base (entry )
736
-
747
+
737
748
if write :
738
749
self .write ()
739
750
# END handle write
740
-
751
+
741
752
return entries_added
742
753
743
754
def _items_to_rela_paths (self , items ):
0 commit comments