Class: R10K::Synchro::Git

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/synchro/git.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • remote (String)

    A git remote URL



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_rootObject

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

#remoteObject (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.

Parameters:

  • remote (String)

    A git remote URL

Returns:



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

.synchrosHash<R10K::Synchro::Git>

Returns A hash of memoized class instances.

Returns:



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.

Returns:

  • (Array<String>)

    A list of all cached remote branches



117
118
119
120
121
# File 'lib/r10k/synchro/git.rb', line 117

def branches(options = {:update_cache => false})
  cache if (options[:update_cache] or not cached?)
  output = git "branch", :git_dir => @cache_path
  output.split("\n").map { |str| str[2..-1] }
end

#cachetrue?

Update the git object cache repository if it hasn’t been done

Returns:

  • (true, nil)

    If the cache was actually updated



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.

Returns:

  • (TrueClass)

    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

Parameters:

  • dirname (String)

    The directory to check

Returns:

  • (true, false)

    If the repo has already been cloned



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.

Parameters:

  • path (String)

    The destination path for the files

  • ref (String)

    The git ref to instantiate at the destination path



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/r10k/synchro/git.rb', line 64

def sync(path, ref, options = {:update_cache => true})
  path = File.expand_path(path)
  cache if options[:update_cache]

  if self.cloned?(path)
    fetch(path)
  else
    clone(path)
  end
  reset(path, ref)
end