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
80
81
82
83
84
85
86
87
88
|
# File 'lib/git_recommend.rb', line 23
def change
q = []
staged = (@@in_git_repo ? `git diff --cached --name-only`.split("\n") : [])
unstaged = (@@in_git_repo ? `git diff --name-only`.split("\n") : [])
from_command_line = (options[:query].nil? ? [] : options[:query])
from_file = if options[:query_from_file].nil?
[]
else
if File.exist?(options[:query_from_file])
File.read(options[:query_from_file]).split(%r((?:\r)?\n)) else
$stderr.puts "No file found at #{options[:query_from_file]}"
[]
end
end
if [:staged,:unstaged,:query,:query_from_file].all? {|k| options[k].nil?}
if @@in_git_repo
q = (staged | unstaged)
$stderr.puts "[default] Change recommendation based on the currently changed files\n"
end
else
staged = (options[:staged].nil? ? [] : staged)
unstaged = (options[:staged].nil? ? [] : unstaged)
q = (staged | unstaged | from_command_line | from_file)
end
if !File.exists?(options[:history])
exit unless HighLine.new.agree("To recommend changes, a history cache needs to be generated, do you want to generate the default cache now? Run 'git recommend help update' for more options.")
GitRecommend.start(["update"])
end
h = Evoc::HistoryStore.initialize(path: options[:history])
if q.size > 0
$stderr.puts "Generating change recommendation for #{q.size} items"
q_tx = Evoc::Tx.new(id: 'query',date: Time.now, items: q)
h.base_history << q_tx
s = Evoc::Scenario.new({query: q_tx.items,
tx_index: q_tx.index,
tx_id: q_tx.id,
model_size: options[:model_size],
algorithm: options[:algorithm],
aggregator: options[:aggregator],
max_size: options[:max_size],
measures: options[:measures]})
s.call
items_in_lhs = s.recommendation.map {|r| r.human_lhs}.flatten.uniq
new_items = q - items_in_lhs
if new_items.size > 0
if new_items.size < 11
$stderr.puts "Unable to generate change recommendations for the following items, maybe update the history?:\n #{new_items.to_a.join("\n")}"
else
$stderr.puts "Unable to generate change recommendations for #{new_items.size} items, maybe update the history?. The first 10 were: \n #{new_items.take(10).join("\n")}"
end
end
s.print
exit 0
else
$stderr.puts "No changed files detected. Change some files or run with --help for more options."
end
end
|