How to limit number of diff lines shown in gitk? - diff

Looking for an option/workaround to limit the diff content (shown in the bottom panel) for any particular file (e.g. to first N lines only), as the patch/diff may be too big and causes the GUI to freeze. I did try the different options accepted by git diff, but don't see any way to pass them to gitk (unlike git-gui). Note that limiting the number of lines of context does not work since the diff will still be shown with zero context.

You will have to change gitk's source code. It's one big Tcl/Tk file you can edit directly or make a copy of. It's location may depend on your operating system. If you are on linux or macos it's probably /usr/bin/gitk or /usr/local/bin/gitk.
Inside the procedure diffcmd, change this line:
set cmd [concat | git diff-tree -r $flags $ids]
You could add diff-tree arguments, or simply pipe the diff output to another program, like head:
set cmd [concat | git diff-tree -r $flags $ids | head -n500]
This applies to the complete diff, not just to one file.

Related

Changing the commit message while grafting a large number of changesets using Mercurial

I need to graft a large number (thousands) of small changesets from branch A to branch B - but need to alter the commit message in the process.
The message change needs to look roughly like this:
"Ref XXX: Fixed foo and bar" -> "GRAFT: Ref YYY: Fixed foo and bar".
In other words, I need to prepend "GRAFT:" and change a reference number. If I can't make these replacements directly with Mercurial, I could create the new commit messages all in advance then make a script to apply each new message during its respective graft. Happily, Mercurial allows editing commit messages during grafting, with the -e argument:
https://www.mercurial-scm.org/repo/hg/help/graft
The problem is that this pops up a text editor for each changeset for me to make the change manually. There doesn't seem to be a way to amend the message in a programmatic way, or just to provide an entirely new message, at the comment line. Given the size of operation, using the editor each time isn't plausible.
My last option would be to use the text editor with some sort of AutoIt/Macro script to enter the right things in the places at the right times - but the thought of needing to resort to this is frankly making me feel a bit ill.
Save me from this ugly fate.
Thanks in advance.
A possible workaround is to specify the use of a shell script in lieu of an editor. For example:
#!/bin/sh
sed -e '1,1s/^/GRAFT: /' -i "$1"
We're making use of the fact that with -i, sed will do an in-place edit. Don't forget to make the shell script executable. Then you can run
hg graft --config ui.editor=/path/to/prepend-graft.sh -e -r <revision>
where /path/to/prepend-graft.sh is the path of the aforementioned shell script.
Changing a number may require code that's more complex than a sed script, but would follow the same approach.

Display the numbers of lines that were changed between two files

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.

What's the best way to perform a differential between a list of directories?

I am interested in looking at a list of directories and comparing the previous list with a current list of directories and setting up a script to do so. Maybe in perl or as a shell script.
Should I use something like diff? Programatically, what would be an ideal way to do this? For example let say I output the diff to an output file, if there is no diff then exit, if there is results, I want to see it.
Let's for example I have the following directories today:
/foo/bar/staging/abc
/foo/bar/staging/def
/foo/bar/staging/a1b2c3
Next day would look like this where a directory is either added, or renamed:
/foo/bar/staging/abc
/foo/bar/staging/def
/foo/bar/staging/ghi
/foo/bar/staging/a1b2c4
There might be better ways, but the way I typically do something like this is to run a find command in each directory root, and pipe the output to separate files. You can then diff the files using the diff tool of your choice. If you want to filter out certain directories or files, you can throw in some grep or grep -v commands in the pipeline, or you can experiment with options on the find command.
The other main option is to find a diff tool that offers directory/folder comparisons. Most of the goods ones support this, but I like the command line method, because you get more control over what you're diffing.
cd /my/directory/one
find . -print | sort > /temp/one.txt
cd /my/directory/two
find . -print | sort > /temp/two.txt
diff /temp/one.txt /temp/two.txt
also check the inotifywait command. it allows you to monitor files in RT.
You might also consider the find command using the -newer switch.
The usage is:
find . -newer timefile.txt -print
The -newer switch makes find return a list of files that are created or updated after the specified file's modification time. In the example above, any file created or updated after timefile.txt would be returned. You'd have to create a timefile.txt file, most likely once per day. Some versions of find have variations of newer that compare against other time stamps for a file (last modified, last accessed, last created, etc.)
This technique would not report a file that was deleted, however. A daily diff of the file listings could report that.

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?