Comparing source files - Page 1
Forum section: electronics / general

author: AussieBruce
author_rank: Regular Contributor
timestamp: March 07, 2024, 09:33:50 am
content: 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.

author: Psi
author_rank: Super Contributor
timestamp: March 07, 2024, 09:36:21 am
content: I'm not entirely sure I understand your issue, but have you tried 'winmerge' for comparing text files

author: brucehoult
author_rank: Super Contributor
timestamp: March 07, 2024, 09:50:58 am
content: Quote from: Psi on March 07, 2024, 09:36:21 amThat 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)

author: RoGeorge
author_rank: Super Contributor
timestamp: March 07, 2024, 09:56:47 am
content: Search for 'diff tool'.I'm using Meld.

author: DiTBho
author_rank: Super Contributor
timestamp: March 07, 2024, 10:51:42 am
content: Beyond Compareit'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 ofBeyond Comparefor 50 euro. Still usable for personal stuff.

author: tszaboo
author_rank: Super Contributor
timestamp: March 07, 2024, 11:42:16 am
content: git will do that for you, just version control the file.

author: DiTBho
author_rank: Super Contributor
timestamp: March 07, 2024, 01:09:27 pm
content: 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

author: srb1954
author_rank: Super Contributor
timestamp: March 07, 2024, 01:19:26 pm
content: Quote from: DiTBho on March 07, 2024, 10:51:42 amAlso available for the Mac.Highly recommended. It is one of the best value commercial S/W packages that I have ever bought.

author: teb
author_rank: Newbie
timestamp: March 07, 2024, 02:00:16 pm
content: If you're using git, you can set your merge diff tool:https://git-scm.com/docs/merge-configOtherwise 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: March 07, 2024, 02:48:04 pm
content: Notepad++ (Win only) also has pretty decent compare plugins.

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 07, 2024, 04:35:18 pm
content: Note that standarddiffsupports 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-UN. 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=alwaysfile1file2| less -rproduces paged, colored output.It turns out that this June,diffwill be 50 years old!

author: eutectique
author_rank: Frequent Contributor
timestamp: March 07, 2024, 05:35:56 pm
content: +1 formeldTo have a scrollable file with colours, saydiff file1.c file2.c | gvim -R -

author: RAPo
author_rank: Super Contributor
timestamp: March 07, 2024, 05:57:30 pm
content: I use meld and ultra compare (https://www.ultraedit.com/products/ultracompare/8

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 07, 2024, 06:49:08 pm
content: Forgot to mention,diff -Nabur --color=alwaysdir1/dir2/ | less -rcompares 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 viadiff -Naburdir1/dir2/ >name.patchby running(cddir3/ && patch -p1 -i ../name.patch)and revert the same changes by running(cddir3/ && patch -p1 -R -i ../name.patch)

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 07, 2024, 08:38:16 pm
content: 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.

author: brucehoult
author_rank: Super Contributor
timestamp: March 07, 2024, 08:38:29 pm
content: Quote from: Nominal Animal on March 07, 2024, 04:35:18 pm"Any Colour You Like"(50 years old, this time last year. You missed the starting gun.)

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 07, 2024, 10:10:25 pm
content: Quote from: brucehoult on March 07, 2024, 08:38:29 pmDid I? I thought the initial release was in June 1974; not in 1973 like the Pink Floyd album.

author: brucehoult
author_rank: Super Contributor
timestamp: March 07, 2024, 10:29:28 pm
content: Quote from: Nominal Animal on March 07, 2024, 10:10:25 pmI 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)

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 08, 2024, 01:50:23 am
content: I'm a couple of years younger myself, born in '76.As to colors, unified formatdiffuses 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 notthatuseful, 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 thediff -Naburoutput togrep -epattern, to output only lines that contain or matchPOSIX basic regular expressionpattern.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 theUnix philosophyandminimalistmodularapproach.

author: AussieBruce
author_rank: Regular Contributor
timestamp: March 08, 2024, 07:29:21 am
content: Thanks, that solved my problem in one, the functionality of that program is way beyond my needs.

author: DiTBho
author_rank: Super Contributor
timestamp: March 08, 2024, 12:20:00 pm
content: Quote from: SiliconWizard on March 07, 2024, 08:38:16 pmthat's precisely +1 for Beyound Compare, as it does it, and it saves your time!

author: radiolistener
author_rank: Super Contributor
timestamp: March 28, 2024, 11:09:06 am
content: Quote from: AussieBruce on March 07, 2024, 09:33:50 amI'm usingMeld. It allows to compare/merge/synchronize files and folders and also allows to see github status for files.It very similar to oldAraxis Merge, but Araxis is paid and Meld is free.

author: DiTBho
author_rank: Super Contributor
timestamp: March 28, 2024, 01:05:33 pm
content: my-cnow 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

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 28, 2024, 10:39:15 pm
content: 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.

author: RoGeorge
author_rank: Super Contributor
timestamp: March 29, 2024, 07:36:53 am
content: 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, calledP4Merge. 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

author: DiTBho
author_rank: Super Contributor
timestamp: March 29, 2024, 12:58:20 pm
content: Quote from: SiliconWizard on March 28, 2024, 10:39:15 pmit only works with "my-c" sources, refuses "c" sources, and the first thing it chckes is the "level", so it can only comparesCode:{{ source0:level0, source1:level0 } <--------- this is 90% compatible with C/89, but not 100%{ source0:level1, source1:level1 }{ source0:level2, source1:level2 }{ source0:level3, source1:level3 }}Quote from: SiliconWizard on March 28, 2024, 10:39:15 pmYup, this is a defect. There must be Only one style.To reduce the things I have to develop, I forced only one style, which is imposed to the user, and whatever the user writes (spaces, tabs, personal indentation), the tool always re-formats the source with the imposed style.Comments are special-cases. A comment is recognized as "token_comment", managed as block, and its context is handled by the traditional diff algorithm, including CR.Tabs are not allowed, and automatically converted into 4 spaces, as well as CR LF are automatically converted intoCRLF (DOS --> Unix)So, comments keepCRsLFs, you can see differences between the same comment, e.g. { typos fixed, more details added &| removed, ... } in two different sources.edit:fixed

author: cfbsoftware
author_rank: Regular Contributor
timestamp: March 30, 2024, 07:43:30 pm
content: Quote from: DiTBho on March 29, 2024, 12:58:20 pmShouldn't that be:Quote

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 30, 2024, 09:03:40 pm
content: Yup. The four newline conventions, listed by the most well-known OSes that used them:LF ("\n"): Unix, BSDs, Linux, Android, OS X.CR ("\r"): Old Mac OS prior to X.CR LF ("\r\n"): DOS, Windows.LF CR ("\n\r"): An error, but invisible on most systems, behaving like a normal newline, and is thus occasionally encountered in the wild. Can also occur in Unix/BSD/Linux/etc. systems when using tty devices (terminals, serial ports) with termiosINLCRandICRNLset on input with the other side using CR LF newlines.My own conception for "universal newlines" is supporting any mix of the four, optionally with NUL ("\0") also considered a newline.

author: Picuino
author_rank: Super Contributor
timestamp: March 30, 2024, 09:24:58 pm
content: +1 for Beyond Compare.There are versions for Win, Mac and Linux, and with trial versions.https://www.scootersoftware.com/download

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 30, 2024, 09:53:58 pm
content: Quote from: Nominal Animal on March 30, 2024, 09:03:40 pmYes.

author: DiTBho
author_rank: Super Contributor
timestamp: March 31, 2024, 06:48:13 am
content: FixedQuote from: Nominal Animal on March 30, 2024, 09:03:40 pmumm, speaking of being invisible, I never noticed it on the DEC VT525(1994) hardware terminal with /usr/bin/screen on /dev/ttyS0it shouldn't have worked

author: Nominal Animal
author_rank: Super Contributor
timestamp: March 31, 2024, 08:48:57 pm
content: LFCR "\n\r" works on Unix, BSD/Linux/etc., and VT terminals because while LF "\n" is a full newline, CR "\r" only returns the cursor to the beginning of the current line. Thus, the effect of LF CR "\n\r" is exactly the same as LF "\n" only. Invisible, like I said!This is often exploited by POSIXy command-line programs that report progress using e.g.fprintf(stderr, "\r%.2f%% done", 100.0*i/n);fflush(stderr); // Optional; normally/by default, this is not neededfollowed byfprintf(stderr, "\r \r"); // or "\r\022[K"fflush(stderr); // Optional; normally/by default, this is not neededwhere"\033[K"(or"\e[K"where\erepresents ASCII/ISO Latin/UTF-8 code 27 = 0x1B = ESC) is the ANSI Control Sequence for clearing the current line from the current cursor position to the end of the line without moving the cursor, when the maximum number of characters needed to "erase" the progress output is unknown.If the progress is reported to standard output, or there is more information printed to standard error that may have been redirected to a file, often the followup is just a simplefprintf(stderr, "\n");orfprintf(stderr, "\rCompleted. \n");, as that way the progress information will look like a single line in the file, but won't otherwise mess up the output.

author: dobsonr741
author_rank: Frequent Contributor
timestamp: April 03, 2024, 04:24:05 am
content: Araxis Merge. They are in this business was for a long time, and for a reason.

author: bobcat2000
author_rank: Regular Contributor
timestamp: September 19, 2024, 07:22:56 am
content: I, too, use Notepad++ with the compare plugin if you just want to look for differences. It will do its best to line up the codes and color codes the differences. Open the 1st file, open the 2nd file, and then click the compare button. Nothing simpler than that.Unless you want to merge the 2 files. I am using DiffMerge for that.

author: westfw
author_rank: Super Contributor
timestamp: September 19, 2024, 09:47:04 pm
content: QuoteAlso most old DEC OSes, and "NVT ASCII", supposedly used by many internet protocols (telnet, specifically. But also SMTP, etc.)This means you also sometimes see CR NUL, which is supposedly a "bare CR" in NVT ASCII.