brew/Library/Homebrew/cmd/versions.rb

117 lines
2.9 KiB
Ruby
Raw Normal View History

2010-10-27 10:03:30 +02:00
require 'formula'
module Homebrew extend self
def versions
raise "Please `brew install git` first" unless which "git"
2012-03-16 00:35:49 +00:00
raise "Please `brew update' first" unless (HOMEBREW_REPOSITORY/".git").directory?
2010-10-27 10:03:30 +02:00
raise FormulaUnspecifiedError if ARGV.named.empty?
ARGV.formulae.all? do |f|
if ARGV.include? '--compact'
puts f.versions * " "
else
f.versions do |version, sha|
print Tty.white.to_s
print "#{version.to_s.ljust(8)} "
print Tty.reset.to_s
puts "git checkout #{sha} #{f.pretty_relative_path}"
end
2010-10-27 10:03:30 +02:00
end
end
end
end
2010-10-27 10:03:30 +02:00
class Formula
def versions
versions = []
rev_list.each do |sha|
version = version_for_sha sha
unless versions.include? version or version.nil?
yield version, sha if block_given?
versions << version
end
2010-10-27 10:03:30 +02:00
end
return versions
2010-10-27 10:03:30 +02:00
end
def bottle_filenames branch='HEAD'
filenames = []
rev_list(branch).each do |sha|
filename = formula_for_sha(sha) {|f| bottle_filename f if f.bottle }
unless filenames.include? filename or filename.nil?
filenames << filename
end
end
return filenames
end
def pretty_relative_path
if Pathname.pwd == repository
entry_name
else
repository/"#{entry_name}"
end
end
2010-10-27 10:03:30 +02:00
private
def repository
@repository ||= begin
if path.realpath.to_s =~ %r{#{HOMEBREW_REPOSITORY}/Library/Taps/(\w+)-(\w+)}
HOMEBREW_REPOSITORY/"Library/Taps/#$1-#$2"
else
HOMEBREW_REPOSITORY
end
end
end
def entry_name
@entry_name ||= begin
repository == HOMEBREW_REPOSITORY ? "Library/Formula/#{name}.rb" : "#{name}.rb"
end
end
def rev_list branch='HEAD'
repository.cd do
`git rev-list --abbrev-commit #{branch} -- #{entry_name}`.split
end
end
2010-10-27 10:03:30 +02:00
def text_from_sha sha
repository.cd do
`git cat-file blob #{sha}:#{entry_name}`
end
end
2010-10-27 10:03:30 +02:00
def sha_for_version version
rev_list.find{ |sha| version == version_for_sha(sha) }
end
2010-10-27 10:03:30 +02:00
IGNORED_EXCEPTIONS = [SyntaxError, TypeError, NameError,
ArgumentError, FormulaSpecificationError]
def version_for_sha sha
formula_for_sha(sha) {|f| f.version }
end
def formula_for_sha sha, &block
mktemp do
path = Pathname.new(Pathname.pwd+"#{name}.rb")
path.write text_from_sha(sha)
# Unload the class so Formula#version returns the correct value
begin
2013-06-23 14:07:46 -07:00
Formulary.unload_formula name
nostdout { yield Formula.factory(path.to_s) }
rescue *IGNORED_EXCEPTIONS => e
# We rescue these so that we can skip bad versions and
# continue walking the history
2013-03-21 17:04:05 -05:00
ohai "#{e} in #{name} at revision #{sha}", e.backtrace if ARGV.debug?
rescue FormulaUnavailableError
# Suppress this error
end
end
2011-08-02 14:45:37 +01:00
end
2010-10-27 10:03:30 +02:00
end