@@ -61,6 +61,7 @@ import {
6161 sameRepositoryRecord ,
6262 sameWorktreeRecord ,
6363 worktreeRecordJson ,
64+ type GitObjectFormat ,
6465 type RepositoryCreationRequest ,
6566 type RepositoryRecord ,
6667 type WorktreeCreationRequest ,
@@ -80,6 +81,10 @@ import {
8081 checkoutPrimary ,
8182 checkoutReadable ,
8283 clone ,
84+ commitPresent ,
85+ commonDirectory ,
86+ originLocator ,
87+ readObjectFormat ,
8388 GitRefusal ,
8489 gitSession ,
8590 objectFormat ,
@@ -592,7 +597,11 @@ type StaleReason =
592597 | "administration"
593598 | "repository"
594599 | "placement"
595- | "locator" ;
600+ | "locator"
601+ | "origin"
602+ | "object-format"
603+ | "creation-commit"
604+ | "linkage" ;
596605
597606const STALE_SENTENCES : ReadonlyMap < StaleReason , string > = new Map ( [
598607 [ "metadata" , "the retained record naming it is gone or names something else" ] ,
@@ -601,6 +610,10 @@ const STALE_SENTENCES: ReadonlyMap<StaleReason, string> = new Map([
601610 [ "repository" , "the repository it belongs to is no longer retained" ] ,
602611 [ "placement" , "the path it recorded is not the one this run's placement gives its identity" ] ,
603612 [ "locator" , "the url retained for it is not the one its identity names" ] ,
613+ [ "origin" , "the checkout it holds came from a different repository" ] ,
614+ [ "object-format" , "the checkout it holds names its objects with a different algorithm" ] ,
615+ [ "creation-commit" , "the commit it was created at is not in the checkout it holds" ] ,
616+ [ "linkage" , "the checkout it holds is not a worktree of the repository it belongs to" ] ,
604617] ) ;
605618
606619function stale ( subject : string , reason : StaleReason ) : RepositoryStaleStateError {
@@ -676,12 +689,27 @@ function worktreeSubject(name: string): string {
676689 return `worktree ${ JSON . stringify ( name ) } ` ;
677690}
678691
692+ /**
693+ * A materialized checkout, and what it has to turn out to be.
694+ *
695+ * `repositoryDirectory` is where the Repository this checkout belongs to was
696+ * materialized — for a primary checkout, itself — and `repository` is that
697+ * Repository's retained record. Both travel with the directory because
698+ * verification is a comparison, and neither half of it can be read off the host
699+ * tree alone.
700+ */
701+ interface Attached {
702+ readonly directory : string ;
703+ readonly repositoryDirectory : string ;
704+ readonly repository : RepositoryRecord ;
705+ }
706+
679707function * prepareRepositoryAttachment (
680708 workspace : PrivateWorkspaceTransaction ,
681709 root : string ,
682710 record : RepositoryRecord ,
683711 subject : string ,
684- ) : Operation < string > {
712+ ) : Operation < Attached > {
685713 const stored = workspace . metadata . readRepository ( record . name ) ;
686714 if ( stored === undefined || ! sameRepositoryRecord ( stored . record , record ) ) {
687715 throw stale ( subject , "metadata" ) ;
@@ -695,15 +723,15 @@ function* prepareRepositoryAttachment(
695723 }
696724 const directory = yield * exportTree ( workspace . filesystem , root , checkoutPath ) ;
697725 localizeAdministration ( root , directory , [ ] ) ;
698- return directory ;
726+ return { directory, repositoryDirectory : directory , repository : record } ;
699727}
700728
701729function * prepareWorktreeAttachment (
702730 workspace : PrivateWorkspaceTransaction ,
703731 root : string ,
704732 record : WorktreeRecord ,
705733 subject : string ,
706- ) : Operation < string > {
734+ ) : Operation < Attached > {
707735 const stored = workspace . metadata . readWorktree ( record . repositoryName , record . name ) ;
708736 if ( stored === undefined || ! sameWorktreeRecord ( stored , record ) ) {
709737 throw stale ( subject , "metadata" ) ;
@@ -727,20 +755,103 @@ function* prepareWorktreeAttachment(
727755 const repositoryDirectory = yield * exportTree ( workspace . filesystem , root , repositoryPath ) ;
728756 const directory = yield * exportTree ( workspace . filesystem , root , checkoutPath ) ;
729757 localizeAdministration ( root , repositoryDirectory , [ directory ] ) ;
730- return directory ;
758+ return { directory, repositoryDirectory, repository : repository . record } ;
759+ }
760+
761+ /**
762+ * What the checkout at a retained path has to turn out to be, or what disagrees.
763+ *
764+ * Readable is where this used to stop, and readable is a property of *any*
765+ * repository. A clone of an unrelated repository written over the retained path
766+ * is perfectly readable, so attachment that asked only that question accepted
767+ * one identity's history against another identity's bytes — and the document
768+ * carried on, restoring recorded reads that no longer describe what is there
769+ * while every later live effect ran against the substitute.
770+ *
771+ * So the three things the record actually claims are read back out of the
772+ * checkout: where it came from, how it names objects, and whether the commit it
773+ * was created at is in it. Each is creation identity, and creation identity does
774+ * not move. What is deliberately *not* asked is where HEAD or the current branch
775+ * is: #294 moves those transactionally, and requiring them to equal creation
776+ * state would make an ordinary commit look like corruption.
777+ *
778+ * The origin is compared by fingerprint rather than by bytes. The fingerprint is
779+ * what every other comparison in this provider uses, and it is the one form of
780+ * the locator that is safe to hold beside a diagnostic.
781+ */
782+ function * repositoryDisagreement (
783+ git : GitSession ,
784+ attached : Attached ,
785+ objectFormat : GitObjectFormat ,
786+ creationCommit : string ,
787+ ) : Operation < StaleReason | undefined > {
788+ if ( ( yield * checkoutReadable ( git , attached . directory ) ) === undefined ) {
789+ return "administration" ;
790+ }
791+
792+ const origin = yield * originLocator ( git , attached . repositoryDirectory ) ;
793+ if (
794+ origin === undefined ||
795+ locatorFingerprint ( origin ) !== attached . repository . locatorFingerprint
796+ ) {
797+ return "origin" ;
798+ }
799+ if ( ( yield * readObjectFormat ( git , attached . repositoryDirectory ) ) !== objectFormat ) {
800+ return "object-format" ;
801+ }
802+ if ( ! ( yield * commitPresent ( git , attached . directory , creationCommit ) ) ) {
803+ return "creation-commit" ;
804+ }
805+ return undefined ;
806+ }
807+
808+ /**
809+ * The same questions for a linked worktree, plus the one only it raises.
810+ *
811+ * A worktree's identity is its Repository's identity and its own place inside
812+ * it, so the origin and the object format are read through the shared
813+ * administration — and *that* is the thing worth proving: the checkout retained
814+ * for this worktree has to be a worktree of the Repository this run says it
815+ * belongs to. Git answers that directly, because `--git-common-dir` resolves
816+ * through the administration pair to the repository the worktree is linked to.
817+ * A worktree of some other repository, or a whole repository of its own sitting
818+ * at the retained path, answers with a different directory.
819+ */
820+ function * worktreeDisagreement (
821+ git : GitSession ,
822+ attached : Attached ,
823+ record : WorktreeRecord ,
824+ ) : Operation < StaleReason | undefined > {
825+ if ( ( yield * checkoutReadable ( git , attached . directory ) ) === undefined ) {
826+ return "administration" ;
827+ }
828+ if (
829+ ( yield * commonDirectory ( git , attached . directory ) ) !== `${ attached . repositoryDirectory } /.git`
830+ ) {
831+ return "linkage" ;
832+ }
833+ return yield * repositoryDisagreement (
834+ git ,
835+ attached ,
836+ attached . repository . objectFormat ,
837+ record . creationCommit ,
838+ ) ;
731839}
732840
733841/**
734- * Rebuild a live checkout from the Workspace and prove Git can still read it .
842+ * Rebuild a live checkout from the Workspace and prove it is the retained one .
735843 *
736844 * The transaction is held only for the export: Git runs afterwards, against
737- * files, so a subprocess never keeps the run's database locked.
845+ * files, so a subprocess never keeps the run's database locked. Nothing here
846+ * writes, clones or repairs — a disagreement is reported and the Workspace is
847+ * left exactly as it was found.
738848 */
739849function * attach (
740850 database : WorkflowRunDatabase ,
741851 host : RepositoryHost ,
742852 subject : string ,
743- prepare : ( workspace : PrivateWorkspaceTransaction , root : string ) => Operation < string > ,
853+ prepare : ( workspace : PrivateWorkspaceTransaction , root : string ) => Operation < Attached > ,
854+ disagreement : ( git : GitSession , attached : Attached ) => Operation < StaleReason | undefined > ,
744855) : Operation < void > {
745856 const root = yield * host . useDirectory ( ) ;
746857 const prepared = yield * transactWorkspaceRoots ( database , ( workspace ) => prepare ( workspace , root ) ) ;
@@ -749,8 +860,9 @@ function* attach(
749860 }
750861
751862 const git : GitSession = gitSession ( host , root ) ;
752- if ( ( yield * checkoutReadable ( git , prepared . value ) ) === undefined ) {
753- throw stale ( subject , "administration" ) ;
863+ const found = yield * disagreement ( git , prepared . value ) ;
864+ if ( found !== undefined ) {
865+ throw stale ( subject , found ) ;
754866 }
755867}
756868
@@ -802,6 +914,8 @@ export function useRepositoryComposition(
802914 record ,
803915 `repository ${ JSON . stringify ( record . name ) } ` ,
804916 ) ,
917+ ( git , attached ) =>
918+ repositoryDisagreement ( git , attached , record . objectFormat , record . creationCommit ) ,
805919 ) ;
806920 } ,
807921
@@ -834,6 +948,7 @@ export function useRepositoryComposition(
834948 record ,
835949 `worktree ${ JSON . stringify ( record . name ) } ` ,
836950 ) ,
951+ ( git , attached ) => worktreeDisagreement ( git , attached , record ) ,
837952 ) ;
838953 } ,
839954 } ,
0 commit comments