Interactive program to selectively exclude parts of a diff file - linux

Is there a program (preferably available on Cygwin) which I can use to "filter" a diff file interactively? i.e. I want something like git interactive add, except that I want to operate on a diff file. I have already discovered filterdiff, but I don't think that it supports interactive editing, only inclusion/exclusion of hunks based on a pre-defined search criteria.
My usage scenario: I have a patch in MQ, which I would like to split up per the tutorial here: https://www.mercurial-scm.org/wiki/MqTutorial#Split_a_patch_into_multiple_patches. In order to do so, I have to edit a patch file so that it includes only the (many) changes I want, and doing this manually with a text editor is kind of a pain.
Thanks!

You can probably get what you want using the record extension: https://www.mercurial-scm.org/wiki/RecordExtension
Apply the patch (but don't commit it) and then selectively commit chunk by chunk using record. You could do that with or without mq in the works.

Emacs’ diff-mode has commands to (un)apply or delete individual hunks of a diff. It also allows editing the hunks (keeping the hunk headers up to date automatically), and it has a hunk-split command that is a bit more powerful than that in git add -p.

Related

Auto-highlight parts of the text which has been changed

Is it possible in VIM to highlight parts of some text the user has added/changed in a file?
Following situation demonstrates this:
user has opened a file
user has made some changes and then :w
all differences between 1 and 2 should be highlighted
user has made some changes and then :w
all differences between 1 and 4 should be highlighted
and so on.
I am looking for a most compact solution - should it be an option or a simple vim script.
You can keep a backup of the original file, then :vsplit file.bak, execute :diffthis in both buffer.
The VIM plugin vim-gitgutter will indicate lines that have been added / removed / changed if the file you are editing is part of a Git repository. This might meet your needs.
For such difference reporting, it's best to use a revision control system. The numbers from your question then map cleanly to revision numbers, and there are indeed plugins that can show those differences (also across multiple revisions, and in the form of log messages, etc.)
The vcscommand.vim - CVS/SVN/SVK/git/hg/bzr integration plugin supports all widely-used systems, and offers this in the form of a vimdiff or a unified diff in a scratch buffer. There are also plugins (vim-signify - Advanced plugin for showing VCS diffs in the SignColumn, or GitGutter for Git) that visualize this neatly in the sign column.
If you don't / can't use a revision control system, there are plugins that show the differences to the saved version (but not across saves, as this information is not available).
If you want a lightweight, Vim-only revision control system solution that can do this, have a look at my writebackup plugin and writebackupVersionControl plugin plugins, which offer vimdiffs / unified diffs, too, without requiring an underlying revision control system.
Vim tracks and maintains an undo history which records all changes made to a file. You could use a plugin like 'sjl/gundo.vim' which gives you the ability to view diffs of subsequent changes to a file (very handy).
If you keep the file under vcs, like git, you can also see diffs. For git you should use 'tpope/vim-fugitive' and then you can view diffs in several different ways, most common being :Gdiff

Vim editing *diff file*, updating diff ranges automatically

Suppose I generate diff of my project before commit, let say using svn. Having one diff for all files in project is a very nice way to review changes before committing them. However sometimes I wish to edit those changes without reopening each file, simple edit the diff and have it reapplied. So I have made such key map(I have setup svn diff to use unified format with fair amount of surrounding lines):
map scde :w! tmpdiff<cr>:!svn revert -R .<cr>:!patch -p0 <tmpdiff<cr>
It works, but only partially; you can edit added lines, but if you mark lines as removed or want to add another line you get some trouble since specified diff ranges do not match with actual text present in the diff. One can update them for simple changes like adding a line, however it is tedious and quickly gets complicated if you make more sophisticated changes. Is there a way to edit diff in so that range would automatically update correctly? I have found that emacs has some diff mode for this(however I have not tried it), however I was unable to find solution for my needs using vim. Maye there someone can give some suggestions?
take a look at rediff. It automatically fixes the offsets within a patch file.

One-way diff file

I would like to generate diffs for the sake of doing incremental backups of an sql database.
Using the standard unix 'diff' tool generates unnecessarily large files, since they include the full text of deleted lines. I only need support to be able to patch in one direction (to generate the current db dump from the full dump and an incremental patch).
How would I go about doing this? I have tried so far using diff -e and patch -e, but it doesn't seem to be working correctly, as the resulting file is corrupt (possibly an issue with the 'ed' tool used in cygwin)
back in the old days, before Vim, there used to be a line-oriented UNIX editor called 'ed' ..
diff has an option built in ( -e option ) , with which you can create an edit script from the diff.
Check here: and look for the section "Edit Script"
http://en.wikipedia.org/wiki/Diff
http://docs.freebsd.org/info/diff/diff.info.ed_Scripts.html
here's an example:
http://www.araxis.com/merge/topic_diff_report_editscript.html
another way to do this is to create a patch file (see 'man patch')

How to store large amounts of text and retain versioning?

Any tips on how to store large amounts of text, such as programming code. So I need to retain the tabs, spaces, etc?
Also how could i keep versions like say someone edits one line, i can see the changes that have been made?
That's what various revision control systems are for.
Any of git, cvs, rcs, subversion and a host of others will work.
I agree with other posters that you probably want to use what has already been done. Sometimes rolling your own can be fun.
You could write a wrapper for the command line diff utils. Each user could have their own config to choose their preferred editor. The script would made a copy of the file, so you would have an orig and new. When done editing the script would kick off a diff and store that to disk and delete the original backup of the file. This way, you would only store the latest versions plus all diffs so you can revert back and also see the changes.
I would keep a log of all diffs created and tag them in a csv with the userid of the person who modified the file and the timestamp of the modification.

A visual patch tool for Linux

I've got a file and a patch for it. I'd like to visually apply the patch, t.i. see how the changes proposed by the patch look in context, make some corrections, and save the resulting file.
What tool can do that?
Neither of the visual diff tools (i.e. meld, diffuse, diffmerge) do what I want: they don't work with patches, they merely merge whole files.
I really like Kompare. It is just a (very nice) graphical interface for diff.
http://www.caffeinated.me.uk/kompare/
sudo apt-get install kompare
Creating and applying patches
Kompare is able to create a patch file
which lists only the differences
between two compared text files A and
B. Further, Kompare can apply a patch
file which was created this way to an
original file A and, in this manner,
recompute the contents of the
corresponding file B. This is a
comfortable utility for passing a
corrected version of a file to a
friend who already has an older
version of the same file, because only
the (relatively small) patch file has
to be delivered and the receiver can
generate the corrected file by
applying the patch to the original
file.
The patches created and applied by
Kompare are compatible to patch files
generated or applied by the command
line interface diff utility, because
Kompare is in fact merely a graphical
front end to diff and the patches are
created and applied by patch, which
gets called by Kompare.
See use vimdiff with a diff file.
gvim original.file +'vert diffpa the.patch'
This will open GVim and split the window, with the original on the left and the patch applied on the right. You can then add, remove, or change hunks, and save the changes. (Well, if you want to create a new.patch you'll have to run diff again, but that's not difficult.)
You may use the emacs ediff mode.
It lets you see and validate each chunk interactively.
For what it's worth, I have this handy script in my ~/bin directory:
#!/bin/bash
if [ "$1" == "--text" ] ; then shift; fi
if diff --brief $1 $2 ; then
exit 0
else
emacs -fn 8x13bold --eval '(ediff-files "'$1'" "'$2'")'
fi
Another option: if you use Eclipse, you can generate and apply patches visually.
The idea with visual diff tools is that you can:
make a backup copy of the patched file (or a new pristine checkout of the whole tree)
apply the patch
use the visual diff tool to review the changes in context
make any desired change to the patched file within the visual diff tool.
Some tools, such as meld or diffuse will automatically diff against the previous committed version of the files.
The key insight is that you CAN apply the patch, they discard everything you don't like as long as you have a backup copy, or if you are working on a clean checkout.
If you feel more comfortable with reading and modifying unified diffs, and just want to have more context for the diff, emacs has a fairly unique feature, which is next-error-follow-mode while viewing a diff file (diff major mode). That shows the context of a diff line in the target file.
On platforms where Kompare (a fairly nice piece of software if KDE is your cup of tea), I agree that Eclipse is a great option.
I have consistently been a reluctant Eclipse user AND I am consistently impressed by how well the tools work (once the platform finally starts up).
On Fedora, I've got just about every front end installed for each of the main version control engines (SVN - also serving my Windows machines, Mercurial, git, etc.);
Meld is quite nice (no patch interface though).
Submerge works well also.
But since Eclipse does know how to apply patches and Working Copy project created itself once I figured out to create the project in the folder containing my working copy, it may become my favorite SVN front end.
I normally use GitExtensions (.NET and Mono) which also supports applying patches interactively. I hope you are already familiar with git.
Edit: Everything mentioned below this line doesn't seem to be available directly in KDiff3. And as I mentioned before: I haven't tried it yet.
GitExtension normally uses KDiff3 as diff tool, which is able to view patches.
http://kdiff3.sourceforge.net/doc/kpart.html
Using it for patches it has only a two window view, (edit) and doesn't support merging :( but it also supports 3 way diff on complete folders etc.
Actually I never tryed it with patches.
Does xxdiff do what you are looking for?
I'd use meld.
Create two copies of the files, one without and another with the patch. Use meld to compare them, and you can see exactly what the patch is changing and make changes as necessary.
Seriously, why is this so hard?

Resources