Class: Dapp::Dimg::GitRepo::Remote

Inherits:
Base
  • Object
show all
Defined in:
lib/dapp/dimg/git_repo/remote.rb

Constant Summary collapse

CACHE_VERSION =
2

Instance Attribute Summary collapse

Attributes inherited from Base

#dapp, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#blobs_entries, #branch, #commit_exists?, #diff, #empty?, #exclude_paths, #find_commit_id_by_message, #ignore_patch?, #lookup_object, #nested_git_directories_patches, #patches, #remote_branches, #remote_origin_url, #remote_origin_url_protocol, #submodule_params, #submodule_url, #submodules, #submodules_params, #tags, #tracked_remote_repository?, #walker

Methods included from Helper::Trivia

#check_path?, #check_subpath?, #class_to_lowercase, class_to_lowercase, #delete_file, #ignore_path?, #ignore_path_base, #kwargs, #make_path, #path_checker, #search_file_upward

Constructor Details

#initialize(dapp, name, url:) ⇒ Remote

Returns a new instance of Remote.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dapp/dimg/git_repo/remote.rb', line 30

def initialize(dapp, name, url:)
  super(dapp, name)

  @url = url

  _with_lock do
    dapp.log_secondary_process(dapp.t(code: 'process.git_artifact_clone', data: { url: url }), short: true) do
      begin
        if [:https, :ssh].include?(remote_origin_url_protocol) && !Rugged.features.include?(remote_origin_url_protocol)
          raise Error::Rugged, code: :rugged_protocol_not_supported, data: { url: url, protocol: remote_origin_url_protocol }
        end

        Rugged::Repository.clone_at(url, path.to_s, bare: true, credentials: _rugged_credentials)
      rescue Rugged::NetworkError, Rugged::SslError, Rugged::OSError => e
        raise Error::Rugged, code: :rugged_remote_error, data: { message: e.message, url: url }
      end
    end
  end unless path.directory?
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.get_or_create(dapp, name, url:, ignore_git_fetch: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dapp/dimg/git_repo/remote.rb', line 10

def get_or_create(dapp, name, url:, ignore_git_fetch: false)
  key         = [url, ignore_git_fetch]
  inverse_key = [url, !ignore_git_fetch]

  repositories[key] ||= begin
    if repositories.key?(inverse_key)
      repositories[inverse_key]
    else
      new(dapp, name, url: url)
    end.tap do |repo|
      repo.fetch! unless ignore_git_fetch
    end
  end
end

.repositoriesObject



25
26
27
# File 'lib/dapp/dimg/git_repo/remote.rb', line 25

def repositories
  @repositories ||= {}
end

Instance Method Details

#_rugged_credentialsObject



54
55
56
57
58
59
60
61
62
# File 'lib/dapp/dimg/git_repo/remote.rb', line 54

def _rugged_credentials
  @_rugged_credentials ||= begin
    if remote_origin_url_protocol == :ssh
      host_with_user = url.split(':', 2).first
      username = host_with_user.split('@', 2).reverse.last
      Rugged::Credentials::SshKeyFromAgent.new(username: username)
    end
  end
end

#_with_lock(&blk) ⇒ Object



50
51
52
# File 'lib/dapp/dimg/git_repo/remote.rb', line 50

def _with_lock(&blk)
  dapp.lock("remote_git_artifact.#{name}", default_timeout: 600, &blk)
end

#fetch!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dapp/dimg/git_repo/remote.rb', line 68

def fetch!
  _with_lock do
    cfg_path = path.join("config")
    cfg = IniFile.load(cfg_path)
    remote_origin_cfg = cfg['remote "origin"']

    old_url = remote_origin_cfg["url"]
    if old_url and old_url != url
      remote_origin_cfg["url"] = url
      cfg.write(filename: cfg_path)
    end

    dapp.log_secondary_process(dapp.t(code: 'process.git_artifact_fetch', data: { url: url }), short: true) do
      begin
        git.remotes.each { |remote| remote.fetch(credentials: _rugged_credentials) }
      rescue Rugged::SshError, Rugged::NetworkError => e
        raise Error::Rugged, code: :rugged_remote_error, data: { url: url, message: e.message.strip }
      end
    end
  end unless dapp.dry_run?
end

#latest_commit(branch) ⇒ Object



90
91
92
93
94
95
# File 'lib/dapp/dimg/git_repo/remote.rb', line 90

def latest_commit(branch)
  git.ref("refs/remotes/#{branch_format(branch)}").tap do |ref|
    raise Error::Rugged, code: :branch_not_exist_in_remote_git_repository, data: { branch: branch, url: url } if ref.nil?
    break ref.target_id
  end
end

#lookup_commit(commit) ⇒ Object



97
98
99
100
101
# File 'lib/dapp/dimg/git_repo/remote.rb', line 97

def lookup_commit(commit)
  super
rescue Rugged::OdbError, TypeError => _e
  raise Error::Rugged, code: :commit_not_found_in_remote_git_repository, data: { commit: commit, url: url }
end

#pathObject



64
65
66
# File 'lib/dapp/dimg/git_repo/remote.rb', line 64

def path
  Pathname(dapp.build_path("remote_git_repo", CACHE_VERSION.to_s, dapp.consistent_uniq_slugify(name)).to_s)
end

#submodules_git(commit) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dapp/dimg/git_repo/remote.rb', line 103

def submodules_git(commit)
  submodules_git_path(commit).tap do |git_path|
    break begin
      if git_path.directory?
        Rugged::Repository.new(git_path.to_s)
      else
        Rugged::Repository.clone_at(path.to_s, git_path.to_s).tap do |submodules_git|
          submodules_git.checkout(commit)
        end
      end
    end
  end
end

#submodules_git_path(commit) ⇒ Object



117
118
119
# File 'lib/dapp/dimg/git_repo/remote.rb', line 117

def submodules_git_path(commit)
  Pathname(File.join(dapp.host_docker_tmp_config_dir, "submodule", dapp.consistent_uniq_slugify(name), commit).to_s)
end