isomorphic-git 1.44.3 → 1.44.5

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.
@@ -393,9 +393,11 @@ const schema = {
393
393
  // section is alphanumeric (ASCII) with - and .
394
394
  // section is case insensitive
395
395
  // subsection is optional
396
- // subsection is specified after section and one or more spaces
397
- // subsection is specified between double quotes
398
- const SECTION_LINE_REGEX = /^\[([A-Za-z0-9-.]+)(?: "(.*)")?\]$/;
396
+ // subsection is specified after section and one or more spaces or tabs
397
+ // subsection is specified between double quotes, where \ escapes the next character
398
+ // a comment can follow the section header
399
+ const SECTION_LINE_REGEX =
400
+ /^\[([A-Za-z0-9-.]+)(?:[ \t]+"((?:[^"\\]|\\.)*)")?\][ \t]*(?:[#;].*)?$/;
399
401
  const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
400
402
 
401
403
  // variable lines contain a name, and equal sign and then a value
@@ -407,14 +409,12 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
407
409
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
408
410
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
409
411
 
410
- // Comments start with either # or ; and extend to the end of line
411
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
412
-
413
412
  const extractSectionLine = line => {
414
413
  const matches = SECTION_LINE_REGEX.exec(line);
415
414
  if (matches != null) {
416
415
  const [section, subsection] = matches.slice(1);
417
- return [section, subsection]
416
+ // Like canonical git, drop the backslash of an escaped character
417
+ return [section, subsection && subsection.replace(/\\(.)/g, '$1')]
418
418
  }
419
419
  return null
420
420
  };
@@ -423,43 +423,61 @@ const extractVariableLine = line => {
423
423
  const matches = VARIABLE_LINE_REGEX.exec(line);
424
424
  if (matches != null) {
425
425
  const [name, rawValue = 'true'] = matches.slice(1);
426
- const valueWithoutComments = removeComments(rawValue);
427
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
428
- return [name, valueWithoutQuotes]
426
+ return [name, parseValue(rawValue)]
429
427
  }
430
428
  return null
431
429
  };
432
430
 
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}`
431
+ const ESCAPED_CHARS = new Map([
432
+ ['t', '\t'],
433
+ ['b', '\b'],
434
+ ['n', '\n'],
435
+ ['\\', '\\'],
436
+ ['"', '"'],
437
+ ]);
438
+
439
+ // This follows parse_value in config.c of canonical git: double quotes are
440
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
441
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
442
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
443
+ // backslash is kept as is.
444
+ const parseValue = rawValue => {
445
+ let value = '';
446
+ let pendingSpace = '';
447
+ let inQuotes = false;
448
+ for (let i = 0; i < rawValue.length; i++) {
449
+ const c = rawValue[i];
450
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
451
+ if (value !== '') pendingSpace += c;
452
+ continue
453
+ }
454
+ if (!inQuotes && (c === '#' || c === ';')) break
455
+ value += pendingSpace;
456
+ pendingSpace = '';
457
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
458
+ value += ESCAPED_CHARS.get(rawValue[++i]);
459
+ } else if (c === '"') {
460
+ inQuotes = !inQuotes;
461
+ } else {
462
+ value += c;
463
+ }
445
464
  }
446
- return valueWithoutComment
465
+ return value
447
466
  };
448
467
 
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
- }, '')
468
+ // This follows write_pair in config.c of canonical git.
469
+ const serializeValue = value => {
470
+ if (typeof value !== 'string') return value
471
+ const quote =
472
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
473
+ ? '"'
474
+ : '';
475
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
476
+ if (c === '\n') return '\\n'
477
+ if (c === '\t') return '\\t'
478
+ return `\\${c}`
479
+ });
480
+ return `${quote}${escaped}${quote}`
463
481
  };
464
482
 
465
483
  const lower = text => {
@@ -631,11 +649,7 @@ class GitConfig {
631
649
  return line
632
650
  }
633
651
  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}`
652
+ return `\t${name} = ${serializeValue(value)}`
639
653
  }
640
654
  if (subsection != null) {
641
655
  return `[${section} "${subsection}"]`
package/managers/index.js CHANGED
@@ -386,9 +386,11 @@ const schema = {
386
386
  // section is alphanumeric (ASCII) with - and .
387
387
  // section is case insensitive
388
388
  // subsection is optional
389
- // subsection is specified after section and one or more spaces
390
- // subsection is specified between double quotes
391
- const SECTION_LINE_REGEX = /^\[([A-Za-z0-9-.]+)(?: "(.*)")?\]$/;
389
+ // subsection is specified after section and one or more spaces or tabs
390
+ // subsection is specified between double quotes, where \ escapes the next character
391
+ // a comment can follow the section header
392
+ const SECTION_LINE_REGEX =
393
+ /^\[([A-Za-z0-9-.]+)(?:[ \t]+"((?:[^"\\]|\\.)*)")?\][ \t]*(?:[#;].*)?$/;
392
394
  const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
393
395
 
394
396
  // variable lines contain a name, and equal sign and then a value
@@ -400,14 +402,12 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
400
402
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
401
403
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
402
404
 
403
- // Comments start with either # or ; and extend to the end of line
404
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
405
-
406
405
  const extractSectionLine = line => {
407
406
  const matches = SECTION_LINE_REGEX.exec(line);
408
407
  if (matches != null) {
409
408
  const [section, subsection] = matches.slice(1);
410
- return [section, subsection]
409
+ // Like canonical git, drop the backslash of an escaped character
410
+ return [section, subsection && subsection.replace(/\\(.)/g, '$1')]
411
411
  }
412
412
  return null
413
413
  };
@@ -416,43 +416,61 @@ const extractVariableLine = line => {
416
416
  const matches = VARIABLE_LINE_REGEX.exec(line);
417
417
  if (matches != null) {
418
418
  const [name, rawValue = 'true'] = matches.slice(1);
419
- const valueWithoutComments = removeComments(rawValue);
420
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
421
- return [name, valueWithoutQuotes]
419
+ return [name, parseValue(rawValue)]
422
420
  }
423
421
  return null
424
422
  };
425
423
 
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}`
424
+ const ESCAPED_CHARS = new Map([
425
+ ['t', '\t'],
426
+ ['b', '\b'],
427
+ ['n', '\n'],
428
+ ['\\', '\\'],
429
+ ['"', '"'],
430
+ ]);
431
+
432
+ // This follows parse_value in config.c of canonical git: double quotes are
433
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
434
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
435
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
436
+ // backslash is kept as is.
437
+ const parseValue = rawValue => {
438
+ let value = '';
439
+ let pendingSpace = '';
440
+ let inQuotes = false;
441
+ for (let i = 0; i < rawValue.length; i++) {
442
+ const c = rawValue[i];
443
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
444
+ if (value !== '') pendingSpace += c;
445
+ continue
446
+ }
447
+ if (!inQuotes && (c === '#' || c === ';')) break
448
+ value += pendingSpace;
449
+ pendingSpace = '';
450
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
451
+ value += ESCAPED_CHARS.get(rawValue[++i]);
452
+ } else if (c === '"') {
453
+ inQuotes = !inQuotes;
454
+ } else {
455
+ value += c;
456
+ }
438
457
  }
439
- return valueWithoutComment
458
+ return value
440
459
  };
441
460
 
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
- }, '')
461
+ // This follows write_pair in config.c of canonical git.
462
+ const serializeValue = value => {
463
+ if (typeof value !== 'string') return value
464
+ const quote =
465
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
466
+ ? '"'
467
+ : '';
468
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
469
+ if (c === '\n') return '\\n'
470
+ if (c === '\t') return '\\t'
471
+ return `\\${c}`
472
+ });
473
+ return `${quote}${escaped}${quote}`
456
474
  };
457
475
 
458
476
  const lower = text => {
@@ -624,11 +642,7 @@ class GitConfig {
624
642
  return line
625
643
  }
626
644
  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}`
645
+ return `\t${name} = ${serializeValue(value)}`
632
646
  }
633
647
  if (subsection != null) {
634
648
  return `[${section} "${subsection}"]`