2012-08-16 18:02:03 +02:00
|
|
|
module Mutant
|
2012-08-16 19:26:15 +02:00
|
|
|
# Class to create diffs from source code
|
2014-05-11 15:04:11 +00:00
|
|
|
class Diff
|
2013-07-02 19:06:03 +02:00
|
|
|
include Adamantium::Flat, Concord.new(:old, :new)
|
2013-06-28 23:33:53 +02:00
|
|
|
|
2014-06-29 22:56:08 +00:00
|
|
|
ADDITION = '+'.freeze
|
|
|
|
DELETION = '-'.freeze
|
|
|
|
NEWLINE = "\n".freeze
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Unified source diff between old and new
|
2012-08-16 19:26:15 +02:00
|
|
|
#
|
|
|
|
# @return [String]
|
2014-06-08 11:42:53 +00:00
|
|
|
# if there is exactly one diff
|
2013-07-05 00:54:50 +02:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-08-16 18:02:03 +02:00
|
|
|
def diff
|
2014-08-11 18:26:19 +00:00
|
|
|
return if diffs.empty?
|
2014-08-11 17:35:18 +00:00
|
|
|
|
|
|
|
minimized_hunks.map do |hunk|
|
|
|
|
hunk.diff(:unified) << NEWLINE
|
|
|
|
end.join
|
2012-08-16 18:02:03 +02:00
|
|
|
end
|
|
|
|
memoize :diff
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Colorized unified source diff between old and new
|
2012-08-16 19:26:15 +02:00
|
|
|
#
|
|
|
|
# @return [String]
|
2013-07-05 00:54:50 +02:00
|
|
|
# if there is a diff
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-08-16 18:02:03 +02:00
|
|
|
def colorized_diff
|
2013-07-05 00:54:50 +02:00
|
|
|
return unless diff
|
2014-08-11 13:54:17 +00:00
|
|
|
diff.lines.map(&self.class.method(:colorize_line)).join
|
2012-08-16 18:02:03 +02:00
|
|
|
end
|
|
|
|
memoize :colorized_diff
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Build new object from source strings
|
2012-08-16 19:26:15 +02:00
|
|
|
#
|
2013-04-17 20:31:21 -07:00
|
|
|
# @param [String] old
|
2012-08-16 19:26:15 +02:00
|
|
|
# @param [String] new
|
|
|
|
#
|
2014-05-11 15:04:11 +00:00
|
|
|
# @return [Diff]
|
2013-07-02 20:10:59 +02:00
|
|
|
def self.build(old, new)
|
|
|
|
new(lines(old), lines(new))
|
2012-08-16 19:26:15 +02:00
|
|
|
end
|
|
|
|
|
2013-07-02 20:12:54 +02:00
|
|
|
# Break up source into lines
|
|
|
|
#
|
|
|
|
# @param [String] source
|
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
|
|
|
def self.lines(source)
|
2014-10-08 00:13:05 +00:00
|
|
|
source.lines.map(&:chomp)
|
2013-07-02 20:12:54 +02:00
|
|
|
end
|
|
|
|
private_class_method :lines
|
|
|
|
|
2013-07-02 19:06:03 +02:00
|
|
|
private
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Diffs between old and new
|
2012-12-29 20:12:48 +01:00
|
|
|
#
|
2013-07-02 19:06:03 +02:00
|
|
|
# @return [Array<Array>]
|
|
|
|
def diffs
|
2014-05-11 15:04:11 +00:00
|
|
|
::Diff::LCS.diff(old, new)
|
2013-07-02 19:06:03 +02:00
|
|
|
end
|
2014-08-11 17:35:18 +00:00
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Raw diff-lcs hunks
|
2014-08-11 17:35:18 +00:00
|
|
|
#
|
|
|
|
# @return [Array<Diff::LCS::Hunk>]
|
|
|
|
def hunks
|
|
|
|
diffs.map do |diff|
|
|
|
|
::Diff::LCS::Hunk.new(old, new, diff, max_length, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Minimized hunks
|
2014-08-11 17:35:18 +00:00
|
|
|
#
|
|
|
|
# @return [Array<Diff::LCS::Hunk>]
|
|
|
|
def minimized_hunks
|
2014-08-13 07:57:09 +00:00
|
|
|
head, *tail = hunks
|
2014-08-11 17:35:18 +00:00
|
|
|
|
2014-08-11 18:27:25 +00:00
|
|
|
tail.each_with_object([head]) do |right, aggregate|
|
2014-08-11 17:35:18 +00:00
|
|
|
left = aggregate.last
|
|
|
|
if right.overlaps?(left)
|
|
|
|
right.merge(left)
|
|
|
|
aggregate.pop
|
|
|
|
end
|
|
|
|
aggregate << right
|
|
|
|
end
|
|
|
|
end
|
2013-07-02 19:06:03 +02:00
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Max length of source line in new and old
|
2013-07-02 19:06:03 +02:00
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
def max_length
|
2013-07-02 23:34:35 +02:00
|
|
|
[old, new].map(&:length).max
|
2012-12-29 20:12:48 +01:00
|
|
|
end
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Colorized a unified diff line
|
2012-08-16 19:26:15 +02:00
|
|
|
#
|
|
|
|
# @param [String] line
|
|
|
|
#
|
|
|
|
# @return [String]
|
2012-08-16 18:02:03 +02:00
|
|
|
def self.colorize_line(line)
|
2013-07-02 20:19:24 +02:00
|
|
|
case line[0]
|
2014-06-29 22:56:08 +00:00
|
|
|
when ADDITION
|
2012-08-16 18:02:03 +02:00
|
|
|
Color::GREEN
|
2014-06-29 22:56:08 +00:00
|
|
|
when DELETION
|
2012-08-16 18:02:03 +02:00
|
|
|
Color::RED
|
|
|
|
else
|
|
|
|
Color::NONE
|
|
|
|
end.format(line)
|
|
|
|
end
|
2014-08-11 13:54:17 +00:00
|
|
|
private_class_method :colorize_line
|
2013-06-14 20:54:02 +02:00
|
|
|
|
2014-05-11 15:04:11 +00:00
|
|
|
end # Diff
|
2013-06-14 20:54:02 +02:00
|
|
|
end # Mutant
|