Class: R10K::Git::WorkingDir

Inherits:
Repository show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/r10k/git/working_dir.rb

Overview

Implements sparse git repositories with shared objects

Working directory instances use the git alternatives object store, so that working directories only contain checked out files and all object files are shared.

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Repository

#basedir, #dirname, #git_dir

Instance Method Summary collapse

Methods included from Logging

formatter, included, level, level=, levels, #logger, #logger_name, outputter, parse_level

Methods inherited from Repository

#remotes, #resolve_commit, #resolve_head, #resolve_ref, #resolve_tag

Constructor Details

#initialize(ref, remote, basedir, dirname = nil) ⇒ WorkingDir

Create a new shallow git working directory

Parameters:

  • ref (String, R10K::Git::Ref)
  • remote (String)
  • basedir (String)
  • dirname (String) (defaults to: nil)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/r10k/git/working_dir.rb', line 34

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

  @remote  = remote
  @basedir = basedir
  @dirname = dirname || ref

  @full_path = File.join(@basedir, @dirname)
  @git_dir   = File.join(@full_path, '.git')

  @cache = R10K::Git::Cache.generate(@remote)

  if ref.is_a? String
    @ref = R10K::Git::Ref.new(ref, self)
  else
    @ref = ref
    @ref.repository = self
  end
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



18
19
20
# File 'lib/r10k/git/working_dir.rb', line 18

def cache
  @cache
end

#refObject (readonly)

Returns the value of attribute ref.



22
23
24
# File 'lib/r10k/git/working_dir.rb', line 22

def ref
  @ref
end

#remoteObject (readonly)

Returns the value of attribute remote.



26
27
28
# File 'lib/r10k/git/working_dir.rb', line 26

def remote
  @remote
end

Instance Method Details

#checkout(ref) ⇒ Object

check out the given ref

Parameters:



88
89
90
91
92
93
94
95
96
# File 'lib/r10k/git/working_dir.rb', line 88

def checkout(ref)
  if ref.resolvable?
    git ["checkout", "--force", @ref.sha1], :path => @full_path
  else
    raise R10K::Git::UnresolvableRefError.new(
      "Cannot check out unresolvable ref '#{@ref}'",
      :git_dir => @full_path)
  end
end

#cloned?true, false Also known as: git?

Determine if repo has been cloned into a specific dir

Returns:

  • (true, false)

    If the repo has already been cloned



74
75
76
# File 'lib/r10k/git/working_dir.rb', line 74

def cloned?
  File.directory? @git_dir
end

#currentR10k::Git::Head

The currently checked out HEAD

Returns:

  • (R10k::Git::Head)


101
102
103
# File 'lib/r10k/git/working_dir.rb', line 101

def current
  R10K::Git::Head.new('HEAD', self)
end

#exist?true, false

Does a directory exist where we expect a working dir to be?

Returns:

  • (true, false)


81
82
83
# File 'lib/r10k/git/working_dir.rb', line 81

def exist?
  File.directory? @full_path
end

#outdated?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/r10k/git/working_dir.rb', line 105

def outdated?
  @ref.fetch? or needs_checkout?
end

#resolve_remote_head(pattern, remote = 'cache') ⇒ Object

Prefer remote heads from the ‘cache’ remote over the real remote



110
111
112
# File 'lib/r10k/git/working_dir.rb', line 110

def resolve_remote_head(pattern, remote = 'cache')
  super(pattern, remote)
end

#syncObject

Synchronize the local git repository.



54
55
56
57
58
59
60
# File 'lib/r10k/git/working_dir.rb', line 54

def sync
  if not cloned?
    clone
  else
    update
  end
end

#updateObject



62
63
64
65
66
67
68
69
# File 'lib/r10k/git/working_dir.rb', line 62

def update
  if fetch?
    fetch_from_cache
    checkout(@ref)
  elsif needs_checkout?
    checkout(@ref)
  end
end