Class: RubyCritic::SourceControlSystem::Perforce

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connected_system_names, create, register_system, systems

Class Method Details

.build_file_cacheObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 63

def self.build_file_cache
  # Chun is very slow if files stats are requested one by one
  # this fill a hash with the result of all ruby file in the current directory (and sub-directories)
  {}.tap do |file_cache|
    line_aggregator = []
    `p4 fstat #{PERFORCE_FSTAT_OPTS} #{Dir.getwd}...`.each_line do |line|
      Perforce.compute_line(file_cache, line_aggregator, line)
    end
    # handle remaining lines
    Perforce.insert_file_cache(file_cache, line_aggregator) if line_aggregator.any?
  end
end

.child?(root, target) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 34

def self.child?(root, target)
  root_size = root.size
  target_size = target.size
  return false if target_size < root_size

  target[0...root_size] == root &&
    (target_size == root_size || [File::ALT_SEPARATOR, File::SEPARATOR].include?(target[root_size]))
end

.compute_cache_lines(lines) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 90

def self.compute_cache_lines(lines)
  perforce_lines = Hash[*lines.map { |line| line.split[1..-1] }.flatten]
  PerforceStats.new(
    Perforce.normalized_file_path(perforce_lines['clientFile']),
    perforce_lines['headRev'],
    perforce_lines['headTime'],
    perforce_lines.key?('action'),
    perforce_lines['headChange']
  )
end

.compute_line(file_cache, line_aggregator, line) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 76

def self.compute_line(file_cache, line_aggregator, line)
  if line.chomp.empty?
    Perforce.insert_file_cache(file_cache, line_aggregator)
  else
    line_aggregator << line
  end
end

.in_client_directory?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 25

def self.in_client_directory?
  p4_info = `p4 info`.each_line.select do |line|
    line.start_with?('Client root', 'Current directory')
  end

  client_directory, current_directory = p4_info.map! { |info| info.split(': ').last.chomp }
  child?(client_directory, current_directory)
end

.insert_file_cache(file_cache, lines) ⇒ Object



84
85
86
87
88
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 84

def self.insert_file_cache(file_cache, lines)
  perforce_stat = Perforce.compute_cache_lines(lines)
  file_cache[perforce_stat.filename] = perforce_stat
  lines.clear
end

.key_file(source_file_path) ⇒ Object



101
102
103
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 101

def self.key_file(source_file_path)
  Perforce.normalized_file_path(File.join(Dir.getwd, source_file_path))
end

.normalized_file_path(file_path) ⇒ Object



105
106
107
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 105

def self.normalized_file_path(file_path)
  file_path.downcase.tr('\\', '/')
end

.p4_installed?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 18

def self.p4_installed?
  p4_exe = Gem.win_platform? ? 'p4.exe' : 'p4'
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, p4_exe))
  end
end

.supported?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 13

def self.supported?
  p4_client = ENV['P4CLIENT'] || ''
  p4_installed? && !p4_client.empty? && in_client_directory?
end

.to_sObject



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

def self.to_s
  'Perforce'
end

Instance Method Details

#date_of_last_commit(path) ⇒ Object



51
52
53
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 51

def date_of_last_commit(path)
  Time.strptime(perforce_files[Perforce.key_file(path)].last_commit, '%s').strftime('%Y-%m-%d %H:%M:%S %z')
end

#head_referenceObject



59
60
61
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 59

def head_reference
  perforce_files.values.map(&:head).max_by(&:to_i)
end

#revision?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 55

def revision?
  !perforce_files.values.count(&:opened?).zero?
end

#revisions_count(path) ⇒ Object



47
48
49
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 47

def revisions_count(path)
  perforce_files[Perforce.key_file(path)].revision.to_i
end