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

.current_branchObject


82
83
84
85
# File 'lib/rubycritic/source_control_systems/git.rb', line 82

def self.current_branch
  branch_list = `git branch`
  branch_list.match(/\*.*$/)[0].gsub('* ', '')
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


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

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

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

.supported?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'lib/rubycritic/source_control_systems/git.rb', line 24

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

.switch_branch(branch) ⇒ Object


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

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

  git("checkout #{branch}")
end

.to_sObject


28
29
30
# File 'lib/rubycritic/source_control_systems/git.rb', line 28

def self.to_s
  'Git'
end

.uncommitted_changesObject


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

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

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

Instance Method Details

#churnObject


32
33
34
# File 'lib/rubycritic/source_control_systems/git.rb', line 32

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

#date_of_last_commit(path) ⇒ Object


40
41
42
# File 'lib/rubycritic/source_control_systems/git.rb', line 40

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


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

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

#revision?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/rubycritic/source_control_systems/git.rb', line 44

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

#revisions_count(path) ⇒ Object


36
37
38
# File 'lib/rubycritic/source_control_systems/git.rb', line 36

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

#travel_to_headObject


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

def travel_to_head
  stash_successful = stash_changes
  yield
ensure
  travel_to_original_state if stash_successful
end