Class: Dapp::GitRepo::Remote

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

Overview

Normal Git repo

Instance Attribute Summary

Attributes inherited from Base

#dimg, #name

Instance Method Summary collapse

Methods inherited from Base

#branch, #commit_at, #container_path, #diff, #file_exist_in_tree?, #lookup_object, #path

Constructor Details

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

Returns a new instance of Remote.



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

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

  @url = url

  dimg.project.log_secondary_process(dimg.project.t(code: 'process.git_artifact_clone', data: { name: name }), short: true) do
    begin
      Rugged::Repository.clone_at(
        url, path,
        bare: true,
        credentials: proc do |_, username|
          @_rugged_credentials = Rugged::Credentials::SshKeyFromAgent.new(username: username)
        end
      )
    rescue Rugged::NetworkError, Rugged::SslError => e
      raise Error::Rugged, code: :rugged_remote_error, data: { message: e.message, url: url }
    end
  end unless File.directory?(path)
end

Instance Method Details

#branch_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/dapp/git_repo/remote.rb', line 37

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

#cleanup!Object



45
46
47
48
# File 'lib/dapp/git_repo/remote.rb', line 45

def cleanup!
  super
  FileUtils.rm_rf path
end

#fetch!(branch = nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/dapp/git_repo/remote.rb', line 29

def fetch!(branch = nil)
  branch ||= self.branch
  dimg.project.log_secondary_process(dimg.project.t(code: 'process.git_artifact_fetch', data: { name: name }), short: true) do
    git_bare.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.project.dry_run?
end

#git_bareObject



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

def git_bare
  @git_bare ||= Rugged::Repository.new(path, bare: true, credentials: @_rugged_credentials)
end

#latest_commit(name) ⇒ Object



41
42
43
# File 'lib/dapp/git_repo/remote.rb', line 41

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

#lookup_commit(commit) ⇒ Object



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

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