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

Parameters:

  • uris (Array<uri string like>)

    target uri

Returns:

  • (String)

    common part of the given uris



124
125
126
127
128
129
130
# File 'lib/svn_command_helper.rb', line 124

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

Parameters:

  • path (path string like)

    target path

Returns:

  • (String)

    file contents



110
111
112
# File 'lib/svn_command_helper.rb', line 110

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

#check_exists(transaction, raise_if_from_not_found = true) ⇒ Boolean

check transaction from file exists

Parameters:

  • transaction (SvnFileCopyTransaction)

    from and to info

  • raise_if_from_not_found (Boolean) (defaults to: true)

    raise if from not found

Returns:

  • (Boolean)

    true if file exists



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/svn_command_helper.rb', line 185

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

Parameters:

  • message (String)

    commit message

  • path (path string like) (defaults to: ".")

    target path



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

Parameters:



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/svn_command_helper.rb', line 160

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

Parameters:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/svn_command_helper.rb', line 135

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

#exist?(uri) ⇒ Boolean

check svn uri exists or not

Parameters:

  • uri (uri string like)

    target uri

Returns:

  • (Boolean)

    true if exists



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

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

Parameters:

  • uri (uri string like)

    target uri

Returns:

  • (Boolean)

    true if exists



69
70
71
72
# File 'lib/svn_command_helper.rb', line 69

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

Parameters:

  • path (path string like) (defaults to: ".")

    target path

Returns:

  • (Hash<String, String>)

    svn info contents



103
104
105
# File 'lib/svn_command_helper.rb', line 103

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

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

svn list

Parameters:

  • uri (uri string like)

    target uri

  • recursive (Boolean) (defaults to: false)

    –recursive

Returns:

  • (Array<String>)

    paths



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

Parameters:

  • uri (uri string like)

    target uri

  • recursive (Boolean) (defaults to: false)

    –recursive

Returns:

  • (Array<String>)

    file paths



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

Parameters:

  • uri (uri string like)

    target uri

Returns:

  • (Array<String>)

    file paths



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

Parameters:

  • uri (uri string like)

    target uri

Returns:

  • (Array<String>)

    paths



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

Parameters:

  • path (path string like) (defaults to: ".")

    target path

  • depth (depth) (defaults to: nil)

    –set-depth



77
78
79
# File 'lib/svn_command_helper.rb', line 77

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

Parameters:

  • path (path string like)

    target path

  • depth (depth) (defaults to: nil)

    –set-depth for only new updated dirs



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/svn_command_helper.rb', line 84

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

Parameters:

  • path (path string like) (defaults to: ".")

    target path

Returns:

  • (String)

    Working Copy Root Path



117
118
119
# File 'lib/svn_command_helper.rb', line 117

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