Skip to content

Commit 5e56226

Browse files
author
Ruben DI BATTISTA
committed
fix: Allow adding PathLike object to index
Close #1382
1 parent 1188b48 commit 5e56226

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

git/index/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,11 @@ def _preprocess_add_items(self, items: Sequence[Union[PathLike, Blob, BaseIndexE
616616
paths = []
617617
entries = []
618618
# if it is a string put in list
619-
if isinstance(items, str):
619+
if isinstance(items, (str, os.PathLike)):
620620
items = [items]
621621

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

test/test_index.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import os.path as osp
4141
from git.cmd import Git
4242

43+
from pathlib import Path
44+
4345
HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
4446

4547
is_win_without_bash = is_win and not shutil.which("bash.exe")
@@ -946,3 +948,12 @@ def test_commit_msg_hook_fail(self, rw_repo):
946948
assert str(err)
947949
else:
948950
raise AssertionError("Should have caught a HookExecutionError")
951+
952+
@with_rw_repo('HEAD')
953+
def test_index_add_pathlike(self, rw_repo):
954+
git_dir = Path(rw_repo.git_dir)
955+
956+
file = git_dir / "file.txt"
957+
file.touch()
958+
959+
rw_repo.index.add(file)

0 commit comments

Comments
 (0)