summaryrefslogtreecommitdiff
path: root/lib/gitlab/git/gitmodules_parser.rb
blob: 4a43b9b444d41c0115cf0a8c157daf519d006792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
77
78
79
# Gitaly note: JV: no RPC's here.

module Gitlab
  module Git
    class GitmodulesParser
      def initialize(content)
        @content = content
      end

      # Parses the contents of a .gitmodules file and returns a hash of
      # submodule information, indexed by path.
      def parse
        reindex_by_path(get_submodules_by_name)
      end

      private

      class State
        def initialize
          @result = {}
          @current_submodule = nil
        end

        def start_section(section)
          # In some .gitmodules files (e.g. nodegit's), a header
          # with the same name appears multiple times; we want to
          # accumulate the configs across these
          @current_submodule = @result[section] || { 'name' => section }
          @result[section] = @current_submodule
        end

        def set_attribute(attr, value)
          @current_submodule[attr] = value
        end

        def section_started?
          !@current_submodule.nil?
        end

        def submodules_by_name
          @result
        end
      end

      def get_submodules_by_name
        iterator = State.new

        @content.split("\n").each_with_object(iterator) do |text, iterator|
          next if text =~ /^\s*#/

          if text =~ /\A\[submodule "(?<name>[^"]+)"\]\z/
            iterator.start_section($~[:name])
          else
            next unless iterator.section_started?

            next unless text =~ /\A\s*(?<key>\w+)\s*=\s*(?<value>.*)\z/

            value = $~[:value].chomp
            iterator.set_attribute($~[:key], value)
          end
        end

        iterator.submodules_by_name
      end

      def reindex_by_path(submodules_by_name)
        # Convert from an indexed by name to an array indexed by path
        # If a submodule doesn't have a path, it is considered bogus
        # and is ignored
        submodules_by_name.each_with_object({}) do |(name, data), results|
          path = data.delete 'path'
          next unless path

          results[path] = data
        end
      end
    end
  end
end