Class: R10K::Synchro::Git
- Inherits:
-
Object
- Object
- R10K::Synchro::Git
- Includes:
- Logging
- Defined in:
- lib/r10k/synchro/git.rb
Class Attribute Summary collapse
-
.cache_root ⇒ Object
Returns the value of attribute cache_root.
Instance Attribute Summary collapse
-
#remote ⇒ Object
readonly
Returns the value of attribute remote.
Class Method Summary collapse
-
.new(remote) ⇒ R10K::Synchro::Git
Memoize class instances and return existing instances.
-
.synchros ⇒ Hash<R10K::Synchro::Git>
A hash of memoized class instances.
Instance Method Summary collapse
-
#branches(options = {:update_cache => false}) ⇒ Array<String>
Retrieve a list of cached branches for the git repo associated with this object.
-
#cache ⇒ true?
Update the git object cache repository if it hasn’t been done.
-
#cache! ⇒ Object
Force a cache refresh.
-
#cached? ⇒ TrueClass
If the git repository is cached.
-
#cloned?(directory) ⇒ true, false
Determine if repo has been cloned into a specific dir.
-
#initialize(remote) ⇒ Git
constructor
Instantiates a new git synchro and optionally prepares for caching.
-
#sync(path, ref, options = {:update_cache => true}) ⇒ Object
Synchronize the local git repository.
Methods included from Logging
formatter, included, level, level=, #logger, outputter
Constructor Details
#initialize(remote) ⇒ Git
Instantiates a new git synchro and optionally prepares for caching
52 53 54 55 56 57 58 |
# File 'lib/r10k/synchro/git.rb', line 52 def initialize(remote) @remote = remote if self.class.cache_root @cache_path = File.join(self.class.cache_root, @remote.gsub(/[^@\w\.-]/, '-')) end end |
Class Attribute Details
.cache_root ⇒ Object
Returns the value of attribute cache_root.
21 22 23 |
# File 'lib/r10k/synchro/git.rb', line 21 def cache_root @cache_root end |
Instance Attribute Details
#remote ⇒ Object (readonly)
Returns the value of attribute remote.
47 48 49 |
# File 'lib/r10k/synchro/git.rb', line 47 def remote @remote end |
Class Method Details
.new(remote) ⇒ R10K::Synchro::Git
Memoize class instances and return existing instances.
This allows objects to mark themselves as cached to prevent unnecessary cache refreshes.
35 36 37 38 39 40 41 42 |
# File 'lib/r10k/synchro/git.rb', line 35 def new(remote) unless synchros[remote] obj = self.allocate obj.send(:initialize, remote) synchros[remote] = obj end synchros[remote] end |
.synchros ⇒ Hash<R10K::Synchro::Git>
Returns A hash of memoized class instances.
24 25 26 |
# File 'lib/r10k/synchro/git.rb', line 24 def synchros @synchros ||= {} end |
Instance Method Details
#branches(options = {:update_cache => false}) ⇒ Array<String>
Retrieve a list of cached branches for the git repo associated with this object.
117 118 119 120 121 |
# File 'lib/r10k/synchro/git.rb', line 117 def branches( = {:update_cache => false}) cache if ([:update_cache] or not cached?) output = git "branch", :git_dir => @cache_path output.split("\n").map { |str| str[2..-1] } end |
#cache ⇒ true?
Update the git object cache repository if it hasn’t been done
93 94 95 96 97 98 |
# File 'lib/r10k/synchro/git.rb', line 93 def cache unless @cached cache! @cached = true end end |
#cache! ⇒ Object
Force a cache refresh
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/r10k/synchro/git.rb', line 101 def cache! if cached? logger.debug "Updating existing cache at #{@cache_path}" git "fetch --prune", :git_dir => @cache_path else logger.debug "No cache for #{@remote.inspect}, forcing cache build" cache_root = self.class.cache_root FileUtils.mkdir_p cache_root unless File.exist? cache_root git "clone --mirror #{@remote} #{@cache_path}" end end |
#cached? ⇒ TrueClass
Returns if the git repository is cached.
77 78 79 |
# File 'lib/r10k/synchro/git.rb', line 77 def cached? @cache_path and File.directory? @cache_path end |
#cloned?(directory) ⇒ true, false
Determine if repo has been cloned into a specific dir
86 87 88 |
# File 'lib/r10k/synchro/git.rb', line 86 def cloned?(directory) File.directory?(File.join(directory, '.git')) end |
#sync(path, ref, options = {:update_cache => true}) ⇒ Object
Synchronize the local git repository.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/r10k/synchro/git.rb', line 64 def sync(path, ref, = {:update_cache => true}) path = File.(path) cache if [:update_cache] if self.cloned?(path) fetch(path) else clone(path) end reset(path, ref) end |