UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

6 lines • 5.24 kB
import{t as hasNodeErrorCode}from"./path-guards-DGttsOJI.mjs";import{n as mergeProcessEnv}from"./process-env-BMQKxoSL.mjs";import{s as composeSessionSourceAssertion}from"./session-source-authority-Cun3s-rH.mjs";import{f as quoteGitConfig,l as gitNullConfigPath,m as requireGitCommandOutput,r as createGitCommandError}from"./git-exec-BRQo7oSy.mjs";import{c as requireGitPath,d as runGit,f as runGitBuffered,n as gitEnvironment,o as requireGit,p as runGitBytes}from"./git-2P0YMMKQ.mjs";import path from"node:path";import fs from"node:fs/promises";import os from"node:os"; //!src/agents/worktrees/checkout-git-config.ts const native={sourceOnly:false,run:runGit,require:requireGit,worker:{text:runGitBytes,buffered:runGitBuffered},withContentEnvironment:async e=>await e(gitEnvironment())};const sharedDirectories=[`objects`,`refs`,`logs`,`worktrees`,`reftable`];const sharedFiles=[`packed-refs`,`shallow`,`info/exclude`];function usesCanonicalMetadata(e){let t=0;while(e[t]===`-c`)t+=2;return[`rev-parse`,`show-ref`,`update-ref`,`branch`,`fetch`,`worktree`].includes(e[t]??``)&&!(e[t]===`worktree`&&e[t+1]===`remove`&&!e.includes(`--force`)&&!e.includes(`-f`))}async function withWorktreeGitConfig(e,t,n,r){if(!t)return await r(native);const check=()=>{n.signal?.throwIfAborted();n.beforeRun?.()};check();const i={...n,operation:`worktree.config`};const a=path.resolve(e,await requireGitPath(e,[`rev-parse`,`--git-common-dir`],i));const o=await requireGit(e,[`rev-parse`,`--show-object-format`],i);if(o!==`sha1`&&o!==`sha256`)throw new Error(`Unsupported Git object format`);const s=await runGit(e,[`config`,`--local`,`--get`,`extensions.refStorage`],i);if(s.code!==0&&s.code!==1)throw createGitCommandError(`git config refStorage`,s);const c=s.code===1?`files`:s.stdout.trim();if(c!==`files`&&c!==`reftable`)throw new Error(`Unsupported Git ref storage`);const l=await runGit(e,[`config`,`--includes`,`--null`,`--get-regexp`,`^core\\.(symlinks|ignorecase|precomposeunicode|excludesfile|autocrlf|eol|sparsecheckout|sparsecheckoutcone)$`],i);if(l.code!==0&&l.code!==1)throw createGitCommandError(`git config layout`,l);const u=/* @__PURE__ */ new Map([[`core.symlinks`,String(process.platform!==`win32`)]]);for(const e of l.stdout.split(`\0`).filter(Boolean)){const t=e.indexOf(` `);u.set(t<0?e:e.slice(0,t),t<0?`true`:e.slice(t+1))}check();const d=await fs.mkdtemp(path.join(os.tmpdir(),`openclaw-git-config-`));let f=true;const p=/* @__PURE__ */ new Set;const m=composeSessionSourceAssertion([n.beforeRun],e=>{n.signal?.throwIfAborted();e();if(!f)throw new Error(`Git configuration scope closed`)});const prepareMetadata=async e=>{for(const t of sharedDirectories){m();const n=path.join(a,t);if(process.platform===`win32`&&!await fs.stat(n).catch(()=>void 0))continue;await fs.symlink(n,path.join(e,t),process.platform===`win32`?`junction`:`dir`)}await fs.mkdir(path.join(e,`info`));for(const t of sharedFiles){m();const n=path.join(a,t);const r=path.join(e,t);if(process.platform===`win32`)await fs.copyFile(n,r).catch(e=>{if(!hasNodeErrorCode(e,`ENOENT`))throw e});else await fs.symlink(n,r,`file`)}};try{await prepareMetadata(d);await fs.writeFile(path.join(d,`config`),`[core]\nrepositoryformatversion=${o===`sha256`||c===`reftable`?1:0}\nbare=false\nfilemode=${process.platform!==`win32`}\n${[...u].map(([e,t])=>`${e.slice(5)}=${quoteGitConfig(t)}\n`).join(``)}[gc]\nauto=0\n[maintenance]\nauto=false\n[extensions]\n${o===`sha256`?`objectFormat=sha256 `:``}${c===`reftable`?`refStorage=reftable `:``}`);m();const optionsFor=async(e,t)=>{m();const r={...t,refMutationDirectory:a,stateOwnerPath:t.stateOwnerPath??n.stateOwnerPath,beforeRun:composeSessionSourceAssertion([m,t.beforeRun])};if(usesCanonicalMetadata(e))return r;let i=d;if(process.platform===`win32`){i=await fs.mkdtemp(path.join(d,`command-`));await prepareMetadata(i);await fs.copyFile(path.join(d,`config`),path.join(i,`config`))}const o=mergeProcessEnv([t.baseEnv??process.env,t.env]);const s=Object.fromEntries(Object.entries(o).filter(([e])=>!e.toUpperCase().startsWith(`GIT_`)));for(const[e,n]of Object.entries(t.env??{}))if(n!==void 0&&/^(?:GIT_INDEX_FILE|GIT_(?:AUTHOR|COMMITTER)_(?:NAME|EMAIL|DATE))$/u.test(e))s[e]=n;Object.assign(s,{GIT_COMMON_DIR:i,GIT_CONFIG_GLOBAL:gitNullConfigPath(),GIT_CONFIG_SYSTEM:gitNullConfigPath(),GIT_CONFIG_NOSYSTEM:`1`,GIT_ATTR_NOSYSTEM:`1`,GIT_NO_REPLACE_OBJECTS:`1`,GIT_NO_LAZY_FETCH:`1`});return{...r,baseEnv:s,env:s}};const track=e=>{m();const t=e();p.add(t);t.then(()=>p.delete(t),()=>p.delete(t));return t};const scopedRun=(e,t,n)=>track(async()=>await runGit(e,t,await optionsFor(t,{...n})));const scopedRequire=async(e,t,n={})=>{const r=await scopedRun(e,t,n);return requireGitCommandOutput(`git ${t.join(` `)}`,r).trim()};return await r({sourceOnly:true,withContentEnvironment:e=>track(async()=>{const t=await optionsFor([`pack-objects`],{env:{}});return await e(gitEnvironment(t.env))}),run:scopedRun,require:scopedRequire,worker:{text:(e,t,n={})=>track(async()=>await runGitBytes(e,t,await optionsFor(t,n))),buffered:(e,t,n)=>track(async()=>await runGitBuffered(e,t,await optionsFor(t,{...n})))}})}finally{f=false;await Promise.allSettled(p);await fs.rm(d,{recursive:true,force:true})}}export{withWorktreeGitConfig as t};