From 0774e353b9e478fd749e62e00d59a59e0ec6d2f9 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 25 Feb 2010 20:35:12 -0800 Subject: [PATCH] Fix a bug with Haml::Version reporting git revisions. --- doc-src/HAML_CHANGELOG.md | 6 ++++++ doc-src/SASS_CHANGELOG.md | 6 ++++++ lib/haml/version.rb | 43 ++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md index 6731398d..70a38cc2 100644 --- a/doc-src/HAML_CHANGELOG.md +++ b/doc-src/HAML_CHANGELOG.md @@ -3,6 +3,12 @@ * Table of contents {:toc} +## 2.2.21 (Unreleased) + +* Fix a few bugs in the git-revision-reporting in {Haml::Version#version}. + In particular, it will still work if `git gc` has been called recently, + or if various files are missing. + ## 2.2.20 [Tagged on GitHub](http://github.com/nex3/haml/commit/2.2.20). diff --git a/doc-src/SASS_CHANGELOG.md b/doc-src/SASS_CHANGELOG.md index a54ac33d..00afc777 100644 --- a/doc-src/SASS_CHANGELOG.md +++ b/doc-src/SASS_CHANGELOG.md @@ -3,6 +3,12 @@ * Table of contents {:toc} +## 2.2.21 (Unreleased) + +* Fix a few bugs in the git-revision-reporting in {Haml::Version#version}. + In particular, it will still work if `git gc` has been called recently, + or if various files are missing. + ## 2.2.20 [Tagged on GitHub](http://github.com/nex3/haml/commit/2.2.20). diff --git a/lib/haml/version.rb b/lib/haml/version.rb index de0b0017..ff387734 100644 --- a/lib/haml/version.rb +++ b/lib/haml/version.rb @@ -38,27 +38,42 @@ module Haml @@version[:number] = [:major, :minor, :teeny].map { |comp| @@version[comp] }.compact.join('.') @@version[:string] = @@version[:number].dup - if File.exists?(scope('REVISION')) - rev = File.read(scope('REVISION')).strip - rev = nil if rev !~ /^([a-f0-9]+|\(.*\))$/ - end - - if (rev.nil? || rev == '(unknown)') && File.exists?(scope('.git/HEAD')) - rev = File.read(scope('.git/HEAD')).strip - if rev =~ /^ref: (.*)$/ - rev = File.read(scope(".git/#{$1}")).strip - end - end - - if rev + if rev = revision_number @@version[:rev] = rev unless rev[0] == ?( @@version[:string] << "." << rev[0...7] end - @@version[:string] << " (#{name})" end + @@version[:string] << " (#{name})" @@version end + + private + + def revision_number + if File.exists?(scope('REVISION')) + rev = File.read(scope('REVISION')).strip + return rev unless rev =~ /^([a-f0-9]+|\(.*\))$/ || rev == '(unknown)' + end + + return unless File.exists?(scope('.git/HEAD')) + rev = File.read(scope('.git/HEAD')).strip + return rev unless rev =~ /^ref: (.*)$/ + + ref_name = $1 + ref_file = scope(".git/#{ref_name}") + info_file = scope(".git/info/refs") + return File.read(ref_file).strip if File.exists?(ref_file) + return unless File.exists?(info_file) + File.open(info_file) do |f| + f.each do |l| + sha, ref = l.strip.split("\t", 2) + next unless ref == ref_name + return sha + end + end + return nil + end end end