uniweb 1.26.8 → 1.26.9

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.
@@ -53,6 +53,11 @@ import { join, dirname, posix } from 'node:path'
53
53
 
54
54
  export const SYNC_TRAILER = 'Uniweb-Sync'
55
55
  export const SITE_TRAILER = 'Uniweb-Site'
56
+ /**
57
+ * Names the tree of each of a site's roots outside its folder — a mounted folder elsewhere in the
58
+ * repository — in a sync point: `Uniweb-Root: <path>:<tree>`, the path from the repository's top.
59
+ */
60
+ export const ROOT_TRAILER = 'Uniweb-Root'
56
61
  /** Marks the commit a push makes of what it wrote back: `Uniweb-Recorded: <origin> <site>`. */
57
62
  export const RECORDED_TRAILER = 'Uniweb-Recorded'
58
63
 
@@ -156,13 +161,32 @@ function isCommit(repo, sha) {
156
161
 
157
162
  /** The tree of the site's directory in a commit or tree, or null. */
158
163
  export function siteTreeIn(repo, rev) {
164
+ return repo.path === '.' ? objectAt(repo, `${rev}^{tree}`) : objectAt(repo, `${rev}:${repo.path}`)
165
+ }
166
+
167
+ // What `spec` names — a tree, a file's blob — or null.
168
+ function objectAt(repo, spec) {
159
169
  try {
160
- return gitText(repo.top, ['rev-parse', '--verify', '-q', repo.path === '.' ? `${rev}^{tree}` : `${rev}:${repo.path}`]).trim() || null
170
+ return gitText(repo.top, ['rev-parse', '--verify', '-q', spec]).trim() || null
161
171
  } catch {
162
172
  return null
163
173
  }
164
174
  }
165
175
 
176
+ /**
177
+ * The site's roots outside its folder, each by its path from the repository's top: a mounted folder
178
+ * elsewhere in the repository (`utils/site-roots.js`). None for a site that is the whole repository.
179
+ */
180
+ function outsideRoots(repo, roots = []) {
181
+ if (!repo.prefix) return []
182
+ return [...new Set(roots.map((r) => underTop(repo, r)))].filter((f) => !`${f}/`.startsWith(repo.prefix))
183
+ }
184
+
185
+ /** What of the repository a pull merges into, as pathspecs: the site's folder, and each root outside it. */
186
+ function siteScope(repo, roots = []) {
187
+ return repo.prefix ? [repo.prefix, ...outsideRoots(repo, roots)] : []
188
+ }
189
+
166
190
  /** The paths in conflict here, whatever left them — a merge, or a stash put back over one. */
167
191
  export function unmergedPaths(repo) {
168
192
  try {
@@ -195,7 +219,10 @@ function trailersOf(body) {
195
219
  if (m) {
196
220
  out.path = m[1]
197
221
  out.tree = m[2]
222
+ continue
198
223
  }
224
+ m = line.match(/^Uniweb-Root: (.+):([0-9a-f]{40,64})\s*$/)
225
+ if (m) (out.roots ||= []).push({ path: m[1], tree: m[2] })
199
226
  }
200
227
  return out
201
228
  }
@@ -229,11 +256,14 @@ export function historySyncPoint(repo, { origin, siteUuid }) {
229
256
  }
230
257
 
231
258
  // Does this commit's message mark it a sync point of this site and backend — and does it
232
- // still hold the site tree its trailer names?
259
+ // still hold the site tree its trailer names, and the tree of each root outside the site's folder?
260
+ // ⭐ A rebase copies the trailers onto a commit that holds whatever came before it: a teammate's edit
261
+ // to a mounted folder outside the site's, rebased under it, would leave the site's tree as it was, and
262
+ // a base that read as the backend's would hold that edit — which the next pull's merge then undid.
233
263
  function marks(repo, sha, body, { origin, siteUuid }) {
234
264
  const t = trailersOf(body)
235
265
  if (t.origin !== origin || t.siteUuid !== siteUuid || t.path !== repo.path) return false
236
- return siteTreeIn(repo, sha) === t.tree
266
+ return siteTreeIn(repo, sha) === t.tree && (t.roots || []).every((r) => objectAt(repo, `${sha}:${r.path}`) === r.tree)
237
267
  }
238
268
 
239
269
  /**
@@ -382,13 +412,42 @@ export function recordedAfter(repo, sha, { origin, siteUuid }) {
382
412
  /** Does the content under `roots` differ between two commits? Anything but "no" counts as yes. */
383
413
  function differsIn(repo, a, b, roots) {
384
414
  try {
385
- git(repo.top, ['diff', '--quiet', a, b, '--', ...roots.map((r) => `${repo.prefix}${r}`)])
415
+ git(repo.top, ['diff', '--quiet', a, b, '--', ...roots.map((r) => underTop(repo, r))])
386
416
  return false
387
417
  } catch {
388
418
  return true
389
419
  }
390
420
  }
391
421
 
422
+ /**
423
+ * The commit a writable mount's submodule merges from: the one the site's base records for it, where
424
+ * the submodule's checkout holds it; else the one that commit was made onto, where the checkout holds
425
+ * that and the recorded commit changed only the mounted folders — a pull's own commit, which a squash
426
+ * merge upstream drops and whose parent it keeps; else none, and the merge is of unrelated histories.
427
+ *
428
+ * @param {object} repo - the site's `repoOf`
429
+ * @param {string|null} siteBase - the commit the site merges from
430
+ * @param {{ at: string, sub: object, roots: string[] }} n - the submodule's path from the site
431
+ * repository's top, its `repoOf`, and the mounted folders in it
432
+ * @returns {string|null}
433
+ */
434
+ export function nestedBase(repo, siteBase, { at, sub, roots }) {
435
+ if (!siteBase || !sub?.head) return null
436
+ const recorded = objectAt(repo, `${siteBase}:${posix.normalize(at)}`)
437
+ if (!recorded) return null
438
+ const holds = (sha) => isCommit(sub, sha) && isAncestor(sub, sha)
439
+ if (holds(recorded)) return recorded
440
+ if (!isCommit(sub, recorded)) return null
441
+ const parent = objectAt(sub, `${recorded}^1`)
442
+ if (!parent || !holds(parent)) return null
443
+ try {
444
+ git(sub.top, ['diff', '--quiet', parent, recorded, '--', '.', ...roots.map((r) => `:(exclude)${r}`)])
445
+ return parent
446
+ } catch {
447
+ return null // it changed more than the mounted folders: not a pull's
448
+ }
449
+ }
450
+
392
451
  // ── the stand-in ────────────────────────────────────────────────────────────
393
452
 
394
453
  /**
@@ -408,24 +467,53 @@ function differsIn(repo, a, b, roots) {
408
467
  * kept out of git — is in no commit, so no merge brings it. `placeIgnored()` copies it
409
468
  * into the project, where the project lacks it.
410
469
  *
470
+ * ⭐ A ROOT IN A REPOSITORY OF ITS OWN — a writable mount in a submodule of the site's (`nested`) — is
471
+ * filled from that repository at its own base, and committed in its own history, as the person who
472
+ * pulls, with no hooks and no site named: the site's tree holds only the submodule's commit, which
473
+ * `commit()` sets to the one it made there.
474
+ *
411
475
  * @param {object} repo - `repoOf`
412
476
  * @param {object} opts
413
477
  * @param {string[]} opts.roots - the site's content roots, relative to the site
414
478
  * @param {string|null} opts.rev - the commit whose content the roots are filled from;
415
479
  * null for none
480
+ * @param {Array<{ repo: object, at: string, roots: string[], rev: string|null, parent: string|null }>} [opts.nested] -
481
+ * each submodule a writable mount is in: its `repoOf`, its path from the site repository's top,
482
+ * the mounted folders in it (relative to its own top), the commit they are filled from, and the
483
+ * one a commit of them is made onto (null: none, an unrelated history)
416
484
  * @returns {{ siteDir: string, commit: Function, changes: Function, placeIgnored: Function, close: Function }}
417
485
  */
418
- export function openStandIn(repo, { roots, rev }) {
419
- for (const r of roots) {
420
- if (r.split('/').includes('..') || r.startsWith('/')) {
421
- throw new Error(`the content root \`${r}\` is outside the site, so a pull cannot merge it`)
486
+ export function openStandIn(repo, { roots, rev, nested = [] }) {
487
+ // ⭐ A root is named relative to the site and held by the repository, so one outside the site's
488
+ // folder — a mounted folder elsewhere in the repository — is filled and committed like the rest.
489
+ // One outside the repository is in no commit of it. ⛔ Until 2026-11-04 every root leading out of
490
+ // the site's folder was refused, so a mount was left out of the merge, linked to the working tree.
491
+ const full = roots.map((r) => underTop(repo, r))
492
+ roots.forEach((r, i) => {
493
+ if (r.startsWith('/') || full[i] === '..' || full[i].startsWith('../')) {
494
+ throw new Error(`the content root \`${r}\` is outside the site's repository, so a pull cannot merge it`)
422
495
  }
423
- }
496
+ })
424
497
  const root = mkdtempSync(join(tmpdir(), 'uniweb-stand-in-'))
425
498
  const indexDir = mkdtempSync(join(tmpdir(), 'uniweb-stand-in-index-'))
426
499
  const env = { GIT_INDEX_FILE: join(indexDir, 'index') }
427
500
  const at = (args, opts = {}) =>
428
501
  git(repo.top, ['--git-dir', repo.gitDir, '--work-tree', root, ...args], { ...opts, env: { ...env, ...(opts.env || {}) } })
502
+ // Each submodule's folders, with its own index; its git run against its part of the stand-in.
503
+ const subs = nested.map((n, i) => {
504
+ const where = posix.normalize(n.at)
505
+ if (where === '.' || where === '..' || where.startsWith('../') || where.startsWith('/')) {
506
+ throw new Error(`the submodule \`${n.at}\` is not inside the site's repository`)
507
+ }
508
+ const own = n.roots.map((r) => posix.normalize(r))
509
+ return { ...n, at: where, roots: own, full: own.map((r) => posix.normalize(`${where}/${r}`)), index: join(indexDir, `nested-${i}`) }
510
+ })
511
+ const nestedFull = new Set(subs.flatMap((n) => n.full))
512
+ const inSub = (n, args, opts = {}) =>
513
+ git(n.repo.top, ['--git-dir', n.repo.gitDir, '--work-tree', join(root, n.at), ...args], {
514
+ ...opts,
515
+ env: { GIT_INDEX_FILE: n.index, ...(opts.env || {}) }
516
+ })
429
517
  const links = []
430
518
  const close = () => {
431
519
  for (const l of links.reverse()) {
@@ -441,12 +529,13 @@ export function openStandIn(repo, { roots, rev }) {
441
529
 
442
530
  try {
443
531
  const sitePrefix = repo.prefix // '' or 'site/'
444
- const full = roots.map((r) => `${sitePrefix}${r}`)
445
532
  const fullSet = new Set(full)
446
533
  const sitePath = sitePrefix.replace(/\/$/, '')
447
534
  const inSite = (name) => (sitePath ? `${sitePath}/${name}` : name)
448
535
  const mustBeReal = (rel) =>
449
- (sitePath && (sitePath === rel || sitePath.startsWith(`${rel}/`))) || full.some((f) => f.startsWith(`${rel}/`))
536
+ (sitePath && (sitePath === rel || sitePath.startsWith(`${rel}/`))) ||
537
+ full.some((f) => f.startsWith(`${rel}/`)) ||
538
+ subs.some((n) => n.at === rel || n.full.some((f) => f.startsWith(`${rel}/`)))
450
539
 
451
540
  const link = (real, stand, name) => {
452
541
  let dir = false
@@ -478,6 +567,9 @@ export function openStandIn(repo, { roots, rev }) {
478
567
  const stand = join(standDir, name)
479
568
  if (!rel && name === '.git') continue
480
569
  if (fullSet.has(r)) continue // filled from `rev`, below
570
+ // A submodule's own folders are filled from its base, below, and its `.git` is no file of the site's.
571
+ if (nestedFull.has(r)) continue
572
+ if (name === '.git' && subs.some((n) => n.at === rel)) continue
481
573
  if (r === inSite('.uniweb')) {
482
574
  // This copy's own state, for the projection to read and write — taken back by the
483
575
  // caller. Not the rename index: it names paths in the working tree, not in `rev`.
@@ -518,21 +610,48 @@ export function openStandIn(repo, { roots, rev }) {
518
610
  : []
519
611
  if (present.length) at(['checkout', rev, '--', ...present])
520
612
 
613
+ // Each submodule's folders, as its own base has them.
614
+ for (const n of subs) {
615
+ mkdirSync(join(root, n.at), { recursive: true })
616
+ inSub(n, n.rev ? ['read-tree', n.rev] : ['read-tree', '--empty'])
617
+ n.present = n.rev
618
+ ? zlist(git(n.repo.top, ['--git-dir', n.repo.gitDir, 'ls-tree', '--name-only', '-z', '--full-tree', n.rev, '--', ...n.roots]))
619
+ : []
620
+ if (n.present.length) inSub(n, ['checkout', n.rev, '--', ...n.present])
621
+ }
622
+
521
623
  const siteDir = sitePath ? join(root, sitePath) : root
522
624
 
523
625
  /**
524
626
  * Commit the roots as the stand-in now holds them, onto `rev`'s tree — or report that
525
627
  * they are what `rev` holds (`always` commits them anyway, to mark a history that has
526
628
  * no sync point yet).
527
- * @returns {{ commit: string|null, tree: string, siteTree: string|null }}
629
+ * `nested`, for each submodule: the commit made of its folders (null where they are its base's), and
630
+ * the files it changed there.
631
+ * @returns {{ commit: string|null, tree: string, siteTree: string|null, nested: Array<{ at: string, repo: object, roots: string[], commit: string|null, files: string[] }> }}
528
632
  */
529
633
  const commit = ({ parent, subject, origin, siteUuid, always = false }) => {
634
+ // ⭐ Each submodule's folders first, in its own history: the site's commit records what it made.
635
+ const made = subs.map((n) => {
636
+ const staged = heldIn((a, o) => inSub(n, a, o), n.roots.filter((r) => n.present.includes(r) || existsSync(join(root, n.at, r))))
637
+ if (staged.length) inSub(n, ['add', '-A', '--', ...staged])
638
+ const tree = inSub(n, ['write-tree']).toString('utf8').trim()
639
+ const before = n.rev ? gitText(n.repo.top, ['rev-parse', `${n.rev}^{tree}`]).trim() : null
640
+ if (tree === before) return { at: n.at, repo: n.repo, roots: n.roots, commit: null, files: [] }
641
+ const files = n.rev ? zlist(inSub(n, ['diff', '--cached', '--name-only', '-z', '--no-renames', n.rev, '--', ...n.roots])) : zlist(inSub(n, ['ls-files', '-z']))
642
+ // ⭐ As the person who pulls — the repository's own identity, which `commit-tree` reads — with no
643
+ // hooks, and a message that names the files and no site: the folder may be shared by several.
644
+ const message = `Edits made in the app\n\n${files.slice(0, 40).map((p) => `- ${p}`).join('\n')}${files.length > 40 ? `\n- … and ${files.length - 40} more` : ''}\n`
645
+ const sha = gitText(n.repo.top, ['--git-dir', n.repo.gitDir, 'commit-tree', '--no-gpg-sign', tree, ...(n.parent ? ['-p', n.parent] : []), '-F', '-'], { input: message }).trim()
646
+ at(['update-index', '--add', '--cacheinfo', `160000,${sha},${n.at}`])
647
+ return { at: n.at, repo: n.repo, roots: n.roots, commit: sha, files }
648
+ })
530
649
  const staged = held(full.filter((f) => present.includes(f) || existsSync(join(root, f))))
531
650
  if (staged.length) at(['add', '-A', '--', ...staged])
532
651
  const tree = at(['write-tree']).toString('utf8').trim()
533
652
  const before = rev ? gitText(repo.top, ['rev-parse', `${rev}^{tree}`]).trim() : null
534
653
  const siteTree = siteTreeIn(repo, tree)
535
- if (tree === before && !always) return { commit: null, tree, siteTree }
654
+ if (tree === before && !always) return { commit: null, tree, siteTree, nested: made }
536
655
  const host = (() => {
537
656
  try {
538
657
  return new URL(origin).host
@@ -540,7 +659,14 @@ export function openStandIn(repo, { roots, rev }) {
540
659
  return 'backend'
541
660
  }
542
661
  })()
543
- const message = `${subject}\n\n${SYNC_TRAILER}: ${origin} ${siteUuid}\n${SITE_TRAILER}: ${repo.path}:${siteTree}\n`
662
+ // ⭐ And the tree of each root outside the site's folder, and the commit of each submodule there (`marks`).
663
+ const outsideSubs = subs.map((n) => n.at).filter((p) => repo.prefix && !`${p}/`.startsWith(repo.prefix))
664
+ const outside = [...outsideRoots(repo, roots), ...outsideSubs]
665
+ .map((p) => [p, objectAt(repo, `${tree}:${p}`)])
666
+ .filter(([, t]) => t)
667
+ .map(([p, t]) => `${ROOT_TRAILER}: ${p}:${t}\n`)
668
+ .join('')
669
+ const message = `${subject}\n\n${SYNC_TRAILER}: ${origin} ${siteUuid}\n${SITE_TRAILER}: ${repo.path}:${siteTree}\n${outside}`
544
670
  const who = {
545
671
  GIT_AUTHOR_NAME: 'uniweb',
546
672
  GIT_AUTHOR_EMAIL: `sync@${host}`,
@@ -551,7 +677,7 @@ export function openStandIn(repo, { roots, rev }) {
551
677
  env: who,
552
678
  input: message
553
679
  }).trim()
554
- return { commit: sha, tree, siteTree }
680
+ return { commit: sha, tree, siteTree, nested: made }
555
681
  }
556
682
 
557
683
  /**
@@ -561,15 +687,25 @@ export function openStandIn(repo, { roots, rev }) {
561
687
  * @returns {string[]} the paths placed, relative to the site
562
688
  */
563
689
  const placeIgnored = () => {
564
- const roots = full.filter((f) => existsSync(join(root, f)))
565
- if (!roots.length) return []
566
690
  const placed = []
567
- for (const p of zlist(at(['ls-files', '--others', '--ignored', '--exclude-standard', '-z', '--', ...roots]))) {
568
- const to = join(repo.top, p)
569
- if (occupied(to)) continue
691
+ const place = (from, to, named) => {
692
+ if (occupied(to)) return
570
693
  mkdirSync(dirname(to), { recursive: true })
571
- copyFileSync(join(root, p), to)
572
- placed.push(sitePrefix && p.startsWith(sitePrefix) ? p.slice(sitePrefix.length) : p)
694
+ copyFileSync(from, to)
695
+ placed.push(named)
696
+ }
697
+ const roots = full.filter((f) => existsSync(join(root, f)))
698
+ if (roots.length) {
699
+ for (const p of zlist(at(['ls-files', '--others', '--ignored', '--exclude-standard', '-z', '--', ...roots]))) {
700
+ place(join(root, p), join(repo.top, p), siteRelative(repo, p))
701
+ }
702
+ }
703
+ for (const n of subs) {
704
+ const own = n.roots.filter((r) => existsSync(join(root, n.at, r)))
705
+ if (!own.length) continue
706
+ for (const p of zlist(inSub(n, ['ls-files', '--others', '--ignored', '--exclude-standard', '-z', '--', ...own]))) {
707
+ place(join(root, n.at, p), join(n.repo.top, p), siteRelative(repo, posix.join(n.at, p)))
708
+ }
573
709
  }
574
710
  return placed
575
711
  }
@@ -582,28 +718,34 @@ export function openStandIn(repo, { roots, rev }) {
582
718
  * @returns {Array<[string, string]>}
583
719
  */
584
720
  const changes = () => {
585
- const roots = full.filter((f) => present.includes(f) || existsSync(join(root, f)))
586
- if (!roots.length) return []
587
- const site = (p) => (sitePrefix && p.startsWith(sitePrefix) ? p.slice(sitePrefix.length) : p)
721
+ const site = (p) => siteRelative(repo, p)
588
722
  const out = []
589
- const tracked = zlist(at(['diff', '--name-status', '-z', '--no-renames', '--', ...roots]))
590
- for (let i = 0; i + 1 < tracked.length; i += 2) out.push([tracked[i], site(tracked[i + 1])])
591
- for (const p of zlist(at(['ls-files', '--others', '--exclude-standard', '-z', '--', ...roots]))) out.push(['A', site(p)])
723
+ const listed = (run, roots, named) => {
724
+ if (!roots.length) return
725
+ const tracked = zlist(run(['diff', '--name-status', '-z', '--no-renames', '--', ...roots]))
726
+ for (let i = 0; i + 1 < tracked.length; i += 2) out.push([tracked[i], named(tracked[i + 1])])
727
+ for (const p of zlist(run(['ls-files', '--others', '--exclude-standard', '-z', '--', ...roots]))) out.push(['A', named(p)])
728
+ }
729
+ listed(at, full.filter((f) => present.includes(f) || existsSync(join(root, f))), site)
730
+ for (const n of subs) {
731
+ listed((a) => inSub(n, a), n.roots.filter((r) => n.present.includes(r) || existsSync(join(root, n.at, r))), (p) => site(posix.join(n.at, p)))
732
+ }
592
733
  return out.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0))
593
734
  }
594
735
 
595
736
  // The roots git can hold here. ⛔ One the repository ignores whole is not named to
596
737
  // `git add`, which refuses an ignored path; what is written under it is placed instead.
597
- const held = (paths) => {
738
+ const heldIn = (run, paths) => {
598
739
  if (!paths.length) return paths
599
740
  let ignored = []
600
741
  try {
601
- ignored = zlist(at(['check-ignore', '-z', '--stdin'], { input: `${paths.join('\0')}\0` }))
742
+ ignored = zlist(run(['check-ignore', '-z', '--stdin'], { input: `${paths.join('\0')}\0` }))
602
743
  } catch (err) {
603
744
  if (err?.status !== 1) throw err // 1: none of them is ignored
604
745
  }
605
746
  return paths.filter((p) => !ignored.includes(p))
606
747
  }
748
+ const held = (paths) => heldIn(at, paths)
607
749
 
608
750
  return { siteDir, commit, changes, placeIgnored, close }
609
751
  } catch (err) {
@@ -629,8 +771,8 @@ const occupied = (path) => {
629
771
  * Files not under git that the merge would write over: their paths, each with whether the
630
772
  * file already holds what `commit` has there.
631
773
  */
632
- function untrackedInTheWay(repo, commit) {
633
- const added = zlist(git(repo.top, ['diff', '--name-only', '-z', '--diff-filter=A', 'HEAD', commit, '--', ...(repo.prefix ? [repo.prefix] : [])]))
774
+ function untrackedInTheWay(repo, commit, roots = []) {
775
+ const added = zlist(git(repo.top, ['diff', '--name-only', '-z', '--diff-filter=A', 'HEAD', commit, '--', ...siteScope(repo, roots)]))
634
776
  const out = []
635
777
  for (const p of added) {
636
778
  const abs = join(repo.top, p)
@@ -652,22 +794,35 @@ function untrackedInTheWay(repo, commit) {
652
794
  return out
653
795
  }
654
796
 
797
+ /**
798
+ * What would stop a merge of `commit` here, before it is made — `{ ok: false, reason, paths? }` — or
799
+ * null. A pull asks it of every repository it merges into before it merges into any: one that cannot
800
+ * take its merge leaves every other as it was.
801
+ */
802
+ export function mergeStopper(repo, commit, { roots = [] } = {}) {
803
+ if (mergeInProgress(repo)) return { ok: false, reason: 'merge-in-progress' }
804
+ // Conflicts already here are not this merge's: git would refuse it, and they would read as its own.
805
+ const unsettled = unmergedPaths(repo)
806
+ if (unsettled.length) return { ok: false, reason: 'unmerged', paths: unsettled }
807
+ const blocking = untrackedInTheWay(repo, commit, roots).filter((u) => !u.same).map((u) => u.path)
808
+ if (blocking.length) return { ok: false, reason: 'untracked', paths: blocking }
809
+ return null
810
+ }
811
+
655
812
  /**
656
813
  * Merge a sync point into HEAD: git's three-way merge, against git's merge base — the sync
657
814
  * point's parent, which HEAD descends from. Uncommitted work is stashed and put back when
658
815
  * the merge is done (`--autostash`); a conflict leaves git's own state for a person to
659
816
  * settle, as any merge does.
660
817
  *
818
+ * `roots`, the site's roots, so what is in the way is looked for in each one outside its folder too.
819
+ *
661
820
  * @returns {{ ok: true, conflicted: string[] } | { ok: false, reason: string, paths?: string[] }}
662
821
  */
663
- export function mergeSyncPoint(repo, commit, { message }) {
664
- if (mergeInProgress(repo)) return { ok: false, reason: 'merge-in-progress' }
665
- // Conflicts already here are not this merge's: git would refuse it, and they would read as its own.
666
- const unsettled = unmergedPaths(repo)
667
- if (unsettled.length) return { ok: false, reason: 'unmerged', paths: unsettled }
668
- const inTheWay = untrackedInTheWay(repo, commit)
669
- const blocking = inTheWay.filter((u) => !u.same).map((u) => u.path)
670
- if (blocking.length) return { ok: false, reason: 'untracked', paths: blocking }
822
+ export function mergeSyncPoint(repo, commit, { message, roots = [] }) {
823
+ const stopped = mergeStopper(repo, commit, { roots })
824
+ if (stopped) return stopped
825
+ const inTheWay = untrackedInTheWay(repo, commit, roots)
671
826
  // One that already holds what the merge writes there is made way for — and only once the
672
827
  // merge is going ahead, or it would be gone with nothing put back.
673
828
  for (const u of inTheWay) rmSync(join(repo.top, u.path))
@@ -687,6 +842,62 @@ export function mergeSyncPoint(repo, commit, { message }) {
687
842
  }
688
843
  }
689
844
 
845
+ /**
846
+ * ⭐ RECORD EACH SUBMODULE'S MERGE AS THE SITE'S PIN, once both are merged. The site's sync point
847
+ * records the commit a pull made in the submodule; the checkout may hold a commit past it — the
848
+ * merge of that commit with the submodule's own — which is what the site's files now hold:
849
+ * - where the site's merge left the submodule in conflict, as git does when both sides moved its pin,
850
+ * the submodule's merge is the resolution, and the site's merge is made once nothing else conflicts;
851
+ * - where the site's merge was made, a checkout past the commit it records is committed, on its own.
852
+ * A submodule whose own merge left conflicts is left to the person, with the site's pin as it is.
853
+ *
854
+ * @param {object} repo - the site's `repoOf`
855
+ * @param {Array<{ at: string, repo: object, conflicted: string[] }>} merged
856
+ * @param {{ message: (at: string, head: string) => string }} opts
857
+ * @returns {{ pins: Array<{ at: string, head: string|null, committed: string|null, left: boolean }>, concluded: boolean, error: string|null }}
858
+ */
859
+ export function recordNestedPins(repo, merged, { message }) {
860
+ const pins = []
861
+ let error = null
862
+ const unmerged = new Set(unmergedPaths(repo))
863
+ for (const m of merged) {
864
+ const head = objectAt(m.repo, 'HEAD')
865
+ if (m.conflicted.length || !head) {
866
+ pins.push({ at: m.at, head, committed: null, left: true })
867
+ continue
868
+ }
869
+ if (unmerged.has(m.at)) {
870
+ try {
871
+ git(repo.top, ['add', '--', m.at])
872
+ } catch (err) {
873
+ error = gitError(err)
874
+ }
875
+ }
876
+ pins.push({ at: m.at, head, committed: null, left: false })
877
+ }
878
+ let concluded = false
879
+ if (!error && mergeInProgress(repo) && !unmergedPaths(repo).length && pins.every((p) => !p.left)) {
880
+ try {
881
+ git(repo.top, ['commit', '--no-edit', '--no-verify', '-q'])
882
+ concluded = true
883
+ } catch (err) {
884
+ error = gitError(err)
885
+ }
886
+ }
887
+ if (!error && !mergeInProgress(repo)) {
888
+ for (const p of pins.filter((x) => !x.left)) {
889
+ if (objectAt(repo, `HEAD:${p.at}`) === p.head) continue
890
+ try {
891
+ git(repo.top, ['commit', '-q', '--no-verify', '-m', message(p.at, p.head), '--', p.at])
892
+ p.committed = objectAt(repo, 'HEAD')
893
+ } catch (err) {
894
+ error = gitError(err)
895
+ }
896
+ }
897
+ }
898
+ return { pins, concluded, error }
899
+ }
900
+
690
901
  /**
691
902
  * Take a sync point's roots whole — `pull --force`. Local changes under them are dropped
692
903
  * and the branch gets a commit holding the sync point's content, with the sync point as a
@@ -696,11 +907,11 @@ export function mergeSyncPoint(repo, commit, { message }) {
696
907
  */
697
908
  export function takeSyncPoint(repo, commit, { roots, message }) {
698
909
  if (mergeInProgress(repo)) return { ok: false, reason: 'merge-in-progress' }
699
- const full = [...roots.map((r) => `${repo.prefix}${r}`)]
910
+ const full = roots.map((r) => underTop(repo, r))
700
911
  try {
701
912
  const tracked = zlist(git(repo.top, ['ls-tree', '--name-only', '-z', '--full-tree', 'HEAD', '--', ...full]))
702
913
  if (tracked.length) git(repo.top, ['checkout', 'HEAD', '--', ...tracked])
703
- for (const u of untrackedInTheWay(repo, commit)) rmSync(join(repo.top, u.path))
914
+ for (const u of untrackedInTheWay(repo, commit, roots)) rmSync(join(repo.top, u.path))
704
915
  const tree = gitText(repo.top, ['rev-parse', `${commit}^{tree}`]).trim()
705
916
  const merged = gitText(repo.top, ['commit-tree', tree, '-p', 'HEAD', '-p', commit, '-F', '-'], { input: `${message}\n` }).trim()
706
917
  git(repo.top, ['read-tree', '-m', '-u', 'HEAD', merged])
@@ -723,8 +934,9 @@ export function takeSyncPoint(repo, commit, { roots, message }) {
723
934
  * did a person's own edit when there was no base — and `--force` said it "took" a change
724
935
  * where it undid their edit.
725
936
  */
726
- export function changedInFiles(repo, { from, files, staged = false, skip = [] }) {
727
- const scope = ['--', ...(repo.prefix ? [repo.prefix] : [])]
937
+ export function changedInFiles(repo, { from, files, staged = false, skip = [], roots = [] }) {
938
+ // The site's folder, and each of its roots outside it: a mounted folder a pull writes into.
939
+ const scope = ['--', ...siteScope(repo, roots)]
728
940
  try {
729
941
  const parts = zlist(git(repo.top, ['diff', '--name-status', '-z', ...(staged ? ['--cached', from] : [from, 'HEAD']), ...scope]))
730
942
  const moved = []
@@ -739,7 +951,7 @@ export function changedInFiles(repo, { from, files, staged = false, skip = [] })
739
951
  )
740
952
  return moved
741
953
  .filter(([s, p]) => s !== 'U' && !skip.includes(p) && differs.has(p))
742
- .map(([s, p]) => [s, repo.prefix && p.startsWith(repo.prefix) ? p.slice(repo.prefix.length) : p])
954
+ .map(([s, p]) => [s, siteRelative(repo, p)])
743
955
  } catch {
744
956
  return []
745
957
  }
@@ -847,7 +1059,9 @@ export function commitRoots(repo, roots, { message, identity = null }) {
847
1059
  return { commit: gitText(repo.top, ['rev-parse', 'HEAD']).trim(), paths: staged.map((p) => siteRelative(repo, p)) }
848
1060
  }
849
1061
 
850
- const siteRelative = (repo, p) => (repo.prefix && p.startsWith(repo.prefix) ? p.slice(repo.prefix.length) : p)
1062
+ // A path relative to the site, as git names it from the repository's top: one outside the site's folder
1063
+ // comes out as `../docs/intro.md`.
1064
+ const siteRelative = (repo, p) => posix.relative(repo.prefix.replace(/\/$/, '') || '.', p) || '.'
851
1065
 
852
1066
  // A path relative to the site, as the repository names it. `../src/main.js` comes out as `src/main.js`.
853
1067
  const underTop = (repo, p) => posix.normalize(`${repo.prefix}${p}`)