File tree Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Expand file tree Collapse file tree 2 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -561,10 +561,9 @@ def _to_relative_path(self, path):
561561 return path
562562 if self .repo .bare :
563563 raise InvalidGitRepositoryError ("require non-bare repository" )
564- relative_path = path .replace (self .repo .working_tree_dir + os .sep , "" )
565- if relative_path == path :
564+ if not path .startswith (self .repo .working_tree_dir ):
566565 raise ValueError ("Absolute path %r is not in git repository at %r" % (path , self .repo .working_tree_dir ))
567- return relative_path
566+ return os . path . relpath ( path , self . repo . working_tree_dir )
568567
569568 def _preprocess_add_items (self , items ):
570569 """ Split the items into two lists of path strings and BaseEntries. """
Original file line number Diff line number Diff line change @@ -838,6 +838,17 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
838838 r .index .add ([fp ])
839839 r .index .commit ('Added [.exe' )
840840
841+ def test__to_relative_path_at_root (self ):
842+ root = osp .abspath (os .sep )
843+ class Mocked (object ):
844+ bare = False
845+ git_dir = root
846+ working_tree_dir = root
847+ repo = Mocked ()
848+ path = os .path .join (root , 'file' )
849+ index = IndexFile (repo )
850+ index ._to_relative_path (path )
851+
841852 @with_rw_repo ('HEAD' , bare = True )
842853 def test_pre_commit_hook_success (self , rw_repo ):
843854 index = rw_repo .index
You can’t perform that action at this time.
0 commit comments