Skip to content

Update API #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "standard",
"plugins": [
"standard",
"promise"
],
"rules": {
"semi": [
"error",
"always"
]
}
}
60 changes: 59 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

23 changes: 23 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
*.log
*.pid
*.seed
.editorconfig
.eslintrc*
.eslintignore
.gitignore
.grunt
.lock-wscript
.node_repl_history
.stylelintrc*
.travis.yml
.vscode
.nyc_output
appveyor.yml
coverage
gulpfile.js
lib-cov
logs
node_modules
npm-debug.log*
pids
test
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sudo: false
language: node_js
node_js:
- "stable"
- "6"
- "4"
before_install:
- npm i npm@latest -g
65 changes: 53 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,73 @@ Create a readable stream from a spawned git process.
## Usage

```js
gitSpawnedStream(repoPath, spawnArguments, limitInBytes, gitBinary)
gitSpawnedStream(args, options)
```

Arguments:
### Parameters:

- `repoPath` - the path to the repo, ex: /home/alex/node/.git (or the path to the git bare repo)
- `spawnArguments` - the arguments that will be passed to the `child_process.spawn` function
- `limitInBytes` - kill the process if it exceeds the imposed limit (sends more data than allowed)
- `gitBinary` - path to the git binary to use (use the one in `PATH` by default)
- `args` - `{string[]}` - the arguments that will be passed to the `child_process.spawn` function
- `options` - `{Object}` - optional options

Example:
### Options:

#### `config` `{Object}`

`-c` from `git`, key-value pairs

#### `gitDir` `{string}`

`--git-dir` from `git`.

#### `workTree` `{string}`

`--work-tree` from `git`

#### `pager` `{boolean}`

Default: `false`

`--no-pager` from `git`.

#### `gitBinary` `{string}`

'git' path to the git binary to use

#### `limit` `{number}`

kill the process if it exceeds the imposed limit (sends more data than allowed)

#### `input` `{string | Buffer | Stream.Readable}`

The value which will be passed as stdin to the spawned process

#### `cwd` `string`

Current working directory of git process

#### `env` `Object`

Environment key-value pairs

## Example:

```js
var gitSpawnedStream = require('git-spawned-stream');
var path = require('path');
var repoPath = process.env.REPO || path.join(__dirname, '.git');
repoPath = path.resolve(repoPath);
var byteLimit = 5 * 1024 * 1024; // 5 Mb
var gitDir = process.env.REPO || path.join(__dirname, '.git');
gitDir = path.resolve(gitDir);
var limit = 5 * 1024 * 1024; // 5 Mb

// sort of a git log -n 2
var stream = gitSpawnedStream(repoPath, [
var stream = gitSpawnedStream([
'rev-list',
'--max-count=2',
'--header',
'HEAD'
], byteLimit);
], {
gitDir: gitDir,
limit: limit
});

stream.on('data', function(data) {
console.log('DATA', data.toString('utf8'));
Expand Down
20 changes: 11 additions & 9 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"use strict";
'use strict';

var gitSpawnedStream = require('./');
var path = require('path');
var repoPath = process.env.REPO || path.join(__dirname, '.git');
repoPath = path.resolve(repoPath);
var byteLimit = 5 * 1024 * 1024; // 5 Mb
var gitDir = process.env.REPO ? path.resolve(process.env.REPO) : path.join(__dirname, '.git');
var limit = 5 * 1024 * 1024; // 5 Mb

// sort of a git log -n 2
var stream = gitSpawnedStream(repoPath, [
var stream = gitSpawnedStream([
'rev-list',
'--max-count=2',
'--header',
'HEAD'
], byteLimit);
], {
gitDir: gitDir,
limit: limit
});

stream.on('data', function(data) {
stream.on('data', function (data) {
console.log('DATA', data.toString('utf8'));
}).on('error', function(err) {
}).on('error', function (err) {
console.error('An error occurred:');
console.error('-----------------\n');
console.error(err.message);
process.exit(1);
}).on('end', function() {
}).on('end', function () {
console.log("\n±±±±±±±±±±±±±±±±±\nThat's all folks!");
});
72 changes: 60 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,74 @@
"use strict";
'use strict';

/**
* git-spawned-stream
* @module git-spawned-stream
*/

var run = require('spawn-to-readstream');
var spawn = require('child_process').spawn;
var debug = require('debug')('git-spawned-stream');

module.exports = function(repoPath, args, limit, gitCommand = 'git') {
var _args = ['--git-dir=' + repoPath];
/**
* Create a readable stream from a spawned git process.
* @param {string[]} args the arguments that will be passed to the `child_process.spawn` function
* @param {Object} [options] options
* @param {Object} [options.config] `-c` from `git`
* @param {string} [options.gitDir] `--git-dir` from `git`
* @param {string} [options.workTree] `--work-tree` from `git`
* @param {boolean} [options.pager=false] `--no-pager` from `git`
* @param {string} [options.gitBinary='git'] path to the git binary to use
* @param {number} [options.limit] kill the process if it exceeds the imposed limit (sends more data than allowed)
* @param {string|Buffer|Stream.Readable} [options.input] The value which will be passed as stdin to the spawned process
* @param {string} [options.cwd] Current working directory of git process
* @param {Object} [options.env] Environment key-value pairs
* @returns {Stream.Readable} readable stream from spawned git process.
*/
module.exports = function (args, options) {
options = Object.assign({
gitBinary: 'git'
}, options);

var _args = [];

if (!options.pager) {
_args.push('--no-pager');
}

if (options.gitDir) {
_args.push('--git-dir=' + options.gitDir);
}

// The limit is a git bin path
if (typeof limit === 'string') {
debug('got string limit, using it as gitCommand and unsetting limit');
gitCommand = limit;
limit = undefined;
if (options.workTree) {
_args.push('--work-tree=' + options.workTree);
}

args.forEach(function(item) {
if (options.config) {
Object.keys(options.config).forEach(function (key) {
_args.push('-c', key + '=' + String(options.config[key]));
});
}

args.forEach(function (item) {
_args.push(item);
});

debug('args', _args);
debug('limit', limit);
debug('gitCommand', gitCommand);
debug('limit', options.limit);
debug('gitBinary', options.gitBinary);

return run(spawn(gitCommand, _args), limit);
var ps = spawn(options.gitBinary, _args, {
cwd: options.cwd,
env: options.env
});

if (options.input) {
if (options.input.pipe) {
options.input.pipe(ps.stdin);
} else {
ps.stdin.write(options.input);
ps.stdin.end();
}
}
return run(ps, options.limit);
};
27 changes: 21 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
{
"name": "git-spawned-stream",
"nyc": {
"cache": true,
"reporter": [
"lcov",
"text"
]
},
"version": "1.0.0",
"description": "Create a readable stream from a spawned git process.",
"main": "index.js",
"dependencies": {
"spawn-to-readstream": "~0.1.3",
"debug": "~0.8.1"
"spawn-to-readstream": "^0.1.3",
"debug": "^2.6.8"
},
"devDependencies": {
"mocha": "~1.19.0",
"proxyquire": "~1.0.0",
"should": "~3.3.2"
"eslint": "^4.0.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-node": "^5.0.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^3.4.2",
"nyc": "^11.0.2",
"proxyquire": "^1.8.0",
"should": "^11.2.1"
},
"scripts": {
"test": "mocha"
"pretest": "eslint *.js",
"test": "nyc mocha"
},
"repository": {
"type": "git",
Expand Down
Loading