class Librarian::Source::Git::Repository
Attributes
environment[RW]
git_ops_history[RW]
path[RW]
Public Class Methods
bin()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 19 def bin @bin ||= Posix.which!("git") end
clone!(environment, path, repository_url)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 11 def clone!(environment, path, repository_url) path = Pathname.new(path) path.mkpath git = new(environment, path) git.clone!(repository_url) git end
git_version()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 23 def git_version command = %W[#{bin} version] Posix.run!(command).strip =~ /\Agit version (\d+(\.\d+)*)/ && $1 end
new(environment, path)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 32 def initialize(environment, path) self.environment = environment self.path = Pathname.new(path) self.git_ops_history = [] end
Public Instance Methods
checked_out?(sha)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 84 def checked_out?(sha) current_commit_hash == sha end
checkout!(reference, options ={ })
click to toggle source
# File lib/librarian/source/git/repository.rb, line 52 def checkout!(reference, options ={ }) command = %W(checkout #{reference}) command << '--quiet' unless environment.ui.debug? command << "--force" if options[:force] run!(command, :chdir => true) end
clean!()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 72 def clean! command = %w(clean -x -d --force --force) run!(command, :chdir => true) end
clone!(repository_url)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 46 def clone!(repository_url) command = %W(clone #{repository_url} .) command << '--quiet' unless environment.ui.debug? run!(command, :chdir => true) end
current_commit_hash()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 119 def current_commit_hash command = %W(rev-parse HEAD) command << '--quiet' unless environment.ui.debug? run!(command, :chdir => true).strip! end
default_remote()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 42 def default_remote "origin" end
fetch!(remote, options = { })
click to toggle source
# File lib/librarian/source/git/repository.rb, line 59 def fetch!(remote, options = { }) command = %W(fetch #{remote}) command << '--quiet' unless environment.ui.debug? command << "--tags" if options[:tags] run!(command, :chdir => true) end
git?()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 38 def git? path.join('.git').exist? end
has_commit?(sha)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 77 def has_commit?(sha) command = %W(log -1 --no-color --format=tformat:%H #{sha}) run!(command, :chdir => true).strip == sha rescue Posix::CommandFailure => e false end
hash_from(remote, reference)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 108 def hash_from(remote, reference) branch_names = remote_branch_names[remote] if branch_names.include?(reference) reference = "#{remote}/#{reference}" end command = %W(rev-parse #{reference}^{commit}) command << '--quiet' unless environment.ui.debug? run!(command, :chdir => true).strip end
remote_branch_names()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 93 def remote_branch_names remotes = remote_names.sort_by(&:length).reverse command = %W(branch -r --no-color) names = run!(command, :chdir => true).strip.lines.map(&:strip).to_a names.each{|n| n.gsub!(/\s*->.*$/, "")} names.reject!{|n| n =~ /\/HEAD$/} Hash[remotes.map do |r| matching_names = names.select{|n| n.start_with?("#{r}/")} matching_names.each{|n| names.delete(n)} matching_names.each{|n| n.slice!(0, r.size + 1)} [r, matching_names] end] end
remote_names()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 88 def remote_names command = %W(remote) run!(command, :chdir => true).strip.lines.map(&:strip) end
reset_hard!()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 66 def reset_hard! command = %W(reset --hard) command << '--quiet' unless environment.ui.debug? run!(command, :chdir => true) end
Private Instance Methods
bin()
click to toggle source
# File lib/librarian/source/git/repository.rb, line 127 def bin self.class.bin end
debug(*args, &block)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 188 def debug(*args, &block) environment.logger.debug(*args, &block) end
logging_command(command, options) { || ... }
click to toggle source
# File lib/librarian/source/git/repository.rb, line 148 def logging_command(command, options) silent = options.delete(:silent) pwd = Dir.pwd out = yield git_ops_history << command + [{:pwd => pwd}] unless silent if out.size > 0 out.lines.each do |line| debug { " --> #{line}" } end else debug { " --- No output" } end end out rescue Posix::CommandFailure => e git_ops_history << command + [{:pwd => pwd}] status, stderr = e.status, e.message unless silent debug { " --- Exited with #{status}" } if stderr.size > 0 stderr.lines.each do |line| debug { " --> #{line}" } end else debug { " --- No output" } end end raise e end
relative_path_to(path)
click to toggle source
# File lib/librarian/source/git/repository.rb, line 192 def relative_path_to(path) environment.logger.relative_path_to(path) end
run!(args, options = { })
click to toggle source
# File lib/librarian/source/git/repository.rb, line 131 def run!(args, options = { }) chdir = options.delete(:chdir) chdir = path.to_s if chdir == true silent = options.delete(:silent) pwd = chdir || Dir.pwd git_dir = File.join(path, ".git") if path env = {"GIT_DIR" => git_dir} command = [bin] command.concat(args) logging_command(command, :silent => silent, :pwd => pwd) do Posix.run!(command, :chdir => chdir, :env => env) end end