UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

2 lines • 3.23 kB
import{a as e}from"./git-operation.worker-errors-BicYkIHf.mjs";import{c as t,f as n}from"./git-operation.worker-git-TpQcAIgG.mjs";import{nn as r}from"./git-operation.worker.js";import{i,u as a}from"./git-operation.worker-git-root-DGrzrh_L.mjs";import{resolveCheckoutRootFromRealPath as o}from"./git-operation.worker-repository-paths-gKPnkAjC.mjs";import s from"node:fs/promises";import c from"node:path";async function listRepositoryBranchRefs(e,n,i){let a=await t(e,[`--no-optional-locks`,`-c`,`core.warnAmbiguousRefs=true`,`for-each-ref`,`--count=${i}`,`--sort=refname`,`--format=%(refname)%00%(refname:short)`,...n],{timeoutMs:1500,maxOutputBytes:262144}),o=r(`git for-each-ref`,a),s=[];for(let e of o.trim().split(` `)){let[t,n]=e.split(`\0`);if(t&&n){if(t.startsWith(`refs/heads/`))s.push({ref:t,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,branch:{name:n,kind:`remote`}})}}}return s}async function readRepositoryBranches(r,i={}){let a,c;try{if(c=await s.realpath(r).catch(()=>{throw Error(`repository does not exist: ${r}`)}),i.includeRepositoryStatus&&!(await s.stat(c)).isDirectory())return{branches:[],repositoryStatus:`unavailable`};let e=await n(c);if(i.includeRepositoryStatus&&!e)return{branches:[],repositoryStatus:`not_git`};a=(await o(c,r)).root}catch(t){if(i.includeRepositoryStatus)return t instanceof e&&t.reason===`unborn`?{branches:[],repositoryStatus:`not_git`}:{branches:[],repositoryStatus:`unavailable`};throw t}let l=/* @__PURE__ */ new Map,u=!1;for(let e of[`refs/remotes/`,`refs/heads/`])try{let t=await listRepositoryBranchRefs(a,[e],100);for(let e of t)e.branchName&&(e.branch.kind!==`remote`||e.branchName!==`HEAD`)&&l.set(e.branchName,e)}catch{u=!0}let d=await t(a,[`symbolic-ref`,`--quiet`,`refs/remotes/origin/HEAD`],{timeoutMs:1500}),f=d.code===0?d.stdout.trim():void 0,p=await t(a,[`symbolic-ref`,`--quiet`,`HEAD`],{timeoutMs:1500}),m=p.code===0?p.stdout.trim():void 0;u||=d.termination!==`exit`||p.termination!==`exit`;let resolveBranch=async e=>{if(!e)return;let t=[...l.values()].find(t=>t.ref===e);if(t)return t?.branchName&&(t.branch.kind!==`remote`||t.branchName!==`HEAD`)?t:void 0;try{return(await listRepositoryBranchRefs(a,[e],1)).find(t=>t.ref===e&&t.branchName&&(t.branch.kind!==`remote`||t.branchName!==`HEAD`))}catch{u=!0;return}},h=await resolveBranch(f?.startsWith(`refs/remotes/origin/`)?`refs/heads/${f.slice(20)}`:void 0)??await resolveBranch(f),g=m===h?.ref?h:await resolveBranch(m);for(let e of[h,g])e&&l.set(e.branchName,e);let rank=e=>e.ref===h?.ref?0:e.ref===g?.ref?1:2;return{branches:[...l.values()].toSorted((e,t)=>rank(e)-rank(t)||e.branch.name.localeCompare(t.branch.name)).map(e=>e.branch),...h?{defaultBranch:h.branch.name}:{},...g?{headBranch:g.branch.name}:{},...u?{branchesUnavailable:!0}:{},...i.includeRepositoryStatus?{repositoryStatus:`git`}:{}}}async function readRepositoryBranchSnapshot(e,t={}){let r=await readRepositoryBranches(e,t);try{let t=i(e);if(t){let i=c.dirname(t.headPath),o=await n(e)??e;return{...r,observation:{root:o,gitDir:i,commonDir:a(t.headPath)}}}}catch{}return r}export{readRepositoryBranchSnapshot};