Yurkee
Yurkee

Reputation: 873

git checkout -p with specified encoding

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?

Upvotes: 3

Views: 351

Answers (1)

joanis
joanis

Reputation: 12229

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.

Upvotes: 1

Related Questions