Module: SvnCommandHelper::Svn

Extended by:
SystemCommandHelper
Defined in:
lib/svn_command_helper.rb

Overview

Subversion native command and some utilities

Class Method Summary collapse

Class Method Details

.base_uri_of(uris) ⇒ String

find common part of the given uris



204
205
206
207
208
209
210
# File 'lib/svn_command_helper.rb', line 204

def base_uri_of(uris)
  uris.reduce(Pathname.new(uris.first.to_s)) do |base_uri, uri|
    rel = Pathname.new(uri).relative_path_from(base_uri)
    to_parent = rel.to_s.match(/(?:\.\.\/)*/).to_s
    to_parent.empty? ? base_uri : base_uri + to_parent
  end.to_s
end

.cat(path) ⇒ String

svn cat



190
191
192
# File 'lib/svn_command_helper.rb', line 190

def cat(path)
  cap("svn cat #{path}")
end

.check_exists(transaction, raise_if_from_not_found = true) ⇒ Boolean

check transaction from file exists



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/svn_command_helper.rb', line 273

def check_exists(transaction, raise_if_from_not_found = true)
  unless transaction.from_exist?
    if !raise_if_from_not_found
      false
    elsif transaction.to_exist?
      puts "[WARNING] File:#{file}はコピー先のみにあります"
      false
    else
      raise "[Error] File:#{file}が見つかりません!"
    end
  else
    true
  end
end

.commit(message, path = ".") ⇒ Object

svn commit



21
22
23
24
25
26
27
28
29
# File 'lib/svn_command_helper.rb', line 21

def commit(message, path = ".")
  if cap("svn status #{path}").empty?
    sys "svn revert -R #{path}"
    puts "[WARNING] no change: #{message}"
  else
    sys "svn commit -m '#{message}' #{path}"
  end
  sys "svn update #{path}"
end

.copied_revision(uri = ".") ⇒ Object

stop-on-copy revision of uri return [Integer] revision number



112
113
114
# File 'lib/svn_command_helper.rb', line 112

def copied_revision(uri = ".")
  log(uri, stop_on_copy: true).first.revision
end

.copy_multi(transactions, message) ⇒ Object

copy multi transactions



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/svn_command_helper.rb', line 245

def copy_multi(transactions, message)
  base_uri = base_uri_of(transactions.map(&:from_base) + transactions.map(&:to_base))
  transactions.each do |transaction|
    raise "copy_multi: #{transaction.from} not exists" unless transaction.from_exist?
  end
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      sys "svn checkout --depth empty #{base_uri} ."
      transactions.each do |transaction|
        relative_to = transaction.relative_to(base_uri)
        Svn.update_deep(relative_to, "empty") # mkpath的な なくてもエラーにはならないので

        if transaction.to_exist?  # toがある場合マージ
          sys "svn export --force #{transaction.from} #{relative_to}"
          sys "svn add --force #{relative_to}"
        else # toがない場合コピー
          sys "svn copy --parents #{transaction.from} #{relative_to}"
        end
      end
      Svn.commit(message, ".")
    end
  end
end

.copy_single(transaction, message) ⇒ Object

copy single transaction



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/svn_command_helper.rb', line 215

def copy_single(transaction, message)
  transactions = transaction.glob_transactions
  raise "copy_single: #{transaction.from} not exists" if transactions.empty?
  to_exist_transactions = Svn.list_files(transaction.to_base).map do |_file|
    transactions.find {|_transaction| _transaction.file == _file}
  end.compact
  only_from_transactions = transactions - to_exist_transactions
  if to_exist_transactions.empty? # toにファイルがない
    sys "svn copy --parents #{only_from_transactions.map(&:from).join(' ')} #{transaction.to_base} -m '#{message}'"
  else
    Dir.mktmpdir do |dir|
      Dir.chdir(dir) do
        sys "svn checkout --depth empty #{transaction.to_base} ."
        unless only_from_transactions.empty?
          sys "svn copy --parents #{only_from_transactions.map(&:from).join(' ')} ."
        end
        sys "svn update --set-depth infinity #{to_exist_transactions.map(&:file).join(' ')}"
        to_exist_transactions.each do |_transaction|
          sys "svn export --force #{_transaction.from} #{_transaction.file}"
          sys "svn add --force #{_transaction.file}"
        end
        Svn.commit(message, ".")
      end
    end
  end
end

.exist?(uri) ⇒ Boolean

check svn uri exists or not



65
66
67
68
# File 'lib/svn_command_helper.rb', line 65

def exist?(uri)
  basename = File.basename(uri)
  list(File.dirname(uri)).find{|_basename| File.fnmatch(basename, _basename.sub(/\/$/, ''))}
end

.exist_file?(uri) ⇒ Boolean

check svn uri file exists or not



73
74
75
76
# File 'lib/svn_command_helper.rb', line 73

def exist_file?(uri)
  file = File.basename(uri)
  list_files(File.dirname(uri)).find{|_file| File.fnmatch(file, _file)}
end

.info(path = ".") ⇒ Hash<String, String>

svn info -> yaml parse



183
184
185
# File 'lib/svn_command_helper.rb', line 183

def info(path = ".")
  YAML.load(cap("svn info #{path}"))
end

.list(uri, recursive = false) ⇒ Array<String>

svn list



35
36
37
38
# File 'lib/svn_command_helper.rb', line 35

def list(uri, recursive = false)
  cap("svn list #{recursive ? '-R' : ''} #{uri}").split(/\n/).compact
    .reject {|path| path.empty?}
end

.list_files(uri, recursive = false) ⇒ Array<String>

svn list -> grep only files



51
52
53
# File 'lib/svn_command_helper.rb', line 51

def list_files(uri, recursive = false)
  list(uri, recursive).reject {|path| path.end_with?("/")} # dir
end

.list_files_recursive(uri) ⇒ Array<String>

svn list –recursive -> grep only files



58
59
60
# File 'lib/svn_command_helper.rb', line 58

def list_files_recursive(uri)
  list_files(uri, true)
end

.list_recursive(uri) ⇒ Array<String>

svn list –recursive



43
44
45
# File 'lib/svn_command_helper.rb', line 43

def list_recursive(uri)
  list(uri, true)
end

.log(uri = ".", limit: nil, stop_on_copy: false) ⇒ Array<OpenStruct>

svn log



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/svn_command_helper.rb', line 90

def log(uri = ".", limit: nil, stop_on_copy: false)
  log = cap "svn log --xml #{limit ? "--limit #{limit}" : ""} #{stop_on_copy ? "--stop-on-copy" : ""} #{uri}"
  REXML::Document.new(log).elements.collect("/log/logentry") do |entry|
    OpenStruct.new({
      revision: entry.attribute("revision").value.to_i,
      author: entry.elements["author"].text,
      date: Time.iso8601(entry.elements["date"].text),
      msg: entry.elements["msg"].text,
    })
  end.reverse
end

.merge1(start_rev, end_rev, from_uri, to_path = ".") ⇒ Object

svn merge -r start_rev:end_rev from_uri to_path



144
145
146
# File 'lib/svn_command_helper.rb', line 144

def merge1(start_rev, end_rev, from_uri, to_path = ".")
  safe_merge "svn merge -r #{start_rev}:#{end_rev} #{from_uri} #{to_path}"
end

.merge_branch_to_trunk(from_uri, to_path = ".") ⇒ Object

svn merge branch to trunk with detecting revision range



162
163
164
165
166
# File 'lib/svn_command_helper.rb', line 162

def merge_branch_to_trunk(from_uri, to_path = ".")
  start_rev = copied_revision(from_uri)
  end_rev = revision(from_uri)
  merge1(start_rev, end_rev, from_uri, to_path)
end

.reverse_merge(start_rev, end_rev = nil, path = ".") ⇒ Object

reverse merge single revision



172
173
174
175
176
177
178
# File 'lib/svn_command_helper.rb', line 172

def reverse_merge(start_rev, end_rev = nil, path = ".")
  if end_rev
    safe_merge "svn merge -r #{end_rev}:#{start_rev} #{path}"
  else
    safe_merge "svn merge -c #{start_rev} #{path}"
  end
end

.revision(uri = ".") ⇒ Object

head revision of uri return [Integer] revision number



105
106
107
# File 'lib/svn_command_helper.rb', line 105

def revision(uri = ".")
  log(uri, limit: 1).last.revision
end

.safe_merge(command) ⇒ Object

merge after dry-run conflict check



150
151
152
153
154
155
156
157
# File 'lib/svn_command_helper.rb', line 150

def safe_merge(command)
  dry_run = cap("#{command} --dry-run")
  if dry_run.each_line.any? {|line| line.start_with?("C")}
    raise "[ERROR] merge_branch_to_trunk: `#{command}` has conflict!\n#{dry_run}"
  else
    sys command
  end
end

.update(path = ".", depth = nil) ⇒ Object

svn update



81
82
83
# File 'lib/svn_command_helper.rb', line 81

def update(path = ".", depth = nil)
  sys "svn update #{depth ? "--set-depth #{depth}" : ""} #{path}"
end

.update_deep(path, depth = nil) ⇒ Object

svn update to deep path recursive



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/svn_command_helper.rb', line 119

def update_deep(path, depth = nil)
  exist_path = path
  until File.exist?(exist_path)
    exist_path = File.dirname(exist_path)
  end
  root = Pathname.new(Svn.working_copy_root_path(exist_path))
  end_path = Pathname.new(path.to_s).expand_path
  parents = [end_path]
  while parents.first != root
    parents.unshift(parents.first.parent)
  end
  parents.each do |dir|
    if dir.exist?
      sys "svn update #{dir}"
    else
      sys "svn update #{depth ? "--set-depth #{depth}" : ""} #{dir}"
    end
  end
end

.working_copy_root_path(path = ".") ⇒ String

Working Copy Root Path from svn info



197
198
199
# File 'lib/svn_command_helper.rb', line 197

def working_copy_root_path(path = ".")
  info(path)["Working Copy Root Path"]
end