Skip to content

Commit 7ab12b4

Browse files
committed
fix(index):allow adding non-unicode paths to index
This issue only surfaced in python 2, in case paths containing unicode characters were not actual unicode objects. In python 3, this was never the issue. Closes #331
1 parent 8324c4b commit 7ab12b4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

‎git/index/fun.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
from gitdb.typ import str_tree_type
4242
from git.compat import (
4343
defenc,
44-
force_text
44+
force_text,
45+
force_bytes
4546
)
4647

4748
S_IFGITLINK = S_IFLNK | S_IFDIR # a submodule
@@ -124,7 +125,7 @@ def write_cache(entries, stream, extension_data=None, ShaStreamCls=IndexFileSHA1
124125
write(entry[4]) # ctime
125126
write(entry[5]) # mtime
126127
path = entry[3]
127-
path = path.encode(defenc)
128+
path = force_bytes(path, encoding=defenc)
128129
plen = len(path) & CE_NAMEMASK # path length
129130
assert plen == len(path), "Path %s too long to fit into index" % entry[3]
130131
flags = plen | (entry[2] & CE_NAMEMASK_INV) # clear possible previous values

‎git/test/test_index.py

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
)
1919
from git import (
2020
IndexFile,
21+
Repo,
2122
BlobFilter,
2223
UnmergedEntriesError,
2324
Tree,
@@ -45,6 +46,7 @@
4546
IndexEntry
4647
)
4748
from git.index.fun import hook_path
49+
from gitdb.test.lib import with_rw_directory
4850

4951

5052
class TestIndex(TestBase):
@@ -780,3 +782,14 @@ def test_index_bare_add(self, rw_bare_repo):
780782
except InvalidGitRepositoryError:
781783
asserted = True
782784
assert asserted, "Adding using a filename is not correctly asserted."
785+
786+
@with_rw_directory
787+
def test_add_utf8P_path(self, rw_dir):
788+
# NOTE: fp is not a Unicode object in python 2 (which is the source of the problem)
789+
fp = os.path.join(rw_dir, 'ø.txt')
790+
with open(fp, 'w') as fs:
791+
fs.write('content of ø')
792+
793+
r = Repo.init(rw_dir)
794+
r.index.add([fp])
795+
r.index.commit('Added orig and prestable')

0 commit comments

Comments
 (0)