1

I have many many files without extensions. I have one file called "model" and another file called "schema" which I know are both UTF-16. Both "model" and "schema" have no extension so are forced to binary when trying to git diff. I want to convert "model" to UTF-8 and "schema" to JSON using .gitattributes so I can compare diffs.

Q1. How can I specify individual filenames without extensions to be correctly decoded?

Q2. I don't want to add bash scripts or change git --global settings so that its easier to use for others.

I could'nt find any example from the docs. I tried following this solution except only using *model diff=utf16 and git config file:

[diff "utf16"]
textconv = "iconv -f utf-16 -t utf-8"

but this didn't work. I've tried this in both VScode and on bash (Ubuntu20.04).

UPDATE: @torek pointed me to helpful Git debugging tool. Here is my output from GIT_TRACE=1 git diff

09:58:09.927031 git.c:439               trace: built-in: git diff
09:58:09.951640 run-command.c:663       trace: run_command: unset GIT_PAGER_IN_USE; LESS=FRX LV=-c pager

Q1 SOLUTION Both @torek and @jthill helped confirm the issue was on my end. The solution above should have worked but my local git config wasn't pointing to the .gitconfig file. I updated the .git/config file and it worked.

Q2 SOLUTION You must edit global settings once for each repo to reference .gitconfig. I've tried following this solution but can't seem to get it to revert to the .gitconfig file in the repo worktree root.

izzleee
  • 315
  • 3
  • 11
  • 3
    This looks like the right track. `*model` should match, but if you just want the literal file name `model`, write it without the leading `*`. You should be able to run `GIT_TRACE=1 git diff` and see it invoke the text conversion filter, and if so (or even if not), debug from there. – torek Sep 10 '21 at 23:37
  • Thanks @torek. I've added the GIT_TRACE output in the solution but the diff is still to binary files: `Binary files a/model and b/model differ`. How do I know if git diff is picking up the `diff=utf16` textconv arg? – izzleee Sep 11 '21 at 00:04
  • 2
    Based on the trace output, it's not; so now the question is to figure out why not. (If it had, you'd see it run the specified diff filter.) – torek Sep 11 '21 at 00:09
  • 2
    For what little it's worth, adding `Makefile diff=foo` to `.gitattributes`, adding `[diff "foo"]` set to `textconv = cat` to `.git/config`, and one line `foo` to the Makefile, and running `GIT_TRACE=1 git diff`, I get: `17:11:46.196341 run-command.c:663 trace: run_command: cat /tmp/SPVsgm_Makefile` just before it shows me the three modified files that aren't `.git/config`. (This is preceded by the same kind of trace you saw, but note the `run_command: cat` part.) – torek Sep 11 '21 at 00:13

1 Answers1

2

so are forced to binary

By what?

I do:

$ echo '/Makefile myattr=test' >.git/info/attributes
$ git ls-files ':(attr:myattr=test)' 
Makefile
$ echo 'Makefile myattr=test' >.git/info/attributes
$ git ls-files ':(attr:myattr=test)' | wc -l
24

so there's 24 Makefiles and the anchored pattern matches only the specific one I want, because I anchored the path.

So put /model diff=utf16 in your .gitattributes if that file's at the work tree root.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks @jthill. This confirms something is wrong on my end and also shown by toreks comments above. – izzleee Sep 11 '21 at 00:22