@feng0531/dsh-vcs 0.7.3 → 0.8.2
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/README.md +68 -5
- package/amend-host.mjs +48 -0
- package/branch-create-host.mjs +42 -0
- package/branch-create-preview.mjs +196 -0
- package/branch-delete-preview.mjs +214 -0
- package/branch-switch-host.mjs +13 -33
- package/commit-host.mjs +49 -0
- package/commit-object-publish.mjs +123 -0
- package/commit-preview.mjs +335 -0
- package/confirmed-operation.mjs +50 -0
- package/dist/client.js +25 -2
- package/dist/frontend.json +1 -1
- package/dist/interface.js +25 -2
- package/docs/RELEASING.md +2 -0
- package/git-blob-store.mjs +151 -0
- package/git-branch-create.mjs +156 -0
- package/git-branch-delete.mjs +175 -0
- package/git-lightweight-tag-create.mjs +181 -0
- package/git-object-store.mjs +162 -0
- package/git-ref-cas.mjs +119 -0
- package/git-tag-create.mjs +186 -0
- package/index-change-host.mjs +19 -0
- package/index-change.mjs +418 -0
- package/index-lock.mjs +231 -0
- package/index-stage-host.mjs +19 -0
- package/index.mjs +301 -4
- package/latest-commit.mjs +109 -0
- package/lightweight-tag-host.mjs +42 -0
- package/lightweight-tag-preview.mjs +183 -0
- package/line-index-host.mjs +58 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +47 -2
- package/partial-stage-host.mjs +52 -0
- package/partial-unstage-host.mjs +52 -0
- package/src/amend-plan.mjs +74 -0
- package/src/branch-delete-plan.mjs +113 -0
- package/src/commit-plan.mjs +78 -0
- package/src/git-commit-object.mjs +99 -0
- package/src/git-index.mjs +113 -0
- package/src/git-tag-object.mjs +110 -0
- package/src/index-tree.mjs +77 -0
- package/src/lightweight-tag-plan.mjs +64 -0
- package/src/line-index.mjs +168 -0
- package/src/partial-index.mjs +199 -0
- package/src/reflog.mjs +33 -0
- package/src/settled-reads.mjs +5 -0
- package/src/verified-branch-delete-plan.mjs +95 -0
- package/src/verified-commit-metadata.mjs +57 -0
- package/src/verified-commit-plan.mjs +49 -0
- package/src/verified-commit.mjs +84 -0
- package/src/verified-tag-plan.mjs +72 -0
- package/tag-create-host.mjs +31 -0
- package/tag-create-preview.mjs +215 -0
- package/tag-object-publish.mjs +49 -0
- package/vcs.mjs +29 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// Internal only: caller owns the repository mutation mutex and trusted callbacks.
|
|
2
|
+
// No RPC/UI/commit adapter. Hostile concurrent directory replacement is excluded:
|
|
3
|
+
// portable Node path/identity checks cannot provide dirfd-relative isolation.
|
|
4
|
+
// Packed refs, linked/shared metadata, reftable and alternates fail closed.
|
|
5
|
+
// No external global guarantee or hostile directory-swap/ABA isolation is claimed.
|
|
6
|
+
// On supported Git, zero-old --no-deref rejects even dangling symbolic targets;
|
|
7
|
+
// the focused real-Git characterization test guards that version-dependent fact.
|
|
8
|
+
import * as fs from 'node:fs/promises';
|
|
9
|
+
import {constants} from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import {parseVerifiedGitTag} from './src/git-tag-object.mjs';
|
|
13
|
+
import {parseVerifiedCommit} from './src/verified-commit.mjs';
|
|
14
|
+
import {git} from './local-command.mjs';
|
|
15
|
+
import {withOperationBudget,currentOperationBudget} from './src/operation-budget.mjs';
|
|
16
|
+
|
|
17
|
+
const fail=(kind,message,cause)=>Object.assign(new Error(message,{cause}),{code:'vcs/tag-create-'+kind});
|
|
18
|
+
const same=(a,b)=>a.dev===b.dev&&a.ino===b.ino&&a.birthtimeNs===b.birthtimeNs;
|
|
19
|
+
const plain=s=>s.isFile()&&!s.isSymbolicLink()&&s.nlink===1n;
|
|
20
|
+
const decode=b=>new TextDecoder('utf-8',{fatal:true,ignoreBOM:true}).decode(b);
|
|
21
|
+
function validRef(ref,prefix='refs/tags/'){
|
|
22
|
+
const name=typeof ref==='string'&&ref.startsWith(prefix)?ref.slice(prefix.length):'';
|
|
23
|
+
if(!name||Buffer.from(name,'utf8').toString('utf8')!==name||Buffer.byteLength(name)>1000||
|
|
24
|
+
name==='@'||name.toUpperCase()==='HEAD'||name.startsWith('-')||name.startsWith('refs/')||
|
|
25
|
+
/[\x00-\x20\x7f~^:?*\[\\<>"|\p{Cc}\p{Cf}]/u.test(name)||name.includes('..')||name.includes('@{')||
|
|
26
|
+
name.split('/').some(p=>!p||Buffer.byteLength(p)>255||p.startsWith('.')||/[. ]$/.test(p)||p.toLowerCase().endsWith('.lock')||/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(p)))
|
|
27
|
+
throw fail('invalid','An exact safe portable '+prefix+' name is required.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Create one absent local tag via Git update-ref --no-deref full-width zero-old CAS.
|
|
31
|
+
* Internal only: the caller MUST hold the repository writer mutex throughout.
|
|
32
|
+
* Existing born symbolic HEAD is pinned as a current fact, never a caller token.
|
|
33
|
+
* Returns {ref,tagOid,commit}; fixed Dsh audit attribution, not user authorship.
|
|
34
|
+
* Packed refs (even unrelated), linked/shared metadata and locks fail closed.
|
|
35
|
+
* Index bodies are not read: optional index metadata versions are pinned only.
|
|
36
|
+
* No checkout, HEAD/index/worktree/object mutation, signing, network or retry.
|
|
37
|
+
* All failures after synchronous onStart are outcome-unknown. Never roll back,
|
|
38
|
+
* remove a competing ref or Git lock, or retry on the caller's behalf.
|
|
39
|
+
* Trusted callbacks must not mutate filesystem state; onStart must be synchronous.
|
|
40
|
+
* Portable path checks do not isolate hostile concurrent directory replacement.
|
|
41
|
+
*/
|
|
42
|
+
export async function createLocalTag(repo,{ref,expectedTagOid,objectFormat,signal,assertAuthorized,onStart}={}){
|
|
43
|
+
const root=repo?.root;
|
|
44
|
+
validRef(ref);
|
|
45
|
+
const oidPattern=objectFormat==='sha1'?/^[0-9a-f]{40}$/:objectFormat==='sha256'?/^[0-9a-f]{64}$/:null;
|
|
46
|
+
if(repo?.type!=='git'||typeof root!=='string'||!path.isAbsolute(root)||path.resolve(root)!==root||
|
|
47
|
+
!oidPattern||typeof expectedTagOid!=='string'||!oidPattern.test(expectedTagOid)||/^0+$/.test(expectedTagOid)||
|
|
48
|
+
typeof assertAuthorized!=='function'||typeof onStart!=='function')throw fail('invalid','Canonical embedded Git root, a full tag OID and callbacks required.');
|
|
49
|
+
let started=false,hooks,owned,headText,headRef,headOid,indexVersion,tagBody,commitBody,tag,headLogs;
|
|
50
|
+
const directory=path.join(root,'.git'),identities=new Map();
|
|
51
|
+
const check=()=>{signal?.throwIfAborted();currentOperationBudget()?.throwIfExpired();};
|
|
52
|
+
const authorize=async()=>{check();await assertAuthorized();check();};
|
|
53
|
+
async function dir(p){
|
|
54
|
+
check();const s=await fs.lstat(p,{bigint:true});
|
|
55
|
+
if(!s.isDirectory()||s.isSymbolicLink()||await fs.realpath(p)!==p||(identities.has(p)&&!same(identities.get(p),s)))throw fail('unsafe','Noncanonical or replaced directory.');
|
|
56
|
+
identities.set(p,s);return s;
|
|
57
|
+
}
|
|
58
|
+
async function read(p,max=4096){
|
|
59
|
+
check();const before=await fs.lstat(p,{bigint:true});
|
|
60
|
+
if(!plain(before)||before.size>BigInt(max))throw fail('unsafe','Expected bounded single-link metadata.');
|
|
61
|
+
const h=await fs.open(p,constants.O_RDONLY|(constants.O_NOFOLLOW||0));
|
|
62
|
+
try{
|
|
63
|
+
const opened=await h.stat({bigint:true});
|
|
64
|
+
if(!plain(opened)||!same(before,opened))throw fail('unsafe','Metadata replaced.');
|
|
65
|
+
const b=Buffer.alloc(max+1);let n=0;
|
|
66
|
+
while(n<b.length){check();const r=await h.read(b,n,b.length-n,n);if(!r.bytesRead)break;n+=r.bytesRead;}
|
|
67
|
+
const after=await h.stat({bigint:true}),now=await fs.lstat(p,{bigint:true});
|
|
68
|
+
if(n>max||!plain(now)||!same(opened,now)||opened.size!==after.size||opened.mtimeNs!==after.mtimeNs||opened.ctimeNs!==after.ctimeNs||after.size!==now.size||after.mtimeNs!==now.mtimeNs||after.ctimeNs!==now.ctimeNs||BigInt(n)!==after.size)throw fail('unsafe','Metadata changed.');
|
|
69
|
+
return decode(b.subarray(0,n));
|
|
70
|
+
}finally{await h.close();}
|
|
71
|
+
}
|
|
72
|
+
async function metadata(){
|
|
73
|
+
await dir(root);let count=0;
|
|
74
|
+
const forbidden=new Set(['commondir','gitdir','worktrees','reftable','packed-refs','shallow','config.worktree','objects/info/alternates','objects/info/http-alternates','MERGE_HEAD','CHERRY_PICK_HEAD','REVERT_HEAD','rebase-merge','rebase-apply','sequencer'].map(s=>s.toLowerCase()));
|
|
75
|
+
async function visit(p,rel){
|
|
76
|
+
check();if(++count>20000)throw fail('unsupported','Metadata node bound exceeded.');
|
|
77
|
+
const lower=rel.toLowerCase();
|
|
78
|
+
if(forbidden.has(lower)||lower.endsWith('.lock')||lower.endsWith('.promisor')||lower.startsWith('sharedindex.'))throw fail('unsupported','Unsupported metadata or lock: '+rel);
|
|
79
|
+
const s=await fs.lstat(p,{bigint:true});
|
|
80
|
+
if(s.isSymbolicLink()||await fs.realpath(p)!==p)throw fail('unsafe','Linked metadata.');
|
|
81
|
+
if(s.isDirectory()){await dir(p);for(const name of await fs.readdir(p))await visit(path.join(p,name),rel?rel+'/'+name:name);}
|
|
82
|
+
else if(!plain(s))throw fail('unsafe','Shared or special metadata.');
|
|
83
|
+
}
|
|
84
|
+
await visit(directory,'');
|
|
85
|
+
}
|
|
86
|
+
const run=(args,max=16384)=>{check();return git(root,['-c','core.hooksPath='+hooks,'-c','user.name=Dsh','-c','user.email=dsh@localhost','-c','committer.name=Dsh','-c','committer.email=dsh@localhost','--git-dir='+directory,'--work-tree='+root,...args],max,{signal});};
|
|
87
|
+
async function config(){
|
|
88
|
+
// FIRST Git call: never expand include/includeIf (including global/system).
|
|
89
|
+
const rows=decode(await run(['config','--no-includes','--null','--list'],128*1024)).split('\0').filter(Boolean);
|
|
90
|
+
for(const row of rows){const i=row.indexOf('\n'),key=(i<0?row:row.slice(0,i)).toLowerCase(),value=i<0?'':row.slice(i+1).toLowerCase();
|
|
91
|
+
if(key.startsWith('include')||key.startsWith('extensions.')&&key!=='extensions.objectformat'||key==='core.worktree'||key==='core.repositoryformatversion'&&!['0','1'].includes(value)||key==='core.bare'&&value!=='false'||key==='core.sharedrepository'&&!['false','0'].includes(value)||/^remote\..*\.(promisor|partialclonefilter)$/.test(key))throw fail('unsupported','Unsupported configuration: '+key);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function indexStat(){
|
|
95
|
+
check();try{
|
|
96
|
+
const s=await fs.lstat(path.join(directory,'index'),{bigint:true});
|
|
97
|
+
if(!plain(s))throw fail('unsafe','Index must be single-link regular metadata.');
|
|
98
|
+
return s;
|
|
99
|
+
}catch(error){if(error.code==='ENOENT')return null;throw error;}
|
|
100
|
+
}
|
|
101
|
+
function sameVersion(a,b){return a===null?b===null:b!==null&&same(a,b)&&a.size===b.size&&a.mtimeNs===b.mtimeNs&&a.ctimeNs===b.ctimeNs;}
|
|
102
|
+
async function absentTarget(){
|
|
103
|
+
// Reject exact, prefix and portable case-fold collisions in both namespaces.
|
|
104
|
+
for(const parts of [ref.split('/'),['logs',...ref.split('/')]]){
|
|
105
|
+
let p=directory;
|
|
106
|
+
for(let i=0;i<parts.length;i++){
|
|
107
|
+
let names;try{names=await fs.readdir(p);}catch(e){if(e.code==='ENOENT')break;throw e;}
|
|
108
|
+
const name=parts[i],matches=names.filter(n=>n.toLowerCase()===name.toLowerCase());
|
|
109
|
+
if(matches.some(n=>n!==name))throw fail('exists','Case-colliding tag namespace.');
|
|
110
|
+
if(!matches.length)break;
|
|
111
|
+
p=path.join(p,name);
|
|
112
|
+
if(i===parts.length-1||(await fs.lstat(p)).isDirectory()===false)throw fail('exists','Target tag, reflog or namespace exists.');
|
|
113
|
+
await dir(p);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function optionalRead(p,max=2*1024*1024){
|
|
118
|
+
try{return await read(p,max);}catch(e){if(e.code==='ENOENT')return null;throw e;}
|
|
119
|
+
}
|
|
120
|
+
async function verifyObjects(){
|
|
121
|
+
// Fixed full OIDs + explicit type reads; do not trust cat-file -t alone.
|
|
122
|
+
const body=await run(['cat-file','tag',expectedTagOid],128*1024);
|
|
123
|
+
const parsed=parseVerifiedGitTag(body,{oid:expectedTagOid,objectFormat,signal});
|
|
124
|
+
if(parsed.tag!==ref.slice('refs/tags/'.length))throw fail('invalid','Tag body name differs from exact requested short name.');
|
|
125
|
+
const target=await run(['cat-file','commit',parsed.object],2*1024*1024);
|
|
126
|
+
parseVerifiedCommit(target,{oid:parsed.object,objectFormat,signal});
|
|
127
|
+
if(tagBody!==undefined&&(!tagBody.equals(body)||!commitBody.equals(target)))throw fail('stale','Original tag or target commit content changed.');
|
|
128
|
+
tagBody=body;commitBody=target;tag=parsed;
|
|
129
|
+
}
|
|
130
|
+
async function state(created=false){
|
|
131
|
+
await metadata();await config();
|
|
132
|
+
const current=await read(path.join(directory,'HEAD'));
|
|
133
|
+
if(headText===undefined){
|
|
134
|
+
if(!current.startsWith('ref: ')||!current.endsWith('\n'))throw fail('stale','Existing born symbolic HEAD required.');
|
|
135
|
+
headRef=current.slice(5,-1);validRef(headRef,'refs/heads/');headText=current;
|
|
136
|
+
headOid=(await read(path.join(directory,...headRef.split('/')))).replace(/\n$/,'');
|
|
137
|
+
if(!oidPattern.test(headOid)||/^0+$/.test(headOid))throw fail('stale','HEAD must name a born direct commit.');
|
|
138
|
+
}
|
|
139
|
+
if(current!==headText||await read(path.join(directory,...headRef.split('/')))!==headOid+'\n')throw fail('stale','Original HEAD changed.');
|
|
140
|
+
const logs=await Promise.all(['logs/HEAD','logs/'+headRef].map(p=>optionalRead(path.join(directory,...p.split('/')))));
|
|
141
|
+
if(headLogs===undefined)headLogs=logs;
|
|
142
|
+
else if(logs.some((v,i)=>v!==headLogs[i]))throw fail('stale','Original HEAD reflogs changed.');
|
|
143
|
+
if(created){
|
|
144
|
+
if(await read(path.join(directory,...ref.split('/')))!==expectedTagOid+'\n')throw fail('stale','Created tag is no longer the exact direct tag OID.');
|
|
145
|
+
const log=await optionalRead(path.join(directory,'logs',...ref.split('/')));
|
|
146
|
+
if(log!==null){
|
|
147
|
+
const zero='0'.repeat(expectedTagOid.length);
|
|
148
|
+
const pattern=new RegExp('^'+zero+' '+expectedTagOid+' Dsh <dsh@localhost> [0-9]+ [+-][0-9]{4}\\tdsh: create local tag\\n$');
|
|
149
|
+
if(!pattern.test(log))throw fail('stale','Unexpected created tag reflog.');
|
|
150
|
+
}
|
|
151
|
+
}else await absentTarget();
|
|
152
|
+
const facts=decode(await run(['rev-parse','--absolute-git-dir','--git-common-dir','--show-toplevel','--is-bare-repository','--show-object-format','--verify','HEAD'])).trimEnd().split('\n');
|
|
153
|
+
if(facts.length!==6||path.resolve(root,facts[0])!==directory||path.resolve(root,facts[1])!==directory||path.resolve(facts[2])!==root||facts[3]!=='false'||facts[4]!==objectFormat||facts[5]!==headOid)throw fail('stale','Git repository facts changed.');
|
|
154
|
+
const now=await indexStat();
|
|
155
|
+
if(indexVersion===undefined)indexVersion=now;
|
|
156
|
+
else if(!sameVersion(indexVersion,now))throw fail('stale','Index metadata version changed.');
|
|
157
|
+
}
|
|
158
|
+
async function verifyHooks(){
|
|
159
|
+
const s=await fs.lstat(hooks,{bigint:true});
|
|
160
|
+
if(!s.isDirectory()||s.isSymbolicLink()||!same(owned,s)||await fs.realpath(hooks)!==hooks||(await fs.readdir(hooks)).length)throw fail('unsafe','Owned hooks directory changed or is not empty.');
|
|
161
|
+
}
|
|
162
|
+
try{
|
|
163
|
+
return await withOperationBudget({signal},async()=>{
|
|
164
|
+
await authorize();await metadata();
|
|
165
|
+
const temp=await fs.realpath(os.tmpdir());
|
|
166
|
+
if(temp===root||temp.startsWith(root+path.sep))throw fail('unsafe','Hooks directory must be outside repository.');
|
|
167
|
+
hooks=await fs.mkdtemp(path.join(temp,'dsh-tag-create-hooks-'+process.pid+'-'));owned=await fs.lstat(hooks,{bigint:true});await verifyHooks();
|
|
168
|
+
await state();
|
|
169
|
+
await verifyObjects();
|
|
170
|
+
if(decode(await run(['cat-file','-t',headOid],1024))!=='commit\n')throw fail('invalid','Expected born branch must name a commit.');
|
|
171
|
+
await authorize();await state();await verifyHooks();await verifyObjects();check();
|
|
172
|
+
started=true;const result=onStart();
|
|
173
|
+
if(result&&typeof result.then==='function'){Promise.resolve(result).catch(()=>{});throw fail('invalid','onStart must be synchronous.');}
|
|
174
|
+
// No asynchronous work between the notification and mutation subprocess.
|
|
175
|
+
await run(['update-ref','--no-deref','-m','dsh: create local tag',ref,expectedTagOid,'0'.repeat(expectedTagOid.length)],4096);
|
|
176
|
+
await authorize();await state(true);await authorize();check();
|
|
177
|
+
return {ref,tagOid:expectedTagOid,commit:tag.object};
|
|
178
|
+
});
|
|
179
|
+
}catch(error){
|
|
180
|
+
if(started)throw Object.assign(fail('outcome-unknown','Tag update outcome is unknown; inspect before any further action.',error),{outcomeUnknown:true});
|
|
181
|
+
throw error;
|
|
182
|
+
}finally{
|
|
183
|
+
// Never recursively remove, unlink foreign entries, or delete a replaced path.
|
|
184
|
+
if(hooks&&owned)try{const s=await fs.lstat(hooks,{bigint:true});if(s.isDirectory()&&!s.isSymbolicLink()&&same(owned,s)&&await fs.realpath(hooks)===hooks&&(await fs.readdir(hooks)).length===0)await fs.rmdir(hooks);}catch{}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {createConfirmedOperationCoordinator} from './confirmed-operation.mjs';
|
|
2
|
+
|
|
3
|
+
// Internal host adapter, not an authorization layer: callers must supply the
|
|
4
|
+
// authenticated binding and assertAuthorized callback required by the coordinator.
|
|
5
|
+
// address.path is host-resolved. A future RPC MUST NOT expose arbitrary paths;
|
|
6
|
+
// it must resolve an authorized repository entry before calling this wrapper.
|
|
7
|
+
export function createIndexUnstageCoordinator({api,now=Date.now,ttlMs=60000,maxTokens=32}){
|
|
8
|
+
return createConfirmedOperationCoordinator({
|
|
9
|
+
operationKind:'index-unstage',now,ttlMs,maxTokens,
|
|
10
|
+
prepare:(repository,{address,signal})=>api.prepareIndexUnstage(repository,{path:address.path,signal}),
|
|
11
|
+
execute:(repository,request)=>api.applyIndexUnstage(repository,request),
|
|
12
|
+
// These are adapter-owned safe summaries, not the private fingerprint, raw
|
|
13
|
+
// index bytes or metadata digests. head may be an object or an oid.
|
|
14
|
+
project:snapshot=>({action:'unstage',path:snapshot.path,head:snapshot.head,index:snapshot.index,target:snapshot.target}),
|
|
15
|
+
invalidCode:'vcs/index-confirmation-invalid',
|
|
16
|
+
unknownCode:'vcs/index-outcome-unknown',
|
|
17
|
+
unknownMessage:'Unstaging started but its final result could not be confirmed. Refresh the repository and inspect the index before preparing another unstage.',
|
|
18
|
+
});
|
|
19
|
+
}
|