java source diff tool?

A

Alex Hunsley

This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?
For instance, the tool would consider the following two groups of lines identical:

group A: (just one line!)
if (evil == true && power > 7) { destroyStuff(); }

group B:


if (evil == true
&& power > 7) {
destroyStuff();
}



The reason I ask is that sometimes I run eclipse source auto formatting over
some weirdly formatted code that someone else has written, in order to make it
more standard and much easier to debug (because group A above is really bad for
stepping over with debugger in a diagnostic way!)

However, I then have the problem that my code is *very* different to the
checked in code, but there is *probably* no functional change, but it's hard to
tell.
(Yes, I should have done the reformat and checked the code straight back in,
but I didn't.. booo.)

I suppose I could always keep a copy of my altered code somewhere else, check
out the code and reformat AGAIN, and diff those two....hmmm.

alex
 
C

Chris Uppal

Alex said:
However, I then have the problem that my code is *very* different to the
checked in code, but there is *probably* no functional change, but it's
hard to tell.
(Yes, I should have done the reformat and checked the code straight back
in,
but I didn't.. booo.)

I suppose I could always keep a copy of my altered code somewhere else,
check
out the code and reformat AGAIN

It's probably better to do that, otherwise the history of the code (as seen by
the next developer to look at it) will consist of a load of mechanical layout
changes, with some "real" changes buried in amongst them. Far better to leave
a trail with two steps, one that /only/ changes the layout (and has a comment
to that effect!), and then another that makes changes that have some real
effect. If you don't then you'll be leaving them exactly the same problem that
you are facing now.

-- chris
 
A

Andy Fish

Alex Hunsley said:
This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?

that would sure be nice, but I don't know of a tool that does it.

Araxis merge - my favourite diff tool - does at least allow you to edit
either of the files and updates the difference view dynamically without
having to save.

So what I typically do in your situation is temporarily edit one of the
files, either splitting lines up or joining them to compensate for
whitespace differences. Then if araxis removes the difference markers I know
that the whitespace was the only difference. After that I would quit without
saving. It sounds like a faff but I find it's not too bad.

Andy
 
R

Roedy Green

This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?
For instance, the tool would consider the following two groups of lines identical:

Here is another expedient way to skin the cat. Write a compactor than
coverts all multiple spaces to one. Then compare those two sources.

You can use http://mindprod.com/quoter.html to do it, or cannibalise
the code. You could write something a little smarter to strip
comments too.
 
A

Alistair Hutton

Alex said:
This is probably a vain hope, but here goes...
I suppose I could always keep a copy of my altered code somewhere else,
check out the code and reformat AGAIN, and diff those two....hmmm.

Another long term solution that doesn't fix your immediate problem
(sorry) is to write a source formater translator that sists between you
and the CVS repository. So when ever you check code in it gets
formatted to an agreed standard for the CVS repository but when you or
your co-worker who each have different preffered formatting styles
checks it out it gets converted to your favoured style.
 
J

John C. Bollinger

Alex said:
This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?
For instance, the tool would consider the following two groups of lines
identical:

group A: (just one line!)
if (evil == true && power > 7) { destroyStuff(); }

group B:


if (evil == true
&& power > 7) {
destroyStuff();
}

If you need to cover all possible cases then your best bet is to run
both sources through the same beautifier and diff the results. This is
because although many diff programs (e.g. GNU diff) can be told to
ignore changes in the amount of whitespace in the sources, they cannot
determine where it's OK to insert where some amount of whitespace should
be equivalent to no whitespace at all. With that caveat, however,
here's a quick hack-together that does what you asked for your
particular example inputs:

differ.sh:
==========
#! /bin/sh

perl -e 'while (<>) { chomp; print $_, " "; }' < $1 > ${1}.temp
perl -e 'while (<>) { chomp; print $_, " "; }' < $2 > ${2}.temp

diff -b ${1}.temp ${2}.temp

rm ${1}.temp ${2}.temp

===========

That depends on GNU diff, approximately recent Perl, and some flavor of
Bourne shell. It makes no effort to create safe temp file names. It
does not check its command line arguments in any way, and may fail
strangely if there are not at least two of them. It works by converting
all line terminators to spaces, and then "diff"ing the results while
ignoring changes in the amount of whitespace. There are code constructs
on which it would report a textual difference where there is no
syntactic difference; see above.

Perhaps it will be useful to you, perhaps not.


John Bollinger
(e-mail address removed)
 
B

Bent C Dalager

This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?

Borland's JBuilder X claims to be able to diff Java source code
regardless of code formatting differences, which seems to be what you
want.

If diffing is included in their "free" version of the product, you
might want to download it and give it a spin on your files.

Cheers
Bent D
 
R

Roedy Green

Another long term solution that doesn't fix your immediate problem
(sorry) is to write a source formater translator that sists between you
and the CVS repository.

does CVS have a hook for that, or does it have to plug into your IDE
which has a hook to CVS?
 
L

Liz

John C. Bollinger said:
If you need to cover all possible cases then your best bet is to run
both sources through the same beautifier and diff the results. This is
because although many diff programs (e.g. GNU diff) can be told to
ignore changes in the amount of whitespace in the sources, they cannot
determine where it's OK to insert where some amount of whitespace should
be equivalent to no whitespace at all. With that caveat, however,

Reminds me of FORTRAN DO statement. No space required between DO and
whatever follows it.
 
J

James D. Veale

You may want to look at the Complite File Comparison Family at:

http://world.std.com/~jdveale/index.html

These tools perform word-by-word comparisons where words can be
separated not only by white-space but by common programming language
delimiters. They can be easily configured to be indifferent
to the formatting changes in your two examples.

Native versions of these tools are available for Windows, OS/2, and DOS.

Since you appear to be posting from Windows, CompSerf or TigSerf
would most likely fit your needs.

Jim Veale

Alex Hunsley said:
This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?
For instance, the tool would consider the following two groups
of lines identical:
group A: (just one line!)
if (evil == true && power > 7) { destroyStuff(); }
group B:

if (evil == true
&& power > 7) {
destroyStuff();
}


The reason I ask is that sometimes I run eclipse source auto formatting over
some weirdly formatted code that someone else has written, in order to make it
more standard and much easier to debug (because group A above is really bad for
stepping over with debugger in a diagnostic way!)
However, I then have the problem that my code is *very* different to the
checked in code, but there is *probably* no functional change, but it's hard to
tell.
(Yes, I should have done the reformat and checked the code straight back in,
but I didn't.. booo.)
 
M

Marcin Grunwald

Alex said:
This is probably a vain hope, but here goes...
Does anyone know of a java source diff tool that ignores whitespace AND
newlines, and just looks at code blocks intelligently?

You can also compile with "javac -g:none" and compare binary files(*.class)
That way you can only check if files differ, it will not show you
differences.
 
R

Roedy Green

You can also compile with "javac -g:none" and compare binary files(*.class)
That way you can only check if files differ, it will not show you
differences.


The advantage of that technique is ANY change that is immaterial, e.g.
a local variable renaming, will not register as a delta.

The disadvantage is, if it tells you there is a difference, you still
have to find out what it is by some other means, such as disassembly
diff or source code comparison by eye. See
http://mindprod.com/jgloss/disassembler.html
http://mindprod.com/jgloss/decompiler.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top