Class: RuboCop::Changed::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/changed/commands.rb

Overview

This class get changed

Constant Summary collapse

ERRORS =
[
  'git: command not found',
  'fatal:'
].freeze
GIT_COMMANDS =
{
  changed_files: 'git diff-tree -r --no-commit-id --name-status %<compared_branch>s %<current_branch>s',
  current_branch: 'git rev-parse --abbrev-ref HEAD',
  default_branch: "git remote show origin | sed -n '/HEAD branch/s/.*: //p' | xargs",
  new_files: 'git status --porcelain=v1'
}.freeze

Class Method Summary collapse

Class Method Details

.changed_files(branch = compared_branch) ⇒ Object



19
20
21
# File 'lib/rubocop/changed/commands.rb', line 19

def changed_files(branch = compared_branch)
  (diffed_files(branch) + new_files).uniq
end

.compared_branchObject

Raises:

  • (ArgumentError)


35
36
37
38
39
40
# File 'lib/rubocop/changed/commands.rb', line 35

def compared_branch
  branch = setted_branch || default_branch
  raise ArgumentError, 'You can not compare branch with itself' if branch == current_branch

  branch
end

.diffed_files(branch = compared_branch) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/changed/commands.rb', line 23

def diffed_files(branch = compared_branch)
  command = format(
    GIT_COMMANDS.fetch(:changed_files),
    compared_branch: branch,
    current_branch: current_branch
  )
  run(command)
    .split("\n")
    .filter_map { |line| line.split("\t").last unless line.match?(/D\t/) }
    .map { |file| File.absolute_path(file) }
end

.new_filesObject



42
43
44
45
46
# File 'lib/rubocop/changed/commands.rb', line 42

def new_files
  command = format(GIT_COMMANDS.fetch(:new_files))
  run(command).split("\n")
              .map { |file| File.absolute_path(file[3..]) }
end