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 --silent]
  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 80
def checked_out?(sha)
  current_commit_hash == sha
end
checkout!(reference, options ={ }) click to toggle source
# File lib/librarian/source/git/repository.rb, line 51
def checkout!(reference, options ={ })
  command = %W(checkout #{reference} --quiet)
  command << "--force" if options[:force]
  run!(command, :chdir => true)
end
clean!() click to toggle source
# File lib/librarian/source/git/repository.rb, line 68
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} . --quiet)
  run!(command, :chdir => true)
end
current_commit_hash() click to toggle source
# File lib/librarian/source/git/repository.rb, line 114
def current_commit_hash
  command = %W(rev-parse HEAD --quiet)
  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 57
def fetch!(remote, options = { })
  command = %W(fetch #{remote} --quiet)
  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 73
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 104
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} --quiet)
  run!(command, :chdir => true).strip
end
remote_branch_names() click to toggle source
# File lib/librarian/source/git/repository.rb, line 89
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 84
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 63
def reset_hard!
  command = %W(reset --hard --quiet)
  run!(command, :chdir => true)
end

Private Instance Methods

bin() click to toggle source
# File lib/librarian/source/git/repository.rb, line 121
def bin
  self.class.bin
end
debug(*args, &block) click to toggle source
# File lib/librarian/source/git/repository.rb, line 182
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 142
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 186
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 125
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