Class: Grit::Submodule

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/submodule.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


4
5
6
# File 'lib/grit/submodule.rb', line 4

def id
  @id
end

#modeObject (readonly)

Returns the value of attribute mode


5
6
7
# File 'lib/grit/submodule.rb', line 5

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name


6
7
8
# File 'lib/grit/submodule.rb', line 6

def name
  @name
end

Class Method Details

.config(repo, ref = "master") ⇒ Object

The configuration information for the given repo

+repo+ is the Repo
+ref+ is the committish (defaults to 'master')

Returns a Hash of { <path:String> => { 'url' => <url:String>, 'id' => <id:String> } } Returns {} if no .gitmodules file was found


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/grit/submodule.rb', line 52

def self.config(repo, ref = "master")
  commit = repo.commit(ref)
  blob = commit.tree/'.gitmodules'
  return {} unless blob

  lines = blob.data.gsub(/\r\n?/, "\n" ).split("\n")

  config = {}
  current = nil

  lines.each do |line|
    if line =~ /^\[submodule "(.+)"\]$/
      current = $1
      config[current] = {}
      config[current]['id'] = (commit.tree/current).id
    elsif line =~ /^\t(\w+) = (.+)$/
      config[current][$1] = $2
      config[current]['id'] = (commit.tree/$2).id if $1 == 'path'
    else
      # ignore
    end
  end

  config
end

.create(repo, atts) ⇒ Object

Create a Submodule containing just the specified attributes

+repo+ is the Repo
+atts+ is a Hash of instance variable data

Returns Grit::Submodule (unbaked)


13
14
15
# File 'lib/grit/submodule.rb', line 13

def self.create(repo, atts)
  self.allocate.create_initialize(repo, atts)
end

Instance Method Details

#basenameObject


78
79
80
# File 'lib/grit/submodule.rb', line 78

def basename
  File.basename(name)
end

#create_initialize(repo, atts) ⇒ Object

Initializer for Submodule.create

+repo+ is the Repo
+atts+ is a Hash of instance variable data

Returns Grit::Submodule


22
23
24
25
26
27
28
# File 'lib/grit/submodule.rb', line 22

def create_initialize(repo, atts)
  @repo = repo
  atts.each do |k, v|
    instance_variable_set("@#{k}".to_sym, v)
  end
  self
end

#inspectObject

Pretty object inspection


83
84
85
# File 'lib/grit/submodule.rb', line 83

def inspect
  %Q{#<Grit::Submodule "#{@id}">}
end

#url(ref) ⇒ Object

The url of this submodule

+ref+ is the committish that should be used to look up the url

Returns String


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grit/submodule.rb', line 34

def url(ref)
  config = self.class.config(@repo, ref)

  lookup = config.keys.inject({}) do |acc, key|
    id = config[key]['id']
    acc[id] = config[key]['url']
    acc
  end

  lookup[@id]
end