Class: Dapp::GitArtifact

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

Overview

Artifact from Git repo

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

  @to = to
  cwd = File.expand_path(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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#repoObject (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

Returns:

  • (Boolean)


62
63
64
# File 'lib/dapp/git_artifact.rb', line 62

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

#apply_archive_command(stage) ⇒ Object

rubocop:enable Metrics/ParameterLists



26
27
28
29
30
31
32
33
34
35
36
37
# 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

  [].tap do |commands|
    commands << "#{repo.dimg.project.install_bin} #{credentials.join(' ')} -d #{to}"

    if include_paths_or_cwd.empty? || include_paths_or_cwd.any? { |path| file_exist_in_repo?(stage.layer_commit(self), path) }
      commands << ["#{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
  end
end

#apply_patch_command(stage) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dapp/git_artifact.rb', line 39

def apply_patch_command(stage)
  current_commit = stage.layer_commit(self)
  prev_commit = stage.prev_g_a_stage.layer_commit(self)

  if 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_excludesObject



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

def archive_command_excludes
  exclude_paths.map { |path| %(--exclude=#{path}) }
end

#base_paths(paths, with_cwd = false) ⇒ Object



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

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



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

def exclude_paths(with_cwd = false)
  base_paths(repo.dimg.project.system_files.concat(@exclude_paths), with_cwd)
end

#full_nameObject



103
104
105
# File 'lib/dapp/git_artifact.rb', line 103

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

#include_paths(with_cwd = false) ⇒ Object



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

def include_paths(with_cwd = false)
  base_paths(@include_paths, with_cwd)
end

#latest_commitObject



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

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

#paramshashObject



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

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

#patch_command_excludesObject



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

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



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

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