Skip to content

Commit 392c72c

Browse files
author
Ruben DI BATTISTA
committed
fix: Allow adding PathLike object to index
Close #1382
1 parent b30720e commit 392c72c

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
@@ -594,11 +594,11 @@ def _preprocess_add_items(self, items: Sequence[Union[PathLike, Blob, BaseIndexE
594594
paths = []
595595
entries = []
596596
# if it is a string put in list
597-
if isinstance(items, str):
597+
if isinstance(items, (str, os.PathLike)):
598598
items = [items]
599599

600600
for item in items:
601-
if isinstance(item, str):
601+
if isinstance(item, (str, os.PathLike)):
602602
paths.append(self._to_relative_path(item))
603603
elif isinstance(item, (Blob, Submodule)):
604604
entries.append(BaseIndexEntry.from_blob(item))

test/test_index.py

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import os.path as osp
5252
from git.cmd import Git
5353

54+
from pathlib import Path
55+
5456
HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
5557

5658
is_win_without_bash = is_win and not shutil.which('bash.exe')
@@ -937,3 +939,12 @@ def test_commit_msg_hook_fail(self, rw_repo):
937939
assert str(err)
938940
else:
939941
raise AssertionError("Should have caught a HookExecutionError")
942+
943+
@with_rw_repo('HEAD')
944+
def test_index_add_pathlike(self, rw_repo):
945+
git_dir = Path(rw_repo.git_dir)
946+
947+
file = git_dir / "file.txt"
948+
file.touch()
949+
950+
rw_repo.index.add(file)

0 commit comments

Comments
 (0)