@netlify/git-utils 2.0.0 → 4.0.0

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/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@netlify/git-utils",
3
- "version": "2.0.0",
3
+ "version": "4.0.0",
4
4
  "description": "Utility for dealing with modified, created, deleted files since a git commit",
5
- "main": "src/main.js",
5
+ "type": "module",
6
+ "exports": "./src/main.js",
7
+ "main": "./src/main.js",
6
8
  "files": [
7
9
  "src/**/*.js"
8
10
  ],
@@ -43,16 +45,16 @@
43
45
  },
44
46
  "license": "MIT",
45
47
  "dependencies": {
46
- "execa": "^3.4.0",
48
+ "execa": "^5.1.1",
47
49
  "map-obj": "^4.0.0",
48
50
  "micromatch": "^4.0.2",
49
51
  "moize": "^6.0.0",
50
52
  "path-exists": "^4.0.0"
51
53
  },
52
54
  "devDependencies": {
53
- "ava": "^2.4.0"
55
+ "ava": "^3.15.0"
54
56
  },
55
57
  "engines": {
56
- "node": ">=10.18.0"
58
+ "node": "^12.20.0 || ^14.14.0 || >=16.0.0"
57
59
  }
58
60
  }
package/src/commits.js CHANGED
@@ -1,21 +1,33 @@
1
- 'use strict'
2
-
3
- const { git } = require('./exec')
1
+ import { git } from './exec.js'
4
2
 
5
3
  // Return information on each commit since the `base` commit, such as SHA,
6
4
  // parent commits, author, committer and commit message
7
- const getCommits = function (base, head, cwd) {
8
- const stdout = git(['log', `--pretty=format:${JSON.stringify(FORMAT_JSON)}`, `${base}...${head}`], cwd)
9
- const commits = JSON.parse(`[${stdout.split('\n').join(',')}]`)
5
+ export const getCommits = function (base, head, cwd) {
6
+ const stdout = git(['log', `--pretty=${GIT_PRETTY_FORMAT}`, `${base}...${head}`], cwd)
7
+ const commits = stdout.split('\n').map(getCommit)
10
8
  return commits
11
9
  }
12
10
 
13
- const FORMAT_JSON = {
14
- sha: '%H',
15
- parents: '%p',
16
- author: { name: '%an', email: '%ae', date: '%ai' },
17
- committer: { name: '%cn', email: '%ce', date: '%ci' },
18
- message: '%f',
11
+ // Parse the commit output from a string to a JavaScript object
12
+ const getCommit = function (line) {
13
+ const [sha, parents, authorName, authorEmail, authorDate, committerName, committerEmail, committerDate, message] =
14
+ line.split(GIT_PRETTY_SEPARATOR)
15
+ return {
16
+ sha,
17
+ parents,
18
+ author: { name: authorName, email: authorEmail, date: authorDate },
19
+ committer: { name: committerName, email: committerEmail, date: committerDate },
20
+ message,
21
+ }
19
22
  }
20
23
 
21
- module.exports = { getCommits }
24
+ // `git log --pretty` does not have any way of separating tokens, except for
25
+ // commits being separated by newlines. Since some tokens (like the commit
26
+ // message or the committer name) might contain a wide range of characters, we
27
+ // need a specific separator.
28
+ // We choose RS (Record separator) which is a rarely used control character
29
+ // intended for this very purpose: separating records. It is used by some
30
+ // formats such as JSON text sequences (RFC 7464).
31
+ const GIT_PRETTY_SEPARATOR = '\u001E'
32
+ // List of commit fields we want to retrieve
33
+ const GIT_PRETTY_FORMAT = ['%H', '%p', '%an', '%ae', '%ai', '%cn', '%ce', '%ci', '%f'].join(GIT_PRETTY_SEPARATOR)
package/src/diff.js CHANGED
@@ -1,10 +1,8 @@
1
- 'use strict'
2
-
3
- const { git } = require('./exec')
1
+ import { git } from './exec.js'
4
2
 
5
3
  // Return the list of modified|created|deleted files according to git, between
6
4
  // the `base` commit and the `HEAD`
7
- const getDiffFiles = function (base, head, cwd) {
5
+ export const getDiffFiles = function (base, head, cwd) {
8
6
  const stdout = git(['diff', '--name-status', '--no-renames', `${base}...${head}`], cwd)
9
7
  const files = stdout.split('\n').map(getDiffFile).filter(Boolean)
10
8
 
@@ -36,5 +34,3 @@ const getFilesByType = function (files, type) {
36
34
  const getFilepath = function ({ filepath }) {
37
35
  return filepath
38
36
  }
39
-
40
- module.exports = { getDiffFiles }
package/src/exec.js CHANGED
@@ -1,20 +1,25 @@
1
- 'use strict'
1
+ import process from 'process'
2
2
 
3
- const process = require('process')
4
-
5
- const execa = require('execa')
6
- const moize = require('moize')
7
- const pathExists = require('path-exists')
3
+ import execa from 'execa'
4
+ import moize from 'moize'
5
+ import pathExists from 'path-exists'
8
6
 
9
7
  // Fires the `git` binary. Memoized.
10
8
  const mGit = function (args, cwd) {
11
9
  const cwdA = safeGetCwd(cwd)
12
- const { stdout } = execa.sync('git', args, { cwd: cwdA })
13
- return stdout
10
+ try {
11
+ const { stdout } = execa.sync('git', args, { cwd: cwdA })
12
+ return stdout
13
+ } catch (error) {
14
+ // The child process `error.message` includes stderr and stdout output which most of the times contains duplicate
15
+ // information. We rely on `error.shortMessage` instead.
16
+ error.message = error.shortMessage
17
+ throw error
18
+ }
14
19
  }
15
20
 
16
21
  // eslint-disable-next-line no-magic-numbers
17
- const git = moize(mGit, { isDeepEqual: true, maxSize: 1e3 })
22
+ export const git = moize(mGit, { isDeepEqual: true, maxSize: 1e3 })
18
23
 
19
24
  const safeGetCwd = function (cwd) {
20
25
  const cwdA = getCwdValue(cwd)
@@ -37,5 +42,3 @@ const getCwdValue = function (cwd) {
37
42
  throw new Error('Current directory does not exist')
38
43
  }
39
44
  }
40
-
41
- module.exports = { git }
package/src/main.js CHANGED
@@ -1,13 +1,11 @@
1
- 'use strict'
2
-
3
- const { getCommits } = require('./commits')
4
- const { getDiffFiles } = require('./diff')
5
- const { fileMatch } = require('./match')
6
- const { getBase, getHead } = require('./refs')
7
- const { getLinesOfCode } = require('./stats')
1
+ import { getCommits } from './commits.js'
2
+ import { getDiffFiles } from './diff.js'
3
+ import { fileMatch } from './match.js'
4
+ import { getBase, getHead } from './refs.js'
5
+ import { getLinesOfCode } from './stats.js'
8
6
 
9
7
  // Main entry point to the git utilities
10
- const getGitUtils = function ({ base, head, cwd } = {}) {
8
+ export const getGitUtils = function ({ base, head, cwd } = {}) {
11
9
  const headA = getHead(cwd, head)
12
10
  const baseA = getBase(base, headA, cwd)
13
11
  const { modifiedFiles, createdFiles, deletedFiles } = getDiffFiles(baseA, headA, cwd)
@@ -16,5 +14,3 @@ const getGitUtils = function ({ base, head, cwd } = {}) {
16
14
  const fileMatchA = fileMatch.bind(null, { modifiedFiles, createdFiles, deletedFiles })
17
15
  return { modifiedFiles, createdFiles, deletedFiles, commits, linesOfCode, fileMatch: fileMatchA }
18
16
  }
19
-
20
- module.exports = getGitUtils
package/src/match.js CHANGED
@@ -1,11 +1,9 @@
1
- 'use strict'
2
-
3
- const mapObj = require('map-obj')
4
- const micromatch = require('micromatch')
1
+ import mapObj from 'map-obj'
2
+ import micromatch from 'micromatch'
5
3
 
6
4
  // Return functions that return modified|created|deleted files filtered by a
7
5
  // globbing pattern
8
- const fileMatch = function ({ modifiedFiles, createdFiles, deletedFiles }, ...patterns) {
6
+ export const fileMatch = function ({ modifiedFiles, createdFiles, deletedFiles }, ...patterns) {
9
7
  const matchFiles = {
10
8
  modified: modifiedFiles,
11
9
  created: createdFiles,
@@ -14,5 +12,3 @@ const fileMatch = function ({ modifiedFiles, createdFiles, deletedFiles }, ...pa
14
12
  }
15
13
  return mapObj(matchFiles, (key, paths) => [key, micromatch(paths, patterns)])
16
14
  }
17
-
18
- module.exports = { fileMatch }
package/src/refs.js CHANGED
@@ -1,11 +1,9 @@
1
- 'use strict'
1
+ import { env } from 'process'
2
2
 
3
- const { env } = require('process')
4
-
5
- const { git } = require('./exec')
3
+ import { git } from './exec.js'
6
4
 
7
5
  // Retrieve the `head` commit
8
- const getHead = function (cwd, head = 'HEAD') {
6
+ export const getHead = function (cwd, head = 'HEAD') {
9
7
  const result = checkRef(head, cwd)
10
8
  if (result.error !== undefined) {
11
9
  throwError('head', result)
@@ -14,7 +12,7 @@ const getHead = function (cwd, head = 'HEAD') {
14
12
  }
15
13
 
16
14
  // Retrieve the `base` commit
17
- const getBase = function (base, head, cwd) {
15
+ export const getBase = function (base, head, cwd) {
18
16
  const refs = getBaseRefs(base, head)
19
17
  const { ref } = findRef(refs, cwd)
20
18
  return ref
@@ -63,5 +61,3 @@ const throwError = function (name, { ref, error: { message, stderr } }) {
63
61
  const messageA = `Invalid ${name} commit ${ref}\n${messages}`
64
62
  throw new Error(messageA)
65
63
  }
66
-
67
- module.exports = { getBase, getHead }
package/src/stats.js CHANGED
@@ -1,10 +1,8 @@
1
- 'use strict'
2
-
3
- const { git } = require('./exec')
1
+ import { git } from './exec.js'
4
2
 
5
3
  // Returns the number of lines of code added, removed or modified since the
6
4
  // `base` commit
7
- const getLinesOfCode = function (base, head, cwd) {
5
+ export const getLinesOfCode = function (base, head, cwd) {
8
6
  const stdout = git(['diff', '--shortstat', `${base}...${head}`], cwd)
9
7
  const insertions = parseStdout(stdout, INSERTION_REGEXP)
10
8
  const deletions = parseStdout(stdout, DELETION_REGEXP)
@@ -23,5 +21,3 @@ const parseStdout = function (stdout, regexp) {
23
21
 
24
22
  const INSERTION_REGEXP = /(\d+) insertion/
25
23
  const DELETION_REGEXP = /(\d+) deletion/
26
-
27
- module.exports = { getLinesOfCode }