@param basedir [String] The base directory of the Git repository @param dirname [String] The directory name of the Git repository
# File lib/r10k/git/rugged/working_repository.rb, line 14 def initialize(basedir, dirname) @path = Pathname.new(File.join(basedir, dirname)) end
# File lib/r10k/git/rugged/working_repository.rb, line 112 def alternates R10K::Git::Alternates.new(git_dir) end
Check out the given Git ref
@param ref [String] The git reference to check out @return [void]
# 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 this git repository
@param remote [String] The Git remote to clone @param opts [Hash]
@options opts [String] :ref The git ref to check out on clone @options opts [String] :reference A Git repository to use as an alternate object database
@return [void]
# File lib/r10k/git/rugged/working_repository.rb, line 27 def clone(remote, opts = {}) logger.debug1 { _("Cloning '%{remote}' into %{path}") % {remote: remote, path: @path } } # 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} options.merge!(:alternates => [File.join(opts[:reference], 'objects')]) if opts[:reference] proxy = R10K::Git.get_proxy_for_remote(remote) 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
# File lib/r10k/git/rugged/working_repository.rb, line 120 def dirty? with_repo { |repo| repo.diff_workdir('HEAD').size > 0 } end
# File lib/r10k/git/rugged/working_repository.rb, line 104 def exist? @path.exist? end
# 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} } options = {:credentials => credentials} refspecs = ["+refs/heads/*:refs/remotes/#{remote_name}/*"] 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) rescue Rugged::SshError, Rugged::NetworkError => e raise R10K::Git::GitError.new(e.message, :git_dir => git_dir, :backtrace => e.backtrace) end
@return [Pathname] The path to the Git repository inside of this directory
# File lib/r10k/git/rugged/working_repository.rb, line 8 def git_dir @path + '.git' end
# File lib/r10k/git/rugged/working_repository.rb, line 108 def head resolve('HEAD') end
# File lib/r10k/git/rugged/working_repository.rb, line 116 def origin with_repo { |repo| repo.config['remote.origin.url'] } end
# File lib/r10k/git/rugged/working_repository.rb, line 133 def setup_rugged_repo @_rugged_repo = ::Rugged::Repository.new(@path.to_s, :alternates => alternates.to_a) end
# File lib/r10k/git/rugged/working_repository.rb, line 126 def with_repo if @_rugged_repo.nil? && git_dir.exist? setup_rugged_repo end super end