Skip to content

Commit

Permalink
diff: Support .gitattribute encodings when applying diffs
Browse files Browse the repository at this point in the history
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
davvid committed May 29, 2012
1 parent e06e134 commit 11e27e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cola/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions cola/diffparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from cola import utils
from cola import gitcmds
from cola import gitcfg


class DiffParser(object):
Expand All @@ -18,13 +19,15 @@ 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
self.end = None
self.offset = None
self.diffs = []
self.selected = []
self.filename = filename

(header, diff) = gitcmds.diff_helper(head=self.head,
amending=self.amending,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cola/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down

0 comments on commit 11e27e3

Please sign in to comment.