Skip to content

Commit 127a1cc

Browse files
pedroasadjimporter
authored andcommitted
Support Unicode in files when generating commits
Previously, there was an issue where `FileInfo` objects could be constructed with the data as a `str`, confusing `git fast-import`, since `len(my_str)` returns the number of code points, not the size in bytes.
1 parent 87a6393 commit 127a1cc

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

CHANGES.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changes
22

3+
## v1.1.1 (in progress)
4+
5+
### Bug fixes
6+
7+
- Fix support for Unicode in redirection templates
8+
9+
---
10+
311
## v1.1.0 (2021-09-01)
412

513
### New features

mike/git_utils.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def try_rebase_branch(remote, branch, force=False):
130130

131131
class FileInfo:
132132
def __init__(self, path, data, mode=0o100644):
133+
if isinstance(data, str):
134+
data = data.encode('utf-8')
133135
self.path = path
134136
self.data = data
135137
self.mode = mode
@@ -171,10 +173,17 @@ def __exit__(self, exc_type, exc_value, traceback):
171173
self.finish()
172174

173175
def _write(self, data):
174-
if isinstance(data, str): # pragma: no branch
176+
if isinstance(data, str):
175177
data = data.encode('utf-8')
176178
return self._pipe.stdin.write(data)
177179

180+
def _write_data(self, data):
181+
if isinstance(data, str):
182+
data = data.encode('utf-8')
183+
self._write('data {}\n'.format(len(data)))
184+
self._write(data)
185+
self._write('\n')
186+
178187
def _start_commit(self, branch, message):
179188
name = get_config('user.name')
180189
if re.search(r'[<>\n]', name):
@@ -190,9 +199,7 @@ def _start_commit(self, branch, message):
190199
self._write('committer {name}<{email}> {time}\n'.format(
191200
name=name + ' ' if name else '', email=email, time=make_when()
192201
))
193-
self._write('data {length}\n{message}\n'.format(
194-
length=len(message), message=message
195-
))
202+
self._write_data(message)
196203
try:
197204
head = get_latest_commit(branch)
198205
self._write('from {}\n'.format(head))
@@ -210,9 +217,7 @@ def add_file(self, file_info):
210217
self._write('M {mode:06o} inline {path}\n'.format(
211218
path=git_path(file_info.path), mode=file_info.mode
212219
))
213-
self._write('data {}\n'.format(len(file_info.data)))
214-
self._write(file_info.data)
215-
self._write('\n')
220+
self._write_data(file_info.data)
216221

217222
def finish(self):
218223
if self._finished:

test/unit/test_git_utils.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ def setUp(self):
243243
self.stage = stage_dir('commit')
244244
git_init()
245245

246-
def _add_file(self, name, branch='master'):
246+
def _add_file(self, name, branch='master', data='this is some text'):
247247
with git_utils.Commit(branch, 'add file') as commit:
248-
commit.add_file(git_utils.FileInfo(name, 'this is some text'))
248+
commit.add_file(git_utils.FileInfo(name, data))
249249

250250
def test_add_file(self):
251251
self._add_file('file.txt')
@@ -254,6 +254,13 @@ def test_add_file(self):
254254
with open('file.txt') as f:
255255
self.assertEqual(f.read(), 'this is some text')
256256

257+
def test_add_file_unicode(self):
258+
self._add_file('file.txt', data='レッサーパンダ')
259+
check_call_silent(['git', 'checkout', 'master'])
260+
assertDirectory('.', {'file.txt'})
261+
with open('file.txt', encoding='utf-8') as f:
262+
self.assertEqual(f.read(), 'レッサーパンダ')
263+
257264
def test_add_file_to_dir(self):
258265
self._add_file(os.path.join('dir', 'file.txt'))
259266
check_call_silent(['git', 'checkout', 'master'])

0 commit comments

Comments
 (0)