Display the numbers of lines that were changed between two files - diff

I have two files: A and its modified version B. Is there some convenient way to display the numbers of lines that were added or changed in B (so basically the + lines in diff output)?
The solution would be even better if it scaled to multiple files (I intend to use it on the output of git diff), displaying the result in a format like filename:line.
Edit: my current idea is to use git difftool with some shell script and diff with --unchanged-group-format='' --old-line-format='' --new-line-format='%dn' (or something like that); these options are not listed in the diff manual on my system for some reason.

Related

diff ignore certain pattern in the file

I want to make diff between two files which contains lines beginning with "line_$NR". I want to make diff between the files making abstraction of the presence of "lines_$NR" but when the differences are printed I want lines_$NR to be displayed.
It is possible to do that?
Thanks.
I believe in this case, you have to preprocess your iput files to remove /^line_[0-9]*/, diff the resulting files, then recombine the diff output with the removed words according to line numbers in diff output.
Python's difflib should be very handy here, or same from perl. If you want to stick to shell, I suppose you could get by with awk.
If you don't need exact output, perhaps you can use diff's --line-format=... directive to inject actual line number in a diff, rather than the word you removed in preprocessing step.

diff tool with extra highlighting

GitHub's diff viewer has a nice feature that it has extra highlighting on changed lines. For example, in this diff, just a single word was inserted:
Are there other tools that display a diff like this? Pastebin's diff viewer and http://www.quickdiff.com/ don't do this, and even GitHub's gist doesn't do this if you tell it to highlight a .diff file.
I'd like some way to take two files or the output of diff and show this nice output without having to put it on GitHub. Does anything exist?
Kompare is able to highlight differences in a viewed patch, although it displays the file content in two different panes and not line over line like in your picture.
For diffing with highlighting I like KDiff3 better, but it will require two files to compare, it does not operate directly on patch files like kompare can.

How to get the cvs change list for a new file

As part of my work I've been asked to log the cvs commands for creating the changelists for files I've updated as follows:
cvs diff -r 1.172 -r 1.173 ./somefile.php
But if the file is newly created for that job, no previous version number exists so I can't compare it to anything. Ideally I'd like to compare it with an empty file so it shows all lines were added. Can this be done?
Initially the best way I found to do this was to use:
cvs annotate -r1.1 ./somefile.php
Assuming the 1.1 is the version number of the new file. It isn't a perfect solution as it displays the added lines in a different format to the diff.
Update
However a better solution, which now seems obvious, has just occurred to me. When creating a new file, first check it in to CVS blank, then check it in again with the code, so I can do something like...
cvs diff -r1.1 -r1.2 ./somefile.php

showing differences within a line in diff output

This StackOverflow answer has an image of KDiff3 highlighting intra-line differences. Does someone know of a tool which can show the same (ex, via color) on the command line?
Another way to think of this is wanting to diff each difference in a patch file.
I don't know if this is sufficiently command line for your purpose, but vimdiff can do this (even does colour). See for example the image in this related question.
I tried all the tools I found: wdiff, dwdiff, kdiff3, vimdiff to show the difference between two long and slightly different lines. My favourite is diff-highlight (part of git contrib)
it supports diff format - great advantage over tools requiring two files like (dwdiff), e.g. if you need to visualize the output of unit tests
it highlights with black+white or with color if you connect it to colordiff
highlights characterwise - helpful for comparing long lines without spaces (better than wdiff)
Installation
On Ubuntu, you probably already have it as part of git contrib (installed within the git deb package).
Copy or link it into your ~/bin folder from /usr/share/doc/git/contrib/diff-highlight/diff-highlight
Usage example
cat tmp.diff | diff-highlight | colordiff
Result:
Another intuitive way to see all word-sized differences (though not side-by-side) is to use wdiff together with colordiff (you might need to install both). An example of this would be:
wdiff -n {file-A} {file-A} | colordiff
You can optionally pipe this into less -R to scroll through the output (-R is used to show the colors in less).
I had a similar problem and wanted to avoid using vimdiff. I found dwdiff (which is available in Debian) to have several advantages over wdiff.
The most useful feature of dwdiff is that you can customise the delimiters with -d [CHARS], so it's useful for comparing all kinds of output. It also has color built in with the -c flag.
You might be able to use colordiff for this.
In their man page:
Any options passed to colordiff are
passed through to diff except for the
colordiff-specific option 'difftype',
e.g.
colordiff --difftype=debdiff file1
file2
Valid values for 'difftype' are: diff,
diffc, diffu, diffy, wdiff, debdiff;
these correspond to plain diffs,
context diffs, unified diffs,
side-by-side diffs, wdiff output and
debdiff output respectively. Use these
overrides when colordiff is not able
to determine the diff-type
automatically.
I haven't tested it, but the side-by-side output (as produced by diff -y file1 file2) might give you the equivalent of in-line differences.
ccdiff is a convenient dedicated tool for the task. Here is what an example may look like with it:
By default, it highlights the differences in color, but it can be used on a console without color support too.
The package is included in the main repository of Debian:
ccdiff is a colored diff that also colors inside changed lines.
All command-line tools that show the difference between two files fall short in showing minor changes visuably useful. ccdiff tries to give the look and feel of diff --color or colordiff, but extending the display of colored output from colored deleted and added lines to colors for deleted and addedd characters within the changed lines.

How do you use the diff command against two source trees

I tried running 'diff' against two source directories get a patch file with a 'diff' between the two directories.
diff -rupN flyingsaucer-R8pre2_b/ flyingsaucer-R8pre2/ > a.patch
The command above does not seem to work, it generates a diff of everything and I get a 13 MB file, when in reality, it should be a couple of changes.
Should work with any recent version of gnu diff (tested here with gnu diff 2.8.1.)
You might want to add -b (and perhaps -B) to ignore difference in white space which perhaps generate large patch files unnecessarily.
I don't see any reason why it wouldn't work. Try adding "wb" to the argument list to ignore whitespace changes. Are you sure you got the trailing slashes the same on both sides?