Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jonmagic/grit
...
head fork: jonmagic/grit
  • 2 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
1  lib/grit.rb
@@ -47,6 +47,7 @@
require 'grit/submodule'
require 'grit/blame'
require 'grit/merge'
+require 'grit/rev_list_parser'
module Grit
VERSION = '2.5.0'
View
47 lib/grit/commit.rb
@@ -137,46 +137,15 @@ def self.find_all(repo, ref, options = {})
# - it broke when 'encoding' was introduced - not sure what else might show up
#
def self.list_from_string(repo, text)
- lines = text.split("\n")
-
- commits = []
-
- while !lines.empty?
- id = lines.shift.split.last
- tree = lines.shift.split.last
-
- parents = []
- parents << lines.shift.split.last while lines.first =~ /^parent/
-
- author_line = lines.shift
- author_line << lines.shift if lines[0] !~ /^committer /
- author, authored_date = self.actor(author_line)
-
- committer_line = lines.shift
- committer_line << lines.shift if lines[0] && lines[0] != '' && lines[0] !~ /^encoding/
- committer, committed_date = self.actor(committer_line)
-
- # not doing anything with this yet, but it's sometimes there
- encoding = lines.shift.split.last if lines.first =~ /^encoding/
-
- if lines[1] && lines[1][0] == ' ' && lines[1][1] && lines[1][1] != ' '
- lines.shift
- while lines[0] && lines[0][0] == ' '
- lines.shift
- end
- end
-
- lines.shift
-
- message_lines = []
- message_lines << lines.shift[4..-1] while lines.first =~ /^ {4}/
-
- lines.shift while lines.first && lines.first.empty?
-
- commits << Commit.new(repo, id, parents, tree, author, authored_date, committer, committed_date, message_lines)
- end
+ parser = RevListParser.new(text)
+ parser.entries.map { |entry| bake_from_parser(repo, entry) }
+ end
- commits
+ def self.bake_from_parser(repo, entry)
+ author, authored_date = actor('author ' + entry.author)
+ committer, committed_date = actor('committer ' + entry.committer)
+ new(repo, entry.commit, entry.parents, entry.tree, author, authored_date,
+ committer, committed_date, entry.message_lines)
end
# Show diffs between two trees.
View
81 lib/grit/rev_list_parser.rb
@@ -0,0 +1,81 @@
+module Grit
+ class RevListParser
+ class Entry
+ attr_reader :meta, :message_lines
+
+ def initialize
+ @done = false
+ @meta = {}
+ @message_lines = []
+ end
+
+ def commit
+ @meta[:commit].to_s
+ end
+
+ def parents
+ @meta[:parent] || []
+ end
+
+ def tree
+ @meta[:tree].to_s
+ end
+
+ def author
+ @meta[:author].to_s
+ end
+
+ def committer
+ @meta[:committer].to_s
+ end
+
+ def parse(line)
+ spaces = line.scan(/^ */).first
+ if spaces.size >= 4
+ parse_message(line)
+ else
+ parse_meta(line, spaces.size)
+ end
+ end
+
+ def parse_meta(line, spaces)
+ return if spaces > 0 || line.empty?
+ header, value = line.split(' ', 2)
+ values = @meta[header.to_sym] ||= []
+ values << value
+ end
+
+ def parse_message(line)
+ @message_lines << line[4..-1]
+ end
+
+ def message?
+ @message_lines.size > 0
+ end
+ end
+
+ attr_reader :entries
+
+ def initialize(text)
+ @entries = []
+ lines = text.split("\n")
+ entry = nil
+ while !lines.empty?
+ line = lines.shift
+ entry = current_entry(entry, line)
+ entry.parse(line)
+ end
+ end
+
+ def current_entry(entry, line)
+ if entry && entry.message? && line.empty?
+ entry = nil
+ end
+
+ if !entry
+ @entries << (entry = Entry.new)
+ end
+ entry
+ end
+ end
+end
View
11 test/test_commit.rb
@@ -194,7 +194,7 @@ def test_to_patch
end
# patch_id
-
+
def test_patch_id
@c = Commit.create(@r, :id => '80f136f500dfdb8c3e8abf4ae716f875f0a1b57f')
assert_equal '9450b04e4f83ad0067199c9e9e338197d1835cbb', @c.patch_id
@@ -238,4 +238,13 @@ def test_list_from_string
assert_equal 4, commits.size
end
+
+ def test_list_from_string_with_single_signed_commit
+ sha = '671d0b0a85af271395eb71ff91f942f54681b144'
+ repo = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git_signed_tag_merged]), :is_bare => true)
+ rev_list = repo.git.rev_list({:pretty => "raw", :max_count => 1}, sha)
+ commits = Commit.list_from_string(@r, rev_list)
+
+ assert_equal 1, commits.size
+ end
end
View
42 test/test_rev_list_parser.rb
@@ -0,0 +1,42 @@
+require File.dirname(__FILE__) + '/helper'
+
+class TestRevListParser < Test::Unit::TestCase
+ def setup
+ @r = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git_signed_tag_merged]), :is_bare => true)
+ end
+
+ def test_parsing_single_commit
+ sha = '671d0b0a85af271395eb71ff91f942f54681b144'
+ rev_list = @r.git.rev_list({:pretty => "raw", :max_count => 1}, sha)
+
+ parser = Grit::RevListParser.new(rev_list)
+ assert_equal 1, parser.entries.size
+ assert entry = parser.entries.first
+ assert_equal "Merge tag 'v1.1' into bar", entry.message_lines.first
+ assert_equal '671d0b0a85af271395eb71ff91f942f54681b144', entry.commit
+ assert_equal 'a9ac6c1e58bbdd7693e49ce34b32d9b0b53c0bcf', entry.tree
+ assert_equal [
+ 'dce37589cfa5748900d05ab07ee2af5010866838', 'b2b1760347d797f3dc79360d487b9afa7baafd6a'],
+ entry.parents
+
+ assert_match /^Jonathan /, entry.author
+ assert_match /^Jonathan /, entry.committer
+ assert_equal 'object b2b1760347d797f3dc79360d487b9afa7baafd6a', entry.meta[:mergetag].to_s
+ end
+
+ def test_parsing_multiple_commits
+ rev_list = @r.git.rev_list({:pretty => "raw", :all => true})
+
+ parser = Grit::RevListParser.new(rev_list)
+ assert_equal 4, parser.entries.size
+ shas = %w(671d0b0a85af271395eb71ff91f942f54681b144
+ dce37589cfa5748900d05ab07ee2af5010866838
+ b2b1760347d797f3dc79360d487b9afa7baafd6a
+ 2ae8b20538f5d358e97978632965efc380c42c9a)
+ shas.each_with_index do |sha, idx|
+ assert entry = parser.entries[idx], "no entry for commit #{idx+1}"
+ assert_equal sha, entry.commit, "different sha for commit #{idx+1}"
+ end
+ end
+end
+

No commit comments for this range

Something went wrong with that request. Please try again.