Class: Externals::SvnProject

Inherits:
Project
  • Object
show all
Defined in:
lib/externals/scms/svn_project.rb

Instance Attribute Summary

Attributes inherited from Project

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Project

#assert_e_dne_i_ni, attr_attr_accessor, #attributes, #checkout, default_branch, #export, #extract_name, inherited, #initialize, #main_project?, #name, #scm, scm, #scm_opts, #scm_opts=, #update_ignore

Constructor Details

This class inherits a constructor from Externals::Project

Class Method Details

.add_allObject

this is a test helper method



147
148
149
150
151
152
153
154
# File 'lib/externals/scms/svn_project.rb', line 147

def self.add_all
  status = `svn st`

  status.split("\n").grep(/^\?/).each do |to_add|
    puts `svn add #{to_add.gsub(/^\?\s*/,"")}`
    raise unless $? == 0
  end
end

.detected?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/externals/scms/svn_project.rb', line 142

def self.detected?
  File.exist?(".svn")
end

.extract_repository(url, branch) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/externals/scms/svn_project.rb', line 175

def self.extract_repository url, branch
  repository = url.gsub(branch, "")
  if url == repository
    raise "Could not determine repository from URL #{info_url}.
Does not appear to have the branch #{branch} as a substring"
  end
  if repository !~ /\/$/
    raise "Was expecting the branch and repository to be separated by '/'
  Please file an issue about this at http://github.com/azimux/externals"
  end
  repository.gsub(/\/$/, "")
end

.fill_in_opts(opts, main_options, sub_options, options = {}) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/externals/scms/svn_project.rb', line 134

def self.fill_in_opts opts, main_options, sub_options, options = {}
  opts.on("--svn", "--subversion",
    Integer,
    *"same as '--scm svn'  Uses subversion to
    checkout/export the main project".lines_by_width(options[:summary_width])
    ) {sub_options[:scm] = main_options[:scm] = 'svn'}
end

.info_url(scm_opts = "") ⇒ Object



269
270
271
272
273
274
275
# File 'lib/externals/scms/svn_project.rb', line 269

def self.info_url scm_opts = ""
  if `svn #{scm_opts} info` =~ /^\s*URL:\s*([^\s]+)\s*$/
    $1
  else
    raise "Could not get URL from svn info"
  end
end

.scm_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/externals/scms/svn_project.rb', line 118

def self.scm_path? path
  return true if path =~ /^svn(\+ssh)?:/

  # Look for http(s)://svn.*/*
  if path =~ /^https?:\/\/([\w+\-]+)\.(?:[\w+\-]+\.)*[\w\-]+(?:\/|$)/
    return true if $1.downcase == "svn"
  end

  # Look for http(s)://*/*svn*/
  if path =~ /^https?:\/\/(?:[\w+\-]+\.?)+\/(\w+)/
    return true if $1.downcase.include? "svn"
  end

  false
end

Instance Method Details

#append_ignore(path) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/externals/scms/svn_project.rb', line 209

def append_ignore path
  parent = File.dirname(path)
  child = File.basename(path)

  rows = ignore_rows(path)

  return if rows.detect {|row| row.strip == child.strip}

  rows << child.strip

  Dir.chdir(parent) do
    puts `svn #{scm_opts} propset svn:ignore "#{rows.compact.join("\n")}\n" .`
    raise "Could not ignore path, something went wrong in svn." unless $? == 0
  end
end

#change_to_revision(command = "") ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/externals/scms/svn_project.rb', line 40

def change_to_revision command = ""
  opts = resolve_opts(command)

  if revision
    Dir.chdir path do
      puts `svn #{opts} up -r #{revision}`
      raise unless $? == 0
    end
  end
end

#co(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/externals/scms/svn_project.rb', line 10

def co *args
  # delete path if empty
  rmdir_ie path unless path == "."

  dest = path
  dest = '' if dest == '.'
  dest = "\"#{dest}\"" if dest && !dest.empty?

  if File.exist?(dest)
    up
  else
    opts = resolve_opts "co"

    url = repository

    if branch
      require_repository
      url = [url, branch].join("/")
    end

    puts(svncocmd = "svn #{opts} co #{url} #{dest}")
    puts `#{svncocmd}`
    unless $? == 0
      raise
    end

    change_to_revision "co"
  end
end

#current_branchObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/externals/scms/svn_project.rb', line 160

def current_branch
  require_repository

  branch = info_url.downcase.gsub(/\/+/, "/").gsub(repository.downcase.gsub(/\/+/, "/"), "")
  if branch == repository
    raise "Could not determine branch from URL #{info_url}.
Does not appear have a substring of #{repository}"
  end
  if branch !~ /^\//
    raise "Was expecting the branch and repository to be separated by '/'
  Please file an issue about this at http://github.com/azimux/externals"
  end
  branch.gsub(/^\//, "")
end

#current_revisionObject



261
262
263
264
265
266
267
# File 'lib/externals/scms/svn_project.rb', line 261

def current_revision
  Dir.chdir path do
    if `svn #{scm_opts} info` =~ /Revision:\s*(\d+)\s*$/
      $1
    end
  end
end

#default_branchObject



5
6
7
# File 'lib/externals/scms/svn_project.rb', line 5

def default_branch
  raise "There is no default_branch for SvnProject"
end

#drop_from_ignore(path) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/externals/scms/svn_project.rb', line 225

def drop_from_ignore path
  parent = File.dirname(path)
  child = File.basename(path).strip

  ir = ignore_rows(path)
  rows = ir.select {|row| row.strip != child}

  if rows.size == ir.size
    raise "row not found matching #{path} in svn propget svn:ignore"
  end

  if ir.size - rows.size != 1
    raise "More than one row found matching #{path} in svn propget svn:ignore"
  end

  Dir.chdir(parent) do
    puts `svn #{scm_opts} propset svn:ignore "#{rows.compact.join("\n")}\n" .`
  end
end

#ex(*args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/externals/scms/svn_project.rb', line 51

def ex *args
  # delete path if  empty
  rmdir_ie path unless path == "."

  dest = path
  dest = '' if dest == '.'
  dest = "\"#{dest}\"" if dest && !dest.empty?

  url = repository

  if branch
    require_repository
    url = [url, branch].join("/")
  end

  if revision
    url += "@#{revision}"
  end

  puts(svncocmd = "svn #{scm_opts_ex} export #{url} #{dest}")
  puts `#{svncocmd}`
end

#ignore_contains?(path) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/externals/scms/svn_project.rb', line 156

def ignore_contains? path
  ignore_text(path) =~ Regexp.new("^\\s*#{File.basename(path)}\\s*$")
end

#ignore_rows(path) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/externals/scms/svn_project.rb', line 245

def ignore_rows(path)
  rows = ignore_text(path).split(/\n/)

  rows.delete_if {|row| row =~ /^\s*$/}

  rows
end

#ignore_text(path) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/externals/scms/svn_project.rb', line 253

def ignore_text(path)
  ignore_text = ''
  Dir.chdir File.dirname(path) do
    ignore_text = `svn #{scm_opts} propget svn:ignore`
  end
  ignore_text
end

#info_urlObject



277
278
279
280
281
# File 'lib/externals/scms/svn_project.rb', line 277

def info_url
  Dir.chdir path do
    self.class.info_url scm_opts
  end
end

#require_repositoryObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/externals/scms/svn_project.rb', line 188

def require_repository
  if repository.nil? || repository.empty?
    url = info_url
    info_url = "svn+ssh://server/path/repository" unless url
    puts "to use any branching features with a subversion project, the
repository must be present in the .externals file.

See http://nopugs.com/ext-svn-branches for more info

The name of the branch should be excluded from the repository URL.

You might need to change your .externals file to contain something like this:

[.]
scm = svn
repository = #{info_url}
    "
    raise "Cannot use subversion branching features without a repository in .externals file"
  end
end

#st(*args) ⇒ Object



111
112
113
114
115
116
# File 'lib/externals/scms/svn_project.rb', line 111

def st *args
  puts "\nstatus for #{path}:"
  Dir.chdir path do
    puts `svn #{scm_opts_st} status`
  end
end

#switch(branch_name, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/externals/scms/svn_project.rb', line 74

def switch branch_name, options = {}
  require_repository

  if current_branch != branch_name
    Dir.chdir path do
      url = [repository, branch_name].join("/")
      `svn #{scm_opts} switch #{url}`
      unless $? == 0
        raise "Could not switch to #{url}"
      end
    end
  end
end

#up(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/externals/scms/svn_project.rb', line 88

def up *args
  # delete path if empty
  rmdir_if_empty_ie path

  if File.exist?(path)
    puts "updating #{path}:"

    if branch
      switch branch
    end

    if revision
      change_to_revision "up"
    else
      Dir.chdir path do
        puts `svn #{scm_opts_up} up .`
      end
    end
  else
    co
  end
end