File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Expand file tree Collapse file tree 2 files changed +17
-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,21 @@ 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+
844+ class Mocked (object ):
845+ bare = False
846+ git_dir = root
847+ working_tree_dir = root
848+
849+ repo = Mocked ()
850+ path = os .path .join (root , 'file' )
851+ index = IndexFile (repo )
852+
853+ rel = index ._to_relative_path (path )
854+ self .assertEqual (rel , os .path .relpath (path , root ))
855+
841856 @with_rw_repo ('HEAD' , bare = True )
842857 def test_pre_commit_hook_success (self , rw_repo ):
843858 index = rw_repo .index
You can’t perform that action at this time.
0 commit comments