Author Topic: Comparing source files  (Read 2745 times)

0 Members and 1 Guest are viewing this topic.

Offline AussieBruceTopic starter

  • Regular Contributor
  • *
  • Posts: 55
  • Country: au
Comparing source files
« on: March 07, 2024, 09:33:50 am »
I started to write my own solution but this is a universal problem. When comparing two text files, if there are line inserts or deletes, it derails the process. Can anyone suggest a - preferably free - compare tool that will deal effectively with this? Even one that will start off at specified line nos would be a big help. Thanks for any advice.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10026
  • Country: nz
Re: Comparing source files
« Reply #1 on: March 07, 2024, 09:36:21 am »
I'm not entirely sure I understand your issue, but have you tried 'winmerge' for comparing text files
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: AndyC_772, boB, DimitriP, JPortici, AussieBruce

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4109
  • Country: nz
Re: Comparing source files
« Reply #2 on: March 07, 2024, 09:50:58 am »
I'm not entirely sure I understand your issue, but have you tried 'winmerge' for comparing text files

That sounds like something specific to a particular operating system that not everyone uses (and the OP may not).

The standard command line utility 'diff' has existed since AT&T Unix version 6 and appears with minor variations in BSD (e.g. MacOS) and GNU/Linux. Windows users can access it in WSL.

Often "diff3" is more useful, when someone has made a branch from some version of a program, and meanwhile more development has been done on the trunk. When it comes time to merge your changes back to trunk it is better do do a 3-way compare between the current head of trunk, your branch, and the common ancestor.

I think diff3 originates with AT&T version 7, and it's available everywhere also.

git always does 3-way diffs (if one of the versions is not a direct ancestor of the other)
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6401
  • Country: ro
Re: Comparing source files
« Reply #3 on: March 07, 2024, 09:56:47 am »
Search for 'diff tool'.
I'm using Meld.

Online DiTBho

  • Super Contributor
  • ***
  • Posts: 3980
  • Country: gb
Re: Comparing source files
« Reply #4 on: March 07, 2024, 10:51:42 am »
Beyond Compare
it's commercial software for Wintel, but it's light years better than any open source alternative.

It compares files, directories, even trees, has multi-user locks, so you avoid making a mess on files that are opened by others, and your changes remain local, and can be "merged" later, under control (Git, Mercury, Svn, ... Doors-itself) anyway  ... and ' scriptable, it can be used to generate patches, and has plug-ins for integration into Understood{C, C++, Ada}, Doors and Stood AADL, AdaMulti, which are "versioned" professional work environments designed for team-working.

When I was a student, I got my Educational version of Beyond Compare for 50 euro. Still usable for personal stuff.
« Last Edit: March 07, 2024, 12:54:45 pm by DiTBho »
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 
The following users thanked this post: Smokey, cfbsoftware, srb1954

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7495
  • Country: nl
  • Current job: ATEX product design
Re: Comparing source files
« Reply #5 on: March 07, 2024, 11:42:16 am »
git will do that for you, just version control the file.
 

Online DiTBho

  • Super Contributor
  • ***
  • Posts: 3980
  • Country: gb
Re: Comparing source files
« Reply #6 on: March 07, 2024, 01:09:27 pm »
Git allows you to do versioning, but needs to use external tools for the interface as soon as you need advance features like "merge", and it certainly does integrate with neither Undertstood nor with Stood AADL, as similar open source tools do not exist.

Think that I even wrote a text console tool that does the same job as Meld (which has too many python dependencies for my taste), but inside an ncurses text-editor.

I've written both, and I like to run them on a serial console hardware VT220. Somehow they can also be used for real hobby projects. But I would never dream of using them for work things: because 'productivity' drops by at least 100 points.

For example, when you are in a team, and you need to analyze a source, without leaving the work environment, it is damn productive to open Understood and Beyond Compare at the same time (oh, even beetter without closing Doors!!!!), to see in real time what impact the changes have on the metrics.

It savev 5 minutes per module. Multiplied by 100-200 modules, a nice day makes the difference between being able to leave work at 4pm and being able to take the racing bike for a workout, or having to stay in the office or laboratory until 6pm when they then kick you out because they have to close.

Yup  8)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline srb1954

  • Super Contributor
  • ***
  • Posts: 1104
  • Country: nz
  • Retired Electronics Design Engineer
Re: Comparing source files
« Reply #7 on: March 07, 2024, 01:19:26 pm »
Beyond Compare
it's commercial software for Wintel, but it's light years better than any open source alternative.
Also available for the Mac.

Highly recommended. It is one of the best value commercial S/W packages that I have ever bought.
 
The following users thanked this post: cfbsoftware

Offline teb

  • Newbie
  • Posts: 1
  • Country: ca
  • Electrical Engineer & Hobbyist
Re: Comparing source files
« Reply #8 on: March 07, 2024, 02:00:16 pm »
If you're using git, you can set your merge diff tool: https://git-scm.com/docs/merge-config

Otherwise for most systems I really like kdiff3: https://kdiff3.sourceforge.net/. Especially for multi-way comparisons and it handles whitespace better than git and unix's diff implementations in my experience.

It's not DNS
There's no way it's DNS
It was DNS
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6038
  • Country: es
Re: Comparing source files
« Reply #9 on: March 07, 2024, 02:48:04 pm »
Notepad++ (Win only) also has pretty decent compare plugins.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6453
  • Country: fi
    • My home page and email address
Re: Comparing source files
« Reply #10 on: March 07, 2024, 04:35:18 pm »
Note that standard diff supports multiple formats for describing the differences, including --side-by-side.  I prefer the unified format (-u); you can adjust how many lines of context (common lines preceding or succeeding a change) is displayed by using -U N.  I also like to use -ab, i.e. 'assume input is text' and 'ignore changes in the amount of white space'.  It is not just an "one size fits all" solution at all, it has features and output format you can adjust to fit your needs.
In a terminal, diff -Nabur --color=always file1 file2 | less -r produces paged, colored output.

It turns out that this June, diff will be 50 years old!
« Last Edit: March 07, 2024, 04:37:54 pm by Nominal Animal »
 

Offline eutectique

  • Frequent Contributor
  • **
  • Posts: 416
  • Country: be
Re: Comparing source files
« Reply #11 on: March 07, 2024, 05:35:56 pm »
+1 for meld

To have a scrollable file with colours, say diff file1.c file2.c | gvim -R -
 

Offline RAPo

  • Frequent Contributor
  • **
  • Posts: 661
  • Country: nl
Re: Comparing source files
« Reply #12 on: March 07, 2024, 05:57:30 pm »
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6453
  • Country: fi
    • My home page and email address
Re: Comparing source files
« Reply #13 on: March 07, 2024, 06:49:08 pm »
Forgot to mention,
    diff -Nabur --color=always dir1/ dir2/ | less -r
compares all files in the two directories, producing unified diff output with (default) three lines of context.

The nice thing about such diffs is that you can apply the changes obtained via
    diff -Nabur dir1/ dir2/ > name.patch
by running
    (cd dir3/ && patch -p1 -i ../name.patch)
and revert the same changes by running
    (cd dir3/ && patch -p1 -R -i ../name.patch)
 
The following users thanked this post: DimitriP

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14695
  • Country: fr
Re: Comparing source files
« Reply #14 on: March 07, 2024, 08:38:16 pm »
I mostly use Meld these days for interactive compare/merge and diff for automated compare.
Note that these are based on pretty much the same algorithms and still don't handle *interleaved* changes over multiple lines all that well, they tend to group lines to minimize the number of different blocks and often require manual editing for merging when you have interleaved changes.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4109
  • Country: nz
Re: Comparing source files
« Reply #15 on: March 07, 2024, 08:38:29 pm »
Note that standard diff supports multiple formats for describing the differences, including --side-by-side.  I prefer the unified format (-u); you can adjust how many lines of context (common lines preceding or succeeding a change) is displayed by using -U N.  I also like to use -ab, i.e. 'assume input is text' and 'ignore changes in the amount of white space'.  It is not just an "one size fits all" solution at all, it has features and output format you can adjust to fit your needs.
In a terminal, diff -Nabur --color=always file1 file2 | less -r produces paged, colored output.

It turns out that this June, diff will be 50 years old!

"Any Colour You Like"

(50 years old, this time last year. You missed the starting gun.)
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6453
  • Country: fi
    • My home page and email address
Re: Comparing source files
« Reply #16 on: March 07, 2024, 10:10:25 pm »
"Any Colour You Like"

(50 years old, this time last year. You missed the starting gun.)
Did I?  I thought the initial release was in June 1974; not in 1973 like the Pink Floyd album.
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4109
  • Country: nz
Re: Comparing source files
« Reply #17 on: March 07, 2024, 10:29:28 pm »
"Any Colour You Like"

(50 years old, this time last year. You missed the starting gun.)
Did I?  I thought the initial release was in June 1974; not in 1973 like the Pink Floyd album.

I meant the music is 51. diff is only 50. Just punning around... Strangers passing in the street By chance two separate glances meet And I am you and what I see is me. (even older)
 

Offline Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6453
  • Country: fi
    • My home page and email address
Re: Comparing source files
« Reply #18 on: March 08, 2024, 01:50:23 am »
:-+

I'm a couple of years younger myself, born in '76.

As to colors, unified format diff uses one for file names, one for the change header, one for the common lines, and one for the deleted and one for added lines.  So it's not that useful, although you can set the colors using --palette= option.  Piping (without --color) to an editor that supports syntax highlighting for whatever language is being edited often yields more useful color highlighting, as suggested by eutectique above.

Using command-line tools in chains also makes it easy to look for something specific in the output, for example by piping the diff -Nabur output to grep -e pattern, to output only lines that contain or match POSIX basic regular expression pattern.
For anything repetitive, I create a (bash or dash) shell script, Makefile, or an awk filter or script.  Or write a throwaway tool program.

This way, I've got much wider palette of tools to combine to do what I want.  I don't want to learn a new graphical tool for every little task I have, so I learned how to combine simpler ones to do what I want with minimum cognitive effort, as suggested by the Unix philosophy and minimalist modular approach.
 

Offline AussieBruceTopic starter

  • Regular Contributor
  • *
  • Posts: 55
  • Country: au
Re: Comparing source files
« Reply #19 on: March 08, 2024, 07:29:21 am »
Thanks, that solved my problem in one, the functionality of that program is way beyond my needs.
 

Online DiTBho

  • Super Contributor
  • ***
  • Posts: 3980
  • Country: gb
Re: Comparing source files
« Reply #20 on: March 08, 2024, 12:20:00 pm »
I mostly use Meld these days for interactive compare/merge and diff for automated compare.
Note that these are based on pretty much the same algorithms and still don't handle *interleaved* changes over multiple lines all that well, they tend to group lines to minimize the number of different blocks and often require manual editing for merging when you have interleaved changes.

that's precisely +1 for Beyound Compare, as it does it, and it saves your time!
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline radiolistener

  • Super Contributor
  • ***
  • Posts: 3505
  • Country: ua
Re: Comparing source files
« Reply #21 on: March 28, 2024, 11:09:06 am »
I started to write my own solution but this is a universal problem. When comparing two text files, if there are line inserts or deletes, it derails the process. Can anyone suggest a - preferably free - compare tool that will deal effectively with this? Even one that will start off at specified line nos would be a big help. Thanks for any advice.

I'm using Meld. It allows to compare/merge/synchronize files and folders and also allows to see github status for files.
It very similar to old Araxis Merge, but Araxis is paid and Meld is free.
« Last Edit: March 28, 2024, 11:16:21 am by radiolistener »
 

Online DiTBho

  • Super Contributor
  • ***
  • Posts: 3980
  • Country: gb
Re: Comparing source files
« Reply #22 on: March 28, 2024, 01:05:33 pm »
my-c now has its own dedicated "diff" tool ("/usr/bin/myc-diff") for comparing two sources.

Instead of processing two text files as an "ascii" stream, the tool calls the my-c lexifier and creates two ASTs, one for each source, then compares the ASTs.

This way
- as a side effect, when you diff two my-c files, you also "format" them
- the comparison is totally immune to tabulations, spaces, interleaving...

Not exactly perfect, as it misses a graphic interface, syntax color highlighting and all the extra features you can find in { meld, beyond compare, ..}; it only works with curses and vt220 (directly driven), but I'm proud of it  8)
The opposite of courage is not cowardice, it is conformity. Even a dead fish can go with the flow
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14695
  • Country: fr
Re: Comparing source files
« Reply #23 on: March 28, 2024, 10:39:15 pm »
That's an interesting approach, although obviously quite different. As I understand it, you could have two "completely" different source files, if the corresponding ASTs are the same, it would show no difference. It would then miss any code style difference, comments, etc. That's interesting, but I personally think code style and comments are important, so missing them may not be all that good. But that's just me.
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6401
  • Country: ro
Re: Comparing source files
« Reply #24 on: March 29, 2024, 07:36:53 am »
Worked once for a company that was using Perforce (git was pretty new back then).  IIRC Perforce was a proprietary and payed only CVS (Control Versioning System), however, their merge tool was free, called P4Merge.  Diffing source files with P4Merge was looking very eye candy, easy and intuitive to work with.

https://www.perforce.com/products/helix-core-apps/merge-diff-tool-p4merge


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf