Class: RubyCritic::SourceControlSystem::Git

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

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



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

def self.current_branch
  branch_list = `git branch`
  branch_list.match(/\*.*$/)[0].gsub('* ', '')
end

.git(arg) ⇒ Object



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

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

.modified_filesObject



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

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)


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

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

.switch_branch(branch) ⇒ Object



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

def self.switch_branch(branch)
  uncommitted_changes? ? abort('Uncommitted changes are present.') : `git checkout #{branch}`
end

.to_sObject



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

def self.to_s
  'Git'
end

.uncommitted_changes?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubycritic/source_control_systems/git.rb', line 58

def self.uncommitted_changes?
  !`git diff-index HEAD --`.empty?
end

Instance Method Details

#date_of_last_commit(path) ⇒ Object



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

def date_of_last_commit(path)
  git("log -1 --date=iso --format=%ad #{path.shellescape}").chomp!
end

#git(arg) ⇒ Object



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

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

#head_referenceObject



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

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

#revision?Boolean

Returns:

  • (Boolean)


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

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

#revisions_count(path) ⇒ Object



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

def revisions_count(path)
  git("log --follow --format=%h #{path.shellescape}").count("\n")
end

#travel_to_headObject



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

def travel_to_head
  stash_successful = stash_changes
  yield
ensure
  travel_to_original_state if stash_successful
end