"use strict";(()=>{var e={};e.id=3286,e.ids=[3286],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")},14371:(e,r,t)=>{t.r(r),t.d(r,{originalPathname:()=>E,patchFetch:()=>v,requestAsyncStorage:()=>g,routeModule:()=>m,serverHooks:()=>_,staticGenerationAsyncStorage:()=>f});var o={};t.r(o),t.d(o,{PUT:()=>h});var n=t(49303),a=t(88716),s=t(60670),i=t(87070),l=t(97425),u=t(4709),p=t(67722),d=t(92048),c=t.n(d);async function h(e){try{let{repositoryPath:r}=await e.json(),t=(0,u.VZ)(r);if(!t.valid)return i.NextResponse.json({success:!1,error:t.error},{status:400});let o=t.resolvedPath,n=(0,l.n)();if(!(0,u.RT)(n,r))return i.NextResponse.json({success:!1,error:"Repository not found in exclusion list"},{status:404});if(!c().existsSync(o))return i.NextResponse.json({success:!0,worktreeCount:0,warning:"Repository path not found on disk. No worktrees were restored."});let a=await (0,p.e9)(o);return a.length>0&&(0,p.h2)(n,a),i.NextResponse.json({success:!0,worktreeCount:a.length,message:`Repository restored with ${a.length} worktree(s)`})}catch(e){return console.error("[Repository Restore] Error:",e),i.NextResponse.json({success:!1,error:"Failed to restore repository"},{status:500})}}let m=new n.AppRouteRouteModule({definition:{kind:a.x.APP_ROUTE,page:"/api/repositories/restore/route",pathname:"/api/repositories/restore",filename:"route",bundlePath:"app/api/repositories/restore/route"},resolvedPagePath:"/home/runner/work/CommandMate/CommandMate/src/app/api/repositories/restore/route.ts",nextConfigOutput:"",userland:o}),{requestAsyncStorage:g,staticGenerationAsyncStorage:f,serverHooks:_}=m,E="/api/repositories/restore/route";function v(){return(0,s.patchFetch)({serverHooks:_,staticGenerationAsyncStorage:f})}},4709:(e,r,t)=>{t.d(r,{Bj:()=>_,Bm:()=>v,L_:()=>R,RT:()=>E,VZ:()=>m,fC:()=>b,h4:()=>u,mm:()=>f,nJ:()=>g,nb:()=>p,sN:()=>y});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 u(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 p(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)||u(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");u(e,{name:a().basename(t),path:t,cloneSource:"local",isEnvManaged:!1,enabled:!1})}}function _(e){return e.prepare("SELECT * FROM repositories WHERE enabled = 0 ORDER BY name ASC").all().map(i)}function E(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 R(e,r){let t=e.prepare(`
    SELECT * FROM clone_jobs
    WHERE id = ?
  `).get(r);return t?l(t):null}function y(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$:()=>u,e9:()=>p,h2:()=>c});var o=t(61282),n=t(21764),a=t(55315),s=t.n(a),i=t(75748),l=t(98838);function u(){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 p(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 p(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(14371));module.exports=o})();