Method: CodeDiff#run

Defined in:
lib/codediff.rb

#run(old_file, new_file) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/codediff.rb', line 19

def run old_file, new_file

@res["old_file_lines"] = File.foreach(old_file).count
@res["new_file_lines"] = File.foreach(new_file).count

cmd = "diff -y --left-column " +  new_file + " " + old_file
`#{cmd}`.each_line { |line| 
	line = line.chomp
	if !line.end_with?("(") 
		lines = line.split("|")
		if lines.size > 1
			@res["accumulated_distance"] += Levenshtein.normalized_distance lines[0].chomp, lines[1].chomp
		end

		lines = line.split("<")
		if lines.size > 1
				@res["accumulated_distance"] += Levenshtein.normalized_distance lines[0].chomp, lines[1].chomp
		end
		
		lines = line.split(">")
		if lines.size > 1
			@res["accumulated_distance"] += Levenshtein.normalized_distance lines[0].chomp, lines[1].chomp
		end
	end
}

# count of different strings
Diffy::Diff.new(new_file, old_file, :source => 'files').each { |item| 	
	# remove white spaces
	item = item.gsub(/\s+/, "")

	# ignore space lines 		
	next if item.size == 1

	# see if changed starts with comment
	case item[0]
	when "+"
		case item[1]
		when "#", "/", "*"
			@res["comment_additions"] += 1 
		else
			@res["additions"] += 1 
		end
	when "-" 
		case item[1]
		when "#"
			@res["comment_subtraction"] += 1
		else
			@res["subtractions"] += 1 
		end
	end
}


	diff = [@res["additions"], @res["subtractions"]].max
	average = (@res["old_file_lines"] + @res["new_file_lines"]) / 2.0
	@res["percent_change"] = diff / average * 100

	return @res

end