forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitConfigHelper.cs
More file actions
139 lines (123 loc) · 5.93 KB
/
GitConfigHelper.cs
File metadata and controls
139 lines (123 loc) · 5.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
namespace GVFS.Common.Git
{
/// <summary>
/// Helper methods for git config-style file reading and parsing.
/// </summary>
public static class GitConfigHelper
{
/// <summary>
/// Sanitizes lines read from Git config files:
/// - Removes leading and trailing whitespace
/// - Removes comments
/// </summary>
/// <param name="fileLine">Input line from config file</param>
/// <param name="sanitizedLine">Sanitized config file line</param>
/// <returns>true if sanitizedLine has content, false if there is no content left after sanitizing</returns>
public static bool TrySanitizeConfigFileLine(string fileLine, out string sanitizedLine)
{
sanitizedLine = fileLine;
int commentIndex = sanitizedLine.IndexOf(GVFSConstants.GitCommentSign);
if (commentIndex >= 0)
{
sanitizedLine = sanitizedLine.Substring(0, commentIndex);
}
sanitizedLine = sanitizedLine.Trim();
return !string.IsNullOrWhiteSpace(sanitizedLine);
}
/// <summary>
/// Get the settings for a section in a given config file.
/// </summary>
/// <param name="configLines">The contents of a config file, one line per entry.</param>
/// <param name="sectionName">The name of the section to grab the settings from.</param>
/// <returns>A dictionary of settings, keyed off the setting name.</returns>
public static Dictionary<string, GitConfigSetting> GetSettings(string[] configLines, string sectionName)
{
List<string> linesToParse = new List<string>();
int currentLineIndex = 0;
string sectionTag = "[" + sectionName + "]";
// There can be multiple occurrences of the same section in a config file.
while (currentLineIndex < configLines.Length)
{
while (currentLineIndex < configLines.Length && !string.Equals(configLines[currentLineIndex].Trim(), sectionTag, StringComparison.OrdinalIgnoreCase))
{
currentLineIndex++;
}
if (currentLineIndex < configLines.Length)
{
// skip [sectionName] line
currentLineIndex++;
while (currentLineIndex < configLines.Length && !configLines[currentLineIndex].StartsWith("["))
{
string currentLineValue = configLines[currentLineIndex].Trim();
if (!string.IsNullOrEmpty(currentLineValue))
{
linesToParse.Add(currentLineValue);
}
currentLineIndex++;
}
}
}
return ParseKeyValues(linesToParse);
}
/// <summary>
/// Returns a list of settings based on a collection of lines of text in the form:
/// settingName = settingValue
/// or
/// section.settingName=settingValue
/// </summary>
/// <param name="input">The lines of text with the settings to parse.</param>
/// <param name="delimiter">The delimiter char, separating key from value</param>
/// <returns>A dictionary of settings, keyed off the setting name representing the settings parsed from input.</returns>
public static Dictionary<string, GitConfigSetting> ParseKeyValues(IEnumerable<string> input, char delimiter = '=')
{
Dictionary<string, GitConfigSetting> configSettings = new Dictionary<string, GitConfigSetting>(StringComparer.OrdinalIgnoreCase);
foreach (string line in input)
{
string[] fields = line.Split(new[] { delimiter }, 2, StringSplitOptions.None);
if (fields.Length > 0)
{
string key = fields[0].Trim();
string value = string.Empty;
if (fields.Length > 1)
{
value = fields[1].Trim();
}
if (!string.IsNullOrEmpty(key))
{
if (!configSettings.ContainsKey(key) && fields.Length == 2)
{
GitConfigSetting setting = new GitConfigSetting(key, value);
configSettings.Add(key, setting);
}
else if (fields.Length == 2)
{
configSettings[key].Add(value);
}
}
}
}
return configSettings;
}
/// <summary>
/// Returns a list of settings based on input of the form:
/// settingName1 = settingValue1
/// settingName2 = settingValue2
/// settingName3 = settingValue3
/// settingNameN = settingValueN
/// or
/// section.settingName1=settingValue1
/// section.settingName2=settingValue2
/// section.settingName3=settingValue3
/// section.settingNameN=settingValueN
/// </summary>
/// <param name="input">The settings as text.</param>
/// <param name="delimiter">The delimiter char, separating key from value</param>
/// <returns>A dictionary of settings, keyed off the setting name representing the settings parsed from input.</returns>
public static Dictionary<string, GitConfigSetting> ParseKeyValues(string input, char delimiter = '=')
{
return ParseKeyValues(input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries), delimiter);
}
}
}