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(ref, remote, basedir, dirname) ⇒ StatefulRepository

Create a new shallow git working directory

Parameters:

  • ref (String)

    The git ref to check out

  • 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



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

def initialize(ref, remote, basedir, dirname)
  @ref = ref
  @remote = remote

  @repo = R10K::Git.thin_repository.new(basedir, dirname)
  @cache = R10K::Git.cache.generate(remote)
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

#statusObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/r10k/git/stateful_repository.rb', line 57

def status
  if !@repo.exist?
    :absent
  elsif !@repo.git_dir.exist?
    :mismatched
  elsif !(@repo.origin == @remote)
    :mismatched
  elsif !(@repo.head == @cache.resolve(@ref))
    :outdated
  elsif @cache.ref_type(@ref) == :branch && !@cache.synced?
    :outdated
  else
    :insync
  end
end

#syncObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/r10k/git/stateful_repository.rb', line 32

def sync
  @cache.sync if sync_cache?

  sha = @cache.resolve(@ref)

  if sha.nil?
    raise R10K::Git::UnresolvableRefError.new("Unable to sync repo to unresolvable ref '#{@ref}'", :git_dir => @repo.git_dir)
  end

  case status
  when :absent
    logger.debug { "Cloning #{@repo.path} and checking out #{@ref}" }
    @repo.clone(@remote, {:ref => sha})
  when :mismatched
    logger.debug { "Replacing #{@repo.path} and checking out #{@ref}" }
    @repo.path.rmtree
    @repo.clone(@remote, {:ref => sha})
  when :outdated
    logger.debug { "Updating #{@repo.path} to #{@ref}" }
    @repo.checkout(sha)
  else
    logger.debug { "#{@repo.path} is already at Git ref #{@ref}" }
  end
end

#sync_cache?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)


74
75
76
77
78
79
# File 'lib/r10k/git/stateful_repository.rb', line 74

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