Class: Svn

Inherits:
Object
  • Object
show all
Defined in:
lib/version_control/svn.rb

Overview

Base class for working with Subversion

Instance Method Summary collapse

Constructor Details

#initialize(svn, params, options) ⇒ Svn

Initializes an instance of the class

Attributes

  • svn - path to the svn executable

  • params - params hash

  • options - hash of options includes:… url - url for svn repository… base_path - path for the local repository… username - repository user username… password - repository user password… verbose - true for verbose output (default = false)… rebase - starting path for checkouts… simulate - simulate command - echo it (default = false)… prerun_lines - pass any text to run before svn (such as env variables)… command_options - options to pass on command line e.g. –non-interactive



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/version_control/svn.rb', line 23

def initialize(svn, params, options)
  self.extend Utilities
  @url = required_option(options,"url")
  @base_path = required_option(options,"base_path")
  user = get_option(options,"username")
  @password = get_option(options,"password")
  @verbose = get_option(options,"verbose", false)
  @prerun = get_option(options, "prerun_lines")
  @command_options = get_option(options, "command_options")
  @command_options += " --non-interactive" unless @command_options.include?("non-interactive")
  @rebase = get_option(options, "rebase")
  @rebase =  @url.split("/")[-1] if @rebase == ""
  @simulate = get_option(options,"simulate", false)
  make_credential(user, @password)
  @svn = svn
end

Instance Method Details

#add_files(options = {}) ⇒ Object

Adds any new files in the local repo to the svn commit list

Attributes

  • exclude_regex - regex to filter add files (file !=~ filter)

Returns

  • command output



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/version_control/svn.rb', line 169

def add_files(options = {})
  result = status
  msg = ""
  exclude = get_option(options,"exclude_regex")
  result.split("\n").each do |item|
    if item.start_with?("?")
      file = item.gsub("?","").strip
      cmd = "#{@svn} add #{file}"
      if exclude == ""
        msg += process_cmd(cmd)
      else
        msg += process_cmd(cmd) if file =~ /#{exclude}/
      end
      res = `#{cmd}`
    end
  end
  "#{result}\n#{msg}"
end

#checkout(init = false) ⇒ Object

Performs an svn checkout

Attributes

  • init - true to initilize the checkout and local repo

Returns

  • command output



75
76
77
78
79
80
81
82
83
# File 'lib/version_control/svn.rb', line 75

def checkout(init = false)
  FileUtils.cd(@base_path, :verbose => true)
  if init
    cmd = "#{@svn} checkout #{@url} #{@rebase}  #{@credential} #{@command_options}"
  else
    cmd = "#{@svn} checkout  #{@command_options}"
  end
  process_cmd(cmd)
end

#commit(message = "Automation pushed changes") ⇒ Object

Performs an svn commit

Attributes

  • message - commit string

Returns

  • command output



126
127
128
129
130
# File 'lib/version_control/svn.rb', line 126

def commit(message = "Automation pushed changes")
  # /usr/bin/svn commit . -m "PSENG-0000 Adding PSENG files"
  cmd = "#{@svn} commit . -m \"#{message}\""
  process_cmd(cmd)
end

#export(target = "", revision = "") ⇒ Object

Performs an svn export

Attributes

  • revision - revision to export (options - defaults to latest)

Returns

  • command output



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/version_control/svn.rb', line 93

def export(target = "", revision = "")
  url_items = URI.parse(@url)
  target = url_items.path if target == ""    
  cmd_options = @command_options
  cmd_options += " --no-auth-cache --trust-server-cert --force" if cmd_options == " --non-interactive"
  base_cmd = "#{@svn} export #{@credential} #{@command_options} #{@url}"
  FileUtils.cd(@base_path, :verbose => true)
  if revision == ""
    cmd = "#{base_cmd} ."
  else
    cmd = "#{base_cmd} -r #{revision} ."
  end
  process_cmd(cmd)
end

#getObject

Performs an svn checkout

Returns

  • command output



113
114
115
116
# File 'lib/version_control/svn.rb', line 113

def get
  cmd = "#{@svn} checkout --non-interactive"
  process_cmd(cmd)
end

#parse_uri(svn_uri, reset_values = false) ⇒ Object

Parses a complex svn uri into parts

Attributes

  • svn_url - svn url, like this:…

user:password@host:port/path/path[#revision]user:[email protected]:9050/svn/16667/appreldep/RLM/artifacts/CATE[7777777]

  • reset_values - resets the svn object parameters host, password etc from url (default=false)

Returns

  • parse result, like this:…

=> URIGemResult, “revision” => “”



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/version_control/svn.rb', line 53

def parse_uri(svn_uri, reset_values = false)
  result = {"uri_result" => nil, "revision" => ""}
  k = svn_uri.scan(/\[.*\]/)
  result["revision"] = k[0].gsub("[","").gsub("]","") if k.size > 0
  rev = k.size > 0 ? "[#{result["revision"]}]" : "__ZZZ__"
  parts = URI.parse(svn_uri.gsub(rev,""))
  result["uri_result"] = parts
  if reset_values
    @url = "#{parts.scheme}://#{parts.host}:#{parts.port}#{parts.path}"
    make_credential(parts.user, parts.password) unless parts.password.nil?
  end
  result
end

#statusObject

Performs an svn status

Returns

  • command output



156
157
158
# File 'lib/version_control/svn.rb', line 156

def status
  process_cmd("#{@svn} status")
end

#tag(source_path, tag_name, message) ⇒ Object

Performs an svn tag

Attributes

  • source_path - path in repo to tag

  • tag_name - name for tag

  • message - message to add to tag

Returns

  • command output



143
144
145
146
147
148
149
# File 'lib/version_control/svn.rb', line 143

def tag(source_path, tag_name, message)
  ipos = @url.index("trunk")
  raise "Cannot locate trunk path" if ipos.nil?
  tag_path = @url[0..ipos] + "tags/" + tag_name
  cmd = "#{@svn} copy #{source_path} #{tag_path} -m \"#{message}\""
  process_cmd(cmd)
end