From 6ca1781072537970cd047b3ed442feedd2eac452 Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Mon, 29 Mar 2021 11:18:46 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/git.js | 7 ++++++ locales/en.json | 10 ++++---- locales/zh-CN.json | 9 +++---- plugin/GitPush/index.js | 56 ++++++++++++++--------------------------- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/lib/git.js b/lib/git.js index 131a532..b2cc2a7 100644 --- a/lib/git.js +++ b/lib/git.js @@ -71,5 +71,12 @@ module.exports = { getCurrentbranch(){ const command = 'git symbolic-ref --short -q HEAD'; return execSync(command); + }, + // 获取当前分支与远程仓库的差异 + getBranchDifferent(branch,origin){ + const command = `git log --pretty=format:"%s" ${branch} ^${origin}/${branch}` + console.log(command) + return execSync(command).toString(); + } } diff --git a/locales/en.json b/locales/en.json index 480943f..5ab728c 100644 --- a/locales/en.json +++ b/locales/en.json @@ -13,8 +13,8 @@ "select-env": "please select the environment where you want to set the Tag", "enter-version": "please enter the version number", "enter-tagMsg": "please enter tag description information", - "enter-repository": "Please enter the name of remote repository", - "enter-remotebranch": "Please enter the name of remote branch" + "enter-repository": "Please enter the name of remote repository" + }, "tip": { @@ -40,10 +40,10 @@ "last-tag": "The last tag of the __ENV__ environment is", "no-tag": "__ENV__ has not set tag", "push-tag": "Do you want to push your tag to remote repository?", - "push-origin":"Do you want to push your commit to remote repository?", - "push-confirm":"You will push from local branch __LOCALBRANCH__ to remote branch __REMOTE__ , are you sure?", "invalid-remote": "Invalid remote", "command-success": "Command commit is successful, and then you can push to the remote origin", - "config-existed": "the config file has exsited, do you want to cover?" + "config-existed": "the config file has exsited, do you want to cover?", + "branch":"Currently in branch" + } } diff --git a/locales/zh-CN.json b/locales/zh-CN.json index ca312e0..62c2ac4 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -13,8 +13,7 @@ "select-env": "请选择要打Tag的环境", "enter-version": "请输入版本号", "enter-tagMsg": "请输入Tag描述信息", - "enter-repository": "请输入远程仓库名称", - "enter-remotebranch": "请输入远程分支名称" + "enter-repository": "请输入远程仓库名称" }, "tip": { "enter-illegal": "输入的命令不合法, 请通过 dogit config --help 来查看可用命令", @@ -30,7 +29,6 @@ "success-plugin": "执行插件 __PLUGIN__ 成功", "error-plugin": "执行插件 __PLUGIN__ 出错,请检查配置", "fetch-origin": "正在fetch远程仓库...", - "fetch-success": "同步远程仓库成功", "recommend": "推荐", "msg-cannot-empty": "描述信息不能为空", @@ -40,10 +38,9 @@ "last-tag": "__ENV__ 环境的最近一次Tag为", "no-tag": "__ENV__ 尚未打过Tag", "push-tag": "是否将tag推送到远程仓库?", - "push-origin":"是否将commit推送到远程分支?", - "push-confirm":"将从本地__LOCALBRANCH__分支推送到远程__REMOTE__分支,确定?", "invalid-remote": "无效的remote", "command-success": "commit 成功,接下来你可以 push 到远端", - "config-existed": "配置文件已存在,是否要覆盖?" + "config-existed": "配置文件已存在,是否要覆盖?", + "branch":"当前位于分支" } } diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index bad6caf..98a18fc 100644 --- a/plugin/GitPush/index.js +++ b/plugin/GitPush/index.js @@ -1,5 +1,5 @@ const prompts = require('prompts'); -const { isGitRoot, fetchRemote, allRemotes, getCurrentbranch } = require('../../lib/git'); +const { isGitRoot, fetchRemote, allRemotes, getCurrentbranch,getBranchDifferent } = require('../../lib/git'); const { exec, execSync } = require('child_process'); const { echo } = require('../../lib/helper'); const ora = require('ora'); @@ -12,50 +12,26 @@ module.exports = class GitPushOrigin { this.handler = handler; } // 询问是否推送到远程分支 - async getPushOrigin() { + async getPushOrigin(origins) { const currentBranch = await getCurrentbranch(); const isPush = await prompts([ { - type: 'toggle', - name: 'isPush', - message: i18.__('tip.push-origin'), - initial: true, - active: 'yes', - inactive: 'no' - }, - { - type: prev => prev ? 'text' : null, - name: 'remotebranch', - message: i18.__("action.enter-remotebranch"), - initial: currentBranch - } - ], { - onCancel() { - process.exit(); - } - }); - if (!isPush.isPush) { - return; - } - const confirm = await prompts([ - { - type: 'toggle', - name: 'confirm', - message: i18.__('tip.push-confirm').replace(/__LOCALBRANCH__/,currentBranch).replace(/__REMOTE__/,isPush.remotebranch), - initial: true, - active: 'yes', - inactive: 'no' + type: 'text' , + name: 'remote', + message: `${i18.__("tip.branch")}${currentBranch},${i18.__("action.enter-repository")}`, + initial: origins[0], + validate: text => !origins.includes(text) ? i18.__("tip.invalid-remote") : true } ], { onCancel() { process.exit(); } - }); - if(!confirm.confirm){ - return; - } + }) + console.log(currentBranch,isPush.remote) + const difference = await getBranchDifferent(currentBranch,isPush.remote); + console.log('检测到本地有N个提交未同步到远程:',difference) return new Promise(resolve => { - const command = `git push origin ${isPush.remotebranch}` + const command = `git push ${isPush.remote}` exec(command, (error, stdout, stderr) => { if (error) { echo(stderr, 'info') @@ -72,6 +48,11 @@ module.exports = class GitPushOrigin { echo(i18.__('tip.not-git-root'), 'error'); return false; } + + const spinner = ora(i18.__("tip.fetch-origin")).start(); + await fetchRemote() + spinner.succeed(i18.__("tip.fetch-success")); + return true; } // 开始运行 @@ -79,7 +60,8 @@ module.exports = class GitPushOrigin { if (!await this.checkEnv()) { process.exit(); } - await this.getPushOrigin(); + const origins = await allRemotes(); + await this.getPushOrigin(origins); await this.handler(this.hook.after, this.params); }; From 0be7075868d07ba986f16f3a9d2e2c0add748dd0 Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Wed, 31 Mar 2021 14:12:55 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E7=A9=BA=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/git.js | 20 ++++++++++++-------- plugin/GitPush/index.js | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/git.js b/lib/git.js index b2cc2a7..b986f88 100644 --- a/lib/git.js +++ b/lib/git.js @@ -41,7 +41,7 @@ module.exports = { }); }) }, - + // 获取某个环境的最新tag latestEnvTag(tagPrefix) { const command = `git tag -l "${tagPrefix}*" --sort=-v:refname | head -n 1`; @@ -68,15 +68,19 @@ module.exports = { return `${matchs[1].replace(/\d+$/, (match) => parseInt(match) + 1)}${matchs[2] || ''}` }, // 查询当前分支名 - getCurrentbranch(){ + getCurrentbranch() { const command = 'git symbolic-ref --short -q HEAD'; - return execSync(command); + return execSync(command).toString().replace(/[\r\n]/g,""); }, // 获取当前分支与远程仓库的差异 - getBranchDifferent(branch,origin){ - const command = `git log --pretty=format:"%s" ${branch} ^${origin}/${branch}` - console.log(command) - return execSync(command).toString(); - + getBranchDifferent(branch) { + const fetchRemote = `git fetch origin ${branch}:branchSave` + exec(fetchRemote) + exec(`git merge ${branch}`) + const command = `git log --pretty=format:"%s" ${branch} ^branchSave` + console.log('aaaa',command) + console.log('===',exec(command).toString()) + return exec(command).toString(); + } } diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index 98a18fc..8890c84 100644 --- a/plugin/GitPush/index.js +++ b/plugin/GitPush/index.js @@ -27,20 +27,19 @@ module.exports = class GitPushOrigin { process.exit(); } }) - console.log(currentBranch,isPush.remote) - const difference = await getBranchDifferent(currentBranch,isPush.remote); + const difference = await getBranchDifferent(currentBranch); console.log('检测到本地有N个提交未同步到远程:',difference) - return new Promise(resolve => { - const command = `git push ${isPush.remote}` - exec(command, (error, stdout, stderr) => { - if (error) { - echo(stderr, 'info') - process.exit(); - } else { - resolve(); - } - }); - }) + // return new Promise(resolve => { + // const command = `git push ${isPush.remote}` + // exec(command, (error, stdout, stderr) => { + // if (error) { + // echo(stderr, 'info') + // process.exit(); + // } else { + // resolve(); + // } + // }); + // }) } // 验证环境 async checkEnv() { From cb3df5c7f46fb6af1f2afd3fdaf179f39f186944 Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Wed, 31 Mar 2021 18:03:36 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/git.js | 12 +++++++----- plugin/GitPush/index.js | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/git.js b/lib/git.js index b986f88..9fd6500 100644 --- a/lib/git.js +++ b/lib/git.js @@ -77,10 +77,12 @@ module.exports = { const fetchRemote = `git fetch origin ${branch}:branchSave` exec(fetchRemote) exec(`git merge ${branch}`) - const command = `git log --pretty=format:"%s" ${branch} ^branchSave` - console.log('aaaa',command) - console.log('===',exec(command).toString()) - return exec(command).toString(); - + const command = `git log --pretty=format:"%s" ${branch} "^branchSave"` + const commandHash = `git log --pretty=format:"%H" ${branch} "^branchSave"` + const num = execSync(commandHash) + return { + 'diff':execSync(command).toString(), + 'num':num.split('\n').length + }; } } diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index 8890c84..b7fdc95 100644 --- a/plugin/GitPush/index.js +++ b/plugin/GitPush/index.js @@ -28,7 +28,7 @@ module.exports = class GitPushOrigin { } }) const difference = await getBranchDifferent(currentBranch); - console.log('检测到本地有N个提交未同步到远程:',difference) + console.log(`检测到本地有${difference.num}个提交未同步到远程:/n`,difference.diff) // return new Promise(resolve => { // const command = `git push ${isPush.remote}` // exec(command, (error, stdout, stderr) => { From 26f1d018263881d1d323a52fddff38c38857a586 Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Thu, 1 Apr 2021 13:35:40 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/dogit.js | 2 +- lib/git.js | 9 ++++---- lib/helper.js | 5 +++-- locales/en.json | 1 + locales/zh-CN.json | 1 + plugin/GitPush/index.js | 46 +++++++++++++++++++++++++++-------------- 6 files changed, 41 insertions(+), 23 deletions(-) diff --git a/bin/dogit.js b/bin/dogit.js index 81f3d02..1569c77 100644 --- a/bin/dogit.js +++ b/bin/dogit.js @@ -1,4 +1,4 @@ -#! /usr/bin/env node +#! /usr/bin/env node const program = require('commander') const Init = require('../action/init') const I18 = require('../lib/i18'); diff --git a/lib/git.js b/lib/git.js index 9fd6500..a8b706a 100644 --- a/lib/git.js +++ b/lib/git.js @@ -75,14 +75,13 @@ module.exports = { // 获取当前分支与远程仓库的差异 getBranchDifferent(branch) { const fetchRemote = `git fetch origin ${branch}:branchSave` - exec(fetchRemote) - exec(`git merge ${branch}`) + execSync(fetchRemote) + execSync(`git merge branchSave`) const command = `git log --pretty=format:"%s" ${branch} "^branchSave"` const commandHash = `git log --pretty=format:"%H" ${branch} "^branchSave"` - const num = execSync(commandHash) return { - 'diff':execSync(command).toString(), - 'num':num.split('\n').length + 'diff':execSync(command).toString().trim(), + 'num':execSync(commandHash).toString().split('\n').length }; } } diff --git a/lib/helper.js b/lib/helper.js index 1c37ed6..d608ccf 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -9,7 +9,8 @@ module.exports = { const colorval = { success: 'green', error: 'red', - info: 'gray' + info: 'gray', + tip:'yellow' }[type] console.log(`\r\n ${colorval ? message[colorval] : message}\r\n`) }, @@ -52,4 +53,4 @@ module.exports = { } return key ? configData[key] : configData; } -} \ No newline at end of file +} diff --git a/locales/en.json b/locales/en.json index 5ab728c..88492a9 100644 --- a/locales/en.json +++ b/locales/en.json @@ -42,6 +42,7 @@ "push-tag": "Do you want to push your tag to remote repository?", "invalid-remote": "Invalid remote", "command-success": "Command commit is successful, and then you can push to the remote origin", + "push-success": "Command push is successful!", "config-existed": "the config file has exsited, do you want to cover?", "branch":"Currently in branch" diff --git a/locales/zh-CN.json b/locales/zh-CN.json index 62c2ac4..0e85845 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -40,6 +40,7 @@ "push-tag": "是否将tag推送到远程仓库?", "invalid-remote": "无效的remote", "command-success": "commit 成功,接下来你可以 push 到远端", + "push-success": "push成功!", "config-existed": "配置文件已存在,是否要覆盖?", "branch":"当前位于分支" } diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index b7fdc95..b742c61 100644 --- a/plugin/GitPush/index.js +++ b/plugin/GitPush/index.js @@ -1,5 +1,5 @@ const prompts = require('prompts'); -const { isGitRoot, fetchRemote, allRemotes, getCurrentbranch,getBranchDifferent } = require('../../lib/git'); +const { isGitRoot, fetchRemote, allRemotes, getCurrentbranch, getBranchDifferent } = require('../../lib/git'); const { exec, execSync } = require('child_process'); const { echo } = require('../../lib/helper'); const ora = require('ora'); @@ -16,7 +16,7 @@ module.exports = class GitPushOrigin { const currentBranch = await getCurrentbranch(); const isPush = await prompts([ { - type: 'text' , + type: 'text', name: 'remote', message: `${i18.__("tip.branch")}${currentBranch},${i18.__("action.enter-repository")}`, initial: origins[0], @@ -28,18 +28,32 @@ module.exports = class GitPushOrigin { } }) const difference = await getBranchDifferent(currentBranch); - console.log(`检测到本地有${difference.num}个提交未同步到远程:/n`,difference.diff) - // return new Promise(resolve => { - // const command = `git push ${isPush.remote}` - // exec(command, (error, stdout, stderr) => { - // if (error) { - // echo(stderr, 'info') - // process.exit(); - // } else { - // resolve(); - // } - // }); - // }) + if (difference.num > 0) { + echo(`检测到本地有${difference.num}个提交未同步到远程:\n${difference.diff}`,'tip') + const confirm = await prompts([ + { + type: 'toggle', + name: 'value', + message: '确定推送到远端?', + initial: true, + active: 'yes', + inactive: 'no' + } + ], { + onCancel() { + process.exit(); + } + }) + if (!confirm.value) { + process.exit(); + } + const command = `git push ${isPush.remote} ${currentBranch}` + return execSync(command); + } else { + echo('检测到本地没有提交未同步到远程','tip') + return + } + } // 验证环境 async checkEnv() { @@ -61,7 +75,9 @@ module.exports = class GitPushOrigin { } const origins = await allRemotes(); await this.getPushOrigin(origins); - await this.handler(this.hook.after, this.params); + echo(i18.__("tip.push-success"), 'info'); + return true; + }; } From 567ce0098ce3dbef95f714a78506041930b5b936 Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Thu, 1 Apr 2021 13:38:53 +0800 Subject: [PATCH 5/6] =?UTF-8?q?push=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/GitPush/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index b742c61..dcead6a 100644 --- a/plugin/GitPush/index.js +++ b/plugin/GitPush/index.js @@ -48,7 +48,12 @@ module.exports = class GitPushOrigin { process.exit(); } const command = `git push ${isPush.remote} ${currentBranch}` - return execSync(command); + return execSync(command,(error, stdout, stderr) => { + if (error) { + echo(stderr, 'info') + process.exit(); + } + }); } else { echo('检测到本地没有提交未同步到远程','tip') return From b6bc201a0d0493c1e27cbb94a82a6510f625d67b Mon Sep 17 00:00:00 2001 From: "yang.jie" <1360233308@qq.com> Date: Thu, 1 Apr 2021 13:45:19 +0800 Subject: [PATCH 6/6] =?UTF-8?q?docs:=20README=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/GitPush/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugin/GitPush/README.md b/plugin/GitPush/README.md index e69de29..de8b35d 100644 --- a/plugin/GitPush/README.md +++ b/plugin/GitPush/README.md @@ -0,0 +1,8 @@ +## 配置 +该插件用来将本地commit push到远程。 +```js +{ + 'plugin': 'GitPush' + +} +```