Class: RubyCritic::SourceControlSystem::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/rubycritic/source_control_systems/git.rb,
lib/rubycritic/source_control_systems/git/churn.rb

Defined Under Namespace

Classes: Churn, Renames, Stats

Constant Summary collapse

GIT_EXECUTABLE =
TTY::Which.which('git')

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connected_system_names, create, register_system, systems

Class Method Details

.check_git_repository?(path) ⇒ Boolean

:reek:DuplicateMethodCall

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/rubycritic/source_control_systems/git.rb', line 38

def self.check_git_repository?(path)
  current_path = File.expand_path(path)
  while current_path != File.dirname(current_path)
    return true if Dir.exist?(File.join(current_path, '.git'))

    current_path = File.dirname(current_path)
  end
  false
end

.current_branchObject



102
103
104
105
106
107
# File 'lib/rubycritic/source_control_systems/git.rb', line 102

def self.current_branch
  branch_list = `git branch`
  branch = branch_list.match(/\*.*$/)[0].gsub('* ', '')
  branch = branch.gsub(/\(HEAD detached at (.*)\)$/, '\1') if /\(HEAD detached at (.*)\)$/.match?(branch)
  branch
end

.git(arg) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/rubycritic/source_control_systems/git.rb', line 12

def self.git(arg)
  if Gem.win_platform?
    `\"#{GIT_EXECUTABLE}\" #{arg}`
  else
    `#{GIT_EXECUTABLE} #{arg}`
  end
end

.modified_filesObject



92
93
94
95
96
97
98
99
100
# File 'lib/rubycritic/source_control_systems/git.rb', line 92

def self.modified_files
  modified_files = `git diff --name-status #{Config.base_branch} #{Config.feature_branch}`
  modified_files.split("\n").filter_map do |line|
    next if line.start_with?('D')

    file_name = line.split("\t")[1]
    file_name
  end
end

.supported?Boolean

:reek:DuplicateMethodCall :reek:NilCheck

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/rubycritic/source_control_systems/git.rb', line 26

def self.supported?
  return true if git('branch 2>&1') && $CHILD_STATUS.success?

  return false if Config.paths.nil? || Config.paths.empty?

  Config.paths.any? do |path|
    absolute_path = File.expand_path(path)
    check_git_repository?(absolute_path)
  end
end

.switch_branch(branch) ⇒ Object



79
80
81
82
83
84
# File 'lib/rubycritic/source_control_systems/git.rb', line 79

def self.switch_branch(branch)
  dirty = !uncommitted_changes.empty?
  abort("Uncommitted changes are present: #{uncommitted_changes}") if dirty

  git("checkout #{branch}")
end

.to_sObject



48
49
50
# File 'lib/rubycritic/source_control_systems/git.rb', line 48

def self.to_s
  'Git'
end

.uncommitted_changesObject



86
87
88
89
90
# File 'lib/rubycritic/source_control_systems/git.rb', line 86

def self.uncommitted_changes
  return @uncommitted_changes if defined? @uncommitted_changes

  @uncommitted_changes = git('diff-index HEAD --').chomp! || ''
end

Instance Method Details

#churnObject



52
53
54
# File 'lib/rubycritic/source_control_systems/git.rb', line 52

def churn
  @churn ||= Churn.new(churn_after: Config.churn_after, paths: Config.paths)
end

#date_of_last_commit(path) ⇒ Object



60
61
62
# File 'lib/rubycritic/source_control_systems/git.rb', line 60

def date_of_last_commit(path)
  churn.date_of_last_commit(path)
end

#git(arg) ⇒ Object



20
21
22
# File 'lib/rubycritic/source_control_systems/git.rb', line 20

def git(arg)
  self.class.git(arg)
end

#head_referenceObject



68
69
70
# File 'lib/rubycritic/source_control_systems/git.rb', line 68

def head_reference
  git('rev-parse --verify HEAD').chomp!
end

#revision?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rubycritic/source_control_systems/git.rb', line 64

def revision?
  head_reference && $CHILD_STATUS.success?
end

#revisions_count(path) ⇒ Object



56
57
58
# File 'lib/rubycritic/source_control_systems/git.rb', line 56

def revisions_count(path)
  churn.revisions_count(path)
end

#travel_to_headObject



72
73
74
75
76
77
# File 'lib/rubycritic/source_control_systems/git.rb', line 72

def travel_to_head
  stash_successful = stash_changes?
  yield
ensure
  travel_to_original_state if stash_successful
end