Class: Perforce2Svn::Perforce::PerforceFile

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/perforce2svn/perforce/perforce_file.rb

Overview

The collection of properties about a Perforce file

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure, log, #log

Constructor Details

#initialize(revision, src, dest, type, action) ⇒ PerforceFile

Returns a new instance of PerforceFile.



13
14
15
16
17
18
19
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 13

def initialize(revision, src, dest, type, action)
  @revision = revision
  @src = src
  @dest = dest
  @type = type
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



11
12
13
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 11

def action
  @action
end

#destObject (readonly)

Returns the value of attribute dest.



11
12
13
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 11

def dest
  @dest
end

#revisionObject (readonly)

Returns the value of attribute revision.



11
12
13
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 11

def revision
  @revision
end

#srcObject (readonly)

Returns the value of attribute src.



11
12
13
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 11

def src
  @src
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 11

def type
  @type
end

Instance Method Details

#binary?Boolean

Is this a binary file?

Returns:

  • (Boolean)


22
23
24
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 22

def binary?
  type =~ /binary/
end

#contentsObject

Pulls the file contents for a given path for the specific file revision



49
50
51
52
53
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 49

def contents
  streamed_contents do |stream|
    return stream.read
  end
end

#deleted?Boolean

Was it deleted in the last commit?

Returns:

  • (Boolean)


32
33
34
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 32

def deleted?
  action == 'delete'
end

#streamed_contents(&block) ⇒ Object

Pull a stream from a file at a specified file revision



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 56

def streamed_contents(&block)
  raise Perforce2Svn::P4Error, "Requires a block to pull the file stream" unless block_given?
  log.debug {  "PERFORCE: Reading file: #{@src}\##{@revision}" }

  tmpfile = File.join(Dir.tmpdir, ".p4file-#{rand}")
  begin
    P4Depot.instance.query do |p4|
      p4.run('print', '-o', tmpfile, '-q', "#{@src}\##{@revision}")
    end

    if !File.file? tmpfile
      raise Perforce2Svn::P4Error, "Unable to retrieve the file contents: #{src}\##{revision}"
    end

    mode = binary? ? 'rb' : 'r'
    File.open(tmpfile, mode) do |file|
      yield file
    end
  ensure
    if File.file? tmpfile
      File.delete(tmpfile)
    end
  end
end

#symlink?Boolean

Is this a symlink

Returns:

  • (Boolean)


27
28
29
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 27

def symlink?
  type =~ /symlink/
end

Retrieves the target of a symlink



41
42
43
44
45
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 41

def symlink_target
  p4query do |p4|
    return p4.run('print', '-q', "#{@src}\##{@revision}")[0].strip
  end
end

#to_sObject



36
37
38
# File 'lib/perforce2svn/perforce/perforce_file.rb', line 36

def to_s
  "(#{@action}:#{@type}\##{@revision})\t#{@src}"
end