Class: Dapp::Dimg::GitArtifact

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

Overview

Git repo artifact

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Tar

#tar_gz_read, #tar_read, #tar_write

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



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

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.options[:git_artifact_branch] || repo.branch
  @commit = commit

  @to = to
  @cwd = (cwd.nil? || cwd.empty? || cwd == '/') ? '' : File.expand_path(File.join('/', cwd, '/'))[1..-1]
  @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.



8
9
10
# File 'lib/dapp/dimg/git_artifact.rb', line 8

def name
  @name
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

Instance Method Details

#apply_archive_command(stage) ⇒ Object

rubocop:enable Metrics/ParameterLists



31
32
33
34
35
36
37
38
39
40
# File 'lib/dapp/dimg/git_artifact.rb', line 31

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 archive_any_changes?(stage)
      commands << "#{sudo}#{repo.dimg.dapp.tar_bin} -xf #{archive_file(*archive_stage_commits(stage))} -C #{to}"
    end
  end
end

#apply_patch_command(stage) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dapp/dimg/git_artifact.rb', line 42

def apply_patch_command(stage)
  [].tap do |commands|
    if dev_mode?
      if any_changes?(*dev_patch_stage_commits(stage))
        changed_files = diff_patches(*dev_patch_stage_commits(stage)).map {|p| File.join(to, cwd, p.delta.new_file[:path])}
        commands << "#{sudo}#{repo.dimg.dapp.rm_bin} -rf #{changed_files.join(' ')}"
        commands << "#{sudo}#{repo.dimg.dapp.tar_bin} -xf #{archive_file(*dev_patch_stage_commits(stage))} -C #{to}"
      end
    else
      if patch_any_changes?(stage)
        commands << "#{sudo}#{repo.dimg.dapp.git_bin} apply --whitespace=nowarn --directory=#{to} --unsafe-paths #{patch_file(*patch_stage_commits(stage))}"
      end
    end
  end
end

#archive_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/dapp/dimg/git_artifact.rb', line 127

def archive_any_changes?(stage)
  any_changes?(*archive_stage_commits(stage))
end

#dev_patch_hashObject



106
107
108
109
110
111
112
113
# File 'lib/dapp/dimg/git_artifact.rb', line 106

def dev_patch_hash
  return unless dev_mode?

  Digest::SHA256.hexdigest(diff_patches(latest_commit, nil).map do |patch|
    next unless (path = repo.path.dirname.join(patch.delta.new_file[:path])).file?
    File.read(path)
  end.join(':::'))
end

#full_nameObject



123
124
125
# File 'lib/dapp/dimg/git_artifact.rb', line 123

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

#latest_commitObject



115
116
117
# File 'lib/dapp/dimg/git_artifact.rb', line 115

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

#paramshashObject



119
120
121
# File 'lib/dapp/dimg/git_artifact.rb', line 119

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

#patch_any_changes?(stage) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/dapp/dimg/git_artifact.rb', line 131

def patch_any_changes?(stage)
  any_changes?(*patch_stage_commits(stage))
end

#patch_size(from_commit, to_commit) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dapp/dimg/git_artifact.rb', line 89

def patch_size(from_commit, to_commit)
  diff_patches(from_commit, to_commit).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_checksum(stage) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dapp/dimg/git_artifact.rb', line 58

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

  paths = (include_paths(true) + base_paths(stage_dependencies, true)).uniq
  to_commit = dev_mode? ? nil : latest_commit

  stage_dependencies_key = [stage.name, to_commit]
  @stage_dependencies_checksums ||= {}
  @stage_dependencies_checksums[stage_dependencies_key] = begin
    if @stage_dependencies_checksums.key?(stage_dependencies_key)
      @stage_dependencies_checksums[stage_dependencies_key]
    else
      if (patches = diff_patches(nil, to_commit, paths: paths)).empty?
        repo.dimg.dapp.log_warning(desc: { code: :stage_dependencies_not_found,
                                           data: { repo: repo.respond_to?(:url) ? repo.url : 'local',
                                                   dependencies: stage_dependencies.join(', ') } })
      end

      patches.sort_by {|patch| patch.delta.new_file[:path]}
        .reduce(nil) {|prev_hash, patch|
        Digest::SHA256.hexdigest [
          prev_hash,
          patch.delta.new_file[:path],
          patch.delta.new_file[:mode].to_s,
          patch.to_s
        ].compact.join(':::')
      }
    end
  end
end