UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 3.57 kB
import{o as e}from"./git-operation.worker-subsystem-DibpW_lB.mjs";import{_t as t,bt as n,rt as r}from"./git-operation.worker.js";import{d as i,s as a}from"./git-operation.worker-git-C2q2uQ5t.mjs";import{resolveCheckoutRootFromRealPath as o}from"./git-operation.worker-repository-paths-_OwpsyRw.mjs";import s from"node:fs/promises";const c=/* @__PURE__ */ new Map;async function listRepositoryBranchRefs(e,n,r){let i=await a(e,[`-c`,`core.warnAmbiguousRefs=true`,`for-each-ref`,...r===void 0?[]:[`--count=${r}`],`--sort=refname`,r===void 0?`--format=%(refname)%00%(refname:short)%00%(symref)%00%(HEAD)`:`--format=%(refname)%00%(refname:short)`,...n],{maxOutputBytes:262144,terminateOnOutputLimit:r===void 0}),o=t(`git for-each-ref`,i),s=[];for(let e of o.trim().split(` `)){let[t,n,r,i]=e.split(`\0`),a={symbolicRef:r||void 0,current:i===`*`};if(t&&n){if(t.startsWith(`refs/heads/`))s.push({ref:t,...a,branchName:t.slice(11),branch:{name:n,kind:`local`}});else if(t.startsWith(`refs/remotes/`)){let e=t.slice(13),r=e.indexOf(`/`),i=r>0?e.slice(r+1):``;s.push({ref:t,branchName:i,...a,branch:{name:n,kind:`remote`}})}}}return s}async function readRepositoryBranches(t,l={}){let u,d,f=r();try{if(d=await s.realpath(t).catch(()=>{throw Error(`repository does not exist: ${t}`)}),l.includeRepositoryStatus&&!(await s.stat(d)).isDirectory())return{branches:[],repositoryStatus:`unavailable`};let e=i(d);if(l.includeRepositoryStatus&&!e)return{branches:[],repositoryStatus:`not_git`};let n=e?c.get(e):void 0;if(e&&f!==void 0&&n?.requested===d&&f===n.revision&&Date.now()-n.refreshedAt<3e4)return c.delete(e),c.set(e,n),{...n.result,...l.includeRepositoryStatus?{repositoryStatus:`git`}:{}};u=(await o(d,t)).root}catch(e){if(l.includeRepositoryStatus)return e instanceof n&&e.reason===`unborn`?{branches:[],repositoryStatus:`not_git`}:{branches:[],repositoryStatus:`unavailable`};throw e}c.delete(u);let p;try{p=await listRepositoryBranchRefs(u,[`refs/heads/`,`refs/remotes/`])}catch{p=void 0}let m=/* @__PURE__ */ new Map,h=!1;for(let e of[`refs/remotes/`,`refs/heads/`])try{let t=p?p.filter(t=>t.ref.startsWith(e)).slice(0,100):await listRepositoryBranchRefs(u,[e],100);for(let e of t)e.branchName&&(e.branch.kind!==`remote`||e.branchName!==`HEAD`)&&m.set(e.branchName,e)}catch{h=!0}let g,_;if(p)g=p.find(e=>e.ref===`refs/remotes/origin/HEAD`)?.symbolicRef,_=p.find(e=>e.current)?.ref;else{let e=await a(u,[`symbolic-ref`,`--quiet`,`refs/remotes/origin/HEAD`]);g=e.code===0?e.stdout.trim():void 0;let t=await a(u,[`symbolic-ref`,`--quiet`,`HEAD`]);_=t.code===0?t.stdout.trim():void 0}let resolveBranch=async e=>{if(!e)return;let t=(p??[...m.values()]).find(t=>t.ref===e);if(t||p)return t?.branchName&&(t.branch.kind!==`remote`||t.branchName!==`HEAD`)?t:void 0;try{return(await listRepositoryBranchRefs(u,[e],1)).find(t=>t.ref===e&&t.branchName&&(t.branch.kind!==`remote`||t.branchName!==`HEAD`))}catch{h=!0;return}},v=await resolveBranch(g?.startsWith(`refs/remotes/origin/`)?`refs/heads/${g.slice(20)}`:void 0)??await resolveBranch(g),y=await resolveBranch(_);for(let e of[v,y])e&&m.set(e.branchName,e);let rank=e=>e.ref===v?.ref?0:e.ref===y?.ref?1:2,b={branches:[...m.values()].toSorted((e,t)=>rank(e)-rank(t)||e.branch.name.localeCompare(t.branch.name)).map(e=>e.branch),...v?{defaultBranch:v.branch.name}:{},...y?{headBranch:y.branch.name}:{},...h?{branchesUnavailable:!0}:{}};return!h&&f!==void 0&&(c.set(u,{revision:f,requested:d,refreshedAt:Date.now(),result:b}),e(c,64)),{...b,...l.includeRepositoryStatus?{repositoryStatus:`git`}:{}}}export{readRepositoryBranches};