@morning-start/dsh-code-toolkit 0.28.1 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +148 -0
- package/README.md +110 -2
- package/agents/caption/AGENT.md +86 -76
- package/agents/caption/references/dispatch.md +103 -93
- package/agents/caption/references/evidence.md +48 -64
- package/agents/caption/references/experts.md +51 -78
- package/agents/caption/references/flows.md +51 -53
- package/agents/caption/references/loop.md +114 -93
- package/agents/design/AGENT.md +2 -1
- package/agents/merge/AGENT.md +79 -0
- package/agents/merge/examples/bad.md +40 -0
- package/agents/merge/examples/good.md +52 -0
- package/agents/merge/references/method.md +71 -0
- package/agents/merge/references/verdicts.md +57 -0
- package/agents/review/AGENT.md +5 -2
- package/agents/scrub/AGENT.md +5 -3
- package/agents/simplify/AGENT.md +11 -7
- package/index.js +76 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
- package/src/caption.js +18 -3
- package/src/command.js +72 -1
- package/src/i18n.js +16 -0
- package/src/merge.js +150 -0
package/src/merge.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { git, mergeBase } from './selection.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Merge analysis with the fork point as the baseline.
|
|
5
|
+
*
|
|
6
|
+
* Comparing "my branch" against "their branch" answers the wrong question: it lists every
|
|
7
|
+
* difference, most of which is not a conflict at all. Two sides that changed different files
|
|
8
|
+
* cannot collide. What matters is three facts, and all three are computed here rather than
|
|
9
|
+
* eyeballed:
|
|
10
|
+
*
|
|
11
|
+
* 1. the merge base (the fork point),
|
|
12
|
+
* 2. what each side changed *relative to that base*,
|
|
13
|
+
* 3. for the files both sides touched, whether their hunks cover the same base lines.
|
|
14
|
+
*
|
|
15
|
+
* Line numbers from `-U0` are base coordinates, so a hunk from each side can be compared
|
|
16
|
+
* directly. Context is what makes git report a conflict next to a change it could not merge,
|
|
17
|
+
* so "adjacent" counts as real: two hunks six lines apart have their context blocks touching.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Files changed by one diff, as `{ status, path }`, renames resolved to the new path. */
|
|
21
|
+
export function parseNameStatus(text) {
|
|
22
|
+
const files = []
|
|
23
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
24
|
+
if (!line.trim()) continue
|
|
25
|
+
const [status, first, second] = line.split('\t')
|
|
26
|
+
if (!status) continue
|
|
27
|
+
// R100/C100 carry two paths; the new path is the one that exists now.
|
|
28
|
+
files.push({ status: status.trim(), path: (second ?? first ?? '').trim() })
|
|
29
|
+
}
|
|
30
|
+
return files
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Split a `git diff -U0` into `{ path -> [{ start, end }] }` in base-line coordinates. */
|
|
34
|
+
export function parseHunks(text) {
|
|
35
|
+
const hunks = new Map()
|
|
36
|
+
let path = ''
|
|
37
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
38
|
+
if (line.startsWith('+++ b/')) {
|
|
39
|
+
path = line.slice('+++ b/'.length).trim()
|
|
40
|
+
if (!hunks.has(path)) hunks.set(path, [])
|
|
41
|
+
continue
|
|
42
|
+
}
|
|
43
|
+
const header = /^@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@/.exec(line)
|
|
44
|
+
if (!header || !path) continue
|
|
45
|
+
const start = Number(header[1])
|
|
46
|
+
const count = header[2] === undefined ? 1 : Number(header[2])
|
|
47
|
+
// A pure deletion reports `-37,0`: it occupies no base line, but it still sits at 37.
|
|
48
|
+
const end = count === 0 ? start : start + count - 1
|
|
49
|
+
hunks.get(path).push({ start, end })
|
|
50
|
+
}
|
|
51
|
+
return hunks
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* How two sides' hunks relate, in base coordinates.
|
|
56
|
+
*
|
|
57
|
+
* `same` — they cover common lines: git will conflict and a person must choose.
|
|
58
|
+
* `adjacent` — no shared line, but within one context block (6 lines, both sides): git will
|
|
59
|
+
* still conflict, because the merged context around them overlaps.
|
|
60
|
+
* `separate` — different regions of the same file: git merges them, but the *semantics* are
|
|
61
|
+
* still worth one read, since both sides changed one file for possibly conflicting reasons.
|
|
62
|
+
*/
|
|
63
|
+
export function classify(ours, theirs) {
|
|
64
|
+
let verdict = 'separate'
|
|
65
|
+
for (const a of ours) {
|
|
66
|
+
for (const b of theirs) {
|
|
67
|
+
const gap = Math.max(a.start, b.start) - Math.min(a.end, b.end)
|
|
68
|
+
if (gap <= 0) return 'same'
|
|
69
|
+
if (gap <= 6) verdict = 'adjacent'
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return verdict
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const side = (hunks) => hunks.map((h) => (h.start === h.end ? `${h.start}` : `${h.start}-${h.end}`)).join(', ')
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Collect everything the merge report needs. Throws when there is nothing to merge, because
|
|
79
|
+
* "no merge in progress and no ref given" is a usage error, not an empty report.
|
|
80
|
+
*/
|
|
81
|
+
export async function collectMergeFacts({ cwd, ref, signal }) {
|
|
82
|
+
let theirs = ref
|
|
83
|
+
if (!theirs) {
|
|
84
|
+
theirs = (await git(cwd, ['rev-parse', '--verify', 'MERGE_HEAD'], signal)).trim()
|
|
85
|
+
}
|
|
86
|
+
const ours = 'HEAD'
|
|
87
|
+
const base = (await mergeBase(cwd, theirs, signal)).trim()
|
|
88
|
+
|
|
89
|
+
const [oursFiles, theirFiles] = await Promise.all([
|
|
90
|
+
git(cwd, ['diff', '--name-status', `${base}..${ours}`], signal).then(parseNameStatus),
|
|
91
|
+
git(cwd, ['diff', '--name-status', `${base}..${theirs}`], signal).then(parseNameStatus),
|
|
92
|
+
])
|
|
93
|
+
const ourPaths = new Map(oursFiles.map((file) => [file.path, file.status]))
|
|
94
|
+
const theirPaths = new Map(theirFiles.map((file) => [file.path, file.status]))
|
|
95
|
+
const shared = [...ourPaths.keys()].filter((path) => theirPaths.has(path)).sort()
|
|
96
|
+
const onlyOurs = [...ourPaths.keys()].filter((path) => !theirPaths.has(path)).length
|
|
97
|
+
const onlyTheirs = [...theirPaths.keys()].filter((path) => !ourPaths.has(path)).length
|
|
98
|
+
|
|
99
|
+
const rows = []
|
|
100
|
+
for (const path of shared) {
|
|
101
|
+
const [ourDiff, theirDiff] = await Promise.all([
|
|
102
|
+
git(cwd, ['diff', '-U0', `${base}..${ours}`, '--', path], signal),
|
|
103
|
+
git(cwd, ['diff', '-U0', `${base}..${theirs}`, '--', path], signal),
|
|
104
|
+
])
|
|
105
|
+
const ourHunks = parseHunks(ourDiff).get(path) ?? []
|
|
106
|
+
const theirHunks = parseHunks(theirDiff).get(path) ?? []
|
|
107
|
+
rows.push({ path, verdict: classify(ourHunks, theirHunks), ours: side(ourHunks), theirs: side(theirHunks) })
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
ours,
|
|
112
|
+
theirs,
|
|
113
|
+
base,
|
|
114
|
+
counts: { ours: oursFiles.length, theirs: theirFiles.length, shared: shared.length, onlyOurs, onlyTheirs },
|
|
115
|
+
rows,
|
|
116
|
+
real: rows.filter((row) => row.verdict !== 'separate').length,
|
|
117
|
+
auto: rows.filter((row) => row.verdict === 'separate').length,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The facts block the report is written against; computed, never guessed. */
|
|
122
|
+
export function renderMergeFacts(facts) {
|
|
123
|
+
const { counts } = facts
|
|
124
|
+
const lines = [
|
|
125
|
+
'## Merge facts (computed from the fork point — do not re-derive these by eye)',
|
|
126
|
+
'',
|
|
127
|
+
`- Fork point (merge base): \`${facts.base.slice(0, 12)}\``,
|
|
128
|
+
`- Ours: \`${facts.ours}\`; theirs: \`${facts.theirs}\``,
|
|
129
|
+
`- Ours changed ${counts.ours} files, theirs changed ${counts.theirs}.`,
|
|
130
|
+
`- **Shared: ${counts.shared}** — the only files that can conflict.`,
|
|
131
|
+
`- Ours-only ${counts.onlyOurs}, theirs-only ${counts.onlyTheirs}: disjoint by construction, each side keeps its own; do not propose touching them.`,
|
|
132
|
+
'',
|
|
133
|
+
]
|
|
134
|
+
if (!facts.rows.length) {
|
|
135
|
+
lines.push('No shared file: this merge has no textual conflict. Say so and stop.')
|
|
136
|
+
return lines.join('\n')
|
|
137
|
+
}
|
|
138
|
+
lines.push(`Real conflicts: **${facts.real}**; same-file-different-region: ${facts.auto}.`, '')
|
|
139
|
+
lines.push('| file | ours (base lines) | theirs (base lines) | verdict |')
|
|
140
|
+
lines.push('| --- | --- | --- | --- |')
|
|
141
|
+
for (const row of facts.rows) {
|
|
142
|
+
const verdict = row.verdict === 'same'
|
|
143
|
+
? '**conflict — same lines**'
|
|
144
|
+
: row.verdict === 'adjacent'
|
|
145
|
+
? '**conflict — contexts overlap**'
|
|
146
|
+
: 'auto-merge, confirm semantics'
|
|
147
|
+
lines.push(`| ${row.path} | ${row.ours || '—'} | ${row.theirs || '—'} | ${verdict} |`)
|
|
148
|
+
}
|
|
149
|
+
return lines.join('\n')
|
|
150
|
+
}
|