Class: CodeCache::Repo::SVN

Inherits:
CodeCache::Repo show all
Defined in:
lib/code_cache/repo/svn.rb

Instance Attribute Summary

Attributes inherited from CodeCache::Repo

#cache, #url

Instance Method Summary collapse

Methods inherited from CodeCache::Repo

#create_cache, #location_in_cache, #repo_type

Constructor Details

#initialize(url, options = {}) ⇒ SVN

Returns a new instance of SVN.



6
7
8
# File 'lib/code_cache/repo/svn.rb', line 6

def initialize(url, options = {})
  super(url, options)
end

Instance Method Details

#check_repo(url) ⇒ Object

Check access to the repo in question



86
87
88
89
90
91
# File 'lib/code_cache/repo/svn.rb', line 86

def check_repo(url)
  output = `svn ls #{url} 2>&1`
  if ($? != 0)
    raise BadRepo.new(url + "<<<" + output.to_s + ">>>")
  end
end

#checkout(revision, destination, branch = nil) ⇒ Object

Checkout a particular revision from the repo into the destination Caches the checkout in the process



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/code_cache/repo/svn.rb', line 12

def checkout( revision, destination, branch=nil )
 
  puts "Checking out branch in not supported for SVN" if branch
  
  if revision != :head
    raise "Checking out revisions other than head currently not supported"
  end
  
  cache_destination = location_in_cache(revision)
  
  # Try and copy into the cache first
  begin
    perform_cache_checkout_or_update(cache_destination)
    FileUtils.mkdir_p(destination)
    
    FileUtils.cp_r(cache_destination+'/.', destination)
    # Ensure the final copy is consistent
    raise CopyError if !Dir.exist?(destination)
    output = perform_update(destination)
    if !output[:status]
      raise UpdateError.new(output)
    end
    
  rescue => e
    puts "Unhandled randomness: " + e.to_s + ' - ' + e.backtrace.to_s
    # If we can't copy into the cache, copy directly to the destination
    perform_checkout(destination)
  end
  
  true
end

#perform_cache_checkout_or_update(destination, attempts = 3) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/code_cache/repo/svn.rb', line 44

def perform_cache_checkout_or_update(destination, attempts = 3)
  update_result = perform_update(destination)
  if update_result[:status] == true
    return true
  else
    checkout_result = perform_checkout(destination)
    if checkout_result[:status]
      return true
    else
      # Cache update in progress, retry with some sleeps
      if checkout_result[:output] =~ /E155037/
        sleep 1
        if attempts > 0
          perform_cache_checkout_or_update( destination, attempts-1 )
        else
          return false
        end
 
      # Cache has become corrupted
      elsif checkout_result[:output] =~ /E155004/  
        raise CacheCorruptionError.new(url)
      else
        raise UnknownCheckoutError.new(url)
      end
    end
  end
end

#perform_checkout(destination) ⇒ Object



79
80
81
82
83
# File 'lib/code_cache/repo/svn.rb', line 79

def perform_checkout(destination)
  output = `svn co #{url} #{destination} 2>&1`
  status = $? == 0
  { :output => output, :status => status }
end

#perform_update(destination) ⇒ Object

Perform an svn update on a directory – checks its a valid svn dir first



73
74
75
76
77
# File 'lib/code_cache/repo/svn.rb', line 73

def perform_update(destination)
  output = `svn info #{destination} 2>&1 && svn up #{destination} 2>&1`
  status = $? == 0
  { :output => output, :status => status }
end

#split_urlObject

Split the url into a unique array for the cache path



94
95
96
# File 'lib/code_cache/repo/svn.rb', line 94

def split_url
  url.split('//').drop(1).collect{ |e| e.split('/') }.flatten
end