import{s as e}from"./chunk-Bj-mKKzh.js";import{Wn as t}from"./src-BOCweS1p.js";import{Oa as n}from"./app-server-manager-signals-Yn0WSnoY.js";import{g as r}from"./vscode-api-DjORcpSo.js";import{t as i}from"./parsePatchFiles-Dx-HvB-f.js";import{t as a}from"./sumBy-BCSe-07a.js";var o=1024*1024,s=new WeakMap;12*o,3*o;function c({fileCount:e,totalChangedLines:t,totalChangedBytes:n}){return e>128||t>9e3||n>12582912}function l(e){return e.additions+e.deletions>15e3||e.changedBytes>3145728||e.maxChangedLineBytes>1048576}function u(e){if(e.hunks.reduce((e,t)=>e+t.additionLines+t.deletionLines,0)>15e3)return!0;let{changedBytes:t,maxChangedLineBytes:n}=d(e);return t>3145728||n>1048576}function d(e){let t=s.get(e);if(t!=null)return t;let n=0,r=0;for(let t of e.hunks)for(let i of t.hunkContent){if(i.type===`context`)continue;let t=i.deletionLineIndex,a=t+i.deletions;for(let i=t;i<a;i+=1){let t=e.deletionLines[i]?.length??0;n+=t,r=Math.max(r,t)}let o=i.additionLineIndex,s=o+i.additions;for(let t=o;t<s;t+=1){let i=e.additionLines[t]?.length??0;n+=i,r=Math.max(r,i)}}let i={changedBytes:n,maxChangedLineBytes:r};return s.set(e,i),i}var f=e(a(),1),p=50,m=2e5,h=new Map;function g(e,{maxFiles:t}={}){let n=e.length<=m,a=n?`${t??`all`}`:``,o=n?h.get(e):void 0,s=o?.get(a);if(o&&(h.delete(e),h.set(e,o)),s)return s;let c=e.includes(`GIT binary patch`)?_(e):new Set,l=e.includes(`160000`)?v(e):new Set,u,d=b(e);try{u=i(d.diff)}catch(e){r.error(`Failed to parse diff`,{safe:{},sensitive:{error:e}}),u=[]}let g=[],S=0;for(let e of u)for(let n of e.files){if(t!==void 0&&g.length>=t)return g;try{let e=x(n,d.pathsByFileIndex.get(S)),{oldPath:t,newPath:r}=y(e),i=T(e),a=e.hunks,o=(0,f.default)(a,e=>e.additionLines),s=(0,f.default)(a,e=>e.deletionLines),u=a.find(e=>e.additionCount>0)?.additionStart,p=a.find(e=>e.deletionLines>0)?.deletionStart;g.push({metadata:e,oldPath:t,newPath:r,additions:o,deletions:s,get changedBytes(){return i().changedBytes},get maxChangedLineBytes(){return i().maxChangedLineBytes},firstAdditionLine:u,firstDeletionLine:p,isBinary:c.has(S),isGitlink:l.has(S)})}catch(e){r.error(`Failed to parse diff`,{safe:{},sensitive:{name:n.name,error:e}})}S+=1}if(n){let t=o??new Map;if(t.set(a,g),h.delete(e),h.set(e,t),h.size>p){let e=h.keys().next().value;e&&h.delete(e)}}return g}var _=e=>{let t=new Set,n=-1;for(let r of e.split(/\r?\n/)){if(r.startsWith(`diff --git `)){n+=1;continue}r.startsWith(`GIT binary patch`)&&n>=0&&t.add(n)}return t},v=e=>{let t=new Set,n=-1;for(let r of e.split(/\r?\n/)){if(r.startsWith(`diff --git `)){n+=1;continue}n>=0&&(/^(?:new file mode|deleted file mode|old mode|new mode) 160000$/.test(r)||/^index [0-9a-f]+\.\.[0-9a-f]+ 160000$/.test(r))&&t.add(n)}return t};function y(e){return{oldPath:e.type===`new`?n:e.prevName??e.name,newPath:e.type===`deleted`?n:e.name}}function b(e){let t=new Map,n=-1;return{diff:e.split(/(?<=\n)/).map(e=>{if(e.startsWith(`diff --git `)&&(n+=1),e.startsWith(`diff --git "`)){let r=S(e);return r==null?e:(t.set(n,r.paths),r.line)}return e.startsWith(`--- "`)?C(e,`---`)??e:e.startsWith(`+++ "`)?C(e,`+++`)??e:e}).join(``),pathsByFileIndex:t}}function x(e,t){return t==null?e:e.type===`rename-pure`||e.type===`rename-changed`?{...e,name:t.newPath,prevName:t.oldPath}:{...e,name:t.newPath}}function S(e){let n=w(e),r=e.slice(0,e.length-n.length).slice(11),i=t(r,0);if(i==null||r[i.nextIndex]!==` `)return null;let a=t(r,i.nextIndex+1);return a==null||a.nextIndex!==r.length||!i.path.startsWith(`a/`)||!a.path.startsWith(`b/`)?null:{line:`diff --git ${i.pathForUnquotedDiffHeader} ${a.pathForUnquotedDiffHeader}${n}`,paths:{oldPath:i.path.slice(2),newPath:a.path.slice(2)}}}function C(e,n){let r=w(e),i=t(e.slice(0,e.length-r.length),n.length+1);return i==null?null:`${n} ${i.pathForUnquotedDiffHeader}${r}`}function w(e){return e.endsWith(`\r
`)?`\r
`:e.endsWith(`
`)?`
`:``}function T(e){let t=null;return()=>(t??=d(e),t)}export{c as i,l as n,u as r,g as t};
//# sourceMappingURL=parse-diff-_HS_rvxc.js.map