18

I need to generate a full-context git diff programmatically for a web ui.

A CLI for generating a full-context diff was covered in questions:

The prevalent answer is something like git diff -U99999

With a -U / --unified option with a ridiculously high threshold (e.g. 999,999), doing git diff -U999999:

  1. Makes me suspect that there could be a performance hit
  2. Even worse, it's a correctness issue if my file is larger than 1M lines

Is there a -U option to show the whole file?

Community
  • 1
  • 1
Aleksandr Levchuk
  • 3,751
  • 4
  • 35
  • 47

2 Answers2

9

If you just use a large number with -U, you could choose the large number to be the point at which your application can't handle displaying such a large file (diff).

it's a correctness issue if my file is larger than 1M lines

And to address this issue, you can check the output for more than one @@ ... @@ line to determine whether it's complete — this allows you to avoid silently giving a wrong number.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • 8
    Experimentally, I found `git diff -U999999999999999999` gave `Segmentation fault (core dumped)`. However, one digit less, `-U99999999999999999`, worked. Also, adding two or more digits to the first command, `-U999999999999999999999999`, caused it to behave as though the option had actually been `-U0`. – Mr. Lance E Sloan May 03 '16 at 19:47
  • "-U 999999999" for current OS X (more 9s will fail) – lilalinux Jul 14 '23 at 12:23
1

Frankly, the best option is to use git difftool rather than vanilla git diff. To see which tools your version of git supports, enter

git difftool --tool-help

which, with my version (2.3.0), shows the following

$ git difftool --tool-help
'git difftool --tool=<tool>' may be set to one of the following:
        araxis
        gvimdiff
        gvimdiff2
        gvimdiff3
        meld
        vimdiff
        vimdiff2
        vimdiff3

The following tools are valid, but not currently available:
        bc
        bc3
        codecompare
        deltawalker
        diffmerge
        diffuse
        ecmerge
        emerge
        kdiff3
        kompare
        opendiff
        p4merge
        tkdiff
        xxdiff

I usually use meld, but that's just a personal preference. git difftool takes the same arguments as git diff plus a few to help with the process (I find -y useful to prevent the prompts when moving from one file to the next).

To check out the changes introduced by a specific commit, for example, you can use

git difftool -y -t meld 08f0f82^..08f0f82

obviously replacing 08f0f82 with the correct SHA-1.

My biggest complaint is that it launches the tool for each modified file in sequence (hence specifying the -y option).

If you only wanted to examine the changes to a particular file in that commit, you can just add the filename to the command line.

git difftool -y -t meld 08f0f82^..08f0f82 myfile.c

Obviously, this is for interactive use - not for scripting

kdopen
  • 8,032
  • 7
  • 44
  • 52