r10k 1.4.2 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (244) hide show
  1. data/.travis.yml +2 -0
  2. data/CHANGELOG.mkd +192 -0
  3. data/CONTRIBUTING.mkd +3 -3
  4. data/Gemfile +4 -0
  5. data/README.mkd +13 -6
  6. data/doc/dynamic-environments/configuration.mkd +77 -8
  7. data/doc/dynamic-environments/git-environments.mkd +12 -2
  8. data/doc/dynamic-environments/quickstart.mkd +6 -0
  9. data/doc/faq.mkd +6 -6
  10. data/doc/git/cloning-and-mirroring.mkd +60 -0
  11. data/doc/git/providers.mkd +68 -0
  12. data/integration/Gemfile +3 -0
  13. data/integration/README.mkd +29 -0
  14. data/integration/configs/README.mkd +7 -0
  15. data/integration/configs/pe/centos-6-64mda +25 -0
  16. data/integration/configs/pe/centos-7-64mda +25 -0
  17. data/integration/configs/pe/debian-6-64mda +25 -0
  18. data/integration/configs/pe/debian-7-64mda +25 -0
  19. data/integration/configs/pe/redhat-6-64mda +25 -0
  20. data/integration/configs/pe/redhat-7-64mda +25 -0
  21. data/integration/configs/pe/sles-11-64mda +25 -0
  22. data/integration/configs/pe/ubuntu-1004-64mda +25 -0
  23. data/integration/configs/pe/ubuntu-1204-64mda +25 -0
  24. data/integration/configs/pe/ubuntu-1404-64mda +25 -0
  25. data/integration/files/README.mkd +4 -0
  26. data/integration/files/hiera.yaml +8 -0
  27. data/integration/files/modules/helloworld/manifests/init.pp +3 -0
  28. data/integration/files/modules/hieratest/manifests/init.pp +3 -0
  29. data/integration/files/modules/unicode/files/pretend_unicode +1 -0
  30. data/integration/files/modules/unicode/manifests/init.pp +6 -0
  31. data/integration/files/pre-suite/prod_env.config +3 -0
  32. data/integration/files/r10k_conf.yaml.erb +9 -0
  33. data/integration/lib/README.mkd +4 -0
  34. data/integration/lib/git_utils.rb +205 -0
  35. data/integration/lib/master_manipulator.rb +205 -0
  36. data/integration/lib/r10k_utils.rb +166 -0
  37. data/integration/manifests/README.mkd +4 -0
  38. data/integration/pre-suite/00_pe_install.rb +4 -0
  39. data/integration/pre-suite/01_git_config.rb +59 -0
  40. data/integration/pre-suite/02_pe_r10k.rb +60 -0
  41. data/integration/pre-suite/03_test_utils.rb +17 -0
  42. data/integration/pre-suite/README.mkd +5 -0
  43. data/integration/test_run_scripts/README.mkd +5 -0
  44. data/integration/test_run_scripts/all_tests-rugged-pe-centos6.sh +20 -0
  45. data/integration/test_run_scripts/all_tests-rugged-pe-rhel7.sh +20 -0
  46. data/integration/test_run_scripts/all_tests-rugged-pe-sles11.sh +20 -0
  47. data/integration/test_run_scripts/all_tests-rugged-pe-ubuntu1204.sh +20 -0
  48. data/integration/test_run_scripts/all_tests-rugged-pe-ubuntu1404.sh +20 -0
  49. data/integration/test_run_scripts/all_tests-shellgit-pe-centos6.sh +20 -0
  50. data/integration/test_run_scripts/all_tests-shellgit-pe-rhel7.sh +20 -0
  51. data/integration/test_run_scripts/all_tests-shellgit-pe-sles11.sh +20 -0
  52. data/integration/test_run_scripts/all_tests-shellgit-pe-ubuntu1204.sh +20 -0
  53. data/integration/test_run_scripts/all_tests-shellgit-pe-ubuntu1404.sh +20 -0
  54. data/integration/test_run_scripts/basic_functionality/all_tests-pe-centos6.sh +20 -0
  55. data/integration/test_run_scripts/command_line/all_tests-pe-centos6.sh +20 -0
  56. data/integration/test_run_scripts/git_source/all_tests-pe-centos6.sh +20 -0
  57. data/integration/test_run_scripts/user_scenario/basic_workflow/all_tests-pe-centos6.sh +20 -0
  58. data/integration/test_run_scripts/user_scenario/complex_workflow/all_tests-pe-centos6.sh +20 -0
  59. data/integration/tests/README.mkd +4 -0
  60. data/integration/tests/basic_functionality/negative/neg_deploy_with_invalid_r10k_yaml.rb +46 -0
  61. data/integration/tests/basic_functionality/negative/neg_deploy_with_missing_r10k_yaml.rb +27 -0
  62. data/integration/tests/command_line/deploy_env_without_mod_update.rb +75 -0
  63. data/integration/tests/command_line/negative/neg_deploy_env_with_module_update.rb +76 -0
  64. data/integration/tests/command_line/negative/neg_invalid_cli_dep_flag.rb +22 -0
  65. data/integration/tests/command_line/negative/neg_invalid_cli_env_flag.rb +22 -0
  66. data/integration/tests/command_line/negative/neg_invalid_cmd_line_arg.rb +24 -0
  67. data/integration/tests/git_source/git_source_git.rb +123 -0
  68. data/integration/tests/git_source/git_source_ssh.rb +84 -0
  69. data/integration/tests/git_source/git_source_submodule.rb +69 -0
  70. data/integration/tests/git_source/negative/neg_git_broken_remote.rb +37 -0
  71. data/integration/tests/git_source/negative/neg_git_unauthorized_https.rb +45 -0
  72. data/integration/tests/git_source/negative/neg_git_unauthorized_ssh.rb +72 -0
  73. data/integration/tests/git_source/negative/neg_git_unicode_branch.rb +34 -0
  74. data/integration/tests/user_scenario/basic_workflow/multi_env_1000_branches.rb +66 -0
  75. data/integration/tests/user_scenario/basic_workflow/multi_env_custom_forge_git_module.rb +110 -0
  76. data/integration/tests/user_scenario/basic_workflow/multi_env_custom_forge_git_module_static.rb +113 -0
  77. data/integration/tests/user_scenario/basic_workflow/multi_env_hiera.rb +93 -0
  78. data/integration/tests/user_scenario/basic_workflow/multi_env_multi_source.rb +132 -0
  79. data/integration/tests/user_scenario/basic_workflow/multi_source_custom_forge_git_module.rb +160 -0
  80. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_basedir.rb +45 -0
  81. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_forge_module.rb +43 -0
  82. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_module.rb +40 -0
  83. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_module_ref.rb +42 -0
  84. data/integration/tests/user_scenario/basic_workflow/negative/neg_bad_git_remote.rb +44 -0
  85. data/integration/tests/user_scenario/basic_workflow/negative/neg_branch_name_collision.rb +63 -0
  86. data/integration/tests/user_scenario/basic_workflow/negative/neg_disk_full.rb +73 -0
  87. data/integration/tests/user_scenario/basic_workflow/negative/neg_duplicate_module_names.rb +45 -0
  88. data/integration/tests/user_scenario/basic_workflow/negative/neg_inaccessible_forge.rb +51 -0
  89. data/integration/tests/user_scenario/basic_workflow/negative/neg_invalid_env_name.rb +33 -0
  90. data/integration/tests/user_scenario/basic_workflow/negative/neg_invalid_puppet_file.rb +35 -0
  91. data/integration/tests/user_scenario/basic_workflow/negative/neg_module_specified_at_deleted_release.rb +44 -0
  92. data/integration/tests/user_scenario/basic_workflow/negative/neg_read_only.rb +57 -0
  93. data/integration/tests/user_scenario/basic_workflow/negative/neg_specify_deleted_forge_module.rb +44 -0
  94. data/integration/tests/user_scenario/basic_workflow/single_env_10000_files.rb +73 -0
  95. data/integration/tests/user_scenario/basic_workflow/single_env_custom_forge_git_module.rb +103 -0
  96. data/integration/tests/user_scenario/basic_workflow/single_env_custom_forge_module.rb +80 -0
  97. data/integration/tests/user_scenario/basic_workflow/single_env_custom_module.rb +48 -0
  98. data/integration/tests/user_scenario/basic_workflow/single_env_large_files.rb +73 -0
  99. data/integration/tests/user_scenario/basic_workflow/single_env_module_already_installed.rb +81 -0
  100. data/integration/tests/user_scenario/basic_workflow/single_env_module_last_release_deleted.rb +67 -0
  101. data/integration/tests/user_scenario/basic_workflow/single_env_non-existent_base_dir.rb +93 -0
  102. data/integration/tests/user_scenario/basic_workflow/single_env_purge_unmanaged_modules.rb +92 -0
  103. data/integration/tests/user_scenario/basic_workflow/single_env_switch_forge_git_module.rb +116 -0
  104. data/integration/tests/user_scenario/basic_workflow/single_env_unicode_paths.rb +59 -0
  105. data/integration/tests/user_scenario/basic_workflow/single_env_upgrade_forge_mod_revert_change.rb +165 -0
  106. data/integration/tests/user_scenario/complex_workflow/multi_env_add_change_remove.rb +162 -0
  107. data/integration/tests/user_scenario/complex_workflow/multi_env_remove_re-add.rb +110 -0
  108. data/lib/r10k/action/cri_runner.rb +10 -13
  109. data/lib/r10k/cli.rb +3 -8
  110. data/lib/r10k/cli/version.rb +10 -1
  111. data/lib/r10k/deployment/config.rb +26 -6
  112. data/lib/r10k/deployment/config/loader.rb +38 -29
  113. data/lib/r10k/deployment/source.rb +2 -4
  114. data/lib/r10k/environment/git.rb +10 -19
  115. data/lib/r10k/environment/name.rb +86 -0
  116. data/lib/r10k/errors.rb +8 -5
  117. data/lib/r10k/feature.rb +35 -0
  118. data/lib/r10k/feature/collection.rb +23 -0
  119. data/lib/r10k/features.rb +18 -0
  120. data/lib/r10k/git.rb +106 -12
  121. data/lib/r10k/git/alternates.rb +2 -2
  122. data/lib/r10k/git/cache.rb +43 -26
  123. data/lib/r10k/git/errors.rb +12 -6
  124. data/lib/r10k/git/head.rb +3 -0
  125. data/lib/r10k/git/ref.rb +3 -0
  126. data/lib/r10k/git/remote_head.rb +6 -0
  127. data/lib/r10k/git/repository.rb +6 -0
  128. data/lib/r10k/git/rugged.rb +17 -0
  129. data/lib/r10k/git/rugged/bare_repository.rb +86 -0
  130. data/lib/r10k/git/rugged/base_repository.rb +90 -0
  131. data/lib/r10k/git/rugged/cache.rb +11 -0
  132. data/lib/r10k/git/rugged/thin_repository.rb +72 -0
  133. data/lib/r10k/git/rugged/working_repository.rb +99 -0
  134. data/lib/r10k/git/shellgit.rb +9 -0
  135. data/lib/r10k/git/shellgit/bare_repository.rb +29 -0
  136. data/lib/r10k/git/shellgit/base_repository.rb +102 -0
  137. data/lib/r10k/git/shellgit/cache.rb +11 -0
  138. data/lib/r10k/git/shellgit/thin_repository.rb +56 -0
  139. data/lib/r10k/git/shellgit/working_repository.rb +73 -0
  140. data/lib/r10k/git/stateful_repository.rb +80 -0
  141. data/lib/r10k/git/tag.rb +3 -0
  142. data/lib/r10k/git/working_dir.rb +15 -2
  143. data/lib/r10k/logging.rb +73 -36
  144. data/lib/r10k/logging/terminaloutputter.rb +36 -0
  145. data/lib/r10k/module/forge.rb +10 -9
  146. data/lib/r10k/module/git.rb +14 -44
  147. data/lib/r10k/module/metadata_file.rb +31 -0
  148. data/lib/r10k/module/svn.rb +0 -1
  149. data/lib/r10k/module_repository/forge.rb +20 -7
  150. data/lib/r10k/puppetfile.rb +1 -1
  151. data/lib/r10k/settings/container.rb +6 -0
  152. data/lib/r10k/settings/mixin.rb +9 -0
  153. data/lib/r10k/source.rb +2 -3
  154. data/lib/r10k/source/base.rb +5 -5
  155. data/lib/r10k/source/git.rb +11 -86
  156. data/lib/r10k/source/svn.rb +8 -52
  157. data/lib/r10k/util/basedir.rb +4 -4
  158. data/lib/r10k/util/commands.rb +31 -0
  159. data/lib/r10k/util/exec_env.rb +36 -0
  160. data/lib/r10k/util/platform.rb +4 -0
  161. data/lib/r10k/util/purgeable.rb +2 -2
  162. data/lib/r10k/util/subprocess.rb +3 -7
  163. data/lib/r10k/util/subprocess/runner.rb +3 -28
  164. data/lib/r10k/util/subprocess/runner/posix.rb +103 -0
  165. data/lib/r10k/util/subprocess/runner/pump.rb +59 -0
  166. data/lib/r10k/util/subprocess/{windows/runner.rb → runner/windows.rb} +2 -11
  167. data/lib/r10k/util/symbolize_keys.rb +31 -0
  168. data/lib/r10k/version.rb +1 -1
  169. data/lib/shared/puppet/module_tool/metadata.rb +197 -0
  170. data/r10k.gemspec +7 -5
  171. data/spec/fixtures/integration/git/puppet-boolean-bare.tar +0 -0
  172. data/spec/fixtures/module/forge/bad_module/metadata.json +1 -0
  173. data/spec/fixtures/unit/util/subprocess/posix/runner/no-execute.sh +3 -0
  174. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/can_fetch_all_versions_of_a_given_module.yml +171 -26
  175. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/can_fetch_the_latest_version_of_a_given_module.yml +171 -26
  176. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/and_the_expected_version_is_latest/ignores_deleted_releases.yml +190 -0
  177. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/it_handles_errors_from_forgeapi_puppetlabs_com/raises_an_error_for_a_non-existant_module.yml +34 -0
  178. data/spec/fixtures/vcr/cassettes/R10K_Module_Forge/and_the_expected_version_is_latest/sets_the_expected_version_based_on_the_latest_forge_version.yml +87 -26
  179. data/spec/integration/git/rugged/bare_repository_spec.rb +13 -0
  180. data/spec/integration/git/rugged/thin_repository_spec.rb +14 -0
  181. data/spec/integration/git/rugged/working_repository_spec.rb +13 -0
  182. data/spec/integration/git/shellgit/bare_repository_spec.rb +13 -0
  183. data/spec/integration/git/shellgit/thin_repository_spec.rb +14 -0
  184. data/spec/integration/git/shellgit/working_repository_spec.rb +13 -0
  185. data/spec/integration/git/stateful_repository_spec.rb +104 -0
  186. data/spec/matchers/match_realpath.rb +18 -0
  187. data/spec/shared-contexts/git-fixtures.rb +55 -0
  188. data/spec/shared-examples/git-repository.rb +38 -0
  189. data/spec/shared-examples/git/bare_repository.rb +70 -0
  190. data/spec/shared-examples/git/thin_repository.rb +26 -0
  191. data/spec/shared-examples/git/working_repository.rb +115 -0
  192. data/spec/shared-examples/subprocess-runner.rb +83 -0
  193. data/spec/spec_helper.rb +13 -9
  194. data/spec/unit/action/cri_runner_spec.rb +0 -6
  195. data/spec/unit/action/puppetfile/{cri_action_spec.rb → cri_runner_spec.rb} +0 -0
  196. data/spec/unit/deployment/config/loader_spec.rb +48 -0
  197. data/spec/unit/deployment/config_spec.rb +49 -0
  198. data/spec/unit/environment/git_spec.rb +5 -39
  199. data/spec/unit/environment/name_spec.rb +135 -0
  200. data/spec/unit/feature_spec.rb +50 -0
  201. data/spec/unit/git/alternates_spec.rb +1 -1
  202. data/spec/unit/git/cache_spec.rb +32 -19
  203. data/spec/unit/git/commit_spec.rb +1 -0
  204. data/spec/unit/git/head_spec.rb +1 -6
  205. data/spec/unit/git/ref_spec.rb +0 -23
  206. data/spec/unit/git/rugged/cache_spec.rb +26 -0
  207. data/spec/unit/git/shellgit/cache_spec.rb +27 -0
  208. data/spec/unit/git/stateful_repository_spec.rb +43 -0
  209. data/spec/unit/git/tag_spec.rb +1 -0
  210. data/spec/unit/git/working_dir_spec.rb +1 -0
  211. data/spec/unit/git_spec.rb +74 -0
  212. data/spec/unit/logging/terminaloutputter_spec.rb +53 -0
  213. data/spec/unit/logging_spec.rb +68 -0
  214. data/spec/unit/module/forge_spec.rb +7 -38
  215. data/spec/unit/module/git_spec.rb +7 -38
  216. data/spec/unit/module/metadata_file_spec.rb +69 -0
  217. data/spec/unit/module/svn_spec.rb +0 -4
  218. data/spec/unit/module_repository/forge_spec.rb +22 -1
  219. data/spec/unit/puppet/module_tool/metadata_spec.rb +301 -0
  220. data/spec/unit/settings/container_spec.rb +17 -0
  221. data/spec/unit/source/git_spec.rb +0 -123
  222. data/spec/unit/source/svn_spec.rb +66 -0
  223. data/spec/unit/util/commands_spec.rb +61 -0
  224. data/spec/unit/util/exec_env_spec.rb +56 -0
  225. data/spec/unit/util/subprocess/runner/posix_spec.rb +7 -0
  226. data/spec/unit/util/subprocess/runner/pump_spec.rb +79 -0
  227. data/spec/unit/util/subprocess_spec.rb +3 -2
  228. data/spec/unit/util/symbolize_keys_spec.rb +51 -0
  229. metadata +226 -45
  230. data/lib/r10k/execution.rb +0 -47
  231. data/lib/r10k/module/metadata.rb +0 -47
  232. data/lib/r10k/util/core_ext/hash_ext.rb +0 -19
  233. data/lib/r10k/util/subprocess/io.rb +0 -7
  234. data/lib/r10k/util/subprocess/posix.rb +0 -4
  235. data/lib/r10k/util/subprocess/posix/io.rb +0 -7
  236. data/lib/r10k/util/subprocess/posix/runner.rb +0 -140
  237. data/lib/r10k/util/subprocess/windows.rb +0 -4
  238. data/lib/r10k/util/subprocess/windows/io.rb +0 -6
  239. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions.yml +0 -42
  240. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions/can_fetch_all_versions_of_a_given_module.yml +0 -42
  241. data/spec/fixtures/vcr/cassettes/R10K_ModuleRepository_Forge/looking_up_versions/can_fetch_the_latest_version_of_a_given_module.yml +0 -42
  242. data/spec/unit/deployment/source_spec.rb +0 -24
  243. data/spec/unit/module/metadata_spec.rb +0 -68
  244. data/spec/unit/util/core_ext/hash_ext_spec.rb +0 -63
@@ -0,0 +1,17 @@
1
+ require 'r10k/git'
2
+
3
+ begin
4
+ require 'rugged'
5
+ rescue LoadError
6
+ end
7
+
8
+ module R10K
9
+ module Git
10
+ module Rugged
11
+ require 'r10k/git/rugged/bare_repository'
12
+ require 'r10k/git/rugged/working_repository'
13
+ require 'r10k/git/rugged/cache'
14
+ require 'r10k/git/rugged/thin_repository'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,86 @@
1
+ require 'r10k/git/rugged'
2
+ require 'r10k/git/rugged/base_repository'
3
+ require 'r10k/git/errors'
4
+
5
+ class R10K::Git::Rugged::BareRepository < R10K::Git::Rugged::BaseRepository
6
+
7
+ # @param basedir [String] The base directory of the Git repository
8
+ # @param dirname [String] The directory name of the Git repository
9
+ def initialize(basedir, dirname)
10
+ @path = Pathname.new(File.join(basedir, dirname))
11
+
12
+ if exist?
13
+ @_rugged_repo = ::Rugged::Repository.bare(@path.to_s)
14
+ end
15
+ end
16
+
17
+ # @return [Pathname] The path to this Git repository
18
+ def git_dir
19
+ @path
20
+ end
21
+
22
+ # Clone the given remote.
23
+ #
24
+ # This should only be called if the repository does not exist.
25
+ #
26
+ # @param remote [String] The URL of the Git remote to clone.
27
+ # @return [void]
28
+ def clone(remote)
29
+ logger.debug1 { "Cloning '#{remote}' into #{@path}" }
30
+ @_rugged_repo = ::Rugged::Repository.init_at(@path.to_s, true)
31
+ with_repo do |repo|
32
+ config = repo.config
33
+ config['remote.origin.url'] = remote
34
+ config['remote.origin.fetch'] = '+refs/*:refs/*'
35
+ config['remote.origin.mirror'] = 'true'
36
+ fetch
37
+ end
38
+ rescue Rugged::SshError, Rugged::NetworkError => e
39
+ raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
40
+ end
41
+
42
+ # Fetch refs and objects from the origin remote
43
+ #
44
+ # @return [void]
45
+ def fetch
46
+ backup_branches = wipe_branches
47
+ logger.debug1 { "Fetching remote 'origin' at #{@path}" }
48
+ options = {:credentials => credentials}
49
+ refspecs = ['+refs/*:refs/*']
50
+ results = with_repo { |repo| repo.fetch('origin', refspecs, options) }
51
+ report_transfer(results, 'origin')
52
+ rescue Rugged::SshError, Rugged::NetworkError => e
53
+ restore_branches(backup_branches)
54
+ raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
55
+ rescue
56
+ restore_branches(backup_branches)
57
+ raise
58
+ end
59
+
60
+ def exist?
61
+ @path.exist?
62
+ end
63
+
64
+ def wipe_branches
65
+ backup_branches = {}
66
+ with_repo do |repo|
67
+ repo.branches.each do |branch|
68
+ if !branch.head?
69
+ backup_branches[branch.name] = branch.target_id
70
+ repo.branches.delete(branch)
71
+ end
72
+ end
73
+ end
74
+ backup_branches
75
+ end
76
+
77
+ def restore_branches(backup_branches)
78
+ with_repo do |repo|
79
+ backup_branches.each_pair do |name, ref|
80
+ if !repo.branches.exist?(name)
81
+ repo.create_branch(name, ref)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,90 @@
1
+ require 'r10k/git/rugged'
2
+ require 'r10k/logging'
3
+
4
+ class R10K::Git::Rugged::BaseRepository
5
+
6
+ include R10K::Logging
7
+
8
+ def resolve(pattern)
9
+ object = with_repo { |repo| repo.rev_parse(pattern) }
10
+ case object
11
+ when NilClass
12
+ nil
13
+ when ::Rugged::Tag, ::Rugged::Tag::Annotation
14
+ object.target.oid
15
+ else
16
+ object.oid
17
+ end
18
+ rescue ::Rugged::ReferenceError
19
+ nil
20
+ end
21
+
22
+ def branches
23
+ with_repo { |repo| repo.branches.each_name(:local).to_a }
24
+ end
25
+
26
+ def tags
27
+ with_repo { |repo| repo.tags.each_name.to_a }
28
+ end
29
+
30
+ # @return [Symbol] The type of the given ref, one of :branch, :tag, :commit, or :unknown
31
+ def ref_type(pattern)
32
+ if branches.include? pattern
33
+ :branch
34
+ elsif tags.include? pattern
35
+ :tag
36
+ elsif resolve(pattern)
37
+ :commit
38
+ else
39
+ :unknown
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def with_repo
46
+ yield @_rugged_repo if @_rugged_repo
47
+ ensure
48
+ @_rugged_repo.close if @_rugged_repo
49
+ end
50
+
51
+
52
+ # Generate a lambda that can create a credentials object for the
53
+ # authentication type in question.
54
+ #
55
+ # @note The Rugged API expects an object that responds to #call; the
56
+ # Credentials subclasses implement #call returning self so that
57
+ # the Credentials object can be used, or a Proc that returns a
58
+ # Credentials object can be used.
59
+ #
60
+ # @api private
61
+ #
62
+ # @return [Proc]
63
+ def credentials
64
+ Proc.new do |url, username_from_url, allowed_types|
65
+ private_key = R10K::Git.settings[:private_key]
66
+ git_user = R10K::Git.settings[:username]
67
+
68
+ if private_key.nil?
69
+ raise R10K::Git::GitError.new("Git remote #{url.inspect} uses the SSH protocol but no private key was given", :git_dir => @path.to_s)
70
+ end
71
+
72
+ if !username_from_url.nil?
73
+ user = username_from_url
74
+ elsif git_user
75
+ user = git_user
76
+ logger.debug1 "URL #{url.inspect} did not specify an SSH user, using #{user.inspect} from configuration"
77
+ else
78
+ user = Etc.getlogin
79
+ logger.debug1 "URL #{url.inspect} did not specify an SSH user, using current user #{user.inspect}"
80
+ end
81
+
82
+ Rugged::Credentials::SshKey.new(:username => user, :privatekey => private_key)
83
+ end
84
+ end
85
+
86
+ def report_transfer(results, remote)
87
+ logger.debug2 { "Transferred #{results[:total_objects]} objects (#{results[:received_bytes]} bytes) from '#{remote}' into #{git_dir}'" }
88
+ nil
89
+ end
90
+ end
@@ -0,0 +1,11 @@
1
+ require 'r10k/git/rugged'
2
+ require 'r10k/git/cache'
3
+
4
+ class R10K::Git::Rugged::Cache < R10K::Git::Cache
5
+
6
+ @instance_cache = R10K::InstanceCache.new(self)
7
+
8
+ def self.bare_repository
9
+ R10K::Git::Rugged::BareRepository
10
+ end
11
+ end
@@ -0,0 +1,72 @@
1
+ require 'r10k/git'
2
+ require 'r10k/git/rugged/working_repository'
3
+ require 'r10k/git/rugged/cache'
4
+
5
+ class R10K::Git::Rugged::ThinRepository < R10K::Git::Rugged::WorkingRepository
6
+ def initialize(basedir, dirname)
7
+ super
8
+
9
+ if exist? && origin
10
+ set_cache(origin)
11
+ end
12
+ end
13
+
14
+ # Clone this git repository
15
+ #
16
+ # @param remote [String] The Git remote to clone
17
+ # @param opts [Hash]
18
+ #
19
+ # @options opts [String] :ref The git ref to check out on clone
20
+ #
21
+ # @return [void]
22
+ def clone(remote, opts = {})
23
+ logger.debug1 { "Cloning '#{remote}' into #{@path}" }
24
+ set_cache(remote)
25
+ @cache_repo.sync
26
+
27
+ objectpath = (@cache_repo.git_dir + 'objects').to_s
28
+
29
+ # {Rugged::Repository.clone_at} doesn't support :alternates, which
30
+ # completely breaks how thin repositories need to work. To circumvent
31
+ # this we manually create a Git repository, set up git remotes, and
32
+ # update 'objects/info/alternates' with the path. We don't actually
33
+ # fetch any objects because we don't need them, and we don't actually
34
+ # use any refs in this repository so we skip all those steps.
35
+ ::Rugged::Repository.init_at(@path.to_s, false)
36
+ @_rugged_repo = ::Rugged::Repository.new(@path.to_s, :alternates => [objectpath])
37
+ alternates << objectpath
38
+
39
+ with_repo do |repo|
40
+ config = repo.config
41
+ config['remote.origin.url'] = remote
42
+ config['remote.origin.fetch'] = '+refs/heads/*:refs/remotes/origin/*'
43
+ config['remote.cache.url'] = @cache_repo.git_dir.to_s
44
+ config['remote.cache.fetch'] = '+refs/heads/*:refs/remotes/cache/*'
45
+ end
46
+
47
+ checkout(opts.fetch(:ref, 'HEAD'))
48
+ end
49
+
50
+ def checkout(ref)
51
+ super(@cache_repo.resolve(ref))
52
+ end
53
+
54
+ # Fetch refs and objects from one of the Git remotes
55
+ #
56
+ # @param remote [String] The remote to fetch, defaults to 'cache'
57
+ # @return [void]
58
+ def fetch(remote = 'cache')
59
+ super(remote)
60
+ end
61
+
62
+ # @return [String] The cache remote URL
63
+ def cache
64
+ with_repo { |repo| repo.config['remote.cache.url'] }
65
+ end
66
+
67
+ private
68
+
69
+ def set_cache(remote)
70
+ @cache_repo = R10K::Git::Rugged::Cache.generate(remote)
71
+ end
72
+ end
@@ -0,0 +1,99 @@
1
+ require 'r10k/git/rugged'
2
+ require 'r10k/git/rugged/base_repository'
3
+ require 'r10k/git/errors'
4
+
5
+ class R10K::Git::Rugged::WorkingRepository < R10K::Git::Rugged::BaseRepository
6
+
7
+ # @return [Pathname] The path to this directory
8
+ attr_reader :path
9
+
10
+ # @return [Pathname] The path to the Git repository inside of this directory
11
+ def git_dir
12
+ @path + '.git'
13
+ end
14
+
15
+ # @param basedir [String] The base directory of the Git repository
16
+ # @param dirname [String] The directory name of the Git repository
17
+ def initialize(basedir, dirname)
18
+ @path = Pathname.new(File.join(basedir, dirname))
19
+ if exist? && git_dir.exist?
20
+ @_rugged_repo = ::Rugged::Repository.new(@path.to_s, :alternates => alternates.to_a)
21
+ end
22
+ end
23
+
24
+ # Clone this git repository
25
+ #
26
+ # @param remote [String] The Git remote to clone
27
+ # @param opts [Hash]
28
+ #
29
+ # @options opts [String] :ref The git ref to check out on clone
30
+ # @options opts [String] :reference A Git repository to use as an alternate object database
31
+ #
32
+ # @return [void]
33
+ def clone(remote, opts = {})
34
+ logger.debug1 { "Cloning '#{remote}' into #{@path}" }
35
+
36
+ # libgit2/rugged doesn't support cloning a repository and providing an
37
+ # alternate object database, making the handling of :alternates a noop.
38
+ # Unfortunately this means that this method can't really use alternates
39
+ # and running the clone will duplicate all objects in the specified
40
+ # repository. However alternate databases can be handled when an existing
41
+ # repository is loaded, so loading a cloned repo will correctly use
42
+ # alternate object database.
43
+ options = {:credentials => credentials}
44
+ options.merge!(:alternates => [File.join(opts[:reference], 'objects')]) if opts[:reference]
45
+ @_rugged_repo = ::Rugged::Repository.clone_at(remote, @path.to_s, options)
46
+
47
+ if opts[:reference]
48
+ alternates << File.join(opts[:reference], 'objects')
49
+ end
50
+
51
+ if opts[:ref]
52
+ # todo: always check out something; since we're fetching a repository we
53
+ # won't populate the working directory.
54
+ checkout(opts[:ref])
55
+ end
56
+ rescue Rugged::SshError, Rugged::NetworkError => e
57
+ raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
58
+ end
59
+
60
+ # Check out the given Git ref
61
+ #
62
+ # @param ref [String] The git reference to check out
63
+ # @return [void]
64
+ def checkout(ref)
65
+ logger.debug1 { "Checking out ref '#{ref}' at #{@path}" }
66
+ sha = resolve(ref)
67
+
68
+ with_repo do |repo|
69
+ repo.checkout(sha)
70
+ repo.reset(sha, :hard)
71
+ end
72
+ end
73
+
74
+ def fetch(remote = 'origin')
75
+ logger.debug1 { "Fetching remote '#{remote}' at #{@path}" }
76
+ options = {:credentials => credentials}
77
+ refspecs = ["+refs/heads/*:refs/remotes/#{remote}/*"]
78
+ results = with_repo { |repo| repo.fetch(remote, refspecs, options) }
79
+ report_transfer(results, remote)
80
+ rescue Rugged::SshError, Rugged::NetworkError => e
81
+ raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
82
+ end
83
+
84
+ def exist?
85
+ @path.exist?
86
+ end
87
+
88
+ def head
89
+ resolve('HEAD')
90
+ end
91
+
92
+ def alternates
93
+ R10K::Git::Alternates.new(git_dir)
94
+ end
95
+
96
+ def origin
97
+ with_repo { |repo| repo.config['remote.origin.url'] }
98
+ end
99
+ end
@@ -0,0 +1,9 @@
1
+ module R10K
2
+ module Git
3
+ module ShellGit
4
+ require 'r10k/git/shellgit/bare_repository'
5
+ require 'r10k/git/shellgit/working_repository'
6
+ require 'r10k/git/shellgit/thin_repository'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'r10k/git/shellgit'
2
+ require 'r10k/git/shellgit/base_repository'
3
+
4
+ # Create and manage Git bare repositories.
5
+ class R10K::Git::ShellGit::BareRepository < R10K::Git::ShellGit::BaseRepository
6
+
7
+ # @return [Pathname] The path to this Git repository
8
+ def git_dir
9
+ @path
10
+ end
11
+
12
+ # @param basedir [String] The base directory of the Git repository
13
+ # @param dirname [String] The directory name of the Git repository
14
+ def initialize(basedir, dirname)
15
+ @path = Pathname.new(File.join(basedir, dirname))
16
+ end
17
+
18
+ def clone(remote)
19
+ git ['clone', '--mirror', remote, git_dir.to_s]
20
+ end
21
+
22
+ def fetch
23
+ git ['fetch', '--prune'], :git_dir => git_dir.to_s
24
+ end
25
+
26
+ def exist?
27
+ @path.exist?
28
+ end
29
+ end
@@ -0,0 +1,102 @@
1
+ require 'r10k/git/shellgit'
2
+ require 'r10k/logging'
3
+
4
+ class R10K::Git::ShellGit::BaseRepository
5
+
6
+ # @abstract
7
+ # @return [Pathname] The path to the Git directory
8
+ def git_dir
9
+ raise NotImplementedError
10
+ end
11
+
12
+ # Resolve the given Git ref to a commit
13
+ #
14
+ # @param pattern [String] The git ref to resolve
15
+ # @return [String, nil] The commit SHA if the ref could be resolved, nil otherwise.
16
+ def resolve(pattern)
17
+ result = git ['rev-parse', "#{pattern}^{commit}"], :git_dir => git_dir.to_s, :raise_on_fail => false
18
+ if result.success?
19
+ result.stdout
20
+ end
21
+ end
22
+
23
+ # For compatibility with R10K::Git::Ref
24
+ # @todo remove alias
25
+ alias rev_parse resolve
26
+
27
+ # @return [Array<String>] All local branches in this repository
28
+ def branches
29
+ for_each_ref('refs/heads')
30
+ end
31
+
32
+ # @return [Array<String>] All tags in this repository
33
+ def tags
34
+ for_each_ref('refs/tags')
35
+ end
36
+
37
+ # @return [Symbol] The type of the given ref, one of :branch, :tag, :commit, or :unknown
38
+ def ref_type(pattern)
39
+ if branches.include? pattern
40
+ :branch
41
+ elsif tags.include? pattern
42
+ :tag
43
+ elsif resolve(pattern)
44
+ :commit
45
+ else
46
+ :unknown
47
+ end
48
+ end
49
+
50
+ include R10K::Logging
51
+
52
+ private
53
+
54
+ # @param pattern [String]
55
+ def for_each_ref(pattern)
56
+ matcher = %r[#{pattern}/(.*)$]
57
+ output = git ['for-each-ref', pattern, '--format', '%(refname)'], :git_dir => git_dir.to_s
58
+ output.stdout.scan(matcher).flatten
59
+ end
60
+
61
+ # Wrap git commands
62
+ #
63
+ # @param cmd [Array<String>] cmd The arguments for the git prompt
64
+ # @param opts [Hash] opts
65
+ #
66
+ # @option opts [String] :path
67
+ # @option opts [String] :git_dir
68
+ # @option opts [String] :work_tree
69
+ # @option opts [String] :raise_on_fail
70
+ #
71
+ # @raise [R10K::ExecutionFailure] If the executed command exited with a
72
+ # nonzero exit code.
73
+ #
74
+ # @return [String] The git command output
75
+ def git(cmd, opts = {})
76
+ raise_on_fail = opts.fetch(:raise_on_fail, true)
77
+
78
+ argv = %w{git}
79
+
80
+ if opts[:path]
81
+ argv << "--git-dir" << File.join(opts[:path], '.git')
82
+ argv << "--work-tree" << opts[:path]
83
+ else
84
+ if opts[:git_dir]
85
+ argv << "--git-dir" << opts[:git_dir]
86
+ end
87
+ if opts[:work_tree]
88
+ argv << "--work-tree" << opts[:work_tree]
89
+ end
90
+ end
91
+
92
+ argv.concat(cmd)
93
+
94
+ subproc = R10K::Util::Subprocess.new(argv)
95
+ subproc.raise_on_fail = raise_on_fail
96
+ subproc.logger = self.logger
97
+
98
+ result = subproc.execute
99
+
100
+ result
101
+ end
102
+ end