@kevlns/xlmerge 1.3.1 → 2.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.
@@ -19,6 +19,7 @@ from typing import Any, Iterable
19
19
  from openpyxl import load_workbook
20
20
  from openpyxl.cell.cell import MergedCell
21
21
  from openpyxl.utils import column_index_from_string, get_column_letter
22
+ from openpyxl.utils.datetime import to_excel
22
23
 
23
24
  # 兼容嵌入式 Python(._pth 忽略 PYTHONPATH):包内自举,将包根加入 sys.path
24
25
  _PKG_ROOT = Path(__file__).resolve().parents[1]
@@ -283,6 +284,111 @@ def _json_value(value: Any) -> Any:
283
284
  return str(value)
284
285
 
285
286
 
287
+ def _sha256_path(path: Path | str) -> str:
288
+ """Return a content digest without loading large workbook into memory."""
289
+ digest = hashlib.sha256()
290
+ with Path(path).open("rb") as handle:
291
+ for block in iter(lambda: handle.read(1024 * 1024), b""):
292
+ digest.update(block)
293
+ return digest.hexdigest()
294
+
295
+
296
+ def _workbook_has_formula(path: Path | str) -> bool:
297
+ workbook = load_workbook(path, data_only=False, keep_links=True, read_only=True)
298
+ try:
299
+ return any(
300
+ cell.data_type == "f"
301
+ for ws in workbook.worksheets
302
+ for row in ws.iter_rows()
303
+ for cell in row
304
+ )
305
+ finally:
306
+ workbook.close()
307
+
308
+
309
+ def _validate_value_workbook(path: Path | str) -> None:
310
+ """Snapshots must be literal and must not contain calculation errors."""
311
+ workbook = load_workbook(path, data_only=False, keep_links=True, read_only=True)
312
+ try:
313
+ for ws in workbook.worksheets:
314
+ for row in ws.iter_rows():
315
+ for cell in row:
316
+ if cell.data_type == "f":
317
+ raise ConflictError(
318
+ f"Formula materialization produced a formula in {path} ({ws.title}!{cell.coordinate})."
319
+ )
320
+ if cell.data_type == "e":
321
+ raise ConflictError(
322
+ f"Formula materialization produced an Excel error in {path} ({ws.title}!{cell.coordinate})."
323
+ )
324
+ finally:
325
+ workbook.close()
326
+
327
+
328
+ def _materialize_value_snapshot(source_path: Path | str, output_path: Path | str | None) -> Path:
329
+ """Calculate one complete source workbook and return its literal snapshot.
330
+
331
+ The implementation lives in formula_values.py so Excel/LibreOffice policy is
332
+ kept in one place. This wrapper intentionally validates its contract at the
333
+ boundary: a successful return that still contains formulas is unsafe.
334
+ """
335
+ source = Path(source_path).resolve()
336
+ if output_path is None:
337
+ output_path = source.with_name(f"{source.stem}.values{source.suffix}")
338
+ destination = Path(output_path).resolve()
339
+ destination.parent.mkdir(parents=True, exist_ok=True)
340
+ try:
341
+ from formula_values import materialize_values
342
+ except ImportError as exc:
343
+ raise ConflictError("Formula materialization is unavailable; refusing to compare formula workbooks.") from exc
344
+ try:
345
+ result = materialize_values(source, destination)
346
+ except Exception as exc:
347
+ raise ConflictError(f"Failed to calculate workbook {source}: {exc}") from exc
348
+ value_path = Path(result) if result is not None else destination
349
+ if not value_path.is_absolute():
350
+ value_path = (destination.parent / value_path).resolve()
351
+ if not value_path.exists():
352
+ raise ConflictError(f"Formula materialization did not produce a workbook for {source}.")
353
+ _validate_value_workbook(value_path)
354
+ return value_path
355
+
356
+
357
+ def _snapshot_workbooks(
358
+ base_path: Path | str | None,
359
+ ours_path: Path | str,
360
+ theirs_path: Path | str,
361
+ analysis_dir: Path,
362
+ ) -> tuple[dict[str, Path | None], dict[str, dict[str, Any]]]:
363
+ """Create and describe immutable value snapshots for all three sides."""
364
+ analysis_dir.mkdir(parents=True, exist_ok=True)
365
+ sources = {"base": Path(base_path).resolve() if base_path else None,
366
+ "ours": Path(ours_path).resolve(), "theirs": Path(theirs_path).resolve()}
367
+ snapshots: dict[str, Path | None] = {}
368
+ metadata: dict[str, dict[str, Any]] = {}
369
+ for side, source in sources.items():
370
+ if source is None:
371
+ snapshots[side] = None
372
+ continue
373
+ source_hash = _sha256_path(source)
374
+ # No-formula workbooks are already valid value snapshots. Avoid a
375
+ # needless package rewrite (and preserve exact zip bytes for safety).
376
+ if _workbook_has_formula(source):
377
+ output = analysis_dir / f"{side}.values{source.suffix.lower()}"
378
+ value_path = _materialize_value_snapshot(source, output)
379
+ else:
380
+ value_path = source
381
+ _validate_value_workbook(value_path)
382
+ snapshots[side] = value_path
383
+ metadata[side] = {
384
+ "sourcePath": str(source),
385
+ "sourceSha256": source_hash,
386
+ "valuePath": str(value_path),
387
+ "valueSha256": _sha256_path(value_path),
388
+ }
389
+ return snapshots, metadata
390
+
391
+
286
392
  def _cell_token(cell, cached_cell=None) -> tuple[Any, str] | None:
287
393
  if cell is None or isinstance(cell, MergedCell):
288
394
  return None
@@ -704,17 +810,21 @@ def diff_workbooks(
704
810
  analysis_dir: Path | str | None = None,
705
811
  skip_hidden_sheets: bool = True,
706
812
  ) -> dict[str, Any]:
707
- wb_base = load_workbook(base_path, data_only=False, keep_links=True) if base_path else None
708
- wb_ours = load_workbook(ours_path, data_only=False, keep_links=True)
709
- wb_theirs = load_workbook(theirs_path, data_only=False, keep_links=True)
710
- wb_base_v = load_workbook(base_path, data_only=True, keep_links=True) if base_path else None
711
- wb_ours_v = load_workbook(ours_path, data_only=True, keep_links=True)
712
- wb_theirs_v = load_workbook(theirs_path, data_only=True, keep_links=True)
813
+ structural_dir = Path(analysis_dir) if analysis_dir else Path(tempfile.mkdtemp(prefix="xlsx-merge-analysis-"))
814
+ snapshot_paths, value_snapshots = _snapshot_workbooks(
815
+ base_path, ours_path, theirs_path, structural_dir,
816
+ )
817
+ # These workbooks are complete, calculated value snapshots. Keeping the
818
+ # three sides separate is important for cross-sheet formulas: each side is
819
+ # calculated against its own original workbook before alignment/merge.
820
+ wb_base = load_workbook(snapshot_paths["base"], data_only=False, keep_links=True) if snapshot_paths["base"] else None
821
+ wb_ours = load_workbook(snapshot_paths["ours"], data_only=False, keep_links=True)
822
+ wb_theirs = load_workbook(snapshot_paths["theirs"], data_only=False, keep_links=True)
823
+ wb_base_v = wb_ours_v = wb_theirs_v = None
713
824
  try:
714
825
  if base_path is not None:
715
- structural_dir = Path(analysis_dir) if analysis_dir else Path(tempfile.mkdtemp(prefix="xlsx-merge-analysis-"))
716
826
  structural = analyze_structural_workbooks(
717
- base_path, ours_path, theirs_path, structural_dir,
827
+ snapshot_paths["base"], snapshot_paths["ours"], snapshot_paths["theirs"], structural_dir,
718
828
  write_sidecars=False, skip_hidden_sheets=skip_hidden_sheets,
719
829
  )
720
830
  sheets = []
@@ -758,6 +868,7 @@ def diff_workbooks(
758
868
  })
759
869
  return {
760
870
  "engineVersion": structural["version"],
871
+ "valueSnapshots": value_snapshots,
761
872
  "sheets": sheets,
762
873
  "summary": {
763
874
  "sheetCount": len(sheets),
@@ -829,6 +940,7 @@ def diff_workbooks(
829
940
  })
830
941
 
831
942
  return {
943
+ "valueSnapshots": value_snapshots,
832
944
  "sheets": sheets,
833
945
  "summary": {
834
946
  "sheetCount": len(sheets),
@@ -847,8 +959,10 @@ def diff_workbooks(
847
959
  wb_theirs.close()
848
960
  if wb_base_v is not None:
849
961
  wb_base_v.close()
850
- wb_ours_v.close()
851
- wb_theirs_v.close()
962
+ if wb_ours_v is not None:
963
+ wb_ours_v.close()
964
+ if wb_theirs_v is not None:
965
+ wb_theirs_v.close()
852
966
 
853
967
 
854
968
  def _author_and_date(repo: Path, relative_path: str, ref: str) -> tuple[str, str]:
@@ -927,6 +1041,8 @@ def prepare_conflicts(
927
1041
  "path": path,
928
1042
  "theirsAuthor": _theirs_author(repo_root, path),
929
1043
  "theirsDate": _theirs_date(repo_root, path),
1044
+ "theirsSha256": hashlib.sha256(_git_blob(repo_root, 3, path)).hexdigest(),
1045
+ "outputSha256": _sha256_path(repo_root / Path(path)) if (repo_root / Path(path)).exists() else None,
930
1046
  })
931
1047
  continue
932
1048
  ours_blob = _git_blob(repo_root, 2, path)
@@ -957,6 +1073,7 @@ def prepare_conflicts(
957
1073
  theirs_path,
958
1074
  analysis_dir=work_dir / "analysis",
959
1075
  )
1076
+ value_snapshots = workbook_diff.get("valueSnapshots") or {}
960
1077
  prepared_files.append({
961
1078
  "id": path,
962
1079
  "path": path,
@@ -969,11 +1086,15 @@ def prepare_conflicts(
969
1086
  "oursDate": _ours_date(repo_root, path),
970
1087
  "theirsDate": _theirs_date(repo_root, path),
971
1088
  "macro": macro,
1089
+ # Keep both source and value digests in the manifest. apply must
1090
+ # reuse exactly what the user saw, never silently recalculate.
1091
+ "valueSnapshots": value_snapshots,
1092
+ "outputSha256": _sha256_path(repo_root / Path(path)) if (repo_root / Path(path)).exists() else None,
972
1093
  "diff": workbook_diff,
973
1094
  })
974
1095
 
975
1096
  manifest = {
976
- "version": 3,
1097
+ "version": 4,
977
1098
  "repoRoot": str(repo_root),
978
1099
  "files": prepared_files,
979
1100
  "autoTheirs": auto_entries,
@@ -993,13 +1114,33 @@ def prepare_conflicts(
993
1114
  def load_manifest(path: Path | str) -> dict[str, Any]:
994
1115
  manifest_path = Path(path).resolve()
995
1116
  data = json.loads(manifest_path.read_text(encoding="utf-8"))
996
- if data.get("version") != 3 or not isinstance(data.get("files"), list):
1117
+ if data.get("version") != 4 or not isinstance(data.get("files"), list):
1118
+ if data.get("version") == 3:
1119
+ raise ConflictError(
1120
+ f"Manifest {manifest_path} was created by the pre-materialization resolver; "
1121
+ "re-run prepare so formula values can be calculated safely."
1122
+ )
997
1123
  raise ConflictError(f"Unsupported manifest: {manifest_path}")
1124
+ for entry in data["files"]:
1125
+ if not isinstance(entry.get("valueSnapshots"), dict):
1126
+ raise ConflictError(f"Manifest {manifest_path} has no immutable formula value snapshots.")
998
1127
  return data
999
1128
 
1000
1129
 
1001
1130
  def _copy_cell(source, target) -> None:
1002
- target.value = source.value
1131
+ if getattr(source, "data_type", None) == "s" and source.value == "":
1132
+ # Formula snapshots may legitimately contain an empty-string result.
1133
+ # Copy the typed rich-text payload so openpyxl does not serialize it as
1134
+ # an absent cell (None) on the next save.
1135
+ target.data_type = "s"
1136
+ target._value = copy(getattr(source, "_value", ""))
1137
+ else:
1138
+ target.value = source.value
1139
+ # openpyxl infers a string beginning with '=' as a formula. Snapshot
1140
+ # cells are literals, so restore the source type after assigning value;
1141
+ # otherwise a calculated string could become executable formula syntax.
1142
+ if getattr(source, "data_type", None) not in (None, "f"):
1143
+ target.data_type = source.data_type
1003
1144
  if source.has_style:
1004
1145
  if source.parent is not None and source.parent.parent is target.parent.parent:
1005
1146
  # 同 workbook:样式表索引通用,浅拷贝 StyleArray 避免对象复制(大表 40 万+ 格)
@@ -1190,64 +1331,90 @@ _FORMULA_CACHE_NS = '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}
1190
1331
  _FORMULA_CACHE_RNS = '{http://schemas.openxmlformats.org/officeDocument/2006/relationships}'
1191
1332
 
1192
1333
 
1193
- def _inject_formula_cache(temp_path: Path, cache: dict[str, dict[str, Any]]) -> None:
1194
- """后处理 xlsx XML,为公式 cell 注入缓存值,使 data_only 读取得到正确值。"""
1195
- if not cache:
1196
- return
1334
+
1335
+
1336
+ def _restore_untouched_formula_caches(source: Path, target: Path, sheet_names: set[str]) -> None:
1337
+ """Copy raw cache types/values, avoiding date-epoch and empty/error coercion."""
1197
1338
  import xml.etree.ElementTree as ET
1198
- ET.register_namespace('', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main')
1199
- with zipfile.ZipFile(temp_path) as zin:
1200
- infos = zin.infolist()
1201
- parts = {info.filename: zin.read(info.filename) for info in infos}
1202
- wb_tree = ET.fromstring(parts['xl/workbook.xml'])
1203
- rels_tree = ET.fromstring(parts['xl/_rels/workbook.xml.rels'])
1204
- rels = {rel.get('Id'): rel.get('Target') for rel in rels_tree}
1205
- sheet_to_file = {}
1206
- for sheet in wb_tree.iter(f'{_FORMULA_CACHE_NS}sheet'):
1207
- name = sheet.get('name')
1208
- rel_id = sheet.get(f'{_FORMULA_CACHE_RNS}id')
1209
- target = rels.get(rel_id, '')
1210
- if target:
1211
- sheet_to_file[name] = target.lstrip('/') if target.startswith('/') else 'xl/' + target
1212
- for sheet_name, coord_cache in cache.items():
1213
- xml_path = sheet_to_file.get(sheet_name)
1214
- if not xml_path or xml_path not in parts:
1339
+
1340
+ def sheet_files(parts):
1341
+ relationships = ET.fromstring(parts['xl/_rels/workbook.xml.rels'])
1342
+ targets = {rel.get('Id'): rel.get('Target') for rel in relationships}
1343
+ result = {}
1344
+ for sheet in ET.fromstring(parts['xl/workbook.xml']).iter(f'{_FORMULA_CACHE_NS}sheet'):
1345
+ value = targets.get(sheet.get(f'{_FORMULA_CACHE_RNS}id'), '')
1346
+ if value:
1347
+ result[sheet.get('name')] = value.lstrip('/') if value.startswith('/') else 'xl/' + value
1348
+ return result
1349
+
1350
+ with zipfile.ZipFile(source) as archive:
1351
+ original = {item.filename: archive.read(item.filename) for item in archive.infolist()}
1352
+ with zipfile.ZipFile(target) as archive:
1353
+ infos = archive.infolist()
1354
+ parts = {item.filename: archive.read(item.filename) for item in infos}
1355
+ original_files, output_files = sheet_files(original), sheet_files(parts)
1356
+ for name in sheet_names:
1357
+ if name not in original_files or name not in output_files:
1215
1358
  continue
1216
- tree = ET.fromstring(parts[xml_path])
1217
- modified = False
1218
- for c in tree.iter(f'{_FORMULA_CACHE_NS}c'):
1219
- coord = c.get('r')
1220
- if coord not in coord_cache:
1221
- continue
1222
- if c.find(f'{_FORMULA_CACHE_NS}f') is None:
1359
+ original_tree = ET.fromstring(original[original_files[name]])
1360
+ caches = {cell.get('r'): cell for cell in original_tree.iter(f'{_FORMULA_CACHE_NS}c')
1361
+ if cell.find(f'{_FORMULA_CACHE_NS}f') is not None}
1362
+ if not caches:
1363
+ continue
1364
+ output_tree = ET.fromstring(parts[output_files[name]])
1365
+ for cell in output_tree.iter(f'{_FORMULA_CACHE_NS}c'):
1366
+ old = caches.get(cell.get('r'))
1367
+ if old is None or cell.find(f'{_FORMULA_CACHE_NS}f') is None:
1223
1368
  continue
1224
- cached = coord_cache[coord]
1225
- if cached is None:
1369
+ cell.attrib.pop('t', None)
1370
+ if 't' in old.attrib:
1371
+ cell.set('t', old.get('t'))
1372
+ for value in list(cell.findall(f'{_FORMULA_CACHE_NS}v')):
1373
+ cell.remove(value)
1374
+ old_value = old.find(f'{_FORMULA_CACHE_NS}v')
1375
+ if old_value is not None:
1376
+ cell.append(copy(old_value))
1377
+ parts[output_files[name]] = ET.tostring(output_tree, encoding='utf-8')
1378
+ with zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as archive:
1379
+ for info in infos:
1380
+ archive.writestr(info, parts[info.filename])
1381
+
1382
+
1383
+ def _preserve_empty_inline_strings(path: Path) -> None:
1384
+ """Make zero-length literal strings round-trip as ``''`` rather than None."""
1385
+ import xml.etree.ElementTree as ET
1386
+ namespace = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
1387
+ ET.register_namespace("", namespace)
1388
+ with zipfile.ZipFile(path) as source:
1389
+ infos = source.infolist()
1390
+ parts = {info.filename: source.read(info.filename) for info in infos}
1391
+ changed = False
1392
+ for name, payload in list(parts.items()):
1393
+ if not name.startswith("xl/worksheets/") or not name.endswith(".xml"):
1394
+ continue
1395
+ tree = ET.fromstring(payload)
1396
+ modified = False
1397
+ for cell in tree.iter(f"{{{namespace}}}c"):
1398
+ if cell.get("t") != "inlineStr" or cell.find(f"{{{namespace}}}is") is not None:
1226
1399
  continue
1227
- v_elem = c.find(f'{_FORMULA_CACHE_NS}v')
1228
- if v_elem is None:
1229
- v_elem = ET.SubElement(c, f'{_FORMULA_CACHE_NS}v')
1230
- if isinstance(cached, bool):
1231
- c.set('t', 'b')
1232
- v_elem.text = '1' if cached else '0'
1233
- elif isinstance(cached, str):
1234
- c.set('t', 'str')
1235
- v_elem.text = cached
1236
- else:
1237
- v_elem.text = str(cached)
1400
+ inline = ET.SubElement(cell, f"{{{namespace}}}is")
1401
+ ET.SubElement(inline, f"{{{namespace}}}t").text = ""
1238
1402
  modified = True
1239
1403
  if modified:
1240
- xml_bytes = ET.tostring(tree, encoding='UTF-8')
1241
- if xml_bytes.startswith(b"<?xml"):
1242
- xml_bytes = xml_bytes.replace(b"encoding='UTF-8'", b'encoding="UTF-8"')
1243
- if b'standalone' not in xml_bytes:
1244
- xml_bytes = xml_bytes.replace(b'?>', b' standalone="yes"?>', 1)
1245
- else:
1246
- xml_bytes = b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' + xml_bytes
1247
- parts[xml_path] = xml_bytes
1248
- with zipfile.ZipFile(temp_path, 'w', zipfile.ZIP_DEFLATED) as zout:
1249
- for info in infos:
1250
- zout.writestr(info, parts[info.filename])
1404
+ parts[name] = ET.tostring(tree, encoding="UTF-8")
1405
+ changed = True
1406
+ if not changed:
1407
+ return
1408
+ replacement = path.with_name(f".{path.name}.empty-strings")
1409
+ try:
1410
+ with zipfile.ZipFile(replacement, "w", zipfile.ZIP_DEFLATED) as output:
1411
+ for info in infos:
1412
+ output.writestr(info, parts[info.filename])
1413
+ os.replace(replacement, path)
1414
+ finally:
1415
+ replacement.unlink(missing_ok=True)
1416
+
1417
+
1251
1418
 
1252
1419
 
1253
1420
  def _rebuild_structural_sheet(wb_out, wb_base, wb_ours, wb_theirs, sheet, selection,
@@ -1370,24 +1537,78 @@ def _build_merged_file(file_entry: dict[str, Any], decisions: dict[str, Any]) ->
1370
1537
  file_choice = _file_decisions(decisions, file_entry["id"])
1371
1538
  macro = file_entry.get("macro") or {}
1372
1539
  macro_choice = file_choice.get("macro", macro.get("default", "ours"))
1540
+ snapshot_meta = file_entry.get("valueSnapshots") or (file_entry.get("diff") or {}).get("valueSnapshots") or {}
1541
+ if not snapshot_meta:
1542
+ # Direct callers from older integrations may still build a workbook
1543
+ # without a manifest. Permit that only when there are no formulas;
1544
+ # a formula-bearing old input must never fall back to stale caches.
1545
+ if any(_workbook_has_formula(file_entry[side]) for side in ("base", "ours", "theirs") if file_entry.get(side)):
1546
+ raise ConflictError(
1547
+ f"{file_entry['path']} has formulas but no immutable value snapshot; re-run prepare."
1548
+ )
1549
+ snapshot_meta = {
1550
+ side: {"valuePath": file_entry.get(side)}
1551
+ for side in ("base", "ours", "theirs") if file_entry.get(side)
1552
+ }
1553
+ value_paths: dict[str, Path | None] = {}
1554
+ for side in ("base", "ours", "theirs"):
1555
+ meta = snapshot_meta.get(side) or {}
1556
+ path = meta.get("valuePath")
1557
+ if path is None:
1558
+ value_paths[side] = None
1559
+ continue
1560
+ value_path = Path(path).resolve()
1561
+ if not value_path.exists():
1562
+ raise ConflictError(f"Missing immutable {side} value snapshot for {file_entry['path']}: {value_path}")
1563
+ expected = meta.get("valueSha256")
1564
+ if expected and _sha256_path(value_path) != expected:
1565
+ raise ConflictError(f"Immutable {side} value snapshot changed for {file_entry['path']}; refusing apply.")
1566
+ _validate_value_workbook(value_path)
1567
+ value_paths[side] = value_path
1373
1568
  fd, temp_name = tempfile.mkstemp(prefix=f".{output.stem}-", suffix=extension, dir=output.parent)
1374
1569
  os.close(fd)
1375
1570
  temp_path = Path(temp_name)
1376
1571
  wb_out = load_workbook(file_entry["ours"], data_only=False, keep_links=True, keep_vba=keep_vba)
1377
- wb_base = load_workbook(file_entry["base"], data_only=False, keep_links=True) if file_entry.get("base") else None
1378
- wb_ours = wb_out # 只读复用,使 _copy_cell 同 workbook 样式索引快路径生效
1379
- wb_theirs = load_workbook(file_entry["theirs"], data_only=False, keep_links=True)
1380
- wb_base_v = load_workbook(file_entry["base"], data_only=True, keep_links=True) if file_entry.get("base") else None
1381
- wb_ours_v = load_workbook(file_entry["ours"], data_only=True, keep_links=True)
1382
- wb_theirs_v = load_workbook(file_entry["theirs"], data_only=True, keep_links=True)
1572
+ # Original ours is the package/style/macro base. All sources used to
1573
+ # rebuild an affected Sheet are the calculated literal snapshots.
1574
+ wb_base = load_workbook(value_paths["base"], data_only=False, keep_links=True, keep_vba=keep_vba) if value_paths["base"] else None
1575
+ wb_ours = load_workbook(value_paths["ours"], data_only=False, keep_links=True, keep_vba=keep_vba)
1576
+ wb_theirs = load_workbook(value_paths["theirs"], data_only=False, keep_links=True, keep_vba=keep_vba)
1577
+ wb_base_v = wb_ours_v = wb_theirs_v = None
1383
1578
  sheet_choices = file_choice.get("sheets") or {}
1384
1579
  formula_cache: dict[str, dict[str, Any]] = {}
1580
+ affected_sheet_names: set[str] = set()
1581
+ affected_output_sheet_names: set[str] = set()
1582
+ output_sheet_names: set[str] = set()
1385
1583
  try:
1386
1584
  theirs_order = wb_theirs.sheetnames
1387
1585
  for sheet in file_entry["diff"]["sheets"]:
1388
1586
  name = sheet["name"]
1389
1587
  action = sheet["action"]
1390
1588
  selection = sheet_choices.get(name) or {}
1589
+ original_name = (sheet.get("names") or {}).get("ours") or name
1590
+ is_affected = (
1591
+ action.startswith("conflict_")
1592
+ or action in ("auto_add_ours", "auto_add_theirs")
1593
+ or (
1594
+ action == "merge"
1595
+ and (
1596
+ sheet.get("rebuildRequired", True)
1597
+ or sheet.get("renameStatus") != "unchanged"
1598
+ or (sheet.get("mergedCells") or {}).get("status") != "same"
1599
+ )
1600
+ )
1601
+ )
1602
+ if is_affected:
1603
+ affected_sheet_names.add(original_name)
1604
+ if action == "merge":
1605
+ affected_output_sheet_names.add(_selected_sheet_name(sheet, selection))
1606
+ elif action.startswith("conflict_"):
1607
+ affected_output_sheet_names.add((sheet.get("names") or {}).get(selection.get("sheet")) or name)
1608
+ elif action == "auto_add_ours":
1609
+ affected_output_sheet_names.add((sheet.get("names") or {}).get("ours") or name)
1610
+ elif action == "auto_add_theirs":
1611
+ affected_output_sheet_names.add((sheet.get("names") or {}).get("theirs") or name)
1391
1612
  if action == "auto_delete":
1392
1613
  if name in wb_out.sheetnames:
1393
1614
  wb_out.remove(wb_out[name])
@@ -1400,24 +1621,54 @@ def _build_merged_file(file_entry: dict[str, Any], decisions: dict[str, Any]) ->
1400
1621
  if theirs_name not in wb_out.sheetnames:
1401
1622
  _copy_sheet(wb_theirs[theirs_name], wb_out, index=min(theirs_order.index(theirs_name), len(wb_out.sheetnames)))
1402
1623
  continue
1624
+ if action == "auto_add_ours":
1625
+ ours_name = (sheet.get("names") or {}).get("ours") or name
1626
+ if ours_name in wb_out.sheetnames:
1627
+ wb_out.remove(wb_out[ours_name])
1628
+ if ours_name in wb_ours.sheetnames:
1629
+ _copy_sheet(
1630
+ wb_ours[ours_name], wb_out,
1631
+ index=min(wb_ours.sheetnames.index(ours_name), len(wb_out.sheetnames)),
1632
+ )
1633
+ continue
1403
1634
  if action.startswith("conflict_"):
1404
1635
  side = selection["sheet"]
1405
1636
  if side == "ours":
1406
- continue
1407
- if name in wb_theirs.sheetnames:
1408
- _replace_sheet(wb_out, wb_theirs[name], theirs_order)
1637
+ source_wb = wb_ours
1638
+ else:
1639
+ source_wb = wb_theirs
1640
+ source_name = (sheet.get("names") or {}).get(side) or name
1641
+ if source_name in source_wb.sheetnames:
1642
+ _replace_sheet(wb_out, source_wb[source_name], source_wb.sheetnames)
1409
1643
  elif name in wb_out.sheetnames:
1410
1644
  wb_out.remove(wb_out[name])
1411
1645
  continue
1412
1646
  if action != "merge":
1413
1647
  continue
1648
+ if not is_affected:
1649
+ # Formula text may differ while final values are equal. This
1650
+ # is intentionally an untouched Sheet: preserve ours exactly.
1651
+ continue
1414
1652
  if not sheet.get("rebuildRequired", True):
1415
1653
  existing_name = (sheet.get("names") or {}).get("ours") or name
1416
1654
  final_name = _selected_sheet_name(sheet, selection)
1417
- if existing_name in wb_out.sheetnames and final_name != existing_name:
1418
- if final_name in wb_out.sheetnames:
1419
- raise ConflictError(f"Sheet name collision after merge: {final_name}")
1420
- wb_out[existing_name].title = final_name
1655
+ # A rename-only/metadata-only changed Sheet is still affected;
1656
+ # replace it from the selected calculated value snapshot so no
1657
+ # formula survives through this fast path.
1658
+ source_side = selection.get("rename", "ours") if sheet.get("renameStatus") == "rename_conflict" else "ours"
1659
+ source_name = (sheet.get("names") or {}).get(source_side) or existing_name
1660
+ source_wb = wb_theirs if source_side == "theirs" else wb_ours
1661
+ if source_name in source_wb.sheetnames:
1662
+ if existing_name in wb_out.sheetnames:
1663
+ old_index = wb_out.sheetnames.index(existing_name)
1664
+ wb_out.remove(wb_out[existing_name])
1665
+ else:
1666
+ old_index = len(wb_out.sheetnames)
1667
+ copied = _copy_sheet(source_wb[source_name], wb_out, index=old_index)
1668
+ if final_name != copied.title:
1669
+ if final_name in wb_out.sheetnames and wb_out[final_name] is not copied:
1670
+ raise ConflictError(f"Sheet name collision after merge: {final_name}")
1671
+ copied.title = final_name
1421
1672
  continue
1422
1673
  if sheet.get("allRows") is not None and sheet.get("columns") is not None:
1423
1674
  cache = _rebuild_structural_sheet(
@@ -1426,26 +1677,26 @@ def _build_merged_file(file_entry: dict[str, Any], decisions: dict[str, Any]) ->
1426
1677
  )
1427
1678
  formula_cache.update(cache)
1428
1679
  continue
1680
+ # Legacy coordinate-only reports are now also sourced from the
1681
+ # literal snapshot. They should be rare (structural reports use
1682
+ # _rebuild_structural_sheet), but must obey the same no-formula rule.
1683
+ if name not in wb_ours.sheetnames:
1684
+ raise ConflictError(f"Missing ours Sheet {name} in legacy merge report.")
1685
+ # Start from the literal ours snapshot so unchanged formula cells
1686
+ # on this affected Sheet cannot survive the legacy patch path.
1687
+ _replace_sheet(wb_out, wb_ours[name], wb_ours.sheetnames)
1429
1688
  ws_out = wb_out[name]
1430
1689
  ws_theirs = wb_theirs[name]
1431
- ws_theirs_v = wb_theirs_v[name] if name in wb_theirs_v.sheetnames else None
1432
1690
  cell_choices = selection.get("cells") or {}
1433
- legacy_cache: dict[str, Any] = {}
1434
1691
  for cell in sheet["cells"]:
1435
1692
  side = cell_choices.get(cell["coordinate"], cell["resolution"])
1436
1693
  if side == "conflict":
1437
1694
  side = cell_choices[cell["coordinate"]]
1438
1695
  if side == "theirs":
1439
- src = ws_theirs[cell["coordinate"]]
1440
- _copy_cell(src, ws_out[cell["coordinate"]])
1441
- if src.data_type == 'f' and ws_theirs_v is not None:
1442
- cached = ws_theirs_v[cell["coordinate"]].value
1443
- if cached is not None:
1444
- legacy_cache[cell["coordinate"]] = cached
1445
- if legacy_cache:
1446
- formula_cache[name] = legacy_cache
1696
+ _copy_cell(ws_theirs[cell["coordinate"]], ws_out[cell["coordinate"]])
1447
1697
  if not wb_out.sheetnames:
1448
1698
  raise ConflictError(f"Cannot save a workbook with no sheets: {file_entry['path']}")
1699
+ output_sheet_names = set(wb_out.sheetnames)
1449
1700
  wb_out.save(temp_path)
1450
1701
  except Exception:
1451
1702
  temp_path.unlink(missing_ok=True)
@@ -1460,24 +1711,40 @@ def _build_merged_file(file_entry: dict[str, Any], decisions: dict[str, Any]) ->
1460
1711
  _close_workbook(wb_ours_v)
1461
1712
  _close_workbook(wb_theirs_v)
1462
1713
 
1463
- _inject_formula_cache(temp_path, formula_cache)
1464
- check_wb = load_workbook(temp_path, read_only=True, data_only=False)
1465
- check_wb.close()
1466
- if keep_vba:
1467
- macro_source = file_entry["theirs"] if macro_choice == "theirs" else file_entry["ours"]
1468
- if macro_choice == "theirs":
1469
- if not macro.get("canChooseTheirs", True):
1470
- temp_path.unlink(missing_ok=True)
1471
- raise ConflictError(
1472
- f"Cannot safely preserve the REMOTE macro payload for {file_entry['path']} because its package structure differs."
1473
- )
1474
- _replace_macro_payload(temp_path, macro_source)
1475
- expected_hash, expected_entries = _macro_payload_digest(macro_source)
1476
- actual_hash, actual_entries = _macro_payload_digest(temp_path)
1477
- if actual_hash != expected_hash or actual_entries != expected_entries:
1478
- temp_path.unlink(missing_ok=True)
1479
- raise ConflictError(f"Macro payload changed while saving {file_entry['path']}; output was not written.")
1480
- return temp_path
1714
+ try:
1715
+ _restore_untouched_formula_caches(
1716
+ Path(file_entry['ours']), temp_path, output_sheet_names - affected_sheet_names,
1717
+ )
1718
+ _preserve_empty_inline_strings(temp_path)
1719
+ check_wb = load_workbook(temp_path, read_only=True, data_only=False)
1720
+ try:
1721
+ for ws in check_wb.worksheets:
1722
+ if ws.title not in affected_output_sheet_names:
1723
+ continue
1724
+ for row in ws.iter_rows():
1725
+ for cell in row:
1726
+ if cell.data_type == "f":
1727
+ raise ConflictError(
1728
+ f"Affected Sheet {ws.title} retained formula at {cell.coordinate}; output was not written."
1729
+ )
1730
+ finally:
1731
+ check_wb.close()
1732
+ if keep_vba:
1733
+ macro_source = file_entry["theirs"] if macro_choice == "theirs" else file_entry["ours"]
1734
+ if macro_choice == "theirs":
1735
+ if not macro.get("canChooseTheirs", True):
1736
+ raise ConflictError(
1737
+ f"Cannot safely preserve the REMOTE macro payload for {file_entry['path']} because its package structure differs."
1738
+ )
1739
+ _replace_macro_payload(temp_path, macro_source)
1740
+ expected_hash, expected_entries = _macro_payload_digest(macro_source)
1741
+ actual_hash, actual_entries = _macro_payload_digest(temp_path)
1742
+ if actual_hash != expected_hash or actual_entries != expected_entries:
1743
+ raise ConflictError(f"Macro payload changed while saving {file_entry['path']}; output was not written.")
1744
+ return temp_path
1745
+ except Exception:
1746
+ temp_path.unlink(missing_ok=True)
1747
+ raise
1481
1748
 
1482
1749
 
1483
1750
  def _git_step(repo: Path, args: list[str], label: str) -> dict[str, Any]:
@@ -1554,6 +1821,107 @@ def _message_value(value) -> str:
1554
1821
  return str(value)
1555
1822
 
1556
1823
 
1824
+ def _validate_manifest_sources(manifest: dict[str, Any], repo: Path) -> None:
1825
+ """Preflight every input before preparing any output replacement.
1826
+
1827
+ This deliberately validates both extracted files and their current index
1828
+ stages. A user may keep the UI open while another process changes a
1829
+ conflict, in which case applying the old choices would be data loss.
1830
+ """
1831
+ for entry in manifest.get("files") or []:
1832
+ relative = entry["path"]
1833
+ for side, stage in (("base", 1), ("ours", 2), ("theirs", 3)):
1834
+ meta = (entry.get("valueSnapshots") or {}).get(side) or {}
1835
+ source_path = entry.get(side)
1836
+ if source_path is None:
1837
+ if stage == 1 and _git_blob(repo, stage, relative) is None:
1838
+ continue
1839
+ raise ConflictError(f"Manifest source missing for {relative} ({side}).")
1840
+ source = Path(source_path).resolve()
1841
+ if not source.exists():
1842
+ raise ConflictError(f"Manifest source missing for {relative} ({side}): {source}")
1843
+ expected_source = meta.get("sourceSha256")
1844
+ actual_source = _sha256_path(source)
1845
+ if expected_source and actual_source != expected_source:
1846
+ raise ConflictError(f"Manifest source changed for {relative} ({side}); refusing apply.")
1847
+ indexed = _git_blob(repo, stage, relative)
1848
+ if indexed is None:
1849
+ raise ConflictError(f"Git index stage {stage} changed or disappeared for {relative} ({side}).")
1850
+ if expected_source and hashlib.sha256(indexed).hexdigest() != expected_source:
1851
+ raise ConflictError(f"Git index source changed for {relative} ({side}); re-run prepare.")
1852
+ value_path = meta.get("valuePath")
1853
+ expected_value = meta.get("valueSha256")
1854
+ if not value_path or not expected_value:
1855
+ raise ConflictError(f"Manifest has no immutable value snapshot for {relative} ({side}).")
1856
+ snapshot = Path(value_path).resolve()
1857
+ if not snapshot.exists() or _sha256_path(snapshot) != expected_value:
1858
+ raise ConflictError(f"Immutable value snapshot changed for {relative} ({side}); refusing apply.")
1859
+ _validate_value_workbook(snapshot)
1860
+ output = (repo / Path(relative)).resolve()
1861
+ expected_output = entry.get("outputSha256")
1862
+ if expected_output is None:
1863
+ if output.exists():
1864
+ raise ConflictError(f"Worktree output appeared after prepare for {relative}; refusing overwrite.")
1865
+ elif not output.exists() or _sha256_path(output) != expected_output:
1866
+ raise ConflictError(f"Worktree output changed for {relative}; re-run prepare before apply.")
1867
+ for entry in manifest.get("autoTheirs") or []:
1868
+ relative = entry["path"]
1869
+ blob = _git_blob(repo, 3, relative)
1870
+ if blob is None:
1871
+ raise ConflictError(f"{relative} requires remote stage 3, but it is no longer available.")
1872
+ expected = entry.get("theirsSha256")
1873
+ if expected and hashlib.sha256(blob).hexdigest() != expected:
1874
+ raise ConflictError(f"Remote source changed for automatic table {relative}; re-run prepare.")
1875
+ output = (repo / Path(relative)).resolve()
1876
+ expected_output = entry.get("outputSha256")
1877
+ if expected_output is None:
1878
+ if output.exists():
1879
+ raise ConflictError(f"Worktree output appeared after prepare for {relative}; refusing overwrite.")
1880
+ elif not output.exists() or _sha256_path(output) != expected_output:
1881
+ raise ConflictError(f"Worktree output changed for {relative}; re-run prepare before apply.")
1882
+
1883
+
1884
+ def _replace_prepared_outputs(prepared: list[tuple[Path, Path]]) -> None:
1885
+ """Replace only after all outputs exist; roll back earlier swaps on failure."""
1886
+ backups: dict[Path, Path | None] = {}
1887
+ replaced: list[Path] = []
1888
+ retained: set[Path] = set()
1889
+ try:
1890
+ for output, _ in prepared:
1891
+ if output.exists():
1892
+ fd, backup_name = tempfile.mkstemp(prefix=f".{output.stem}-backup-", suffix=output.suffix, dir=output.parent)
1893
+ os.close(fd)
1894
+ backup = Path(backup_name)
1895
+ backups[output] = backup
1896
+ shutil.copy2(output, backup)
1897
+ else:
1898
+ backups[output] = None
1899
+ try:
1900
+ for output, temporary in prepared:
1901
+ os.replace(temporary, output)
1902
+ replaced.append(output)
1903
+ except Exception as exc:
1904
+ errors = []
1905
+ for output in reversed(replaced):
1906
+ backup = backups[output]
1907
+ try:
1908
+ if backup is None:
1909
+ output.unlink(missing_ok=True)
1910
+ else:
1911
+ os.replace(backup, output)
1912
+ except Exception as restore_error:
1913
+ if backup is not None:
1914
+ retained.add(backup)
1915
+ errors.append(f"{output}: {restore_error}; recovery copy: {backup}")
1916
+ if errors:
1917
+ raise ConflictError("Output replacement failed; recovery copies retained: " + "; ".join(errors)) from exc
1918
+ raise ConflictError("Output replacement failed; earlier replacements rolled back; Git index unchanged.") from exc
1919
+ finally:
1920
+ for backup in backups.values():
1921
+ if backup is not None and backup not in retained:
1922
+ backup.unlink(missing_ok=True)
1923
+
1924
+
1557
1925
  def apply_manifest(
1558
1926
  manifest_path: Path | str,
1559
1927
  decisions: dict[str, Any],
@@ -1565,21 +1933,46 @@ def apply_manifest(
1565
1933
  manifest = load_manifest(manifest_path)
1566
1934
  _validate_decisions(manifest, decisions)
1567
1935
  repo = find_repo_root(manifest["repoRoot"])
1936
+ # Validate every immutable source, calculated-value snapshot, and worktree
1937
+ # output before creating any replacement. A failed preflight leaves both
1938
+ # the worktree and Git index untouched.
1939
+ _validate_manifest_sources(manifest, repo)
1568
1940
  auto_entries = manifest.get("autoTheirs") or []
1569
1941
  if not manifest["files"] and not auto_entries:
1570
1942
  raise ConflictError("Manifest has no conflicts to resolve.")
1571
1943
  prepared: list[tuple[Path, Path]] = []
1944
+ manual_outputs: list[Path] = []
1945
+ auto_outputs: list[Path] = []
1572
1946
  try:
1573
1947
  for file_entry in manifest["files"]:
1574
1948
  output = Path(file_entry["output"]).resolve()
1575
1949
  if os.path.commonpath([str(repo), str(output)]) != str(repo):
1576
- raise ConflictError(f"Output escapes meta repository: {output}")
1950
+ raise ConflictError(f"Output escapes repository: {output}")
1577
1951
  prepared.append((output, _build_merged_file(file_entry, decisions)))
1578
- for output, temp_path in prepared:
1579
- os.replace(temp_path, output)
1580
- auto_outputs = _apply_auto_theirs(repo, auto_entries)
1581
-
1582
- outputs = [output for output, _ in prepared] + auto_outputs
1952
+ manual_outputs.append(output)
1953
+ for entry in auto_entries:
1954
+ output = (repo / entry["path"]).resolve()
1955
+ if os.path.commonpath([str(repo), str(output)]) != str(repo):
1956
+ raise ConflictError(f"Output escapes repository: {output}")
1957
+ blob = _git_blob(repo, 3, entry["path"])
1958
+ if blob is None or hashlib.sha256(blob).hexdigest() != entry.get("theirsSha256"):
1959
+ raise ConflictError(f"Remote source changed for automatic table {entry['path']}.")
1960
+ output.parent.mkdir(parents=True, exist_ok=True)
1961
+ fd, temporary_name = tempfile.mkstemp(
1962
+ prefix=f".{output.stem}-", suffix=output.suffix, dir=output.parent
1963
+ )
1964
+ temporary = Path(temporary_name)
1965
+ prepared.append((output, temporary))
1966
+ with os.fdopen(fd, "wb") as handle:
1967
+ handle.write(blob)
1968
+ auto_outputs.append(output)
1969
+
1970
+ # Builds may be expensive, so check the inputs once more immediately
1971
+ # before the atomic batch replacement.
1972
+ _validate_manifest_sources(manifest, repo)
1973
+ _replace_prepared_outputs(prepared)
1974
+
1975
+ outputs = [output for output, _ in prepared]
1583
1976
  relative_paths = [str(output.relative_to(repo)).replace("\\", "/") for output in outputs]
1584
1977
  steps = [_git_step(repo, ["add", "--", *relative_paths], "git add")]
1585
1978
  success = steps[-1]["success"]
@@ -1592,7 +1985,7 @@ def apply_manifest(
1592
1985
  success = steps[-1]["success"]
1593
1986
  return {
1594
1987
  "success": success,
1595
- "merged": [str(output) for output, _ in prepared],
1988
+ "merged": [str(output) for output in manual_outputs],
1596
1989
  "autoTheirs": [str(output.relative_to(repo)).replace("\\", "/") for output in auto_outputs],
1597
1990
  "steps": steps,
1598
1991
  }