Class: Berkshelf::GitLocation

Inherits:
BaseLocation show all
Includes:
Mixin::Git
Defined in:
lib/berkshelf/locations/git.rb

Direct Known Subclasses

GithubLocation

Instance Attribute Summary collapse

Attributes inherited from BaseLocation

#dependency, #options

Instance Method Summary collapse

Methods included from Mixin::Git

#git

Methods included from ShellOut

#shell_out, #shell_out!

Methods inherited from BaseLocation

#validate_cached!

Constructor Details

#initialize(dependency, options = {}) ⇒ GitLocation

Returns a new instance of GitLocation.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/berkshelf/locations/git.rb', line 12

def initialize(dependency, options = {})
  super

  @uri      = options[:git]
  @branch   = options[:branch]
  @tag      = options[:tag]
  @ref      = options[:ref]
  @revision = options[:revision]
  @rel      = options[:rel]

  # The revision to parse
  @rev_parse = options[:ref] || options[:branch] || options[:tag] || "master"
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



6
7
8
# File 'lib/berkshelf/locations/git.rb', line 6

def branch
  @branch
end

#refObject (readonly)

Returns the value of attribute ref.



8
9
10
# File 'lib/berkshelf/locations/git.rb', line 8

def ref
  @ref
end

#relObject (readonly)

Returns the value of attribute rel.



10
11
12
# File 'lib/berkshelf/locations/git.rb', line 10

def rel
  @rel
end

#revisionObject (readonly)

Returns the value of attribute revision.



9
10
11
# File 'lib/berkshelf/locations/git.rb', line 9

def revision
  @revision
end

#tagObject (readonly)

Returns the value of attribute tag.



7
8
9
# File 'lib/berkshelf/locations/git.rb', line 7

def tag
  @tag
end

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/berkshelf/locations/git.rb', line 5

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/berkshelf/locations/git.rb', line 83

def ==(other)
  other.is_a?(GitLocation) &&
    other.uri == uri &&
    other.branch == branch &&
    other.tag == tag &&
    other.shortref == shortref &&
    other.rel == rel
end

#cached_cookbookObject



75
76
77
78
79
80
81
# File 'lib/berkshelf/locations/git.rb', line 75

def cached_cookbook
  if installed?
    @cached_cookbook ||= CachedCookbook.from_path(install_path)
  else
    nil
  end
end

#installObject

Install this git cookbook into the cookbook store. This method leverages a cached git copy and a scratch directory to prevent bad cookbooks from making their way into the cookbook store.



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
68
69
70
71
72
# File 'lib/berkshelf/locations/git.rb', line 36

def install
  scratch_path = Pathname.new(Dir.mktmpdir)

  if cached?
    git(%{fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"}, cwd: cache_path.to_s)
  else
    git %{clone #{uri} "#{cache_path}" --bare --no-hardlinks}
  end

  @revision ||= git(%{rev-parse #{@rev_parse}}, cwd: cache_path.to_s)

  # Clone into a scratch directory for validations
  git %{clone --no-checkout "#{cache_path}" "#{scratch_path}"}

  # Make sure the scratch directory is up-to-date and account for rel paths
  git(%{fetch --force --tags "#{cache_path}"}, cwd: scratch_path.to_s)
  git(%{reset --hard #{@revision}}, cwd: scratch_path.to_s)

  if rel
    git(%{filter-branch --subdirectory-filter "#{rel}" --force}, cwd: scratch_path.to_s)
  end

  # Validate the scratched path is a valid cookbook
  validate_cached!(scratch_path)

  # If we got this far, we should atomically move
  FileUtils.rm_rf(install_path) if install_path.exist?
  FileUtils.mv(scratch_path, install_path)

  # Remove the git history
  FileUtils.rm_rf(File.join(install_path, ".git"))

  install_path.chmod(0777 & ~File.umask)
ensure
  # Ensure the scratch directory is cleaned up
  FileUtils.rm_rf(scratch_path)
end

#installed?Boolean

Returns:

  • (Boolean)

See Also:

  • BaseLoation#installed?


27
28
29
# File 'lib/berkshelf/locations/git.rb', line 27

def installed?
  !!(revision && install_path.exist?)
end

#to_lockObject



102
103
104
105
106
107
108
109
110
# File 'lib/berkshelf/locations/git.rb', line 102

def to_lock
  out =  "    git: #{uri}\n"
  out << "    revision: #{revision}\n"
  out << "    ref: #{shortref}\n"  if shortref
  out << "    branch: #{branch}\n" if branch
  out << "    tag: #{tag}\n"       if tag
  out << "    rel: #{rel}\n"       if rel
  out
end

#to_sObject



92
93
94
95
96
97
98
99
100
# File 'lib/berkshelf/locations/git.rb', line 92

def to_s
  info = tag || branch || shortref || @rev_parse

  if rel
    "#{uri} (at #{info}/#{rel})"
  else
    "#{uri} (at #{info})"
  end
end