3

I'm working with the mixed encoding files in a repo.

My system $LANG is en_US.UTF8, file encoding is iso-8859-1. when I run git checkout -p HEAD file git uses UTF8 to show me differences.

And I see something like this:

-               "�" - EUR
-               "�" - GBP
-               "�" - JPY
+               "�" - EUR
+               "�" - GBP
+               "�" - JPY
Discard this hunk from index and worktree [y,n,q,a,d,/,j,J,g,e,?]?

The problem is, that "�" is not the correct content of a file. When I run iconv -f iso-8859-1 -t UTF8 file | less I see

"¤" - EUR
"£" - GBP
"¥" - JPY

When I accept or discard a change when running git checkout -p I'd like to see exactly what characters changed instead of "�" character, which is uninformative. How can I do it?

Yurkee
  • 795
  • 2
  • 9
  • 23
  • I guess "switch everything to UTF-8" would not be a useful answer... I can just feel the pain of an incomplete conversion, which we've had for a long time too. Could you temporarily set your terminal to iso-8859-1 while doing the operation? This commands might do the trick, temporarily setting the encoding just for that command line, and using `gvim` which should adapt to that encoding too: `LANG=en_US.iso-8859-1 GIT_EDITOR=gvim git checkout -p HEAD file`? (Note: all on one line, variable definitions preceding `git` without semi colon or export - ask if you're not familiar with this.) – joanis Jul 30 '19 at 18:22
  • So, @Yurkee, was my answer below any help? Did you manage to solve this problem, one way or another? – joanis Aug 02 '19 at 23:59
  • kinda. Didnt work out exactly with gvim, and its hard to say why, but it directed me into a solution that was good for the moment, but wouldn't work out in larger cases. – Yurkee Aug 05 '19 at 14:24

1 Answers1

1

If you use a GUI editor that pays attention to LANG, you should able to invoke it with a different encoding than your terminal's by temporarily setting LANG on its command line.

For example, in a utf-8 terminal, I use this syntax to edit iso-8859-1 files in gvim:

LANG=en_US.iso-8859-1 gvim file-encoded-in-latin1

You should be able to accomplish the same temporary setting for git checkout -p by temporarily changing the Git editor:

LANG=en_US.iso-8859-1 GIT_EDITOR=gvim git checkout -p HEAD file

If you cannot use a GUI editor, I'm not sure how you could do this, because Git will always show you the real contents of the file, and the terminal controls the display encoding. That's why I think you would need to pop out to a GUI editor like this.

joanis
  • 10,635
  • 14
  • 30
  • 40
  • Although using gvim didnt work out, your solution directed me into right place, and u'r right that this can be solved with graphical text interface. For this problem I decided to run iconv parallel to checkout -p, and it gave acceptable results, but was time consuming. – Yurkee Aug 05 '19 at 14:23