Skip to content

Commit

Permalink
feat(git): ✨check remote differ to info users to pull first; check lo…
Browse files Browse the repository at this point in the history
…cal status to decide i
  • Loading branch information
vivaxy committed Nov 27, 2016
1 parent 707750d commit 7bca53c
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 54 deletions.
58 changes: 49 additions & 9 deletions commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,61 @@
* @author vivaxy
*/

import isAGitRepository from '../lib/isAGitRepository';
import isGitStatusClean from '../lib/isGitStatusClean';
import Listr from 'listr';

import isGitRepository from '../git/status/isGitRepository';
import gitClean from '../git/status/gitClean';
import needGitPush from '../git/status/needGitPush';
import * as console from '../lib/console';
import acp from '../lib/acp';
import gitAdd from '../git/commands/gitAdd';
import gitCommit from '../git/commands/gitCommit';
import gitPush from '../git/commands/gitPush';
import prompt from '../lib/prompt';

export default async() => {
if (!isAGitRepository()) {
const needGitAddOrCommit = !gitClean;

const prepare = () => {
if (!isGitRepository) {
console.error(`not a git repository`);
process.exit(1);
}
if (isGitStatusClean()) {
console.error(`nothing to commit, working tree clean`);
process.exit(1);
if (!needGitAddOrCommit) {
if (!needGitPush) {
console.error(`nothing to commit, working tree clean`);
process.exit(1);
}
gitPush();
process.exit(0);
}
};

export default async() => {

prepare();

const commitMessage = await prompt();
acp(commitMessage);

const listr = new Listr([
{
title: `git add`,
task: gitAdd,
},
{
title: `git commit`,
task: () => {
return gitCommit(commitMessage);
},
},
{
title: `git push`,
task: gitPush,
}
]);

try {
await listr.run();
} catch (ex) {
console.error(ex);
}

};
14 changes: 14 additions & 0 deletions git/commands/gitAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @since 2016-11-27 14:28
* @author vivaxy
*/

import sh from 'shelljs';

import * as console from '../../lib/console';

export default () => {
const addCommand = `git add .`;
console.info(addCommand);
sh.exec(addCommand);
};
14 changes: 14 additions & 0 deletions git/commands/gitCommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @since 2016-11-27 14:28
* @author vivaxy
*/

import sh from 'shelljs';

import * as console from '../../lib/console';

export default (commitMessage) => {
const commitCommand = `git commit -m "${commitMessage}"`;
console.info(commitCommand);
sh.exec(commitCommand);
};
25 changes: 25 additions & 0 deletions git/commands/gitPush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @since 2016-11-22 21:20
* @author vivaxy
*/

import sh from 'shelljs';

import * as console from '../../lib/console';
import gitRemote from '../status/gitRemote';
import gitBranch from '../status/gitBranch';
import getGitRemoteDiffer from '../status/getGitRemoteDiffer';

export default () => {
// do not push when remove not exists
if (gitRemote) {

if (getGitRemoteDiffer()) {
throw new Error(`remote differ, please pull changes`);
}

const pushCommand = `git push ${gitRemote} ${gitBranch} --tag`;
console.info(pushCommand);
sh.exec(pushCommand);
}
};
10 changes: 10 additions & 0 deletions git/status/getGitRemoteDiffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @since 2016-11-27 14:52
* @author vivaxy
*/

import getInfoFromShell from '../../lib/getInfoFromShell';

export default () => {
return getInfoFromShell(`git rev-list --count --left-only @{u}...HEAD`) !== `0`;
};
8 changes: 8 additions & 0 deletions git/status/gitBranch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @since 2016-11-27 14:02
* @author vivaxy
*/

import getInfoFromShell from '../../lib/getInfoFromShell';

export default getInfoFromShell(`git symbolic-ref --short HEAD`);
8 changes: 8 additions & 0 deletions git/status/gitClean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @since 2016-11-27 13:49
* @author vivaxy
*/

import getInfoFromShell from '../../lib/getInfoFromShell';

export default getInfoFromShell(`git status -s`) === ``;
8 changes: 8 additions & 0 deletions git/status/gitRemote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @since 2016-11-27 14:02
* @author vivaxy
*/

import getInfoFromShell from '../../lib/getInfoFromShell';

export default getInfoFromShell(`git remote`);
6 changes: 4 additions & 2 deletions lib/isAGitRepository.js → git/status/isGitRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import path from 'path';
import sh from 'shelljs';

import getInfoFromShell from './getInfoFromShell';
import getInfoFromShell from '../../lib/getInfoFromShell';

const cwd = process.cwd();

export default () => {
const check = () => {
if (sh.test(`-d`, path.join(cwd, `.git`))) {
if (getInfoFromShell(`git rev-parse --is-inside-work-tree`) === `true`) {
return true;
}
}
return false;
};

export default check();
37 changes: 37 additions & 0 deletions git/status/needGitPush.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @since 2016-11-27 13:59
* @author vivaxy
*/

import sh from 'shelljs';

import gitBranch from './gitBranch';
import gitRemote from './gitRemote';

const shellSilentConfig = {
silent: true,
};

const hasCommitInShell = (command) => {
let result = true;
const shellExec = sh.exec(command, shellSilentConfig);
if (shellExec.code === 0) {
const splitResult = shellExec.stdout.split(`\n\r`);
if (splitResult[0] === ``) {
result = false;
}
}
return result;
};

const needPush = () => {
let result = true;
if (!gitRemote) {
result = false;
} else {
result = hasCommitInShell(`git log ${gitRemote}/${gitBranch}..${gitBranch} --pretty=format:%H`);
}
return result;
};

export default needPush();
33 changes: 0 additions & 33 deletions lib/acp.js

This file was deleted.

4 changes: 4 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export const info = (...args) => {
export const error = (...args) => {
console.log(chalk.red(...args));
};

export const log = (...args) => {
console.log(chalk.green(...args));
};
10 changes: 0 additions & 10 deletions lib/isGitStatusClean.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"chalk": "^1.1.3",
"conventional-commit-types": "^2.1.0",
"inquirer": "^1.2.3",
"listr": "^0.8.0",
"minimatch": "^3.0.3",
"right-pad": "^1.0.1",
"shelljs": "^0.7.5",
Expand Down

0 comments on commit 7bca53c

Please sign in to comment.