Skip to content

Commit

Permalink
Remove duplicates without converting to Hash
Browse files Browse the repository at this point in the history
Otherwise tests will fail randomly
  • Loading branch information
isabanin committed Mar 27, 2013
1 parent 1d6f8e0 commit 3882121
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
44 changes: 21 additions & 23 deletions lib/mercurial-ruby/factories/node_factory.rb
Expand Up @@ -64,30 +64,28 @@ def find!(path, revision=nil)
#
def entries_for(path, revision=nil, parent=nil)
revision ||= 'tip'
entries = []
manifest_entries = repository.manifest.scan_for_path(path, revision)
manifest_entries.each do |me|
path_without_source = me[3].gsub(/^#{ Regexp.escape(path.without_trailing_slash) }\//, '')
entry_name = path_without_source.split('/').first
entry_path = File.join(path, entry_name).gsub(/^\//, '')
dir = me[3].scan(/^(#{ Regexp.escape(entry_path) }\/)/).flatten.first ? true : false
entry_name << '/' if dir

entries << build(
:path => entry_path,
:name => entry_name,
:revision => revision,
:nodeid => (me[0] unless dir),
:fmode => dir ? nil : me[1],
:exec => dir ? nil : me[2],
:parent => parent
)
[].tap do |entries|
manifest_entries = repository.manifest.scan_for_path(path, revision)
manifest_entries.each do |me|
path_without_source = me[3].gsub(/^#{ Regexp.escape(path.without_trailing_slash) }\//, '')
entry_name = path_without_source.split('/').first
entry_path = File.join(path, entry_name).gsub(/^\//, '')
dir = me[3].scan(/^(#{ Regexp.escape(entry_path) }\/)/).flatten.first ? true : false
entry_name << '/' if dir

if entries.select{|item| item.name == entry_name}.size == 0
entries << build(
:path => entry_path,
:name => entry_name,
:revision => revision,
:nodeid => (me[0] unless dir),
:fmode => dir ? nil : me[1],
:exec => dir ? nil : me[2],
:parent => parent
)
end
end
end

entries = entries.inject({}) do |hash,item|
hash[item.name] ||= item
hash
end.values
end

private
Expand Down
10 changes: 5 additions & 5 deletions test/test_node_factory.rb
Expand Up @@ -67,14 +67,14 @@
entries.map(&:name).sort.must_equal %w(another-boring-file something.csv subdirectory/).sort
entries.map(&:parent).uniq.must_equal [node]

entries[0].directory?.must_equal true
entries[0].file?.must_equal false
entries[0].file?.must_equal true
entries[0].directory?.must_equal false

entries[1].file?.must_equal true
entries[1].directory?.must_equal false

entries[2].file?.must_equal true
entries[2].directory?.must_equal false
entries[2].directory?.must_equal true
entries[2].file?.must_equal false
end

it "should find entries for tip revision" do
Expand Down

0 comments on commit 3882121

Please sign in to comment.