Skip to content

Commit

Permalink
feat(git): teach git more common tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Aug 23, 2016
1 parent 2b5e118 commit e1fcad6
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions git.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";
const exec = require("child_process").exec;
const path = require("path");
const async = require("marcosc-async");

function toExecPromise(cmd, timeout) {
if (!timeout) {
timeout = 40000;
timeout = 60000;
}
return new Promise((resolve, reject) => {
const id = setTimeout(() => {
Expand All @@ -24,4 +26,47 @@ function git(cmd) {
return toExecPromise(`git ${cmd}`);
}

module.exports = git;
git.getCurrentBranch = async(function*() {
const branch = yield git(`rev-parse --abbrev-ref HEAD`);
return branch.trim();
});

git.getConfigData = async(function*(configItem) {
let data;
try {
data = yield git(configItem);
} catch (err) {
data = "";
}
return data;
});

git.getBranches = async(function*() {
const rawBranches = yield git("branch --no-color");
const branches = rawBranches
.split("\n")
.map(branch => branch.replace("*", "").trim())
.reduce((collector, branch) => collector.add(branch), new Set());
return Array.from(branches);
});

git.hasBranch = async(function*(branch) {
const branches = yield git.getBranches();
return branches.includes(branch);
});

git.switchBranch = async(function*(branch) {
const hasBranch = yield git.hasBranch(branch);
if (!hasBranch) {
yield git(`checkout -b ${branch}`);
} else {
yield git(`checkout ${branch}`);
}
});

git.getRepoName = async(function*() {
const name = yield git("rev-parse --show-toplevel");
return path.basename(name).trim();
});

module.exports = git;

0 comments on commit e1fcad6

Please sign in to comment.