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

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

Constant Summary collapse

CACHE_VERSION =
1

Instance Attribute Summary collapse

Attributes inherited from Base

#dapp, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#branch, #commit_at, #commit_exists?, #diff, #entries, #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_url, #submodules_params, #submodules_params_base, #tag_at, #tags, #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.



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

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

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#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:, branch:, ignore_git_fetch: false) ⇒ Object



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

def get_or_create(dapp, name, url:, branch:, ignore_git_fetch: false)
  key         = [url, branch, ignore_git_fetch]
  inverse_key = [url, branch, !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!(branch, ignore_git_fetch: ignore_git_fetch) unless ignore_git_fetch
    end
  end
end

.repositoriesObject



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

def repositories
  @repositories ||= {}
end

Instance Method Details

#_rugged_credentialsObject



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

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



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

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

#branch_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def branch_exist?(name)
  git.branches.exist?(branch_format(name))
end

#fetch!(branch = nil, ignore_git_fetch: false) ⇒ Object



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

def fetch!(branch = nil, ignore_git_fetch: false)
  _with_lock do
    branch ||= self.branch

    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.fetch('origin', [branch], credentials: _rugged_credentials)
      rescue Rugged::SshError => e
        raise Error::Rugged, code: :rugged_remote_error, data: { url: url, message: e.message.strip }
      end
      raise Error::Rugged, code: :branch_not_exist_in_remote_git_repository, data: { branch: branch, url: url } unless branch_exist?(branch)
    end
  end unless ignore_git_fetch || dapp.dry_run?
end

#latest_commit(name) ⇒ Object



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

def latest_commit(name)
  git.ref("refs/remotes/#{branch_format(name)}").target_id
end

#lookup_commit(commit) ⇒ Object



102
103
104
105
106
# File 'lib/dapp/dimg/git_repo/remote.rb', line 102

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