Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 130x 130x 130x 130x 130x 130x 130x 53x 53x 53x 53x 689x 110x 53x 110x 53x 53x 50x 50x 50x 100x 15x 50x 91x 50x 76x 50x 223x 199x 149x 21x 2x 2x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 4x 4x 60x 60x 374x 327x 142x 274x 273x 32x 32x 392x 23x 25x 23x 18x 18x 18x 18x 18x 9x 9x 9x 9x 9x 27x 17x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 8x 24x 8x 4x 4x 1x 3x 3x 3x 2x 7x 7x 7x 11x 33x 11x 11x 11x 21x 12x 12x 21x 7x 8x 8x 7x 7x 7x 7x 7x 4x 4x 2x 2x 2x 3x 7x | import { diff3Merge, MergeRegion } from 'node-diff3';
import { cloneDeep, get, isArray, isEmpty, isObject, isString, set, sortBy, toPath, uniq, unset } from 'lodash-es';
import { Ref, writeRef } from '../model/ref';
import { Ext, writeExt } from '../model/ext';
import { User, writeUser } from '../model/user';
import { Template, writeTemplate } from '../model/template';
import { Plugin, writePlugin } from '../model/plugin';
import { clear, Config, Mod } from '../model/tag';
import { DateTime } from 'luxon';
export function sortEntity(entity: Record<string, any>): Record<string, any> {
const { origin, modified, metadata, created, modifiedString, ...rest } = entity as any;
const ordered: any = {};
const fieldOrder = ['url', 'tag', 'name', 'config', 'defaults', 'schema', 'title', 'comment', 'tags', 'sources', 'alternateUrls', 'published', 'plugins'];
for (const field of fieldOrder) {
if (rest[field] !== undefined) {
ordered[field] = sortValue(rest[field]);
}
}
const remainingKeys = Object.keys(rest)
.filter((key) => !fieldOrder.includes(key))
.sort();
for (const key of remainingKeys) {
ordered[key] = sortValue(rest[key]);
}
return ordered;
}
export function sortObj(entity: Record<string, any>): Record<string, any> {
const ordered: any = {};
const fieldOrder = ['version', 'mod'];
for (const field of fieldOrder) {
if (entity[field] !== undefined) {
ordered[field] = sortValue(entity[field]);
}
}
const remainingKeys = Object.keys(entity)
.filter((key) => !fieldOrder.includes(key))
.sort();
for (const key of remainingKeys) {
ordered[key] = sortValue(entity[key]);
}
return ordered;
}
function sortValue(value: any): any {
if (isArray(value)) return value.map(sortValue);
if (isObject(value) && !DateTime.isDateTime(value)) return sortObj(value);
return value;
}
export function formatDiff(obj: Ref | Ext | User | Plugin | Template): string {
return JSON.stringify(sortEntity(obj), null, 2);
}
export function equalBundle(a?: Mod, b?: Mod) {
Iif (!a || !b) return false;
return formatBundleDiff(a, false) === formatBundleDiff(b, false);
}
export function clearMod<T extends Mod | undefined>(mod: T, strict = true): T {
Iif (!mod) return mod;
const result = { } as any;
Iif (!isEmpty(mod.ref)) result.ref = sortBy(mod.ref!.map((r: Ref) => clearInternalFields(writeRef(r))), 'url');
Iif (!isEmpty(mod.ext)) result.ext = sortBy(mod.ext!.map((e: Ext) => clearInternalFields(writeExt(copyConfig(e)))), 'tag');
Iif (!isEmpty(mod.user)) result.user = sortBy(mod.user!.map((u: User) => clearInternalFields(writeUser(u))), 'tag');
Eif (!isEmpty(mod.plugin)) result.plugin = sortBy(mod.plugin!.map((p: Plugin) => clearConfig(writePlugin(copyConfig(p)), strict)), 'tag');
Iif (!isEmpty(mod.template)) result.template = sortBy(mod.template!.map((t: Template) => clearConfig(writeTemplate(copyConfig(t)), strict)), 'tag');
return result;
}
function copyConfig<T extends { config?: any }>(e: T): T {
return {
...e,
config: e.config && { ...e.config },
};
}
function clearConfig<T extends Config>(e: T, strict = false): T {
const result = {
...e,
config: e.config && { ...e.config },
} as any;
Eif (isEmpty(result.defaults)) delete result.defaults;
Iif (result.schema === null) delete result.schema;
if (!strict) {
delete result.config?.version;
delete result.config?.generated;
}
delete result.origin;
return clearInternalFields(clear(result));
}
function clearInternalFields<T>(value: T): T {
if (isArray(value)) return value.map(clearInternalFields) as T;
if (!isObject(value) || DateTime.isDateTime(value)) return value;
return Object.fromEntries(
Object.entries(value)
.filter(([key]) => !key.startsWith('_'))
.map(([key, nested]) => [key, clearInternalFields(nested)])
) as T;
}
export function formatBundleDiff(mod: Mod, strict = true): string {
mod = clearMod(mod, strict);
return JSON.stringify({
ref: isEmpty(mod.ref) ? undefined : sortBy(mod.ref!, 'url').map(sortEntity),
ext: isEmpty(mod.ext) ? undefined : sortBy(mod.ext!, 'tag').map(sortEntity),
user: isEmpty(mod.user) ? undefined : sortBy(mod.user!, 'tag').map(sortEntity),
plugin: isEmpty(mod.plugin) ? undefined : sortBy(mod.plugin!, 'tag').map(sortEntity),
template: isEmpty(mod.template) ? undefined : sortBy(mod.template!, 'tag').map(sortEntity),
}, (key, value) => key === '_parent' ? undefined : value, 2);
}
export type Merge = { result?: string, conflict?: MergeRegion<string>[] };
export function merge3(ours: string, base: string, theirs: string, delimiter = '\n'): Merge {
const result = diff3Merge<string>(ours, base, theirs, { stringSeparator: delimiter });
const hasConflict = result.some(chunk => chunk.conflict);
if (hasConflict) return { conflict: result };
const mergedLines: string[] = [];
for (const chunk of result) {
Eif (chunk.ok) {
mergedLines.push(...chunk.ok);
}
}
return { result: mergedLines.join(delimiter) };
}
export type BundleMerge = { result?: Mod, conflict?: boolean };
type SubDiff = { type: 'plugin' | 'template', tag: string, path: string, value?: string, prune: string[][] };
/**
* Three-way merge of mod bundles.
* String config fields listed in a Plugin or Template config.subDiff are
* merged line-by-line separately, so independent changes to different lines
* of the same script do not conflict.
*/
export function mergeBundle(ours: Mod, base: Mod, theirs: Mod): BundleMerge {
ours = cloneDeep(clearMod(ours));
base = cloneDeep(clearMod(base));
theirs = cloneDeep(clearMod(theirs));
const subDiffs: SubDiff[] = [];
for (const type of ['plugin', 'template'] as const) {
const tags = uniq([...ours[type] || [], ...base[type] || [], ...theirs[type] || []].map(c => c.tag));
for (const tag of tags) {
const o = ours[type]?.find(c => c.tag === tag);
const b = base[type]?.find(c => c.tag === tag);
const t = theirs[type]?.find(c => c.tag === tag);
Iif (!o || !b || !t) continue;
const paths = uniq([...o.config?.subDiff || [], ...b.config?.subDiff || [], ...t.config?.subDiff || []]);
for (const path of paths) {
Iif (!isString(path) || !path) continue;
const ov = get(o.config, path);
const bv = get(b.config, path);
const tv = get(t.config, path);
Iif (ov === undefined && bv === undefined && tv === undefined) continue;
Iif ([ov, bv, tv].some(v => v !== undefined && !isString(v))) continue;
let value: string | undefined;
if (ov === tv) {
value = ov;
} else if (ov === bv) {
value = tv;
I} else if (tv === bv) {
value = ov;
} else {
const merged = merge3(ov || '', bv || '', tv || '');
if (merged.conflict || merged.result === undefined) return { conflict: true };
value = merged.result;
}
const segments = toPath(path);
const prune: string[][] = [];
for (let i = segments.length - 1; i >= 0; i--) {
const container = ['config', ...segments.slice(0, i)];
const [oh, bh, th] = [o, b, t].map(c => get(c, container) !== undefined);
if (!(oh === th ? oh : oh === bh ? th : oh)) prune.push(container);
}
const placeholder = `subDiff:${type}:${tag}:${path}`;
const parents = segments.slice(0, -1).map((_, i) => ['config', ...segments.slice(0, i + 1)]);
Iif ([o, b, t].some(c => parents.some(parent => {
const value = get(c, parent);
return value !== undefined && !isObject(value);
}))) continue;
for (const c of [o, b, t]) set(c.config ||= {}, path, placeholder);
subDiffs.push({ type, tag, path, value, prune });
}
}
}
const merged = merge3(formatBundleDiff(ours), formatBundleDiff(base), formatBundleDiff(theirs));
if (merged.conflict || !merged.result) return { conflict: true };
const result: Mod = JSON.parse(merged.result);
for (const d of subDiffs) {
const c = result[d.type]?.find(c => c.tag === d.tag);
Iif (!c) continue;
if (d.value === undefined) {
unset(c.config, d.path);
for (const container of d.prune) {
const v = get(c, container);
Iif (!isObject(v) || !isEmpty(v)) break;
unset(c, container);
}
} else {
set(c.config ||= {}, d.path, d.value);
}
}
return { result };
}
|