Class: Perforce2Svn::Subversion::SvnTransaction

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/perforce2svn/subversion/svn_transaction.rb

Constant Summary collapse

SVN_MIME_TYPE =
'svn:mime-type'
BINARY_MIME_TYPE =
'application/octet-stream'
BLOCK_SIZE =

TODO: Maybe make this configurable?

16384

Instance Method Summary collapse

Methods included from Logging

configure, log, #log

Constructor Details

#initialize(txn, root) ⇒ SvnTransaction

Returns a new instance of SvnTransaction.



14
15
16
17
18
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 14

def initialize(txn, root)
  @txn = txn
  @root = root
  @possible_deletions = Set.new
end

Instance Method Details

#copy(src_path, dest_path) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 75

def copy(src_path, dest_path)
  if exists? src_path
    @txn.root.copy(dest_path, @root, src_path)
  else
    log.warn "No path to copy from: #{src_path} -> #{dest_path}"
  end
end

#delete(path) ⇒ Object



88
89
90
91
92
93
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 88

def delete(path)
  if exists? path
    @txn.root.delete(path)
    @possible_deletions << parent_path(path)
  end
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 95

def exists?(path)
  @txn.root.check_path(path) != 0
end

#has_children?(path) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 99

def has_children?(path)
  @txn.root.dir_entries(path).keys.length != 0
end

#mkdir(path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 63

def mkdir(path)
  path_parts = path.split(/\//).delete_if {|p| p == ''}
  current_path = ''
  path_parts.each do |part|
    current_path += '/' + part
    if not exists? current_path
      @txn.root.make_dir(current_path)
      log.debug("SVN: Adding directory:\t#{current_path}")
    end
  end
end

#move(src_path, dest_path) ⇒ Object



83
84
85
86
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 83

def move(src_path, dest_path)
  copy(src_path, dest_path)
  delete(src_path)
end


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 20

def symlink(original_path, new_path)
  mkfile new_path

  # Apply the properties
  @txn.root.set_node_prop(new_path, 'svn:special', '*')
  stream = @txn.root.apply_text(new_path)
  log.debug("SVN: Writing symlink:\t#{new_path}")
  log.debug("SVN:      Linking to:\t#{original_path}")
  stream.write("link #{original_path}")
  stream.close
end

#update(path, content_stream, binary = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/perforce2svn/subversion/svn_transaction.rb', line 32

def update(path, content_stream, binary = false)
  if path.nil? or path == ''
    raise Perforce2Svn::SvnTransactionError, "The subversion path was empty"
  end

  if not content_stream.respond_to? :readpartial
    raise Perforce2Svn::SvnTransactionError, "The content stream must support block file reading with the 'readpartial' method"
  end

  mkfile path
  # If this is a binary file, set the mime-type
  if binary
    @txn.root.set_node_prop(path, SVN_MIME_TYPE, BINARY_MIME_TYPE)
  end

  # Open the output stream to write the contents to
  outstream = @txn.root.apply_text(path)
  log.debug("SVN: Writing file:\t#{path}")

  begin
    while buf = content_stream.readpartial(BLOCK_SIZE)
      outstream.write(buf)
    end
  rescue EOFError
    # Always throws an error at the end.  Very weird API.
  end

  outstream.close # close the output stream
  self
end