Permalink
Browse files

Added methods to retrieve code and metadata separately for Blame. Ver…

…y handy for code highlighting.
  • Loading branch information...
1 parent 95edef5 commit 3f6a3f425feaf39c0cb2e888b6581cf56cd8788a @isabanin committed Mar 16, 2012
Showing with 35 additions and 4 deletions.
  1. +1 −1 VERSION
  2. +1 −1 lib/mercurial-ruby.rb
  3. +20 −1 lib/mercurial-ruby/blame.rb
  4. +1 −1 mercurial-ruby.gemspec
  5. +12 −0 test/test_blame.rb
View
@@ -1 +1 @@
-0.7.4
+0.7.5
View
@@ -6,7 +6,7 @@
#
module Mercurial
- VERSION = '0.7.4'
+ VERSION = '0.7.5'
class Error < RuntimeError; end
@@ -8,6 +8,9 @@ module Mercurial
# look documentation for {Mercurial::BlameFactory BlameFactory}.
#
class Blame
+
+ METADATA_RE = /^(.+) (\w{12}): *(\d+): /
+ METADATA_AND_CODE_RE = /^(.+) (\w{12}): *(\d+): (.*)$/
attr_reader :repository
attr_reader :contents
@@ -18,12 +21,28 @@ def initialize(repository, data)
end
#
+ # Returns code only as a String without the usual blame metadata.
+ # Useful for code highlighting.
+ #
+ def contents_without_metadata
+ contents.gsub(METADATA_RE, '')
+ end
+
+ #
+ # Returns an Array of blame metadata for every line of blame.
+ # Does not return code itself.
+ #
+ def raw_metadata
+ contents.scan(METADATA_RE)
+ end
+
+ #
# Returns an array of {Mercurial::BlameLine BlameLine} instances.
#
def lines
[].tap do |result|
contents.each do |line|
- author, revision, linenum, text = line.scan(/^(.+) (\w{12}): *(\d+): (.*)$/).first
+ author, revision, linenum, text = line.scan(METADATA_AND_CODE_RE).first
result << BlameLine.new(
:author => author,
:revision => revision,
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{mercurial-ruby}
- s.version = "0.7.4"
+ s.version = "0.7.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ilya Sabanin"]
View
@@ -24,5 +24,17 @@
lines[3].num.must_equal 4
lines[3].contents.must_equal ''
end
+
+ it "should return metadata separately from code" do
+ blame = @factory.for_path('diff-test.rb')
+ blame.raw_metadata.size.must_equal 26
+ blame.raw_metadata[6].must_equal(['ilya', '88b5cc786015', '7'])
+ end
+
+ it "should return code separately from metadata" do
+ blame = @factory.for_path('diff-test.rb')
+ blame.contents_without_metadata.split("\n").size.must_equal 26
+ blame.contents_without_metadata.split("\n")[12].must_equal " helper_method :repository_scope?"
+ end
end

0 comments on commit 3f6a3f4

Please sign in to comment.