Take the 2-minute tour ×

I'm trying to maintain a set of patches, which needs to be applied to several versions/branches of the code. The current diff/patch method (and git cherry-picking) is not flexible enough, mainly due to the dependency of the context. For example assume my patch adds a few constants in a java class. If in one version of the code another constant was inserted, exactly at the same place, patch will fail. In a general circumstance this is ok, since the order might be important, however in case of java code the order of the fields (at leaast in 99% of the cases) doesn't matter. In another example I wanted to insert one line right after another one. However the context contains in total 6 lines, and the patch is applied normally only if all of those matches.

I tried to manually edit the diff files, to keeping for example only one line of context, but then patch failed with the message "broken patch".

So I would like to know if there is a better (open source preferably) alternative to diff/patch which is more flexible.

Thank you in advance! /Pal

share|improve this question
add comment

1 Answer

Try to use git format-patch -U1 <revisions>, it produces a patch with 1 line of context.

share|improve this answer
While that's probably a decent work around it's a bit of a shame if there aren't any language aware diff tools that could do this in a much better way. –  Thor84no Oct 19 '11 at 9:41
I believe it is so just because it would work worse than any regular text diff. It sounds counter-intuitive, but I guess it's true. Meanwhile a lot of different text files could mean the same from a language compiler point of view, but for a human - the difference is meaningful. First of all, a source code is a text files, because good a code is written for humans, not for robots. –  kan Oct 19 '11 at 9:57
It's not hard to determine that .java files are written in Java and it's certainly possible to know whether a difference makes any actual difference as long as you know the language. I assume the reason is simply that it's hard and a lot of work to get right. –  Thor84no Oct 19 '11 at 10:02
What exactly does mean the actual difference? From user perspective only whitespace changes are meaningless. And most tools already able to ignore whitespaces. –  kan Oct 19 '11 at 10:11
Have you never done refactoring and clean up? In large Classes there is absolutely an advantage in tidying things up, putting them in a more natural order (for example order by public/protected/private) etc. Clean up by it's very nature should NOT change functionality. Your example just shows why it's hard (which is what I said) not why it's not desirable (which seems to be what you're arguing). The fact is a language aware diff tool would be better equipped at showing you what has changed in a program than a pure text one. –  Thor84no Oct 19 '11 at 12:29
show 4 more comments

Your Answer


