class GitRecommend

Constants

DEFAULT_HISTORY

Public Instance Methods

change() click to toggle source
# 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]})
    recommendation = Evoc::RecommendationCache.get_recommendation(algorithm: s.algorithm,
                                                                  query: s.query,
                                                                  model_start: s.model_start,
                                                                  model_end: s.model_end,
                                                                  max_size: s.max_size,
                                                                  aggregator: s.aggregator,
                                                                  measures: s.measures)
    # check if there were some items in the query which could not be found in the history
    items_in_lhs = 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
    recommendation.print(s.measures)
    exit 0
  else
    $stderr.puts "No changed files detected. Change some files or run with --help for more options."
  end
end
update() click to toggle source
# File lib/git_recommend.rb, line 104
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