UNPKG

@supos-ce-platform/git-tools

Version:

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

86 lines (73 loc) 23.9 kB
#!/usr/bin/env node import {Command}from'commander';import $ from'inquirer';import x from'simple-git';import E from'node:fs';import C from'node:path';import S from'node:os';var R=class{constructor(){this.configPath=C.join(S.homedir(),".supos-git-config.json"),this.config=this.loadConfig();}loadConfig(){try{if(E.existsSync(this.configPath)){let e=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{E.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 r=this.config.repos.findIndex(t=>t.name===e.name);r>=0?this.config.repos[r]=e:this.config.repos.push(e),this.saveConfig();}getRepo(e){return this.config.repos.find(r=>r.name===e)}removeRepo(e){this.config.repos=this.config.repos.filter(r=>r.name!==e),this.saveConfig();}listRepos(){return this.config.repos}saveRemoteMergeHistory(e){this.config.remoteMergeHistory=e,this.saveConfig();}getRemoteMergeHistory(){return this.config.remoteMergeHistory}};var w=class{constructor(){this.configManager=new R,this.tempDir=C.join(S.tmpdir(),"supos-git-merge"),E.existsSync(this.tempDir)||E.mkdirSync(this.tempDir,{recursive:true});}async initRepo(e){let r=C.join(this.tempDir,e.name);E.existsSync(r)||E.mkdirSync(r,{recursive:true});let t=x(r);try{E.existsSync(C.join(r,".git"))||await t.init();let a=e.url;try{let n=new URL(e.url);n.username="",n.password="",e.token?a=n.toString().replace(/^(https?:\/\/)(.+)$/,`$1${encodeURIComponent(e.token)}@$2`):e.username&&e.password&&(a=n.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()!==a.trim()&&await t.remote(["set-url","origin",a]);}catch{await t.remote(["add","origin",a]);}return await t.fetch(["--all","--prune","--tags","--force"]),t}catch(a){let n=a.message||"\u672A\u77E5\u9519\u8BEF",o=a.git?` Git\u9519\u8BEF: ${JSON.stringify(a.git,null,2)}`:"";throw new Error(`[${e.name}] Git\u64CD\u4F5C\u5931\u8D25: ${n}${o}`)}}async mergeCrossBranch(e,r,t,a){let n=this.configManager.getRepo(e),o=this.configManager.getRepo(t);if(!n||!o)throw new Error("\u6E90\u4ED3\u5E93\u6216\u76EE\u6807\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let m=await this.initRepo(n),c=await this.initRepo(o);try{if(await m.fetch(["--all","--prune","--tags","--force"]),!await this.checkRemoteBranchExists(e,r,m))throw new Error(`\u6E90\u5206\u652F '${r}' \u5728\u4ED3\u5E93 '${e}' \u4E2D\u4E0D\u5B58\u5728`);let i=r.replace(/^(origin\/|remotes\/origin\/|source_[^/]+\/)/,"");try{await m.checkout(i);}catch{try{await m.checkout(["-b",i,"--track",`origin/${i}`]);}catch(v){console.error(`[${e}] \u521B\u5EFA\u8DDF\u8E2A\u5206\u652F\u5931\u8D25:`,v),await m.fetch(["origin",i,"--update-head-ok"]),await m.checkout(["-b",i]);}}try{await m.pull("origin",i);}catch(l){console.warn(`[${e}] \u62C9\u53D6\u66F4\u65B0\u5931\u8D25\uFF0C\u7EE7\u7EED\u4F7F\u7528\u5F53\u524D\u5185\u5BB9:`,l);}let u=`source_${e}`;try{await c.remote(["remove",u]);}catch{}let h=new URL(n.url);n.token?(h.username=encodeURIComponent(n.token),h.password=""):n.username&&n.password&&(h.username=encodeURIComponent(n.username),h.password=encodeURIComponent(n.password)),await c.remote(["add",u,h.toString()]),await c.fetch([u,"--force"]),await c.fetch(["--all","--prune","--tags","--force"]);let p=await c.branch(["--all"]),f=a.replace(/^origin\//,""),B=p.all.some(l=>l.trim().replace(/^remotes\/origin\//,"").replace(/^origin\//,"")===f);if(!B){if(B=(await c.listRemote(["--heads"])).includes(`refs/heads/${f}`),!B)throw new Error(`\u76EE\u6807\u5206\u652F '${a}' \u5728\u4ED3\u5E93 '${t}' \u4E2D\u4E0D\u5B58\u5728 \u53EF\u7528\u7684\u8FDC\u7A0B\u5206\u652F: ${p.all.join(` `)}`);await c.fetch(["origin",f]);}try{await c.checkout(f);}catch{try{await c.checkout(["-b",f,"--track",`origin/${f}`]);}catch(v){console.error(`[${t}] \u521B\u5EFA\u8DDF\u8E2A\u5206\u652F\u5931\u8D25:`,v),await c.fetch(["origin",f,"--update-head-ok"]),await c.checkout(["-b",f]);}}try{await c.pull("origin",f);}catch(l){console.warn(`[${t}] \u62C9\u53D6\u66F4\u65B0\u5931\u8D25\uFF0C\u7EE7\u7EED\u4F7F\u7528\u5F53\u524D\u5185\u5BB9:`,l);}try{await c.merge([`${u}/${i}`]),await c.push("origin",f);}catch(l){throw console.error(`[${t}] \u5408\u5E76\u5931\u8D25:`,l.message),new Error(`\u5206\u652F\u5408\u5E76\u5931\u8D25: ${l.message}`)}await c.remote(["remove",u]);}catch(g){let i=g.message||"\u672A\u77E5\u9519\u8BEF",u=g.git?` Git\u9519\u8BEF: ${JSON.stringify(g.git,null,2)}`:"";throw new Error(`\u8DE8\u4ED3\u5E93\u5408\u5E76\u5931\u8D25: ${i}${u}`)}}async abortMerge(e){let r=this.configManager.getRepo(e);if(!r)throw new Error("\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let t=await this.initRepo(r);try{await t.merge(["--abort"]);}catch(a){let n=a.message||"\u672A\u77E5\u9519\u8BEF",o=a.git?` Git\u9519\u8BEF: ${JSON.stringify(a.git,null,2)}`:"";throw new Error(`\u4E2D\u6B62\u5408\u5E76\u5931\u8D25: ${n}${o}`)}}async resolveConflicts(e,r){let t=this.configManager.getRepo(e);if(!t)throw new Error("\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let a=await this.initRepo(t);try{r==="ours"?await a.checkout(["--ours","."]):await a.checkout(["--theirs","."]),await a.add("."),console.log(`[${e}] \u51B2\u7A81\u5DF2\u89E3\u51B3`);}catch(n){let o=n.message||"\u672A\u77E5\u9519\u8BEF",m=n.git?` Git\u9519\u8BEF: ${JSON.stringify(n.git,null,2)}`:"";throw new Error(`\u89E3\u51B3\u51B2\u7A81\u5931\u8D25: ${o}${m}`)}}async mergeRemoteBranches(e,r,t,a,n={}){let o=this.configManager.getRepo(e),m=this.configManager.getRepo(t);if(!o||!m)throw new Error("\u6E90\u4ED3\u5E93\u6216\u76EE\u6807\u4ED3\u5E93\u914D\u7F6E\u4E0D\u5B58\u5728");let c=r.replace(/^(remotes\/)?origin\//,""),g=a.replace(/^(remotes\/)?origin\//,""),i=await this.initRepo(m),u=`source_${e}`;try{try{await i.remote(["remove",u]);}catch{}let h=new URL(o.url);o.token?(h.username=encodeURIComponent(o.token),h.password=""):o.username&&o.password&&(h.username=encodeURIComponent(o.username),h.password=encodeURIComponent(o.password)),await i.remote(["add",u,h.toString()]),await i.fetch(["--all","--prune","--tags"]),await i.fetch([u,"--force"]);let p=await i.listRemote(["--heads"]);if(!(await this.checkRemoteBranchExists(u,c,i)||p.includes(`refs/heads/${c}`))){let l=await this.initRepo(o);if(!await this.checkRemoteBranchExists(e,c,l))throw new Error(`\u6E90\u5206\u652F '${r}' \u5728\u4ED3\u5E93 '${e}' \u4E2D\u4E0D\u5B58\u5728`)}if(!p.includes(`refs/heads/${g}`))throw new Error(`\u76EE\u6807\u5206\u652F '${a}' \u5728\u4ED3\u5E93 '${t}' \u4E2D\u4E0D\u5B58\u5728`);try{await i.checkout(g);}catch{await i.checkout(["-b",g,"--track",`origin/${g}`]);}if(await i.pull("origin",g),n.showDiff){console.log(`[${t}] \u663E\u793A\u5C06\u8981\u5408\u5E76\u7684\u5DEE\u5F02...`);let l=await i.diff(["HEAD",`${u}/${c}`]);console.log(l);}try{let l=await i.merge([`${u}/${c}`]);if(!(l.summary.changes>0||l.summary.insertions>0||l.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 i.remote(["remove",u]);return}if(n.conflictStrategy){let b=await i.status();b.conflicted.length>0&&(console.log(`[${t}] \u68C0\u6D4B\u5230${b.conflicted.length}\u4E2A\u51B2\u7A81\u6587\u4EF6\uFF0C\u4F7F\u7528${n.conflictStrategy==="ours"?"\u6211\u4EEC\u7684":"\u4ED6\u4EEC\u7684"}\u7B56\u7565\u89E3\u51B3...`),await this.resolveConflicts(t,n.conflictStrategy),await i.commit(`\u81EA\u52A8\u5408\u5E76 ${e}/${c} \u5230 ${t}/${g}`));}n.push!==!1&&(console.log(`[${t}] \u63A8\u9001\u5408\u5E76\u7ED3\u679C...`),await i.push("origin",g),console.log(`[${t}] \u63A8\u9001\u5B8C\u6210`));}catch(l){throw console.error(`[${t}] \u5408\u5E76\u5931\u8D25:`,l.message),new Error(`\u5206\u652F\u5408\u5E76\u5931\u8D25: ${l.message}`)}console.log(`[${t}] \u6E05\u7406\u4E34\u65F6\u8FDC\u7A0B\u4ED3\u5E93...`),await i.remote(["remove",u]),console.log("\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5B8C\u6210");}catch(h){try{await i.remote(["remove",u]);}catch(B){console.log(B);}let p=h.message||"\u672A\u77E5\u9519\u8BEF",f=h.git?` Git\u9519\u8BEF: ${JSON.stringify(h.git,null,2)}`:"";throw new Error(`\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5931\u8D25: ${p}${f}`)}}async checkRemoteBranchExists(e,r,t){let a=await t.listRemote(["--heads"]),n=r.replace(/^(origin\/|remotes\/origin\/|source_[^/]+\/|refs\/heads\/|refs\/remotes\/[^/]+\/)/,""),o=[`refs/heads/${n}`,`refs/remotes/source_${e}/${n}`,`refs/remotes/origin/${n}`,`HEAD:refs/heads/${n}`,n,`source_${e}/${n}`,`origin/${n}`,`refs/remotes/${e}/${n}`],m=a.split(` `).map(g=>{let i=g.trim().split(/\s+/);return i.length>1?i[1]:""}).filter(Boolean);for(let g of o)if(m.includes(g))return true;let c=m.some(g=>g.replace(/^refs\/heads\//,"").replace(/^refs\/remotes\/[^/]+\//,"").replace(/^(origin\/|source_[^/]+\/)/,"")===n);if(!c)try{if((await t.listRemote([e,"--heads"])).split(` `).map(h=>{let p=h.trim().split(/\s+/);return p.length>1?p[1]:""}).filter(Boolean).some(h=>h.replace(/^refs\/heads\//,"")===n))return !0}catch(g){console.warn(`\u4ECE\u8FDC\u7A0B\u4ED3\u5E93 ${e} \u83B7\u53D6\u5206\u652F\u4FE1\u606F\u5931\u8D25:`,g);}return c}};var d=new Command,y=new R;d.name("supos-git").description("Git tools").version("0.0.5");d.command("merge").description("\u5408\u5E76\u8DE8\u4ED3\u5E93\u5206\u652F").option("-s, --source-repo <repo>","\u6E90\u4ED3\u5E93\u540D\u79F0").option("-b, --source-branch <branch>","\u6E90\u5206\u652F\u540D\u79F0").option("-t, --target-repo <repo>","\u76EE\u6807\u4ED3\u5E93\u540D\u79F0").option("-tb, --target-branch <branch>","\u76EE\u6807\u5206\u652F\u540D\u79F0").action(async s=>{try{let e=y.listRepos();if(e.length===0)throw new Error("\u6CA1\u6709\u914D\u7F6E\u4EFB\u4F55\u4ED3\u5E93\uFF0C\u8BF7\u5148\u4F7F\u7528 config \u547D\u4EE4\u914D\u7F6E\u4ED3\u5E93");let r=await $.prompt([{type:"list",name:"sourceRepo",message:"\u8BF7\u9009\u62E9\u6E90\u4ED3\u5E93\uFF1A",choices:e.map(a=>a.name),when:!s.sourceRepo},{type:"input",name:"sourceBranch",message:"\u8BF7\u8F93\u5165\u6E90\u5206\u652F\u540D\u79F0\uFF1A",when:!s.sourceBranch},{type:"list",name:"targetRepo",message:"\u8BF7\u9009\u62E9\u76EE\u6807\u4ED3\u5E93\uFF1A",choices:e.map(a=>a.name),when:!s.targetRepo},{type:"input",name:"targetBranch",message:"\u8BF7\u8F93\u5165\u76EE\u6807\u5206\u652F\u540D\u79F0\uFF1A",when:!s.targetBranch}]);await new w().mergeCrossBranch(s.sourceRepo||r.sourceRepo,s.sourceBranch||r.sourceBranch,s.targetRepo||r.targetRepo,s.targetBranch||r.targetBranch),console.log("\u5206\u652F\u5408\u5E76\u6210\u529F");}catch(e){console.error("\u5206\u652F\u5408\u5E76\u5931\u8D25:",e.message),process.exit(1);}});d.command("remote-merge").description("\u5408\u5E76\u8FDC\u7A0B\u5206\u652F").option("-s, --source-repo <repo>","\u6E90\u4ED3\u5E93\u540D\u79F0").option("-b, --source-branch <branch>","\u6E90\u5206\u652F\u540D\u79F0").option("-t, --target-repo <repo>","\u76EE\u6807\u4ED3\u5E93\u540D\u79F0").option("-tb, --target-branch <branch>","\u76EE\u6807\u5206\u652F\u540D\u79F0").option("-d, --show-diff","\u663E\u793A\u5C06\u8981\u5408\u5E76\u7684\u5DEE\u5F02").option("-c, --conflict-strategy <strategy>","\u5408\u5E76\u51B2\u7A81\u65F6\u7684\u81EA\u52A8\u89E3\u51B3\u7B56\u7565 (ours/theirs)").option("-np, --no-push","\u4E0D\u63A8\u9001\u5408\u5E76\u7ED3\u679C").action(async s=>{try{let e=y.listRepos();if(e.length===0)throw new Error("\u6CA1\u6709\u914D\u7F6E\u4EFB\u4F55\u4ED3\u5E93\uFF0C\u8BF7\u5148\u4F7F\u7528 config \u547D\u4EE4\u914D\u7F6E\u4ED3\u5E93");let r=y.getRemoteMergeHistory(),t=!1;if(r&&!s.sourceRepo&&!s.sourceBranch&&!s.targetRepo&&!s.targetBranch){let o=`\u662F\u5426\u4F7F\u7528\u4E0A\u6B21\u7684\u5408\u5E76\u914D\u7F6E\uFF1F \u6E90\u4ED3\u5E93: ${r.sourceRepo} \u6E90\u5206\u652F: ${r.sourceBranch} \u76EE\u6807\u4ED3\u5E93: ${r.targetRepo} \u76EE\u6807\u5206\u652F: ${r.targetBranch}`;r.options&&(o+=` \u5408\u5E76\u9009\u9879:`,r.options.showDiff!==void 0&&(o+=` - \u663E\u793A\u5DEE\u5F02: ${r.options.showDiff?"\u662F":"\u5426"}`),r.options.conflictStrategy?o+=` - \u51B2\u7A81\u89E3\u51B3\u7B56\u7565: ${r.options.conflictStrategy==="ours"?"\u4F7F\u7528\u6211\u4EEC\u7684\u66F4\u6539 (ours)":"\u4F7F\u7528\u4ED6\u4EEC\u7684\u66F4\u6539 (theirs)"}`:o+=` - \u51B2\u7A81\u89E3\u51B3\u7B56\u7565: \u4E0D\u81EA\u52A8\u89E3\u51B3`,r.options.push!==void 0&&(o+=` - \u63A8\u9001\u5408\u5E76\u7ED3\u679C: ${r.options.push?"\u662F":"\u5426"}`));let{confirm:m}=await $.prompt([{type:"confirm",name:"confirm",message:o,default:!0}]);t=m;}let a=await $.prompt([{type:"list",name:"sourceRepo",message:"\u8BF7\u9009\u62E9\u6E90\u4ED3\u5E93\uFF1A",choices:e.map(o=>o.name),when:!t&&!s.sourceRepo},{type:"list",name:"sourceBranch",message:"\u8BF7\u9009\u62E9\u6E90\u5206\u652F\uFF1A",when:!t&&!s.sourceBranch,async choices(o){let m=o.sourceRepo,c=new w,g=y.getRepo(m);if(!g)throw new Error(`\u627E\u4E0D\u5230\u4ED3\u5E93\u914D\u7F6E: ${m}`);let h=(await(await c.initRepo(g)).listRemote(["--heads"])).split(` `).filter(p=>p.trim()).map(p=>{let f=p.match(/refs\/heads\/(.+)$/);return f?f[1]:null}).filter(p=>p!==null).sort();if(h.length===0)throw new Error(`[${m}] \u672A\u627E\u5230\u4EFB\u4F55\u6709\u6548\u7684\u8FDC\u7A0B\u5206\u652F`);return h}},{type:"list",name:"targetRepo",message:"\u8BF7\u9009\u62E9\u76EE\u6807\u4ED3\u5E93\uFF1A",choices:e.map(o=>o.name),when:!t&&!s.targetRepo},{type:"list",name:"targetBranch",message:"\u8BF7\u9009\u62E9\u76EE\u6807\u5206\u652F\uFF1A",when:!t&&!s.targetBranch,async choices(o){let m=o.targetRepo,c=new w,g=y.getRepo(m);if(!g)throw new Error(`\u627E\u4E0D\u5230\u4ED3\u5E93\u914D\u7F6E: ${m}`);let h=(await(await c.initRepo(g)).listRemote(["--heads"])).split(` `).filter(p=>p.trim()).map(p=>{let f=p.match(/refs\/heads\/(.+)$/);return f?f[1]:null}).filter(p=>p!==null).sort();if(h.length===0)throw new Error(`[${m}] \u672A\u627E\u5230\u4EFB\u4F55\u6709\u6548\u7684\u8FDC\u7A0B\u5206\u652F`);return h}},{type:"confirm",name:"showDiff",message:"\u662F\u5426\u663E\u793A\u5C06\u8981\u5408\u5E76\u7684\u5DEE\u5F02\uFF1F",default:!1,when:()=>!t&&s.showDiff===void 0},{type:"list",name:"conflictStrategy",message:"\u8BF7\u9009\u62E9\u5408\u5E76\u51B2\u7A81\u65F6\u7684\u81EA\u52A8\u89E3\u51B3\u7B56\u7565\uFF1A",choices:[{name:"\u4E0D\u81EA\u52A8\u89E3\u51B3",value:null},{name:"\u4F7F\u7528\u6211\u4EEC\u7684\u66F4\u6539 (ours)",value:"ours"},{name:"\u4F7F\u7528\u4ED6\u4EEC\u7684\u66F4\u6539 (theirs)",value:"theirs"}],when:()=>!t&&!s.conflictStrategy&&s.conflictStrategy!==null}]),n=new w;try{let o=t&&r?.options?{...r.options}:{showDiff:s.showDiff!==void 0?s.showDiff:a.showDiff,conflictStrategy:s.conflictStrategy||a.conflictStrategy,push:s.push};await n.mergeRemoteBranches(t?r?.sourceRepo:s.sourceRepo||a.sourceRepo,t?r?.sourceBranch:s.sourceBranch||a.sourceBranch,t?r?.targetRepo:s.targetRepo||a.targetRepo,t?r?.targetBranch:s.targetBranch||a.targetBranch,o),y.saveRemoteMergeHistory({sourceRepo:t?r?.sourceRepo:s.sourceRepo||a.sourceRepo,sourceBranch:t?r?.sourceBranch:s.sourceBranch||a.sourceBranch,targetRepo:t?r?.targetRepo:s.targetRepo||a.targetRepo,targetBranch:t?r?.targetBranch:s.targetBranch||a.targetBranch,options:o,timestamp:Date.now()});}catch(o){o.message&&o.message.includes("Already up to date")?console.log("\u5206\u652F\u5DF2\u7ECF\u662F\u6700\u65B0\u7684\uFF0C\u65E0\u9700\u5408\u5E76"):(console.error("\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5931\u8D25:",o.message),process.exit(1));}}catch(e){console.error("\u8FDC\u7A0B\u5206\u652F\u5408\u5E76\u5931\u8D25:",e.message),process.exit(1);}});d.command("abort").description("\u4E2D\u6B62\u5408\u5E76\u64CD\u4F5C").option("-r, --repo <repo>","\u4ED3\u5E93\u540D\u79F0").action(async s=>{try{if(!s.repo){let r=y.listRepos(),t=await $.prompt([{type:"list",name:"repo",message:"\u8BF7\u9009\u62E9\u8981\u4E2D\u6B62\u5408\u5E76\u7684\u4ED3\u5E93\uFF1A",choices:r.map(a=>a.name)}]);s.repo=t.repo;}await new w().abortMerge(s.repo),console.log("\u6210\u529F\u4E2D\u6B62\u5408\u5E76\u64CD\u4F5C");}catch(e){console.error("\u4E2D\u6B62\u5408\u5E76\u5931\u8D25:",e.message),process.exit(1);}});d.command("resolve").description("\u89E3\u51B3\u5408\u5E76\u51B2\u7A81").option("-r, --repo <repo>","\u4ED3\u5E93\u540D\u79F0").option("-s, --strategy <strategy>","\u89E3\u51B3\u7B56\u7565 (ours/theirs)").action(async s=>{try{let e=await $.prompt([{type:"list",name:"repo",message:"\u8BF7\u9009\u62E9\u8981\u89E3\u51B3\u51B2\u7A81\u7684\u4ED3\u5E93\uFF1A",choices:y.listRepos().map(t=>t.name),when:!s.repo},{type:"list",name:"strategy",message:"\u8BF7\u9009\u62E9\u89E3\u51B3\u51B2\u7A81\u7684\u7B56\u7565\uFF1A",choices:[{name:"\u4F7F\u7528\u6211\u4EEC\u7684\u66F4\u6539 (ours)",value:"ours"},{name:"\u4F7F\u7528\u4ED6\u4EEC\u7684\u66F4\u6539 (theirs)",value:"theirs"}],when:!s.strategy}]);await new w().resolveConflicts(s.repo||e.repo,s.strategy||e.strategy),console.log("\u6210\u529F\u89E3\u51B3\u51B2\u7A81");}catch(e){console.error("\u89E3\u51B3\u51B2\u7A81\u5931\u8D25:",e.message),process.exit(1);}});d.command("config").description("\u914D\u7F6E\u4ED3\u5E93\u4FE1\u606F").option("-n, --name <name>","\u4ED3\u5E93\u540D\u79F0").option("-u, --url <url>","\u4ED3\u5E93URL").option("-t, --token <token>","Git\u4EE4\u724C").option("--username <username>","\u7528\u6237\u540D").option("--password <password>","\u5BC6\u7801").action(async s=>{try{let e=new R,r=await $.prompt([{type:"input",name:"name",message:"\u8BF7\u8F93\u5165\u4ED3\u5E93\u540D\u79F0\uFF1A",when:!s.name,validate:o=>o?/^[\w-]+$/.test(o)?(e.listRepos()||[]).some(c=>c.name===o)?"\u4ED3\u5E93\u540D\u79F0\u5DF2\u5B58\u5728":!0:"\u4ED3\u5E93\u540D\u79F0\u53EA\u80FD\u5305\u542B\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u548C\u6A2A\u7EBF":"\u4ED3\u5E93\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A"},{type:"input",name:"url",message:"\u8BF7\u8F93\u5165\u4ED3\u5E93URL\uFF1A",when:!s.url,validate:o=>{if(!o)return "Git\u4ED3\u5E93URL\u4E0D\u80FD\u4E3A\u7A7A";try{return new URL(o),o.endsWith(".git")?!0:"Git\u4ED3\u5E93URL\u5FC5\u987B\u4EE5.git\u7ED3\u5C3E"}catch{return "\u8BF7\u8F93\u5165\u6709\u6548\u7684Git\u4ED3\u5E93URL"}}},{type:"list",name:"type",message:"\u8BF7\u9009\u62E9\u4ED3\u5E93\u7C7B\u578B\uFF1A",choices:["gitee","gitea"]},{type:"list",name:"authType",message:"\u8BF7\u9009\u62E9\u8BA4\u8BC1\u65B9\u5F0F\uFF1A",choices:["token","password"]},{type:"password",name:"token",message:"\u8BF7\u8F93\u5165Git\u4EE4\u724C\uFF1A",when:o=>!s.token&&o.authType==="token",validate:o=>o?!0:"Git\u4EE4\u724C\u4E0D\u80FD\u4E3A\u7A7A"},{type:"input",name:"username",message:"\u8BF7\u8F93\u5165\u7528\u6237\u540D\uFF1A",when:o=>!s.username&&o.authType==="password",validate:o=>o?!0:"\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A"},{type:"password",name:"password",message:"\u8BF7\u8F93\u5165\u5BC6\u7801\uFF1A",when:o=>!s.password&&o.authType==="password",validate:o=>o?!0:"\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A"}]),t={token:s.token||r.token,username:s.username||r.username,password:s.password||r.password};if(!t.token&&(!t.username||!t.password))throw new Error("\u5FC5\u987B\u63D0\u4F9BGit\u4EE4\u724C\u6216\u7528\u6237\u540D\u5BC6\u7801");let a=new R,n=new w;try{await n.initRepo({name:s.name||r.name,url:s.url||r.url,type:r.type,...t});}catch(o){throw new Error(`Git\u8BA4\u8BC1\u9A8C\u8BC1\u5931\u8D25: ${o.message}`)}a.addRepo({name:s.name||r.name,url:s.url||r.url,type:r.type,...t}),console.log("\u4ED3\u5E93\u914D\u7F6E\u5DF2\u4FDD\u5B58");}catch(e){console.error("\u4FDD\u5B58\u914D\u7F6E\u5931\u8D25:",e.message),process.exit(1);}});d.command("list").description("\u5217\u51FA\u6240\u6709\u5DF2\u914D\u7F6E\u7684\u4ED3\u5E93\u4FE1\u606F").action(()=>{try{let s=y.listRepos();if(s.length===0){console.log("\u6CA1\u6709\u914D\u7F6E\u4EFB\u4F55\u4ED3\u5E93\uFF0C\u8BF7\u5148\u4F7F\u7528 config \u547D\u4EE4\u914D\u7F6E\u4ED3\u5E93");return}console.log(` \u5DF2\u914D\u7F6E\u7684\u4ED3\u5E93\u4FE1\u606F\uFF1A`),console.table(s.map(e=>({\u540D\u79F0:e.name,URL:e.url,\u7C7B\u578B:e.type,\u8BA4\u8BC1\u65B9\u5F0F:e.token?"token":"password",\u7528\u6237\u540D:e.username||"-"})));}catch(s){console.error("\u83B7\u53D6\u4ED3\u5E93\u4FE1\u606F\u5931\u8D25:",s.message),process.exit(1);}});d.command("remove").description("\u5220\u9664\u4ED3\u5E93\u914D\u7F6E").option("-n, --name <name>","\u4ED3\u5E93\u540D\u79F0").action(async s=>{try{let e=y.listRepos();if(e.length===0)throw new Error("\u6CA1\u6709\u914D\u7F6E\u4EFB\u4F55\u4ED3\u5E93\uFF0C\u8BF7\u5148\u4F7F\u7528 config \u547D\u4EE4\u914D\u7F6E\u4ED3\u5E93");let r=s.name;r||(r=(await $.prompt([{type:"list",name:"name",message:"\u8BF7\u9009\u62E9\u8981\u5220\u9664\u7684\u4ED3\u5E93\uFF1A",choices:e.map(n=>n.name)}])).name),(await $.prompt([{type:"confirm",name:"sure",message:`\u786E\u5B9A\u8981\u5220\u9664\u4ED3\u5E93 "${r}" \u7684\u914D\u7F6E\u5417\uFF1F`,default:!1}])).sure?(y.removeRepo(r),console.log(`\u4ED3\u5E93 "${r}" \u7684\u914D\u7F6E\u5DF2\u5220\u9664`)):console.log("\u64CD\u4F5C\u5DF2\u53D6\u6D88");}catch(e){console.error("\u5220\u9664\u4ED3\u5E93\u914D\u7F6E\u5931\u8D25:",e.message),process.exit(1);}});d.command("help").description("\u663E\u793A\u5E2E\u52A9\u4FE1\u606F").action(()=>{console.log(` \u4F7F\u7528\u8BF4\u660E\uFF1A git-tools <\u547D\u4EE4> [\u9009\u9879] \u53EF\u7528\u547D\u4EE4\uFF1A merge \u5408\u5E76\u8DE8\u4ED3\u5E93\u5206\u652F \u9009\u9879\uFF1A -s, --source-repo <repo> \u6E90\u4ED3\u5E93\u540D\u79F0 -b, --source-branch <branch> \u6E90\u5206\u652F\u540D\u79F0 -t, --target-repo <repo> \u76EE\u6807\u4ED3\u5E93\u540D\u79F0 -tb, --target-branch <branch> \u76EE\u6807\u5206\u652F\u540D\u79F0 remote-merge \u5408\u5E76\u8FDC\u7A0B\u5206\u652F \u9009\u9879\uFF1A -s, --source-repo <repo> \u6E90\u4ED3\u5E93\u540D\u79F0 -b, --source-branch <branch> \u6E90\u5206\u652F\u540D\u79F0 -t, --target-repo <repo> \u76EE\u6807\u4ED3\u5E93\u540D\u79F0 -tb, --target-branch <branch> \u76EE\u6807\u5206\u652F\u540D\u79F0 -d, --show-diff \u663E\u793A\u5C06\u8981\u5408\u5E76\u7684\u5DEE\u5F02 -c, --conflict-strategy <strategy> \u5408\u5E76\u51B2\u7A81\u65F6\u7684\u81EA\u52A8\u89E3\u51B3\u7B56\u7565 (ours/theirs) -np, --no-push \u4E0D\u63A8\u9001\u5408\u5E76\u7ED3\u679C abort \u4E2D\u6B62\u5408\u5E76\u64CD\u4F5C \u9009\u9879\uFF1A -r, --repo <repo> \u4ED3\u5E93\u540D\u79F0 resolve \u89E3\u51B3\u5408\u5E76\u51B2\u7A81 \u9009\u9879\uFF1A -r, --repo <repo> \u4ED3\u5E93\u540D\u79F0 -s, --strategy <strategy> \u89E3\u51B3\u7B56\u7565 (ours/theirs) config \u914D\u7F6E\u4ED3\u5E93\u4FE1\u606F \u9009\u9879\uFF1A -n, --name <name> \u4ED3\u5E93\u540D\u79F0 -u, --url <url> \u4ED3\u5E93URL -t, --token <token> Git\u4EE4\u724C --username <username> \u7528\u6237\u540D --password <password> \u5BC6\u7801 remove \u5220\u9664\u4ED3\u5E93\u914D\u7F6E \u9009\u9879\uFF1A -n, --name <name> \u4ED3\u5E93\u540D\u79F0 list \u5217\u51FA\u6240\u6709\u5DF2\u914D\u7F6E\u7684\u4ED3\u5E93\u4FE1\u606F \u793A\u4F8B\uFF1A # \u914D\u7F6E\u4ED3\u5E93 git-tools config -n myrepo -u https://github.com/user/repo.git -t your-token # \u5408\u5E76\u5206\u652F git-tools merge -s source-repo -b feature -t target-repo -tb main # \u5408\u5E76\u8FDC\u7A0B\u5206\u652F\uFF08\u4F18\u5316\u7248\u672C\uFF09 git-tools remote-merge -s source-repo -b feature -t target-repo -tb main -d -c ours # \u89E3\u51B3\u51B2\u7A81 git-tools resolve -r myrepo -s ours # \u5220\u9664\u4ED3\u5E93\u914D\u7F6E git-tools remove -n myrepo `);});d.parse();//# sourceMappingURL=cli.js.map //# sourceMappingURL=cli.js.map