Skip to content

Adding files to repository that is located directly in the root #743

Description

@efiop

This is a weird scenario, but if you init git repo in the root directory(e.g. D:\) and try to add a file using gitpython, it will throw a ValueError:

Absolute path `D:\\file` is not in git repository at `D:\\`

The reason for this is improper validation in _to_relative_path method located at git/index/base.py:

    def _to_relative_path(self, path):
        """:return: Version of path relative to our git directory or raise ValueError
        if it is not within our git direcotory"""
        if not osp.isabs(path):
            return path
        if self.repo.bare:
            raise InvalidGitRepositoryError("require non-bare repository")
        relative_path = path.replace(self.repo.working_tree_dir + os.sep, "")
        if relative_path == path:
            raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir))
        return relative_path

I.e. when path is /file and working_tree_dir is /, relative_path stays equal to path, because self.repo.working_tree + os.sep is equal to //. The way to solve this is to simply use os.path.relpath methond. Please see pull-request attached.

Activity

  1. added 6 commits that reference this issue on Apr 3, 2018
    6fa24ef
    450faa8
    ef6453c
    91846f2
    97164b0
    176474b
  2. Byron commented on Apr 3, 2018

    @Byron
    Member

    Thanks a lot for digging in a providing the fix with the issue :). I am looking forward to closing this one when the review items were addressed.

  3. added a commit that references this issue on Apr 3, 2018
    c554ab1
  4. efiop commented on Apr 3, 2018

    @efiop
    ContributorAuthor

    And thank you for the quick feedback!

  5. added a commit that references this issue on Apr 4, 2018
    0857d33
  6. added this to the v2.1.10 - Bugfixes milestone on Apr 4, 2018
  7. added a commit that references this issue on Dec 7, 2023
    1ec43f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions