Class: Dapp::GitArtifact
- Inherits:
-
Object
- Object
- Dapp::GitArtifact
- Defined in:
- lib/dapp/git_artifact.rb
Overview
Artifact from Git repo
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Instance Method Summary collapse
- #any_changes?(from, to = latest_commit) ⇒ Boolean
-
#apply_archive_command(stage) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- #apply_patch_command(stage) ⇒ Object
- #archive_command_excludes ⇒ Object
- #base_paths(paths, with_cwd = false) ⇒ Object
- #exclude_paths(with_cwd = false) ⇒ Object
- #full_name ⇒ Object
- #include_paths(with_cwd = false) ⇒ Object
-
#initialize(repo, to:, name: nil, branch: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil) ⇒ GitArtifact
constructor
rubocop:disable Metrics/ParameterLists.
- #latest_commit ⇒ Object
- #paramshash ⇒ Object
- #patch_command_excludes ⇒ Object
- #patch_size(from, to) ⇒ Object
Constructor Details
#initialize(repo, to:, name: nil, branch: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil) ⇒ GitArtifact
rubocop:disable Metrics/ParameterLists
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dapp/git_artifact.rb', line 8 def initialize(repo, to:, name: nil, branch: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil) @repo = repo @name = name @branch = branch || repo.dimg.project.[:git_artifact_branch] || repo.branch @commit = commit @to = to cwd = File.(File.join('/', cwd))[1..-1] unless cwd.nil? || cwd.empty? # must be relative!!! @cwd = cwd @include_paths = include_paths @exclude_paths = exclude_paths @owner = owner @group = group end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/dapp/git_artifact.rb', line 5 def name @name end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
4 5 6 |
# File 'lib/dapp/git_artifact.rb', line 4 def repo @repo end |
Instance Method Details
#any_changes?(from, to = latest_commit) ⇒ Boolean
57 58 59 |
# File 'lib/dapp/git_artifact.rb', line 57 def any_changes?(from, to = latest_commit) !repo.(diff_command(from, to, quiet: true), returns: [0, 1]).status.success? end |
#apply_archive_command(stage) ⇒ Object
rubocop:enable Metrics/ParameterLists
26 27 28 29 30 31 32 |
# File 'lib/dapp/git_artifact.rb', line 26 def apply_archive_command(stage) credentials = [:owner, :group].map { |attr| "--#{attr}=#{send(attr)}" unless send(attr).nil? }.compact ["#{repo.dimg.project.install_bin} #{credentials.join(' ')} -d #{to}", ["#{repo.dimg.project.git_bin} --git-dir=#{repo.container_path} archive #{stage.layer_commit(self)}:#{cwd} #{include_paths.join(' ')}", "#{sudo}#{repo.dimg.project.tar_bin} -x -C #{to} #{archive_command_excludes.join(' ')}"].join(' | ')] end |
#apply_patch_command(stage) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dapp/git_artifact.rb', line 34 def apply_patch_command(stage) current_commit = stage.layer_commit(self) prev_commit = stage.prev_g_a_stage.layer_commit(self) if prev_commit != current_commit || any_changes?(prev_commit, current_commit) [["#{repo.dimg.project.git_bin} --git-dir=#{repo.container_path} #{diff_command(prev_commit, current_commit)}", "#{sudo}#{repo.dimg.project.git_bin} apply --whitespace=nowarn --directory=#{to} #{patch_command_excludes.join(' ')} --unsafe-paths"].join(' | ')] else [] end end |
#archive_command_excludes ⇒ Object
46 47 48 |
# File 'lib/dapp/git_artifact.rb', line 46 def archive_command_excludes exclude_paths.map { |path| %(--exclude=#{path}) } end |
#base_paths(paths, with_cwd = false) ⇒ Object
81 82 83 |
# File 'lib/dapp/git_artifact.rb', line 81 def base_paths(paths, with_cwd = false) [paths].flatten.compact.map { |path| (with_cwd && cwd ? File.join(cwd, path) : path).gsub(%r{^\/*|\/*$}, '') } end |
#exclude_paths(with_cwd = false) ⇒ Object
73 74 75 |
# File 'lib/dapp/git_artifact.rb', line 73 def exclude_paths(with_cwd = false) base_paths(repo.dimg.project.system_files.concat(@exclude_paths), with_cwd) end |
#full_name ⇒ Object
85 86 87 |
# File 'lib/dapp/git_artifact.rb', line 85 def full_name "#{repo.name}#{name ? "_#{name}" : nil}" end |
#include_paths(with_cwd = false) ⇒ Object
77 78 79 |
# File 'lib/dapp/git_artifact.rb', line 77 def include_paths(with_cwd = false) base_paths(@include_paths, with_cwd) end |
#latest_commit ⇒ Object
65 66 67 |
# File 'lib/dapp/git_artifact.rb', line 65 def latest_commit @latest_commit ||= commit || repo.latest_commit(branch) end |
#paramshash ⇒ Object
69 70 71 |
# File 'lib/dapp/git_artifact.rb', line 69 def paramshash Digest::SHA256.hexdigest [full_name, to, cwd, *include_paths, *exclude_paths, owner, group].map(&:to_s).join(':::') end |
#patch_command_excludes ⇒ Object
50 51 52 53 54 55 |
# File 'lib/dapp/git_artifact.rb', line 50 def patch_command_excludes exclude_paths.map do |path| base = File.join(to, path) path =~ /[\*\?\[\]\{\}]/ ? %(--exclude=#{base} ) : %(--exclude=#{base} --exclude=#{File.join(base, '*')}) end end |
#patch_size(from, to) ⇒ Object
61 62 63 |
# File 'lib/dapp/git_artifact.rb', line 61 def patch_size(from, to) repo.("#{diff_command(from, to)} | wc -c").stdout.strip.to_i end |