Class: R10K::Git::Rugged::WorkingRepository

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

Direct Known Subclasses

ThinRepository

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary

Attributes inherited from BaseRepository

#path

Instance Method Summary collapse

Methods inherited from BaseRepository

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

Methods included from Logging

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

Constructor Details

#initialize(basedir, dirname) ⇒ WorkingRepository

Returns a new instance of WorkingRepository.

Parameters:

  • basedir (String)

    The base directory of the Git repository

  • dirname (String)

    The directory name of the Git repository



14
15
16
# File 'lib/r10k/git/rugged/working_repository.rb', line 14

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

Instance Method Details

#alternatesObject



114
115
116
# File 'lib/r10k/git/rugged/working_repository.rb', line 114

def alternates
  R10K::Git::Alternates.new(git_dir)
end

#checkout(ref, opts = {}) ⇒ void

This method returns an undefined value.

Check out the given Git ref

Parameters:

  • ref (String)

    The git reference to check out



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/r10k/git/rugged/working_repository.rb', line 63

def checkout(ref, opts = {})
  sha = resolve(ref)

  if sha
    logger.debug2 { _("Checking out ref '%{ref}' (resolved to SHA '%{sha}') in repository %{path}") % {ref: ref, sha: sha, path: @path} }
  else
    raise R10K::Git::GitError.new("Unable to check out unresolvable ref '#{ref}'", git_dir: git_dir)
  end

  # :force defaults to true
  force = !opts.has_key?(:force) || opts[:force]

  with_repo do |repo|
    # rugged/libgit2 will not update (at least) the execute bit a file if the SHA is already at
    # the value being reset to, so this is now changed to an if ... else
    if force
      repo.reset(sha, :hard)
    else
      repo.checkout(sha)
    end
  end
end

#clone(remote, opts = {}) ⇒ void

This method returns an undefined value.

Clone this git repository

Parameters:

  • remote (String)

    The Git remote to clone

  • opts (Hash) (defaults to: {})


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/r10k/git/rugged/working_repository.rb', line 27

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

  proxy = R10K::Git.get_proxy_for_remote(remote)
  # libgit2/rugged doesn't support cloning a repository and providing an
  # alternate object database, making the handling of :alternates a noop.
  # Unfortunately this means that this method can't really use alternates
  # and running the clone will duplicate all objects in the specified
  # repository. However alternate databases can be handled when an existing
  # repository is loaded, so loading a cloned repo will correctly use
  # alternate object database.
  options = {:credentials => credentials, :proxy_url => proxy}
  options.merge!(:alternates => [File.join(opts[:reference], 'objects')]) if opts[:reference]


  R10K::Git.with_proxy(proxy) do
    @_rugged_repo = ::Rugged::Repository.clone_at(remote, @path.to_s, options)
  end

  if opts[:reference]
    alternates << File.join(opts[:reference], 'objects')
  end

  if opts[:ref]
    # todo:  always check out something; since we're fetching a repository we
    # won't populate the working directory.
    checkout(opts[:ref])
  end
rescue Rugged::SshError, Rugged::NetworkError => e
  raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace)
end

#dirty?(exclude_spec = true) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/r10k/git/rugged/working_repository.rb', line 122

def dirty?(exclude_spec=true)
  with_repo do |repo|
    if exclude_spec
      diff = repo.diff_workdir('HEAD').select { |d| ! d.delta.old_file[:path].start_with?('spec/') }
    else
      diff = repo.diff_workdir('HEAD').to_a
    end

    diff.each do |p|
      logger.debug(_("Found local modifications in %{file_path}" % {file_path: File.join(@path, p.delta.old_file[:path])}))
      logger.debug1(p.to_s)
    end

    return diff.size > 0
  end
end

#exist?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/r10k/git/rugged/working_repository.rb', line 106

def exist?
  @path.exist?
end

#fetch(remote_name = 'origin') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/r10k/git/rugged/working_repository.rb', line 86

def fetch(remote_name = 'origin')
  logger.debug1 { _("Fetching remote '%{remote}' at %{path}") % {remote: remote_name, path: @path} }

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

  options = {:credentials => credentials, :proxy_url => proxy}
  refspecs = ["+refs/heads/*:refs/remotes/#{remote_name}/*"]

  results = nil

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

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

#git_dirPathname

Returns The path to the Git repository inside of this directory.

Returns:

  • (Pathname)

    The path to the Git repository inside of this directory



8
9
10
# File 'lib/r10k/git/rugged/working_repository.rb', line 8

def git_dir
  @path + '.git'
end

#headObject



110
111
112
# File 'lib/r10k/git/rugged/working_repository.rb', line 110

def head
  resolve('HEAD')
end

#originObject



118
119
120
# File 'lib/r10k/git/rugged/working_repository.rb', line 118

def origin
  with_repo { |repo| repo.config['remote.origin.url'] }
end