-
Notifications
You must be signed in to change notification settings - Fork 259
/
diff
executable file
·50 lines (40 loc) · 1.19 KB
/
diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
#!/usr/bin/env ruby
# Diffs STDIN against a known license
# Example usage: cat LICENSE.md | be script/diff mit
require 'licensee'
require 'tmpdir'
if STDIN.tty? || ARGV[0].to_s.empty?
puts 'USAGE: [LICENSE_CONTENT] | script/diff [LICENSE_ID]'
exit 1
end
text = STDIN.read
license_file = Licensee::ProjectFiles::LicenseFile.new(text, 'LICENSE')
license = Licensee::License.find(ARGV[0])
if license.nil?
puts "#{ARGV[0]} is not a valid license"
licenses = Licensee::License.all(hidden: true).map(&:key).join(', ')
puts "Valid licenses: #{licenses}"
exit 1
end
puts "Comparing to #{license.name}:"
left = license.content_normalized(wrap: 80)
right = license_file.content_normalized(wrap: 80)
puts " Input length: #{license_file.length}"
puts " License length: #{license.length}"
similarity = license.similarity(license_file)
similarity = Licensee::ContentHelper.format_percent(similarity)
puts " Similarity: #{similarity}"
if left == right
puts ' Exact match!'
exit
end
dir = Dir.mktmpdir
path = File.expand_path 'LICENSE', dir
Dir.chdir(dir) do
`git init`
File.write(path, left)
`git add LICENSE`
`git commit -m 'left'`
File.write(path, right)
puts `git diff --word-diff`
end