diff options
| author | Jack Nagel | 2012-01-05 16:15:33 -0600 |
|---|---|---|
| committer | Jack Nagel | 2012-01-06 14:51:15 -0600 |
| commit | 016a508c7a8a1456a52a32ad4330d811d1a043aa (patch) | |
| tree | 7156e0b2144ec711757579a59256657126ff6ea3 /Library | |
| parent | 3b29fbdbbab3923fda64272565fc377ebff8e19e (diff) | |
| download | brew-016a508c7a8a1456a52a32ad4330d811d1a043aa.tar.bz2 | |
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>
Diffstat (limited to 'Library')
| -rw-r--r-- | Library/Homebrew/cmd/versions.rb | 84 |
1 files changed, 38 insertions, 46 deletions
diff --git a/Library/Homebrew/cmd/versions.rb b/Library/Homebrew/cmd/versions.rb index 5283d9cb3..bd234d843 100644 --- a/Library/Homebrew/cmd/versions.rb +++ b/Library/Homebrew/cmd/versions.rb @@ -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,49 +13,56 @@ 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 + return versions end - def sha_for_version version - revlist.find{ |sha| version == version_for(sha) } + def pretty_relative_path + if Pathname.pwd == HOMEBREW_REPOSITORY + "Library/Formula/#{name}.rb" + else + "#{HOMEBREW_REPOSITORY}/Library/Formula/#{name}.rb" + end 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` + private + def rev_list + HOMEBREW_REPOSITORY.cd do + `git rev-list --abbrev-commit HEAD -- Library/Formula/#{name}.rb`.split + end 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? + def text_from_sha sha + HOMEBREW_REPOSITORY.cd do + `git cat-file blob #{sha}:Library/Formula/#{name}.rb` + end 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}." - end + def sha_for_version version + rev_list.find{ |sha| version == version_for_sha(sha) } + end - def pretty_relative_path - if Pathname.pwd == HOMEBREW_REPOSITORY - "Library/Formula/#{name}.rb" - else - "#{HOMEBREW_REPOSITORY}/Library/Formula/#{name}.rb" + 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 end |
