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



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/svn_wc_client.rb', line 376

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



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/svn_wc_client.rb', line 393

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



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/svn_wc_client.rb', line 423

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



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

def svn_add
    get_repo
    rev = Array.new
    begin
      raise 'svn add requires file list!' if @files.nil?
      @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



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

def svn_commit
    get_repo
    rev = Array.new
    begin
      raise 'svn commit requires file list!' if @files.nil?
      @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



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/svn_wc_client.rb', line 269

def svn_delete
    get_repo
    rev = Array.new
    begin
      raise 'svn delete requires file list!' if @files.nil?
      @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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/svn_wc_client.rb', line 152

def svn_diff
   raise 'svn diff requires file selections!' if @files.nil?
   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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/svn_wc_client.rb', line 316

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



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/svn_wc_client.rb', line 284

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/svn_wc_client.rb', line 334

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|
        fqpn = File.join(dir, el[:entry])
        next if not_new_rev(el, fqpn) if @current_rev
        #fqpn = File.join(@repo_root, el[:entry])
        status_info = {}
        #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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/svn_wc_client.rb', line 298

def svn_revert
    get_repo
    infos = Array.new
    begin
      raise 'svn revert requires file list!' if @files.nil?
      @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_status_show_updates(f_regex = nil, f_amt = nil, dir = nil) ⇒ Object

emulate a svn status -u returns entries on remote host not on local shows what files would be updated from an svn up



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/svn_wc_client.rb', line 111

def svn_status_show_updates(f_regex=nil, f_amt=nil, dir=nil)
    get_repo
    repo_entries = Array.new
    dir = @repo_root unless dir and not dir.empty?
    # only way to test
    # @error = "1.#{f_regex} 2.#{f_amt} 3.#{dir}"
    # repo_entries.push info_data
    @current_rev = nil
    begin
      @@svn_wc.info(dir).each do |r|
        @current_rev = r[1] if r[0].to_s == 'rev'
      end
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      repo_entries.push info_data  
    ensure
      if @current_rev.nil?
        @error = 'Error: Revision Unknown'
        repo_entries.push info_data
      end
    end
   svn_list(f_regex, f_amt, dir)
end

#svn_updateObject

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



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/svn_wc_client.rb', line 205

def svn_update
    get_repo
    remote_files = Array.new
    @dir = @repo_root unless @dir and not @dir.empty?
    begin
      #@content = "Updated: Revision #{@@svn_wc.update.to_a.join("\n")}"
      @content = 'Updated: Revision '
      @content << @@svn_wc.update(@dir).to_a.join("\n")
      #if @dir and not @dir.empty?
      #  @content = 'Updated: Revision '
      #  @content << @@svn_wc.update(@dir).to_a.join("\n")
      #else
      #  @content = "Updated: Revision #{@@svn_wc.update.to_a.join("\n")}"
      #end
      remote_files.push info_data
    rescue SvnWc::RepoAccessError => e
      @error = e.message
      remote_files.push info_data
    end
    remote_files
end

#svn_update_selectedObject

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



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

def svn_update_selected
    raise 'svn update selected requires file list!' if @files.nil?
   remote_files = Array.new
   @files.each { |f_list_str|
     f_stat, f_name = f_list_str.split(/\s/)
     #@error = "1.#{@files} 2.#{@files.class} 3.#{@files[1]}"
     #remote_files.push info_data
     begin
       get_repo
       @path = f_name
       #@content = @@svn_wc.diff(f_name).to_s
       @content = 'Updated: Revision '
       @content << @@svn_wc.update(f_name).to_s
       remote_files.push info_data
     rescue SvnWc::RepoAccessError => e
       @error = e.message
       remote_files.push info_data
     end
   }
   remote_files
end