samg / diffy Goto Github PK
View Code? Open in Web Editor NEWEasy Diffing in Ruby
Home Page: http://rubygems.org/gems/diffy
License: MIT License
Easy Diffing in Ruby
Home Page: http://rubygems.org/gems/diffy
License: MIT License
The following (input) diff string is not getting rendered correctly in html output, the "\n" is treated as a newline, so it is not visible and the quote that follows it gets shown on the next line by itself.
+#include < iostream>
+
+using namespace std;
+
void main()
{
Anyway to offer Diffy css in an scss variant?
.diff {
overflow:auto;
ul {
background:#fff;
overflow:auto;
font-size:13px;
list-style:none;
margin:0;
padding:0;
display:table;
width:100%;
}
del, ins {
display:block;
text-decoration:none;
}
li{
padding:0;
display:table-row;
margin: 0;
height:1em;
&.ins{
background:#dfd;
color:#080
}
&.del{
background:#fee;
color:#b00
}
&.diff-comment {
display: none;
}
&.diff-block-info {background: none repeat scroll 0 0 gray;}
&:hover{background:#ffc}
}
del strong{
font-weight:normal;
background:#fcc;
}
ins strong{
font-weight:normal;
background:#9f9;
}
del, ins, span {
white-space:pre-wrap;
font-family:courier;
}
}
I'm using ruby 1.9.3p327 (2012-11-10) [i386-mingw32] under Windows XP SP 3.
Here are the steps I followed:
which diff
worked. Output: C:\Program Files\GnuWin32\bin\diff.EXEgem install diffy 2.1.2
.After all that, I tried using the following command in a script: puts Diffy::Diff.new(item, item2, :context => 5), where item and item2 are strings containing relative file paths (e.g., "./path to /file"). Unfortunately, I get the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/diff.rb:52:in ``': No
such file or directory - C:\Program Files\GnuWin32\bin\diff.exe -U 5 D:/DOCUME11/Temp/diffy20131018-3216-9e9xgo D:/DOCUME
/t0148524/LOCALS1/t0148524/LOCALS1/
Temp/diffy20131018-3216-1l2fpzz (Errno::ENOENT)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/diff.rb:5
2:indiff' from C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/diff.rb:6 9:in
each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/format.rb
:23:in`to_a'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/format.rb
:23:in `text'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/diffy-2.1.2/lib/diffy/diff.rb:1
17:in`to_s'
from script_diff.rb:44:in `puts'
from script_diff.rb:44:in`puts'
from script_diff.rb:44:in `block (2 levels) in
I'm not sure what I'm doing wrong, but any help here would be appreciated!
I'm trying to run this simle code in irb:
require 'rubygems'
require 'diffy'
puts Diffy::Diff.new('asdqwerty ion', 'qwertyu iop')
At this point I'm getting error:
Errno::ENOENT: No such file or directory - which diff
Error appears only after using 'puts' method. Is it a known issue? What is my problem?
Thanks, Marc.
I am not sure whether there is a feature to apply the diff to the original string to get back the new string. If not please consider this as feature request.
Input:
Diffy::Diff.new("a b c d e f g\n", "a B c D e F g\n").to_s(:html)
Output:
<div class="diff">
<ul>
<li class="del"><del>a <strong>b c d e f</strong> g</del></li>
<li class="ins"><ins>a <strong>B c D e F</strong> g</ins></li>
</ul>
</div>
Desired output (given a new option :html_multiple
):
<div class="diff">
<ul>
<li class="del"><del>a <strong>b</strong> c <strong>d</strong> e <strong>f</strong> g</del></li>
<li class="ins"><ins>a <strong>B</strong> c <strong>D</strong> e <strong>F</strong> g</ins></li>
</ul>
</div>
This would make it possible to easily spot three modifications on a single long line. Currently it is only easy to spot the two corners.
Should be technically feasible, since git diff --color-words
does it:
If diff is failing for any reason (e.g. because of wrong arguments when providing them via diff_options) the diff method is silently failing, returning no diff but no error message either. That's probably because it's not evaluating stderr nor return code in the block where diff output is being processed.
Just a thought: On WIndows it may be worthwhile checking for ldiff
as a lot of people use rspec
and rspec
depends on diff-lcs
. May be feckless too. What do you think?
Hi
I've been testing diffy and was wondering if it's possible to achieve a similar output to:
http://img715.imageshack.us/img715/8824/highlightingstyle.png
This is produced using another gem called htmldiff, the downside though is it's not as clever as diffy when it comes to highlighting characters within a word.
Essentially what I'm after is to view one version of the text with both additions and removals in it. A bit like track changes in MS Word.
Currently I'm seeing the following using :html http://img41.imageshack.us/img41/9761/highlightingstyle2.png
It does not make any sense for that message to show up on comparison of two strings.
I have not seen the code, but seems like you are taking two strings, putting them in
two files and comparing them. Could you provide an option to suppress that warning.
It would be nice to not be able to see that warning especially when comparing
a lot of strings.
Diffy 3.0.6
Ruby 2.1.3
diffutils 3.3
https://github.com/samg/diffy#overriding-the-command-line-options-passed-to-diff claims
>> puts Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :diff => "-w")
foo
bar
But when I try:
2.1.3p242 :001 > puts Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :diff => "-w")
=> nil
or even with a blank override:
2.1.3p242 :002 > puts Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n", :diff => "")
=> nil
but works with no override:
2.1.3p242 :003 > puts Diffy::Diff.new(" foo\nbar\n", "foo\nbar\n")
- foo
+foo
bar
=> nil
In default and ANSI color views, it deems like DIffy doesn't show line numbers. That would be a really great feature to have!
When using the split diff on 2 string with a different number of lines, the matching lines don't line up. For example:
input1:
A
B
C
D
input 2:
A
D
The current (html) output is something like:
A A
B D
C
D
while I would prefer
A A
B
C
D D
Is this supported by Diffy or do you have any pointer on how to add such functionality?
I have two files A, B with a handful of differing lines.
If I do:
puts Diffy::Diff.new(A, B, {:source => 'files'}).to_s
It gives me the output of bash command diff -U 10000 A B
as expected.
If I instead do
puts Diffy::Diff.new(A, B, {:diff => '', :source => 'files'}).to_s
It gives me no output at all, whereas bash command diff A B
gives me the minimal output I'm looking for i.e. just the changed lines.
Can you please explain why this doesn't work?
Running diffs for a larger amount (several thousands) of texts under JRuby 1.7.10 throws Bad file descriptor errors:
Errno::EBADF: Bad file descriptor - Bad file descriptor
getStackTrace at java/lang/Thread.java:1588
getBacktraceData at org/jruby/runtime/backtrace/TraceType.java:175
getBacktrace at org/jruby/runtime/backtrace/TraceType.java:39
prepareBacktrace at org/jruby/RubyException.java:224
preRaise at org/jruby/exceptions/RaiseException.java:213
preRaise at org/jruby/exceptions/RaiseException.java:194
<init> at org/jruby/exceptions/RaiseException.java:110
newRaiseException at org/jruby/Ruby.java:3774
newErrnoEBADFError at org/jruby/Ruby.java:3322
finalize at org/jruby/util/io/OpenFile.java:490
cleanup at org/jruby/util/io/OpenFile.java:401
ioClose at org/jruby/RubyIO.java:2120
close at org/jruby/RubyIO.java:2097
cleanupPOpen at org/jruby/RubyIO.java:4477
popen3_19 at org/jruby/RubyIO.java:4435
callBlock at org/jruby/runtime/callsite/CachingCallSite.java:79
call at org/jruby/runtime/callsite/CachingCallSite.java:85
callVarargs at org/jruby/runtime/callsite/CachingCallSite.java:116
popen3 at /home/paul/.rvm/rubies/jruby-1.7.10/lib/ruby/1.9/open3.rb:74
popen3 at /home/paul/.rvm/rubies/jruby-1.7.10/lib/ruby/1.9/open3.rb:74
call at org/jruby/internal/runtime/methods/JittedMethod.java:101
call at org/jruby/internal/runtime/methods/DefaultMethod.java:167
call at org/jruby/internal/runtime/methods/WrapperMethod.java:90
callBlock at org/jruby/runtime/callsite/CachingCallSite.java:79
callIter at org/jruby/runtime/callsite/CachingCallSite.java:90
callVarargsIter at org/jruby/runtime/callsite/CachingCallSite.java:126
diff at /home/paul/.rvm/gems/jruby-1.7.10/gems/diffy-3.0.1/lib/diffy/diff.rb:55
diff at /home/paul/.rvm/gems/jruby-1.7.10/gems/diffy-3.0.1/lib/diffy/diff.rb:55
call at org/jruby/internal/runtime/methods/JittedMethod.java:141
call at org/jruby/runtime/callsite/CachingCallSite.java:134
This might be related to jruby/jruby/issues/781.
Since different diff implementation support different feature sets (e.g. BSD find vs. GNU find), it would be helpful if one could chose which find binary is used. Could this feature be added?
diffy does not work on windows due to:
I propose that diffy uses system() instead.
I'm using diffy-2.1.2 with Ruby 1.9.3p327 (2012-11-10) [i386-mingw32] on Windows XP SP3.
I have successfully (or so I think) configured diffy on the computer. However, diffy doesn't seem to be diffing.
For example, the output for puts Diffy::Diff.new("blue\n", "red\n").to_s(:html)
is
`
Am I missing something here?
Thanks,
Grasswistle
Great gem! I dig it. But I'm still really unclear how to utilize the css stylings mentioned in the readme. If I've diffed two strings, how I do get different colors for additions and deletetions? Or am I 100% misunderstanding things and I just need to create my own diff elements in my css file?
I love a lot of what I read in the ReadMe. The functionality seems to be almost exactly what I need.
The one thing that's missing is that it seems to focus primarily on raw text as the input.
Would this gem work with the actual diff data returned by Github API?
For instance, here is a sample from the API:
:patch=>
"@@ -727,7 +727,6 @@ def require_association_class(class_name)\n def add_multiple_associated_save_callbacks(association_name)\n method_name = \"validate_associated_records_for_\#{association_name}\".to_sym\n define_method(method_name) do\n- @new_record_before_save = new_record?\n association = instance_variable_get(\"@\#{association_name}\")\n if association.respond_to?(:loaded?)\n if new_record?\n@@ -741,6 +740,7 @@ def add_multiple_associated_save_callbacks(association_name)\n end\n \n validate method_name\n+ before_save(\"@new_record_before_save = new_record?; true\")\n \n after_callback = <<-end_eval\n association = instance_variable_get(\"@\#{association_name}\")\n@@ -754,9 +754,6 @@ def add_multiple_associated_save_callbacks(association_name)\n records_to_save.each { |record| association.send(:insert_record, record) }\n association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id\n end\n- \n- @new_record_before_save = false\n- true\n end_eval\n \n # Doesn't use after_save as that would save associations added in after_create/after_update twice"}
]
Can I use this patch
data as input and be able to customize it easily per the docs?
Hello Sam,
great gem, thanks for writing it. Looks like the current master has some useful things in it that have not been released as a gem yet. Would it be possible to release a new version?
When diffing two files,
ArgumentError: invalid byte sequence in UTF-8
from /var/lib/gems/1.9.1/gems/diffy-2.0.7/lib/diffy/diff.rb:49:in diff' from /var/lib/gems/1.9.1/gems/diffy-2.0.7/lib/diffy/diff.rb:61:in
each'
from /var/lib/gems/1.9.1/gems/diffy-2.0.7/lib/diffy/format.rb:23:in to_a' from /var/lib/gems/1.9.1/gems/diffy-2.0.7/lib/diffy/format.rb:23:in
text'
from /var/lib/gems/1.9.1/gems/diffy-2.0.7/lib/diffy/diff.rb:104:in to_s' from /usr/bin/irb:12:in
When the only difference between two string is a final newline, the last line disappears in the HTML diff.
> puts Diffy::Diff.new("1\n2", "1\n2\n").to_s
1
-2
\ No newline at end of file
+2
=> nil
> puts Diffy::Diff.new("1\n2", "1\n2\n").to_s(:html)
<div class="diff">
<ul>
<li class="unchanged"><span>1</span></li>
</ul>
</div>
=> nil
When a line on the left side of a diff starts with --
, the line is dropped from the diff. When running puts Diffy::Diff.new("aa\n--\ncc\n", "aa\nbb\ncc\n")
you get:
aa
+bb
cc
but you'd expect:
aa
---
+bb
cc
More examples:
puts Diffy::Diff.new("a a\n-- b\nc c", "a a\nb b\nc c") # fails
puts Diffy::Diff.new("a a\n- b\nc c", "a a\nb b\nc c") # works
puts Diffy::Diff.new("a\n-\nc", "a\nb\nc") # works
puts Diffy::Diff.new("a\n---\nc", "a\n----\nc") # fails
Diffy's been serving us really well otherwise, btw, so thanks for the great work!
Description on Stack Overflow: http://stackoverflow.com/questions/26146785/diffy-does-not-recognize-unchanged-strings
Diffy is almost what I needed, except for the part where I also wanted to generate patches from a diff.
Is this feature being considered for the long run?
Hello, I dev on my Windows but my production server is an Ubuntu 14.04. When I deployed my application in production, Diffy gem was installed, but when I use it, it said:
RuntimeError (Can't find a diff executable in PATH /usr/local/lib/ruby/gems/2.3.0/bin):
Here is a screen of my linux diff package:
http://prntscr.com/b6105m
Thanks for help ๐
Diffy 3.0.2, Ruby 2.0.0.
2.0.0p247 :014 > Diffy::Diff.new("one\ntwo\nthree\n", "one\r\ntwo\r\nthree\r\n").to_s
=> "-one\n-two\n-three\n+one\r\n+two\r\n+three\r\n"
2.0.0p247 :015 > Diffy::Diff.new("one\ntwo\nthree\n", "one\r\ntwo\r\nthree\r\n").to_s(:html_simple)
=> "<div class=\"diff\">\n <ul>\n <li class=\"del\"><del>one</del></li>\n <li class=\"del\"><del>two</del></li>\n <li class=\"del\"><del>three</del></li>\n <li class=\"ins\"><ins>one</ins></li>\n <li class=\"ins\"><ins>two</ins></li>\n <li class=\"ins\"><ins>three</ins></li>\n </ul>\n</div>\n"
2.0.0p247 :016 > Diffy::Diff.new("one\ntwo\nthree\n", "one\r\ntwo\r\nthree\r\n").to_s(:html)
=> "<div class=\"diff\"></div>"
Why would these not show up as different when using :html output only?
for html output, would be nice to have an option to keep the '+' and '-' you get in the text output, reason being someone might be colour blind, this way they see the +/- and other users can enjoy the nice colours
diffy = Diffy::Diff.new(content, content_resign ,:allow_empty_diff => false ,:diff => '-f').
I want to implement the command 'diff -f <(xxd /Users/mac/Documents/ruby/signapp/public/textss ) <(xxd /Users/mac/Documents/ruby/signapp/public/textss_r )'. So I add :diff option. When I add :diff option, it outputs nothing.
The following script produce the error on OSX
require 'diffy'
1.upto (1000) {|i|
s1= "this is string #{i} which \nwill be compared to #{i+1}\n"
s2= "this is string #{i+1} which \nwill be compared to #{i}\n"
a = Diffy::Diff.new(s1, s2).to_s(:html)
puts a
}
I guess the problem is, that tempfile() does keep the temporary files open.
Try the following two diffs
line 1
line 2
line 3
\\( line 4 \\) test test2
line 5
and
line 1
line 2
line 3
\\ line 4
line 5
Line 4 will be omitted completely from the diffs because the diff utility freaks out for some reason.
Hi there, awesome gem - thank you!
I'd like to store my diffs in a DB as plain text diffs to make them smaller, more portable, etc. This part is easy.
But then, I'd like to pull them out of the DB and display them as HTML - basically with the same output as
Diffy::Diff.default_format = :html
Would give me. I don't want to redo the diff (the info that created the diff is gone at this point).
Maybe something like
Diffy::Diff.convert(stored_diff).to_s(:html)
Seems like I might be missing something or someone else might have solved this problem.
Right now, I am storing the diff as HTML but that wouldn't be as cool b/c later I'd have to strip out the html to display it in a txt environment.
Much thanks!
I would like to display the diff as a side-by-side comparison split view. Similar to the GitHub split view:
I'm using the following code:
left = 'left document'
right = 'right document'
html = Diffy::Diff.new(left, right).to_s(:html)
Diffy generates the HTML as a list, so I'm not sure how to split it apart in order to display left and right.
If you diff two strings that rarely have newlines or none at all, how would you avoid duplicating a lot of text in the unchanged part? Especially in the HTML output? Any hints on this?
For example:
"This is my string which has a lot of some text but no newlines and only a very small fraction of information is changed"
I review the source code, it only support string
and file
, but actually diff
in linux support directory, so will it support?
Is it possible to get a example of true or false if statement using diffy ? I'm working on a project that just needs to check if theres a difference if so then take a action.
I would like to know if there is some configuration for accept ONLY char diff like: http://kpdecker.github.io/jsdiff/
So this way it keeps all in one unique text, instead of a duplicate text
Thanks!
Hi there,
thanks for making diffy! Helped a lot in a current task :)
One thing though, is there an option or would you want there to be an option to ignore different line endings?
E.g.
2.0.0-p451 :001 > string1 = "Hello there!\nWhat a beautiful day\n"
=> "Hello there!\nWhat a beautiful day\n"
2.0.0-p451 :002 > string2 = string1.clone.gsub "\n", "\r\n"
=> "Hello there!\r\nWhat a beautiful day\r\n"
2.0.0-p451 :003 > Diffy::Diff.new(string1, string2).to_s
=> "-Hello there!\n-What a beautiful day\n+Hello there!\r\n+What a beautiful day\r\n"
bit me in the project (data is saved with UNIX line endings, comes in through the browser with /r/n)
I know it's a matter of a simple gsub, which is my current workaround, but I think it'd be a nice feature to have (or thing to mention in the README if it exists already, didn't find it)
Cheers and thanks!
Tobi
Some companies will only use gems with a certain license.
The canonical and easy way to check is via the gemspec
via e.g.
spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']
There is even a License Finder to help companies ensure all gems they use
meet their licensing needs. This tool depends on license information being available in the gemspec.
Including a license in your gemspec is a good practice, in any case.
How did I find you?
I'm using a script to collect stats on gems, originally looking for download data, but decided to collect licenses too,
and make issues for missing ones as a public service :)
https://gist.github.com/bf4/5952053#file-license_issue-rb-L13 So far it's going pretty well
Diffy::Diff.new(str2, str2, include_diff_info: true, diff: '-u').to_s :text
returns str2
content instead of ''
Any reason for this? You're doing the work that system
does automatically. Unless you're checking for the existence of diff
I don't think calling which
makes much sense.
Plus, on Windows, it's possible one has diff
but not which
. For example:
irb(main):002:0> Diffy::Diff.new("A","B").to_a
Errno::ENOENT: No such file or directory - which diff.exe
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/diffy-3.0.6/lib/diffy/diff.rb:1
47:in ``'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/diffy-3.0.6/lib/diffy/diff.rb:1
47:in `diff_bin'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/diffy-3.0.6/lib/diffy/diff.rb:5
4:in `diff'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/diffy-3.0.6/lib/diffy/diff.rb:8
5:in `each'
from (irb):2:in `to_a'
from (irb):2
from C:/Ruby200/bin/irb:12:in `<main>'
irb(main):003:0> `diff`
diff: missing operand after `diff'
diff: Try `diff --help' for more information.
Though the diff binary that ships with Alpine linux will produce output as expected for differing files, diffy fails to produce any output.
I've tested diffy with all output types => :text
, :color
, :html
, etc.
I've also tested with variables and files.
After compiling gnu diff from source and installing in /usr/bin, diffy works!
I'm using diffy-2.1.2 with Ruby 1.9.3p327 (2012-11-10) [i386-mingw32] on Windows XP SP3.
For
Diffy::Diff.new(item, item2, :source => 'files')
where item and item2 are strings containing a path to files (at least one of which contains spaces), the following message is obtained:
C:\unixutils\diff.exe: extra operand
to/file.ext'
C:\unixutils\diff.exe: Try C:\unixutils\diff.exe --help' for more information.
Not a big deal because there is an easy work around:
content1="" content2="" File.open(item) {|f| content1 = f.read} File.open(item2) {|f| content2 = f.read} Diffy::Diff.new(content1, content2)
Grasswistle
We ended up filling up the /tmp partition on a production server, seems as it creates a whole bunch of temp files that aren't cleaned up.
I don't have specific details, but I bet I could reproduce it by doing lots and lots of diffs in a short time period (which is something I'm doing.)
It would be nice for some purposes to be able to save the diff in a db and then load it again, but it looks like there's no method on Diffy::Diff
for loading a previously dumped diff. Am I viewing this correctly?
I'm attempting to compare two html strings.
In my view I have the following:
<%string1 = "<p>Hello</p>"%>
<%string2 = "<p>Hello World</p>"%>
<%= Diffy::Diff.new(string1, string2).to_s(:html).html_safe %>
but the output still contains the
tags
Hello
Hello World
I would like:
Hello
Hello World
Even if i try to make the strings safe I still get the same output. Any ideas about how I could get around this?
At certain times I just want to have the number of lines added and deleted, rather than a visual diff output. I can't seem to find a method to give me access to this information - it seems the only way is to regex the output to determine this by using each
on ever new line . Am I wrong? It seems as if this would be a nice feature to have, even if diffy
is more intended for visual output.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.