java source diff tool?

Discussion in 'Java' started by Alex Hunsley, Jun 23, 2004.

  1. Alex Hunsley

    Alex Hunsley Guest

    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
    Alex Hunsley, Jun 23, 2004
    #1
    1. Advertising

  2. Alex Hunsley

    Chris Uppal Guest

    Alex Hunsley wrote:

    > 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
    Chris Uppal, Jun 23, 2004
    #2
    1. Advertising

  3. Alex Hunsley

    Andy Fish Guest

    "Alex Hunsley" <> wrote in message
    news:...
    > 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
    Andy Fish, Jun 23, 2004
    #3
  4. Alex Hunsley

    Roedy Green Guest

    On Wed, 23 Jun 2004 13:28:51 +0100, Alex Hunsley
    <> wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 23, 2004
    #4
  5. Alex Hunsley

    Roedy Green Guest

    On Wed, 23 Jun 2004 13:28:51 +0100, Alex Hunsley
    <> wrote or quoted :

    >Does anyone know of a java source diff tool that ignores whitespace AND
    >newlines, and just looks at code blocks intelligently?


    another technique is to put both sets of code through a beautifier
    before comparing.


    see http://mindprod.com/jgloss/beautifier.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 23, 2004
    #5
  6. Alex Hunsley wrote:
    > This is probably a vain hope, but here goes...

    <SNIP>
    >
    > 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.

    --
    Alistar Hutton
    Alistair Hutton, Jun 24, 2004
    #6
  7. Alex Hunsley wrote:

    > 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
    John C. Bollinger, Jun 24, 2004
    #7
  8. In article <>,
    Alex Hunsley <> wrote:
    >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
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
    Bent C Dalager, Jun 24, 2004
    #8
  9. Alex Hunsley

    Roedy Green Guest

    On Thu, 24 Jun 2004 14:27:46 +0100, Alistair Hutton
    <> wrote or quoted :

    >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?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 24, 2004
    #9
  10. Alex Hunsley

    Liz Guest

    "John C. Bollinger" <> wrote in message
    news:cbenp5$ife$...
    > Alex Hunsley wrote:
    >
    > > 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,


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

    > 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
    >
    Liz, Jun 25, 2004
    #10
  11. 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 <> writes:
    >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
    James D. Veale, Jun 25, 2004
    #11
  12. Alex Hunsley wrote:

    > 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.

    --
    Cheers
    grundig
    Marcin Grunwald, Jun 25, 2004
    #12
  13. Alex Hunsley

    Roedy Green Guest

    On Fri, 25 Jun 2004 10:34:40 +0200, Marcin Grunwald <>
    wrote or quoted :

    >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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 25, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    501
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    620
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,907
    Steve Fulton
    Aug 31, 2004
  4. Dacong Yan
    Replies:
    21
    Views:
    2,317
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    179
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page