Class: CodeCache::Repo::Git

Inherits:
CodeCache::Repo show all
Defined in:
lib/code_cache/repo/git.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 = {}) ⇒ Git

Returns a new instance of Git.



6
7
8
# File 'lib/code_cache/repo/git.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



17
18
19
20
21
22
# File 'lib/code_cache/repo/git.rb', line 17

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

#checkout(revision, destination) ⇒ Object

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/code_cache/repo/git.rb', line 26

def checkout( revision, destination )
  
  if revision != :head
    raise "Checking out anything other than the head of the default branch not supported"
  end
  
  cache_destination = location_in_cache()
  
  # Try and copy into the cache first
  clone_or_update_repo_in_cache(cache_destination)
  
  puts "Cache: #{cache_destination}"
  puts "Destination: #{destination}"
  
  checkout_from_cache_to_destination(cache_destination, destination, revision)
  
  true
end

#checkout_from_cache_to_destination(cache_destination, destination, revision) ⇒ Object



66
67
68
69
70
# File 'lib/code_cache/repo/git.rb', line 66

def checkout_from_cache_to_destination(cache_destination, destination, revision)
  output = `git clone --single-branch #{cache_destination} #{destination} 2>&1`
  status = $? == 0
  { :output => output, :status => status }
end

#clone_bare_repo_to_cache(cache_destination) ⇒ Object



60
61
62
63
64
# File 'lib/code_cache/repo/git.rb', line 60

def clone_bare_repo_to_cache(cache_destination)
  output = `git clone --bare #{url} #{cache_destination} 2>&1`
  status = $? == 0
  { :output => output, :status => status }
end

#clone_or_update_repo_in_cache(cache_destination) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/code_cache/repo/git.rb', line 45

def clone_or_update_repo_in_cache(cache_destination)
  update_result = update_cache(cache_destination)
  if update_result[:status] == true
    return true
  else
    clone_bare_repo_to_cache(cache_destination)
  end
end

#split_urlObject

Split the url into a unique array for the cache path



11
12
13
14
# File 'lib/code_cache/repo/git.rb', line 11

def split_url
  match = /(?:(?:git|ssh)@(.*):|(?:https?:\/\/(.*?)\/))(.*).git/.match(url)
  match.captures.compact
end

#update_cache(cache_destination) ⇒ Object



54
55
56
57
58
# File 'lib/code_cache/repo/git.rb', line 54

def update_cache(cache_destination)
  output = `GIT_DIR=#{cache_destination} git fetch origin +refs/heads/*:refs/heads/* 2>&1`
  status = $? == 0
  { :output => output, :status => status }
end