Module: SvnRepoClient

Included in:
Object, SvnWcBroker
Defined in:
lib/svn_wc_client.rb

Overview

receives method/action requests from web app (AJAX) returns data in expected format is just a ‘simple’ client of ruby gem lib ‘svn_wc’

returned data is generally a array of anonymous hashes containing svn entries info, or errors.

e.g.

each_details           = {}
each_details[:file]    = path
each_details[:status]  = status
each_details[:content] = content
each_details[:entries] = entries_list
each_details[:error]   = error
each_details[:repo_root_local_path]  = File.join(@repo_root, '/')

Constant Summary collapse

STATUS_SPACER =

swt.js (js/jqtree file uses this)

"\t"
@@svn_wc =
SvnWc::RepoAccess.new

Instance Method Summary collapse

Instance Method Details

#_collect_and_group(file_info_list) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/svn_wc_client.rb', line 278

def _collect_and_group(file_info_list)

  entries_group_by_dir = {}
  file_info_list.each do |el|
    en = el[:entry_name]
    if el[:kind] == 2
      entries_group_by_dir[en] = [] unless entries_group_by_dir[en]
      entries_group_by_dir[en].push en
    elsif el[:kind] == 1
      entries_group_by_dir[File.dirname(en)] = [] unless entries_group_by_dir[File.dirname(en)] 
      entries_group_by_dir[File.dirname(en)].push "#{en}:#{el[:status]}"
    end
  end

  entries_group_by_dir
end

#_to_expected_json_format(file_info_list) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/svn_wc_client.rb', line 295

def _to_expected_json_format(file_info_list)
  require 'enumerator' # to_enum :each_with_index

  entries_group_by_dir = _collect_and_group(file_info_list)

  entries_formatted = {}
  entries_formatted[:children] = []
  atts = {}
  atts[:id] = ''
  c_entries = {}
  c_entries[:attributes] = atts
  c_entries[:data] = ''
  entries = []
  id = 0
  entries_group_by_dir.each do |k|
    entries_formatted[:state] = 'open'
    entries_formatted[:data] = k[0]
    entries_formatted[:children] = k[1].to_enum(:each_with_index).collect{|x,i|
      next unless x and x.split(':')[1]
      {:attributes => {:id => i}, :data => x.split(':')[1] + STATUS_SPACER + x.split(':')[0]}
    }
    entries.push entries_formatted
    entries_formatted = {}
    c_entries = {}
  end

  entries
  
end

#get_repoObject

if conf file exists and have a working copy of the repo, return the path to it, otherwise create it (do a checkout, not forced) set force_checkout = true to force a checkout returns svn_repo_working_copy abs_path



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/svn_wc_client.rb', line 47

def get_repo
  if File.file? @conf_file
    @@svn_wc.set_conf @conf_file
  else raise ArgumentError, "config file not found! #{@conf_file}" end

  #if not File.directory? @@svn_wc.svn_repo_working_copy
  begin
    if not File.directory? @@svn_wc.svn_repo_working_copy \
       or @@svn_wc.force_checkout
      #@@svn_wc.do_checkout(true, @@svn_wc.force_checkout)
      @@svn_wc.do_checkout(true)
    end
  rescue SvnWc::RepoAccessError => e
     raise e.message
  end
  @repo_root = @@svn_wc.svn_repo_working_copy
end

#info_dataObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/svn_wc_client.rb', line 325

def info_data
  each_details           = Hash.new
  each_details[:file]    = @path
  each_details[:status]  = @status
  each_details[:content] = @content
  #each_details[:rev]     = rev
  # entries_list expects an array of anon hash's
  each_details[:entries] = @entries_list
  each_details[:error]   = @error
  each_details[:repo_root_local_path]  = File.join(@repo_root, '/')

  # high level repo details
  each_details[:svn_repo_master] = @@svn_wc.svn_repo_master
  each_details[:svn_user]        = @@svn_wc.svn_user
  #each_details[:svn_pass]        = @@svn_wc.svn_pass
  each_details[:svn_repo_config_file] = @@svn_wc.svn_repo_config_file

  each_details
end

#repo_rootObject

getter for repo root



41
# File 'lib/svn_wc_client.rb', line 41

def repo_root ; @@svn_wc.svn_repo_working_copy ; end

#svn_addObject

add, returns ‘added’ message and added file list



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/svn_wc_client.rb', line 146

def svn_add
    get_repo
    rev = Array.new
    begin
      @content = "Added. #{@@svn_wc.add(@files).to_a.join("\n")}"
      rev.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      rev.push info_data
    end
    rev
end

#svn_commitObject

commit, return message and revision, committed file list



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/svn_wc_client.rb', line 130

def svn_commit
    get_repo
    rev = Array.new
    begin
      @content = "Committed. Revision: #{@@svn_wc.commit(@files)}
                                Files:
                                #{@files.join("\n")}"
      rev.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      rev.push info_data
    end
    rev
end

#svn_deleteObject

delete



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/svn_wc_client.rb', line 174

def svn_delete
    get_repo
    rev = Array.new
    begin
      @content = "Deleted. #{@@svn_wc.delete(@files).to_a.join("\n")}"
      rev.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      rev.push info_data
    end
    rev
end

#svn_diffObject

diff current to previous (HEAD only) returns diff content



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/svn_wc_client.rb', line 110

def svn_diff
   file_list_diffs = Array.new
   @files.each { |f_list_str|
     f_stat, f_name = f_list_str.split(/\s/)
     begin
       get_repo
       @path = f_name
       @content = @@svn_wc.diff(f_name).to_s
       @status = f_stat
       file_list_diffs.push info_data
     rescue SvnWc::RepoAccessError => e
       @error = e.message
       file_list_diffs.push info_data
     end
   }

   file_list_diffs
end

#svn_ignoreObject

ignore, returns ‘added’ message and added file list



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/svn_wc_client.rb', line 219

def svn_ignore
    get_repo
    rev = Array.new
    begin
      cmd = @@svn_wc.propset('ignore', @files, @repo_root)
      #@content = "Ignoring. #{cmd} #{@files.to_a.join("\n")}"
      @content = "Ignoring. #{cmd}"
      rev.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      rev.push info_data
    end
    rev
end

#svn_infoObject

info, returns ‘updated’ message, revision and update data



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/svn_wc_client.rb', line 188

def svn_info
    get_repo
    infos = Array.new
    begin
      @content = "Info: #{@@svn_wc.info(@files)[:url]}"
      infos.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      infos.push info_data
    end
    infos
end

#svn_list(f_regex = nil, f_amt = nil, dir = nil) ⇒ Object

recursively list entries, provides file or dir info and access to nice repo/file info



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/svn_wc_client.rb', line 237

def svn_list(f_regex=nil, f_amt=nil, dir=nil)
    get_repo
    repo_entries = Array.new
    dir = @repo_root unless dir and not dir.empty?
    begin
      l_svn_list = Array.new
      @@svn_wc.list(dir).each { |el|
        status_info = {}
        #fqpn = File.join(@repo_root, el[:entry])
        fqpn = File.join(dir, el[:entry])
        #status_info[:last_changed_rev] = el[:last_changed_rev]
        status_info[:entry_name] = fqpn
        status_info[:status] = ' '
        if File.directory? fqpn
          status_info[:kind] = 2 # is dir
        else
          status_info[:kind] = 1 # is 'file'
        end
        #apply filter - limit result set with filter
        #f_regex, f_amt = 'cof/htdocs/template', 10
        if f_regex.nil?
         l_svn_list.push status_info
        elsif el[:entry].match(f_regex)
         l_svn_list.push status_info
        end
        #l_svn_list.push status_info
        if f_amt.nil? or f_amt.empty?
        elsif l_svn_list.size > f_amt.to_i
          break
        end
      }
      #@entries_list = l_svn_list
      @entries_list = _to_expected_json_format(l_svn_list)
      repo_entries.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      repo_entries.push info_data
    end
    repo_entries
end

#svn_revertObject

revert, returns message



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/svn_wc_client.rb', line 202

def svn_revert
    get_repo
    infos = Array.new
    begin
      @content = "Reverted: #{@@svn_wc.revert(@files)}
                                Files:
                                #{@files.join("\n")}"
      infos.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      infos.push info_data
    end
    infos
end

#svn_status(f_regex = nil, f_amt = nil, dir = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/svn_wc_client.rb', line 65

def svn_status(f_regex=nil, f_amt=nil, dir=nil)
    get_repo
    repo_entries = Array.new
    dir = @repo_root unless dir and not dir.empty?
    begin
      l_svn_list = Array.new
      @@svn_wc.status(dir).each { |el|
        status_info = {}
        #status_info[:last_changed_rev] = el[:last_changed_rev]
        status_info[:status] = el[:status]
        status_info[:error] = ''
        status_info[:entry_name] = el[:path]
        #if File.directory?(File.join(@repo_root, el[:path]))
        if File.directory?(File.join(dir, el[:path]))
          status_info[:kind] = 2 # is dir
        else
          status_info[:kind] = 1 # is 'file'
        end
        #apply filter - limit result set with filter
        #f_regex, f_amt = 'cof/htdocs/template', 10
        if f_regex.nil?
         l_svn_list.push status_info
        elsif el[:path].match(f_regex)
         l_svn_list.push status_info
        end
        #l_svn_list.push status_info
        if f_amt.nil? or f_amt.empty?
        elsif l_svn_list.size > f_amt.to_i
          break
        end
      }

      @entries_list = _to_expected_json_format(l_svn_list)
      repo_entries.push info_data
    rescue SvnWc::RepoAccessError => e
      #@error = e.message
      @error = "#{e.message} #{@@svn_wc}"
      repo_entries.push info_data
    end
    repo_entries
end

#svn_updateObject

update, returns ‘updated’ message, revision and update data



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/svn_wc_client.rb', line 160

def svn_update
    get_repo
    remote_files = Array.new
    begin
      @content = "Updated: Revision #{@@svn_wc.update.to_a.join("\n")}"
      remote_files.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      remote_files.push info_data
    end
    remote_files
end