Skip to content

Commit

Permalink
Enjoy
Browse files Browse the repository at this point in the history
  • Loading branch information
piuccio committed Apr 26, 2014
0 parents commit 38f2f05
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
tmp
coverage
13 changes: 13 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 Fabio Crisci

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# git-promise

Simple wrapper that allows you to run any `git` command using a more intuitive syntax.

## Getting Started

```shell
npm install git-promise --save
```

Once installed, you can use it in your JavaScript files like so:

```js
var git = require("git-promise");

git("rev-parse --abbrev-ref HEAD").then(function (branch) {
console.log(branch); // This is your current branch
});
```

The module will handle exit code automatically, so

```js
var git = require("git-promise");

git("merge origin/master").then(function () {
// Everything was fine
}).fail(function (err) {
// Something went bad, maybe merge conflict?
console.error(err);
});
```

## Advanced usage

The `git` command accepts a second parameter that can be used to parse the output or to deal with non 0 exit code.

```js
var git = require("git-promise");

git("status -sb", function (stdout) {
return stdout.match(/## (.*)/)[1];
}).then(function (branch) {
console.log(branch); // This is your current branch
});
```

The callback accepts 2 parameters, `(stdout, code)`, where `stdout` is the output of the git command and `code` is the exit code.

The return value of this function will be the resolved value of the promise.

If the `code` parameter is not specified, it'll be handled automatically and the promise will be rejected in case of non 0 code.

```js
var git = require("git-promise");

git("merge-base --is-ancestor master HEAD", function (stdout, code) {
if (code === 0) {
// the branch we are on is fast forward to master
return true;
} else if (code === 1) {
// no, it's not
return false;
} else {
// some other error happened
throw new Error("Something bad happened: " + stdout);
}
}).then(function (isFastForward) {
console.log(isFastForward);
}).fail(function (err) {
// deal with the error
});
```


### Chaining commands

Imagine to be on a local branch which is not fast forward with master and you want to know which commit were pushed on master after the forking point:

```js
var git = require("git-promise");

function findForkCommit () {
return git("merge-base master HEAD", function (output) {
return output.trim();
});
}

function findChanges (forkCommit) {
return git("log " + forkCommit + "..master --format=oneline", function (output) {
return output.trim().split("\n");
});
}

// synchronization can be done in many ways, for instance with Q
var Q = require("q");
[findForkCommit, findChanges].reduce(Q.when, Q({})).then(function (commits) {
console.log(commits);
});

// or simply using promises, simple cases only?
findForkCommit().then(findChanges).then(function (commits) {
console.log(commits);
});
```

## Utility methods

This module comes with some utility methods to parse the output of some git commands

```js
var util = require("git-promise/util");
```

* `util.extractStatus(output [, lineSeparator])`

Parse the output of `git status --procelain` and returns an object with

```
{
branch: "current branch name, only if git status -b is used",
index: {
modified: ["list of files modified in the index"],
added: ["list of files added in the index"],
deleted: ["list of files deleted in the index"],
renamed: ["list of files renamed in the index"],
copied: ["list of files copied in the index"]
},
workingTree: {
modified: ["list of files modified in the local working tree"],
added: ["list of files added / renamed / copied in the local working tree"],
deleted: ["list of files deleted in the local working tree"]
}
}
```

The method works both with or without option `-z`.

* `util.hasConflict(output)`

Try to determine if there's a merge conflict from the output of `git merge-tree`

```js
var git = require("git-promise");
var util = require("git-promise/util");

git("merge-tree <root-commit> <branch1> <branch2>").then(function (stdout) {
console.log(util.hasConflict(stdout));
});
```

## Release History

* 0.1.0 Just started
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var shell = require("shelljs");
var Q = require("q");

module.exports = function (command, callback) {
var deferred = Q.defer();

if (command.substring(0, 4) !== "git ") {
command = "git " + command;
}
shell.exec(command, {silent: true}, function (code, output) {
var args;
if (!callback) {
// If we completely ignore the command, resolve with the command output
callback = function (stdout) {
return stdout;
};
}

if (callback.length === 1) {
// Automatically handle non 0 exit codes
if (code !== 0) {
var error = new Error("'" + command + "' exited with error code " + code);
error.stdout = output;
return deferred.reject(error);
}
args = [output];
} else {
// This callback is interested in the exit code, don't handle exit code
args = [output, code];
}

try {
deferred.resolve(callback.apply(null, args));
} catch (ex) {
deferred.reject(ex);
}
});
return deferred.promise;
};
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "git-promise",
"description": "Simple wrapper to run any git command and process it's output using promises.",
"version": "0.1.0",
"homepage": "https://github.com/piuccio/git-promise",
"author": {
"name": "Fabio Crisci",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "git://github.com/piuccio/git-promise.git"
},
"bugs": {
"url": "https://github.com/piuccio/git-promise/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/piuccio/git-promise/blob/master/LICENSE"
}
],
"main": "./index.js",
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "nodeunit test",
"cover": "istanbul cover node_modules/nodeunit/bin/nodeunit -- test"
},
"keywords": [
"git",
"shell"
],
"dependencies": {
"q": "~1.0.1",
"shelljs": "~0.2.6"
},
"devDependencies": {
"nodeunit": "~0.8.6"
}
}
Loading

0 comments on commit 38f2f05

Please sign in to comment.