Class: R10K::Git::StatefulRepository
- Inherits:
-
Object
- Object
- R10K::Git::StatefulRepository
- Extended by:
- Forwardable
- Includes:
- Logging
- Defined in:
- lib/r10k/git/stateful_repository.rb
Overview
Manage how Git repositories are created and set to specific refs
Constant Summary
Constants included from Logging
Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
-
#initialize(remote, basedir, dirname) ⇒ StatefulRepository
constructor
Create a new shallow git working directory.
- #resolve(ref) ⇒ Object
- #status(ref, exclude_spec = false) ⇒ Object
-
#sync(ref, force = true, exclude_spec = false) ⇒ Object
Returns true if the sync actually updated the repo, false otherwise.
- #sync_cache?(ref) ⇒ Boolean private
Methods included from Logging
add_outputters, debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Constructor Details
#initialize(remote, basedir, dirname) ⇒ StatefulRepository
Create a new shallow git working directory
27 28 29 30 31 |
# File 'lib/r10k/git/stateful_repository.rb', line 27 def initialize(remote, basedir, dirname) @remote = remote @cache = R10K::Git.cache.generate(@remote) @repo = R10K::Git.thin_repository.new(basedir, dirname, @cache) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
17 18 19 |
# File 'lib/r10k/git/stateful_repository.rb', line 17 def cache @cache end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
13 14 15 |
# File 'lib/r10k/git/stateful_repository.rb', line 13 def repo @repo end |
Instance Method Details
#resolve(ref) ⇒ Object
33 34 35 36 |
# File 'lib/r10k/git/stateful_repository.rb', line 33 def resolve(ref) @cache.sync if sync_cache?(ref) @cache.resolve(ref) end |
#status(ref, exclude_spec = false) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/r10k/git/stateful_repository.rb', line 78 def status(ref, exclude_spec=false) if !@repo.exist? :absent elsif !@cache.exist? :mismatched elsif !@repo.git_dir.exist? :mismatched elsif !@repo.git_dir.directory? :mismatched elsif !(@repo.origin == @remote) :mismatched elsif @repo.head.nil? :mismatched elsif @repo.dirty?(exclude_spec) :dirty elsif !(@repo.head == @cache.resolve(ref)) :outdated elsif @cache.ref_type(ref) == :branch && !@cache.synced? :outdated else :insync end end |
#sync(ref, force = true, exclude_spec = false) ⇒ Object
Returns true if the sync actually updated the repo, false otherwise
39 40 41 42 43 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 71 72 73 74 75 76 |
# File 'lib/r10k/git/stateful_repository.rb', line 39 def sync(ref, force=true, exclude_spec=false) @cache.sync if sync_cache?(ref) sha = @cache.resolve(ref) if sha.nil? raise R10K::Git::UnresolvableRefError.new(_("Unable to sync repo to unresolvable ref '%{ref}'") % {ref: ref}, :git_dir => @repo.git_dir) end workdir_status = status(ref, exclude_spec) updated = true case workdir_status when :absent logger.debug(_("Cloning %{repo_path} and checking out %{ref}") % {repo_path: @repo.path, ref: ref }) @repo.clone(@remote, {:ref => sha}) when :mismatched logger.debug(_("Replacing %{repo_path} and checking out %{ref}") % {repo_path: @repo.path, ref: ref }) @repo.path.rmtree @repo.clone(@remote, {:ref => sha}) when :outdated logger.debug(_("Updating %{repo_path} to %{ref}") % {repo_path: @repo.path, ref: ref }) @repo.checkout(sha, {:force => force}) when :dirty if force logger.warn(_("Overwriting local modifications to %{repo_path}") % {repo_path: @repo.path}) logger.debug(_("Updating %{repo_path} to %{ref}") % {repo_path: @repo.path, ref: ref }) @repo.checkout(sha, {:force => force}) else logger.warn(_("Skipping %{repo_path} due to local modifications") % {repo_path: @repo.path}) updated = false end else logger.debug(_("%{repo_path} is already at Git ref %{ref}") % {repo_path: @repo.path, ref: ref }) updated = false end updated end |
#sync_cache?(ref) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
103 104 105 106 107 108 |
# File 'lib/r10k/git/stateful_repository.rb', line 103 def sync_cache?(ref) return true if !@cache.exist? return true if ref == 'HEAD' return true if !([:commit, :tag].include? @cache.ref_type(ref)) return false end |