isomorphic-git 1.44.3 → 1.44.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -407,9 +407,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
407
407
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
408
408
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
409
409
 
410
- // Comments start with either # or ; and extend to the end of line
411
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
412
-
413
410
  const extractSectionLine = line => {
414
411
  const matches = SECTION_LINE_REGEX.exec(line);
415
412
  if (matches != null) {
@@ -423,43 +420,61 @@ const extractVariableLine = line => {
423
420
  const matches = VARIABLE_LINE_REGEX.exec(line);
424
421
  if (matches != null) {
425
422
  const [name, rawValue = 'true'] = matches.slice(1);
426
- const valueWithoutComments = removeComments(rawValue);
427
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
428
- return [name, valueWithoutQuotes]
423
+ return [name, parseValue(rawValue)]
429
424
  }
430
425
  return null
431
426
  };
432
427
 
433
- const removeComments = rawValue => {
434
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
435
- if (commentMatches == null) {
436
- return rawValue
437
- }
438
- const [valueWithoutComment, comment] = commentMatches.slice(1);
439
- // if odd number of quotes before and after comment => comment is escaped
440
- if (
441
- hasOddNumberOfQuotes(valueWithoutComment) &&
442
- hasOddNumberOfQuotes(comment)
443
- ) {
444
- return `${valueWithoutComment}${comment}`
428
+ const ESCAPED_CHARS = new Map([
429
+ ['t', '\t'],
430
+ ['b', '\b'],
431
+ ['n', '\n'],
432
+ ['\\', '\\'],
433
+ ['"', '"'],
434
+ ]);
435
+
436
+ // This follows parse_value in config.c of canonical git: double quotes are
437
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
438
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
439
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
440
+ // backslash is kept as is.
441
+ const parseValue = rawValue => {
442
+ let value = '';
443
+ let pendingSpace = '';
444
+ let inQuotes = false;
445
+ for (let i = 0; i < rawValue.length; i++) {
446
+ const c = rawValue[i];
447
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
448
+ if (value !== '') pendingSpace += c;
449
+ continue
450
+ }
451
+ if (!inQuotes && (c === '#' || c === ';')) break
452
+ value += pendingSpace;
453
+ pendingSpace = '';
454
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
455
+ value += ESCAPED_CHARS.get(rawValue[++i]);
456
+ } else if (c === '"') {
457
+ inQuotes = !inQuotes;
458
+ } else {
459
+ value += c;
460
+ }
445
461
  }
446
- return valueWithoutComment
462
+ return value
447
463
  };
448
464
 
449
- const hasOddNumberOfQuotes = text => {
450
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
451
- return numberOfQuotes % 2 !== 0
452
- };
453
-
454
- const removeQuotes = text => {
455
- return text.split('').reduce((newText, c, idx, text) => {
456
- const isQuote = c === '"' && text[idx - 1] !== '\\';
457
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
458
- if (isQuote || isEscapeForQuote) {
459
- return newText
460
- }
461
- return newText + c
462
- }, '')
465
+ // This follows write_pair in config.c of canonical git.
466
+ const serializeValue = value => {
467
+ if (typeof value !== 'string') return value
468
+ const quote =
469
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
470
+ ? '"'
471
+ : '';
472
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
473
+ if (c === '\n') return '\\n'
474
+ if (c === '\t') return '\\t'
475
+ return `\\${c}`
476
+ });
477
+ return `${quote}${escaped}${quote}`
463
478
  };
464
479
 
465
480
  const lower = text => {
@@ -631,11 +646,7 @@ class GitConfig {
631
646
  return line
632
647
  }
633
648
  if (name != null && value != null) {
634
- if (typeof value === 'string' && /[#;]/.test(value)) {
635
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
636
- return `\t${name} = "${value}"`
637
- }
638
- return `\t${name} = ${value}`
649
+ return `\t${name} = ${serializeValue(value)}`
639
650
  }
640
651
  if (subsection != null) {
641
652
  return `[${section} "${subsection}"]`
package/managers/index.js CHANGED
@@ -400,9 +400,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
400
400
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
401
401
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
402
402
 
403
- // Comments start with either # or ; and extend to the end of line
404
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
405
-
406
403
  const extractSectionLine = line => {
407
404
  const matches = SECTION_LINE_REGEX.exec(line);
408
405
  if (matches != null) {
@@ -416,43 +413,61 @@ const extractVariableLine = line => {
416
413
  const matches = VARIABLE_LINE_REGEX.exec(line);
417
414
  if (matches != null) {
418
415
  const [name, rawValue = 'true'] = matches.slice(1);
419
- const valueWithoutComments = removeComments(rawValue);
420
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
421
- return [name, valueWithoutQuotes]
416
+ return [name, parseValue(rawValue)]
422
417
  }
423
418
  return null
424
419
  };
425
420
 
426
- const removeComments = rawValue => {
427
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
428
- if (commentMatches == null) {
429
- return rawValue
430
- }
431
- const [valueWithoutComment, comment] = commentMatches.slice(1);
432
- // if odd number of quotes before and after comment => comment is escaped
433
- if (
434
- hasOddNumberOfQuotes(valueWithoutComment) &&
435
- hasOddNumberOfQuotes(comment)
436
- ) {
437
- return `${valueWithoutComment}${comment}`
421
+ const ESCAPED_CHARS = new Map([
422
+ ['t', '\t'],
423
+ ['b', '\b'],
424
+ ['n', '\n'],
425
+ ['\\', '\\'],
426
+ ['"', '"'],
427
+ ]);
428
+
429
+ // This follows parse_value in config.c of canonical git: double quotes are
430
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
431
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
432
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
433
+ // backslash is kept as is.
434
+ const parseValue = rawValue => {
435
+ let value = '';
436
+ let pendingSpace = '';
437
+ let inQuotes = false;
438
+ for (let i = 0; i < rawValue.length; i++) {
439
+ const c = rawValue[i];
440
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
441
+ if (value !== '') pendingSpace += c;
442
+ continue
443
+ }
444
+ if (!inQuotes && (c === '#' || c === ';')) break
445
+ value += pendingSpace;
446
+ pendingSpace = '';
447
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
448
+ value += ESCAPED_CHARS.get(rawValue[++i]);
449
+ } else if (c === '"') {
450
+ inQuotes = !inQuotes;
451
+ } else {
452
+ value += c;
453
+ }
438
454
  }
439
- return valueWithoutComment
455
+ return value
440
456
  };
441
457
 
442
- const hasOddNumberOfQuotes = text => {
443
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
444
- return numberOfQuotes % 2 !== 0
445
- };
446
-
447
- const removeQuotes = text => {
448
- return text.split('').reduce((newText, c, idx, text) => {
449
- const isQuote = c === '"' && text[idx - 1] !== '\\';
450
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
451
- if (isQuote || isEscapeForQuote) {
452
- return newText
453
- }
454
- return newText + c
455
- }, '')
458
+ // This follows write_pair in config.c of canonical git.
459
+ const serializeValue = value => {
460
+ if (typeof value !== 'string') return value
461
+ const quote =
462
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
463
+ ? '"'
464
+ : '';
465
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
466
+ if (c === '\n') return '\\n'
467
+ if (c === '\t') return '\\t'
468
+ return `\\${c}`
469
+ });
470
+ return `${quote}${escaped}${quote}`
456
471
  };
457
472
 
458
473
  const lower = text => {
@@ -624,11 +639,7 @@ class GitConfig {
624
639
  return line
625
640
  }
626
641
  if (name != null && value != null) {
627
- if (typeof value === 'string' && /[#;]/.test(value)) {
628
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
629
- return `\t${name} = "${value}"`
630
- }
631
- return `\t${name} = ${value}`
642
+ return `\t${name} = ${serializeValue(value)}`
632
643
  }
633
644
  if (subsection != null) {
634
645
  return `[${section} "${subsection}"]`