Skip to content

Commit d6fb3df

Browse files
committed
Enable Git Includes (Sparse Checkout)
1 parent 6d00012 commit d6fb3df

File tree

1 file changed

+44
-2
lines changed
  • lib/puppet/provider/vcsrepo

1 file changed

+44
-2
lines changed

lib/puppet/provider/vcsrepo/git.rb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
has_features :bare_repositories, :reference_tracking, :ssh_identity, :multiple_remotes,
99
:user, :depth, :branch, :submodules, :safe_directory, :hooks_allowed,
10-
:umask, :http_proxy, :tmpdir
10+
:umask, :http_proxy, :tmpdir, :include_paths
1111

1212
def create
1313
check_force
@@ -19,6 +19,7 @@ def create
1919
set_mirror if @resource.value(:ensure) == :mirror && @resource.value(:source).is_a?(Hash)
2020
self.skip_hooks = @resource.value(:skip_hooks) unless @resource.value(:skip_hooks).nil?
2121

22+
configure_sparse_checkout if @resource.value(:includes)
2223
checkout if @resource.value(:revision)
2324
update_submodules if !ensure_bare_or_mirror? && @resource.value(:submodules) == :true
2425

@@ -91,6 +92,20 @@ def revision=(desired)
9192
update_owner_and_excludes
9293
end
9394

95+
def includes
96+
return nil if bare_exists?
97+
98+
at_path do
99+
return nil unless File.file?('.git/info/sparse-checkout')
100+
File.readlines('.git/info/sparse-checkout').map(&:chomp)
101+
end
102+
end
103+
104+
def includes=(_desired)
105+
configure_sparse_checkout
106+
checkout
107+
end
108+
94109
def bare_exists?
95110
bare_git_config_exists? && !working_copy_exists?
96111
end
@@ -407,6 +422,33 @@ def init_repository
407422
end
408423
end
409424

425+
# @!visibility private
426+
def configure_sparse_checkout
427+
raise("Cannot set includes on a #{@resource.value(:ensure)} repository") if ensure_bare_or_mirror? || bare_exists?
428+
429+
git_ver = git_version
430+
if Gem::Version.new(git_ver) >= Gem::Version.new('2.25.0')
431+
# sparse-checkout command was introduced in version 2.25.0.
432+
at_path do
433+
args = ['sparse-checkout', 'set', '--no-cone'] + @resource.value(:includes)
434+
exec_git(*args)
435+
end
436+
else
437+
at_path do
438+
exec_git('config', '--local', '--bool', 'core.sparseCheckout', 'true')
439+
440+
# Includes may be an Array or a String
441+
File.open('.git/info/sparse-checkout', 'w') do |f|
442+
if @resource.value(:includes).respond_to?(:each)
443+
@resource.value(:includes).each { |inc| f.puts inc }
444+
else
445+
f.puts @resource.value(:includes)
446+
end
447+
end
448+
end
449+
end
450+
end
451+
410452
# @!visibility private
411453
def commits?
412454
at_path do
@@ -493,7 +535,7 @@ def tags
493535
def set_excludes
494536
# Excludes may be an Array or a String.
495537
at_path do
496-
open('.git/info/exclude', 'w') do |f|
538+
File.open('.git/info/exclude', 'w') do |f|
497539
if @resource.value(:excludes).respond_to?(:each)
498540
@resource.value(:excludes).each { |ex| f.puts ex }
499541
else

0 commit comments

Comments
 (0)