{"id":13483893,"url":"https://github.com/samg/diffy","last_synced_at":"2025-04-22T21:41:10.842Z","repository":{"id":959175,"uuid":"745996","full_name":"samg/diffy","owner":"samg","description":"Easy Diffing in Ruby","archived":false,"fork":false,"pushed_at":"2024-10-21T16:27:32.000Z","size":166,"stargazers_count":1277,"open_issues_count":41,"forks_count":105,"subscribers_count":24,"default_branch":"main","last_synced_at":"2025-04-20T02:37:07.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://rubygems.org/gems/diffy","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-06-29T02:49:04.000Z","updated_at":"2025-04-19T09:49:06.000Z","dependencies_parsed_at":"2024-01-03T01:18:52.362Z","dependency_job_id":"d5d5b2b2-acbf-4ac6-ab70-91f7450bc49d","html_url":"https://github.com/samg/diffy","commit_stats":{"total_commits":201,"total_committers":35,"mean_commits":5.742857142857143,"dds":0.6716417910447761,"last_synced_commit":"830689b3730fbc4ace8e9be9a8e73df0ff652e5d"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samg%2Fdiffy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samg%2Fdiffy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samg%2Fdiffy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samg%2Fdiffy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samg","download_url":"https://codeload.github.com/samg/diffy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249990383,"owners_count":21357065,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:16.580Z","updated_at":"2025-04-22T21:41:10.781Z","avatar_url":"https://github.com/samg.png","language":"Ruby","readme":"Diffy - Easy Diffing With Ruby [![Build Status](https://travis-ci.org/samg/diffy.svg?branch=master)](https://travis-ci.org/samg/diffy)\n============================\n\nNeed diffs in your ruby app?  Diffy has you covered.  It provides a convenient\nway to generate a diff from two strings or files.  Instead of reimplementing\nthe LCS diff algorithm Diffy uses battle tested Unix diff to generate diffs,\nand focuses on providing a convenient interface, and getting out of your way.\n\nSupported Formats\n-----------------\n\nIt provides several built in format options which can be passed to\n`Diffy::Diff#to_s`.\n\n* `:text`         - Plain text output\n* `:color`        - ANSI colorized text suitable for use in a terminal\n* `:html`         - HTML output.  Since version 2.0 this format does inline highlighting of the character changes between lines.\n* `:html_simple`  - HTML output without inline highlighting.  This may be useful in situations where high performance is required or simpler output is desired.\n\nA default format can be set like so:\n\n    Diffy::Diff.default_format = :html\n\nInstallation\n------------\n\n### on Unix\n\n    gem install diffy\n\n### on Windows:\n\n1.  Ensure that you have a working `diff` on your machine and in your search path.\n\n    There are several options:\n\n\t1.  Install [Diff::LCS](https://github.com/halostatue/diff-lcs), which includes `ldiff`. [RSpec](https://www.relishapp.com/rspec/docs/gettingstarted)\n\t\tdepends on Diff::LCS so you may already have it installed.\n\t\t\n\t1.  If you're using [RubyInstaller](http://rubyinstaller.org), install the [devkit](http://rubyinstaller.org/add-ons/devkit).\n\n\t1.  Install unxutils \u003chttp://sourceforge.net/projects/unxutils\u003e\n\n        note that these tools contain diff 2.7 which has a different handling\n        of whitespace in the diff results. This makes Diffy spec tests\n        yielding one fail on Windows.\n\n    1.  Install these two individually from the gnuwin32 project\n        \u003chttp://gnuwin32.sourceforge.net/\u003e\n\n        note that this delivers diff 2.8 which makes Diffy spec pass\n        even on Windows.\n\n\n2.   Install the gem by\n\n         gem install diffy\n\n\nGetting Started\n---------------\n\nHere's an example of using Diffy to diff two strings\n\n    $ irb\n    \u003e\u003e string1 = \u003c\u003c-TXT\n    \u003e\" Hello how are you\n    \u003e\" I'm fine\n    \u003e\" That's great\n    \u003e\" TXT\n    =\u003e \"Hello how are you\\nI'm fine\\nThat's great\\n\"\n    \u003e\u003e string2 = \u003c\u003c-TXT\n    \u003e\" Hello how are you?\n    \u003e\" I'm fine\n    \u003e\" That's swell\n    \u003e\" TXT\n    =\u003e \"Hello how are you?\\nI'm fine\\nThat's swell\\n\"\n    \u003e\u003e puts Diffy::Diff.new(string1, string2)\n    -Hello how are you\n    +Hello how are you?\n     I'm fine\n    -That's great\n    +That's swell\n\nHTML Output\n---------------\n\nOutputing the diff as html is easy too.  Here's an example using the\n`:html_simple` formatter.\n\n    \u003e\u003e puts Diffy::Diff.new(string1, string2).to_s(:html_simple)\n    \u003cdiv class=\"diff\"\u003e\n      \u003cul\u003e\n        \u003cli class=\"del\"\u003e\u003cdel\u003eHello how are you\u003c/del\u003e\u003c/li\u003e\n        \u003cli class=\"ins\"\u003e\u003cins\u003eHello how are you?\u003c/ins\u003e\u003c/li\u003e\n        \u003cli class=\"unchanged\"\u003e\u003cspan\u003eI'm fine\u003c/span\u003e\u003c/li\u003e\n        \u003cli class=\"del\"\u003e\u003cdel\u003eThat's great\u003c/del\u003e\u003c/li\u003e\n        \u003cli class=\"ins\"\u003e\u003cins\u003eThat's swell\u003c/ins\u003e\u003c/li\u003e\n      \u003c/ul\u003e\n    \u003c/div\u003e\n\nThe `:html` formatter will give you inline highlighting a la github.\n\n    \u003e\u003e puts Diffy::Diff.new(\"foo\\n\", \"Foo\\n\").to_s(:html)\n    \u003cdiv class=\"diff\"\u003e\n      \u003cul\u003e\n        \u003cli class=\"del\"\u003e\u003cdel\u003e\u003cstrong\u003ef\u003c/strong\u003eoo\u003c/del\u003e\u003c/li\u003e\n        \u003cli class=\"ins\"\u003e\u003cins\u003e\u003cstrong\u003eF\u003c/strong\u003eoo\u003c/ins\u003e\u003c/li\u003e\n      \u003c/ul\u003e\n    \u003c/div\u003e\n\nThere's some pretty nice css provided in `Diffy::CSS`.\n\n    \u003e\u003e puts Diffy::CSS\n    .diff{overflow:auto;}\n    .diff ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;}\n    .diff del, .diff ins{display:block;text-decoration:none;}\n    .diff li{padding:0; display:table-row;margin: 0;height:1em;}\n    .diff li.ins{background:#dfd; color:#080}\n    .diff li.del{background:#fee; color:#b00}\n    .diff li:hover{background:#ffc}\n    /* try 'whitespace:pre;' if you don't want lines to wrap */\n    .diff del, .diff ins, .diff span{white-space:pre-wrap;font-family:courier;}\n    .diff del strong{font-weight:normal;background:#fcc;}\n    .diff ins strong{font-weight:normal;background:#9f9;}\n    .diff li.diff-comment { display: none; }\n    .diff li.diff-block-info { background: none repeat scroll 0 0 gray; }\n\n\nThere's also a colorblind-safe version of the pallete provided in `Diffy::CSS_COLORBLIND_1`.\n\n\nSide-by-side comparisons\n------------------------\n\nSide-by-side comparisons, or split views as called by some, are supported by\nusing the `Diffy::SplitDiff` class.  This class takes a diff returned from\n`Diffy::Diff` and splits it in two parts (or two sides): left and right.  The\nleft side represents deletions while the right side represents insertions.\n\nThe class is used as follows:\n\n```\nDiffy::SplitDiff.new(string1, string2, options = {})\n```\n\nThe optional options hash is passed along to the main `Diff::Diff` class, so\nall default options such as full diff output are supported.  The output format\nmay be changed by passing the format with the options hash (see below), and all\ndefault formats are supported.\n\nUnlike `Diffy::Diff`, `Diffy::SplitDiff` does not use `#to_s` to output\nthe resulting diff.  Instead, two self-explanatory methods are used to output\nthe diff: `#left` and `#right`.  Using the earlier example, this is what they\nlook like in action:\n\n```\n\u003e\u003e puts Diffy::SplitDiff.new(string1, string2).left\n-Hello how are you\n I'm fine\n-That's great\n```\n\n```\n\u003e\u003e puts Diffy::SplitDiff.new(string1, string2).right\n+Hello how are you?\n I'm fine\n+That's swell\n```\n\n### Changing the split view output format\n\nThe output format may be changed by passing the format with the options hash:\n\n```\nDiffy::SplitDiff.new(string1, string2, :format =\u003e :html)\n```\n\nThis will result in the following:\n\n```\n\u003e\u003e puts Diffy::SplitDiff.new(string1, string2, :format =\u003e :html).left\n\u003cdiv class=\"diff\"\u003e\n  \u003cul\u003e\n    \u003cli class=\"del\"\u003e\u003cdel\u003eHello how are you\u003c/del\u003e\u003c/li\u003e\n    \u003cli class=\"unchanged\"\u003e\u003cspan\u003eI\u0026#39;m fine\u003c/span\u003e\u003c/li\u003e\n    \u003cli class=\"del\"\u003e\u003cdel\u003eThat\u0026#39;s \u003cstrong\u003egreat\u003c/strong\u003e\u003c/del\u003e\u003c/li\u003e\n  \u003c/ul\u003e\n\u003c/div\u003e\n```\n\n```\n\u003e\u003e puts Diffy::SplitDiff.new(string1, string2, :format =\u003e :html).right\n\u003cdiv class=\"diff\"\u003e\n  \u003cul\u003e\n    \u003cli class=\"ins\"\u003e\u003cins\u003eHello how are you\u003cstrong\u003e?\u003c/strong\u003e\u003c/ins\u003e\u003c/li\u003e\n    \u003cli class=\"unchanged\"\u003e\u003cspan\u003eI\u0026#39;m fine\u003c/span\u003e\u003c/li\u003e\n    \u003cli class=\"ins\"\u003e\u003cins\u003eThat\u0026#39;s \u003cstrong\u003eswell\u003c/strong\u003e\u003c/ins\u003e\u003c/li\u003e\n  \u003c/ul\u003e\n\u003c/div\u003e\n```\n\n\nOther Diff Options\n------------------\n\n### Diffing files instead of strings\n\nYou can diff files instead of strings by using the `:source` option.\n\n    \u003e\u003e puts Diffy::Diff.new('/tmp/foo', '/tmp/bar', :source =\u003e 'files')\n\n### Full Diff Output\n\nBy default Diffy removes the superfluous diff output.  This is because its\ndefault is to show the complete diff'ed file (`diff -U10000` is the default).\n\nDiffy does support full output, just use the `:include_diff_info =\u003e true`\noption when initializing:\n\n    \u003e\u003e Diffy::Diff.new(\"foo\\nbar\\n\", \"foo\\nbar\\nbaz\\n\", :include_diff_info =\u003e true).to_s(:text)\n    =\u003e--- /Users/chaffeqa/Projects/stiwiki/tmp/diffy20111116-82153-ie27ex\t2011-11-16 20:16:41.000000000 -0500\n    +++ /Users/chaffeqa/Projects/stiwiki/tmp/diffy20111116-82153-wzrhw5\t2011-11-16 20:16:41.000000000 -0500\n    @@ -1,2 +1,3 @@\n     foo\n     bar\n    +baz\n\nAnd even deals a bit with the formatting!\n\n### Empty Diff Behavior\n\nBy default Diffy will return empty string if there are no\ndifferences in inputs. In previous versions the full text of its first input\nwas returned in this case. To restore this behaviour simply use the\n`:allow_empty_diff =\u003e false` option when initializing.\n\n### Plus and Minus symbols in HTML output\n\nBy default Diffy doesn't include the `+`, `-`, and ` ` at the beginning of line for\nHTML output.\n\nYou can use the `:include_plus_and_minus_in_html` option to include those\nsymbols in the output.\n\n    \u003e\u003e puts Diffy::Diff.new(string1, string2, :include_plus_and_minus_in_html =\u003e true).to_s(:html_simple)\n    \u003cdiv class=\"diff\"\u003e\n      \u003cul\u003e\n        \u003cli class=\"del\"\u003e\u003cdel\u003e\u003cspan class=\"symbol\"\u003e-\u003c/span\u003eHello how are you\u003c/del\u003e\u003c/li\u003e\n        \u003cli class=\"ins\"\u003e\u003cins\u003e\u003cspan class=\"symbol\"\u003e+\u003c/span\u003eHello how are you?\u003c/ins\u003e\u003c/li\u003e\n        \u003cli class=\"unchanged\"\u003e\u003cspan class=\"symbol\"\u003e \u003c/span\u003e\u003cspan\u003eI'm fine\u003c/span\u003e\u003c/li\u003e\n        \u003cli class=\"del\"\u003e\u003cdel\u003e\u003cspan class=\"symbol\"\u003e-\u003c/span\u003eThat's great\u003c/del\u003e\u003c/li\u003e\n        \u003cli class=\"ins\"\u003e\u003cins\u003e\u003cspan class=\"symbol\"\u003e+\u003c/span\u003eThat's swell\u003c/ins\u003e\u003c/li\u003e\n      \u003c/ul\u003e\n    \u003c/div\u003e\n\n### Number of lines of context around changes\n\nYou can use the `:context` option to override the number of lines of context\nthat are shown around each change (this defaults to 10000 to show the full\nfile).\n\n    \u003e\u003e puts Diffy::Diff.new(\"foo\\nfoo\\nBAR\\nbang\\nbaz\", \"foo\\nfoo\\nbar\\nbang\\nbaz\", :context =\u003e 1)\n     foo\n    -BAR\n    +bar\n     bang\n\n\n### Overriding the command line options passed to diff.\n\nYou can use the `:diff` option to override the command line options that are\npassed to unix diff. They default to `-U10000`.  This option will noop if\ncombined with the `:context` option.\n\n    \u003e\u003e puts Diffy::Diff.new(\" foo\\nbar\\n\", \"foo\\nbar\\n\", :diff =\u003e \"-w\")\n      foo\n     bar\n\n### `:ignore_crlf` when doing HTML compares\n\nYou can make the HTML output ignore the CRLF by passing the `:ignore_crlf` option a truthy value.\n\n    \u003e\u003e puts Diffy::Diff.new(\" foo\\nbar\\n\", \"foo\\r\\nbar\\r\\n\", ignore_crlf: true).to_s(:html)\n      \"\u003cdiv class=\\\"diff\\\"\u003e\u003c/div\u003e\"\n\n\n\nDefault Diff Options\n--------------------\n\nYou can set the default options for new `Diffy::Diff`s using the\n`Diffy::Diff.default_options` and `Diffy::Diff.default_options=` methods.\nOptions passed to `Diffy::Diff.new` will be merged into the default options.\n\n    \u003e\u003e Diffy::Diff.default_options\n    =\u003e {:diff=\u003e\"-U10000\", :source=\u003e\"strings\", :include_diff_info=\u003efalse, :include_plus_and_minus_in_html=\u003efalse}\n    \u003e\u003e Diffy::Diff.default_options.merge!(:source =\u003e 'files')\n    =\u003e {:diff=\u003e\"-U10000\", :source=\u003e\"files\", :include_diff_info=\u003efalse, :include_plus_and_minus_in_html=\u003efalse}\n\n\nCustom Formats\n--------------\n\nDiffy tries to make generating your own custom formatted output easy.\n`Diffy::Diff` provides an enumerable interface which lets you iterate over\nlines in the diff.\n\n    \u003e\u003e Diffy::Diff.new(\"foo\\nbar\\n\", \"foo\\nbar\\nbaz\\n\").each do |line|\n    \u003e*   case line\n    \u003e\u003e   when /^\\+/ then puts \"line #{line.chomp} added\"\n    \u003e\u003e   when /^-/ then puts \"line #{line.chomp} removed\"\n    \u003e\u003e   end\n    \u003e\u003e end\n    line +baz added\n    =\u003e [\" foo\\n\", \" bar\\n\", \"+baz\\n\"]\n\nYou can also use `Diffy::Diff#each_chunk` to iterate each grouping of additions,\ndeletions, and unchanged in a diff.\n\n    \u003e\u003e Diffy::Diff.new(\"foo\\nbar\\nbang\\nbaz\\n\", \"foo\\nbar\\nbing\\nbong\\n\").each_chunk.to_a\n    =\u003e [\" foo\\n bar\\n\", \"-bang\\n-baz\\n\", \"+bing\\n+bong\\n\"]\n\nUse `#map`, `#inject`, or any of Enumerable's methods.  Go crazy.\n\n\nTesting\n------------\n\nDiffy includes a full set of rspec tests.  When contributing please include\ntests for your changes.\n\n[![Build Status](https://secure.travis-ci.org/samg/diffy.png)](http://travis-ci.org/samg/diffy)\n\n---------------------------------------------------------------------\n\nReport bugs or request features at http://github.com/samg/diffy/issues\n\n","funding_links":[],"categories":["Ruby","Diff"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamg%2Fdiffy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamg%2Fdiffy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamg%2Fdiffy/lists"}