-
-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathGitConfig.js
More file actions
296 lines (266 loc) · 8.33 KB
/
Copy pathGitConfig.js
File metadata and controls
296 lines (266 loc) · 8.33 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// This is straight from parse_unit_factor in config.c of canonical git
const num = val => {
if (typeof val === 'number') {
return val
}
val = val.toLowerCase()
let n = parseInt(val)
if (val.endsWith('k')) n *= 1024
if (val.endsWith('m')) n *= 1024 * 1024
if (val.endsWith('g')) n *= 1024 * 1024 * 1024
return n
}
// This is straight from git_parse_maybe_bool_text in config.c of canonical git
const bool = val => {
if (typeof val === 'boolean') {
return val
}
val = val.trim().toLowerCase()
if (val === 'true' || val === 'yes' || val === 'on') return true
if (val === 'false' || val === 'no' || val === 'off') return false
throw Error(
`Expected 'true', 'false', 'yes', 'no', 'on', or 'off', but got ${val}`
)
}
const schema = {
core: {
filemode: bool,
bare: bool,
logallrefupdates: bool,
symlinks: bool,
ignorecase: bool,
bigFileThreshold: num,
},
}
// https://git-scm.com/docs/git-config#_syntax
// section starts with [ and ends with ]
// section is alphanumeric (ASCII) with - and .
// section is case insensitive
// subsection is optional
// subsection is specified after section and one or more spaces
// subsection is specified between double quotes
const SECTION_LINE_REGEX = /^\[([A-Za-z0-9-.]+)(?: "(.*)")?\]$/
const SECTION_REGEX = /^[A-Za-z0-9-.]+$/
// variable lines contain a name, and equal sign and then a value
// variable lines can also only contain a name (the implicit value is a boolean true)
// variable name is alphanumeric (ASCII) with -
// variable name starts with an alphabetic character
// variable name is case insensitive
const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z-]*)(?: *= *(.*))?$/
const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z-]*$/
// Comments start with either # or ; and extend to the end of line
const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/
const extractSectionLine = line => {
const matches = SECTION_LINE_REGEX.exec(line)
if (matches != null) {
const [section, subsection] = matches.slice(1)
return [section, subsection]
}
return null
}
const extractVariableLine = line => {
const matches = VARIABLE_LINE_REGEX.exec(line)
if (matches != null) {
const [name, rawValue = 'true'] = matches.slice(1)
const valueWithoutComments = removeComments(rawValue)
const valueWithoutQuotes = removeQuotes(valueWithoutComments)
return [name, valueWithoutQuotes]
}
return null
}
const removeComments = rawValue => {
const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue)
if (commentMatches == null) {
return rawValue
}
const [valueWithoutComment, comment] = commentMatches.slice(1)
// if odd number of quotes before and after comment => comment is escaped
if (
hasOddNumberOfQuotes(valueWithoutComment) &&
hasOddNumberOfQuotes(comment)
) {
return `${valueWithoutComment}${comment}`
}
return valueWithoutComment
}
const hasOddNumberOfQuotes = text => {
const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length
return numberOfQuotes % 2 !== 0
}
const removeQuotes = text => {
return text.split('').reduce((newText, c, idx, text) => {
const isQuote = c === '"' && text[idx - 1] !== '\\'
const isEscapeForQuote = c === '\\' && text[idx + 1] === '"'
if (isQuote || isEscapeForQuote) {
return newText
}
return newText + c
}, '')
}
const lower = text => {
return text != null ? text.toLowerCase() : null
}
const getPath = (section, subsection, name) => {
return [lower(section), subsection, lower(name)]
.filter(a => a != null)
.join('.')
}
const normalizePath = path => {
const pathSegments = path.split('.')
const section = pathSegments.shift()
const name = pathSegments.pop()
const subsection = pathSegments.length ? pathSegments.join('.') : undefined
return {
section,
subsection,
name,
path: getPath(section, subsection, name),
sectionPath: getPath(section, subsection, null),
isSection: !!section,
}
}
const findLastIndex = (array, callback) => {
return array.reduce((lastIndex, item, index) => {
return callback(item) ? index : lastIndex
}, -1)
}
// Note: there are a LOT of edge cases that aren't covered (e.g. keys in sections that also
// have subsections, [include] directives, etc.
export class GitConfig {
constructor(text) {
let section = null
let subsection = null
this.parsedConfig = text
? text.split('\n').map(line => {
let name = null
let value = null
const trimmedLine = line.trim()
const extractedSection = extractSectionLine(trimmedLine)
const isSection = extractedSection != null
if (isSection) {
;[section, subsection] = extractedSection
} else {
const extractedVariable = extractVariableLine(trimmedLine)
const isVariable = extractedVariable != null
if (isVariable) {
;[name, value] = extractedVariable
}
}
const path = getPath(section, subsection, name)
return { line, isSection, section, subsection, name, value, path }
})
: []
}
static from(text) {
return new GitConfig(text)
}
async get(path, getall = false) {
const normalizedPath = normalizePath(path).path
const allValues = this.parsedConfig
.filter(config => config.path === normalizedPath)
.map(({ section, name, value }) => {
const fn = schema[section] && schema[section][name]
return fn ? fn(value) : value
})
return getall ? allValues : allValues.pop()
}
async getall(path) {
return this.get(path, true)
}
async getSubsections(section) {
return this.parsedConfig
.filter(config => config.isSection && config.section === section)
.map(config => config.subsection)
}
async deleteSection(section, subsection) {
this.parsedConfig = this.parsedConfig.filter(
config =>
!(config.section === section && config.subsection === subsection)
)
}
async append(path, value) {
return this.set(path, value, true)
}
async set(path, value, append = false) {
const {
section,
subsection,
name,
path: normalizedPath,
sectionPath,
isSection,
} = normalizePath(path)
const configIndex = findLastIndex(
this.parsedConfig,
config => config.path === normalizedPath
)
if (value == null) {
if (configIndex !== -1) {
this.parsedConfig.splice(configIndex, 1)
}
} else {
if (configIndex !== -1) {
const config = this.parsedConfig[configIndex]
// Name should be overwritten in case the casing changed
const modifiedConfig = Object.assign({}, config, {
name,
value,
modified: true,
})
if (append) {
this.parsedConfig.splice(configIndex + 1, 0, modifiedConfig)
} else {
this.parsedConfig[configIndex] = modifiedConfig
}
} else {
const sectionIndex = this.parsedConfig.findIndex(
config => config.path === sectionPath
)
const newConfig = {
section,
subsection,
name,
value,
modified: true,
path: normalizedPath,
}
if (SECTION_REGEX.test(section) && VARIABLE_NAME_REGEX.test(name)) {
if (sectionIndex >= 0) {
// Reuse existing section
this.parsedConfig.splice(sectionIndex + 1, 0, newConfig)
} else {
// Add a new section
const newSection = {
isSection,
section,
subsection,
modified: true,
path: sectionPath,
}
this.parsedConfig.push(newSection, newConfig)
}
}
}
}
}
toString() {
return this.parsedConfig
.map(({ line, section, subsection, name, value, modified = false }) => {
if (!modified) {
return line
}
if (name != null && value != null) {
if (typeof value === 'string' && /[#;]/.test(value)) {
// A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
return `\t${name} = "${value}"`
}
return `\t${name} = ${value}`
}
if (subsection != null) {
return `[${section} "${subsection}"]`
}
return `[${section}]`
})
.join('\n')
}
}