Class: Turbulence::Scm::Perforce

Inherits:
Object
  • Object
show all
Defined in:
lib/turbulence/scm/perforce.rb

Class Method Summary collapse

Class Method Details

.changes(commit_range = "") ⇒ Object



29
30
31
32
33
# File 'lib/turbulence/scm/perforce.rb', line 29

def changes(commit_range = "")
  p4_list_changes.each_line.map do |change|
    change.match(/Change (\d+)/)[1]
  end
end

.depot_to_local(depot_file) ⇒ Object



35
36
37
38
# File 'lib/turbulence/scm/perforce.rb', line 35

def depot_to_local(depot_file)
  abs_path = extract_clientfile_from_fstat_of(depot_file)
  Pathname.new(abs_path).relative_path_from(Pathname.new(FileUtils.pwd)).to_s
end

.extract_clientfile_from_fstat_of(depot_file) ⇒ Object



40
41
42
43
44
# File 'lib/turbulence/scm/perforce.rb', line 40

def extract_clientfile_from_fstat_of(depot_file)
  p4_fstat(depot_file).each_line.select {
    |line| line =~ /clientFile/
  }[0].split(" ")[2].tr("\\","/")
end

.filename_from_describe(output, index) ⇒ Object



59
60
61
# File 'lib/turbulence/scm/perforce.rb', line 59

def filename_from_describe(output,index)
  depot_to_local(output[index].match(/==== (\/\/.*)#\d+/)[1])
end

.files_per_change(change) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/turbulence/scm/perforce.rb', line 46

def files_per_change(change)
  describe_output = p4_describe_change(change).split("\n")
  map = []
  describe_output.each_index do |index|
    if describe_output[index].start_with?("====")
      fn = filename_from_describe(describe_output, index)
      churn = sum_of_changes(describe_output[index .. index + 4].join("\n"))
      map << [churn,fn]
    end
  end
  return map
end

.has_p4?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/turbulence/scm/perforce.rb', line 23

def has_p4?
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, 'p4'))
  end
end

.is_repo?(directory) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/turbulence/scm/perforce.rb', line 18

def is_repo?(directory)
  p4client = ENV['P4CLIENT']
  return !((p4client.nil? or p4client.empty?) and not self.has_p4?)
end

.log_command(commit_range = "") ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/turbulence/scm/perforce.rb', line 8

def log_command(commit_range = "")
  full_log = ""
  changes.each do |cn|
    files_per_change(cn).each do |file|
      full_log << transform_for_output(file)
    end
  end
  return full_log
end

.p4_describe_change(change) ⇒ Object



84
85
86
# File 'lib/turbulence/scm/perforce.rb', line 84

def p4_describe_change(change)
  `p4 describe -ds #{change}`
end

.p4_fstat(depot_file) ⇒ Object



80
81
82
# File 'lib/turbulence/scm/perforce.rb', line 80

def p4_fstat(depot_file)
  `p4 fstat #{depot_file}`
end

.p4_list_changes(commit_range = "") ⇒ Object



76
77
78
# File 'lib/turbulence/scm/perforce.rb', line 76

def p4_list_changes(commit_range = "")
  `p4 changes -s submitted ...#{commit_range}`
end

.sum_of_changes(p4_describe_output) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/turbulence/scm/perforce.rb', line 67

def sum_of_changes(p4_describe_output)
  churn = 0
  p4_describe_output.each_line do |line|
    next unless line =~ /(add|deleted|changed) .* (\d+) lines/
    churn += line.match(/(\d+) lines/)[1].to_i
  end
  return churn
end

.transform_for_output(arr) ⇒ Object



63
64
65
# File 'lib/turbulence/scm/perforce.rb', line 63

def transform_for_output(arr)
  "#{arr[0]}\t0\t#{arr[1]}\n"
end