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

#name

Instance Method Summary collapse

Methods inherited from Base

#branch, #commit_at, #commit_exists?, #dapp, #diff, #dimg, #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
24
25
# File 'lib/dapp/dimg/git_repo/remote.rb', line 7

def initialize(dimg, name, url:)
  super(dimg, 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?(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, 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.



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

def url
  @url
end

Instance Method Details

#_rugged_credentialsObject



31
32
33
34
35
36
37
38
39
# File 'lib/dapp/dimg/git_repo/remote.rb', line 31

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

#_with_lock(&blk) ⇒ Object



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

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

#branch_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dapp/dimg/git_repo/remote.rb', line 66

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

#fetch!(branch = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dapp/dimg/git_repo/remote.rb', line 45

def fetch!(branch = nil)
  _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
      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
  end unless dimg.ignore_git_fetch || dapp.dry_run?
end

#latest_commit(name) ⇒ Object



70
71
72
# File 'lib/dapp/dimg/git_repo/remote.rb', line 70

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

#lookup_commit(commit) ⇒ Object



74
75
76
77
78
# File 'lib/dapp/dimg/git_repo/remote.rb', line 74

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



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

def path
  Pathname(dimg.build_path('git_repo_remote', name).to_s)
end