class GitRev::Sha
Public Class Methods
new(options={})
click to toggle source
# File lib/git_rev/sha.rb, line 4 def initialize(options={}) repository = options[:repository] || Dir.pwd cache = options.has_key?(:cache) ? options[:cache] : true @repository=File.expand_path(repository) @cache = cache ensure_repository! end
Public Instance Methods
full()
click to toggle source
# File lib/git_rev/sha.rb, line 12 def full cached? && @revision || load_revision end
short()
click to toggle source
# File lib/git_rev/sha.rb, line 16 def short full[0, 7] end
Private Instance Methods
cached?()
click to toggle source
# File lib/git_rev/sha.rb, line 22 def cached? !!@cache end
ensure_repository!()
click to toggle source
# File lib/git_rev/sha.rb, line 34 def ensure_repository! raise "Not a git repository #{@repository}" unless git_repository? end
git_directory()
click to toggle source
# File lib/git_rev/sha.rb, line 30 def git_directory File.join(@repository, '.git') end
git_repository?()
click to toggle source
# File lib/git_rev/sha.rb, line 26 def git_repository? File.directory?(git_directory) end
load_revision()
click to toggle source
# File lib/git_rev/sha.rb, line 38 def load_revision head=File.read(File.join(git_directory, 'HEAD')).chomp # Detached head if sha1?(head) @revision = head else @revision = ref(head) end end
ref(head)
click to toggle source
# File lib/git_rev/sha.rb, line 52 def ref(head) ref = head.scan(/^ref:\s+(.*)$/).first File.read(File.join(git_directory, ref)).chomp end
sha1?(subject)
click to toggle source
# File lib/git_rev/sha.rb, line 48 def sha1?(subject) /^[0-9a-f]{40}$/ =~ subject end