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, branch = nil) ⇒ 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
# File 'lib/code_cache/repo/git.rb', line 26

def checkout( revision, destination , branch=nil)
  
  raise "Checking out anything other than the head of the default branch not supported" if revision != :head
  raise "Not checking out, as #{destination} directory has another git repository" if !Dir["#{destination}/.git"].empty?
  
  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}"
  output = checkout_from_cache_to_destination(cache_destination, destination, revision, branch)

  true
end

#checkout_from_cache_to_destination(cache_destination, destination, revision, branch) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/code_cache/repo/git.rb', line 64

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

  raise CodeCache::BranchNotFound if output.include? 'Could not find remote branch'

  { :output => output, :status => status }
end

#clone_bare_repo_to_cache(cache_destination) ⇒ Object



58
59
60
61
62
# File 'lib/code_cache/repo/git.rb', line 58

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



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

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 if match
end

#update_cache(cache_destination) ⇒ Object



52
53
54
55
56
# File 'lib/code_cache/repo/git.rb', line 52

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