Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/dogit.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
18 changes: 15 additions & 3 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
});
})
},

// 获取某个环境的最新tag
latestEnvTag(tagPrefix) {
const command = `git tag -l "${tagPrefix}*" --sort=-v:refname | head -n 1`;
Expand All @@ -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
};
}
}
5 changes: 3 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
},
Expand Down Expand Up @@ -52,4 +53,4 @@ module.exports = {
}
return key ? configData[key] : configData;
}
}
}
11 changes: 6 additions & 5 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"

}
}
10 changes: 4 additions & 6 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"select-env": "请选择要打Tag的环境",
"enter-version": "请输入版本号",
"enter-tagMsg": "请输入Tag描述信息",
"enter-repository": "请输入远程仓库名称",
"enter-remotebranch": "请输入远程分支名称"
"enter-repository": "请输入远程仓库名称"
},
"tip": {
"enter-illegal": "输入的命令不合法, 请通过 dogit config --help 来查看可用命令",
Expand All @@ -30,7 +29,6 @@
"success-plugin": "执行插件 __PLUGIN__ 成功",
"error-plugin": "执行插件 __PLUGIN__ 出错,请检查配置",
"fetch-origin": "正在fetch远程仓库...",

"fetch-success": "同步远程仓库成功",
"recommend": "推荐",
"msg-cannot-empty": "描述信息不能为空",
Expand All @@ -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":"当前位于分支"
}
}
8 changes: 8 additions & 0 deletions plugin/GitPush/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## 配置
该插件用来将本地commit push到远程。
```js
{
'plugin': 'GitPush'

}
```
84 changes: 43 additions & 41 deletions plugin/GitPush/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -12,75 +12,77 @@ 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() {
if (!await isGitRoot()) {
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;
}
// 开始运行
async start() {
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;

};

}
Expand Down