@kevlns/xlmerge 2.0.0 → 2.0.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.
@@ -1,1611 +1,1611 @@
1
- """Debug bridge: project three XLSX versions to JSONL and let Git merge rows.
2
-
3
- This tool is intentionally independent from the production XLSX resolver. It
4
- uses ``git merge-file`` only to segment clean row edits from candidate conflict
5
- blocks. The generated sidecars retain workbook row numbers and complete cell
6
- values so later experiments can map text results back to XLSX safely.
7
- """
8
-
9
- from __future__ import annotations
10
-
11
- import argparse
12
- from difflib import SequenceMatcher
13
- import hashlib
14
- import json
15
- import re
16
- import subprocess
17
- import sys
18
- from dataclasses import dataclass
19
- from functools import lru_cache
20
- from datetime import date, datetime, time
21
- from pathlib import Path
22
- from typing import Any, Iterable
23
-
24
- from openpyxl import load_workbook
25
- from openpyxl.utils import get_column_letter
26
-
27
-
28
- MISSING = object()
29
-
30
-
31
- class BridgeError(RuntimeError):
32
- """Raised when an XLSX projection or Git text merge cannot be completed."""
33
-
34
-
35
- def _json_value(value: Any) -> Any:
36
- if value is None or isinstance(value, (bool, int, float, str)):
37
- return value
38
- if isinstance(value, (datetime, date, time)):
39
- return value.isoformat()
40
- return str(value)
41
-
42
-
43
- def _line(values: Iterable[Any]) -> str:
44
- return json.dumps(list(values), ensure_ascii=False, separators=(",", ":"))
45
-
46
-
47
- def _hash_line(line: str) -> str:
48
- return hashlib.sha256(line.encode("utf-8")).hexdigest()
49
-
50
-
51
- def _safe_name(name: str, index: int) -> str:
52
- slug = re.sub(r"[^0-9A-Za-z._-]+", "_", name).strip("._") or "sheet"
53
- return f"{index:02d}-{slug[:80]}"
54
-
55
-
56
- @dataclass(frozen=True)
57
- class SheetLayout:
58
- name: str
59
- var_row: int | None
60
- data_start_row: int
61
- fields: tuple[str, ...]
62
- columns: dict[str, int]
63
-
64
-
65
- @dataclass(frozen=True)
66
- class RowProjection:
67
- xlsx_row: int
68
- projection: tuple[Any, ...]
69
- all_cells: dict[str, Any]
70
- empty: bool = False
71
-
72
- @property
73
- def text(self) -> str:
74
- return _line(self.projection)
75
-
76
-
77
- @dataclass(frozen=True)
78
- class PairAlignment:
79
- matches: dict[int, int]
80
- similarities: dict[int, float]
81
- side_only: tuple[int, ...]
82
- base_only: tuple[int, ...]
83
-
84
-
85
- def _last_semantic_column(ws) -> int:
86
- """Ignore styled trailing placeholders but retain every column inside the used range."""
87
- last = 1
88
- for cell in ws._cells.values():
89
- if (
90
- cell.value not in (None, "")
91
- or getattr(cell, "comment", None) is not None
92
- or getattr(cell, "hyperlink", None) is not None
93
- ):
94
- last = max(last, cell.column)
95
- for merged_range in ws.merged_cells.ranges:
96
- last = max(last, merged_range.max_col)
97
- return last
98
-
99
-
100
- def _layout(ws) -> SheetLayout:
101
- var_row = None
102
- for row in range(1, min(ws.max_row or 1, 40) + 1):
103
- if str(ws.cell(row, 1).value or "").strip() == "##var":
104
- var_row = row
105
- break
106
-
107
- columns: dict[str, int] = {}
108
- if var_row is None:
109
- for column in range(1, _last_semantic_column(ws) + 1):
110
- columns[f"@{column}"] = column
111
- return SheetLayout(ws.title, None, 1, tuple(columns), columns)
112
-
113
- columns["@1"] = 1
114
- occurrences: dict[str, int] = {}
115
- for column in range(2, _last_semantic_column(ws) + 1):
116
- raw_name = ws.cell(var_row, column).value
117
- if raw_name is None or str(raw_name).strip() == "":
118
- columns[f"@{column}"] = column
119
- continue
120
- base_name = str(raw_name).strip()
121
- occurrences[base_name] = occurrences.get(base_name, 0) + 1
122
- suffix = occurrences[base_name]
123
- name = base_name if suffix == 1 else f"{base_name}#{suffix}"
124
- columns[name] = column
125
-
126
- row = var_row
127
- while row <= (ws.max_row or var_row):
128
- marker = ws.cell(row, 1).value
129
- if not isinstance(marker, str) or not marker.startswith("##"):
130
- break
131
- row += 1
132
- return SheetLayout(ws.title, var_row, row, tuple(columns), columns)
133
-
134
-
135
- def _active_rows(ws, layout: SheetLayout) -> list[int]:
136
- return [
137
- row
138
- for row in range(layout.data_start_row, (ws.max_row or 0) + 1)
139
- if any(ws.cell(row, column).value is not None for column in layout.columns.values())
140
- ]
141
-
142
-
143
- @lru_cache(maxsize=65536)
144
- def _token_string(value: Any) -> str:
145
- return _line((_json_value(value),))
146
-
147
-
148
- def _column_tokens(ws, rows: Iterable[int], column: int) -> tuple[str, ...]:
149
- return tuple(_token_string(ws.cell(row, column).value) for row in rows)
150
-
151
-
152
- def _sheet_column_tokens(
153
- ws, active_rows: list[int], columns: list[int]
154
- ) -> list[tuple[str, ...]]:
155
- """一次 iter_rows 读取整表,按列生成 token 序列(大表避免逐 cell 访问)。"""
156
- if not active_rows or not columns:
157
- return [() for _ in columns]
158
- row_set = set(active_rows)
159
- first = min(active_rows)
160
- last = max(active_rows)
161
- buckets: list[list[str]] = [[] for _ in columns]
162
- for offset, values in enumerate(
163
- ws.iter_rows(min_row=first, max_row=last, values_only=True), first
164
- ):
165
- if offset not in row_set:
166
- continue
167
- for bucket, column in zip(buckets, columns):
168
- bucket.append(
169
- _token_string(values[column - 1] if values is not None else None)
170
- )
171
- return [tuple(bucket) for bucket in buckets]
172
-
173
-
174
- def _column_similarity(left: tuple[str, ...], right: tuple[str, ...]) -> float:
175
- if not left and not right:
176
- return 1.0
177
- if not left or not right:
178
- return 0.0
179
- # 频次配对(线性):SequenceMatcher 在长序列且大部分不同的场景 O(n*m) 退化
180
- counts: dict[str, int] = {}
181
- for token in left:
182
- counts[token] = counts.get(token, 0) + 1
183
- matched = 0
184
- for token in right:
185
- if counts.get(token, 0):
186
- counts[token] -= 1
187
- matched += 1
188
- # 上限截断:公共 token 数不超过 min 长度,保证相似度 ∈ [0, 1]
189
- return min(matched, len(left), len(right)) / min(len(left), len(right))
190
-
191
-
192
- def _align_column_sequences(
193
- base_columns: list[tuple[str, ...]],
194
- side_columns: list[tuple[str, ...]],
195
- *,
196
- minimum_similarity: float = 0.0,
197
- ) -> PairAlignment:
198
- """Globally align ordered columns while allowing inserts and deletes."""
199
- base_count = len(base_columns)
200
- side_count = len(side_columns)
201
- gap = -0.40
202
- negative = float("-inf")
203
- scores = [[negative] * (side_count + 1) for _ in range(base_count + 1)]
204
- moves: list[list[str | None]] = [
205
- [None] * (side_count + 1) for _ in range(base_count + 1)
206
- ]
207
- scores[0][0] = 0.0
208
- for base_index in range(1, base_count + 1):
209
- scores[base_index][0] = base_index * gap
210
- moves[base_index][0] = "delete"
211
- for side_index in range(1, side_count + 1):
212
- scores[0][side_index] = side_index * gap
213
- moves[0][side_index] = "insert"
214
-
215
- similarities = [
216
- [
217
- _column_similarity(base_columns[base_index], side_columns[side_index])
218
- for side_index in range(side_count)
219
- ]
220
- for base_index in range(base_count)
221
- ]
222
- priority = {"match": 2, "delete": 1, "insert": 0}
223
- for base_index in range(1, base_count + 1):
224
- for side_index in range(1, side_count + 1):
225
- candidates = [
226
- (scores[base_index - 1][side_index] + gap, "delete"),
227
- (scores[base_index][side_index - 1] + gap, "insert"),
228
- ]
229
- similarity = similarities[base_index - 1][side_index - 1]
230
- if similarity >= minimum_similarity:
231
- candidates.append(
232
- (scores[base_index - 1][side_index - 1] + similarity, "match")
233
- )
234
- score, move = max(candidates, key=lambda item: (item[0], priority[item[1]]))
235
- scores[base_index][side_index] = score
236
- moves[base_index][side_index] = move
237
-
238
- matches: dict[int, int] = {}
239
- match_similarities: dict[int, float] = {}
240
- base_only: list[int] = []
241
- side_only: list[int] = []
242
- base_index = base_count
243
- side_index = side_count
244
- while base_index or side_index:
245
- move = moves[base_index][side_index]
246
- if move == "match":
247
- left = base_index
248
- right = side_index
249
- matches[left] = right
250
- match_similarities[left] = similarities[left - 1][right - 1]
251
- base_index -= 1
252
- side_index -= 1
253
- elif move == "delete":
254
- base_only.append(base_index)
255
- base_index -= 1
256
- elif move == "insert":
257
- side_only.append(side_index)
258
- side_index -= 1
259
- else:
260
- raise BridgeError("Column sequence alignment backtracking failed.")
261
-
262
- base_only_set = set(base_only)
263
- side_only_set = set(side_only)
264
- anchors = [(0, 0), *sorted(matches.items()), (base_count + 1, side_count + 1)]
265
- for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
266
- base_gap = [
267
- index for index in range(left_base + 1, right_base)
268
- if index in base_only_set
269
- ]
270
- side_gap = [
271
- index for index in range(left_side + 1, right_side)
272
- if index in side_only_set
273
- ]
274
- if not base_gap or len(base_gap) != len(side_gap):
275
- continue
276
- for base_position, side_position in zip(base_gap, side_gap):
277
- matches[base_position] = side_position
278
- match_similarities[base_position] = similarities[base_position - 1][side_position - 1]
279
- base_only_set.remove(base_position)
280
- side_only_set.remove(side_position)
281
-
282
- return PairAlignment(
283
- matches=matches,
284
- similarities=match_similarities,
285
- side_only=tuple(sorted(side_only_set)),
286
- base_only=tuple(sorted(base_only_set)),
287
- )
288
-
289
-
290
- def _insertion_anchor(column: int, matches: dict[int, int]) -> int:
291
- return max((base for base, side in matches.items() if side < column), default=0)
292
-
293
-
294
- def _logical_insert_name(side: str, physical_column: int) -> str:
295
- return f"@{side}+{get_column_letter(physical_column)}"
296
-
297
-
298
- def _is_positional_field(field: str) -> bool:
299
- return re.fullmatch(r"@\d+", field) is not None
300
-
301
-
302
- def _align_content_layouts(
303
- worksheets: dict[str, Any], layouts: dict[str, SheetLayout]
304
- ) -> tuple[dict[str, SheetLayout], list[dict[str, Any]]]:
305
- entries = {
306
- side: [(field, layouts[side].columns[field]) for field in layouts[side].fields]
307
- for side in ("base", "ours", "theirs")
308
- }
309
- active_rows = {
310
- side: _active_rows(worksheets[side], layouts[side])
311
- for side in ("base", "ours", "theirs")
312
- }
313
- vectors = {
314
- side: _sheet_column_tokens(
315
- worksheets[side],
316
- active_rows[side],
317
- [column for _, column in entries[side]],
318
- )
319
- for side in ("base", "ours", "theirs")
320
- }
321
- pair = {
322
- side: _align_column_sequences(vectors["base"], vectors[side])
323
- for side in ("ours", "theirs")
324
- }
325
- aligned_columns: dict[str, dict[str, int]] = {
326
- "base": {field: column for field, column in entries["base"]},
327
- "ours": {},
328
- "theirs": {},
329
- }
330
- column_report: list[dict[str, Any]] = []
331
- for base_position, (field, base_physical) in enumerate(entries["base"], 1):
332
- ours_position = pair["ours"].matches.get(base_position)
333
- theirs_position = pair["theirs"].matches.get(base_position)
334
- ours_physical = entries["ours"][ours_position - 1][1] if ours_position else None
335
- theirs_physical = entries["theirs"][theirs_position - 1][1] if theirs_position else None
336
- ours_label = entries["ours"][ours_position - 1][0] if ours_position else None
337
- theirs_label = entries["theirs"][theirs_position - 1][0] if theirs_position else None
338
- if ours_physical is not None:
339
- aligned_columns["ours"][field] = ours_physical
340
- if theirs_physical is not None:
341
- aligned_columns["theirs"][field] = theirs_physical
342
- if ours_physical is None and theirs_physical is None:
343
- status = "deleted_both"
344
- elif ours_physical is None:
345
- status = "deleted_ours"
346
- elif theirs_physical is None:
347
- status = "deleted_theirs"
348
- else:
349
- status = "common"
350
- column_report.append({
351
- "field": field,
352
- "status": status,
353
- "labels": {"base": field, "ours": ours_label, "theirs": theirs_label},
354
- "baseColumn": get_column_letter(base_physical),
355
- "oursColumn": get_column_letter(ours_physical) if ours_physical else None,
356
- "theirsColumn": get_column_letter(theirs_physical) if theirs_physical else None,
357
- "oursSimilarity": pair["ours"].similarities.get(base_position),
358
- "theirsSimilarity": pair["theirs"].similarities.get(base_position),
359
- })
360
-
361
- unmatched = {
362
- side: [
363
- (column, _insertion_anchor(column, pair[side].matches))
364
- for column in pair[side].side_only
365
- ]
366
- for side in ("ours", "theirs")
367
- }
368
- anchors = sorted({anchor for values in unmatched.values() for _, anchor in values})
369
- inserted_fields: dict[int, list[str]] = {}
370
- used_fields = set(aligned_columns["base"])
371
-
372
- def inserted_field(side: str, position: int) -> str:
373
- label, physical = entries[side][position - 1]
374
- if not _is_positional_field(label) and label not in used_fields:
375
- used_fields.add(label)
376
- return label
377
- candidate = _logical_insert_name(side, physical)
378
- suffix = 2
379
- while candidate in used_fields:
380
- candidate = f"{_logical_insert_name(side, physical)}#{suffix}"
381
- suffix += 1
382
- used_fields.add(candidate)
383
- return candidate
384
-
385
- for anchor in anchors:
386
- ours_positions = [column for column, value in unmatched["ours"] if value == anchor]
387
- theirs_positions = [column for column, value in unmatched["theirs"] if value == anchor]
388
- matched_theirs: set[int] = set()
389
- for ours_position in ours_positions:
390
- theirs_position = next((
391
- candidate for candidate in theirs_positions
392
- if candidate not in matched_theirs
393
- and vectors["ours"][ours_position - 1] == vectors["theirs"][candidate - 1]
394
- ), None)
395
- if theirs_position is None:
396
- label, physical = entries["ours"][ours_position - 1]
397
- field = inserted_field("ours", ours_position)
398
- inserted_fields.setdefault(anchor, []).append(field)
399
- aligned_columns["ours"][field] = physical
400
- column_report.append({
401
- "field": field,
402
- "status": "added_ours",
403
- "labels": {"base": None, "ours": label},
404
- "baseColumn": None,
405
- "oursColumn": get_column_letter(physical),
406
- "theirsColumn": None,
407
- "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
408
- })
409
- continue
410
- ours_label, ours_physical = entries["ours"][ours_position - 1]
411
- theirs_label, theirs_physical = entries["theirs"][theirs_position - 1]
412
- if ours_label == theirs_label and not _is_positional_field(ours_label) and ours_label not in used_fields:
413
- field = ours_label
414
- used_fields.add(field)
415
- else:
416
- field = f"@both+{anchor + 1}.{len(matched_theirs)}"
417
- used_fields.add(field)
418
- inserted_fields.setdefault(anchor, []).append(field)
419
- aligned_columns["ours"][field] = ours_physical
420
- aligned_columns["theirs"][field] = theirs_physical
421
- column_report.append({
422
- "field": field,
423
- "status": "added_both_same",
424
- "labels": {"base": None, "ours": ours_label, "theirs": theirs_label},
425
- "baseColumn": None,
426
- "oursColumn": get_column_letter(ours_physical),
427
- "theirsColumn": get_column_letter(theirs_physical),
428
- "oursTheirsSimilarity": 1.0,
429
- "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
430
- })
431
- matched_theirs.add(theirs_position)
432
- for theirs_position in theirs_positions:
433
- if theirs_position in matched_theirs:
434
- continue
435
- label, physical = entries["theirs"][theirs_position - 1]
436
- field = inserted_field("theirs", theirs_position)
437
- inserted_fields.setdefault(anchor, []).append(field)
438
- aligned_columns["theirs"][field] = physical
439
- column_report.append({
440
- "field": field,
441
- "status": "added_theirs",
442
- "labels": {"base": None, "theirs": label},
443
- "baseColumn": None,
444
- "oursColumn": None,
445
- "theirsColumn": get_column_letter(physical),
446
- "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
447
- })
448
-
449
- logical_order = list(inserted_fields.get(0, ()))
450
- for base_position, (field, _) in enumerate(entries["base"], 1):
451
- logical_order.append(field)
452
- logical_order.extend(inserted_fields.get(base_position, ()))
453
- aligned_layouts = {}
454
- for side in ("base", "ours", "theirs"):
455
- columns = aligned_columns[side]
456
- fields = tuple(field for field in logical_order if field in columns)
457
- source = layouts[side]
458
- aligned_layouts[side] = SheetLayout(
459
- source.name, source.var_row, source.data_start_row, fields, columns
460
- )
461
- report_by_field = {entry["field"]: entry for entry in column_report}
462
- return aligned_layouts, [report_by_field[field] for field in logical_order]
463
-
464
-
465
- def _align_layouts(
466
- worksheets: dict[str, Any], layouts: dict[str, SheetLayout]
467
- ) -> tuple[dict[str, SheetLayout], list[dict[str, Any]], str]:
468
- aligned, report = _align_content_layouts(worksheets, layouts)
469
- return aligned, report, "content_anchor_sequence"
470
-
471
-
472
- def _rows(ws, layout: SheetLayout, projection_fields: tuple[str, ...]) -> list[RowProjection]:
473
- result: list[RowProjection] = []
474
- columns = [(field, column) for field, column in layout.columns.items()]
475
- for offset, values in enumerate(
476
- ws.iter_rows(
477
- min_row=layout.data_start_row,
478
- max_row=ws.max_row or layout.data_start_row,
479
- values_only=True,
480
- ),
481
- layout.data_start_row,
482
- ):
483
- all_cells = {
484
- field: _json_value(values[column - 1] if values is not None else None)
485
- for field, column in columns
486
- }
487
- projection = tuple(all_cells.get(field) for field in projection_fields)
488
- if not any(value is not None for value in all_cells.values()):
489
- # 空行保留:投影全 None,empty 标记供对齐判断
490
- result.append(RowProjection(offset, projection, all_cells, empty=True))
491
- continue
492
- result.append(RowProjection(offset, projection, all_cells))
493
- return result
494
-
495
-
496
- def _paired_projected_rows(
497
- base_rows: list[RowProjection], side_rows: list[RowProjection]
498
- ) -> tuple[list[tuple[RowProjection, RowProjection]], list[RowProjection]]:
499
- """Pair preserved/replaced rows and return rows inserted only on one side."""
500
- matches, insertions = _row_side_alignment(base_rows, side_rows)
501
- pairs = [
502
- (base_rows[base_index], side_rows[side_index])
503
- for base_index, side_index in sorted(matches.items())
504
- ]
505
- inserted = [
506
- side_rows[side_index]
507
- for anchor in sorted(insertions)
508
- for side_index in insertions[anchor]
509
- ]
510
- return pairs, inserted
511
-
512
-
513
- def _row_similarity(left: RowProjection, right: RowProjection) -> float:
514
- if not left.projection and not right.projection:
515
- return 1.0
516
- size = max(len(left.projection), len(right.projection))
517
- if not size:
518
- return 0.0
519
- limit = min(len(left.projection), len(right.projection))
520
- equal = 0
521
- for index in range(limit):
522
- if left.projection[index] == right.projection[index]:
523
- equal += 1
524
- return equal / size
525
-
526
-
527
- _FAST_ALIGN_THRESHOLD = 1_000_000 # base*side 超过该值走分层快速对齐
528
- _SIMILAR_MATCH_MIN = 0.3 # match 分 2s-1 > gap(-0.4) 的临界相似度
529
-
530
-
531
- def _similar_anchors_in_gap(
532
- base_rows: list[RowProjection],
533
- side_rows: list[RowProjection],
534
- base_gap: list[int],
535
- side_gap: list[int],
536
- matches: dict[int, int],
537
- used_base: set[int],
538
- used_side: set[int],
539
- ) -> None:
540
- """大区间退化场景:空行同序配对 + 候选剪枝相似锚点,其余按删+插。"""
541
- b_empty = [i for i in base_gap if base_rows[i].empty]
542
- s_empty = [j for j in side_gap if side_rows[j].empty]
543
- for i, j in zip(b_empty, s_empty):
544
- matches[i] = j
545
- used_base.add(i)
546
- used_side.add(j)
547
- index: dict[tuple[int, Any], list[int]] = {}
548
- for j in side_gap:
549
- if j in used_side:
550
- continue
551
- for p, v in enumerate(side_rows[j].projection):
552
- if v is None:
553
- continue
554
- index.setdefault((p, v), []).append(j)
555
- side_gap_set = set(side_gap)
556
- last_side = max(
557
- (j for _, j in matches.items() if j in used_side and j in side_gap_set),
558
- default=-1,
559
- )
560
- for i in base_gap:
561
- if i in used_base:
562
- continue
563
- candidates: set[int] = set()
564
- for p, v in enumerate(base_rows[i].projection):
565
- if v is None:
566
- continue
567
- candidates.update(index.get((p, v), ()))
568
- best = None
569
- best_score = _SIMILAR_MATCH_MIN
570
- for j in candidates:
571
- if j in used_side or j <= last_side:
572
- continue
573
- score = _row_similarity(base_rows[i], side_rows[j])
574
- if score >= best_score:
575
- best_score = score
576
- best = j
577
- if best is not None:
578
- matches[i] = best
579
- used_base.add(i)
580
- used_side.add(best)
581
- last_side = best
582
-
583
-
584
- def _fast_row_side_alignment(
585
- base_rows: list[RowProjection], side_rows: list[RowProjection]
586
- ) -> tuple[dict[int, int], dict[int, list[int]]]:
587
- """大数据量分层对齐:hash 锚点切区间 → 区间内局部 DP / 候选剪枝。
588
-
589
- 与全量 DP 输出内容等价(配对与删+插的写回结果一致),仅行状态标注可能有差异。
590
- """
591
- base_count = len(base_rows)
592
- side_count = len(side_rows)
593
- matches: dict[int, int] = {}
594
- used_base: set[int] = set()
595
- used_side: set[int] = set()
596
-
597
- # 阶段 1:hash 锚点(投影相同的行直接配对)
598
- base_hashes = [_hash_line(row.text) for row in base_rows]
599
- side_hashes = [_hash_line(row.text) for row in side_rows]
600
- matcher = SequenceMatcher(None, base_hashes, side_hashes, autojunk=False)
601
- for op, b1, b2, s1, s2 in matcher.get_opcodes():
602
- if op != "equal":
603
- continue
604
- for offset in range(b2 - b1):
605
- matches[b1 + offset] = s1 + offset
606
- used_base.add(b1 + offset)
607
- used_side.add(s1 + offset)
608
-
609
- # 阶段 2:按锚点切区间处理未匹配行
610
- anchors = [(-1, -1), *sorted(matches.items()), (base_count, side_count)]
611
- for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
612
- base_gap = [i for i in range(left_base + 1, right_base) if i not in used_base]
613
- side_gap = [i for i in range(left_side + 1, right_side) if i not in used_side]
614
- if not base_gap and not side_gap:
615
- continue
616
- if len(base_gap) * len(side_gap) <= _FAST_ALIGN_THRESHOLD:
617
- # 区间小:局部 DP,与全量语义一致(含空行/低相似同序行)
618
- local_matches, _ = _row_side_alignment(
619
- [base_rows[i] for i in base_gap], [side_rows[j] for j in side_gap]
620
- )
621
- for local_base, local_side in local_matches.items():
622
- matches[base_gap[local_base]] = side_gap[local_side]
623
- used_base.add(base_gap[local_base])
624
- used_side.add(side_gap[local_side])
625
- continue
626
- # 区间大(大量行被改):候选剪枝近似处理
627
- _similar_anchors_in_gap(
628
- base_rows, side_rows, base_gap, side_gap, matches, used_base, used_side
629
- )
630
-
631
- # 阶段 3:插入归属(与全量路径一致)
632
- insertion_indexes = []
633
- for inserted_index in range(side_count):
634
- if inserted_index in used_side:
635
- continue
636
- anchor = max(
637
- (matched_base + 1 for matched_base, matched_side in matches.items() if matched_side < inserted_index),
638
- default=0,
639
- )
640
- insertion_indexes.append((anchor, inserted_index))
641
- insertions: dict[int, list[int]] = {}
642
- last_matched_base = max(matches, default=-1)
643
- last_matched_side = max(matches.values(), default=-1)
644
- for anchor, inserted_index in insertion_indexes:
645
- if inserted_index > last_matched_side and anchor > last_matched_base:
646
- anchor = base_count
647
- insertions.setdefault(anchor, []).append(inserted_index)
648
- return matches, insertions
649
-
650
-
651
- def _row_side_alignment(
652
- base_rows: list[RowProjection], side_rows: list[RowProjection]
653
- ) -> tuple[dict[int, int], dict[int, list[int]]]:
654
- # Constant columns (including padding, flags and repeated formula results)
655
- # provide no evidence of row identity. Counting them as matches can make a
656
- # deleted row plus an insertion score higher than the surviving record.
657
- # Drop them only from the alignment projection; all_cells stays intact for
658
- # the actual three-way comparison and output. If all columns are constant,
659
- # retain the original sequence semantics for duplicate/empty rows.
660
- combined = [*base_rows, *side_rows]
661
- width = max((len(row.projection) for row in combined), default=0)
662
- informative = [
663
- column for column in range(width)
664
- if len({
665
- _line([row.projection[column] if column < len(row.projection) else None])
666
- for row in combined
667
- }) > 1
668
- ]
669
- if informative and len(informative) < width:
670
- def project(rows):
671
- return [RowProjection(
672
- row.xlsx_row,
673
- tuple(row.projection[column] if column < len(row.projection) else None
674
- for column in informative),
675
- row.all_cells, row.empty,
676
- ) for row in rows]
677
- base_rows, side_rows = project(base_rows), project(side_rows)
678
- if len(base_rows) * len(side_rows) > _FAST_ALIGN_THRESHOLD:
679
- return _fast_row_side_alignment(base_rows, side_rows)
680
- base_count = len(base_rows)
681
- side_count = len(side_rows)
682
- gap = -0.40
683
- negative = float("-inf")
684
- scores = [[negative] * (side_count + 1) for _ in range(base_count + 1)]
685
- moves: list[list[str | None]] = [[None] * (side_count + 1) for _ in range(base_count + 1)]
686
- scores[0][0] = 0.0
687
- for base_index in range(1, base_count + 1):
688
- scores[base_index][0] = base_index * gap
689
- moves[base_index][0] = "delete"
690
- for side_index in range(1, side_count + 1):
691
- scores[0][side_index] = side_index * gap
692
- moves[0][side_index] = "insert"
693
-
694
- similarities = [
695
- [_row_similarity(base_row, side_row) for side_row in side_rows]
696
- for base_row in base_rows
697
- ]
698
- priority = {"match": 2, "delete": 1, "insert": 0}
699
- for base_index in range(1, base_count + 1):
700
- for side_index in range(1, side_count + 1):
701
- candidates = [
702
- (scores[base_index - 1][side_index] + gap, "delete"),
703
- (scores[base_index][side_index - 1] + gap, "insert"),
704
- (
705
- scores[base_index - 1][side_index - 1]
706
- + (2.0 * similarities[base_index - 1][side_index - 1] - 1.0),
707
- "match",
708
- ),
709
- ]
710
- score, move = max(candidates, key=lambda item: (item[0], priority[item[1]]))
711
- scores[base_index][side_index] = score
712
- moves[base_index][side_index] = move
713
-
714
- matches: dict[int, int] = {}
715
- base_index = base_count
716
- side_index = side_count
717
- while base_index or side_index:
718
- move = moves[base_index][side_index]
719
- if move == "match":
720
- matches[base_index - 1] = side_index - 1
721
- base_index -= 1
722
- side_index -= 1
723
- elif move == "delete":
724
- base_index -= 1
725
- elif move == "insert":
726
- side_index -= 1
727
- else:
728
- raise BridgeError("Row sequence alignment backtracking failed.")
729
-
730
- used_base = set(matches)
731
- used_side = set(matches.values())
732
- anchors = [(-1, -1), *sorted(matches.items()), (base_count, side_count)]
733
- for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
734
- base_gap = [index for index in range(left_base + 1, right_base) if index not in used_base]
735
- side_gap = [index for index in range(left_side + 1, right_side) if index not in used_side]
736
- if not base_gap or len(base_gap) != len(side_gap):
737
- continue
738
- if any(
739
- base_rows[left].empty != side_rows[right].empty
740
- for left, right in zip(base_gap, side_gap)
741
- ):
742
- continue
743
- pair_similarities = [similarities[left][right] for left, right in zip(base_gap, side_gap)]
744
- if base_count != side_count and not all(similarity > 0.0 for similarity in pair_similarities):
745
- continue
746
- for left, right in zip(base_gap, side_gap):
747
- matches[left] = right
748
- used_base.add(left)
749
- used_side.add(right)
750
-
751
- insertion_indexes = []
752
- for inserted_index in range(side_count):
753
- if inserted_index in used_side:
754
- continue
755
- anchor = max(
756
- (matched_base + 1 for matched_base, matched_side in matches.items() if matched_side < inserted_index),
757
- default=0,
758
- )
759
- insertion_indexes.append((anchor, inserted_index))
760
-
761
- insertions: dict[int, list[int]] = {}
762
- last_matched_base = max(matches, default=-1)
763
- last_matched_side = max(matches.values(), default=-1)
764
- for anchor, inserted_index in insertion_indexes:
765
- if inserted_index > last_matched_side and anchor > last_matched_base:
766
- anchor = base_count
767
- insertions.setdefault(anchor, []).append(inserted_index)
768
- return matches, insertions
769
-
770
-
771
- def _base_row_changed(base: RowProjection, side: RowProjection) -> bool:
772
- common = set(base.all_cells) & set(side.all_cells)
773
- return any(base.all_cells[field] != side.all_cells[field] for field in common)
774
-
775
-
776
- def _row_alignment_report(rows: dict[str, list[RowProjection]]) -> list[dict[str, Any]]:
777
- mappings = {
778
- side: _row_side_alignment(rows["base"], rows[side])
779
- for side in ("ours", "theirs")
780
- }
781
- result: list[dict[str, Any]] = []
782
- insertion_fields: dict[int, list[dict[str, Any]]] = {}
783
- anchors = sorted(set(mappings["ours"][1]) | set(mappings["theirs"][1]))
784
- for anchor in anchors:
785
- ours_indexes = list(mappings["ours"][1].get(anchor, ()))
786
- theirs_indexes = list(mappings["theirs"][1].get(anchor, ()))
787
- matched_theirs: set[int] = set()
788
- for ours_index in ours_indexes:
789
- ours_row = rows["ours"][ours_index]
790
- theirs_index = next((
791
- candidate for candidate in theirs_indexes
792
- if candidate not in matched_theirs
793
- and ours_row.projection == rows["theirs"][candidate].projection
794
- ), None)
795
- if theirs_index is None:
796
- insertion_fields.setdefault(anchor, []).append({
797
- "id": f"row:ours:{ours_row.xlsx_row}",
798
- "status": "added_ours",
799
- "baseRow": None,
800
- "oursRow": ours_row.xlsx_row,
801
- "theirsRow": None,
802
- "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
803
- })
804
- continue
805
- matched_theirs.add(theirs_index)
806
- theirs_row = rows["theirs"][theirs_index]
807
- insertion_fields.setdefault(anchor, []).append({
808
- "id": f"row:both:{ours_row.xlsx_row}:{theirs_row.xlsx_row}",
809
- "status": "added_both_same",
810
- "baseRow": None,
811
- "oursRow": ours_row.xlsx_row,
812
- "theirsRow": theirs_row.xlsx_row,
813
- "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
814
- })
815
- for theirs_index in theirs_indexes:
816
- if theirs_index in matched_theirs:
817
- continue
818
- theirs_row = rows["theirs"][theirs_index]
819
- insertion_fields.setdefault(anchor, []).append({
820
- "id": f"row:theirs:{theirs_row.xlsx_row}",
821
- "status": "added_theirs",
822
- "baseRow": None,
823
- "oursRow": None,
824
- "theirsRow": theirs_row.xlsx_row,
825
- "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
826
- })
827
-
828
- result.extend(insertion_fields.get(0, ()))
829
- for base_index, base_row in enumerate(rows["base"]):
830
- ours_index = mappings["ours"][0].get(base_index)
831
- theirs_index = mappings["theirs"][0].get(base_index)
832
- ours_row = rows["ours"][ours_index] if ours_index is not None else None
833
- theirs_row = rows["theirs"][theirs_index] if theirs_index is not None else None
834
- if ours_row is None and theirs_row is None:
835
- status = "deleted_both"
836
- elif ours_row is None:
837
- status = "delete_modify_ours" if _base_row_changed(base_row, theirs_row) else "deleted_ours"
838
- elif theirs_row is None:
839
- status = "delete_modify_theirs" if _base_row_changed(base_row, ours_row) else "deleted_theirs"
840
- else:
841
- status = "common"
842
- result.append({
843
- "id": f"row:base:{base_row.xlsx_row}",
844
- "status": status,
845
- "baseRow": base_row.xlsx_row,
846
- "oursRow": ours_row.xlsx_row if ours_row else None,
847
- "theirsRow": theirs_row.xlsx_row if theirs_row else None,
848
- })
849
- result.extend(insertion_fields.get(base_index + 1, ()))
850
- return result
851
-
852
-
853
- def _deleted_column_content_changes(
854
- field: str,
855
- base_rows: list[RowProjection],
856
- side_rows: list[RowProjection],
857
- side: str,
858
- ) -> list[dict[str, Any]]:
859
- pairs, inserted = _paired_projected_rows(base_rows, side_rows)
860
- changes: list[dict[str, Any]] = []
861
- for base_row, side_row in pairs:
862
- base_value = base_row.all_cells.get(field, MISSING)
863
- side_value = side_row.all_cells.get(field, MISSING)
864
- if base_value == side_value:
865
- continue
866
- changes.append({
867
- "kind": f"modified_{side}",
868
- "baseXlsxRow": base_row.xlsx_row,
869
- f"{side}XlsxRow": side_row.xlsx_row,
870
- "base": _display(base_value),
871
- side: _display(side_value),
872
- })
873
- for side_row in inserted:
874
- side_value = side_row.all_cells.get(field, MISSING)
875
- if side_value is MISSING or side_value is None:
876
- continue
877
- changes.append({
878
- "kind": f"inserted_{side}",
879
- "baseXlsxRow": None,
880
- f"{side}XlsxRow": side_row.xlsx_row,
881
- "base": None,
882
- side: _display(side_value),
883
- })
884
- return changes
885
-
886
-
887
- def _annotate_column_conflicts(
888
- column_alignment: list[dict[str, Any]],
889
- rows: dict[str, list[RowProjection]],
890
- ) -> int:
891
- conflict_count = 0
892
- for column in column_alignment:
893
- status = column["status"]
894
- if status == "deleted_theirs":
895
- survivor = "ours"
896
- deleted = "theirs"
897
- elif status == "deleted_ours":
898
- survivor = "theirs"
899
- deleted = "ours"
900
- else:
901
- continue
902
- changes = _deleted_column_content_changes(
903
- column["field"], rows["base"], rows[survivor], survivor
904
- )
905
- if not changes:
906
- continue
907
- column["status"] = f"delete_modify_{deleted}"
908
- column["conflict"] = True
909
- column["contentChanges"] = changes
910
- conflict_count += 1
911
- return conflict_count
912
-
913
-
914
- def _write_text_lf(path: Path, content: str) -> None:
915
- """3.9-compatible LF write (Path.write_text gained `newline` only in 3.10)."""
916
- with path.open("w", encoding="utf-8", newline="\n") as handle:
917
- handle.write(content)
918
-
919
-
920
- def _write_projection(path: Path, rows: list[RowProjection]) -> None:
921
- content = "\n".join(row.text for row in rows)
922
- if content:
923
- content += "\n"
924
- _write_text_lf(path, content)
925
-
926
-
927
- def _write_sidecar(path: Path, layout: SheetLayout, rows: list[RowProjection]) -> None:
928
- payload = {
929
- "sheet": layout.name,
930
- "varRow": layout.var_row,
931
- "dataStartRow": layout.data_start_row,
932
- "fields": list(layout.fields),
933
- "rows": [
934
- {
935
- "textLine": index,
936
- "xlsxRow": row.xlsx_row,
937
- "hash": _hash_line(row.text),
938
- "projection": list(row.projection),
939
- "cells": row.all_cells,
940
- }
941
- for index, row in enumerate(rows, 1)
942
- ],
943
- }
944
- path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
945
-
946
-
947
- def _git_merge_file(ours: Path, base: Path, theirs: Path) -> tuple[str, bool, str]:
948
- result = subprocess.run(
949
- [
950
- "git", "merge-file", "-p", "--diff3",
951
- "-L", "OURS", "-L", "BASE", "-L", "THEIRS",
952
- str(ours), str(base), str(theirs),
953
- ],
954
- capture_output=True,
955
- text=True,
956
- encoding="utf-8",
957
- errors="replace",
958
- timeout=60,
959
- )
960
- output = result.stdout.replace("\r\n", "\n")
961
- has_conflicts = "<<<<<<< OURS" in output
962
- if result.returncode != 0 and not has_conflicts:
963
- raise BridgeError(f"git merge-file failed ({result.returncode}): {result.stderr.strip()}")
964
- return output, not has_conflicts, result.stderr.strip()
965
-
966
-
967
- def _decode_rows(lines: list[str]) -> list[list[Any]]:
968
- rows = []
969
- for line in lines:
970
- if not line.strip():
971
- continue
972
- try:
973
- value = json.loads(line)
974
- except json.JSONDecodeError as exc:
975
- raise BridgeError(f"Merged projection contains invalid JSONL: {line!r}") from exc
976
- if not isinstance(value, list):
977
- raise BridgeError(f"Merged projection line is not a JSON array: {line!r}")
978
- rows.append(value)
979
- return rows
980
-
981
-
982
- def _display(value: Any) -> Any:
983
- return None if value is MISSING else value
984
-
985
-
986
- def _row_kind(base: Any, ours: Any, theirs: Any) -> str:
987
- if base is MISSING:
988
- if ours is MISSING:
989
- return "inserted_theirs"
990
- if theirs is MISSING:
991
- return "inserted_ours"
992
- return "inserted_both_same" if ours == theirs else "inserted_both_conflict"
993
- if ours is MISSING and theirs is MISSING:
994
- return "deleted_both"
995
- if ours is MISSING:
996
- return "deleted_ours" if theirs == base else "delete_modify_ours"
997
- if theirs is MISSING:
998
- return "deleted_theirs" if ours == base else "delete_modify_theirs"
999
- if ours == theirs:
1000
- return "same"
1001
- if ours == base:
1002
- return "modified_theirs"
1003
- if theirs == base:
1004
- return "modified_ours"
1005
- return "modified_both"
1006
-
1007
-
1008
- def _cell_diffs(
1009
- fields: tuple[str, ...],
1010
- base_rows: list[list[Any]],
1011
- ours_rows: list[list[Any]],
1012
- theirs_rows: list[list[Any]],
1013
- ) -> tuple[list[dict[str, Any]], bool]:
1014
- alignment_required = len({len(base_rows), len(ours_rows), len(theirs_rows)}) > 1
1015
- slots: list[dict[str, Any]] = []
1016
- for index in range(max(len(base_rows), len(ours_rows), len(theirs_rows), 0)):
1017
- base = base_rows[index] if index < len(base_rows) else MISSING
1018
- ours = ours_rows[index] if index < len(ours_rows) else MISSING
1019
- theirs = theirs_rows[index] if index < len(theirs_rows) else MISSING
1020
- differences = []
1021
- for column, field in enumerate(fields):
1022
- base_value = base[column] if base is not MISSING and column < len(base) else MISSING
1023
- ours_value = ours[column] if ours is not MISSING and column < len(ours) else MISSING
1024
- theirs_value = theirs[column] if theirs is not MISSING and column < len(theirs) else MISSING
1025
- if ours_value == theirs_value == base_value:
1026
- continue
1027
- if base_value is MISSING and theirs_value is not MISSING:
1028
- resolution = "theirs"
1029
- elif base_value is MISSING and ours_value is not MISSING:
1030
- resolution = "ours"
1031
- elif ours_value == theirs_value:
1032
- resolution = "same"
1033
- elif ours_value == base_value:
1034
- resolution = "theirs"
1035
- elif theirs_value == base_value:
1036
- resolution = "ours"
1037
- else:
1038
- resolution = "conflict"
1039
- differences.append({
1040
- "field": field,
1041
- "base": _display(base_value),
1042
- "ours": _display(ours_value),
1043
- "theirs": _display(theirs_value),
1044
- "basePresent": base_value is not MISSING,
1045
- "oursPresent": ours_value is not MISSING,
1046
- "theirsPresent": theirs_value is not MISSING,
1047
- "resolution": resolution,
1048
- })
1049
- slots.append({
1050
- "slot": index + 1,
1051
- "kind": _row_kind(base, ours, theirs),
1052
- "base": _display(base),
1053
- "ours": _display(ours),
1054
- "theirs": _display(theirs),
1055
- "cellDiffs": differences,
1056
- })
1057
- return slots, alignment_required
1058
-
1059
-
1060
- def _merge_inserted_rows(
1061
- ours_rows: list[list[Any]], theirs_rows: list[list[Any]]
1062
- ) -> list[list[Any]]:
1063
- """Union same-anchor inserts, collapsing cross-side duplicates, local first."""
1064
- resolved = list(ours_rows)
1065
- local_counts: dict[str, int] = {}
1066
- for row in ours_rows:
1067
- key = _line(row)
1068
- local_counts[key] = local_counts.get(key, 0) + 1
1069
- for row in theirs_rows:
1070
- key = _line(row)
1071
- if local_counts.get(key, 0):
1072
- local_counts[key] -= 1
1073
- else:
1074
- resolved.append(row)
1075
- return resolved
1076
-
1077
-
1078
- def _row_edit_plan(
1079
- base_rows: list[list[Any]], side_rows: list[list[Any]]
1080
- ) -> tuple[list[Any], dict[int, list[list[Any]]]]:
1081
- states: list[Any] = [MISSING] * len(base_rows)
1082
- insertions: dict[int, list[list[Any]]] = {}
1083
- matcher = SequenceMatcher(
1084
- None,
1085
- [_line(row) for row in base_rows],
1086
- [_line(row) for row in side_rows],
1087
- autojunk=False,
1088
- )
1089
- for operation, base_start, base_end, side_start, side_end in matcher.get_opcodes():
1090
- if operation == "equal":
1091
- for offset, row in enumerate(side_rows[side_start:side_end]):
1092
- states[base_start + offset] = row
1093
- elif operation == "replace":
1094
- base_count = base_end - base_start
1095
- side_block = side_rows[side_start:side_end]
1096
- paired_count = min(base_count, len(side_block))
1097
- for offset, row in enumerate(side_block[:paired_count]):
1098
- states[base_start + offset] = row
1099
- if len(side_block) > paired_count:
1100
- insertions.setdefault(base_start + paired_count, []).extend(side_block[paired_count:])
1101
- elif operation == "insert":
1102
- insertions.setdefault(base_start, []).extend(side_rows[side_start:side_end])
1103
- elif operation != "delete":
1104
- raise BridgeError(f"Unsupported conflict-block row operation: {operation}")
1105
- return states, insertions
1106
-
1107
-
1108
- def _merge_base_row(
1109
- fields: tuple[str, ...], base: list[Any], ours: Any, theirs: Any
1110
- ) -> tuple[str, Any]:
1111
- if ours is MISSING and theirs is MISSING:
1112
- return "resolved", MISSING
1113
- if ours is MISSING:
1114
- if theirs == base:
1115
- return "resolved", MISSING
1116
- slots, _ = _cell_diffs(fields, [base], [], [theirs])
1117
- return "conflict", slots[0]
1118
- if theirs is MISSING:
1119
- if ours == base:
1120
- return "resolved", MISSING
1121
- slots, _ = _cell_diffs(fields, [base], [ours], [])
1122
- return "conflict", slots[0]
1123
- if ours == theirs:
1124
- return "resolved", ours
1125
- if ours == base:
1126
- return "resolved", theirs
1127
- if theirs == base:
1128
- return "resolved", ours
1129
-
1130
- resolved = []
1131
- has_conflict = False
1132
- for index in range(len(fields)):
1133
- base_value = base[index]
1134
- ours_value = ours[index]
1135
- theirs_value = theirs[index]
1136
- if ours_value == theirs_value:
1137
- resolved.append(ours_value)
1138
- elif ours_value == base_value:
1139
- resolved.append(theirs_value)
1140
- elif theirs_value == base_value:
1141
- resolved.append(ours_value)
1142
- else:
1143
- has_conflict = True
1144
- resolved.append(theirs_value)
1145
- if not has_conflict:
1146
- return "resolved", resolved
1147
- slots, _ = _cell_diffs(fields, [base], [ours], [theirs])
1148
- return "conflict", slots[0]
1149
-
1150
-
1151
- def _resolve_conflict_block(
1152
- fields: tuple[str, ...],
1153
- base_rows: list[list[Any]],
1154
- ours_rows: list[list[Any]],
1155
- theirs_rows: list[list[Any]],
1156
- ) -> list[dict[str, Any]]:
1157
- if not base_rows:
1158
- return [{
1159
- "type": "resolved",
1160
- "policy": "same_anchor_insert_union_ours_first",
1161
- "ours": ours_rows,
1162
- "base": [],
1163
- "theirs": theirs_rows,
1164
- "rows": _merge_inserted_rows(ours_rows, theirs_rows),
1165
- }]
1166
-
1167
- ours_states, ours_insertions = _row_edit_plan(base_rows, ours_rows)
1168
- theirs_states, theirs_insertions = _row_edit_plan(base_rows, theirs_rows)
1169
- segments: list[dict[str, Any]] = []
1170
- resolved_rows: list[list[Any]] = []
1171
-
1172
- def flush_resolved() -> None:
1173
- nonlocal resolved_rows
1174
- if resolved_rows:
1175
- segments.append({
1176
- "type": "resolved",
1177
- "policy": "continuous_anchor_three_way",
1178
- "rows": resolved_rows,
1179
- })
1180
- resolved_rows = []
1181
-
1182
- for anchor in range(len(base_rows) + 1):
1183
- resolved_rows.extend(_merge_inserted_rows(
1184
- ours_insertions.get(anchor, []), theirs_insertions.get(anchor, [])
1185
- ))
1186
- if anchor == len(base_rows):
1187
- break
1188
- status, value = _merge_base_row(
1189
- fields, base_rows[anchor], ours_states[anchor], theirs_states[anchor]
1190
- )
1191
- if status == "resolved":
1192
- if value is not MISSING:
1193
- resolved_rows.append(value)
1194
- continue
1195
- flush_resolved()
1196
- ours = ours_states[anchor]
1197
- theirs = theirs_states[anchor]
1198
- segments.append({
1199
- "type": "conflict",
1200
- "ours": [] if ours is MISSING else [ours],
1201
- "base": [base_rows[anchor]],
1202
- "theirs": [] if theirs is MISSING else [theirs],
1203
- "alignmentRequired": ours is MISSING or theirs is MISSING,
1204
- "rowSlots": [value],
1205
- })
1206
- flush_resolved()
1207
- return segments
1208
-
1209
-
1210
- def _parse_merge(output: str, fields: tuple[str, ...]) -> list[dict[str, Any]]:
1211
- segments: list[dict[str, Any]] = []
1212
- normal: list[str] = []
1213
- ours: list[str] = []
1214
- base: list[str] = []
1215
- theirs: list[str] = []
1216
- state = "normal"
1217
-
1218
- def flush_normal() -> None:
1219
- nonlocal normal
1220
- if normal:
1221
- segments.append({"type": "merged", "rows": _decode_rows(normal)})
1222
- normal = []
1223
-
1224
- for line in output.splitlines():
1225
- if state == "normal" and line.startswith("<<<<<<< OURS"):
1226
- flush_normal()
1227
- state = "ours"
1228
- ours, base, theirs = [], [], []
1229
- elif state == "ours" and line.startswith("||||||| BASE"):
1230
- state = "base"
1231
- elif state == "base" and line == "=======":
1232
- state = "theirs"
1233
- elif state == "theirs" and line.startswith(">>>>>>> THEIRS"):
1234
- decoded_ours = _decode_rows(ours)
1235
- decoded_base = _decode_rows(base)
1236
- decoded_theirs = _decode_rows(theirs)
1237
- segments.extend(_resolve_conflict_block(
1238
- fields, decoded_base, decoded_ours, decoded_theirs
1239
- ))
1240
- state = "normal"
1241
- elif state == "normal":
1242
- normal.append(line)
1243
- elif state == "ours":
1244
- ours.append(line)
1245
- elif state == "base":
1246
- base.append(line)
1247
- else:
1248
- theirs.append(line)
1249
-
1250
- if state != "normal":
1251
- raise BridgeError(f"Unterminated git conflict marker block ({state}).")
1252
- flush_normal()
1253
- return segments
1254
-
1255
-
1256
- def _sheet_lines(ws) -> tuple[str, ...]:
1257
- lines = []
1258
- for row in range(1, (ws.max_row or 0) + 1):
1259
- values = [_json_value(ws.cell(row, column).value) for column in range(1, (ws.max_column or 0) + 1)]
1260
- while values and values[-1] is None:
1261
- values.pop()
1262
- if values and any(value is not None for value in values):
1263
- lines.append(_line(values))
1264
- return tuple(lines)
1265
-
1266
-
1267
- def _sheet_similarity(left, right) -> float:
1268
- return _column_similarity(_sheet_lines(left), _sheet_lines(right))
1269
-
1270
-
1271
- def _match_base_sheets(base_workbook, side_workbook) -> tuple[dict[str, str], set[str]]:
1272
- matches = {
1273
- name: name for name in base_workbook.sheetnames if name in side_workbook.sheetnames
1274
- }
1275
- remaining_base = [name for name in base_workbook.sheetnames if name not in matches]
1276
- remaining_side = [name for name in side_workbook.sheetnames if name not in matches.values()]
1277
- scores = {
1278
- (base_name, side_name): _sheet_similarity(
1279
- base_workbook[base_name], side_workbook[side_name]
1280
- )
1281
- for base_name in remaining_base
1282
- for side_name in remaining_side
1283
- }
1284
- while remaining_base and remaining_side:
1285
- candidates = []
1286
- for base_name in remaining_base:
1287
- ranked = sorted(
1288
- ((scores[(base_name, side_name)], side_name) for side_name in remaining_side),
1289
- reverse=True,
1290
- )
1291
- if not ranked or ranked[0][0] < 0.60:
1292
- continue
1293
- if len(ranked) > 1 and ranked[0][0] == ranked[1][0]:
1294
- continue
1295
- score, side_name = ranked[0]
1296
- reverse_ranked = sorted(
1297
- ((scores[(candidate, side_name)], candidate) for candidate in remaining_base),
1298
- reverse=True,
1299
- )
1300
- if len(reverse_ranked) > 1 and reverse_ranked[0][0] == reverse_ranked[1][0]:
1301
- continue
1302
- if reverse_ranked[0][1] == base_name:
1303
- candidates.append((score, base_name, side_name))
1304
- if not candidates:
1305
- break
1306
- _, base_name, side_name = max(candidates)
1307
- matches[base_name] = side_name
1308
- remaining_base.remove(base_name)
1309
- remaining_side.remove(side_name)
1310
- return matches, set(remaining_side)
1311
-
1312
-
1313
- def _sheet_triplets(base_workbook, ours_workbook, theirs_workbook) -> list[dict[str, str | None]]:
1314
- ours_matches, ours_only = _match_base_sheets(base_workbook, ours_workbook)
1315
- theirs_matches, theirs_only = _match_base_sheets(base_workbook, theirs_workbook)
1316
- triplets = [
1317
- {
1318
- "logical": base_name,
1319
- "base": base_name,
1320
- "ours": ours_matches.get(base_name),
1321
- "theirs": theirs_matches.get(base_name),
1322
- }
1323
- for base_name in base_workbook.sheetnames
1324
- ]
1325
- for name in sorted(ours_only & theirs_only):
1326
- triplets.append({"logical": name, "base": None, "ours": name, "theirs": name})
1327
- for name in sorted(theirs_only - ours_only):
1328
- triplets.append({"logical": name, "base": None, "ours": None, "theirs": name})
1329
- for name in sorted(ours_only - theirs_only):
1330
- triplets.append({"logical": name, "base": None, "ours": name, "theirs": None})
1331
- return triplets
1332
-
1333
-
1334
- def _sheet_rename_status(base_name: str | None, ours_name: str | None, theirs_name: str | None) -> str:
1335
- if base_name is None:
1336
- return "not_applicable"
1337
- ours_changed = ours_name is not None and ours_name != base_name
1338
- theirs_changed = theirs_name is not None and theirs_name != base_name
1339
- if ours_changed and theirs_changed:
1340
- return "renamed_both_same" if ours_name == theirs_name else "rename_conflict"
1341
- if ours_changed:
1342
- return "renamed_ours"
1343
- if theirs_changed:
1344
- return "renamed_theirs"
1345
- return "unchanged"
1346
-
1347
-
1348
- def _presence_action(base_exists: bool, ours_exists: bool, theirs_exists: bool) -> str:
1349
- if not base_exists:
1350
- if ours_exists and theirs_exists:
1351
- return "added_both"
1352
- return "added_ours" if ours_exists else "added_theirs"
1353
- if not ours_exists and not theirs_exists:
1354
- return "deleted_both"
1355
- if not ours_exists:
1356
- return "deleted_ours_or_modified_theirs"
1357
- if not theirs_exists:
1358
- return "deleted_theirs_or_modified_ours"
1359
- return "merge"
1360
-
1361
-
1362
- def analyze_workbooks(
1363
- base_path: Path | str,
1364
- ours_path: Path | str,
1365
- theirs_path: Path | str,
1366
- output_dir: Path | str,
1367
- *,
1368
- ignore_columns: Iterable[str] = (),
1369
- write_sidecars: bool = True,
1370
- skip_hidden_sheets: bool = True,
1371
- ) -> dict[str, Any]:
1372
- base_path = Path(base_path).resolve()
1373
- ours_path = Path(ours_path).resolve()
1374
- theirs_path = Path(theirs_path).resolve()
1375
- output_dir = Path(output_dir).resolve()
1376
- output_dir.mkdir(parents=True, exist_ok=True)
1377
- ignore_columns = tuple(str(name) for name in ignore_columns)
1378
- ignored = {str(name).casefold() for name in ignore_columns}
1379
-
1380
- wb_base = load_workbook(base_path, data_only=False, keep_links=False)
1381
- wb_ours = load_workbook(ours_path, data_only=False, keep_links=False)
1382
- wb_theirs = load_workbook(theirs_path, data_only=False, keep_links=False)
1383
- try:
1384
- sheets = []
1385
- total_conflicts = 0
1386
- total_column_conflicts = 0
1387
- total_sheet_conflicts = 0
1388
- clean_merges = 0
1389
- triplets = _sheet_triplets(wb_base, wb_ours, wb_theirs)
1390
- for sheet_index, triplet in enumerate(triplets, 1):
1391
- name = str(triplet["logical"])
1392
- base_name = triplet["base"]
1393
- ours_name = triplet["ours"]
1394
- theirs_name = triplet["theirs"]
1395
- if skip_hidden_sheets:
1396
- # 隐藏页签(工具派生表)默认不参与冲突检测:以本地可见性为准
1397
- hidden = False
1398
- if ours_name is not None and ours_name in wb_ours.sheetnames:
1399
- hidden = wb_ours[ours_name].sheet_state == "hidden"
1400
- elif theirs_name is not None and theirs_name in wb_theirs.sheetnames:
1401
- hidden = wb_theirs[theirs_name].sheet_state == "hidden"
1402
- if hidden:
1403
- continue
1404
- base_exists = base_name is not None
1405
- ours_exists = ours_name is not None
1406
- theirs_exists = theirs_name is not None
1407
- action = _presence_action(base_exists, ours_exists, theirs_exists)
1408
- rename_status = _sheet_rename_status(base_name, ours_name, theirs_name)
1409
- sheet_report: dict[str, Any] = {
1410
- "name": name,
1411
- "names": {"base": base_name, "ours": ours_name, "theirs": theirs_name},
1412
- "action": action,
1413
- "renameStatus": rename_status,
1414
- "present": {"base": base_exists, "ours": ours_exists, "theirs": theirs_exists},
1415
- }
1416
- if action != "merge":
1417
- sheet_conflict = None
1418
- if not base_exists and ours_exists and theirs_exists:
1419
- sheet_report.update({
1420
- "status": "clean",
1421
- "resolution": "theirs",
1422
- "policy": "no_base_remote_wins",
1423
- })
1424
- elif not base_exists:
1425
- sheet_report.update({
1426
- "status": "clean",
1427
- "resolution": "ours" if ours_exists else "theirs",
1428
- })
1429
- elif not ours_exists and not theirs_exists:
1430
- sheet_report.update({"status": "clean", "resolution": "deleted_both"})
1431
- else:
1432
- survivor = "ours" if ours_exists else "theirs"
1433
- survivor_name = ours_name if ours_exists else theirs_name
1434
- survivor_workbook = wb_ours if ours_exists else wb_theirs
1435
- unchanged = (
1436
- survivor_name == base_name
1437
- and _sheet_lines(wb_base[base_name]) == _sheet_lines(survivor_workbook[survivor_name])
1438
- )
1439
- if unchanged:
1440
- sheet_report.update({
1441
- "status": "clean",
1442
- "resolution": "theirs" if not ours_exists else "ours",
1443
- })
1444
- else:
1445
- deleted = "ours" if not ours_exists else "theirs"
1446
- sheet_conflict = {
1447
- "kind": f"delete_modify_{deleted}",
1448
- "survivor": survivor,
1449
- "survivorName": survivor_name,
1450
- }
1451
- sheet_report.update({"status": "conflict", "sheetConflict": sheet_conflict})
1452
- total_sheet_conflicts += 1
1453
- sheets.append(sheet_report)
1454
- continue
1455
-
1456
- layouts = {
1457
- "base": _layout(wb_base[base_name]),
1458
- "ours": _layout(wb_ours[ours_name]),
1459
- "theirs": _layout(wb_theirs[theirs_name]),
1460
- }
1461
- worksheets = {
1462
- "base": wb_base[base_name],
1463
- "ours": wb_ours[ours_name],
1464
- "theirs": wb_theirs[theirs_name],
1465
- }
1466
- layouts, column_alignment, column_alignment_mode = _align_layouts(
1467
- worksheets, layouts
1468
- )
1469
- common = set(layouts["base"].fields) & set(layouts["ours"].fields) & set(layouts["theirs"].fields)
1470
- projection_fields = tuple(
1471
- field for field in layouts["base"].fields
1472
- if field in common
1473
- and field.casefold() not in ignored
1474
- and not (layouts["base"].var_row is not None and field == "@1")
1475
- )
1476
- if not projection_fields:
1477
- sheet_report.update({
1478
- "status": "unsupported",
1479
- "error": "No common projection fields remain after column alignment/ignore filtering.",
1480
- })
1481
- sheets.append(sheet_report)
1482
- continue
1483
-
1484
- rows = {
1485
- "base": _rows(wb_base[base_name], layouts["base"], projection_fields),
1486
- "ours": _rows(wb_ours[ours_name], layouts["ours"], projection_fields),
1487
- "theirs": _rows(wb_theirs[theirs_name], layouts["theirs"], projection_fields),
1488
- }
1489
- row_alignment = _row_alignment_report(rows)
1490
- column_conflict_count = _annotate_column_conflicts(column_alignment, rows)
1491
- total_column_conflicts += column_conflict_count
1492
- sheet_dir = output_dir / _safe_name(name, sheet_index)
1493
- sheet_dir.mkdir(parents=True, exist_ok=True)
1494
- paths = {}
1495
- for side in ("base", "ours", "theirs"):
1496
- projection_path = sheet_dir / f"{side}.jsonl"
1497
- _write_projection(projection_path, rows[side])
1498
- if write_sidecars:
1499
- sidecar_path = sheet_dir / f"{side}.sidecar.json"
1500
- _write_sidecar(sidecar_path, layouts[side], rows[side])
1501
- paths[side] = projection_path
1502
-
1503
- merged_text, raw_git_clean, git_stderr = _git_merge_file(
1504
- paths["ours"], paths["base"], paths["theirs"]
1505
- )
1506
- merge_output_path = sheet_dir / "git-merge-output.txt"
1507
- _write_text_lf(merge_output_path, merged_text)
1508
- segments = _parse_merge(merged_text, projection_fields)
1509
- conflict_count = sum(segment["type"] == "conflict" for segment in segments)
1510
- total_conflicts += conflict_count
1511
- git_clean = conflict_count == 0
1512
- sheet_conflict_count = int(rename_status == "rename_conflict")
1513
- total_sheet_conflicts += sheet_conflict_count
1514
- sheet_clean = git_clean and column_conflict_count == 0 and sheet_conflict_count == 0
1515
- clean_merges += int(sheet_clean)
1516
- all_fields = [entry["field"] for entry in column_alignment]
1517
- sheet_report.update({
1518
- "status": "clean" if sheet_clean else "conflict",
1519
- "gitStatus": "clean" if git_clean else "conflict",
1520
- "gitRawStatus": "clean" if raw_git_clean else "conflict",
1521
- "columnAlignmentMode": column_alignment_mode,
1522
- "columnAlignment": column_alignment,
1523
- "columnConflictCount": column_conflict_count,
1524
- "rowAlignment": row_alignment,
1525
- "sheetConflictCount": sheet_conflict_count,
1526
- "projectionFields": list(projection_fields),
1527
- "ignoredFields": [field for field in layouts["base"].fields if field.casefold() in ignored],
1528
- "columns": {
1529
- "base": list(layouts["base"].fields),
1530
- "ours": list(layouts["ours"].fields),
1531
- "theirs": list(layouts["theirs"].fields),
1532
- "baseOnly": sorted(set(layouts["base"].fields) - set(layouts["ours"].fields) - set(layouts["theirs"].fields)),
1533
- "oursOnly": sorted(set(layouts["ours"].fields) - set(layouts["base"].fields)),
1534
- "theirsOnly": sorted(set(layouts["theirs"].fields) - set(layouts["base"].fields)),
1535
- "all": all_fields,
1536
- },
1537
- "rowCounts": {side: len(rows[side]) for side in rows},
1538
- "conflictBlockCount": conflict_count,
1539
- "segments": segments,
1540
- "artifacts": {
1541
- "directory": str(sheet_dir),
1542
- "gitMergeOutput": str(merge_output_path),
1543
- "base": str(paths["base"]),
1544
- "ours": str(paths["ours"]),
1545
- "theirs": str(paths["theirs"]),
1546
- },
1547
- "gitStderr": git_stderr,
1548
- })
1549
- sheets.append(sheet_report)
1550
-
1551
- report = {
1552
- "version": 4,
1553
- "inputs": {"base": str(base_path), "ours": str(ours_path), "theirs": str(theirs_path)},
1554
- "ignoreColumns": sorted(ignore_columns),
1555
- "summary": {
1556
- "sheetCount": len(sheets),
1557
- "cleanMergeSheetCount": clean_merges,
1558
- "conflictBlockCount": total_conflicts,
1559
- "columnConflictCount": total_column_conflicts,
1560
- "sheetConflictCount": total_sheet_conflicts,
1561
- },
1562
- "sheets": sheets,
1563
- }
1564
- report_path = output_dir / "report.json"
1565
- report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
1566
- report["reportPath"] = str(report_path)
1567
- return report
1568
- finally:
1569
- wb_base.close()
1570
- wb_ours.close()
1571
- wb_theirs.close()
1572
-
1573
-
1574
- def build_parser() -> argparse.ArgumentParser:
1575
- parser = argparse.ArgumentParser(description=__doc__)
1576
- parser.add_argument("--base", required=True, help="Common-ancestor XLSX path.")
1577
- parser.add_argument("--ours", required=True, help="Local/ours XLSX path.")
1578
- parser.add_argument("--theirs", required=True, help="Remote/theirs XLSX path.")
1579
- parser.add_argument("--output-dir", required=True, help="Directory for JSONL, sidecars, and report.json.")
1580
- parser.add_argument(
1581
- "--ignore-column", action="append", default=[],
1582
- help="Field name excluded from row-alignment JSONL; repeat for multiple fields.",
1583
- )
1584
- parser.add_argument("--print-report", action="store_true", help="Print the complete report instead of the summary.")
1585
- return parser
1586
-
1587
-
1588
- def main(argv: list[str] | None = None) -> int:
1589
- args = build_parser().parse_args(argv)
1590
- try:
1591
- report = analyze_workbooks(
1592
- args.base,
1593
- args.ours,
1594
- args.theirs,
1595
- args.output_dir,
1596
- ignore_columns=args.ignore_column,
1597
- )
1598
- output = report if args.print_report else {
1599
- "ok": True,
1600
- "report": report["reportPath"],
1601
- "summary": report["summary"],
1602
- }
1603
- print(json.dumps(output, ensure_ascii=False, indent=2))
1604
- return 0
1605
- except (BridgeError, OSError, ValueError) as exc:
1606
- print(json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False, indent=2))
1607
- return 1
1608
-
1609
-
1610
- if __name__ == "__main__":
1611
- sys.exit(main())
1
+ """Debug bridge: project three XLSX versions to JSONL and let Git merge rows.
2
+
3
+ This tool is intentionally independent from the production XLSX resolver. It
4
+ uses ``git merge-file`` only to segment clean row edits from candidate conflict
5
+ blocks. The generated sidecars retain workbook row numbers and complete cell
6
+ values so later experiments can map text results back to XLSX safely.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ from difflib import SequenceMatcher
13
+ import hashlib
14
+ import json
15
+ import re
16
+ import subprocess
17
+ import sys
18
+ from dataclasses import dataclass
19
+ from functools import lru_cache
20
+ from datetime import date, datetime, time
21
+ from pathlib import Path
22
+ from typing import Any, Iterable
23
+
24
+ from openpyxl import load_workbook
25
+ from openpyxl.utils import get_column_letter
26
+
27
+
28
+ MISSING = object()
29
+
30
+
31
+ class BridgeError(RuntimeError):
32
+ """Raised when an XLSX projection or Git text merge cannot be completed."""
33
+
34
+
35
+ def _json_value(value: Any) -> Any:
36
+ if value is None or isinstance(value, (bool, int, float, str)):
37
+ return value
38
+ if isinstance(value, (datetime, date, time)):
39
+ return value.isoformat()
40
+ return str(value)
41
+
42
+
43
+ def _line(values: Iterable[Any]) -> str:
44
+ return json.dumps(list(values), ensure_ascii=False, separators=(",", ":"))
45
+
46
+
47
+ def _hash_line(line: str) -> str:
48
+ return hashlib.sha256(line.encode("utf-8")).hexdigest()
49
+
50
+
51
+ def _safe_name(name: str, index: int) -> str:
52
+ slug = re.sub(r"[^0-9A-Za-z._-]+", "_", name).strip("._") or "sheet"
53
+ return f"{index:02d}-{slug[:80]}"
54
+
55
+
56
+ @dataclass(frozen=True)
57
+ class SheetLayout:
58
+ name: str
59
+ var_row: int | None
60
+ data_start_row: int
61
+ fields: tuple[str, ...]
62
+ columns: dict[str, int]
63
+
64
+
65
+ @dataclass(frozen=True)
66
+ class RowProjection:
67
+ xlsx_row: int
68
+ projection: tuple[Any, ...]
69
+ all_cells: dict[str, Any]
70
+ empty: bool = False
71
+
72
+ @property
73
+ def text(self) -> str:
74
+ return _line(self.projection)
75
+
76
+
77
+ @dataclass(frozen=True)
78
+ class PairAlignment:
79
+ matches: dict[int, int]
80
+ similarities: dict[int, float]
81
+ side_only: tuple[int, ...]
82
+ base_only: tuple[int, ...]
83
+
84
+
85
+ def _last_semantic_column(ws) -> int:
86
+ """Ignore styled trailing placeholders but retain every column inside the used range."""
87
+ last = 1
88
+ for cell in ws._cells.values():
89
+ if (
90
+ cell.value not in (None, "")
91
+ or getattr(cell, "comment", None) is not None
92
+ or getattr(cell, "hyperlink", None) is not None
93
+ ):
94
+ last = max(last, cell.column)
95
+ for merged_range in ws.merged_cells.ranges:
96
+ last = max(last, merged_range.max_col)
97
+ return last
98
+
99
+
100
+ def _layout(ws) -> SheetLayout:
101
+ var_row = None
102
+ for row in range(1, min(ws.max_row or 1, 40) + 1):
103
+ if str(ws.cell(row, 1).value or "").strip() == "##var":
104
+ var_row = row
105
+ break
106
+
107
+ columns: dict[str, int] = {}
108
+ if var_row is None:
109
+ for column in range(1, _last_semantic_column(ws) + 1):
110
+ columns[f"@{column}"] = column
111
+ return SheetLayout(ws.title, None, 1, tuple(columns), columns)
112
+
113
+ columns["@1"] = 1
114
+ occurrences: dict[str, int] = {}
115
+ for column in range(2, _last_semantic_column(ws) + 1):
116
+ raw_name = ws.cell(var_row, column).value
117
+ if raw_name is None or str(raw_name).strip() == "":
118
+ columns[f"@{column}"] = column
119
+ continue
120
+ base_name = str(raw_name).strip()
121
+ occurrences[base_name] = occurrences.get(base_name, 0) + 1
122
+ suffix = occurrences[base_name]
123
+ name = base_name if suffix == 1 else f"{base_name}#{suffix}"
124
+ columns[name] = column
125
+
126
+ row = var_row
127
+ while row <= (ws.max_row or var_row):
128
+ marker = ws.cell(row, 1).value
129
+ if not isinstance(marker, str) or not marker.startswith("##"):
130
+ break
131
+ row += 1
132
+ return SheetLayout(ws.title, var_row, row, tuple(columns), columns)
133
+
134
+
135
+ def _active_rows(ws, layout: SheetLayout) -> list[int]:
136
+ return [
137
+ row
138
+ for row in range(layout.data_start_row, (ws.max_row or 0) + 1)
139
+ if any(ws.cell(row, column).value is not None for column in layout.columns.values())
140
+ ]
141
+
142
+
143
+ @lru_cache(maxsize=65536)
144
+ def _token_string(value: Any) -> str:
145
+ return _line((_json_value(value),))
146
+
147
+
148
+ def _column_tokens(ws, rows: Iterable[int], column: int) -> tuple[str, ...]:
149
+ return tuple(_token_string(ws.cell(row, column).value) for row in rows)
150
+
151
+
152
+ def _sheet_column_tokens(
153
+ ws, active_rows: list[int], columns: list[int]
154
+ ) -> list[tuple[str, ...]]:
155
+ """一次 iter_rows 读取整表,按列生成 token 序列(大表避免逐 cell 访问)。"""
156
+ if not active_rows or not columns:
157
+ return [() for _ in columns]
158
+ row_set = set(active_rows)
159
+ first = min(active_rows)
160
+ last = max(active_rows)
161
+ buckets: list[list[str]] = [[] for _ in columns]
162
+ for offset, values in enumerate(
163
+ ws.iter_rows(min_row=first, max_row=last, values_only=True), first
164
+ ):
165
+ if offset not in row_set:
166
+ continue
167
+ for bucket, column in zip(buckets, columns):
168
+ bucket.append(
169
+ _token_string(values[column - 1] if values is not None else None)
170
+ )
171
+ return [tuple(bucket) for bucket in buckets]
172
+
173
+
174
+ def _column_similarity(left: tuple[str, ...], right: tuple[str, ...]) -> float:
175
+ if not left and not right:
176
+ return 1.0
177
+ if not left or not right:
178
+ return 0.0
179
+ # 频次配对(线性):SequenceMatcher 在长序列且大部分不同的场景 O(n*m) 退化
180
+ counts: dict[str, int] = {}
181
+ for token in left:
182
+ counts[token] = counts.get(token, 0) + 1
183
+ matched = 0
184
+ for token in right:
185
+ if counts.get(token, 0):
186
+ counts[token] -= 1
187
+ matched += 1
188
+ # 上限截断:公共 token 数不超过 min 长度,保证相似度 ∈ [0, 1]
189
+ return min(matched, len(left), len(right)) / min(len(left), len(right))
190
+
191
+
192
+ def _align_column_sequences(
193
+ base_columns: list[tuple[str, ...]],
194
+ side_columns: list[tuple[str, ...]],
195
+ *,
196
+ minimum_similarity: float = 0.0,
197
+ ) -> PairAlignment:
198
+ """Globally align ordered columns while allowing inserts and deletes."""
199
+ base_count = len(base_columns)
200
+ side_count = len(side_columns)
201
+ gap = -0.40
202
+ negative = float("-inf")
203
+ scores = [[negative] * (side_count + 1) for _ in range(base_count + 1)]
204
+ moves: list[list[str | None]] = [
205
+ [None] * (side_count + 1) for _ in range(base_count + 1)
206
+ ]
207
+ scores[0][0] = 0.0
208
+ for base_index in range(1, base_count + 1):
209
+ scores[base_index][0] = base_index * gap
210
+ moves[base_index][0] = "delete"
211
+ for side_index in range(1, side_count + 1):
212
+ scores[0][side_index] = side_index * gap
213
+ moves[0][side_index] = "insert"
214
+
215
+ similarities = [
216
+ [
217
+ _column_similarity(base_columns[base_index], side_columns[side_index])
218
+ for side_index in range(side_count)
219
+ ]
220
+ for base_index in range(base_count)
221
+ ]
222
+ priority = {"match": 2, "delete": 1, "insert": 0}
223
+ for base_index in range(1, base_count + 1):
224
+ for side_index in range(1, side_count + 1):
225
+ candidates = [
226
+ (scores[base_index - 1][side_index] + gap, "delete"),
227
+ (scores[base_index][side_index - 1] + gap, "insert"),
228
+ ]
229
+ similarity = similarities[base_index - 1][side_index - 1]
230
+ if similarity >= minimum_similarity:
231
+ candidates.append(
232
+ (scores[base_index - 1][side_index - 1] + similarity, "match")
233
+ )
234
+ score, move = max(candidates, key=lambda item: (item[0], priority[item[1]]))
235
+ scores[base_index][side_index] = score
236
+ moves[base_index][side_index] = move
237
+
238
+ matches: dict[int, int] = {}
239
+ match_similarities: dict[int, float] = {}
240
+ base_only: list[int] = []
241
+ side_only: list[int] = []
242
+ base_index = base_count
243
+ side_index = side_count
244
+ while base_index or side_index:
245
+ move = moves[base_index][side_index]
246
+ if move == "match":
247
+ left = base_index
248
+ right = side_index
249
+ matches[left] = right
250
+ match_similarities[left] = similarities[left - 1][right - 1]
251
+ base_index -= 1
252
+ side_index -= 1
253
+ elif move == "delete":
254
+ base_only.append(base_index)
255
+ base_index -= 1
256
+ elif move == "insert":
257
+ side_only.append(side_index)
258
+ side_index -= 1
259
+ else:
260
+ raise BridgeError("Column sequence alignment backtracking failed.")
261
+
262
+ base_only_set = set(base_only)
263
+ side_only_set = set(side_only)
264
+ anchors = [(0, 0), *sorted(matches.items()), (base_count + 1, side_count + 1)]
265
+ for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
266
+ base_gap = [
267
+ index for index in range(left_base + 1, right_base)
268
+ if index in base_only_set
269
+ ]
270
+ side_gap = [
271
+ index for index in range(left_side + 1, right_side)
272
+ if index in side_only_set
273
+ ]
274
+ if not base_gap or len(base_gap) != len(side_gap):
275
+ continue
276
+ for base_position, side_position in zip(base_gap, side_gap):
277
+ matches[base_position] = side_position
278
+ match_similarities[base_position] = similarities[base_position - 1][side_position - 1]
279
+ base_only_set.remove(base_position)
280
+ side_only_set.remove(side_position)
281
+
282
+ return PairAlignment(
283
+ matches=matches,
284
+ similarities=match_similarities,
285
+ side_only=tuple(sorted(side_only_set)),
286
+ base_only=tuple(sorted(base_only_set)),
287
+ )
288
+
289
+
290
+ def _insertion_anchor(column: int, matches: dict[int, int]) -> int:
291
+ return max((base for base, side in matches.items() if side < column), default=0)
292
+
293
+
294
+ def _logical_insert_name(side: str, physical_column: int) -> str:
295
+ return f"@{side}+{get_column_letter(physical_column)}"
296
+
297
+
298
+ def _is_positional_field(field: str) -> bool:
299
+ return re.fullmatch(r"@\d+", field) is not None
300
+
301
+
302
+ def _align_content_layouts(
303
+ worksheets: dict[str, Any], layouts: dict[str, SheetLayout]
304
+ ) -> tuple[dict[str, SheetLayout], list[dict[str, Any]]]:
305
+ entries = {
306
+ side: [(field, layouts[side].columns[field]) for field in layouts[side].fields]
307
+ for side in ("base", "ours", "theirs")
308
+ }
309
+ active_rows = {
310
+ side: _active_rows(worksheets[side], layouts[side])
311
+ for side in ("base", "ours", "theirs")
312
+ }
313
+ vectors = {
314
+ side: _sheet_column_tokens(
315
+ worksheets[side],
316
+ active_rows[side],
317
+ [column for _, column in entries[side]],
318
+ )
319
+ for side in ("base", "ours", "theirs")
320
+ }
321
+ pair = {
322
+ side: _align_column_sequences(vectors["base"], vectors[side])
323
+ for side in ("ours", "theirs")
324
+ }
325
+ aligned_columns: dict[str, dict[str, int]] = {
326
+ "base": {field: column for field, column in entries["base"]},
327
+ "ours": {},
328
+ "theirs": {},
329
+ }
330
+ column_report: list[dict[str, Any]] = []
331
+ for base_position, (field, base_physical) in enumerate(entries["base"], 1):
332
+ ours_position = pair["ours"].matches.get(base_position)
333
+ theirs_position = pair["theirs"].matches.get(base_position)
334
+ ours_physical = entries["ours"][ours_position - 1][1] if ours_position else None
335
+ theirs_physical = entries["theirs"][theirs_position - 1][1] if theirs_position else None
336
+ ours_label = entries["ours"][ours_position - 1][0] if ours_position else None
337
+ theirs_label = entries["theirs"][theirs_position - 1][0] if theirs_position else None
338
+ if ours_physical is not None:
339
+ aligned_columns["ours"][field] = ours_physical
340
+ if theirs_physical is not None:
341
+ aligned_columns["theirs"][field] = theirs_physical
342
+ if ours_physical is None and theirs_physical is None:
343
+ status = "deleted_both"
344
+ elif ours_physical is None:
345
+ status = "deleted_ours"
346
+ elif theirs_physical is None:
347
+ status = "deleted_theirs"
348
+ else:
349
+ status = "common"
350
+ column_report.append({
351
+ "field": field,
352
+ "status": status,
353
+ "labels": {"base": field, "ours": ours_label, "theirs": theirs_label},
354
+ "baseColumn": get_column_letter(base_physical),
355
+ "oursColumn": get_column_letter(ours_physical) if ours_physical else None,
356
+ "theirsColumn": get_column_letter(theirs_physical) if theirs_physical else None,
357
+ "oursSimilarity": pair["ours"].similarities.get(base_position),
358
+ "theirsSimilarity": pair["theirs"].similarities.get(base_position),
359
+ })
360
+
361
+ unmatched = {
362
+ side: [
363
+ (column, _insertion_anchor(column, pair[side].matches))
364
+ for column in pair[side].side_only
365
+ ]
366
+ for side in ("ours", "theirs")
367
+ }
368
+ anchors = sorted({anchor for values in unmatched.values() for _, anchor in values})
369
+ inserted_fields: dict[int, list[str]] = {}
370
+ used_fields = set(aligned_columns["base"])
371
+
372
+ def inserted_field(side: str, position: int) -> str:
373
+ label, physical = entries[side][position - 1]
374
+ if not _is_positional_field(label) and label not in used_fields:
375
+ used_fields.add(label)
376
+ return label
377
+ candidate = _logical_insert_name(side, physical)
378
+ suffix = 2
379
+ while candidate in used_fields:
380
+ candidate = f"{_logical_insert_name(side, physical)}#{suffix}"
381
+ suffix += 1
382
+ used_fields.add(candidate)
383
+ return candidate
384
+
385
+ for anchor in anchors:
386
+ ours_positions = [column for column, value in unmatched["ours"] if value == anchor]
387
+ theirs_positions = [column for column, value in unmatched["theirs"] if value == anchor]
388
+ matched_theirs: set[int] = set()
389
+ for ours_position in ours_positions:
390
+ theirs_position = next((
391
+ candidate for candidate in theirs_positions
392
+ if candidate not in matched_theirs
393
+ and vectors["ours"][ours_position - 1] == vectors["theirs"][candidate - 1]
394
+ ), None)
395
+ if theirs_position is None:
396
+ label, physical = entries["ours"][ours_position - 1]
397
+ field = inserted_field("ours", ours_position)
398
+ inserted_fields.setdefault(anchor, []).append(field)
399
+ aligned_columns["ours"][field] = physical
400
+ column_report.append({
401
+ "field": field,
402
+ "status": "added_ours",
403
+ "labels": {"base": None, "ours": label},
404
+ "baseColumn": None,
405
+ "oursColumn": get_column_letter(physical),
406
+ "theirsColumn": None,
407
+ "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
408
+ })
409
+ continue
410
+ ours_label, ours_physical = entries["ours"][ours_position - 1]
411
+ theirs_label, theirs_physical = entries["theirs"][theirs_position - 1]
412
+ if ours_label == theirs_label and not _is_positional_field(ours_label) and ours_label not in used_fields:
413
+ field = ours_label
414
+ used_fields.add(field)
415
+ else:
416
+ field = f"@both+{anchor + 1}.{len(matched_theirs)}"
417
+ used_fields.add(field)
418
+ inserted_fields.setdefault(anchor, []).append(field)
419
+ aligned_columns["ours"][field] = ours_physical
420
+ aligned_columns["theirs"][field] = theirs_physical
421
+ column_report.append({
422
+ "field": field,
423
+ "status": "added_both_same",
424
+ "labels": {"base": None, "ours": ours_label, "theirs": theirs_label},
425
+ "baseColumn": None,
426
+ "oursColumn": get_column_letter(ours_physical),
427
+ "theirsColumn": get_column_letter(theirs_physical),
428
+ "oursTheirsSimilarity": 1.0,
429
+ "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
430
+ })
431
+ matched_theirs.add(theirs_position)
432
+ for theirs_position in theirs_positions:
433
+ if theirs_position in matched_theirs:
434
+ continue
435
+ label, physical = entries["theirs"][theirs_position - 1]
436
+ field = inserted_field("theirs", theirs_position)
437
+ inserted_fields.setdefault(anchor, []).append(field)
438
+ aligned_columns["theirs"][field] = physical
439
+ column_report.append({
440
+ "field": field,
441
+ "status": "added_theirs",
442
+ "labels": {"base": None, "theirs": label},
443
+ "baseColumn": None,
444
+ "oursColumn": None,
445
+ "theirsColumn": get_column_letter(physical),
446
+ "anchorAfterBaseColumn": get_column_letter(entries["base"][anchor - 1][1]) if anchor else None,
447
+ })
448
+
449
+ logical_order = list(inserted_fields.get(0, ()))
450
+ for base_position, (field, _) in enumerate(entries["base"], 1):
451
+ logical_order.append(field)
452
+ logical_order.extend(inserted_fields.get(base_position, ()))
453
+ aligned_layouts = {}
454
+ for side in ("base", "ours", "theirs"):
455
+ columns = aligned_columns[side]
456
+ fields = tuple(field for field in logical_order if field in columns)
457
+ source = layouts[side]
458
+ aligned_layouts[side] = SheetLayout(
459
+ source.name, source.var_row, source.data_start_row, fields, columns
460
+ )
461
+ report_by_field = {entry["field"]: entry for entry in column_report}
462
+ return aligned_layouts, [report_by_field[field] for field in logical_order]
463
+
464
+
465
+ def _align_layouts(
466
+ worksheets: dict[str, Any], layouts: dict[str, SheetLayout]
467
+ ) -> tuple[dict[str, SheetLayout], list[dict[str, Any]], str]:
468
+ aligned, report = _align_content_layouts(worksheets, layouts)
469
+ return aligned, report, "content_anchor_sequence"
470
+
471
+
472
+ def _rows(ws, layout: SheetLayout, projection_fields: tuple[str, ...]) -> list[RowProjection]:
473
+ result: list[RowProjection] = []
474
+ columns = [(field, column) for field, column in layout.columns.items()]
475
+ for offset, values in enumerate(
476
+ ws.iter_rows(
477
+ min_row=layout.data_start_row,
478
+ max_row=ws.max_row or layout.data_start_row,
479
+ values_only=True,
480
+ ),
481
+ layout.data_start_row,
482
+ ):
483
+ all_cells = {
484
+ field: _json_value(values[column - 1] if values is not None else None)
485
+ for field, column in columns
486
+ }
487
+ projection = tuple(all_cells.get(field) for field in projection_fields)
488
+ if not any(value is not None for value in all_cells.values()):
489
+ # 空行保留:投影全 None,empty 标记供对齐判断
490
+ result.append(RowProjection(offset, projection, all_cells, empty=True))
491
+ continue
492
+ result.append(RowProjection(offset, projection, all_cells))
493
+ return result
494
+
495
+
496
+ def _paired_projected_rows(
497
+ base_rows: list[RowProjection], side_rows: list[RowProjection]
498
+ ) -> tuple[list[tuple[RowProjection, RowProjection]], list[RowProjection]]:
499
+ """Pair preserved/replaced rows and return rows inserted only on one side."""
500
+ matches, insertions = _row_side_alignment(base_rows, side_rows)
501
+ pairs = [
502
+ (base_rows[base_index], side_rows[side_index])
503
+ for base_index, side_index in sorted(matches.items())
504
+ ]
505
+ inserted = [
506
+ side_rows[side_index]
507
+ for anchor in sorted(insertions)
508
+ for side_index in insertions[anchor]
509
+ ]
510
+ return pairs, inserted
511
+
512
+
513
+ def _row_similarity(left: RowProjection, right: RowProjection) -> float:
514
+ if not left.projection and not right.projection:
515
+ return 1.0
516
+ size = max(len(left.projection), len(right.projection))
517
+ if not size:
518
+ return 0.0
519
+ limit = min(len(left.projection), len(right.projection))
520
+ equal = 0
521
+ for index in range(limit):
522
+ if left.projection[index] == right.projection[index]:
523
+ equal += 1
524
+ return equal / size
525
+
526
+
527
+ _FAST_ALIGN_THRESHOLD = 1_000_000 # base*side 超过该值走分层快速对齐
528
+ _SIMILAR_MATCH_MIN = 0.3 # match 分 2s-1 > gap(-0.4) 的临界相似度
529
+
530
+
531
+ def _similar_anchors_in_gap(
532
+ base_rows: list[RowProjection],
533
+ side_rows: list[RowProjection],
534
+ base_gap: list[int],
535
+ side_gap: list[int],
536
+ matches: dict[int, int],
537
+ used_base: set[int],
538
+ used_side: set[int],
539
+ ) -> None:
540
+ """大区间退化场景:空行同序配对 + 候选剪枝相似锚点,其余按删+插。"""
541
+ b_empty = [i for i in base_gap if base_rows[i].empty]
542
+ s_empty = [j for j in side_gap if side_rows[j].empty]
543
+ for i, j in zip(b_empty, s_empty):
544
+ matches[i] = j
545
+ used_base.add(i)
546
+ used_side.add(j)
547
+ index: dict[tuple[int, Any], list[int]] = {}
548
+ for j in side_gap:
549
+ if j in used_side:
550
+ continue
551
+ for p, v in enumerate(side_rows[j].projection):
552
+ if v is None:
553
+ continue
554
+ index.setdefault((p, v), []).append(j)
555
+ side_gap_set = set(side_gap)
556
+ last_side = max(
557
+ (j for _, j in matches.items() if j in used_side and j in side_gap_set),
558
+ default=-1,
559
+ )
560
+ for i in base_gap:
561
+ if i in used_base:
562
+ continue
563
+ candidates: set[int] = set()
564
+ for p, v in enumerate(base_rows[i].projection):
565
+ if v is None:
566
+ continue
567
+ candidates.update(index.get((p, v), ()))
568
+ best = None
569
+ best_score = _SIMILAR_MATCH_MIN
570
+ for j in candidates:
571
+ if j in used_side or j <= last_side:
572
+ continue
573
+ score = _row_similarity(base_rows[i], side_rows[j])
574
+ if score >= best_score:
575
+ best_score = score
576
+ best = j
577
+ if best is not None:
578
+ matches[i] = best
579
+ used_base.add(i)
580
+ used_side.add(best)
581
+ last_side = best
582
+
583
+
584
+ def _fast_row_side_alignment(
585
+ base_rows: list[RowProjection], side_rows: list[RowProjection]
586
+ ) -> tuple[dict[int, int], dict[int, list[int]]]:
587
+ """大数据量分层对齐:hash 锚点切区间 → 区间内局部 DP / 候选剪枝。
588
+
589
+ 与全量 DP 输出内容等价(配对与删+插的写回结果一致),仅行状态标注可能有差异。
590
+ """
591
+ base_count = len(base_rows)
592
+ side_count = len(side_rows)
593
+ matches: dict[int, int] = {}
594
+ used_base: set[int] = set()
595
+ used_side: set[int] = set()
596
+
597
+ # 阶段 1:hash 锚点(投影相同的行直接配对)
598
+ base_hashes = [_hash_line(row.text) for row in base_rows]
599
+ side_hashes = [_hash_line(row.text) for row in side_rows]
600
+ matcher = SequenceMatcher(None, base_hashes, side_hashes, autojunk=False)
601
+ for op, b1, b2, s1, s2 in matcher.get_opcodes():
602
+ if op != "equal":
603
+ continue
604
+ for offset in range(b2 - b1):
605
+ matches[b1 + offset] = s1 + offset
606
+ used_base.add(b1 + offset)
607
+ used_side.add(s1 + offset)
608
+
609
+ # 阶段 2:按锚点切区间处理未匹配行
610
+ anchors = [(-1, -1), *sorted(matches.items()), (base_count, side_count)]
611
+ for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
612
+ base_gap = [i for i in range(left_base + 1, right_base) if i not in used_base]
613
+ side_gap = [i for i in range(left_side + 1, right_side) if i not in used_side]
614
+ if not base_gap and not side_gap:
615
+ continue
616
+ if len(base_gap) * len(side_gap) <= _FAST_ALIGN_THRESHOLD:
617
+ # 区间小:局部 DP,与全量语义一致(含空行/低相似同序行)
618
+ local_matches, _ = _row_side_alignment(
619
+ [base_rows[i] for i in base_gap], [side_rows[j] for j in side_gap]
620
+ )
621
+ for local_base, local_side in local_matches.items():
622
+ matches[base_gap[local_base]] = side_gap[local_side]
623
+ used_base.add(base_gap[local_base])
624
+ used_side.add(side_gap[local_side])
625
+ continue
626
+ # 区间大(大量行被改):候选剪枝近似处理
627
+ _similar_anchors_in_gap(
628
+ base_rows, side_rows, base_gap, side_gap, matches, used_base, used_side
629
+ )
630
+
631
+ # 阶段 3:插入归属(与全量路径一致)
632
+ insertion_indexes = []
633
+ for inserted_index in range(side_count):
634
+ if inserted_index in used_side:
635
+ continue
636
+ anchor = max(
637
+ (matched_base + 1 for matched_base, matched_side in matches.items() if matched_side < inserted_index),
638
+ default=0,
639
+ )
640
+ insertion_indexes.append((anchor, inserted_index))
641
+ insertions: dict[int, list[int]] = {}
642
+ last_matched_base = max(matches, default=-1)
643
+ last_matched_side = max(matches.values(), default=-1)
644
+ for anchor, inserted_index in insertion_indexes:
645
+ if inserted_index > last_matched_side and anchor > last_matched_base:
646
+ anchor = base_count
647
+ insertions.setdefault(anchor, []).append(inserted_index)
648
+ return matches, insertions
649
+
650
+
651
+ def _row_side_alignment(
652
+ base_rows: list[RowProjection], side_rows: list[RowProjection]
653
+ ) -> tuple[dict[int, int], dict[int, list[int]]]:
654
+ # Constant columns (including padding, flags and repeated formula results)
655
+ # provide no evidence of row identity. Counting them as matches can make a
656
+ # deleted row plus an insertion score higher than the surviving record.
657
+ # Drop them only from the alignment projection; all_cells stays intact for
658
+ # the actual three-way comparison and output. If all columns are constant,
659
+ # retain the original sequence semantics for duplicate/empty rows.
660
+ combined = [*base_rows, *side_rows]
661
+ width = max((len(row.projection) for row in combined), default=0)
662
+ informative = [
663
+ column for column in range(width)
664
+ if len({
665
+ _line([row.projection[column] if column < len(row.projection) else None])
666
+ for row in combined
667
+ }) > 1
668
+ ]
669
+ if informative and len(informative) < width:
670
+ def project(rows):
671
+ return [RowProjection(
672
+ row.xlsx_row,
673
+ tuple(row.projection[column] if column < len(row.projection) else None
674
+ for column in informative),
675
+ row.all_cells, row.empty,
676
+ ) for row in rows]
677
+ base_rows, side_rows = project(base_rows), project(side_rows)
678
+ if len(base_rows) * len(side_rows) > _FAST_ALIGN_THRESHOLD:
679
+ return _fast_row_side_alignment(base_rows, side_rows)
680
+ base_count = len(base_rows)
681
+ side_count = len(side_rows)
682
+ gap = -0.40
683
+ negative = float("-inf")
684
+ scores = [[negative] * (side_count + 1) for _ in range(base_count + 1)]
685
+ moves: list[list[str | None]] = [[None] * (side_count + 1) for _ in range(base_count + 1)]
686
+ scores[0][0] = 0.0
687
+ for base_index in range(1, base_count + 1):
688
+ scores[base_index][0] = base_index * gap
689
+ moves[base_index][0] = "delete"
690
+ for side_index in range(1, side_count + 1):
691
+ scores[0][side_index] = side_index * gap
692
+ moves[0][side_index] = "insert"
693
+
694
+ similarities = [
695
+ [_row_similarity(base_row, side_row) for side_row in side_rows]
696
+ for base_row in base_rows
697
+ ]
698
+ priority = {"match": 2, "delete": 1, "insert": 0}
699
+ for base_index in range(1, base_count + 1):
700
+ for side_index in range(1, side_count + 1):
701
+ candidates = [
702
+ (scores[base_index - 1][side_index] + gap, "delete"),
703
+ (scores[base_index][side_index - 1] + gap, "insert"),
704
+ (
705
+ scores[base_index - 1][side_index - 1]
706
+ + (2.0 * similarities[base_index - 1][side_index - 1] - 1.0),
707
+ "match",
708
+ ),
709
+ ]
710
+ score, move = max(candidates, key=lambda item: (item[0], priority[item[1]]))
711
+ scores[base_index][side_index] = score
712
+ moves[base_index][side_index] = move
713
+
714
+ matches: dict[int, int] = {}
715
+ base_index = base_count
716
+ side_index = side_count
717
+ while base_index or side_index:
718
+ move = moves[base_index][side_index]
719
+ if move == "match":
720
+ matches[base_index - 1] = side_index - 1
721
+ base_index -= 1
722
+ side_index -= 1
723
+ elif move == "delete":
724
+ base_index -= 1
725
+ elif move == "insert":
726
+ side_index -= 1
727
+ else:
728
+ raise BridgeError("Row sequence alignment backtracking failed.")
729
+
730
+ used_base = set(matches)
731
+ used_side = set(matches.values())
732
+ anchors = [(-1, -1), *sorted(matches.items()), (base_count, side_count)]
733
+ for (left_base, left_side), (right_base, right_side) in zip(anchors, anchors[1:]):
734
+ base_gap = [index for index in range(left_base + 1, right_base) if index not in used_base]
735
+ side_gap = [index for index in range(left_side + 1, right_side) if index not in used_side]
736
+ if not base_gap or len(base_gap) != len(side_gap):
737
+ continue
738
+ if any(
739
+ base_rows[left].empty != side_rows[right].empty
740
+ for left, right in zip(base_gap, side_gap)
741
+ ):
742
+ continue
743
+ pair_similarities = [similarities[left][right] for left, right in zip(base_gap, side_gap)]
744
+ if base_count != side_count and not all(similarity > 0.0 for similarity in pair_similarities):
745
+ continue
746
+ for left, right in zip(base_gap, side_gap):
747
+ matches[left] = right
748
+ used_base.add(left)
749
+ used_side.add(right)
750
+
751
+ insertion_indexes = []
752
+ for inserted_index in range(side_count):
753
+ if inserted_index in used_side:
754
+ continue
755
+ anchor = max(
756
+ (matched_base + 1 for matched_base, matched_side in matches.items() if matched_side < inserted_index),
757
+ default=0,
758
+ )
759
+ insertion_indexes.append((anchor, inserted_index))
760
+
761
+ insertions: dict[int, list[int]] = {}
762
+ last_matched_base = max(matches, default=-1)
763
+ last_matched_side = max(matches.values(), default=-1)
764
+ for anchor, inserted_index in insertion_indexes:
765
+ if inserted_index > last_matched_side and anchor > last_matched_base:
766
+ anchor = base_count
767
+ insertions.setdefault(anchor, []).append(inserted_index)
768
+ return matches, insertions
769
+
770
+
771
+ def _base_row_changed(base: RowProjection, side: RowProjection) -> bool:
772
+ common = set(base.all_cells) & set(side.all_cells)
773
+ return any(base.all_cells[field] != side.all_cells[field] for field in common)
774
+
775
+
776
+ def _row_alignment_report(rows: dict[str, list[RowProjection]]) -> list[dict[str, Any]]:
777
+ mappings = {
778
+ side: _row_side_alignment(rows["base"], rows[side])
779
+ for side in ("ours", "theirs")
780
+ }
781
+ result: list[dict[str, Any]] = []
782
+ insertion_fields: dict[int, list[dict[str, Any]]] = {}
783
+ anchors = sorted(set(mappings["ours"][1]) | set(mappings["theirs"][1]))
784
+ for anchor in anchors:
785
+ ours_indexes = list(mappings["ours"][1].get(anchor, ()))
786
+ theirs_indexes = list(mappings["theirs"][1].get(anchor, ()))
787
+ matched_theirs: set[int] = set()
788
+ for ours_index in ours_indexes:
789
+ ours_row = rows["ours"][ours_index]
790
+ theirs_index = next((
791
+ candidate for candidate in theirs_indexes
792
+ if candidate not in matched_theirs
793
+ and ours_row.projection == rows["theirs"][candidate].projection
794
+ ), None)
795
+ if theirs_index is None:
796
+ insertion_fields.setdefault(anchor, []).append({
797
+ "id": f"row:ours:{ours_row.xlsx_row}",
798
+ "status": "added_ours",
799
+ "baseRow": None,
800
+ "oursRow": ours_row.xlsx_row,
801
+ "theirsRow": None,
802
+ "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
803
+ })
804
+ continue
805
+ matched_theirs.add(theirs_index)
806
+ theirs_row = rows["theirs"][theirs_index]
807
+ insertion_fields.setdefault(anchor, []).append({
808
+ "id": f"row:both:{ours_row.xlsx_row}:{theirs_row.xlsx_row}",
809
+ "status": "added_both_same",
810
+ "baseRow": None,
811
+ "oursRow": ours_row.xlsx_row,
812
+ "theirsRow": theirs_row.xlsx_row,
813
+ "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
814
+ })
815
+ for theirs_index in theirs_indexes:
816
+ if theirs_index in matched_theirs:
817
+ continue
818
+ theirs_row = rows["theirs"][theirs_index]
819
+ insertion_fields.setdefault(anchor, []).append({
820
+ "id": f"row:theirs:{theirs_row.xlsx_row}",
821
+ "status": "added_theirs",
822
+ "baseRow": None,
823
+ "oursRow": None,
824
+ "theirsRow": theirs_row.xlsx_row,
825
+ "anchorAfterBaseRow": rows["base"][anchor - 1].xlsx_row if anchor else None,
826
+ })
827
+
828
+ result.extend(insertion_fields.get(0, ()))
829
+ for base_index, base_row in enumerate(rows["base"]):
830
+ ours_index = mappings["ours"][0].get(base_index)
831
+ theirs_index = mappings["theirs"][0].get(base_index)
832
+ ours_row = rows["ours"][ours_index] if ours_index is not None else None
833
+ theirs_row = rows["theirs"][theirs_index] if theirs_index is not None else None
834
+ if ours_row is None and theirs_row is None:
835
+ status = "deleted_both"
836
+ elif ours_row is None:
837
+ status = "delete_modify_ours" if _base_row_changed(base_row, theirs_row) else "deleted_ours"
838
+ elif theirs_row is None:
839
+ status = "delete_modify_theirs" if _base_row_changed(base_row, ours_row) else "deleted_theirs"
840
+ else:
841
+ status = "common"
842
+ result.append({
843
+ "id": f"row:base:{base_row.xlsx_row}",
844
+ "status": status,
845
+ "baseRow": base_row.xlsx_row,
846
+ "oursRow": ours_row.xlsx_row if ours_row else None,
847
+ "theirsRow": theirs_row.xlsx_row if theirs_row else None,
848
+ })
849
+ result.extend(insertion_fields.get(base_index + 1, ()))
850
+ return result
851
+
852
+
853
+ def _deleted_column_content_changes(
854
+ field: str,
855
+ base_rows: list[RowProjection],
856
+ side_rows: list[RowProjection],
857
+ side: str,
858
+ ) -> list[dict[str, Any]]:
859
+ pairs, inserted = _paired_projected_rows(base_rows, side_rows)
860
+ changes: list[dict[str, Any]] = []
861
+ for base_row, side_row in pairs:
862
+ base_value = base_row.all_cells.get(field, MISSING)
863
+ side_value = side_row.all_cells.get(field, MISSING)
864
+ if base_value == side_value:
865
+ continue
866
+ changes.append({
867
+ "kind": f"modified_{side}",
868
+ "baseXlsxRow": base_row.xlsx_row,
869
+ f"{side}XlsxRow": side_row.xlsx_row,
870
+ "base": _display(base_value),
871
+ side: _display(side_value),
872
+ })
873
+ for side_row in inserted:
874
+ side_value = side_row.all_cells.get(field, MISSING)
875
+ if side_value is MISSING or side_value is None:
876
+ continue
877
+ changes.append({
878
+ "kind": f"inserted_{side}",
879
+ "baseXlsxRow": None,
880
+ f"{side}XlsxRow": side_row.xlsx_row,
881
+ "base": None,
882
+ side: _display(side_value),
883
+ })
884
+ return changes
885
+
886
+
887
+ def _annotate_column_conflicts(
888
+ column_alignment: list[dict[str, Any]],
889
+ rows: dict[str, list[RowProjection]],
890
+ ) -> int:
891
+ conflict_count = 0
892
+ for column in column_alignment:
893
+ status = column["status"]
894
+ if status == "deleted_theirs":
895
+ survivor = "ours"
896
+ deleted = "theirs"
897
+ elif status == "deleted_ours":
898
+ survivor = "theirs"
899
+ deleted = "ours"
900
+ else:
901
+ continue
902
+ changes = _deleted_column_content_changes(
903
+ column["field"], rows["base"], rows[survivor], survivor
904
+ )
905
+ if not changes:
906
+ continue
907
+ column["status"] = f"delete_modify_{deleted}"
908
+ column["conflict"] = True
909
+ column["contentChanges"] = changes
910
+ conflict_count += 1
911
+ return conflict_count
912
+
913
+
914
+ def _write_text_lf(path: Path, content: str) -> None:
915
+ """3.9-compatible LF write (Path.write_text gained `newline` only in 3.10)."""
916
+ with path.open("w", encoding="utf-8", newline="\n") as handle:
917
+ handle.write(content)
918
+
919
+
920
+ def _write_projection(path: Path, rows: list[RowProjection]) -> None:
921
+ content = "\n".join(row.text for row in rows)
922
+ if content:
923
+ content += "\n"
924
+ _write_text_lf(path, content)
925
+
926
+
927
+ def _write_sidecar(path: Path, layout: SheetLayout, rows: list[RowProjection]) -> None:
928
+ payload = {
929
+ "sheet": layout.name,
930
+ "varRow": layout.var_row,
931
+ "dataStartRow": layout.data_start_row,
932
+ "fields": list(layout.fields),
933
+ "rows": [
934
+ {
935
+ "textLine": index,
936
+ "xlsxRow": row.xlsx_row,
937
+ "hash": _hash_line(row.text),
938
+ "projection": list(row.projection),
939
+ "cells": row.all_cells,
940
+ }
941
+ for index, row in enumerate(rows, 1)
942
+ ],
943
+ }
944
+ path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
945
+
946
+
947
+ def _git_merge_file(ours: Path, base: Path, theirs: Path) -> tuple[str, bool, str]:
948
+ result = subprocess.run(
949
+ [
950
+ "git", "merge-file", "-p", "--diff3",
951
+ "-L", "OURS", "-L", "BASE", "-L", "THEIRS",
952
+ str(ours), str(base), str(theirs),
953
+ ],
954
+ capture_output=True,
955
+ text=True,
956
+ encoding="utf-8",
957
+ errors="replace",
958
+ timeout=60,
959
+ )
960
+ output = result.stdout.replace("\r\n", "\n")
961
+ has_conflicts = "<<<<<<< OURS" in output
962
+ if result.returncode != 0 and not has_conflicts:
963
+ raise BridgeError(f"git merge-file failed ({result.returncode}): {result.stderr.strip()}")
964
+ return output, not has_conflicts, result.stderr.strip()
965
+
966
+
967
+ def _decode_rows(lines: list[str]) -> list[list[Any]]:
968
+ rows = []
969
+ for line in lines:
970
+ if not line.strip():
971
+ continue
972
+ try:
973
+ value = json.loads(line)
974
+ except json.JSONDecodeError as exc:
975
+ raise BridgeError(f"Merged projection contains invalid JSONL: {line!r}") from exc
976
+ if not isinstance(value, list):
977
+ raise BridgeError(f"Merged projection line is not a JSON array: {line!r}")
978
+ rows.append(value)
979
+ return rows
980
+
981
+
982
+ def _display(value: Any) -> Any:
983
+ return None if value is MISSING else value
984
+
985
+
986
+ def _row_kind(base: Any, ours: Any, theirs: Any) -> str:
987
+ if base is MISSING:
988
+ if ours is MISSING:
989
+ return "inserted_theirs"
990
+ if theirs is MISSING:
991
+ return "inserted_ours"
992
+ return "inserted_both_same" if ours == theirs else "inserted_both_conflict"
993
+ if ours is MISSING and theirs is MISSING:
994
+ return "deleted_both"
995
+ if ours is MISSING:
996
+ return "deleted_ours" if theirs == base else "delete_modify_ours"
997
+ if theirs is MISSING:
998
+ return "deleted_theirs" if ours == base else "delete_modify_theirs"
999
+ if ours == theirs:
1000
+ return "same"
1001
+ if ours == base:
1002
+ return "modified_theirs"
1003
+ if theirs == base:
1004
+ return "modified_ours"
1005
+ return "modified_both"
1006
+
1007
+
1008
+ def _cell_diffs(
1009
+ fields: tuple[str, ...],
1010
+ base_rows: list[list[Any]],
1011
+ ours_rows: list[list[Any]],
1012
+ theirs_rows: list[list[Any]],
1013
+ ) -> tuple[list[dict[str, Any]], bool]:
1014
+ alignment_required = len({len(base_rows), len(ours_rows), len(theirs_rows)}) > 1
1015
+ slots: list[dict[str, Any]] = []
1016
+ for index in range(max(len(base_rows), len(ours_rows), len(theirs_rows), 0)):
1017
+ base = base_rows[index] if index < len(base_rows) else MISSING
1018
+ ours = ours_rows[index] if index < len(ours_rows) else MISSING
1019
+ theirs = theirs_rows[index] if index < len(theirs_rows) else MISSING
1020
+ differences = []
1021
+ for column, field in enumerate(fields):
1022
+ base_value = base[column] if base is not MISSING and column < len(base) else MISSING
1023
+ ours_value = ours[column] if ours is not MISSING and column < len(ours) else MISSING
1024
+ theirs_value = theirs[column] if theirs is not MISSING and column < len(theirs) else MISSING
1025
+ if ours_value == theirs_value == base_value:
1026
+ continue
1027
+ if base_value is MISSING and theirs_value is not MISSING:
1028
+ resolution = "theirs"
1029
+ elif base_value is MISSING and ours_value is not MISSING:
1030
+ resolution = "ours"
1031
+ elif ours_value == theirs_value:
1032
+ resolution = "same"
1033
+ elif ours_value == base_value:
1034
+ resolution = "theirs"
1035
+ elif theirs_value == base_value:
1036
+ resolution = "ours"
1037
+ else:
1038
+ resolution = "conflict"
1039
+ differences.append({
1040
+ "field": field,
1041
+ "base": _display(base_value),
1042
+ "ours": _display(ours_value),
1043
+ "theirs": _display(theirs_value),
1044
+ "basePresent": base_value is not MISSING,
1045
+ "oursPresent": ours_value is not MISSING,
1046
+ "theirsPresent": theirs_value is not MISSING,
1047
+ "resolution": resolution,
1048
+ })
1049
+ slots.append({
1050
+ "slot": index + 1,
1051
+ "kind": _row_kind(base, ours, theirs),
1052
+ "base": _display(base),
1053
+ "ours": _display(ours),
1054
+ "theirs": _display(theirs),
1055
+ "cellDiffs": differences,
1056
+ })
1057
+ return slots, alignment_required
1058
+
1059
+
1060
+ def _merge_inserted_rows(
1061
+ ours_rows: list[list[Any]], theirs_rows: list[list[Any]]
1062
+ ) -> list[list[Any]]:
1063
+ """Union same-anchor inserts, collapsing cross-side duplicates, local first."""
1064
+ resolved = list(ours_rows)
1065
+ local_counts: dict[str, int] = {}
1066
+ for row in ours_rows:
1067
+ key = _line(row)
1068
+ local_counts[key] = local_counts.get(key, 0) + 1
1069
+ for row in theirs_rows:
1070
+ key = _line(row)
1071
+ if local_counts.get(key, 0):
1072
+ local_counts[key] -= 1
1073
+ else:
1074
+ resolved.append(row)
1075
+ return resolved
1076
+
1077
+
1078
+ def _row_edit_plan(
1079
+ base_rows: list[list[Any]], side_rows: list[list[Any]]
1080
+ ) -> tuple[list[Any], dict[int, list[list[Any]]]]:
1081
+ states: list[Any] = [MISSING] * len(base_rows)
1082
+ insertions: dict[int, list[list[Any]]] = {}
1083
+ matcher = SequenceMatcher(
1084
+ None,
1085
+ [_line(row) for row in base_rows],
1086
+ [_line(row) for row in side_rows],
1087
+ autojunk=False,
1088
+ )
1089
+ for operation, base_start, base_end, side_start, side_end in matcher.get_opcodes():
1090
+ if operation == "equal":
1091
+ for offset, row in enumerate(side_rows[side_start:side_end]):
1092
+ states[base_start + offset] = row
1093
+ elif operation == "replace":
1094
+ base_count = base_end - base_start
1095
+ side_block = side_rows[side_start:side_end]
1096
+ paired_count = min(base_count, len(side_block))
1097
+ for offset, row in enumerate(side_block[:paired_count]):
1098
+ states[base_start + offset] = row
1099
+ if len(side_block) > paired_count:
1100
+ insertions.setdefault(base_start + paired_count, []).extend(side_block[paired_count:])
1101
+ elif operation == "insert":
1102
+ insertions.setdefault(base_start, []).extend(side_rows[side_start:side_end])
1103
+ elif operation != "delete":
1104
+ raise BridgeError(f"Unsupported conflict-block row operation: {operation}")
1105
+ return states, insertions
1106
+
1107
+
1108
+ def _merge_base_row(
1109
+ fields: tuple[str, ...], base: list[Any], ours: Any, theirs: Any
1110
+ ) -> tuple[str, Any]:
1111
+ if ours is MISSING and theirs is MISSING:
1112
+ return "resolved", MISSING
1113
+ if ours is MISSING:
1114
+ if theirs == base:
1115
+ return "resolved", MISSING
1116
+ slots, _ = _cell_diffs(fields, [base], [], [theirs])
1117
+ return "conflict", slots[0]
1118
+ if theirs is MISSING:
1119
+ if ours == base:
1120
+ return "resolved", MISSING
1121
+ slots, _ = _cell_diffs(fields, [base], [ours], [])
1122
+ return "conflict", slots[0]
1123
+ if ours == theirs:
1124
+ return "resolved", ours
1125
+ if ours == base:
1126
+ return "resolved", theirs
1127
+ if theirs == base:
1128
+ return "resolved", ours
1129
+
1130
+ resolved = []
1131
+ has_conflict = False
1132
+ for index in range(len(fields)):
1133
+ base_value = base[index]
1134
+ ours_value = ours[index]
1135
+ theirs_value = theirs[index]
1136
+ if ours_value == theirs_value:
1137
+ resolved.append(ours_value)
1138
+ elif ours_value == base_value:
1139
+ resolved.append(theirs_value)
1140
+ elif theirs_value == base_value:
1141
+ resolved.append(ours_value)
1142
+ else:
1143
+ has_conflict = True
1144
+ resolved.append(theirs_value)
1145
+ if not has_conflict:
1146
+ return "resolved", resolved
1147
+ slots, _ = _cell_diffs(fields, [base], [ours], [theirs])
1148
+ return "conflict", slots[0]
1149
+
1150
+
1151
+ def _resolve_conflict_block(
1152
+ fields: tuple[str, ...],
1153
+ base_rows: list[list[Any]],
1154
+ ours_rows: list[list[Any]],
1155
+ theirs_rows: list[list[Any]],
1156
+ ) -> list[dict[str, Any]]:
1157
+ if not base_rows:
1158
+ return [{
1159
+ "type": "resolved",
1160
+ "policy": "same_anchor_insert_union_ours_first",
1161
+ "ours": ours_rows,
1162
+ "base": [],
1163
+ "theirs": theirs_rows,
1164
+ "rows": _merge_inserted_rows(ours_rows, theirs_rows),
1165
+ }]
1166
+
1167
+ ours_states, ours_insertions = _row_edit_plan(base_rows, ours_rows)
1168
+ theirs_states, theirs_insertions = _row_edit_plan(base_rows, theirs_rows)
1169
+ segments: list[dict[str, Any]] = []
1170
+ resolved_rows: list[list[Any]] = []
1171
+
1172
+ def flush_resolved() -> None:
1173
+ nonlocal resolved_rows
1174
+ if resolved_rows:
1175
+ segments.append({
1176
+ "type": "resolved",
1177
+ "policy": "continuous_anchor_three_way",
1178
+ "rows": resolved_rows,
1179
+ })
1180
+ resolved_rows = []
1181
+
1182
+ for anchor in range(len(base_rows) + 1):
1183
+ resolved_rows.extend(_merge_inserted_rows(
1184
+ ours_insertions.get(anchor, []), theirs_insertions.get(anchor, [])
1185
+ ))
1186
+ if anchor == len(base_rows):
1187
+ break
1188
+ status, value = _merge_base_row(
1189
+ fields, base_rows[anchor], ours_states[anchor], theirs_states[anchor]
1190
+ )
1191
+ if status == "resolved":
1192
+ if value is not MISSING:
1193
+ resolved_rows.append(value)
1194
+ continue
1195
+ flush_resolved()
1196
+ ours = ours_states[anchor]
1197
+ theirs = theirs_states[anchor]
1198
+ segments.append({
1199
+ "type": "conflict",
1200
+ "ours": [] if ours is MISSING else [ours],
1201
+ "base": [base_rows[anchor]],
1202
+ "theirs": [] if theirs is MISSING else [theirs],
1203
+ "alignmentRequired": ours is MISSING or theirs is MISSING,
1204
+ "rowSlots": [value],
1205
+ })
1206
+ flush_resolved()
1207
+ return segments
1208
+
1209
+
1210
+ def _parse_merge(output: str, fields: tuple[str, ...]) -> list[dict[str, Any]]:
1211
+ segments: list[dict[str, Any]] = []
1212
+ normal: list[str] = []
1213
+ ours: list[str] = []
1214
+ base: list[str] = []
1215
+ theirs: list[str] = []
1216
+ state = "normal"
1217
+
1218
+ def flush_normal() -> None:
1219
+ nonlocal normal
1220
+ if normal:
1221
+ segments.append({"type": "merged", "rows": _decode_rows(normal)})
1222
+ normal = []
1223
+
1224
+ for line in output.splitlines():
1225
+ if state == "normal" and line.startswith("<<<<<<< OURS"):
1226
+ flush_normal()
1227
+ state = "ours"
1228
+ ours, base, theirs = [], [], []
1229
+ elif state == "ours" and line.startswith("||||||| BASE"):
1230
+ state = "base"
1231
+ elif state == "base" and line == "=======":
1232
+ state = "theirs"
1233
+ elif state == "theirs" and line.startswith(">>>>>>> THEIRS"):
1234
+ decoded_ours = _decode_rows(ours)
1235
+ decoded_base = _decode_rows(base)
1236
+ decoded_theirs = _decode_rows(theirs)
1237
+ segments.extend(_resolve_conflict_block(
1238
+ fields, decoded_base, decoded_ours, decoded_theirs
1239
+ ))
1240
+ state = "normal"
1241
+ elif state == "normal":
1242
+ normal.append(line)
1243
+ elif state == "ours":
1244
+ ours.append(line)
1245
+ elif state == "base":
1246
+ base.append(line)
1247
+ else:
1248
+ theirs.append(line)
1249
+
1250
+ if state != "normal":
1251
+ raise BridgeError(f"Unterminated git conflict marker block ({state}).")
1252
+ flush_normal()
1253
+ return segments
1254
+
1255
+
1256
+ def _sheet_lines(ws) -> tuple[str, ...]:
1257
+ lines = []
1258
+ for row in range(1, (ws.max_row or 0) + 1):
1259
+ values = [_json_value(ws.cell(row, column).value) for column in range(1, (ws.max_column or 0) + 1)]
1260
+ while values and values[-1] is None:
1261
+ values.pop()
1262
+ if values and any(value is not None for value in values):
1263
+ lines.append(_line(values))
1264
+ return tuple(lines)
1265
+
1266
+
1267
+ def _sheet_similarity(left, right) -> float:
1268
+ return _column_similarity(_sheet_lines(left), _sheet_lines(right))
1269
+
1270
+
1271
+ def _match_base_sheets(base_workbook, side_workbook) -> tuple[dict[str, str], set[str]]:
1272
+ matches = {
1273
+ name: name for name in base_workbook.sheetnames if name in side_workbook.sheetnames
1274
+ }
1275
+ remaining_base = [name for name in base_workbook.sheetnames if name not in matches]
1276
+ remaining_side = [name for name in side_workbook.sheetnames if name not in matches.values()]
1277
+ scores = {
1278
+ (base_name, side_name): _sheet_similarity(
1279
+ base_workbook[base_name], side_workbook[side_name]
1280
+ )
1281
+ for base_name in remaining_base
1282
+ for side_name in remaining_side
1283
+ }
1284
+ while remaining_base and remaining_side:
1285
+ candidates = []
1286
+ for base_name in remaining_base:
1287
+ ranked = sorted(
1288
+ ((scores[(base_name, side_name)], side_name) for side_name in remaining_side),
1289
+ reverse=True,
1290
+ )
1291
+ if not ranked or ranked[0][0] < 0.60:
1292
+ continue
1293
+ if len(ranked) > 1 and ranked[0][0] == ranked[1][0]:
1294
+ continue
1295
+ score, side_name = ranked[0]
1296
+ reverse_ranked = sorted(
1297
+ ((scores[(candidate, side_name)], candidate) for candidate in remaining_base),
1298
+ reverse=True,
1299
+ )
1300
+ if len(reverse_ranked) > 1 and reverse_ranked[0][0] == reverse_ranked[1][0]:
1301
+ continue
1302
+ if reverse_ranked[0][1] == base_name:
1303
+ candidates.append((score, base_name, side_name))
1304
+ if not candidates:
1305
+ break
1306
+ _, base_name, side_name = max(candidates)
1307
+ matches[base_name] = side_name
1308
+ remaining_base.remove(base_name)
1309
+ remaining_side.remove(side_name)
1310
+ return matches, set(remaining_side)
1311
+
1312
+
1313
+ def _sheet_triplets(base_workbook, ours_workbook, theirs_workbook) -> list[dict[str, str | None]]:
1314
+ ours_matches, ours_only = _match_base_sheets(base_workbook, ours_workbook)
1315
+ theirs_matches, theirs_only = _match_base_sheets(base_workbook, theirs_workbook)
1316
+ triplets = [
1317
+ {
1318
+ "logical": base_name,
1319
+ "base": base_name,
1320
+ "ours": ours_matches.get(base_name),
1321
+ "theirs": theirs_matches.get(base_name),
1322
+ }
1323
+ for base_name in base_workbook.sheetnames
1324
+ ]
1325
+ for name in sorted(ours_only & theirs_only):
1326
+ triplets.append({"logical": name, "base": None, "ours": name, "theirs": name})
1327
+ for name in sorted(theirs_only - ours_only):
1328
+ triplets.append({"logical": name, "base": None, "ours": None, "theirs": name})
1329
+ for name in sorted(ours_only - theirs_only):
1330
+ triplets.append({"logical": name, "base": None, "ours": name, "theirs": None})
1331
+ return triplets
1332
+
1333
+
1334
+ def _sheet_rename_status(base_name: str | None, ours_name: str | None, theirs_name: str | None) -> str:
1335
+ if base_name is None:
1336
+ return "not_applicable"
1337
+ ours_changed = ours_name is not None and ours_name != base_name
1338
+ theirs_changed = theirs_name is not None and theirs_name != base_name
1339
+ if ours_changed and theirs_changed:
1340
+ return "renamed_both_same" if ours_name == theirs_name else "rename_conflict"
1341
+ if ours_changed:
1342
+ return "renamed_ours"
1343
+ if theirs_changed:
1344
+ return "renamed_theirs"
1345
+ return "unchanged"
1346
+
1347
+
1348
+ def _presence_action(base_exists: bool, ours_exists: bool, theirs_exists: bool) -> str:
1349
+ if not base_exists:
1350
+ if ours_exists and theirs_exists:
1351
+ return "added_both"
1352
+ return "added_ours" if ours_exists else "added_theirs"
1353
+ if not ours_exists and not theirs_exists:
1354
+ return "deleted_both"
1355
+ if not ours_exists:
1356
+ return "deleted_ours_or_modified_theirs"
1357
+ if not theirs_exists:
1358
+ return "deleted_theirs_or_modified_ours"
1359
+ return "merge"
1360
+
1361
+
1362
+ def analyze_workbooks(
1363
+ base_path: Path | str,
1364
+ ours_path: Path | str,
1365
+ theirs_path: Path | str,
1366
+ output_dir: Path | str,
1367
+ *,
1368
+ ignore_columns: Iterable[str] = (),
1369
+ write_sidecars: bool = True,
1370
+ skip_hidden_sheets: bool = True,
1371
+ ) -> dict[str, Any]:
1372
+ base_path = Path(base_path).resolve()
1373
+ ours_path = Path(ours_path).resolve()
1374
+ theirs_path = Path(theirs_path).resolve()
1375
+ output_dir = Path(output_dir).resolve()
1376
+ output_dir.mkdir(parents=True, exist_ok=True)
1377
+ ignore_columns = tuple(str(name) for name in ignore_columns)
1378
+ ignored = {str(name).casefold() for name in ignore_columns}
1379
+
1380
+ wb_base = load_workbook(base_path, data_only=False, keep_links=False)
1381
+ wb_ours = load_workbook(ours_path, data_only=False, keep_links=False)
1382
+ wb_theirs = load_workbook(theirs_path, data_only=False, keep_links=False)
1383
+ try:
1384
+ sheets = []
1385
+ total_conflicts = 0
1386
+ total_column_conflicts = 0
1387
+ total_sheet_conflicts = 0
1388
+ clean_merges = 0
1389
+ triplets = _sheet_triplets(wb_base, wb_ours, wb_theirs)
1390
+ for sheet_index, triplet in enumerate(triplets, 1):
1391
+ name = str(triplet["logical"])
1392
+ base_name = triplet["base"]
1393
+ ours_name = triplet["ours"]
1394
+ theirs_name = triplet["theirs"]
1395
+ if skip_hidden_sheets:
1396
+ # 隐藏页签(工具派生表)默认不参与冲突检测:以本地可见性为准
1397
+ hidden = False
1398
+ if ours_name is not None and ours_name in wb_ours.sheetnames:
1399
+ hidden = wb_ours[ours_name].sheet_state == "hidden"
1400
+ elif theirs_name is not None and theirs_name in wb_theirs.sheetnames:
1401
+ hidden = wb_theirs[theirs_name].sheet_state == "hidden"
1402
+ if hidden:
1403
+ continue
1404
+ base_exists = base_name is not None
1405
+ ours_exists = ours_name is not None
1406
+ theirs_exists = theirs_name is not None
1407
+ action = _presence_action(base_exists, ours_exists, theirs_exists)
1408
+ rename_status = _sheet_rename_status(base_name, ours_name, theirs_name)
1409
+ sheet_report: dict[str, Any] = {
1410
+ "name": name,
1411
+ "names": {"base": base_name, "ours": ours_name, "theirs": theirs_name},
1412
+ "action": action,
1413
+ "renameStatus": rename_status,
1414
+ "present": {"base": base_exists, "ours": ours_exists, "theirs": theirs_exists},
1415
+ }
1416
+ if action != "merge":
1417
+ sheet_conflict = None
1418
+ if not base_exists and ours_exists and theirs_exists:
1419
+ sheet_report.update({
1420
+ "status": "clean",
1421
+ "resolution": "theirs",
1422
+ "policy": "no_base_remote_wins",
1423
+ })
1424
+ elif not base_exists:
1425
+ sheet_report.update({
1426
+ "status": "clean",
1427
+ "resolution": "ours" if ours_exists else "theirs",
1428
+ })
1429
+ elif not ours_exists and not theirs_exists:
1430
+ sheet_report.update({"status": "clean", "resolution": "deleted_both"})
1431
+ else:
1432
+ survivor = "ours" if ours_exists else "theirs"
1433
+ survivor_name = ours_name if ours_exists else theirs_name
1434
+ survivor_workbook = wb_ours if ours_exists else wb_theirs
1435
+ unchanged = (
1436
+ survivor_name == base_name
1437
+ and _sheet_lines(wb_base[base_name]) == _sheet_lines(survivor_workbook[survivor_name])
1438
+ )
1439
+ if unchanged:
1440
+ sheet_report.update({
1441
+ "status": "clean",
1442
+ "resolution": "theirs" if not ours_exists else "ours",
1443
+ })
1444
+ else:
1445
+ deleted = "ours" if not ours_exists else "theirs"
1446
+ sheet_conflict = {
1447
+ "kind": f"delete_modify_{deleted}",
1448
+ "survivor": survivor,
1449
+ "survivorName": survivor_name,
1450
+ }
1451
+ sheet_report.update({"status": "conflict", "sheetConflict": sheet_conflict})
1452
+ total_sheet_conflicts += 1
1453
+ sheets.append(sheet_report)
1454
+ continue
1455
+
1456
+ layouts = {
1457
+ "base": _layout(wb_base[base_name]),
1458
+ "ours": _layout(wb_ours[ours_name]),
1459
+ "theirs": _layout(wb_theirs[theirs_name]),
1460
+ }
1461
+ worksheets = {
1462
+ "base": wb_base[base_name],
1463
+ "ours": wb_ours[ours_name],
1464
+ "theirs": wb_theirs[theirs_name],
1465
+ }
1466
+ layouts, column_alignment, column_alignment_mode = _align_layouts(
1467
+ worksheets, layouts
1468
+ )
1469
+ common = set(layouts["base"].fields) & set(layouts["ours"].fields) & set(layouts["theirs"].fields)
1470
+ projection_fields = tuple(
1471
+ field for field in layouts["base"].fields
1472
+ if field in common
1473
+ and field.casefold() not in ignored
1474
+ and not (layouts["base"].var_row is not None and field == "@1")
1475
+ )
1476
+ if not projection_fields:
1477
+ sheet_report.update({
1478
+ "status": "unsupported",
1479
+ "error": "No common projection fields remain after column alignment/ignore filtering.",
1480
+ })
1481
+ sheets.append(sheet_report)
1482
+ continue
1483
+
1484
+ rows = {
1485
+ "base": _rows(wb_base[base_name], layouts["base"], projection_fields),
1486
+ "ours": _rows(wb_ours[ours_name], layouts["ours"], projection_fields),
1487
+ "theirs": _rows(wb_theirs[theirs_name], layouts["theirs"], projection_fields),
1488
+ }
1489
+ row_alignment = _row_alignment_report(rows)
1490
+ column_conflict_count = _annotate_column_conflicts(column_alignment, rows)
1491
+ total_column_conflicts += column_conflict_count
1492
+ sheet_dir = output_dir / _safe_name(name, sheet_index)
1493
+ sheet_dir.mkdir(parents=True, exist_ok=True)
1494
+ paths = {}
1495
+ for side in ("base", "ours", "theirs"):
1496
+ projection_path = sheet_dir / f"{side}.jsonl"
1497
+ _write_projection(projection_path, rows[side])
1498
+ if write_sidecars:
1499
+ sidecar_path = sheet_dir / f"{side}.sidecar.json"
1500
+ _write_sidecar(sidecar_path, layouts[side], rows[side])
1501
+ paths[side] = projection_path
1502
+
1503
+ merged_text, raw_git_clean, git_stderr = _git_merge_file(
1504
+ paths["ours"], paths["base"], paths["theirs"]
1505
+ )
1506
+ merge_output_path = sheet_dir / "git-merge-output.txt"
1507
+ _write_text_lf(merge_output_path, merged_text)
1508
+ segments = _parse_merge(merged_text, projection_fields)
1509
+ conflict_count = sum(segment["type"] == "conflict" for segment in segments)
1510
+ total_conflicts += conflict_count
1511
+ git_clean = conflict_count == 0
1512
+ sheet_conflict_count = int(rename_status == "rename_conflict")
1513
+ total_sheet_conflicts += sheet_conflict_count
1514
+ sheet_clean = git_clean and column_conflict_count == 0 and sheet_conflict_count == 0
1515
+ clean_merges += int(sheet_clean)
1516
+ all_fields = [entry["field"] for entry in column_alignment]
1517
+ sheet_report.update({
1518
+ "status": "clean" if sheet_clean else "conflict",
1519
+ "gitStatus": "clean" if git_clean else "conflict",
1520
+ "gitRawStatus": "clean" if raw_git_clean else "conflict",
1521
+ "columnAlignmentMode": column_alignment_mode,
1522
+ "columnAlignment": column_alignment,
1523
+ "columnConflictCount": column_conflict_count,
1524
+ "rowAlignment": row_alignment,
1525
+ "sheetConflictCount": sheet_conflict_count,
1526
+ "projectionFields": list(projection_fields),
1527
+ "ignoredFields": [field for field in layouts["base"].fields if field.casefold() in ignored],
1528
+ "columns": {
1529
+ "base": list(layouts["base"].fields),
1530
+ "ours": list(layouts["ours"].fields),
1531
+ "theirs": list(layouts["theirs"].fields),
1532
+ "baseOnly": sorted(set(layouts["base"].fields) - set(layouts["ours"].fields) - set(layouts["theirs"].fields)),
1533
+ "oursOnly": sorted(set(layouts["ours"].fields) - set(layouts["base"].fields)),
1534
+ "theirsOnly": sorted(set(layouts["theirs"].fields) - set(layouts["base"].fields)),
1535
+ "all": all_fields,
1536
+ },
1537
+ "rowCounts": {side: len(rows[side]) for side in rows},
1538
+ "conflictBlockCount": conflict_count,
1539
+ "segments": segments,
1540
+ "artifacts": {
1541
+ "directory": str(sheet_dir),
1542
+ "gitMergeOutput": str(merge_output_path),
1543
+ "base": str(paths["base"]),
1544
+ "ours": str(paths["ours"]),
1545
+ "theirs": str(paths["theirs"]),
1546
+ },
1547
+ "gitStderr": git_stderr,
1548
+ })
1549
+ sheets.append(sheet_report)
1550
+
1551
+ report = {
1552
+ "version": 4,
1553
+ "inputs": {"base": str(base_path), "ours": str(ours_path), "theirs": str(theirs_path)},
1554
+ "ignoreColumns": sorted(ignore_columns),
1555
+ "summary": {
1556
+ "sheetCount": len(sheets),
1557
+ "cleanMergeSheetCount": clean_merges,
1558
+ "conflictBlockCount": total_conflicts,
1559
+ "columnConflictCount": total_column_conflicts,
1560
+ "sheetConflictCount": total_sheet_conflicts,
1561
+ },
1562
+ "sheets": sheets,
1563
+ }
1564
+ report_path = output_dir / "report.json"
1565
+ report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
1566
+ report["reportPath"] = str(report_path)
1567
+ return report
1568
+ finally:
1569
+ wb_base.close()
1570
+ wb_ours.close()
1571
+ wb_theirs.close()
1572
+
1573
+
1574
+ def build_parser() -> argparse.ArgumentParser:
1575
+ parser = argparse.ArgumentParser(description=__doc__)
1576
+ parser.add_argument("--base", required=True, help="Common-ancestor XLSX path.")
1577
+ parser.add_argument("--ours", required=True, help="Local/ours XLSX path.")
1578
+ parser.add_argument("--theirs", required=True, help="Remote/theirs XLSX path.")
1579
+ parser.add_argument("--output-dir", required=True, help="Directory for JSONL, sidecars, and report.json.")
1580
+ parser.add_argument(
1581
+ "--ignore-column", action="append", default=[],
1582
+ help="Field name excluded from row-alignment JSONL; repeat for multiple fields.",
1583
+ )
1584
+ parser.add_argument("--print-report", action="store_true", help="Print the complete report instead of the summary.")
1585
+ return parser
1586
+
1587
+
1588
+ def main(argv: list[str] | None = None) -> int:
1589
+ args = build_parser().parse_args(argv)
1590
+ try:
1591
+ report = analyze_workbooks(
1592
+ args.base,
1593
+ args.ours,
1594
+ args.theirs,
1595
+ args.output_dir,
1596
+ ignore_columns=args.ignore_column,
1597
+ )
1598
+ output = report if args.print_report else {
1599
+ "ok": True,
1600
+ "report": report["reportPath"],
1601
+ "summary": report["summary"],
1602
+ }
1603
+ print(json.dumps(output, ensure_ascii=False, indent=2))
1604
+ return 0
1605
+ except (BridgeError, OSError, ValueError) as exc:
1606
+ print(json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False, indent=2))
1607
+ return 1
1608
+
1609
+
1610
+ if __name__ == "__main__":
1611
+ sys.exit(main())