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

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

Instance Attribute Summary collapse

Attributes inherited from Base

#dimg, #name

Instance Method Summary collapse

Methods inherited from Base

#branch, #commit_at, #commit_exists?, #diff, #entries, #exclude_paths, #find_commit_id_by_message, #lookup_object, #patches, #walker

Constructor Details

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

Returns a new instance of Remote.



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

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

  @url = url

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

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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#_rugged_credentialsObject



25
26
27
28
29
30
31
32
33
# File 'lib/dapp/dimg/git_repo/remote.rb', line 25

def _rugged_credentials
  @_rugged_credentials ||= begin
    if 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

#branch_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#fetch!(branch = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/dapp/dimg/git_repo/remote.rb', line 39

def fetch!(branch = nil)
  branch ||= self.branch
  dimg.dapp.log_secondary_process(dimg.dapp.t(code: 'process.git_artifact_fetch', data: { url: url }), short: true) do
    git.fetch('origin', [branch], credentials: _rugged_credentials)
    raise Error::Rugged, code: :branch_not_exist_in_remote_git_repository, data: { branch: branch, url: url } unless branch_exist?(branch)
  end unless dimg.ignore_git_fetch || dimg.dapp.dry_run?
end

#latest_commit(name) ⇒ Object



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

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

#lookup_commit(commit) ⇒ Object



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

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



35
36
37
# File 'lib/dapp/dimg/git_repo/remote.rb', line 35

def path
  Pathname(dimg.build_path('git_repo_remote', name, Digest::MD5.hexdigest(url)).to_s)
end