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 131a532..a8b706a 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,8 +68,20 @@ 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) { + const fetchRemote = `git fetch origin ${branch}:branchSave` + execSync(fetchRemote) + execSync(`git merge branchSave`) + const command = `git log --pretty=format:"%s" ${branch} "^branchSave"` + const commandHash = `git log --pretty=format:"%H" ${branch} "^branchSave"` + return { + '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 480943f..88492a9 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,11 @@ "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?" + "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 ca312e0..0e85845 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,10 @@ "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": "配置文件已存在,是否要覆盖?" + "push-success": "push成功!", + "config-existed": "配置文件已存在,是否要覆盖?", + "branch":"当前位于分支" } } 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' + +} +``` diff --git a/plugin/GitPush/index.js b/plugin/GitPush/index.js index bad6caf..dcead6a 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,59 +12,53 @@ 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 + 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 (!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' - } - ], { - onCancel() { + }) + const difference = await getBranchDifferent(currentBranch); + 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(); } - }); - if(!confirm.confirm){ - return; - } - return new Promise(resolve => { - const command = `git push origin ${isPush.remotebranch}` - exec(command, (error, stdout, stderr) => { + const command = `git push ${isPush.remote} ${currentBranch}` + return execSync(command,(error, stdout, stderr) => { if (error) { echo(stderr, 'info') process.exit(); - } else { - resolve(); } }); - }) + } else { + echo('检测到本地没有提交未同步到远程','tip') + return + } + } // 验证环境 async checkEnv() { @@ -72,6 +66,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,8 +78,11 @@ module.exports = class GitPushOrigin { if (!await this.checkEnv()) { process.exit(); } - await this.getPushOrigin(); - await this.handler(this.hook.after, this.params); + const origins = await allRemotes(); + await this.getPushOrigin(origins); + echo(i18.__("tip.push-success"), 'info'); + return true; + }; }