versions: determine version from a Formula instance

Rather than regex the output of `git show <rev>:<path>`, we write the
output of `git cat-file blog <rev>:<path>` to a temporary file, and then
operate on it just as we would a normal formula.

I haven't observed any speed difference. `git cat-file blob` is faster
than `git show`, but the "slow part" is still the git-rev-list
invocation. But really it's pretty fast overall.

Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
Jack Nagel 2012-01-05 16:15:33 -06:00
parent 3b29fbdbba
commit 016a508c7a

View File

@ -1,26 +1,11 @@
require 'formula'
module Homebrew extend self
module Versions
# yields version, sha for all versions in the git history
def self.old_versions f
yielded = []
f.rev_list.each do |sha|
version = f.version_for_sha sha
unless yielded.include? version or version.nil?
yield version, sha
yielded << version
end
end
end
end
def versions
raise "Please `brew install git` first" unless system "/usr/bin/which -s git"
ARGV.formulae.all? do |f|
old_versions = Versions.old_versions f do |version, sha|
f.versions do |version, sha|
print Tty.white
print "#{version.ljust(8)} "
print Tty.reset
@ -28,42 +13,20 @@ module Homebrew extend self
end
end
end
end
class Formula
def rev_list
Dir.chdir HOMEBREW_REPOSITORY do
`git rev-list --abbrev-commit HEAD Library/Formula/#{name}.rb`.split
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
end
end
def sha_for_version version
revlist.find{ |sha| version == version_for(sha) }
end
def version_for_sha sha
# TODO really we should open a new ruby instance and parse the formula
# class and then get the version but this would be too slow (?)
code = Dir.chdir(HOMEBREW_REPOSITORY) do
`git show #{sha}:Library/Formula/#{name}.rb`
end
version = code.match(/class #{Formula.class_s name} < ?Formula.*?(?:version\s|@version\s*=)\s*(?:'|")(.+?)(?:'|").*?end\s/m)
return version[1] unless version.nil?
url = code.match(/class #{Formula.class_s name} < ?Formula.*?(?:url\s|@url\s*=)\s*(?:'|")(.+?)(?:'|").*?end\s/m)
unless url.nil?
version = Pathname.new(url[1]).version
return version unless version.to_s.empty?
end
head = code.match(/class #{Formula.class_s name} < ?Formula.*?head\s'(.*?)'.*?end\s\s/m)
return 'HEAD' unless head.nil?
opoo "Version of #{name} could not be determined for #{sha}."
return versions
end
def pretty_relative_path
@ -73,4 +36,33 @@ class Formula
"#{HOMEBREW_REPOSITORY}/Library/Formula/#{name}.rb"
end
end
private
def rev_list
HOMEBREW_REPOSITORY.cd do
`git rev-list --abbrev-commit HEAD -- Library/Formula/#{name}.rb`.split
end
end
def text_from_sha sha
HOMEBREW_REPOSITORY.cd do
`git cat-file blob #{sha}:Library/Formula/#{name}.rb`
end
end
def sha_for_version version
rev_list.find{ |sha| version == version_for_sha(sha) }
end
def version_for_sha sha
begin
version = mktemp do
path = Pathname.new(Pathname.pwd+"#{name}.rb")
path.write text_from_sha(sha)
Formula.factory(path).version
end
rescue
opoo "Version of #{name} could not be determined for #{sha}."
end
end
end