Class: GitRecommend

Inherits:
Thor
  • Object
show all
Defined in:
lib/git_recommend.rb

Constant Summary collapse

DEFAULT_HISTORY =
'.history'
@@in_git_repo =
File.exist?('.git')

Instance Method Summary collapse

Instance Method Details

#changeObject



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
  # Initialize query
  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)) # split lines on \r\n or \n
                else
                  $stderr.puts "No file found at #{options[:query_from_file]}"
                  []
                end
              end

  # default to locally changed files
  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

  # Initialize history
  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
  # could do some validation here
  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
    # check if there were some items in the query which could not be found in the history
    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

#updateObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/git_recommend.rb', line 98

def update
  if @@in_git_repo
    if File.exists?(DEFAULT_HISTORY)
      exit unless HighLine.new.agree("History already exists, do you want to replace it?")
    end
    $stderr.puts "Generating new history cache to #{DEFAULT_HISTORY}, this may take a while."
    old_stdout = $stdout              # backup old stdout
    $stdout.reopen(DEFAULT_HISTORY, 'w')   # redirect stdout to file
    Vcs2Json::Git.new(options).parse  # write history
    $stdout = STDOUT              # restore stdout
  else
    $stderr.puts "Need to be in a git repository to generate history cache."
  end
end