Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

diff: Support .gitattribute encodings when applying diffs

Teach the diff editor to apply diffs for files with non-utf-8 encodings.
We now write patch files according to the encoding specified using
.gitattributes.

Closes #96

Reported-by: @mmark on github.com
Helped-by: Ingo Weinhold <ingo_weinhold@gmx.de>
Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information...
commit 11e27e3531e315dd672ad2296a8d6a7e0f136bc3 1 parent e06e134
@davvid davvid authored
Showing with 14 additions and 6 deletions.
  1. +4 −2 cola/core.py
  2. +8 −2 cola/diffparse.py
  3. +2 −2 cola/utils.py
View
6 cola/core.py
@@ -35,10 +35,12 @@ def decode(enc, encoding=None):
return unicode(enc)
-def encode(unenc):
+def encode(unenc, encoding=None):
"""encode(unencoded_string) returns a string encoded in utf-8
"""
- return unenc.encode('utf-8', 'replace')
+ if encoding is None:
+ encoding = 'utf-8'
+ return unenc.encode(encoding, 'replace')
@interruptable
View
10 cola/diffparse.py
@@ -3,6 +3,7 @@
from cola import utils
from cola import gitcmds
+from cola import gitcfg
class DiffParser(object):
@@ -18,6 +19,7 @@ def __init__(self, model, filename='',
self._diff_spans = []
self._diff_offsets = []
+ self.config = gitcfg.instance()
self.head = model.head
self.amending = model.amending()
self.start = None
@@ -25,6 +27,7 @@ def __init__(self, model, filename='',
self.offset = None
self.diffs = []
self.selected = []
+ self.filename = filename
(header, diff) = gitcmds.diff_helper(head=self.head,
amending=self.amending,
@@ -49,7 +52,9 @@ def write_diff(self,filename,which,selected=False,noop=False):
"""Writes a new diff corresponding to the user's selection."""
if not noop and which < len(self.diffs):
diff = self.diffs[which]
- utils.write(filename, self.header + '\n' + diff + '\n')
+ encoding = self.config.file_encoding(self.filename)
+ utils.write(filename, self.header + '\n' + diff + '\n',
+ encoding=encoding)
return True
else:
return False
@@ -221,12 +226,13 @@ def process_diff_selection(self, selected, offset, selection,
status = 0
# Process diff selection only
if selected:
+ encoding = self.config.file_encoding(self.filename)
for idx in self.selected:
contents = self.diff_subset(idx, start, end)
if not contents:
continue
tmpfile = utils.tmp_filename('selection')
- utils.write(tmpfile, contents)
+ utils.write(tmpfile, contents, encoding=encoding)
if apply_to_worktree:
stat, out = self.model.apply_diff_to_worktree(tmpfile)
output += out
View
4 cola/utils.py
@@ -237,10 +237,10 @@ def slurp(path):
return core.decode(slushy)
-def write(path, contents):
+def write(path, contents, encoding=None):
"""Writes a raw string to a file."""
fh = open(core.encode(path), 'wb')
- core.write(fh, core.encode(contents))
+ core.write(fh, core.encode(contents, encoding=encoding))
fh.close()
Please sign in to comment.
Something went wrong with that request. Please try again.