Class: Piston::Svn::WorkingCopy

Inherits:
WorkingCopy show all
Defined in:
lib/piston/svn/working_copy.rb

Instance Attribute Summary

Attributes inherited from WorkingCopy

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WorkingCopy

add_handler, #copy_from, #copy_to, #diff, guess, handlers, #import, #info, #initialize, #logger, logger, #pistonized?, #recall, #remember, #remotely_modified, #repository, #temp_dir_name, #to_s, #update, #validate!

Constructor Details

This class inherits a constructor from Piston::WorkingCopy

Class Method Details

.clientObject



15
16
17
# File 'lib/piston/svn/working_copy.rb', line 15

def client
  @@client ||= Piston::Svn::Client.instance
end

.old_repositories(*directories) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/piston/svn/working_copy.rb', line 23

def old_repositories(*directories)
  repositories = []
  unless directories.empty?
    folders = svn(:propget, '--recursive', Piston::Svn::ROOT, *directories)
    folders.each_line do |line|
      next unless line =~ /^(.+) - \S+/
      logger.debug {"Found repository #{$1}"}
      repositories << $1
    end
  end
  repositories
end

.svn(*args) ⇒ Object



19
20
21
# File 'lib/piston/svn/working_copy.rb', line 19

def svn(*args)
  client.svn(*args)
end

.understands_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/piston/svn/working_copy.rb', line 10

def understands_dir?(dir)
  result = svn(:info, dir) rescue :failed
  result == :failed ? false : true
end

Instance Method Details

#add(added) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/piston/svn/working_copy.rb', line 78

def add(added)
  added.each do |item|
    target = path + item
    target.mkdir unless target.exist?
    svn(:add, '--parents', target)
  end
end

#after_remember(path) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/piston/svn/working_copy.rb', line 61

def after_remember(path)
  begin
    info = svn(:info, path)
  rescue Piston::Svn::Client::CommandError
  ensure
    return unless info =~ /\(not a versioned resource\)/i || info =~ /\(is not under version control\)/i || info.blank?
    svn(:add, path)
  end
end

#createObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/piston/svn/working_copy.rb', line 49

def create
  logger.debug {"Creating #{path}"}
  begin
    svn(:mkdir, path)
  rescue Piston::Svn::Client::CommandError
    logger.error do
      "Folder #{path} could not be created.  Is #{path.parent} a working copy? (Tip: svn mkdir it)"
    end
    raise
  end
end

#delete(deleted) ⇒ Object



86
87
88
89
90
# File 'lib/piston/svn/working_copy.rb', line 86

def delete(deleted)
  deleted.each do |item|
    svn(:rm, path + item)
  end
end

#downgrade_to(revision) ⇒ Object



98
99
100
101
# File 'lib/piston/svn/working_copy.rb', line 98

def downgrade_to(revision)
  logger.debug {"Downgrading to revision when last update was made"}
  svn(:update, '--revision', revision, path)
end

#exclude_for_diffObject



157
158
159
# File 'lib/piston/svn/working_copy.rb', line 157

def exclude_for_diff
  Piston::Svn::EXCLUDE
end

#exist?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/piston/svn/working_copy.rb', line 41

def exist?
  result = svn(:info, path) rescue :failed
  logger.debug {"result: #{result.inspect}"}
  return false if result == :failed
  return false if result.nil? || result.chomp.strip.empty?
  return true if YAML.load(result).has_key?("Path")
end

#externalsObject

Returns all defined externals (recursively) of this WC. Returns a Hash:

{"vendor/rails" => {:revision => :head, :url => "http://dev.rubyonrails.org/svn/rails/trunk"},
 "vendor/plugins/will_paginate" => {:revision => 1234, :url => "http://will_paginate.org/svn/trunk"}}


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/piston/svn/working_copy.rb', line 119

def externals
  externals = svn(:proplist, "--recursive", "--verbose")
  return Hash.new if externals.blank?
  returning(Hash.new) do |result|
    YAML.load(externals).each_pair do |dir, props|
      next if props["svn:externals"].blank?
      next unless dir =~ /Properties on '([^']+)'/
      basedir = self.path + $1
      exts = props["svn:externals"]
      exts.split("\n").each do |external|
        data = external.match(/^([^\s]+)\s+(?:(?:-r|--revision)\s*(\d+)\s+)?(.+)$/)
        case data.length
        when 4
          subdir, rev, url = data[1], data[2].nil? ? :head : data[2].to_i, data[3]
        else
          raise SyntaxError, "Could not parse svn:externals on #{basedir}: #{external}"
        end

        result[basedir + subdir] = {:revision => rev, :url => url}
      end
    end
  end
end

#finalizeObject



71
72
73
74
75
76
# File 'lib/piston/svn/working_copy.rb', line 71

def finalize
  targets = []
  Dir[(path + "*").to_s].each do |item|
    svn(:add, item)
  end
end

#locally_modifiedObject



147
148
149
150
151
152
153
154
155
# File 'lib/piston/svn/working_copy.rb', line 147

def locally_modified
  logger.debug {"Get last changed revision for #{yaml_path}"}
  # get latest revision for .piston.yml
  initial_revision = last_changed_revision(yaml_path)
  logger.debug {"Get last log line for #{path} after #{initial_revision}"}
  # get latest revisions for this working copy since last update
  log = svn(:log, '--revision', "#{initial_revision}:HEAD", '--quiet', '--limit', '2', path)
  log.count("\n") > 3
end

#merge_local_changes(revision) ⇒ Object



103
104
105
106
# File 'lib/piston/svn/working_copy.rb', line 103

def merge_local_changes(revision)
  logger.debug {"Update to #{revision} in order to merge local changes"}
  svn(:update, path)
end

#remove_external_references(*targets) ⇒ Object



143
144
145
# File 'lib/piston/svn/working_copy.rb', line 143

def remove_external_references(*targets)
  svn(:propdel, "svn:externals", *targets)
end

#rename(renamed) ⇒ Object



92
93
94
95
96
# File 'lib/piston/svn/working_copy.rb', line 92

def rename(renamed)
  renamed.each do |from, to|
    svn(:mv, '--parents', path + from, path + to)
  end
end

#status(subpath = nil) ⇒ Object



108
109
110
111
112
113
# File 'lib/piston/svn/working_copy.rb', line 108

def status(subpath=nil)
  svn(:status, path + subpath.to_s).split("\n").inject([]) do |memo, line|
    next memo unless line =~ /^\w.+\s(.*)$/
    memo << [$1, $2]
  end
end

#svn(*args) ⇒ Object



37
38
39
# File 'lib/piston/svn/working_copy.rb', line 37

def svn(*args)
  self.class.svn(*args)
end

#upgradeObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/piston/svn/working_copy.rb', line 161

def upgrade
  props = Hash.new
  svn(:proplist, '--verbose', path).each_line do |line|
    if line =~ /(piston:[-\w]+)\s*:\s*(.*)$/
      props[$1] = $2
      svn(:propdel, $1, path)
    end
  end
  remember({:repository_url => props[Piston::Svn::ROOT], :lock => props[Piston::Svn::LOCKED] || false, :repository_class => Piston::Svn::Repository.name}, {Piston::Svn::REMOTE_REV => props[Piston::Svn::REMOTE_REV], Piston::Svn::UUID => props[Piston::Svn::UUID]})
end