Module: SvnCommandHelper::Svn::ModuleMethods

Includes:
SystemCommandHelper
Included in:
SvnCommandHelper::Svn
Defined in:
lib/svn_command_helper.rb

Overview

module methods

Instance Method Summary collapse

Instance Method Details

#base_uri_of(uris) ⇒ String

find common part of the given uris


108
109
110
111
112
113
114
# File 'lib/svn_command_helper.rb', line 108

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


94
95
96
# File 'lib/svn_command_helper.rb', line 94

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

#check_exists(transaction, raise_if_from_not_found = true) ⇒ Boolean

check transaction from file exists


169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/svn_command_helper.rb', line 169

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


17
18
19
20
21
22
23
24
25
# File 'lib/svn_command_helper.rb', line 17

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

#copy_multi(transactions, message) ⇒ Object

copy multi transactionS


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/svn_command_helper.rb', line 144

def copy_multi(transactions, message)
  base_uri = base_uri_of(transactions.map(&:from_base) + trnsactions.map(&:to_base))
  transactions.each do |transaction|
    raise "copy_multi: #{transaction.from} not exists" unless transaction.from_exist?
  end
  Dir.tmpdir do |dir|
    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 merge --accept theirs-full #{transaction.from} #{relative_to}"
      else # toがない場合コピー
        sys "svn copy --parents #{transaction.from} #{relative_to}"
      end
    end
    Svn.commit(message, ".")
  end
end

#copy_single(transaction, message) ⇒ Object

copy single transaction


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

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).select 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|
      sys "svn checkout --depth empty #{transaction.to_base} ."
      sys "svn copy --parents #{only_from_transactions.map(&:from).join(' ')} ."
      to_exist_transactions.each do |_transaction|
        sys "svn update --set-depth infinity #{_transaction.file}"
        sys "svn merge --accept theirs-full #{_transaction.from} #{_transaction.file}"
      end
      Svn.commit(message, ".")
    end
  end
end

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

svn info -> yaml parse


87
88
89
# File 'lib/svn_command_helper.rb', line 87

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

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

svn list


31
32
33
34
# File 'lib/svn_command_helper.rb', line 31

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


47
48
49
# File 'lib/svn_command_helper.rb', line 47

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


54
55
56
# File 'lib/svn_command_helper.rb', line 54

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

#list_recursive(uri) ⇒ Array<String>

svn list –recursive


39
40
41
# File 'lib/svn_command_helper.rb', line 39

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

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

svn update


61
62
63
# File 'lib/svn_command_helper.rb', line 61

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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/svn_command_helper.rb', line 68

def update_deep(path, depth = nil)
  root = Pathname.new(Svn.working_copy_root_path(path)).realpath
  end_path = Pathname.new(path.to_s).expand_path.realpath
  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


101
102
103
# File 'lib/svn_command_helper.rb', line 101

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