Class: R10K::Git::Rugged::BareRepository

Inherits:
BaseRepository show all
Defined in:
lib/r10k/git/rugged/bare_repository.rb

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary

Attributes inherited from BaseRepository

#path

Instance Method Summary collapse

Methods inherited from BaseRepository

#branches, #ref_type, #remotes, #resolve, #tags

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(basedir, dirname) ⇒ BareRepository

Returns a new instance of BareRepository.

Parameters:

  • basedir (String)

    The base directory of the Git repository

  • dirname (String)

    The directory name of the Git repository



9
10
11
12
13
14
15
# File 'lib/r10k/git/rugged/bare_repository.rb', line 9

def initialize(basedir, dirname)
  @path = Pathname.new(File.join(basedir, dirname))

  if exist?
    @_rugged_repo = ::Rugged::Repository.bare(@path.to_s)
  end
end

Instance Method Details

#clone(remote) ⇒ void

This method returns an undefined value.

Clone the given remote.

This should only be called if the repository does not exist.

Parameters:

  • remote (String)

    The URL of the Git remote to clone.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/r10k/git/rugged/bare_repository.rb', line 33

def clone(remote)
  logger.debug1 { _("Cloning '%{remote}' into %{path}") % {remote: remote, path: @path} }

  @_rugged_repo = ::Rugged::Repository.init_at(@path.to_s, true).tap do |repo|
    config = repo.config
    config['remote.origin.url']    = remote
    config['remote.origin.fetch']  = '+refs/*:refs/*'
    config['remote.origin.mirror'] = 'true'
  end

  fetch('origin')
rescue Rugged::SshError, Rugged::NetworkError => e
  raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
end

#exist?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/r10k/git/rugged/bare_repository.rb', line 74

def exist?
  @path.exist?
end

#fetch(remote_name = 'origin') ⇒ void

This method returns an undefined value.

Fetch refs and objects from the origin remote



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/r10k/git/rugged/bare_repository.rb', line 51

def fetch(remote_name='origin')
  backup_branches = wipe_branches
  logger.debug1 { _("Fetching remote '%{remote_name}' at %{path}") % {remote_name: remote_name, path: @path } }
  options = {:credentials => credentials}
  refspecs = ['+refs/*:refs/*']

  remote = remotes[remote_name]
  proxy = R10K::Git.get_proxy_for_remote(remote)
  results = nil

  R10K::Git.with_proxy(proxy) do
    results = with_repo { |repo| repo.fetch(remote_name, refspecs, options) }
  end

  report_transfer(results, remote_name)
rescue Rugged::SshError, Rugged::NetworkError => e
  restore_branches(backup_branches)
  raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
rescue
  restore_branches(backup_branches)
  raise
end

#git_dirPathname

Returns The path to this Git repository.

Returns:

  • (Pathname)

    The path to this Git repository



18
19
20
# File 'lib/r10k/git/rugged/bare_repository.rb', line 18

def git_dir
  @path
end

#objects_dirPathname

Returns The path to the objects directory in this Git repository.

Returns:

  • (Pathname)

    The path to the objects directory in this Git repository



23
24
25
# File 'lib/r10k/git/rugged/bare_repository.rb', line 23

def objects_dir
  @path + "objects"
end

#restore_branches(backup_branches) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/r10k/git/rugged/bare_repository.rb', line 91

def restore_branches(backup_branches)
  with_repo do |repo|
    backup_branches.each_pair do |name, ref|
      if !repo.branches.exist?(name)
        repo.create_branch(name, ref)
      end
    end
  end
end

#wipe_branchesObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/r10k/git/rugged/bare_repository.rb', line 78

def wipe_branches
  backup_branches = {}
  with_repo do |repo|
    repo.branches.each do |branch|
      if !branch.head?
        backup_branches[branch.name] = branch.target_id
        repo.branches.delete(branch)
      end
    end
  end
  backup_branches
end