Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Diff
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Standard UNIX utility for file comparison}} {{About|the utility program|the general topic of file comparison|File comparison|diffs in Wikipedia|:Help:Diff|other uses|DIFF (disambiguation){{!}}DIFF}} {{Lowercase title|title=diff}} {{Infobox software | name = diff | title = diff | screenshot = <!-- Image name is enough --> | caption = | screenshot size = | screenshot alt = | collapsible = | author = [[Douglas McIlroy]]<br />([[AT&T Bell Laboratories]]) | developer = Various [[open-source software|open-source]] and [[commercial software|commercial]] developers | released = {{Start date and age|1974|6}} | latest release version = | latest release date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | programming language = [[C (programming language)|C]] | operating system = [[Unix]], [[Unix-like]], [[V (operating system)|V]], [[Plan 9 from Bell Labs|Plan 9]], [[Inferno (operating system)|Inferno]] | platform = [[Cross-platform]] | genre = [[Command (computing)|Command]] | license = Plan 9: [[MIT License]] | website = | standard = | AsOf = }} In [[computing]], the utility '''diff''' is a [[data comparison]] tool that computes and displays the differences between the contents of files. Unlike [[edit distance]] notions used for other purposes, diff is line-oriented rather than character-oriented, but it is like [[Levenshtein distance]] in that it tries to determine the smallest set of deletions and insertions to create one file from the other. The utility displays the changes in one of several standard formats, such that both humans or computers can parse the changes, and use them for [[patch (Unix)|patching]]. Typically, ''diff'' is used to show the changes between two versions of the same file. Modern implementations also support [[binary file]]s.<ref>MacKenzie ''et al.'' "Binary Files and Forcing Text Comparison" in ''Comparing and Merging Files with GNU Diff and Patch''. Downloaded 28 April 2007. [https://www.gnu.org/software/diffutils/manual/html_node/Binary.html] {{Webarchive|url=https://web.archive.org/web/20171219223414/http://www.gnu.org/software/diffutils/manual/html_node/Binary.html|date=2017-12-19}}</ref> The output is called a "diff", or a [[patch (computing)|patch]], since the output can be applied with the [[Unix]] program [[patch (Unix)|{{Mono|patch}}]]. The output of similar file comparison utilities is also called a "diff"; like the use of the word "[[grep]]" for describing the act of searching, the word ''diff'' became a generic term for calculating data difference and the results thereof.<ref>Eric S. Raymond (ed.), [http://catb.org/jargon/html/D/diff.html "diff"] {{Webarchive|url=https://web.archive.org/web/20140131071625/http://www.catb.org/jargon/html/D/diff.html |date=2014-01-31 }}, ''The Jargon File'', version 4.4.7</ref> The [[POSIX]] standard specifies the behavior of the "diff" and "patch" utilities and their file formats.<ref>{{cite book|author1 = IEEE Computer Society|author2-link = The Open Group|author2 = The Open Group|date=26 September 2008|title = Standard for Information Technology—Portable Operating System Interface (POSIX) Base Specifications, Issue 7|pages = 2599–2607|author1-link = IEEE Computer Society}} IEEE Std. 1003.1-2001 specifies traditional, "ed script", and context diff output formats; IEEE Std. 1003.1-2008 added the (by then more common) unified format.</ref> == History == diff was developed in the early 1970s on the Unix operating system, which was emerging from [[Bell Labs]] in Murray Hill, New Jersey. It was part of the 5th Edition of Unix released in 1974,<ref>https://minnie.tuhs.org/cgi-bin/utree.pl?file=V5/usr/source/s1/diff1.c</ref> and was written by [[Douglas McIlroy]], and [[James W. Hunt|James Hunt]]. This research was published in a 1976 paper co-written with James W. Hunt, who developed an initial prototype of {{Mono|diff}}.<ref name="diff paper">{{cite journal|author1=James W. Hunt|author2=M. Douglas McIlroy|title=An Algorithm for Differential File Comparison|volume=41|journal=Computing Science Technical Report, Bell Laboratories|date=June 1976|url=http://www.cs.dartmouth.edu/~doug/diff.pdf|access-date=2015-05-06|archive-date=2014-12-26|archive-url=https://web.archive.org/web/20141226005228/http://www.cs.dartmouth.edu/~doug/diff.pdf|url-status=live}}</ref> The algorithm this paper described became known as the [[Hunt–Szymanski algorithm]]. McIlroy's work was preceded and influenced by [[Stephen C. Johnson|Steve Johnson]]'s comparison program on [[GECOS]] and [[Mike Lesk]]'s {{Mono|proof}} program. {{Mono|Proof}} also originated on Unix and, like {{Mono|diff}}, produced line-by-line changes and even used angle-brackets (">" and "<") for presenting line insertions and deletions in the program's output. The [[heuristic]]s used in these early applications were, however, deemed unreliable. The potential usefulness of a diff tool provoked McIlroy into researching and designing a more robust tool that could be used in a variety of tasks, but perform well in the processing and size limitations of the [[PDP-11]]'s hardware. His approach to the problem resulted from collaboration with individuals at Bell Labs including [[Alfred Aho]], Elliot Pinson, [[Jeffrey Ullman]], and Harold S. Stone. In the context of Unix, the use of the [[ed (Unix)|{{Mono|ed}}]] line editor provided {{Mono|diff}} with the natural ability to create machine-usable "edit scripts". These edit scripts, when saved to a file, can, along with the original file, be reconstituted by {{Mono|ed}} into the modified file in its entirety. This greatly reduced the [[secondary storage]] necessary to maintain multiple versions of a file. McIlroy considered writing a post-processor for {{Mono|diff}} where a variety of output formats could be designed and implemented, but he found it more frugal and simpler to have {{Mono|diff}} be responsible for generating the syntax and reverse-order input accepted by the {{Mono|ed}} command. In 1984, [[Larry Wall]] created a separate utility, [[patch (Unix)|patch]], releasing its source code on the ''mod.sources'' and ''net.sources'' newsgroups.<ref>{{cite newsgroup | title = A patch applier--YOU WANT THIS!!! | author = Larry Wall | date = November 9, 1984 | newsgroup = net.sources | message-id = 1457@sdcrdcf.UUCP | url = https://groups.google.com/d/msg/net.sources/qtfVio1sSHs/G0cPT5HFDFcJ | access-date = May 11, 2015 | archive-date = February 19, 2022 | archive-url = https://web.archive.org/web/20220219233604/https://groups.google.com/g/net.sources/c/qtfVio1sSHs/m/G0cPT5HFDFcJ | url-status = live }}</ref><ref>{{cite newsgroup | title = patch version 1.2--YOU WANT THIS | author = Larry Wall | date = November 29, 1984 | newsgroup = net.sources | message-id = 1508@sdcrdcf.UUCP | url = https://groups.google.com/d/msg/net.sources/uWFr9NOp_fw/SRS_P2vSgFgJ | access-date = May 11, 2015 | archive-date = March 21, 2020 | archive-url = https://web.archive.org/web/20200321140052/https://groups.google.com/forum/ | url-status = live }}</ref><ref>{{cite newsgroup | title = patch version 1.3 | author = Larry Wall | date = May 8, 1985 | newsgroup = net.sources | message-id = 813@genrad.UUCP | url = https://groups.google.com/d/msg/mod.sources/xSQM63e39YY/apNNJSkJi0gJ | access-date = May 11, 2015 | archive-date = February 19, 2022 | archive-url = https://web.archive.org/web/20220219233604/https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ | url-status = live }}</ref> This program modifies files using output from {{Mono|diff}} and has the ability to match context. [[X/Open]] Portability Guide issue 2 of 1987 includes diff. Context mode was added in POSIX.1-2001 (issue 6). Unified mode was added in POSIX.1-2008 (issue 7).<ref>{{man|cu|diff|SUS}}</ref> In {{Mono|diff}}'s early years, common uses included comparing changes in the source of software code and markup for technical documents, verifying program debugging output, comparing filesystem listings and analyzing computer assembly code. The output targeted for {{Mono|ed}} was motivated to provide compression for a sequence of modifications made to a file.{{citation needed|date=May 2023}} The [[Source Code Control System]] (SCCS) and its ability to archive revisions emerged in the late 1970s as a consequence of storing edit scripts from {{Mono|diff}}. == Algorithm == The operation of {{Mono|diff}} is based on solving the [[longest common subsequence problem]].<ref name="diff paper" /> In this problem, given two sequences of items: {{underline|a}} {{underline|b}} {{underline|c}} {{underline|d}} {{underline|f}} {{underline|g}} h {{underline|j}} q {{underline|z}} {{underline|a}} {{underline|b}} {{underline|c}} {{underline|d}} e {{underline|f}} {{underline|g}} i {{underline|j}} k r x y {{underline|z}} and we want to find a longest sequence of items that is present in both original sequences in the same order. That is, we want to find a new sequence which can be obtained from the first original sequence by deleting some items, and from the second original sequence by deleting other items. We also want this sequence to be as long as possible. In this case it is a b c d f g j z From a longest common subsequence it is only a small step to get {{Mono|diff}}-like output: if an item is absent in the subsequence but present in the first original sequence, it must have been deleted (as indicated by the '-' marks, below). If it is absent in the subsequence but present in the second original sequence, it must have been inserted (as indicated by the '+' marks). e h i q k r x y + - + - + + + + == Usage == The <code>diff</code> command is invoked from the command line, passing it the names of two files: <code>diff ''original'' ''new''</code>. The output of the command represents the changes required to transform the ''original'' file into the ''new'' file. If ''original'' and ''new'' are directories, then {{Mono|diff}} will be run on each file that exists in both directories. An option, <code>-r</code>, will recursively descend any matching subdirectories to compare files between directories. Any of the examples in the article use the following two files, ''original'' and ''new'': {{Col-begin}} {{Col-break|width=33%}} ''original'': <syntaxhighlight lang="text" line> This part of the document has stayed the same from version to version. It shouldn't be shown if it doesn't change. Otherwise, that would not be helping to compress the size of the changes. This paragraph contains text that is outdated. It will be deleted in the near future. It is important to spell check this dokument. On the other hand, a misspelled word isn't the end of the world. Nothing in the rest of this paragraph needs to be changed. Things can be added after it. </syntaxhighlight> {{col-break}} ''new'': <syntaxhighlight lang="text" line> This is an important notice! It should therefore be located at the beginning of this document! This part of the document has stayed the same from version to version. It shouldn't be shown if it doesn't change. Otherwise, that would not be helping to compress the size of the changes. It is important to spell check this document. On the other hand, a misspelled word isn't the end of the world. Nothing in the rest of this paragraph needs to be changed. Things can be added after it. This paragraph contains important new additions to this document. </syntaxhighlight> {{col-break|width=33%}} The command '''<code>diff original new</code>''' produces the following ''normal diff output'': {{pre| 0a1,6 {{font color|darkgreen|> This is an important > notice! It should > therefore be located at > the beginning of this > document! >}} 11,15d16 {{font color|darkred|< This paragraph contains < text that is outdated. < It will be deleted in the < near future. <}} 17c18 {{font color|darkred|< check this dokument. On}} --- {{font color|darkgreen|> check this document. On}} 24a26,29 {{font color|darkgreen|> > This paragraph contains > important new additions > to this document.}}}} '''Note''': ''Here, the diff output is shown with colors to make it easier to read. The diff utility does not produce colored output; its output is [[plain text]]. However, many tools can show the output with colors by using [[syntax highlighting]].'' {{col-end}} In this traditional output format, '''<samp>a</samp>''' stands for ''added'', '''<samp>d</samp>''' for ''deleted'' and '''<samp>c</samp>''' for ''changed''. Line numbers of the original file appear before <samp>a</samp>/<samp>d</samp>/<samp>c</samp> and those of the new file appear after. The [[Less-than sign|less-than]] and [[Greater-than sign|greater-than]] signs (at the beginning of lines that are added, deleted or changed) indicate which file the lines appear in. Addition lines are added to the original file to appear in the new file. Deletion lines are deleted from the original file to be missing in the new file. By default, lines common to both files are not shown. Lines that have moved are shown as added at their new location and as deleted from their old location.<ref>{{cite book|title=Comparing and Merging Files with GNU Diff and Patch|url=https://www.gnu.org/software/diffutils/manual/|author1=David MacKenzie|author2=Paul Eggert|author3=Richard Stallman|isbn=978-0-9541617-5-0|publisher=Network Theory|year=1997|location=Bristol|access-date=2015-03-17|archive-date=2015-03-31|archive-url=https://web.archive.org/web/20150331031946/http://www.gnu.org/software/diffutils/manual/|url-status=live}}</ref> However, some diff tools highlight moved lines. == Output variations{{anchor|variations}} == === Edit script === An [[Ed (text editor)|ed script]] can still be generated by modern versions of diff with the <code>-e</code> option. The resulting edit script for this example is as follows: 24'''a''' ''This paragraph contains'' ''important new additions'' ''to this document.'' . 17'''c''' ''check this document. On'' . 11,15'''d''' 0'''a''' ''This is an important'' ''notice! It should'' ''therefore be located at'' ''the beginning of this'' ''document!'' . In order to transform the content of file ''original'' into the content of file ''new'' using {{Mono|ed}}, we should append two lines to this diff file, one line containing a <code>w</code> (write) command, and one containing a <code>q</code> (quit) command (e.g. by {{code|lang=bash|printf "w\nq\n" >> mydiff}}). Here we gave the diff file the name ''mydiff'' and the transformation will then happen when we run {{code|lang=bash|ed -s original < mydiff}}. === Context format === The [[BSD|Berkeley distribution of Unix]] made a point of adding the ''context format'' ({{code|-c}}) and the ability to recurse on filesystem directory structures ({{code|-r}}), adding those features in 2.8 BSD, released in July 1981. The context format of diff introduced at Berkeley helped with distributing patches for source code that may have been changed minimally. In the context format, any changed lines are shown alongside unchanged lines before and after. The inclusion of any number of unchanged lines provides a ''context'' to the patch. The ''context'' consists of lines that have not changed between the two files and serve as a reference to locate the lines' place in a modified file and find the intended location for a change to be applied regardless of whether the line numbers still correspond. The context format introduces greater readability for humans and reliability when applying the patch, and an output which is accepted as input to the [[patch (Unix)|patch]] program. This intelligent behavior is not possible with the traditional diff output. The number of unchanged lines shown above and below a change ''hunk'' can be defined by the user, even zero, but three lines is typically the default. If the context of unchanged lines in a hunk overlap with an adjacent hunk, then diff will avoid duplicating the unchanged lines and merge the hunks into a single hunk. A "{{code|!}}" represents a change between lines that correspond in the two files, whereas a "{{code|+}}" represents the addition of a line, and a "{{code|-}}" the removal of a line. A blank [[space (punctuation)|space]] represents an unchanged line. At the beginning of the patch is the file information, including the full path and a [[time stamp]] delimited by a tab character. At the beginning of each hunk are the line numbers that apply for the corresponding change in the files. A number range appearing between sets of three asterisks applies to the original file, while sets of three dashes apply to the new file. The hunk ranges specify the starting and ending line numbers in the respective file. The command {{code|diff -c original new}} produces the following output: <syntaxhighlight lang="diff"> *** /path/to/original timestamp --- /path/to/new timestamp *************** *** 1,3 **** --- 1,9 ---- + This is an important + notice! It should + therefore be located at + the beginning of this + document! + This part of the document has stayed the same from version to *************** *** 8,20 **** compress the size of the changes. - This paragraph contains - text that is outdated. - It will be deleted in the - near future. It is important to spell ! check this dokument. On the other hand, a misspelled word isn't the end of the world. --- 14,21 ---- compress the size of the changes. It is important to spell ! check this document. On the other hand, a misspelled word isn't the end of the world. *************** *** 22,24 **** --- 23,29 ---- this paragraph needs to be changed. Things can be added after it. + + This paragraph contains + important new additions + to this document. </syntaxhighlight> '''Note''': ''Here, the diff output is shown with colors to make it easier to read. The diff utility does not produce colored output; its output is [[plain text]]. However, many tools can show the output with colors by using [[syntax highlighting]].'' === Unified format === The ''unified format'' (or ''unidiff'')<ref>{{Cite web|url=https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html|title=Detailed Description of Unified Format|website=GNU Diffutils (version 3.7, 7 January 2018)|access-date=29 January 2020|archive-date=18 January 2020|archive-url=https://web.archive.org/web/20200118142136/http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html|url-status=live}}</ref><ref>{{Cite web|url=https://www.artima.com/weblogs/viewpost.jsp?thread=164293|title=Unified Diff Format|last=van Rossum|first=Guido|website=All Things Pythonic|access-date=2020-01-29|archive-date=2019-12-25|archive-url=https://web.archive.org/web/20191225234517/https://www.artima.com/weblogs/viewpost.jsp?thread=164293|url-status=live}}</ref> inherits the technical improvements made by the context format, but produces a smaller diff with old and new text presented immediately adjacent. Unified format is usually invoked using the "<code>-u</code>" [[command-line option]]. This output is often used as input to the [[patch (Unix)|patch]] program. Many projects specifically request that "diffs" be submitted in the unified format, making unified diff format the most common format for exchange between software developers. Unified context diffs were originally developed by Wayne Davison in August 1990 (in '''unidiff''' which appeared in Volume 14 of comp.sources.misc). [[Richard Stallman]] added unified diff support to the [[GNU|GNU Project]]'s diff utility one month later, and the feature debuted in '''GNU diff''' 1.15, released in January 1991. GNU diff has since generalized the context format to allow arbitrary formatting of diffs. The format starts with the same two-line [[Header (computing)|header]] as the context format, except that the original file is preceded by "<samp>---</samp>" and the new file is preceded by "<samp>+++</samp>". Following this are one or more '''change hunks''' that contain the line differences in the file. The unchanged, contextual lines are preceded by a space character, addition lines are preceded by a [[plus sign]], and deletion lines are preceded by a [[minus sign]]. A hunk begins with '''range information''' and is immediately followed with the line additions, line deletions, and any number of the contextual lines. The range information is surrounded by double [[at sign]]s, and combines onto a single line what appears on two lines in the context format ([[#Context format|above]]). The format of the range information line is as follows: @@ -l,s +l,s @@ ''optional section heading''<!-- <syntaxhighlight lang="text"> ruins the italics --> The hunk range information contains two hunk ranges. The range for the hunk of the original file is preceded by a minus symbol, and the range for the new file is preceded by a plus symbol. Each hunk range is of the format ''l,s'' where ''l'' is the starting line number and ''s'' is the number of lines the change hunk applies to for each respective file. In many versions of GNU diff, each range can omit the comma and trailing value ''s'', in which case ''s'' defaults to 1. Note that the only really interesting value is the ''l'' line number of the first range; all the other values can be computed from the diff. The hunk range for the original should be the sum of all contextual and deletion (including changed) hunk lines. The hunk range for the new file should be a sum of all contextual and addition (including changed) hunk lines. If hunk size information does not correspond with the number of lines in the hunk, then the diff could be considered invalid and be rejected. Optionally, the hunk range can be followed by the heading of the section or function that the hunk is part of. This is mainly useful to make the diff easier to read. When creating a diff with GNU diff, the heading is identified by [[regular expression]] matching.<ref>[https://www.gnu.org/software/diffutils/manual/html_node/Sections.html 2.2.3 Showing Which Sections Differences Are in], GNU diffutils manual</ref> If a line is modified, it is represented as a deletion and addition. Since the hunks of the original and new file appear in the same hunk, such changes would appear adjacent to one another.<ref>[http://www.artima.com/weblogs/viewpost.jsp?thread=164293 Unified Diff Format] by [[Guido van Rossum]], June 14, 2006</ref> An occurrence of this in the example below is: <pre> -check this dokument. On +check this document. On </pre> The command <code>diff -u original new</code> produces the following output: <syntaxhighlight lang="diff"> --- /path/to/original timestamp +++ /path/to/new timestamp @@ -1,3 +1,9 @@ +This is an important +notice! It should +therefore be located at +the beginning of this +document! + This part of the document has stayed the same from version to @@ -8,13 +14,8 @@ compress the size of the changes. -This paragraph contains -text that is outdated. -It will be deleted in the -near future. - It is important to spell -check this dokument. On +check this document. On the other hand, a misspelled word isn't the end of the world. @@ -22,3 +23,7 @@ this paragraph needs to be changed. Things can be added after it. + +This paragraph contains +important new additions +to this document. </syntaxhighlight> '''Note''': ''Here, the diff output is shown with colors to make it easier to read. The diff utility does not produce colored output; its output is [[plain text]]. However, many tools can show the output with colors by using [[syntax highlighting]].'' Note that to successfully separate the file names from the timestamps, the delimiter between them is a tab character. This is invisible on screen and can be lost when diffs are copy/pasted from console/terminal screens. === Extensions === There are some modifications and extensions to the diff formats that are used and understood by certain programs and in certain contexts. For example, some [[revision control]] systems—such as [[Subversion (software)|Subversion]]—specify a version number, "working copy", or any other comment instead of or in addition to a timestamp in the diff's header section. Some tools allow diffs for several different files to be merged into one, using a header for each modified file that may look something like this: Index: path/to/file.cpp The special case of files that do not end in a newline is not handled. Neither the unidiff utility nor the POSIX diff standard define a way to handle this type of files. (Indeed, such files are not "text" files by strict POSIX definitions.<ref>http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 {{Webarchive|url=https://web.archive.org/web/20130429195728/http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 |date=2013-04-29 }} Section 3.206</ref>) GNU diff and git produce "\ No newline at end of file" (or a translated version) as a diagnostic, but this behavior is not portable.<ref>{{cite web |title=Incomplete Lines (Comparing and Merging Files) |url=https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html |website=www.gnu.org}}</ref> GNU patch does not seem to handle this case, while git-apply does.<ref>{{cite web |title=git: apply.c |url=https://github.com/git/git/blob/69c786637d7a7fe3b2b8f7d989af095f5f49c3a8/apply.c#LL2901C35-L2901C39 |publisher=Git |date=8 May 2023}}</ref> The [[Patch (Unix)|patch]] program does not necessarily recognize implementation-specific diff output. GNU patch is, however, known to recognize git patches and act a little differently.<ref>{{cite web |title=patch.c\src - patch.git - GNU patch |url=https://git.savannah.gnu.org/cgit/patch.git/tree/src/patch.c?id=c835ecc67b7e37c0d0b7dd7e032209fdaa285808#n1919 |website=git.savannah.gnu.org |quote=In git-style diffs, the "before" state of each patch refers to the initial state before modifying any files,..}}</ref> == Implementations and related programs{{anchor|Others}} == Changes since 1975 include improvements to the core algorithm, the addition of useful features to the command, and the design of new output formats. The basic algorithm is described in the papers ''An O(ND) Difference Algorithm and its Variations'' by [[Eugene Myers|Eugene W. Myers]]<ref>{{cite journal|author=E. Myers|title=An O(ND) Difference Algorithm and Its Variations|journal=Algorithmica|volume=1|issue=2|year=1986|pages=251–266|doi=10.1007/BF01840446|citeseerx=10.1.1.4.6927|s2cid=6996809}}</ref> and in ''A File Comparison Program'' by Webb Miller and Myers.<ref>{{cite journal|author1=Webb Miller |author2=Eugene W. Myers |title=A File Comparison Program|journal=Software: Practice and Experience|volume=15|issue=11|year=1985|pages=1025–1040|doi=10.1002/spe.4380151102|citeseerx=10.1.1.189.70 |s2cid=15489614 }}</ref> The algorithm was independently discovered and described in ''Algorithms for Approximate String Matching'', by [[Esko Ukkonen]].<ref>{{cite journal|author=Esko Ukkonen|title=Algorithms for Approximate String Matching|volume=64|journal=Information and Control|issue=1–3|year=1985|pages=100–118 | doi = 10.1016/S0019-9958(85)80046-2|doi-access=free}}</ref> The first editions of the diff program were designed for line comparisons of text files expecting the [[newline]] character to delimit lines. By the 1980s, support for binary files resulted in a shift in the application's design and implementation. GNU diff and diff3 are included in the '''diffutils''' package with other diff and [[Patch (Unix)|patch]] related utilities.<ref>[https://www.gnu.org/software/diffutils/ GNU Diff utilities] {{Webarchive|url=https://web.archive.org/web/20150316032418/http://www.gnu.org/software/diffutils/ |date=2015-03-16 }}. Made available by the [[Free Software Foundation]]. Free Documentation. Free source code.</ref> === Formatters and front-ends === Postprocessors '''sdiff''' and '''diffmk''' render side-by-side diff listings and applied change marks to printed documents, respectively. Both were developed elsewhere in Bell Labs in or before 1981.{{citation needed|date=February 2015}}{{discuss|topicality of front-ends}} [[Diff3]] compares one file against two other files by reconciling two diffs. It was originally conceived by Paul Jensen to reconcile changes made by two people editing a common source. It is also used by revision control systems, e.g. [[Revision Control System|RCS]], for [[merge (revision control)|merging]].<ref>{{cite web|url=https://www.gnu.org/software/rcs/manual/html_node/merge.html|title=merge (GNU RCS 5.10.0)|website=gnu.org|access-date=22 January 2021|archive-date=18 September 2019|archive-url=https://web.archive.org/web/20190918125803/http://www.gnu.org/software/rcs/manual/html_node/merge.html|url-status=live}}</ref> [[Emacs]] has [[Ediff]] for showing the changes a patch would provide in a user interface that combines interactive editing and merging capabilities for patch files. [[Vim (text editor)|Vim]] provides '''vimdiff''' to compare from two to eight files, with differences highlighted in color.<ref>{{Cite web|author-link=Bram Moolenaar|first=Bram|last=Moolenaar|url=https://vimhelp.org/diff.txt.html#vimdiff|title=Vim documentation: diff|website=vimdoc.sourceforge.net|access-date=1 May 2020|quote=The easiest way to start editing in diff mode is with the "vimdiff" command. This starts Vim as usual, and additionally sets up for viewing the differences between the arguments. <code>vimdiff file1 file2 [file3] [file4] [...file8]</code> This is equivalent to: <code>vim -d file1 file2 [file3] [file4] [...file8]</code>|archive-date=16 February 2020|archive-url=https://web.archive.org/web/20200216203940/https://vimhelp.org/diff.txt.html#vimdiff|url-status=live}}</ref> While historically invoking the diff program, modern vim uses [[git]]'s fork of xdiff library (LibXDiff) code, providing improved speed and functionality.<ref name="brabandt2018">{{Cite web|title=The power of diff|url=https://vimways.org/2018/the-power-of-diff/|date=1 December 2018|website=Vimways|access-date=1 May 2020|first=Christian |last=Brabandt|url-status=live|archive-url=https://web.archive.org/web/20181202020105/https://vimways.org/2018/the-power-of-diff/|archive-date=2 December 2018}}</ref> GNU [[Wdiff]]<ref>{{Cite web|url=https://www.gnu.org/software/wdiff/wdiff.html|title=gnu.org|website=www.gnu.org|access-date=2020-09-12|archive-date=2020-08-11|archive-url=https://web.archive.org/web/20200811124535/https://www.gnu.org/software/wdiff/wdiff.html|url-status=live}}</ref> is a front end to diff that shows the words or phrases that changed in a text document of written language even in the presence of word-wrapping or different column widths. colordiff is a Perl wrapper for 'diff' and produces the same output but with colorization for added and deleted bits.<ref>{{Cite web|url=https://www.colordiff.org/|title=colordiff|website=www.colordiff.org|access-date=2018-06-14|archive-date=2018-06-14|archive-url=https://web.archive.org/web/20180614121429/https://www.colordiff.org/|url-status=live}}</ref> diff-so-fancy and diff-highlight are newer analogues.<ref>{{cite web |title=diff-so-fancy |url=https://github.com/so-fancy/diff-so-fancy |publisher=So Fancy |date=6 May 2023}}</ref> "delta" is a Rust rewrite that highlights changes and the underlying code at the same time.<ref>{{cite web |last1=Davison |first1=Dan |title=dandavison/delta |website=[[GitHub]] |url=https://github.com/dandavison/delta |date=8 May 2023}}</ref> '''Patchutils''' contains tools that combine, rearrange, compare and fix context diffs and unified diffs.<ref>{{cite web |last1=Waugh |first1=Tim |title=twaugh/patchutils |website=[[GitHub]] |url=https://github.com/twaugh/patchutils |date=12 June 2020 |access-date=28 June 2020 |archive-date=1 October 2020 |archive-url=https://web.archive.org/web/20201001234609/https://github.com/twaugh/patchutils |url-status=live }}</ref> === Algorithmic derivatives === Utilities that compare source files by their syntactic structure have been built mostly as research tools for some programming languages;<ref>{{cite journal|last1=Horwitz|first1=Susan|title=Identifying the semantic and textual differences between two versions of a program|journal=ACM SIGPLAN Notices|date=June 1990|volume=25|issue=6|pages=234–245|doi=10.1145/93548.93574|url=http://www.cs.wisc.edu/wpis/papers/sigplan90.ps|citeseerx=10.1.1.49.3377|access-date=2017-11-01|archive-date=2010-06-12|archive-url=https://web.archive.org/web/20100612103752/http://www.cs.wisc.edu/wpis/papers/sigplan90.ps|url-status=live}}</ref><ref>{{cite journal|last1=Yang|first1=Wuu|title=Identifying syntactic differences between two programs|journal=Software: Practice and Experience|date=July 1991|volume=21|issue=7|pages=739–755|doi=10.1002/spe.4380210706|citeseerx=10.1.1.13.9377|s2cid=10853673}}</ref><ref>Grass. Cdiff: A syntax directed Diff for C++ programs. Proceedings USENIX C++ Conf., pp. 181-193, 1992</ref> some are available as commercial tools.<ref>Compare++, http://www.coodesoft.com/ {{Webarchive|url=https://web.archive.org/web/20111129033738/http://www.coodesoft.com/ |date=2011-11-29 }}</ref><ref>SmartDifferencer, http://www.semanticdesigns.com/Products/SmartDifferencer {{Webarchive|url=https://web.archive.org/web/20091014160445/http://www.semanticdesigns.com/Products/SmartDifferencer/ |date=2009-10-14 }}</ref> In addition, free tools that perform syntax-aware diff include: * C++: zograscope, AST-based.<ref>{{cite web |title=xaizek/zograscope |url=https://github.com/xaizek/zograscope |website=GitHub |date=26 May 2020 |access-date=27 June 2020 |archive-date=21 December 2020 |archive-url=https://web.archive.org/web/20201221105419/https://github.com/xaizek/zograscope |url-status=live }}</ref> * HTML: Daisydiff,<ref>''DaisyDiff'', https://code.google.com/p/daisydiff/ {{Webarchive|url=https://web.archive.org/web/20150319014218/http://code.google.com/p/daisydiff/ |date=2015-03-19 }}</ref> html-differ. * XML: ''xmldiffpatch'' by Microsoft and ''xmldiffmerge'' for IBM.<ref>''xmldiffpatch'', http://msdn.microsoft.com/en-us/library/aa302294.aspx {{Webarchive|url=https://web.archive.org/web/20091027123710/http://msdn.microsoft.com/en-us/library/aa302294.aspx |date=2009-10-27 }}</ref><ref>''xmldiffmerge'', http://www.alphaworks.ibm.com/tech/xmldiffmerge {{Webarchive|url=https://web.archive.org/web/20090924034538/http://www.alphaworks.ibm.com/tech/xmldiffmerge |date=2009-09-24 }}</ref> * [[JavaScript]]: astii (AST-based). * Multi-language: [[Pretty Diff]] (format code and then diff)<ref>Cheney, Austin. ''Pretty Diff - Documentation''. http://prettydiff.com/documentation.php {{Webarchive|url=https://web.archive.org/web/20120731233435/http://prettydiff.com/documentation.php |date=2012-07-31 }}</ref> '''spiff''' is a variant of ''diff'' that ignores differences in floating point calculations with roundoff errors and [[Space (punctuation)|whitespace]], both of which are generally irrelevant to source code comparison. [[Telcordia Technologies|Bellcore]] wrote the original version.<ref name="dontcallme"/><ref name="hpux"/> An [[HPUX]] port is the most current public release. spiff does not support binary files. spiff outputs to the [[standard output]] in standard diff format and accepts inputs in the [[C (programming language)|C]], [[Bourne shell]], [[Fortran]], [[Modula-2]] and [[Lisp (programming language)|Lisp]] [[programming language]]s.<ref>{{cite web|url=http://www.math.utah.edu/cgi-bin/man2html.cgi?/usr/local/man/man1/spiff.1|title=SPIFF 1|date=1988-02-02|access-date=2013-06-16|archive-date=2016-10-02|archive-url=https://web.archive.org/web/20161002135508/http://www.math.utah.edu/cgi-bin/man2html.cgi?%2Fusr%2Flocal%2Fman%2Fman1%2Fspiff.1|url-status=live}}</ref><ref>{{cite web|url=http://hpux.connect.org.uk/hppd/hpux/Text/spiff-1.0/man.html|title=Man page|location=UK|first=Daniel W|last=Nachbar|date=1988-02-02|access-date=2013-06-16|archive-date=2012-09-10|archive-url=https://web.archive.org/web/20120910004102/http://hpux.connect.org.uk/hppd/hpux/Text/spiff-1.0/man.html|url-status=live}}</ref><ref name="dontcallme">{{cite web|url=https://github.com/dontcallmedom/spiff|title=spiff|author=dontcallmedotcom|website=[[GitHub]]|access-date=2013-06-16|archive-date=2015-03-26|archive-url=https://web.archive.org/web/20150326181245/https://github.com/dontcallmedom/spiff|url-status=live}}</ref><ref>{{cite web|url=https://stackoverflow.com/a/1489107/2291035|date=2009-09-28|author=Davide|title=stackoverflow|access-date=2013-06-16|archive-date=2022-02-19|archive-url=https://web.archive.org/web/20220219233604/https://stackoverflow.com/questions/1428177/diff-tool-that-ignores-floating-point-formats-but-not-values-in-text/1489107|url-status=live}}</ref><ref name="hpux">{{cite web|url=http://hpux.connect.org.uk/hppd/hpux/Text/spiff-1.0/|title=HP-UX Porting and Archiving|location=UK|first=Daniel W|last=Nachbar|date=1999-12-01|access-date=2013-06-13|archive-date=2012-09-05|archive-url=https://web.archive.org/web/20120905154135/http://hpux.connect.org.uk/hppd/hpux/Text/spiff-1.0/|url-status=live}}</ref> LibXDiff is an LGPL [[Library (computing)|library]] that provides an interface to many algorithms from 1998. An improved Myers algorithm with [[Rabin fingerprint]] was originally implemented (as of the final release of 2008),<ref>{{cite web |last1=Libenzi |first1=Davide |title=LibXDiff |url=http://freshmeat.sourceforge.net/projects/xdiff-lib |website=SourceForge FreshMeat |language=en |access-date=2020-06-28 |archive-date=2020-07-01 |archive-url=https://web.archive.org/web/20200701070301/http://freshmeat.sourceforge.net/projects/xdiff-lib |url-status=live }}</ref> but [[git]] and [[libgit2]]'s fork has since expanded the repository with many of its own. One algorithm called "histogram" is generally regarded as much better than the original Myers algorithm, both in speed and quality.<ref>{{cite journal |last1=Nugroho |first1=Yusuf Sulistyo |last2=Hata |first2=Hideaki |last3=Matsumoto |first3=Kenichi |title=How different are different diff algorithms in Git?: Use --histogram for code changes |website=Empirical Software Engineering |pages=790–823 |language=en |doi=10.1007/s10664-019-09772-z |date=January 2020|s2cid=59608676 |doi-access=free |arxiv=1902.02467 }}</ref><ref>{{cite web |title=algorithm - What's the difference between 'git diff --patience' and 'git diff --histogram'? |url=https://stackoverflow.com/a/32367597/ |website=Stack Overflow |quote=This does indeed show that histogram diff slightly beats Myers, while patience is much slower than the others. |access-date=2020-06-28 |archive-date=2022-02-19 |archive-url=https://web.archive.org/web/20220219233605/https://stackoverflow.com/questions/32365271/whats-the-difference-between-git-diff-patience-and-git-diff-histogram/32367597 |url-status=live }}</ref> This is the modern version of ''LibXDiff'' used by Vim.<ref name="brabandt2018"/> ==See also== {{colbegin}} *[[Comparison of file comparison tools]] *[[Delta encoding]] *[[Difference operator]] *[[Edit distance]] **[[Levenshtein distance]] *[[History of software configuration management]] *[[Longest common subsequence problem]] *[[Microsoft File Compare]] *[[WinDiff|Microsoft WinDiff]] *[[Revision control]] *[[Software configuration management]] {{colend}} ===Other free file comparison tools=== {{colbegin}} *[[cmp (Unix)|cmp]] *[[comm]] *[[tkdiff]] *[[WinMerge]] (Microsoft Windows) *[[meld (software)|meld]] *[[Pretty Diff]] {{colend}} ==References== {{Reflist}} ==Further reading== *{{cite journal|author=Paul Heckel|title=A technique for isolating differences between files|journal=Communications of the ACM|volume=21|issue=4|date=April 1978|pages=264–268|doi=10.1145/359460.359467|s2cid=207683976|doi-access=free}}[http://doi.acm.org/10.1145/359460.359467 A technique for isolating differences between files] *A generic implementation of the Myers SES/LCS algorithm with the Hirschberg linear space refinement [http://www.ioplex.com/~miallen/libmba/dl/src/diff.c (C source code)] ==External links== {{Wikibooks|Guide to Unix|Commands}} {{Prone to spam|date=May 2012}} <!-- {{No more links}} Please be cautious adding more external links. Wikipedia is not a collection of links and should not be used for advertising. Excessive or inappropriate links will be removed. See [[Wikipedia:External links]] & [[Wikipedia:Spam]] for details. If there are already suitable links, propose additions or replacements on the article's talk page, or submit your link to the relevant category at the Open Directory Project (dmoz.org) and link there using {{Dmoz}}. --> *{{man|cu|diff|SUS|compare two files}} * {{man|1|diff|Plan 9}} * {{man|1|diff|Inferno}} *[http://www.bodurov.com/JavaScriptDiff/ JavaScript Implementation] {{Unix commands}} {{Plan 9 commands}} {{Version control software}} [[Category:1974 software]] [[Category:Free file comparison tools]] [[Category:Formal languages]] [[Category:Pattern matching]] [[Category:Data differencing]] [[Category:Standard Unix programs]] [[Category:Unix SUS2008 utilities]] [[Category:Plan 9 commands]] [[Category:Inferno (operating system) commands]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Anchor
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite newsgroup
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Col-begin
(
edit
)
Template:Col-break
(
edit
)
Template:Col-end
(
edit
)
Template:Colbegin
(
edit
)
Template:Colend
(
edit
)
Template:Discuss
(
edit
)
Template:Infobox
(
edit
)
Template:Infobox software
(
edit
)
Template:Lowercase title
(
edit
)
Template:Main other
(
edit
)
Template:Man
(
edit
)
Template:Mono
(
edit
)
Template:Plan 9 commands
(
edit
)
Template:Pre
(
edit
)
Template:Prone to spam
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Template other
(
edit
)
Template:Underline
(
edit
)
Template:Unix commands
(
edit
)
Template:Version control software
(
edit
)
Template:Webarchive
(
edit
)
Template:Wikibooks
(
edit
)