@allons-y/actions-file-diff 0.2.0 → 0.3.1
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/README.md +64 -29
- package/dist/index.js +272 -69
- package/package.json +16 -37
- package/action.yml +0 -80
- package/src/changes.js +0 -372
- package/src/index.js +0 -18
- package/src/page.js +0 -472
- package/src/publish.js +0 -171
- package/src/render.js +0 -191
- package/src/report-page.client.js +0 -398
- package/src/report-page.css +0 -1115
- package/src/report.js +0 -515
- package/src/utilities.js +0 -141
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"registry": "https://registry.npmjs.org/"
|
|
6
6
|
},
|
|
7
7
|
"name": "@allons-y/actions-file-diff",
|
|
8
|
-
"version": "0.
|
|
8
|
+
"version": "0.3.1",
|
|
9
9
|
"description": "Compare compiled assets between branches and report the diff back on the pull request.",
|
|
10
10
|
"license": "MPL-2.0",
|
|
11
11
|
"author": "Cassondra Roberts <castastrophe@users.noreply.github.com> (https://allons-y.studio)",
|
|
@@ -20,30 +20,19 @@
|
|
|
20
20
|
"type": "module",
|
|
21
21
|
"main": "dist/index.js",
|
|
22
22
|
"files": [
|
|
23
|
-
"dist
|
|
24
|
-
"src/changes.js",
|
|
25
|
-
"src/index.js",
|
|
26
|
-
"src/page.js",
|
|
27
|
-
"src/publish.js",
|
|
28
|
-
"src/render.js",
|
|
29
|
-
"src/report-page.client.js",
|
|
30
|
-
"src/report-page.css",
|
|
31
|
-
"src/report.js",
|
|
32
|
-
"src/utilities.js",
|
|
33
|
-
"action.yml"
|
|
23
|
+
"dist"
|
|
34
24
|
],
|
|
35
25
|
"scripts": {
|
|
36
26
|
"build": "node scripts/build.js",
|
|
37
27
|
"changeset": "opx release add",
|
|
38
28
|
"ci": "yarn lint && yarn build && yarn test",
|
|
39
|
-
"coverage": "
|
|
40
|
-
"format": "
|
|
41
|
-
"lint": "
|
|
42
|
-
"precommit": "lint-staged",
|
|
29
|
+
"coverage": "vitest run --coverage",
|
|
30
|
+
"format": "opx lint --fix",
|
|
31
|
+
"lint": "opx lint",
|
|
43
32
|
"prepare": "husky && envoy || true",
|
|
44
33
|
"preview": "node scripts/preview.js",
|
|
45
34
|
"release": "yarn build && opx release publish",
|
|
46
|
-
"test": "
|
|
35
|
+
"test": "vitest run",
|
|
47
36
|
"test:browser": "node --test test/browser.test.js",
|
|
48
37
|
"version": "opx release version && yarn install --mode update-lockfile"
|
|
49
38
|
},
|
|
@@ -56,26 +45,23 @@
|
|
|
56
45
|
"devDependencies": {
|
|
57
46
|
"@allons-y/envoy": "^1.1.0",
|
|
58
47
|
"@allons-y/opx-cli": "^0.2.0",
|
|
48
|
+
"@allons-y/opx-lint-js": "^1.0.1",
|
|
49
|
+
"@allons-y/opx-lint-json": "^0.1.1",
|
|
50
|
+
"@allons-y/opx-lint-md": "^0.1.1",
|
|
59
51
|
"@allons-y/opx-release": "^0.1.1",
|
|
60
52
|
"@commitlint/cli": "^21.1.0",
|
|
61
53
|
"@commitlint/config-conventional": "^21.1.0",
|
|
62
|
-
"@
|
|
63
|
-
"@eslint/js": "^10.0.1",
|
|
64
|
-
"@eslint/json": "^2.0.0",
|
|
65
|
-
"@eslint/markdown": "^8.0.2",
|
|
66
|
-
"@stylistic/eslint-plugin": "^5.10.0",
|
|
54
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
67
55
|
"esbuild": "^0.28.2",
|
|
68
56
|
"eslint": "^10.6.0",
|
|
69
|
-
"eslint-
|
|
70
|
-
"eslint-plugin-jest": "^29.15.3",
|
|
71
|
-
"eslint-plugin-jsonc": "^3.2.0",
|
|
72
|
-
"eslint-plugin-node": "^11.1.0",
|
|
57
|
+
"eslint-plugin-license-header": "^0.9.0",
|
|
73
58
|
"globals": "^17.7.0",
|
|
74
59
|
"husky": "^9.1.7",
|
|
75
|
-
"jest": "^30.4.2",
|
|
76
|
-
"lint-staged": "^17.0.8",
|
|
77
60
|
"playwright-core": "^1.65.1",
|
|
78
|
-
"prettier": "^3.8.5"
|
|
61
|
+
"prettier": "^3.8.5",
|
|
62
|
+
"typescript": "^5.9.0",
|
|
63
|
+
"vite": "^8.3.5",
|
|
64
|
+
"vitest": "^5.0.5"
|
|
79
65
|
},
|
|
80
66
|
"keywords": [
|
|
81
67
|
"actions",
|
|
@@ -87,12 +73,5 @@
|
|
|
87
73
|
"engines": {
|
|
88
74
|
"node": ">=24"
|
|
89
75
|
},
|
|
90
|
-
"
|
|
91
|
-
"*": [
|
|
92
|
-
"prettier --write --ignore-unknown",
|
|
93
|
-
"eslint --fix --format stylish"
|
|
94
|
-
],
|
|
95
|
-
".github/renovate.json": "npx --yes --package renovate -- renovate-config-validator --strict"
|
|
96
|
-
},
|
|
97
|
-
"packageManager": "yarn@4.17.0"
|
|
76
|
+
"packageManager": "yarn@4.18.1"
|
|
98
77
|
}
|
package/action.yml
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
name: File comparisons
|
|
2
|
-
description: Compares files between branches and reports size changes back on the pull request.
|
|
3
|
-
author: castastrophe
|
|
4
|
-
branding:
|
|
5
|
-
icon: bar-chart-2
|
|
6
|
-
color: blue
|
|
7
|
-
|
|
8
|
-
inputs:
|
|
9
|
-
head-path:
|
|
10
|
-
description: Path to file or directory for file sizes analysis.
|
|
11
|
-
required: false
|
|
12
|
-
default: ${{ github.workspace }}
|
|
13
|
-
base-path:
|
|
14
|
-
description: Optional path to another directory to perform file size diff against the provided path.
|
|
15
|
-
required: false
|
|
16
|
-
token:
|
|
17
|
-
description: GITHUB_TOKEN for the repository.
|
|
18
|
-
required: false
|
|
19
|
-
default: ${{ github.token }}
|
|
20
|
-
file-glob-pattern:
|
|
21
|
-
description: Glob pattern for filtering of the files.
|
|
22
|
-
required: false
|
|
23
|
-
default: "**/dist/**"
|
|
24
|
-
comment:
|
|
25
|
-
description: Whether to comment on the PR.
|
|
26
|
-
required: false
|
|
27
|
-
default: "true"
|
|
28
|
-
comment-header:
|
|
29
|
-
description: Comment header to be used in the PR comment. Also identifies the sticky comment, so distinct headers allow multiple instances to coexist.
|
|
30
|
-
required: false
|
|
31
|
-
default: "## File metrics"
|
|
32
|
-
build-dir:
|
|
33
|
-
description: Path segment separating a package from its built output. Files are grouped into a package named by the directory immediately before this segment; the readable filename is the path after it.
|
|
34
|
-
required: false
|
|
35
|
-
default: "dist"
|
|
36
|
-
main-file:
|
|
37
|
-
description: Fallback filename (relative to the build dir) treated as a package's main file.
|
|
38
|
-
required: false
|
|
39
|
-
default: "index.js"
|
|
40
|
-
read-package-json:
|
|
41
|
-
description: When true, use the `main` field of a package's package.json (relative to the build dir) as its main file instead of `main-file`.
|
|
42
|
-
required: false
|
|
43
|
-
default: "true"
|
|
44
|
-
extensions:
|
|
45
|
-
description: Optional list (multiline or comma-separated) of base extensions to report and count, e.g. `css, js`. `.map`, `.gz` and `.min` layers are stripped before comparing. Empty reports all files.
|
|
46
|
-
required: false
|
|
47
|
-
default: ""
|
|
48
|
-
|
|
49
|
-
visual-diff:
|
|
50
|
-
description: "Where to publish the visual diff (size chart, line diffs and changed images) linked from the comment. Line diffs need git on the PATH. `summary` writes collapsible sections to the job summary and links the run. `branch` also commits a tabbed HTML page to `report-branch` (needs contents write permission and GitHub Pages). `none` turns it off."
|
|
51
|
-
required: false
|
|
52
|
-
default: "summary"
|
|
53
|
-
report-branch:
|
|
54
|
-
description: Branch the HTML report is committed to when `visual-diff` is `branch`. Created as an orphan branch when missing, and kept to a single commit.
|
|
55
|
-
required: false
|
|
56
|
-
default: "file-diff-reports"
|
|
57
|
-
pages-url:
|
|
58
|
-
description: Base URL that serves `report-branch`. Defaults to the repository's GitHub Pages URL.
|
|
59
|
-
required: false
|
|
60
|
-
default: ""
|
|
61
|
-
max-diff-size:
|
|
62
|
-
description: Largest file (in bytes) that gets a line diff. Larger files are listed without one.
|
|
63
|
-
required: false
|
|
64
|
-
default: "262144"
|
|
65
|
-
max-diff-lines:
|
|
66
|
-
description: Most diff lines kept for the whole report. A diff that would go over is left out of the report, with its line counts kept.
|
|
67
|
-
required: false
|
|
68
|
-
default: "20000"
|
|
69
|
-
|
|
70
|
-
outputs:
|
|
71
|
-
has-changed:
|
|
72
|
-
description: '"true" when the head total differs from the base total; "false" otherwise, including when there is no base.'
|
|
73
|
-
total-size:
|
|
74
|
-
description: Integer byte total of the head, summing all reported files and using the minified size where one exists.
|
|
75
|
-
report-url:
|
|
76
|
-
description: "Link to the visual diff: the published page, or the workflow run's summary. Empty when `visual-diff` is `none` or outside a runner."
|
|
77
|
-
|
|
78
|
-
runs:
|
|
79
|
-
using: node24
|
|
80
|
-
main: dist/index.js
|
package/src/changes.js
DELETED
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright 2025 Allons-Y. All rights reserved.
|
|
3
|
-
*
|
|
4
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
6
|
-
* of the License at <http://www.apache.org/licenses/LICENSE-2.0>
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { execFile } from "node:child_process";
|
|
10
|
-
import { closeSync, openSync, readSync } from "node:fs";
|
|
11
|
-
import { devNull, tmpdir } from "node:os";
|
|
12
|
-
import { join } from "node:path";
|
|
13
|
-
import { promisify } from "node:util";
|
|
14
|
-
|
|
15
|
-
import { baseExtension, filterByExtensions, isNew, isRemoved, isReportedFile, normalizeExtensions } from "./report.js";
|
|
16
|
-
import { mapLimit } from "./utilities.js";
|
|
17
|
-
|
|
18
|
-
const execFileAsync = promisify(execFile);
|
|
19
|
-
|
|
20
|
-
/** Extensions treated as images for the visual diff tab. */
|
|
21
|
-
export const IMAGE_EXTENSIONS = new Set(["png", "jpg", "jpeg", "gif", "svg", "webp", "avif", "ico", "bmp"]);
|
|
22
|
-
|
|
23
|
-
/** Default per-file byte limit for line diffs. */
|
|
24
|
-
export const DEFAULT_MAX_DIFF_SIZE = 256 * 1024;
|
|
25
|
-
|
|
26
|
-
/** Default limit on diff lines kept for the whole report. */
|
|
27
|
-
export const DEFAULT_MAX_DIFF_LINES = 20000;
|
|
28
|
-
|
|
29
|
-
/** Largest image (bytes) copied into a published report. */
|
|
30
|
-
export const MAX_IMAGE_SIZE = 5 * 1024 * 1024;
|
|
31
|
-
|
|
32
|
-
/** Time (ms) one `git diff` may run before it is killed. */
|
|
33
|
-
const DIFF_TIMEOUT = 10_000;
|
|
34
|
-
|
|
35
|
-
/** Time (ms) all diffs together may take before the rest are skipped. */
|
|
36
|
-
const DIFF_TIME_BUDGET = 60_000;
|
|
37
|
-
|
|
38
|
-
/** Concurrent `git diff` processes. */
|
|
39
|
-
const DIFF_CONCURRENCY = 4;
|
|
40
|
-
|
|
41
|
-
/** Bytes sniffed for NUL to decide a file is binary (the same window git uses). */
|
|
42
|
-
const SNIFF_BYTES = 8000;
|
|
43
|
-
|
|
44
|
-
/** Chunk size when comparing two files of equal size. */
|
|
45
|
-
const COMPARE_CHUNK = 64 * 1024;
|
|
46
|
-
|
|
47
|
-
/** Largest `git diff` output (bytes) accepted for one file. */
|
|
48
|
-
const MAX_OUTPUT = 32 * 1024 * 1024;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @typedef {"added"|"removed"|"modified"} ChangeStatus
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @typedef {object} Hunk
|
|
56
|
-
* @property {number} oldStart
|
|
57
|
-
* @property {number} oldLines
|
|
58
|
-
* @property {number} newStart
|
|
59
|
-
* @property {number} newLines
|
|
60
|
-
* @property {string[]} lines Prefixed with " ", "+", "-" or "\\".
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @typedef {object} TextChange
|
|
65
|
-
* @property {string} file Path relative to the roots.
|
|
66
|
-
* @property {ChangeStatus} status
|
|
67
|
-
* @property {number} additions Lines added (0 when `skipped`).
|
|
68
|
-
* @property {number} deletions Lines removed (0 when `skipped`).
|
|
69
|
-
* @property {Hunk[]} hunks Empty when `skipped` or `omitted`.
|
|
70
|
-
* @property {string} [skipped] Why no diff was computed. Line counts are unknown.
|
|
71
|
-
* @property {string} [omitted] Why a computed diff was left out of the report. Line counts are accurate.
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @typedef {object} ImageChange
|
|
76
|
-
* @property {string} file Path relative to the roots.
|
|
77
|
-
* @property {ChangeStatus} status
|
|
78
|
-
* @property {number|undefined} headByteSize
|
|
79
|
-
* @property {number|undefined} baseByteSize
|
|
80
|
-
* @property {string|undefined} headPath Absolute path on head, when present.
|
|
81
|
-
* @property {string|undefined} basePath Absolute path on base, when present.
|
|
82
|
-
*/
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* @typedef {object} Stats
|
|
86
|
-
* @property {number} added Files only on head.
|
|
87
|
-
* @property {number} removed Files only on base.
|
|
88
|
-
* @property {number} modified Files on both with different contents.
|
|
89
|
-
* @property {number} additions Changed lines added.
|
|
90
|
-
* @property {number} deletions Changed lines removed.
|
|
91
|
-
*/
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Whether a buffer looks binary (contains a NUL byte in its first 8000 bytes).
|
|
95
|
-
* @param {Buffer} buffer
|
|
96
|
-
* @returns {boolean}
|
|
97
|
-
*/
|
|
98
|
-
export function isBinary(buffer) {
|
|
99
|
-
return buffer.subarray(0, SNIFF_BYTES).includes(0);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Whether a file looks binary, reading only its first 8000 bytes.
|
|
104
|
-
* @param {string} path
|
|
105
|
-
* @returns {boolean}
|
|
106
|
-
*/
|
|
107
|
-
export function isBinaryFile(path) {
|
|
108
|
-
const fd = openSync(path, "r");
|
|
109
|
-
try {
|
|
110
|
-
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
111
|
-
const read = readSync(fd, buffer, 0, SNIFF_BYTES, 0);
|
|
112
|
-
return isBinary(buffer.subarray(0, read));
|
|
113
|
-
} finally {
|
|
114
|
-
closeSync(fd);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Whether two files have identical bytes, reading in chunks and stopping at the
|
|
120
|
-
* first difference. Callers should compare sizes first.
|
|
121
|
-
* @param {string} a
|
|
122
|
-
* @param {string} b
|
|
123
|
-
* @returns {boolean}
|
|
124
|
-
*/
|
|
125
|
-
export function sameContent(a, b) {
|
|
126
|
-
const fa = openSync(a, "r");
|
|
127
|
-
let fb;
|
|
128
|
-
try {
|
|
129
|
-
fb = openSync(b, "r");
|
|
130
|
-
const bufA = Buffer.alloc(COMPARE_CHUNK);
|
|
131
|
-
const bufB = Buffer.alloc(COMPARE_CHUNK);
|
|
132
|
-
for (let position = 0; ; position += COMPARE_CHUNK) {
|
|
133
|
-
const readA = readSync(fa, bufA, 0, COMPARE_CHUNK, position);
|
|
134
|
-
const readB = readSync(fb, bufB, 0, COMPARE_CHUNK, position);
|
|
135
|
-
if (readA !== readB || !bufA.subarray(0, readA).equals(bufB.subarray(0, readB))) return false;
|
|
136
|
-
if (readA === 0) return true;
|
|
137
|
-
}
|
|
138
|
-
} finally {
|
|
139
|
-
closeSync(fa);
|
|
140
|
-
if (fb !== undefined) closeSync(fb);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Whether a file is an image by base extension.
|
|
146
|
-
* @param {string} filepath
|
|
147
|
-
* @returns {boolean}
|
|
148
|
-
*/
|
|
149
|
-
export const isImage = (filepath) => IMAGE_EXTENSIONS.has(baseExtension(filepath));
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Change status for a pair of sizes, or undefined when the file is on neither side.
|
|
153
|
-
* @param {number|undefined} head
|
|
154
|
-
* @param {number|undefined} base
|
|
155
|
-
* @returns {ChangeStatus|undefined}
|
|
156
|
-
*/
|
|
157
|
-
export function changeStatus(head, base) {
|
|
158
|
-
if (isNew(head, base)) return "added";
|
|
159
|
-
if (isRemoved(head, base)) return "removed";
|
|
160
|
-
if (head == null) return undefined;
|
|
161
|
-
return "modified";
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Parses unified diff text (as `git diff` prints it) into hunks and line counts.
|
|
168
|
-
* Everything before the first hunk header (file headers, modes) is ignored.
|
|
169
|
-
* @param {string} text
|
|
170
|
-
* @returns {{ hunks: Hunk[], additions: number, deletions: number }}
|
|
171
|
-
*/
|
|
172
|
-
export function parseUnifiedDiff(text) {
|
|
173
|
-
const hunks = [];
|
|
174
|
-
let additions = 0;
|
|
175
|
-
let deletions = 0;
|
|
176
|
-
let current;
|
|
177
|
-
|
|
178
|
-
for (const line of text.split("\n")) {
|
|
179
|
-
const header = HUNK_HEADER.exec(line);
|
|
180
|
-
if (header) {
|
|
181
|
-
current = { oldStart: Number(header[1]), oldLines: header[2] === undefined ? 1 : Number(header[2]), newStart: Number(header[3]), newLines: header[4] === undefined ? 1 : Number(header[4]), lines: [] };
|
|
182
|
-
hunks.push(current);
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
if (!current) continue;
|
|
186
|
-
const sign = line[0];
|
|
187
|
-
if (sign === "+") additions++;
|
|
188
|
-
else if (sign === "-") deletions++;
|
|
189
|
-
else if (sign !== " " && sign !== "\\") continue;
|
|
190
|
-
current.lines.push(line);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return { hunks, additions, deletions };
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/** Environment that keeps user and system git config out of the diff. */
|
|
197
|
-
const gitEnv = () => ({ ...process.env, GIT_CONFIG_NOSYSTEM: "1", GIT_CONFIG_GLOBAL: devNull, GIT_OPTIONAL_LOCKS: "0" });
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Whether `git` can be run.
|
|
201
|
-
* @param {string} [git] Executable name or path.
|
|
202
|
-
* @returns {Promise<boolean>}
|
|
203
|
-
*/
|
|
204
|
-
export async function hasGit(git = "git") {
|
|
205
|
-
try {
|
|
206
|
-
await execFileAsync(git, ["--version"], { timeout: 5000 });
|
|
207
|
-
return true;
|
|
208
|
-
} catch {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Diffs two files with `git diff --no-index`. A missing side is diffed against
|
|
215
|
-
* `/dev/null`. The command runs outside any repository, so repository config
|
|
216
|
-
* and attributes never apply.
|
|
217
|
-
* @param {object} options
|
|
218
|
-
* @param {string|undefined} options.basePath
|
|
219
|
-
* @param {string|undefined} options.headPath
|
|
220
|
-
* @param {string} [options.git]
|
|
221
|
-
* @param {number} [options.timeout] Milliseconds before the process is killed.
|
|
222
|
-
* @returns {Promise<{ hunks: Hunk[], additions: number, deletions: number }>}
|
|
223
|
-
* @throws {Error} When git fails or times out (`error.reason` says which).
|
|
224
|
-
*/
|
|
225
|
-
export async function gitDiff({ basePath, headPath, git = "git", timeout = DIFF_TIMEOUT }) {
|
|
226
|
-
const args = ["-c", "core.quotepath=off", "diff", "--no-index", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames", "--unified=3", "--", basePath ?? "/dev/null", headPath ?? "/dev/null"];
|
|
227
|
-
try {
|
|
228
|
-
const { stdout } = await execFileAsync(git, args, { cwd: tmpdir(), env: gitEnv(), timeout, maxBuffer: MAX_OUTPUT, encoding: "utf8" });
|
|
229
|
-
return parseUnifiedDiff(stdout);
|
|
230
|
-
} catch (error) {
|
|
231
|
-
// `git diff --no-index` exits 1 when the files differ.
|
|
232
|
-
if (error.code === 1 && typeof error.stdout === "string") return parseUnifiedDiff(error.stdout);
|
|
233
|
-
const reason = error.killed ? "diff timed out" : error.code === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER" ? "diff output too large" : `git failed (${error.code ?? error.message})`;
|
|
234
|
-
throw Object.assign(new Error(reason), { reason, cause: error });
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Compares head and base trees file by file. Sizes decide most cases without
|
|
240
|
-
* reading anything: only same-size files are compared byte by byte, and only
|
|
241
|
-
* small text files are diffed (with `git diff --no-index`, which must be on
|
|
242
|
-
* the PATH). Minified, gzip and sourcemap siblings are skipped.
|
|
243
|
-
*
|
|
244
|
-
* A file that cannot be read or diffed is still reported, with `skipped` saying
|
|
245
|
-
* why. Diffs beyond `maxDiffLines` in total keep their line counts but not
|
|
246
|
-
* their hunks.
|
|
247
|
-
* @param {object} options
|
|
248
|
-
* @param {Map<string, number>} options.headMap Relative path → size on head.
|
|
249
|
-
* @param {Map<string, number>} [options.baseMap] Relative path → size on base.
|
|
250
|
-
* @param {string} options.headRoot
|
|
251
|
-
* @param {string} [options.baseRoot]
|
|
252
|
-
* @param {string|string[]} [options.extensions]
|
|
253
|
-
* @param {number} [options.maxDiffSize] Per-file byte limit for line diffs.
|
|
254
|
-
* @param {number} [options.maxDiffLines] Total diff lines kept for the report.
|
|
255
|
-
* @param {string} [options.git] Executable used for diffs.
|
|
256
|
-
* @param {number} [options.diffTimeout] Per-file diff time limit in ms.
|
|
257
|
-
* @param {number} [options.diffTimeBudget] Total diff time limit in ms.
|
|
258
|
-
* @param {{ warning?: Function }} [options.core]
|
|
259
|
-
* @returns {Promise<{ text: TextChange[], images: ImageChange[], stats: Stats }>}
|
|
260
|
-
*/
|
|
261
|
-
export async function collectChanges({ headMap, baseMap = new Map(), headRoot, baseRoot, extensions = [], maxDiffSize = DEFAULT_MAX_DIFF_SIZE, maxDiffLines = DEFAULT_MAX_DIFF_LINES, git = "git", diffTimeout = DIFF_TIMEOUT, diffTimeBudget = DIFF_TIME_BUDGET, core }) {
|
|
262
|
-
const allowed = normalizeExtensions(extensions);
|
|
263
|
-
const head = filterByExtensions(headMap, allowed);
|
|
264
|
-
const base = filterByExtensions(baseMap, allowed);
|
|
265
|
-
|
|
266
|
-
const text = [];
|
|
267
|
-
const images = [];
|
|
268
|
-
const stats = { added: 0, removed: 0, modified: 0, additions: 0, deletions: 0 };
|
|
269
|
-
/** Text changes that still need a diff, with their paths. */
|
|
270
|
-
const pending = [];
|
|
271
|
-
const unreadable = [];
|
|
272
|
-
|
|
273
|
-
const files = [...new Set([...head.keys(), ...base.keys()])].filter(isReportedFile).sort();
|
|
274
|
-
|
|
275
|
-
files.forEach((file) => {
|
|
276
|
-
const headByteSize = head.get(file);
|
|
277
|
-
const baseByteSize = base.get(file);
|
|
278
|
-
const status = changeStatus(headByteSize, baseByteSize);
|
|
279
|
-
if (!status) return;
|
|
280
|
-
|
|
281
|
-
const headPath = headByteSize != null ? join(headRoot, file) : undefined;
|
|
282
|
-
const basePath = baseByteSize != null && baseRoot ? join(baseRoot, file) : undefined;
|
|
283
|
-
/** First failure for this file, if any. */
|
|
284
|
-
let failure;
|
|
285
|
-
|
|
286
|
-
// Only same-size files need their bytes compared; different sizes already differ.
|
|
287
|
-
if (status === "modified" && headByteSize === baseByteSize) {
|
|
288
|
-
try {
|
|
289
|
-
if (sameContent(headPath, basePath)) return;
|
|
290
|
-
} catch (error) {
|
|
291
|
-
failure = error;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
stats[status]++;
|
|
296
|
-
|
|
297
|
-
if (isImage(file)) {
|
|
298
|
-
images.push({ file, status, headByteSize, baseByteSize, headPath, basePath });
|
|
299
|
-
// SVG is text, so it also gets a line diff.
|
|
300
|
-
if (baseExtension(file) !== "svg") return;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
const change = { file, status, additions: 0, deletions: 0, hunks: [] };
|
|
304
|
-
text.push(change);
|
|
305
|
-
|
|
306
|
-
if (!failure) {
|
|
307
|
-
try {
|
|
308
|
-
if ((headPath && isBinaryFile(headPath)) || (basePath && isBinaryFile(basePath))) {
|
|
309
|
-
change.skipped = "binary file";
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
} catch (error) {
|
|
313
|
-
failure = error;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (failure) {
|
|
317
|
-
change.skipped = `unreadable (${failure.code ?? failure.message})`;
|
|
318
|
-
unreadable.push(file);
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
if (Math.max(headByteSize ?? 0, baseByteSize ?? 0) > maxDiffSize) {
|
|
322
|
-
change.skipped = "file exceeds max-diff-size";
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
pending.push({ change, headPath, basePath });
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
if (unreadable.length > 0) core?.warning?.(`Could not read ${unreadable.length} file${unreadable.length === 1 ? "" : "s"}; listed without a diff: ${unreadable.slice(0, 5).join(", ")}${unreadable.length > 5 ? ", …" : ""}`);
|
|
330
|
-
|
|
331
|
-
if (pending.length > 0 && !(await hasGit(git))) {
|
|
332
|
-
core?.warning?.("`git` was not found on the PATH, so line diffs are skipped. Install git (it is preinstalled on GitHub-hosted runners) to see them.");
|
|
333
|
-
pending.forEach(({ change }) => (change.skipped = "git is not available on PATH"));
|
|
334
|
-
return { text, images, stats };
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// Diff in ordered batches so the line budget is applied in file order and memory stays bounded.
|
|
338
|
-
const started = Date.now();
|
|
339
|
-
let retained = 0;
|
|
340
|
-
for (let i = 0; i < pending.length; i += DIFF_CONCURRENCY * 8) {
|
|
341
|
-
const batch = pending.slice(i, i + DIFF_CONCURRENCY * 8);
|
|
342
|
-
const results = await mapLimit(batch, DIFF_CONCURRENCY, async ({ headPath, basePath }) => {
|
|
343
|
-
if (Date.now() - started > diffTimeBudget) return { reason: "diff time budget exhausted" };
|
|
344
|
-
try {
|
|
345
|
-
return await gitDiff({ basePath, headPath, git, timeout: diffTimeout });
|
|
346
|
-
} catch (error) {
|
|
347
|
-
return { reason: error.reason ?? error.message };
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
results.forEach((result, index) => {
|
|
352
|
-
const { change } = batch[index];
|
|
353
|
-
if (result.reason) {
|
|
354
|
-
change.skipped = result.reason;
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
change.additions = result.additions;
|
|
358
|
-
change.deletions = result.deletions;
|
|
359
|
-
stats.additions += result.additions;
|
|
360
|
-
stats.deletions += result.deletions;
|
|
361
|
-
|
|
362
|
-
const lines = result.hunks.reduce((sum, hunk) => sum + hunk.lines.length + 1, 0);
|
|
363
|
-
if (retained + lines > maxDiffLines) change.omitted = `the report's diff budget (${maxDiffLines.toLocaleString("en-US")} lines) is used up`;
|
|
364
|
-
else {
|
|
365
|
-
retained += lines;
|
|
366
|
-
change.hunks = result.hunks;
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
return { text, images, stats };
|
|
372
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright 2025 Allons-Y. All rights reserved.
|
|
3
|
-
*
|
|
4
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
6
|
-
* of the License at <http://www.apache.org/licenses/LICENSE-2.0>
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import * as core from "@actions/core";
|
|
10
|
-
import { context, getOctokit } from "@actions/github";
|
|
11
|
-
|
|
12
|
-
import { run } from "./run.js";
|
|
13
|
-
|
|
14
|
-
// Entry: errors propagate to setFailed at the top level only.
|
|
15
|
-
run({ core, context, getOctokit }).catch((error) => {
|
|
16
|
-
core.error(error.stack ?? String(error));
|
|
17
|
-
core.setFailed(error.message);
|
|
18
|
-
});
|