Skip to content

Commit 4dcb7a6

Browse files
committed
OTWO-1048 - Captures committer/author name/email correctly.
1 parent 5796ef1 commit 4dcb7a6

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

lib/scm/parsers/bzr_xml_parser.rb

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ def tag_end(name)
4646
when 'message'
4747
@commit.message = @cdata
4848
when 'committer'
49-
@commit.committer_name = @text[/(.+?)(\s+<(.+)>)/, 1]
50-
@commit.committer_email = @text[/(.+?)(\s+<(.+)>)/, 3]
49+
committer = BzrXmlParser.capture_name(@text)
50+
@commit.committer_name = committer[0]
51+
@commit.committer_email = committer[1]
5152
when 'author'
52-
@authors << {:author_name => @text[/(.+?)(\s+<(.+)>)/, 1], :author_email => @text[/(.+?)(\s+<(.+)>)/, 3] }
53+
author = BzrXmlParser.capture_name(@text)
54+
@authors << {:author_name => author[0], :author_email => author[1]}
5355
when 'timestamp'
5456
@commit.committer_date = Time.parse(@text)
5557
when 'file'
@@ -111,22 +113,12 @@ def strip_trailing_asterisk(path)
111113

112114
def remove_dupes(diffs)
113115
BzrXmlParser.remove_dupes(diffs)
114-
# Bazaar may report that a file was added and modified in a single commit.
115-
# Reduce these cases to a single 'A' action.
116-
#diffs.delete_if do |d|
117-
# d.action == 'M' && diffs.select { |x| x.path == d.path && x.action == 'A' }.any?
118-
#end
119-
120-
# Bazaar may report that a file was both deleted and added in a single commit.
121-
# Reduce these cases to a single 'M' action.
122-
#diffs.each do |d|
123-
# d.action = 'M' if diffs.select { |x| x.path == d.path }.size > 1
124-
#end.uniq
125116
end
126117

127118
end
128119

129120
class BzrXmlParser < Parser
121+
NAME_REGEX = /^(.+?)(\s+<(.+)>\s*)?$/
130122
def self.internal_parse(buffer, opts)
131123
buffer = '<?xml?>' if buffer.is_a?(StringIO) and buffer.length < 2
132124
begin
@@ -152,5 +144,16 @@ def self.remove_dupes(diffs)
152144
d.action = 'M' if diffs.select { |x| x.path == d.path }.size > 1
153145
end.uniq
154146
end
147+
148+
# Bazaar expects committer/author to be specified in this format
149+
# Name <email>, or John Doe <jdoe@example.com>
150+
# However, we find many variations in the real world including
151+
# ones where only email is specified as name.
152+
def self.capture_name(text)
153+
parts = text.match(NAME_REGEX).to_a
154+
name = parts[1] || parts[0]
155+
email = parts[3]
156+
[name, email]
157+
end
155158
end
156159
end

test/unit/bzr_xml_parser_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,48 @@ def test_strip_trailing_asterisk_from_executables
362362
assert_equal 'D', diffs[1].action
363363
assert_equal 'arch', diffs[1].path
364364
end
365+
366+
def test_committer_name_capture
367+
name, email = BzrXmlParser.capture_name('John')
368+
assert_equal name, 'John'
369+
assert_equal email, nil
370+
assert_equal ['John Doe', nil], BzrXmlParser.capture_name('John Doe')
371+
assert_equal ['John Doe jdoe@example.com', nil], BzrXmlParser.capture_name('John Doe jdoe@example.com')
372+
assert_equal ['John Doe <jdoe@example.com', nil], BzrXmlParser.capture_name('John Doe <jdoe@example.com')
373+
assert_equal ['John Doe jdoe@example.com>', nil], BzrXmlParser.capture_name('John Doe jdoe@example.com>')
374+
assert_equal ['John Doe', 'jdoe@example.com'], BzrXmlParser.capture_name('John Doe <jdoe@example.com>')
375+
assert_equal ['John Doe', 'jdoe@example.com'], BzrXmlParser.capture_name('John Doe <jdoe@example.com> ')
376+
assert_equal ['jdoe@example.com', nil], BzrXmlParser.capture_name('jdoe@example.com')
377+
xml = <<-XML
378+
<logs>
379+
<log>
380+
<revno>15</revno>
381+
<revisionid>a@b.com-20111013152207-q8uec9pp1330gfbh</revisionid>
382+
<parents>
383+
<parent>test@example.com-20111012195747-seei62z2wmefjhmo</parent>
384+
</parents>
385+
<committer>a@b.com</committer>
386+
<authors>
387+
<author>author@c.com</author>
388+
</authors>
389+
<branch-nick>myproject</branch-nick>
390+
<timestamp>Thu 2011-10-13 11:22:07 -0400</timestamp>
391+
<message><![CDATA[Updated with only email as committer name.]]></message>
392+
<affected-files>
393+
<modified>
394+
<file fid="test3.txt-20110722163813-257mjqqrvw3mav0f-4">test_a.txt</file>
395+
</modified>
396+
</affected-files>
397+
</log>
398+
</logs>
399+
XML
400+
c = BzrXmlParser.parse(xml).first
401+
assert_equal 'M', c.diffs.first.action
402+
assert_equal 'test_a.txt', c.diffs.first.path
403+
assert_equal 'a@b.com', c.committer_name
404+
assert_equal nil, c.committer_email
405+
assert_equal 'author@c.com', c.author_name
406+
assert_equal nil, c.author_email
407+
end
365408
end
366409
end

0 commit comments

Comments
 (0)