Skip to content

Index.add fails when certain patterns are entered that could be file system globs #994

Description

@krcrouse

It looks like add() attempts to glob any string path, which fails when a verbatim filename string matches an invalid glob regular expression. I don't see a way to override the globbing of the filename.

Here is a simple example:


import git 
repo = git.Repo()
repo.index.add('filename [2019-2020].txt')

Result:


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/git/index/base.py", line 740, in add
    entries_added.extend(self._entries_for_paths(paths, path_rewriter, fprogress, entries))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/git/util.py", line 68, in wrapper
    return func(self, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/git/index/util.py", line 91, in set_git_working_dir
    return func(self, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/git/index/base.py", line 625, in _entries_for_paths
    for filepath in self._iter_expand_paths(paths):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/git/index/base.py", line 378, in _iter_expand_paths
    resolved_paths = glob.glob(abs_path)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/glob.py", line 20, in glob
    return list(iglob(pathname, recursive=recursive))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/glob.py", line 72, in _iglob
    for name in glob_in_dir(dirname, basename, dironly):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/glob.py", line 83, in _glob1
    return fnmatch.filter(names, pattern)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/fnmatch.py", line 52, in filter
    match = _compile_pattern(pat)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/fnmatch.py", line 46, in _compile_pattern
    return re.compile(res).match
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 234, in compile
    return _compile(pattern, flags)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 286, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 930, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 426, in _parse_sub
    not nested and not items))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 816, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 426, in _parse_sub
    not nested and not items))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 580, in _parse
    raise source.error(msg, len(this) + 1 + len(that))
re.error: bad character range 9-2 at position 18

Activity

  1. changed the title [-]Index.add fails when certain regular expressions are entered[/-] [+]Index.add fails when certain patterns are entered that could be file system globs[/+] on Mar 6, 2020
  2. krcrouse commented on Mar 6, 2020

    @krcrouse
    Author

    I've fixed this in a local clone of the repository - the issue comes about because any string-based path that has certain characters are assumed to be attempting to use filename globs in the path resolution.

    This can be fixed by only resorting to the glob logic if the path is not pointing to an existing file.

    So, working off of commit d39bd53 (Harmon, Feb 25), here's the 2-line patch:

    
    diff --git a/git/index/base.py b/git/index/base.py
    index 8ff0f98..2569e3d 100644
    --- a/git/index/base.py
    +++ b/git/index/base.py
    @@ -373,8 +373,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable):
                         continue
                 # end check symlink
     
    -            # resolve globs if possible
    -            if '?' in path or '*' in path or '[' in path:
    +            # if the path is not already pointing to an existing file, resolve globs if possible
    +            if not os.path.exists(path) and ('?' in path or '*' in path or '[' in path):
                     resolved_paths = glob.glob(abs_path)
                     # not abs_path in resolved_paths:
                     #   a glob() resolving to the same path we are feeding it with
    li
    
    
  3. Byron commented on Mar 8, 2020

    @Byron
    Member

    Thanks for letting us know, and for posting the fix.

    I have applied the patch and hope it will make it into the next patch release!

  4. added this to the v3.1.1 - Bugfixes milestone on Mar 8, 2020
  5. added a commit that references this issue on Apr 28, 2025
    0420b01
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