"use strict";(()=>{var e={};e.id=3546,e.ids=[3546],e.modules={85890:e=>{e.exports=require("better-sqlite3")},20399:e=>{e.exports=require("next/dist/compiled/next-server/app-page.runtime.prod.js")},30517:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},61282:e=>{e.exports=require("child_process")},84770:e=>{e.exports=require("crypto")},92048:e=>{e.exports=require("fs")},19801:e=>{e.exports=require("os")},55315:e=>{e.exports=require("path")},21764:e=>{e.exports=require("util")},98165:(e,r,t)=>{t.r(r),t.d(r,{originalPathname:()=>f,patchFetch:()=>E,requestAsyncStorage:()=>h,routeModule:()=>c,serverHooks:()=>g,staticGenerationAsyncStorage:()=>m});var o={};t.r(o),t.d(o,{POST:()=>d});var n=t(49303),a=t(88716),s=t(60670),i=t(87070),l=t(97425),p=t(67722),u=t(4709);async function d(){try{let e=(0,p.a$)();if(0===e.length)return i.NextResponse.json({error:"No repositories configured. Please set WORKTREE_REPOS or CM_ROOT_DIR environment variable."},{status:400});let r=(0,l.n)(),{filteredPaths:t}=(0,u.nJ)(r,e),o=await (0,p.Lj)(t);(0,p.h2)(r,o);let n=new Set(o.map(e=>e.repositoryPath));return i.NextResponse.json({success:!0,message:`Successfully synced ${o.length} worktree(s) from ${n.size} repository/repositories`,worktreeCount:o.length,repositoryCount:n.size,repositories:Array.from(n)},{status:200})}catch(r){console.error("Error syncing repositories:",r);let e=r instanceof Error?r.message:"Failed to sync repositories";return i.NextResponse.json({error:e},{status:500})}}let c=new n.AppRouteRouteModule({definition:{kind:a.x.APP_ROUTE,page:"/api/repositories/sync/route",pathname:"/api/repositories/sync",filename:"route",bundlePath:"app/api/repositories/sync/route"},resolvedPagePath:"/home/runner/work/CommandMate/CommandMate/src/app/api/repositories/sync/route.ts",nextConfigOutput:"",userland:o}),{requestAsyncStorage:h,staticGenerationAsyncStorage:m,serverHooks:g}=c,f="/api/repositories/sync/route";function E(){return(0,s.patchFetch)({serverHooks:g,staticGenerationAsyncStorage:m})}},4709:(e,r,t)=>{t.d(r,{Bj:()=>E,Bm:()=>v,L_:()=>y,RT:()=>_,VZ:()=>m,fC:()=>b,h4:()=>p,mm:()=>f,nJ:()=>g,nb:()=>u,sN:()=>R});var o=t(84770),n=t(55315),a=t.n(n),s=t(43753);function i(e){return{id:e.id,name:e.name,path:e.path,enabled:1===e.enabled,cloneUrl:e.clone_url||void 0,normalizedCloneUrl:e.normalized_clone_url||void 0,cloneSource:e.clone_source,isEnvManaged:1===e.is_env_managed,createdAt:new Date(e.created_at),updatedAt:new Date(e.updated_at)}}function l(e){return{id:e.id,cloneUrl:e.clone_url,normalizedCloneUrl:e.normalized_clone_url,targetPath:e.target_path,repositoryId:e.repository_id||void 0,status:e.status,pid:e.pid||void 0,progress:e.progress,errorCategory:e.error_category||void 0,errorCode:e.error_code||void 0,errorMessage:e.error_message||void 0,startedAt:e.started_at?new Date(e.started_at):void 0,completedAt:e.completed_at?new Date(e.completed_at):void 0,createdAt:new Date(e.created_at)}}function p(e,r){let t=(0,o.randomUUID)(),n=Date.now();return e.prepare(`
    INSERT INTO repositories (
      id, name, path, enabled, clone_url, normalized_clone_url,
      clone_source, is_env_managed, created_at, updated_at
    )
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  `).run(t,r.name,r.path,!1!==r.enabled?1:0,r.cloneUrl||null,r.normalizedCloneUrl||null,r.cloneSource,r.isEnvManaged?1:0,n,n),{id:t,name:r.name,path:r.path,enabled:!1!==r.enabled,cloneUrl:r.cloneUrl,normalizedCloneUrl:r.normalizedCloneUrl,cloneSource:r.cloneSource,isEnvManaged:r.isEnvManaged||!1,createdAt:new Date(n),updatedAt:new Date(n)}}function u(e,r){let t=e.prepare(`
    SELECT * FROM repositories
    WHERE normalized_clone_url = ?
  `).get(r);return t?i(t):null}function d(e,r){let t=e.prepare(`
    SELECT * FROM repositories
    WHERE path = ?
  `).get(r);return t?i(t):null}function c(e,r,t){let o=Date.now(),n=["updated_at = ?"],a=[o];void 0!==t.name&&(n.push("name = ?"),a.push(t.name)),void 0!==t.enabled&&(n.push("enabled = ?"),a.push(t.enabled?1:0)),void 0!==t.cloneUrl&&(n.push("clone_url = ?"),a.push(t.cloneUrl||null)),void 0!==t.normalizedCloneUrl&&(n.push("normalized_clone_url = ?"),a.push(t.normalizedCloneUrl||null)),a.push(r),e.prepare(`
    UPDATE repositories
    SET ${n.join(", ")}
    WHERE id = ?
  `).run(...a)}function h(e){return a().resolve(e)}function m(e){if(!e||"string"!=typeof e)return{valid:!1,error:"repositoryPath is required"};if(e.includes("\0"))return{valid:!1,error:"Invalid repository path"};let r=h(e);return(0,s.F)(r)?{valid:!1,error:"Invalid repository path"}:{valid:!0,resolvedPath:r}}function g(e,r){!function(e,r){for(let t of r){let r=h(t);d(e,r)||p(e,{name:a().basename(r),path:r,cloneSource:"local",isEnvManaged:!0,enabled:!0})}}(e,r);let t=function(e,r){let t=e.prepare("SELECT path FROM repositories WHERE enabled = 0").all().map(e=>e.path);return r.filter(e=>!t.includes(h(e)))}(e,r),o=r.filter(e=>!t.includes(e));return{filteredPaths:t,excludedPaths:o,excludedCount:o.length}}function f(e,r){let t=h(r),o=d(e,t);if(o)c(e,o.id,{enabled:!1});else{if(e.prepare("SELECT COUNT(*) as count FROM repositories WHERE enabled = 0").get().count>=1e3)throw Error("Disabled repository limit exceeded");p(e,{name:a().basename(t),path:t,cloneSource:"local",isEnvManaged:!1,enabled:!1})}}function E(e){return e.prepare("SELECT * FROM repositories WHERE enabled = 0 ORDER BY name ASC").all().map(i)}function _(e,r){let t=d(e,h(r));return t?(c(e,t.id,{enabled:!0}),{...t,enabled:!0}):null}function v(e,r){let t=(0,o.randomUUID)(),n=Date.now();return e.prepare(`
    INSERT INTO clone_jobs (
      id, clone_url, normalized_clone_url, target_path,
      status, progress, created_at
    )
    VALUES (?, ?, ?, ?, 'pending', 0, ?)
  `).run(t,r.cloneUrl,r.normalizedCloneUrl,r.targetPath,n),{id:t,cloneUrl:r.cloneUrl,normalizedCloneUrl:r.normalizedCloneUrl,targetPath:r.targetPath,status:"pending",progress:0,createdAt:new Date(n)}}function y(e,r){let t=e.prepare(`
    SELECT * FROM clone_jobs
    WHERE id = ?
  `).get(r);return t?l(t):null}function R(e,r,t){let o=[],n=[];void 0!==t.status&&(o.push("status = ?"),n.push(t.status)),void 0!==t.pid&&(o.push("pid = ?"),n.push(t.pid)),void 0!==t.progress&&(o.push("progress = ?"),n.push(t.progress)),void 0!==t.repositoryId&&(o.push("repository_id = ?"),n.push(t.repositoryId)),void 0!==t.errorCategory&&(o.push("error_category = ?"),n.push(t.errorCategory)),void 0!==t.errorCode&&(o.push("error_code = ?"),n.push(t.errorCode)),void 0!==t.errorMessage&&(o.push("error_message = ?"),n.push(t.errorMessage)),void 0!==t.startedAt&&(o.push("started_at = ?"),n.push(t.startedAt.getTime())),void 0!==t.completedAt&&(o.push("completed_at = ?"),n.push(t.completedAt.getTime())),0!==o.length&&(n.push(r),e.prepare(`
    UPDATE clone_jobs
    SET ${o.join(", ")}
    WHERE id = ?
  `).run(...n))}function b(e,r){let t=e.prepare(`
    SELECT * FROM clone_jobs
    WHERE normalized_clone_url = ?
      AND status IN ('pending', 'running')
    ORDER BY created_at DESC
    LIMIT 1
  `).get(r);return t?l(t):null}},67722:(e,r,t)=>{t.d(r,{Lj:()=>d,a$:()=>p,e9:()=>u,h2:()=>c});var o=t(61282),n=t(21764),a=t(55315),s=t.n(a),i=t(75748),l=t(98838);function p(){let e=process.env.WORKTREE_REPOS;if(e&&e.trim())return e.split(",").map(e=>e.trim()).filter(e=>e.length>0);let r=(0,l.Hb)("CM_ROOT_DIR");return r&&r.trim()?[r.trim()]:[]}async function u(e){let r=(0,n.promisify)(o.exec);try{let{stdout:t}=await r("git worktree list",{cwd:e}),o=function(e){if(!e||""===e.trim())return[];let r=e.trim().split("\n"),t=[];for(let e of r){let r=e.trim();if(!r)continue;let o=r.match(/^(.+?)\s+([a-z0-9]+)\s+(?:\[(.+?)\]|\(detached HEAD\))/);if(o){let[,e,r,n]=o;t.push({path:e.trim(),branch:n||`detached-${r}`,commit:r})}}return t}(t),n=s().resolve(e),a=s().basename(n);return o.map(e=>({id:function(e,r){if(!e)return"";let t=e=>e.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,""),o=t(e);if(r){let e=t(r);return`${e}-${o}`}return o}(e.branch,a),name:e.branch,path:s().resolve(e.path),repositoryPath:n,repositoryName:a})).filter(e=>["/etc","/root","/sys","/proc","/dev","/boot","/bin","/sbin","/usr/bin","/usr/sbin"].some(r=>e.path.startsWith(r))?(console.warn(`Skipping potentially unsafe worktree path: ${e.path}`),!1):!(e.path.includes("\0")||e.path.includes(".."))||(console.warn(`Skipping path with potentially malicious characters: ${e.path}`),!1))}catch(t){let e=t instanceof Error?t.message:String(t),r=t.code;if(e?.includes("not a git repository")||128===r)return[];throw t}}async function d(e){let r=[];for(let t of e)try{console.log(`Scanning repository: ${t}`);let e=await u(t);r.push(...e),console.log(`  Found ${e.length} worktree(s)`)}catch(e){console.error(`Error scanning repository ${t}:`,e)}return r}function c(e,r){if(0===r.length)return;let t=new Map;for(let e of r){let r=e.repositoryPath||"";t.has(r)||t.set(r,[]),t.get(r).push(e)}for(let[r,o]of t){if(!r)continue;let t=(0,i.Pv)(e,r),n=new Set(o.map(e=>e.id)),a=t.filter(e=>!n.has(e));if(a.length>0){let t=(0,i.pM)(e,a);console.log(`Removed ${t.deletedCount} deleted worktree(s) from ${r}`)}for(let r of o)(0,i.ly)(e,r)}}}};var r=require("../../../../webpack-runtime.js");r.C(e);var t=e=>r(r.s=e),o=r.X(0,[8948,5972,7425],()=>t(98165));module.exports=o})();