Class: R10K::Git::StatefulRepository

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Parameters:

  • remote (String)

    The git remote to use for the repo

  • basedir (String)

    The path containing the Git repo

  • dirname (String)

    The directory name of the Git repo



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

#cacheObject (readonly)

Returns the value of attribute cache.



17
18
19
# File 'lib/r10k/git/stateful_repository.rb', line 17

def cache
  @cache
end

#repoObject (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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/r10k/git/stateful_repository.rb', line 78

def status(ref)
  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.dirty?
    :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) ⇒ 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)
  @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)

  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.

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/r10k/git/stateful_repository.rb', line 101

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