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


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

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)

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

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


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

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


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

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)

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

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


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

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


99
100
101
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 99

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


103
104
105
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 103

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

.p4_installed?Boolean

Returns:

  • (Boolean)

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

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)

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

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

.to_sObject


41
42
43
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 41

def self.to_s
  'Perforce'
end

Instance Method Details

#date_of_last_commit(path) ⇒ Object


49
50
51
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 49

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

#head_referenceObject


57
58
59
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 57

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

#revision?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 53

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

#revisions_count(path) ⇒ Object


45
46
47
# File 'lib/rubycritic/source_control_systems/perforce.rb', line 45

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