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.
package/index.cjs CHANGED
@@ -1837,9 +1837,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
1837
1837
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
1838
1838
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
1839
1839
 
1840
- // Comments start with either # or ; and extend to the end of line
1841
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
1842
-
1843
1840
  const extractSectionLine = line => {
1844
1841
  const matches = SECTION_LINE_REGEX.exec(line);
1845
1842
  if (matches != null) {
@@ -1853,43 +1850,61 @@ const extractVariableLine = line => {
1853
1850
  const matches = VARIABLE_LINE_REGEX.exec(line);
1854
1851
  if (matches != null) {
1855
1852
  const [name, rawValue = 'true'] = matches.slice(1);
1856
- const valueWithoutComments = removeComments(rawValue);
1857
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
1858
- return [name, valueWithoutQuotes]
1853
+ return [name, parseValue(rawValue)]
1859
1854
  }
1860
1855
  return null
1861
1856
  };
1862
1857
 
1863
- const removeComments = rawValue => {
1864
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
1865
- if (commentMatches == null) {
1866
- return rawValue
1867
- }
1868
- const [valueWithoutComment, comment] = commentMatches.slice(1);
1869
- // if odd number of quotes before and after comment => comment is escaped
1870
- if (
1871
- hasOddNumberOfQuotes(valueWithoutComment) &&
1872
- hasOddNumberOfQuotes(comment)
1873
- ) {
1874
- return `${valueWithoutComment}${comment}`
1858
+ const ESCAPED_CHARS = new Map([
1859
+ ['t', '\t'],
1860
+ ['b', '\b'],
1861
+ ['n', '\n'],
1862
+ ['\\', '\\'],
1863
+ ['"', '"'],
1864
+ ]);
1865
+
1866
+ // This follows parse_value in config.c of canonical git: double quotes are
1867
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
1868
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
1869
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
1870
+ // backslash is kept as is.
1871
+ const parseValue = rawValue => {
1872
+ let value = '';
1873
+ let pendingSpace = '';
1874
+ let inQuotes = false;
1875
+ for (let i = 0; i < rawValue.length; i++) {
1876
+ const c = rawValue[i];
1877
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
1878
+ if (value !== '') pendingSpace += c;
1879
+ continue
1880
+ }
1881
+ if (!inQuotes && (c === '#' || c === ';')) break
1882
+ value += pendingSpace;
1883
+ pendingSpace = '';
1884
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
1885
+ value += ESCAPED_CHARS.get(rawValue[++i]);
1886
+ } else if (c === '"') {
1887
+ inQuotes = !inQuotes;
1888
+ } else {
1889
+ value += c;
1890
+ }
1875
1891
  }
1876
- return valueWithoutComment
1877
- };
1878
-
1879
- const hasOddNumberOfQuotes = text => {
1880
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
1881
- return numberOfQuotes % 2 !== 0
1892
+ return value
1882
1893
  };
1883
1894
 
1884
- const removeQuotes = text => {
1885
- return text.split('').reduce((newText, c, idx, text) => {
1886
- const isQuote = c === '"' && text[idx - 1] !== '\\';
1887
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
1888
- if (isQuote || isEscapeForQuote) {
1889
- return newText
1890
- }
1891
- return newText + c
1892
- }, '')
1895
+ // This follows write_pair in config.c of canonical git.
1896
+ const serializeValue = value => {
1897
+ if (typeof value !== 'string') return value
1898
+ const quote =
1899
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
1900
+ ? '"'
1901
+ : '';
1902
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
1903
+ if (c === '\n') return '\\n'
1904
+ if (c === '\t') return '\\t'
1905
+ return `\\${c}`
1906
+ });
1907
+ return `${quote}${escaped}${quote}`
1893
1908
  };
1894
1909
 
1895
1910
  const lower = text => {
@@ -2061,11 +2076,7 @@ class GitConfig {
2061
2076
  return line
2062
2077
  }
2063
2078
  if (name != null && value != null) {
2064
- if (typeof value === 'string' && /[#;]/.test(value)) {
2065
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
2066
- return `\t${name} = "${value}"`
2067
- }
2068
- return `\t${name} = ${value}`
2079
+ return `\t${name} = ${serializeValue(value)}`
2069
2080
  }
2070
2081
  if (subsection != null) {
2071
2082
  return `[${section} "${subsection}"]`
@@ -10115,8 +10126,8 @@ function filterCapabilities(server, client) {
10115
10126
 
10116
10127
  const pkg = {
10117
10128
  name: 'isomorphic-git',
10118
- version: '1.44.3',
10119
- agent: 'git/isomorphic-git@1.44.3',
10129
+ version: '1.44.4',
10130
+ agent: 'git/isomorphic-git@1.44.4',
10120
10131
  };
10121
10132
 
10122
10133
  class FIFO {
package/index.js CHANGED
@@ -1830,9 +1830,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
1830
1830
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
1831
1831
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
1832
1832
 
1833
- // Comments start with either # or ; and extend to the end of line
1834
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
1835
-
1836
1833
  const extractSectionLine = line => {
1837
1834
  const matches = SECTION_LINE_REGEX.exec(line);
1838
1835
  if (matches != null) {
@@ -1846,43 +1843,61 @@ const extractVariableLine = line => {
1846
1843
  const matches = VARIABLE_LINE_REGEX.exec(line);
1847
1844
  if (matches != null) {
1848
1845
  const [name, rawValue = 'true'] = matches.slice(1);
1849
- const valueWithoutComments = removeComments(rawValue);
1850
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
1851
- return [name, valueWithoutQuotes]
1846
+ return [name, parseValue(rawValue)]
1852
1847
  }
1853
1848
  return null
1854
1849
  };
1855
1850
 
1856
- const removeComments = rawValue => {
1857
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
1858
- if (commentMatches == null) {
1859
- return rawValue
1860
- }
1861
- const [valueWithoutComment, comment] = commentMatches.slice(1);
1862
- // if odd number of quotes before and after comment => comment is escaped
1863
- if (
1864
- hasOddNumberOfQuotes(valueWithoutComment) &&
1865
- hasOddNumberOfQuotes(comment)
1866
- ) {
1867
- return `${valueWithoutComment}${comment}`
1851
+ const ESCAPED_CHARS = new Map([
1852
+ ['t', '\t'],
1853
+ ['b', '\b'],
1854
+ ['n', '\n'],
1855
+ ['\\', '\\'],
1856
+ ['"', '"'],
1857
+ ]);
1858
+
1859
+ // This follows parse_value in config.c of canonical git: double quotes are
1860
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
1861
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
1862
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
1863
+ // backslash is kept as is.
1864
+ const parseValue = rawValue => {
1865
+ let value = '';
1866
+ let pendingSpace = '';
1867
+ let inQuotes = false;
1868
+ for (let i = 0; i < rawValue.length; i++) {
1869
+ const c = rawValue[i];
1870
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
1871
+ if (value !== '') pendingSpace += c;
1872
+ continue
1873
+ }
1874
+ if (!inQuotes && (c === '#' || c === ';')) break
1875
+ value += pendingSpace;
1876
+ pendingSpace = '';
1877
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
1878
+ value += ESCAPED_CHARS.get(rawValue[++i]);
1879
+ } else if (c === '"') {
1880
+ inQuotes = !inQuotes;
1881
+ } else {
1882
+ value += c;
1883
+ }
1868
1884
  }
1869
- return valueWithoutComment
1870
- };
1871
-
1872
- const hasOddNumberOfQuotes = text => {
1873
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
1874
- return numberOfQuotes % 2 !== 0
1885
+ return value
1875
1886
  };
1876
1887
 
1877
- const removeQuotes = text => {
1878
- return text.split('').reduce((newText, c, idx, text) => {
1879
- const isQuote = c === '"' && text[idx - 1] !== '\\';
1880
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
1881
- if (isQuote || isEscapeForQuote) {
1882
- return newText
1883
- }
1884
- return newText + c
1885
- }, '')
1888
+ // This follows write_pair in config.c of canonical git.
1889
+ const serializeValue = value => {
1890
+ if (typeof value !== 'string') return value
1891
+ const quote =
1892
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
1893
+ ? '"'
1894
+ : '';
1895
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
1896
+ if (c === '\n') return '\\n'
1897
+ if (c === '\t') return '\\t'
1898
+ return `\\${c}`
1899
+ });
1900
+ return `${quote}${escaped}${quote}`
1886
1901
  };
1887
1902
 
1888
1903
  const lower = text => {
@@ -2054,11 +2069,7 @@ class GitConfig {
2054
2069
  return line
2055
2070
  }
2056
2071
  if (name != null && value != null) {
2057
- if (typeof value === 'string' && /[#;]/.test(value)) {
2058
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
2059
- return `\t${name} = "${value}"`
2060
- }
2061
- return `\t${name} = ${value}`
2072
+ return `\t${name} = ${serializeValue(value)}`
2062
2073
  }
2063
2074
  if (subsection != null) {
2064
2075
  return `[${section} "${subsection}"]`
@@ -10102,8 +10113,8 @@ function filterCapabilities(server, client) {
10102
10113
 
10103
10114
  const pkg = {
10104
10115
  name: 'isomorphic-git',
10105
- version: '1.44.3',
10106
- agent: 'git/isomorphic-git@1.44.3',
10116
+ version: '1.44.4',
10117
+ agent: 'git/isomorphic-git@1.44.4',
10107
10118
  };
10108
10119
 
10109
10120
  class FIFO {