Class: Mercurial::NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/mercurial-ruby/factories/node_factory.rb

Overview

This class represents a factory for Node instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ NodeFactory

Returns a new instance of NodeFactory.



12
13
14
# File 'lib/mercurial-ruby/factories/node_factory.rb', line 12

def initialize(repository)
  @repository = repository
end

Instance Attribute Details

#repositoryObject (readonly)

Instance of a Repository.



10
11
12
# File 'lib/mercurial-ruby/factories/node_factory.rb', line 10

def repository
  @repository
end

Instance Method Details

#entries_for(path, revision = nil, parent = nil) ⇒ Object

Find all entries (files and directories) inside a specified path and revision. Returns an array of Node instances.

Will find node in the latest version of repo if revision is ommitted.

Example:

repository.nodes.entries_for('/')
repository.nodes.entries_for('some-fancy-directory/subdirectory/', '291a498f04e9')


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mercurial-ruby/factories/node_factory.rb', line 65

def entries_for(path, revision=nil, parent=nil)
  revision ||= 'tip'
  [].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
end

#find(path, revision = nil) ⇒ Object

Finds a specified file or a directory in the repository at a specified revision. Returns an instance of Node.

Will find node in the latest version of repo if revision is ommitted. Will return nil if node wasn’t found.

Example:

repository.nodes.find('/')
repository.nodes.find('some-fancy-directory/Weird File Name.pdf', '291a498f04e9')
repository.nodes.find('some-fancy-directory/subdirectory/', '291a498f04e9')


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mercurial-ruby/factories/node_factory.rb', line 27

def find(path, revision=nil)
  revision ||= 'tip'
  return RootNode.new(:repository => repository, :revision => revision) if path == '/'
  entry = repository.manifest.scan_for_path(path, revision).first
  return unless entry
  if exact_path = entry[3].scan(/^(#{ Regexp.escape(path.without_trailing_slash) }\/)/).flatten.first
    name = exact_path.split('/').last + '/'
    build(
      :path     => exact_path,
      :name     => name,
      :revision => revision
    )
  else
    build(
      :path     => entry[3],
      :name     => entry[3].split('/').last,
      :revision => revision,
      :nodeid   => entry[0],
      :fmode    => entry[1],
      :exec     => entry[2]
    )
  end
end

#find!(path, revision = nil) ⇒ Object

Same as find but will raise a NodeMissing exception if node wasn’t found.



52
53
54
# File 'lib/mercurial-ruby/factories/node_factory.rb', line 52

def find!(path, revision=nil)
  find(path, revision) || raise(NodeMissing, "#{ path } at revision #{ revision }")
end