Skip to content

Commit 8232b17

Browse files
author
Ruben DI BATTISTA
committed
fix: Allow adding PathLike object to index
Close #1382
1 parent 38e9a18 commit 8232b17

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

‎git/index/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,11 @@ def _preprocess_add_items(
617617
paths = []
618618
entries = []
619619
# if it is a string put in list
620-
if isinstance(items, str):
620+
if isinstance(items, (str, os.PathLike)):
621621
items = [items]
622622

623623
for item in items:
624-
if isinstance(item, str):
624+
if isinstance(item, (str, os.PathLike)):
625625
paths.append(self._to_relative_path(item))
626626
elif isinstance(item, (Blob, Submodule)):
627627
entries.append(BaseIndexEntry.from_blob(item))

‎test/test_index.py

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import os.path as osp
3838
from git.cmd import Git
3939

40+
from pathlib import Path
41+
4042
HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
4143

4244
is_win_without_bash = is_win and not shutil.which("bash.exe")
@@ -943,3 +945,12 @@ def test_commit_msg_hook_fail(self, rw_repo):
943945
assert str(err)
944946
else:
945947
raise AssertionError("Should have caught a HookExecutionError")
948+
949+
@with_rw_repo('HEAD')
950+
def test_index_add_pathlike(self, rw_repo):
951+
git_dir = Path(rw_repo.git_dir)
952+
953+
file = git_dir / "file.txt"
954+
file.touch()
955+
956+
rw_repo.index.add(file)

0 commit comments

Comments
 (0)