-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathdiff.js
More file actions
227 lines (207 loc) · 6.75 KB
/
diff.js
File metadata and controls
227 lines (207 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
/** @import {InternalSupportStatement, InternalIdentifier, BrowserName} from '../types/index.js' */
import { styleText } from 'node:util';
import deepDiff from 'deep-diff';
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { query } from '../utils/index.js';
import { getMergeBase, getFileContent, getGitDiffStatuses } from './lib/git.js';
import mirror from './build/mirror.js';
/**
* @typedef {object} Contents
* @property {string} base
* @property {string} head
*/
/**
* @typedef {object} DiffItem
* @property {string} name
* @property {string} description
*/
/**
* Get contents from base and head commits
* Note: This does not detect renamed files
* @param {string} baseCommit - Base commit
* @param {string} basePath - Base path
* @param {string} headCommit - Head commit
* @param {string} headPath - Head path
* @returns {Contents} The contents of both commits
*/
const getBaseAndHeadContents = (baseCommit, basePath, headCommit, headPath) => {
const base = JSON.parse(getFileContent(baseCommit, basePath));
const head = JSON.parse(getFileContent(headCommit, headPath));
return { base, head };
};
/**
* Returns a formatted string of before-and-after changes
* @param {any} lhs - Left-hand (before) side
* @param {any} rhs - Right-hand (after) side
* @returns {string} Formatted string
*/
const stringifyChange = (lhs, rhs) =>
`${JSON.stringify(lhs)} → ${JSON.stringify(rhs)}`;
/**
* Perform mirroring on specified diff statement
* @param {object} diff - The diff to perform mirroring on
* @param {InternalSupportStatement} diff.base
* @param {InternalSupportStatement} diff.head
* @param {object} contents - The contents to mirror from
* @param {InternalIdentifier} contents.base
* @param {InternalIdentifier} contents.head
* @param {string[]} path - The feature path to mirror
* @param {'base' | 'head'} direction - Whether to mirror 'base' or 'head'
*/
const doMirror = (diff, contents, path, direction) => {
const browser = /** @type {BrowserName} */ (path[path.length - 1]);
const dataPath = path.slice(0, path.length - 3).join('.');
const data = contents[direction];
const queried = /** @type {InternalIdentifier} */ (query(dataPath, data));
if (queried.__compat?.support) {
diff[direction] = mirror(browser, queried.__compat.support);
}
};
/**
* Describe the diff in text form (internal function)
* @param {import('deep-diff').Diff<string, string>} diffItem - The diff to describe
* @param {Contents} contents - The contents of the diff
* @returns {string} A human-readable diff description
*/
const describeByKind = (diffItem, contents) => {
const diff = {
base: /** @type {any} */ (diffItem).lhs,
head: /** @type {any} */ (diffItem).rhs,
};
// Handle mirroring
let doesMirror = '';
if (diff.base === 'mirror') {
doesMirror = 'No longer mirrors';
doMirror(
diff,
/** @type {any} */ (contents),
/** @type {string[]} */ (diffItem.path),
'base',
);
} else if (diff.head === 'mirror') {
doesMirror = 'Now mirrors';
doMirror(
diff,
/** @type {any} */ (contents),
/** @type {string[]} */ (diffItem.path),
'head',
);
}
switch (diffItem.kind) {
case 'N':
return 'added';
case 'D':
return 'deleted';
case 'A':
case 'E':
return `edited (${stringifyChange(diff.base, diff.head)})${
doesMirror && ` - ${doesMirror}`
}`;
default:
return '';
}
};
/**
* Describe the diff in text form
* @param {import('deep-diff').Diff<string, string>} diffItem - The diff to describe
* @param {Contents} contents - The contents of the diff
* @returns {DiffItem} A human-readable diff description
*/
const describeDiffItem = (diffItem, contents) => {
const path = /** @type {string[]} */ (diffItem.path).join('.');
if (path.includes('.__compat.')) {
const [name, member] = path.split('.__compat.');
if (path.endsWith('.notes') && diffItem.kind === 'E') {
return { name, description: `${member} is edited (prose change)` };
}
return {
name,
description: `${member} is ${describeByKind(diffItem, contents)}`,
};
}
return {
name: /** @type {string[]} */ (diffItem.path).slice(0, -1).join('.'),
description: `${path} is ${describeByKind(diffItem, contents)}`,
};
};
/**
* Merge diff together as a map
* @param {DiffItem[]} items - Diff items to merge
* @returns {Map<string, string[]>} A map of the diff items
*/
const mergeAsMap = (items) => {
const map = new Map();
for (const item of items) {
const descriptions = map.get(item.name) || [];
descriptions.push(item.description);
map.set(item.name, descriptions);
}
return map;
};
/**
* Get the diffs as a map
* @param {string} base - Base ref
* @param {string} [head] - Head ref
* @returns {Map<string, string[]>} A map of the diff items
*/
const getDiffs = (base, head = '') => {
/** @type {{ name: string; description: string }[]} */
const namedDescriptions = [];
for (const status of getGitDiffStatuses(base, head)) {
if (!status.headPath.endsWith('.json') || !status.headPath.includes('/')) {
continue;
}
// Note that A means Added for git while it means Array for deep-diff
if (status.value === 'A' || status.value === 'D') {
namedDescriptions.push({
name: status.basePath.replace(/\//g, '.').slice(0, -5), // trim file extension
description: status.value === 'A' ? 'Newly added' : 'Entirely removed',
});
} else {
const contents = getBaseAndHeadContents(
base,
status.basePath,
head,
status.headPath,
);
const diff = deepDiff.diff(contents.base, contents.head);
if (diff) {
namedDescriptions.push(
...diff.map((item) => describeDiffItem(item, contents)),
);
}
}
}
return mergeAsMap(namedDescriptions);
};
if (esMain(import.meta)) {
const argv = yargs(hideBin(process.argv))
.command(
'$0 [base] [head]',
'Print a formatted diff for changes between base and head commits',
)
.positional('base', {
describe:
'The base commit; may be commit hash or other git ref (e.g. "origin/main")',
type: 'string',
default: 'origin/main',
})
.positional('head', {
describe:
'The head commit that changes are applied to; may be commit hash or other git ref (e.g. "origin/main")',
type: 'string',
default: 'HEAD',
})
.parseSync();
const { base, head } = argv;
for (const [key, values] of getDiffs(getMergeBase(base, head), head)) {
console.log(`${styleText('bold', key)}:`);
for (const value of values) {
console.log(` → ${value}`);
}
}
}