UNPKG

@supos-ce-platform/git-tools

Version:

Supos ce platform Git tools, supos, ce, platform, git tools

13 lines 10.2 kB
import M from'simple-git';import $ from'node:fs';import R from'node:path';import B from'node:os';var d=class{constructor(){this.configPath=R.join(B.homedir(),".supos-git-config.json"),this.config=this.loadConfig();}loadConfig(){try{if($.existsSync(this.configPath)){let e=$.readFileSync(this.configPath,"utf-8");return JSON.parse(e)}}catch(e){console.error("\u52A0\u8F7D\u914D\u7F6E\u6587\u4EF6\u5931\u8D25:",e);}return {repos:[]}}saveConfig(){try{$.writeFileSync(this.configPath,JSON.stringify(this.config,null,2));}catch(e){throw new Error(`\u4FDD\u5B58\u914D\u7F6E\u6587\u4EF6\u5931\u8D25: ${e.message}`)}}addRepo(e){let i=this.config.repos.findIndex(t=>t.name===e.name);i>=0?this.config.repos[i]=e:this.config.repos.push(e),this.saveConfig();}getRepo(e){return this.config.repos.find(i=>i.name===e)}removeRepo(e){this.config.repos=this.config.repos.filter(i=>i.name!==e),this.saveConfig();}listRepos(){return this.config.repos}saveRemoteMergeHistory(e){this.config.remoteMergeHistory=e,this.saveConfig();}getRemoteMergeHistory(){return this.config.remoteMergeHistory}};var v=class{constructor(){this.configManager=new d,this.tempDir=R.join(B.tmpdir(),"supos-git-merge"),$.existsSync(this.tempDir)||$.mkdirSync(this.tempDir,{recursive:true});}async initRepo(e){let i=R.join(this.tempDir,e.name);$.existsSync(i)||$.mkdirSync(i,{recursive:true});let t=M(i);try{$.existsSync(R.join(i,".git"))||await t.init();let s=e.url;try{let r=new URL(e.url);r.username="",r.password="",e.token?s=r.toString().replace(/^(https?:\/\/)(.+)$/,`$1${encodeURIComponent(e.token)}@$2`):e.username&&e.password&&(s=r.toString().replace(/^(https?:\/\/)(.+)$/,`$1${encodeURIComponent(e.username)}:${encodeURIComponent(e.password)}@$2`));}catch{console.warn(`[${e.name}] URL\u89E3\u6790\u5931\u8D25\uFF0C\u4F7F\u7528\u539F\u59CBURL`);}try{(await t.remote(["get-url","origin"]))?.trim()!==s.trim()&&await t.remote(["set-url","origin",s]);}catch{await t.remote(["add","origin",s]);}return await t.fetch(["--all","--prune","--tags","--force"]),t}catch(s){let r=s.message||"\u672A\u77E5\u9519\u8BEF",g=s.git?` Git\u9519\u8BEF: ${JSON.stringify(s.git,null,2)}`:"";throw new Error(`[${e.name}] Git\u64CD\u4F5C\u5931\u8D25: ${r}${g}`)}}async mergeCrossBranch(e,i,t,s){let r=this.configManager.getRepo(e),g=this.configManager.getRepo(t);if(!r||!g)throw new Error("\u6E90\u4ED3\u5E93\u6216\u76EE\u6807\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let l=await this.initRepo(r),n=await this.initRepo(g);try{if(await l.fetch(["--all","--prune","--tags","--force"]),!await this.checkRemoteBranchExists(e,i,l))throw new Error(`\u6E90\u5206\u652F '${i}' \u5728\u4ED3\u5E93 '${e}' \u4E2D\u4E0D\u5B58\u5728`);let o=i.replace(/^(origin\/|remotes\/origin\/|source_[^/]+\/)/,"");try{await l.checkout(o);}catch{try{await l.checkout(["-b",o,"--track",`origin/${o}`]);}catch(p){console.error(`[${e}] \u521B\u5EFA\u8DDF\u8E2A\u5206\u652F\u5931\u8D25:`,p),await l.fetch(["origin",o,"--update-head-ok"]),await l.checkout(["-b",o]);}}try{await l.pull("origin",o);}catch(a){console.warn(`[${e}] \u62C9\u53D6\u66F4\u65B0\u5931\u8D25\uFF0C\u7EE7\u7EED\u4F7F\u7528\u5F53\u524D\u5185\u5BB9:`,a);}let h=`source_${e}`;try{await n.remote(["remove",h]);}catch{}let m=new URL(r.url);r.token?(m.username=encodeURIComponent(r.token),m.password=""):r.username&&r.password&&(m.username=encodeURIComponent(r.username),m.password=encodeURIComponent(r.password)),await n.remote(["add",h,m.toString()]),await n.fetch([h,"--force"]),await n.fetch(["--all","--prune","--tags","--force"]);let u=await n.branch(["--all"]),f=s.replace(/^origin\//,""),w=u.all.some(a=>a.trim().replace(/^remotes\/origin\//,"").replace(/^origin\//,"")===f);if(!w){if(w=(await n.listRemote(["--heads"])).includes(`refs/heads/${f}`),!w)throw new Error(`\u76EE\u6807\u5206\u652F '${s}' \u5728\u4ED3\u5E93 '${t}' \u4E2D\u4E0D\u5B58\u5728 \u53EF\u7528\u7684\u8FDC\u7A0B\u5206\u652F: ${u.all.join(` `)}`);await n.fetch(["origin",f]);}try{await n.checkout(f);}catch{try{await n.checkout(["-b",f,"--track",`origin/${f}`]);}catch(p){console.error(`[${t}] \u521B\u5EFA\u8DDF\u8E2A\u5206\u652F\u5931\u8D25:`,p),await n.fetch(["origin",f,"--update-head-ok"]),await n.checkout(["-b",f]);}}try{await n.pull("origin",f);}catch(a){console.warn(`[${t}] \u62C9\u53D6\u66F4\u65B0\u5931\u8D25\uFF0C\u7EE7\u7EED\u4F7F\u7528\u5F53\u524D\u5185\u5BB9:`,a);}try{await n.merge([`${h}/${o}`]),await n.push("origin",f);}catch(a){throw console.error(`[${t}] \u5408\u5E76\u5931\u8D25:`,a.message),new Error(`\u5206\u652F\u5408\u5E76\u5931\u8D25: ${a.message}`)}await n.remote(["remove",h]);}catch(c){let o=c.message||"\u672A\u77E5\u9519\u8BEF",h=c.git?` Git\u9519\u8BEF: ${JSON.stringify(c.git,null,2)}`:"";throw new Error(`\u8DE8\u4ED3\u5E93\u5408\u5E76\u5931\u8D25: ${o}${h}`)}}async abortMerge(e){let i=this.configManager.getRepo(e);if(!i)throw new Error("\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let t=await this.initRepo(i);try{await t.merge(["--abort"]);}catch(s){let r=s.message||"\u672A\u77E5\u9519\u8BEF",g=s.git?` Git\u9519\u8BEF: ${JSON.stringify(s.git,null,2)}`:"";throw new Error(`\u4E2D\u6B62\u5408\u5E76\u5931\u8D25: ${r}${g}`)}}async resolveConflicts(e,i){let t=this.configManager.getRepo(e);if(!t)throw new Error("\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let s=await this.initRepo(t);try{i==="ours"?await s.checkout(["--ours","."]):await s.checkout(["--theirs","."]),await s.add("."),console.log(`[${e}] \u51B2\u7A81\u5DF2\u89E3\u51B3`);}catch(r){let g=r.message||"\u672A\u77E5\u9519\u8BEF",l=r.git?` Git\u9519\u8BEF: ${JSON.stringify(r.git,null,2)}`:"";throw new Error(`\u89E3\u51B3\u51B2\u7A81\u5931\u8D25: ${g}${l}`)}}async mergeRemoteBranches(e,i,t,s,r={}){let g=this.configManager.getRepo(e),l=this.configManager.getRepo(t);if(!g||!l)throw new Error("\u6E90\u4ED3\u5E93\u6216\u76EE\u6807\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let n=i.replace(/^(remotes\/)?origin\//,""),c=s.replace(/^(remotes\/)?origin\//,""),o=await this.initRepo(l),h=`source_${e}`;try{try{await o.remote(["remove",h]);}catch{}let m=new URL(g.url);g.token?(m.username=encodeURIComponent(g.token),m.password=""):g.username&&g.password&&(m.username=encodeURIComponent(g.username),m.password=encodeURIComponent(g.password)),await o.remote(["add",h,m.toString()]),await o.fetch(["--all","--prune","--tags"]),await o.fetch([h,"--force"]);let u=await o.listRemote(["--heads"]);if(!(await this.checkRemoteBranchExists(h,n,o)||u.includes(`refs/heads/${n}`))){let a=await this.initRepo(g);if(!await this.checkRemoteBranchExists(e,n,a))throw new Error(`\u6E90\u5206\u652F '${i}' \u5728\u4ED3\u5E93 '${e}' \u4E2D\u4E0D\u5B58\u5728`)}if(!u.includes(`refs/heads/${c}`))throw new Error(`\u76EE\u6807\u5206\u652F '${s}' \u5728\u4ED3\u5E93 '${t}' \u4E2D\u4E0D\u5B58\u5728`);try{await o.checkout(c);}catch{await o.checkout(["-b",c,"--track",`origin/${c}`]);}if(await o.pull("origin",c),r.showDiff){console.log(`[${t}] \u663E\u793A\u5C06\u8981\u5408\u5E76\u7684\u5DEE\u5F02...`);let a=await o.diff(["HEAD",`${h}/${n}`]);console.log(a);}try{let a=await o.merge([`${h}/${n}`]);if(!(a.summary.changes>0||a.summary.insertions>0||a.summary.deletions>0)){console.log(`[${t}] \u5206\u652F\u5DF2\u7ECF\u662F\u6700\u65B0\u7684\uFF0C\u65E0\u9700\u5408\u5E76`),console.log(`[${t}] \u6E05\u7406\u4E34\u65F6\u8FDC\u7A0B\u4ED3\u5E93...`),await o.remote(["remove",h]);return}if(r.conflictStrategy){let y=await o.status();y.conflicted.length>0&&(console.log(`[${t}] \u68C0\u6D4B\u5230${y.conflicted.length}\u4E2A\u51B2\u7A81\u6587\u4EF6\uFF0C\u4F7F\u7528${r.conflictStrategy==="ours"?"\u6211\u4EEC\u7684":"\u4ED6\u4EEC\u7684"}\u7B56\u7565\u89E3\u51B3...`),await this.resolveConflicts(t,r.conflictStrategy),await o.commit(`\u81EA\u52A8\u5408\u5E76 ${e}/${n} \u5230 ${t}/${c}`));}r.push!==!1&&(console.log(`[${t}] \u63A8\u9001\u5408\u5E76\u7ED3\u679C...`),await o.push("origin",c),console.log(`[${t}] \u63A8\u9001\u5B8C\u6210`));}catch(a){throw console.error(`[${t}] \u5408\u5E76\u5931\u8D25:`,a.message),new Error(`\u5206\u652F\u5408\u5E76\u5931\u8D25: ${a.message}`)}console.log(`[${t}] \u6E05\u7406\u4E34\u65F6\u8FDC\u7A0B\u4ED3\u5E93...`),await o.remote(["remove",h]),console.log("\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5B8C\u6210");}catch(m){try{await o.remote(["remove",h]);}catch(w){console.log(w);}let u=m.message||"\u672A\u77E5\u9519\u8BEF",f=m.git?` Git\u9519\u8BEF: ${JSON.stringify(m.git,null,2)}`:"";throw new Error(`\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5931\u8D25: ${u}${f}`)}}async checkRemoteBranchExists(e,i,t){let s=await t.listRemote(["--heads"]),r=i.replace(/^(origin\/|remotes\/origin\/|source_[^/]+\/|refs\/heads\/|refs\/remotes\/[^/]+\/)/,""),g=[`refs/heads/${r}`,`refs/remotes/source_${e}/${r}`,`refs/remotes/origin/${r}`,`HEAD:refs/heads/${r}`,r,`source_${e}/${r}`,`origin/${r}`,`refs/remotes/${e}/${r}`],l=s.split(` `).map(c=>{let o=c.trim().split(/\s+/);return o.length>1?o[1]:""}).filter(Boolean);for(let c of g)if(l.includes(c))return true;let n=l.some(c=>c.replace(/^refs\/heads\//,"").replace(/^refs\/remotes\/[^/]+\//,"").replace(/^(origin\/|source_[^/]+\/)/,"")===r);if(!n)try{if((await t.listRemote([e,"--heads"])).split(` `).map(m=>{let u=m.trim().split(/\s+/);return u.length>1?u[1]:""}).filter(Boolean).some(m=>m.replace(/^refs\/heads\//,"")===r))return !0}catch(c){console.warn(`\u4ECE\u8FDC\u7A0B\u4ED3\u5E93 ${e} \u83B7\u53D6\u5206\u652F\u4FE1\u606F\u5931\u8D25:`,c);}return n}};var k=class{constructor(e){this.git=M(e);}async mergeBranch(e,i){try{await this.git.checkout(i),await this.git.merge([e]);}catch(t){throw new Error(`\u5408\u5E76\u5206\u652F\u5931\u8D25: ${t.message}`)}}async abortMerge(){try{await this.git.merge(["--abort"]);}catch(e){throw new Error(`\u4E2D\u6B62\u5408\u5E76\u5931\u8D25: ${e.message}`)}}async resolveConflicts(e){try{e==="ours"?await this.git.checkout(["--ours","."]):await this.git.checkout(["--theirs","."]),await this.git.add(".");}catch(i){throw new Error(`\u89E3\u51B3\u51B2\u7A81\u5931\u8D25: ${i.message}`)}}async isMerging(){try{return (await this.git.status()).conflicted.length>0}catch(e){throw new Error(`\u83B7\u53D6\u5408\u5E76\u72B6\u6001\u5931\u8D25: ${e.message}`)}}}; export{d as ConfigManager,k as GitMergeTools,v as MultiRepoMerge};//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map