zephyr-agent
Version:
Zephyr plugin agent
164 lines (163 loc) • 7.31 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, getters, values)=>{
var define = (defs, kind)=>{
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
[kind]: defs[key]
});
};
define(getters, "get");
define(values, "value");
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
compareSourceContents: ()=>compareSourceContents,
compareSourceRecords: ()=>compareSourceRecords
});
const external_node_child_process_namespaceObject = require("node:child_process");
const external_node_fs_namespaceObject = require("node:fs");
const external_node_os_namespaceObject = require("node:os");
const external_node_path_namespaceObject = require("node:path");
const external_git_ai_js_namespaceObject = require("./git-ai.js");
const external_source_js_namespaceObject = require("./source.js");
function originAt(file, line) {
return file?.attribution.find((range)=>range.start <= line && range.end >= line)?.origin ?? external_git_ai_js_namespaceObject.unknownOrigin;
}
function compareSourceRecords(directory, before, after) {
const left = (0, external_source_js_namespaceObject.loadSourceRecord)(directory, before);
const right = (0, external_source_js_namespaceObject.loadSourceRecord)(directory, after);
return compareSourceContents(left, right, directory);
}
function compareSourceContents(left, right, directory) {
const changes = [];
const temporary = (0, external_node_fs_namespaceObject.mkdtempSync)((0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'zephyr-source-diff-'));
try {
for (const file of [
...new Set([
...Object.keys(left.files),
...Object.keys(right.files)
])
].sort()){
if (left.files[file]?.hash === right.files[file]?.hash && left.files[file]?.mode === right.files[file]?.mode) continue;
const oldFile = directory ? (0, external_source_js_namespaceObject.comparisonFile)(directory, left, file) : left.files[file];
const newFile = directory ? (0, external_source_js_namespaceObject.comparisonFile)(directory, right, file) : right.files[file];
const oldContent = Buffer.from(oldFile?.content ?? '', 'base64');
const newContent = Buffer.from(newFile?.content ?? '', 'base64');
(0, external_node_fs_namespaceObject.writeFileSync)((0, external_node_path_namespaceObject.join)(temporary, 'before'), oldContent, {
mode: 384
});
(0, external_node_fs_namespaceObject.writeFileSync)((0, external_node_path_namespaceObject.join)(temporary, 'after'), newContent, {
mode: 384
});
const result = (0, external_node_child_process_namespaceObject.spawnSync)('git', [
'diff',
'--no-index',
'--no-ext-diff',
'--no-textconv',
'--no-color',
'--unified=3',
'--',
'before',
'after'
], {
cwd: temporary,
encoding: 'utf8',
timeout: 5000,
maxBuffer: 67108864
});
if (result.error || 0 !== result.status && 1 !== result.status) throw result.error ?? new Error(result.stderr);
const lines = [];
let oldLine = 0;
let newLine = 0;
let inHunk = false;
for (const line of result.stdout.split('\n')){
const hunk = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line);
if (hunk) {
oldLine = Number(hunk[1]);
newLine = Number(hunk[2]);
inHunk = true;
continue;
}
if (inHunk) {
if (line.startsWith('+')) lines.push({
operation: 'add',
line: newLine++,
text: line.slice(1),
origin: originAt(newFile, newLine - 1)
});
else if (line.startsWith('-')) lines.push({
operation: 'remove',
line: oldLine++,
text: line.slice(1),
origin: originAt(oldFile, oldLine - 1)
});
else if (line.startsWith(' ')) {
oldLine++;
newLine++;
}
}
}
const patchLines = result.stdout.split('\n');
const firstHunk = patchLines.findIndex((entry)=>entry.startsWith('@@ '));
changes.push({
file,
status: oldFile ? newFile ? 'modified' : 'deleted' : 'added',
binary: oldContent.includes(0) || newContent.includes(0),
beforeMode: oldFile?.mode,
afterMode: newFile?.mode,
patch: patchLines.map((line, index)=>{
if (firstHunk >= 0 && index >= firstHunk) return line;
if (line.startsWith('diff --git ') || line.startsWith('--- ') || line.startsWith('+++ ')) {
if ('diff --git a/before b/after' === line) return `diff --git a/${file} b/${file}`;
if ('--- a/before' === line) return `--- a/${file}`;
if ('+++ b/after' === line) return `+++ b/${file}`;
}
if ('Binary files a/before and b/after differ' === line) return `Binary files a/${file} and b/${file} differ`;
return line;
}).join('\n'),
lines
});
}
} finally{
(0, external_node_fs_namespaceObject.rmSync)(temporary, {
recursive: true,
force: true
});
}
return {
schemaVersion: 1,
before: left.id,
after: right.id,
beforeFingerprint: left.fingerprint,
afterFingerprint: right.fingerprint,
sameCommit: left.baseCommit === right.baseCommit,
changes
};
}
exports.compareSourceContents = __webpack_exports__.compareSourceContents;
exports.compareSourceRecords = __webpack_exports__.compareSourceRecords;
for(var __rspack_i in __webpack_exports__)if (-1 === [
"compareSourceContents",
"compareSourceRecords"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=compare.js.map