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



23
24
25
26
27
# File 'lib/r10k/git/stateful_repository.rb', line 23

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

#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



29
30
31
32
# File 'lib/r10k/git/stateful_repository.rb', line 29

def resolve(ref)
  @cache.sync if sync_cache?(ref)
  @cache.resolve(ref)
end

#status(ref) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/r10k/git/stateful_repository.rb', line 69

def status(ref)
  if !@repo.exist?
    :absent
  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



34
35
36
37
38
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
# File 'lib/r10k/git/stateful_repository.rb', line 34

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)

  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})
    end
  else
    logger.debug(_("%{repo_path} is already at Git ref %{ref}") % {repo_path: @repo.path, ref: ref })
  end
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)


90
91
92
93
94
# File 'lib/r10k/git/stateful_repository.rb', line 90

def sync_cache?(ref)
  return true if !@cache.exist?
  return true if !([:commit, :tag].include? @cache.ref_type(ref))
  return false
end