Skip to content

Commit 0c43ee3

Browse files
author
Ruben DI BATTISTA
committed
fix: Allow adding PathLike object to index
Close gitpython-developers#1382
1 parent b30720e commit 0c43ee3

File tree

2 files changed

+62
-77
lines changed

2 files changed

+62
-77
lines changed

git/index/base.py

+37-45
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,69 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

77
import glob
8-
from io import BytesIO
98
import os
10-
from stat import S_ISLNK
9+
import os.path as osp
1110
import subprocess
1211
import tempfile
13-
14-
from git.compat import (
15-
force_bytes,
16-
defenc,
17-
)
18-
from git.exc import (
19-
GitCommandError,
20-
CheckoutError,
21-
GitError,
22-
InvalidGitRepositoryError
23-
)
24-
from git.objects import (
25-
Blob,
26-
Submodule,
27-
Tree,
28-
Object,
29-
Commit,
12+
from io import BytesIO
13+
from stat import S_ISLNK
14+
from typing import (
15+
IO,
16+
TYPE_CHECKING,
17+
Any,
18+
BinaryIO,
19+
Callable,
20+
Dict,
21+
Iterable,
22+
Iterator,
23+
List,
24+
NoReturn,
25+
Sequence,
26+
Tuple,
27+
Type,
28+
Union,
3029
)
30+
31+
import git.diff as git_diff
32+
from git.compat import defenc, force_bytes
33+
from git.exc import CheckoutError, GitCommandError, GitError, InvalidGitRepositoryError
34+
from git.objects import Blob, Commit, Object, Submodule, Tree
3135
from git.objects.util import Serializable
36+
from git.types import Commit_ish, PathLike
3237
from git.util import (
3338
LazyMixin,
3439
LockedFD,
35-
join_path_native,
3640
file_contents_ro,
41+
join_path_native,
42+
to_bin_sha,
3743
to_native_path_linux,
3844
unbare_repo,
39-
to_bin_sha
4045
)
4146
from gitdb.base import IStream
4247
from gitdb.db import MemoryDB
4348

44-
import git.diff as git_diff
45-
import os.path as osp
46-
4749
from .fun import (
50+
S_IFGITLINK,
51+
aggressive_tree_merge,
4852
entry_key,
49-
write_cache,
5053
read_cache,
51-
aggressive_tree_merge,
52-
write_tree_from_cache,
54+
run_commit_hook,
5355
stat_mode_to_index_mode,
54-
S_IFGITLINK,
55-
run_commit_hook
56-
)
57-
from .typ import (
58-
BaseIndexEntry,
59-
IndexEntry,
60-
)
61-
from .util import (
62-
TemporaryFileSwap,
63-
post_clear_cache,
64-
default_index,
65-
git_working_dir
56+
write_cache,
57+
write_tree_from_cache,
6658
)
59+
from .typ import BaseIndexEntry, IndexEntry
60+
from .util import TemporaryFileSwap, default_index, git_working_dir, post_clear_cache
6761

6862
# typing -----------------------------------------------------------------------------
6963

70-
from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List, NoReturn,
71-
Sequence, TYPE_CHECKING, Tuple, Type, Union)
7264

73-
from git.types import Commit_ish, PathLike
7465

7566
if TYPE_CHECKING:
7667
from subprocess import Popen
77-
from git.repo import Repo
68+
7869
from git.refs.reference import Reference
70+
from git.repo import Repo
7971
from git.util import Actor
8072

8173

@@ -594,11 +586,11 @@ def _preprocess_add_items(self, items: Sequence[Union[PathLike, Blob, BaseIndexE
594586
paths = []
595587
entries = []
596588
# if it is a string put in list
597-
if isinstance(items, str):
589+
if isinstance(items, (str, os.PathLike)):
598590
items = [items]
599591

600592
for item in items:
601-
if isinstance(item, str):
593+
if isinstance(item, (str, os.PathLike)):
602594
paths.append(self._to_relative_path(item))
603595
elif isinstance(item, (Blob, Submodule)):
604596
entries.append(BaseIndexEntry.from_blob(item))

test/test_index.py

+25-32
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,36 @@
55
# This module is part of GitPython and is released under
66
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
77

8-
from io import BytesIO
98
import os
10-
from stat import (
11-
S_ISLNK,
12-
ST_MODE
13-
)
9+
import os.path as osp
10+
import shutil
1411
import tempfile
12+
from io import BytesIO
13+
from pathlib import Path
14+
from stat import S_ISLNK, ST_MODE
15+
from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo
1516
from unittest import skipIf
16-
import shutil
1717

1818
from git import (
19-
IndexFile,
20-
Repo,
2119
BlobFilter,
22-
UnmergedEntriesError,
23-
Tree,
24-
Object,
20+
CheckoutError,
2521
Diff,
2622
GitCommandError,
27-
CheckoutError,
23+
IndexFile,
24+
Object,
25+
Repo,
26+
Tree,
27+
UnmergedEntriesError,
2828
)
29+
from git.cmd import Git
2930
from git.compat import is_win
30-
from git.exc import (
31-
HookExecutionError,
32-
InvalidGitRepositoryError
33-
)
31+
from git.exc import HookExecutionError, InvalidGitRepositoryError
3432
from git.index.fun import hook_path
35-
from git.index.typ import (
36-
BaseIndexEntry,
37-
IndexEntry
38-
)
33+
from git.index.typ import BaseIndexEntry, IndexEntry
3934
from git.objects import Blob
40-
from test.lib import (
41-
TestBase,
42-
fixture_path,
43-
fixture,
44-
with_rw_repo
45-
)
46-
from test.lib import with_rw_directory
47-
from git.util import Actor, rmtree
48-
from git.util import HIDE_WINDOWS_KNOWN_ERRORS, hex_to_bin
35+
from git.util import HIDE_WINDOWS_KNOWN_ERRORS, Actor, hex_to_bin, rmtree
4936
from gitdb.base import IStream
5037

51-
import os.path as osp
52-
from git.cmd import Git
53-
5438
HOOKS_SHEBANG = "#!/usr/bin/env sh\n"
5539

5640
is_win_without_bash = is_win and not shutil.which('bash.exe')
@@ -937,3 +921,12 @@ def test_commit_msg_hook_fail(self, rw_repo):
937921
assert str(err)
938922
else:
939923
raise AssertionError("Should have caught a HookExecutionError")
924+
925+
@with_rw_repo('HEAD')
926+
def test_index_add_pathlike(self, rw_repo):
927+
git_dir = Path(rw_repo.git_dir)
928+
929+
file = git_dir / "file.txt"
930+
file.touch()
931+
932+
rw_repo.index.add(file)

0 commit comments

Comments
 (0)