Module: GitFusion::ConfigParser

Includes:
Util
Included in:
App
Defined in:
lib/git_fusion/config_parser.rb

Constant Summary

REPO_MAPPING_NOT_MATCHING =
'Submitted repository configuration does not point to the chosen repository'
REPO_CONFIG_JSON_NOT_VALID =
'Submitted repository JSON configuration is not valid'

Constants included from Util

Util::DF_MATCH, Util::KEY_ALREADY_EXISTS, Util::PATCH_PARAMS_DONT_EXIST, Util::REPO_INITIALISED, Util::USER_NOT_MATCHING

Instance Method Summary (collapse)

Methods included from Util

#all_keys, #check_if_key_exists, #check_if_key_name_exists, #decode_from_uri, #encode_to_gf_format, error?, #error_and_clean_up, #get_by_key, #keys_for_user, #print_result, #submit_file, #temp_client, #to_message

Methods included from HelixVersioningEngine

invalid_user_email?

Instance Method Details

- (Object) branch_config_section(branch_name)



95
96
97
98
99
100
101
# File 'lib/git_fusion/config_parser.rb', line 95

def branch_config_section(branch_name)
  config = <<-EOC.gsub(/^\s*/, '')
    [#{branch_name}]
    git-branch-name = #{branch_name}
  EOC
  config
end

- (Object) config_line(repo, all_values, key, value)



103
104
105
106
107
108
109
110
111
112
# File 'lib/git_fusion/config_parser.rb', line 103

def config_line(repo, all_values, key, value)
  line = ''
  if key == 'depot_path'
    value = value.gsub(GitFusionStrings.decode(repo), repo)
    line = "view = #{value} #{all_values['client_path']}\n"
  elsif key != 'client_path'
    line = "#{key} = #{value}\n"
  end
  line
end

- (Object) deep_merge(merge_to, merge_from)



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/git_fusion/config_parser.rb', line 129

def deep_merge(merge_to, merge_from)
  merged = merge_to.clone
  merge_from.each do |key, value|
    next unless merged.keys.include?(key)
    if value.is_a?(Hash) && merged[key].is_a?(Hash)
      merged[key] = deep_merge(merged[key], value)
    else
      merged[key] = value
    end
  end
  merged
end

- (Object) get_branch_sections(repo, branches)



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/git_fusion/config_parser.rb', line 114

def get_branch_sections(repo, branches)
  config = ''
  branches.each do |branch, all_values|
    branch_name = all_values['git-branch-name'] || branch
    all_values.delete('git-branch-name')

    branch_config = branch_config_section(branch_name)
    all_values.each do |key, value|
      branch_config << config_line(repo, all_values, key, value)
    end
    config << "#{branch_config}\n"
  end
  config
end

- (Object) get_config_sections(config)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git_fusion/config_parser.rb', line 57

def get_config_sections(config)
  config = config.split('[')
  config.shift
  sections = []
  config.each do |section|
    section = section.split(']')
    values = section.last.lstrip.split("\n")
    section_values = {}
    values.each do |value|
      key, value = value.split('=')
      section_values[key.strip] = value.strip
    end
    sections << { section.first => section_values }
  end
  sections
end

- (Boolean) matching_branches?(ms_match, new_branches, old_branches)

Returns:

  • (Boolean)


142
143
144
145
# File 'lib/git_fusion/config_parser.rb', line 142

def matching_branches?(ms_match, new_branches, old_branches)
  matches = ms_match && (new_branches.keys - old_branches.keys).empty?
  fail P4Error.default_error(PATCH_PARAMS_DONT_EXIST) unless matches
end

- (Object) modify_config(oldconf, newconf)



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/git_fusion/config_parser.rb', line 147

def modify_config(oldconf, newconf)
  oldconf = parse_to_hash(oldconf)
  ms_matches = (newconf.keys - oldconf.keys).empty?
  if !newconf['branches'].nil?
    matching_branches?(ms_matches, newconf['branches'], oldconf['branches'])

    config = deep_merge(oldconf, newconf)
  else
    config = oldconf.merge(newconf)
  end
  config
end

- (Object) parse_from_json(repo, config)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/git_fusion/config_parser.rb', line 41

def parse_from_json(repo, config)
  main_section_keys = config.keys.select { |k| k != 'branches' }
  main_section = ''
  main_section_keys.each do |key|
    main_section << "#{key} = #{config[key]}\n"
  end
  branches = config['branches']
  branch_sections = get_branch_sections(repo, branches)
  config = <<-EOC.gsub(/^\s*/, '')
  [@repo]
  #{main_section}
  #{branch_sections}
  EOC
  config
end

- (Object) parse_to_hash(config)



34
35
36
37
38
39
# File 'lib/git_fusion/config_parser.rb', line 34

def parse_to_hash(config)
  config = config
  sections = get_config_sections(config)

  sections_to_json(sections)
end

- (Object) sections_to_json(config)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/git_fusion/config_parser.rb', line 74

def sections_to_json(config)
  out = { 'description' => '', 'branches' => {} }
  config.each do |section|
    value = section.values.first
    if section.keys.include? '@repo'
      out['description'] = value['description']
    else
      branch = value['git-branch-name']
      if value.include? 'view'
        view_mapping = %r{(?<depot_path>"?.*\/...)\s(?<client_path>.*)}
        view = value['view'].match(view_mapping)
        value['depot_path'] = GitFusionStrings.decode(view['depot_path'])
        value['client_path'] = view['client_path']
        values = value.tap { |hs| hs.delete('view') }
      end
      out['branches'][branch] = values
    end
  end
  out
end

- (Object) validate_json_config(config, repo)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git_fusion/config_parser.rb', line 13

def validate_json_config(config, repo)
  # must have description
  # must have branches
  # depot_path must include chosen endpoint
  # must have a stream or depot_path and client_path
  # all keys must be from a known list
  error_and_clean_up(REPO_CONFIG_JSON_NOT_VALID) unless !config['branches'].nil? or !config['description'].nil?
  points_to_repo = false
  config['branches'].values.each do |branch|
    points_to_repo = true if branch['depot_path'].include? repo
  end
  error_and_clean_up(REPO_MAPPING_NOT_MATCHING) unless points_to_repo

  view_or_stream = false
  config['branches'].each do |_branch, values|
    view_or_stream = true if (values.keys.include? 'depot_path' and values.keys.include? 'client_path') or
        values.keys.include? 'stream'
  end
  error_and_clean_up(REPO_CONFIG_JSON_NOT_VALID) unless view_or_stream
end