Class: Dapp::Dimg::GitArtifact

Inherits:
Object
  • Object
show all
Defined in:
lib/dapp/dimg/git_artifact.rb

Overview

Git repo artifact

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, to:, name: nil, branch: nil, commit: nil, cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil, stages_dependencies: {}) ⇒ GitArtifact

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dapp/dimg/git_artifact.rb', line 9

def initialize(repo, to:, name: nil, branch: nil, commit: nil,
               cwd: nil, include_paths: nil, exclude_paths: nil, owner: nil, group: nil,
               stages_dependencies: {})
  @repo = repo
  @name = name

  @branch = branch || repo.dimg.dapp.cli_options[:git_artifact_branch] || repo.branch
  @commit = commit

  @to = to
  @cwd = begin
    if cwd.nil? || cwd.empty?
      ''
    else
      cwd = File.expand_path(File.join('/', cwd))[1..-1] # must be relative
      "#{cwd.chomp('/')}/"
    end
  end
  @include_paths = include_paths
  @exclude_paths = exclude_paths
  @owner = owner
  @group = group

  @stages_dependencies = stages_dependencies
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dapp/dimg/git_artifact.rb', line 6

def name
  @name
end

#repoObject (readonly)

Returns the value of attribute repo.



5
6
7
# File 'lib/dapp/dimg/git_artifact.rb', line 5

def repo
  @repo
end

Instance Method Details

#any_changes?(from, to = latest_commit) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/dapp/dimg/git_artifact.rb', line 98

def any_changes?(from, to = latest_commit)
  diff_patches(from, to).any?
end

#apply_archive_command(stage) ⇒ Object

rubocop:enable Metrics/ParameterLists



36
37
38
39
40
41
42
43
44
45
# File 'lib/dapp/dimg/git_artifact.rb', line 36

def apply_archive_command(stage)
  credentials = [:owner, :group].map { |attr| "--#{attr}=#{send(attr)}" unless send(attr).nil? }.compact

  [].tap do |commands|
    commands << "#{repo.dimg.dapp.install_bin} #{credentials.join(' ')} -d #{to}"
    if any_changes?(nil, stage.layer_commit(self))
      commands << "#{sudo}#{repo.dimg.dapp.tar_bin} -xf #{archive_file(stage.layer_commit(self))} -C #{to}"
    end
  end
end

#apply_dev_patch_command(stage) ⇒ Object



51
52
53
# File 'lib/dapp/dimg/git_artifact.rb', line 51

def apply_dev_patch_command(stage)
  patch_command(stage.prev_g_a_stage.layer_commit(self), nil)
end

#apply_patch_command(stage) ⇒ Object



47
48
49
# File 'lib/dapp/dimg/git_artifact.rb', line 47

def apply_patch_command(stage)
  patch_command(stage.prev_g_a_stage.layer_commit(self), stage.layer_commit(self))
end

#dev_patch_hash(stage) ⇒ Object



82
83
84
# File 'lib/dapp/dimg/git_artifact.rb', line 82

def dev_patch_hash(stage)
  Digest::SHA256.hexdigest diff_patches(stage.prev_g_a_stage.layer_commit(self), nil).map(&:to_s).join(':::')
end

#full_nameObject



94
95
96
# File 'lib/dapp/dimg/git_artifact.rb', line 94

def full_name
  "#{repo.name}#{name ? "_#{name}" : nil}"
end

#latest_commitObject



86
87
88
# File 'lib/dapp/dimg/git_artifact.rb', line 86

def latest_commit
  @latest_commit ||= commit || repo.latest_commit(branch)
end

#paramshashObject



90
91
92
# File 'lib/dapp/dimg/git_artifact.rb', line 90

def paramshash
  Digest::SHA256.hexdigest [full_name, to, cwd, *include_paths, *exclude_paths, owner, group].map(&:to_s).join(':::')
end

#patch_size(from, to) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dapp/dimg/git_artifact.rb', line 65

def patch_size(from, to)
  diff_patches(from, to).reduce(0) do |bytes, patch|
    patch.hunks.each do |hunk|
      hunk.lines.each do |l|
        bytes +=
          case l.line_origin
          when :eof_newline_added, :eof_newline_removed then 1
          when :addition, :deletion, :binary            then l.content.size
          else # :context, :file_header, :hunk_header, :eof_no_newline
            0
          end
      end
    end
    bytes
  end
end

#stage_dependencies_checksums(stage) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/dapp/dimg/git_artifact.rb', line 55

def stage_dependencies_checksums(stage)
  return [] if (stage_dependencies = stages_dependencies[stage.name]).empty?

  paths = include_paths(true) + base_paths(stage_dependencies, true)
  diff_patches(nil, latest_commit, paths: paths).map do |patch|
    delta_new_file = patch.delta.new_file
    Digest::SHA256.hexdigest [delta_new_file[:path], repo.lookup_object(delta_new_file[:oid]).content].join(':::')
  end
end