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