Class: R10K::Git::Cache

Inherits:
Repository show all
Includes:
Logging, Settings::Mixin
Defined in:
lib/r10k/git/cache.rb

Overview

Mirror a git repository for use shared git object repositories

See Also:

  • git-clone(1)

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Repository

#basedir, #dirname, #git_dir, #remote

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

formatter, included, level, level=, levels, #logger, #logger_name, outputter, parse_level

Methods included from Settings::Mixin

included

Methods inherited from Repository

#remotes, #resolve_commit, #resolve_head, #resolve_ref, #resolve_remote_head, #resolve_tag, #tags

Constructor Details

#initialize(remote) ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • remote (String)
  • cache_root (String)


38
39
40
41
42
# File 'lib/r10k/git/cache.rb', line 38

def initialize(remote)
  @remote = remote

  @git_dir = File.join(settings[:cache_root], sanitized_dirname)
end

Instance Attribute Details

#pathObject (readonly)



31
32
33
34
# File 'lib/r10k/git/cache.rb', line 31

def path
  logger.warn "#{self.class}#path is deprecated; use #git_dir"
  @git_dir
end

Class Method Details

.generate(remote) ⇒ Object



22
23
24
# File 'lib/r10k/git/cache.rb', line 22

def self.generate(remote)
  registry.generate(remote)
end

.registryObject



18
19
20
# File 'lib/r10k/git/cache.rb', line 18

def self.registry
  @registry ||= R10K::Registry.new(self)
end

Instance Method Details

#branchesArray<String>

Returns A list the branches for the git repository.

Returns:

  • (Array<String>)

    A list the branches for the git repository



74
75
76
77
# File 'lib/r10k/git/cache.rb', line 74

def branches
  output = git %w[for-each-ref refs/heads --format %(refname)], :git_dir => git_dir
  output.stdout.scan(%r[refs/heads/(.*)$]).flatten
end

#cached?true, false

Returns If the repository has been locally cached.

Returns:

  • (true, false)

    If the repository has been locally cached



80
81
82
# File 'lib/r10k/git/cache.rb', line 80

def cached?
  File.exist? git_dir
end

#syncObject



44
45
46
47
48
49
# File 'lib/r10k/git/cache.rb', line 44

def sync
  if not @synced
    sync!
    @synced = true
  end
end

#sync!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/r10k/git/cache.rb', line 51

def sync!
  if cached?
    fetch
  else
    logger.debug "Creating new git cache for #{@remote.inspect}"

    # TODO extract this to an initialization step
    unless File.exist? settings[:cache_root]
      FileUtils.mkdir_p settings[:cache_root]
    end

    git ['clone', '--mirror', @remote, git_dir]
  end
rescue R10K::Util::Subprocess::SubprocessError => e
  msg = e.result.stderr.slice(/^fatal: .*$/)
  if msg
    raise R10K::Git::GitError, "Couldn't update git cache for #{@remote}: #{msg.inspect}"
  else
    raise e
  end
end