Skip to content
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

[WIP] support --contents #8

Open
wants to merge 1 commit 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
61 changes: 38 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Shelling out to [git blame](http://git-scm.com/docs/git-blame) in a streaming No
## Usage

```js
gitBlame(repoPath, options)
gitBlame(file, options)
```

Example:
Expand All @@ -20,8 +20,8 @@ var repoPath = path.resolve(process.env.REPO || (__dirname + '/.git'));
var file = process.env.FILE || 'package.json';
var rev = process.env.REV || 'HEAD';

gitBlame(repoPath, {
file: file,
gitBlame(file, {
repoPath: repoPath,
rev: rev
}).on('data', function(type, data) {
// type can be 'line' or 'commit'
Expand Down Expand Up @@ -168,43 +168,58 @@ line { hash: '856f13ab053f6b5dfa58d6e6c726d43cc5e73d00',
That's all, folks!
```

## Parameters:

- `file` - `String` - `<file>` in `git blame`
- `options` - `Object` - optional options

## Options

The options should be an `object`.
The options should be an `object` extend from [git-spawned-stream](https://github.com/gucong3000/git-spawned-stream/tree/options.input#options)

### `rev` `{String}`

### `rev` (`Boolean` or `String`)
`<rev>` from `git blame`. If empty it will default to `HEAD`. If `false` and `workTree` is set it will use the work tree.

### `workTree` (`String`)
`--work-tree` from `git`. If empty no work tree will be used. Use full path.
### `ignoreWhitespace` `{boolean}`

Default: `true`

### `ignoreWhitespace` (`Boolean`)
`-w` from `git blame`.

### `limitLines` (`String`)
### `limitLines` `{String}`

`-L` from `git blame`.

### `detectMoved` (`Boolean` or `Number`)
### `contents` `{String}`

`--contents <file>` from `git blame`

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

`--contents -` from `git blame`

### `detectMoved` `{boolean|number}`

Default: `true`

`-M` from `git blame`. Requiered for `detectCopy`.

### `detectCopy` (`Boolean` or `Number`)
`-C` from `git blame`.
### `detectCopy` `{boolean|number}`

### `detectCopyMode` (`String`)
Possible options:
* `any` - Look in all files and at all times
* `created` - Look in files changed in the commit creating the file
* `default` - Look in the same commit
Default: `true`

If left empty it will default to `default`.
`-C` from `git blame`.

### `detectCopyMode` `{String}`

### `file` (`String`)
`<file>` in `git blame`.
Possible options:

## gitCommand
- `any` - Look in all files and at all times
- `created` - Look in files changed in the commit creating the file
- `default` - Look in the same commit

This is an optional 3rd parameter besides the repo path and options.
It's the path to the git binary to use (use the one in `PATH` by default).
If left empty it will default to `default`.

## Tests

Expand Down
6 changes: 3 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ var path = require('path');

var repoPath = path.resolve(process.env.REPO || (__dirname + '/.git'));
var file = process.env.FILE || 'package.json';
var rev = process.env.REV || 'HEAD';
var rev = process.env.REV;

gitBlame(repoPath, {
file: file,
gitBlame(file, {
repoPath: repoPath,
rev: rev
}).on('data', function(type, data) {
// type can be 'line' or 'commit'
Expand Down
47 changes: 34 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,40 @@
var gitSpawnedStream = require('git-spawned-stream');
var streamingParser = require('./lib/parser');

function blame(repoPath, opts, gitCommand = 'git') {
var rev = typeof opts.rev !== 'undefined' ? opts.rev : 'HEAD';
var args = [];
/**
* Create a readable stream from a spawned git process.
* @param {String} file `<file>` in `git blame`
* @param {Object} [options] options extend from [git-spawned-stream](https://github.com/gucong3000/git-spawned-stream/tree/options.input#options)
* @param {String} [rev] `<rev>` from `git blame`
* @param {boolean} [ignoreWhitespace=true] `-w` from `git blame`
* @param {String} [limitLines] `-L` from `git blame`
* @param {String} [contents] `--contents <file>` from `git blame`
* @param {String|Buffer|Stream.Readable} [input] `--contents -` from `git blame`
* @param {boolean|number} [detectMoved=true] `-M` from `git blame`. Requiered for `detectCopy`
* @param {boolean|number} [detectCopy=true] `-C` from `git blame`
* @param {String} [detectCopyMode] Possible options:
- `any` - Look in all files and at all times
- `created` - Look in files changed in the commit creating the file
- `default` - Look in the same commit
* @returns {Stream.Readable} readable stream from spawned git process.
*/
function blame(file, opts) {
opts = Object.assign({
ignoreWhitespaces: true,
detectMoved: true,
detectCopy: true,
}, opts)

if (typeof opts.workTree === 'string') {
args.push('--work-tree=' + opts.workTree);
}

args.push('blame');
var args = ['blame'];

if (rev) {
args.push(rev);
if (opts.rev) {
args.push(opts.rev);
} else if (opts.input) {
args.push('--contents');
args.push('-');
} else if (opts.contents) {
args.push('--contents');
args.push(opts.contents);
}

if (opts.ignoreWhitespaces) {
Expand Down Expand Up @@ -58,10 +80,9 @@ function blame(repoPath, opts, gitCommand = 'git') {

args.push('-p');
args.push('--');
args.push(opts.file);
args.push(file);

// TODO: implement limit
return streamingParser(gitSpawnedStream(repoPath, args, gitCommand));
return streamingParser(gitSpawnedStream(args, opts));
}

module.exports = blame;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Shelling out to git blame in a streaming Node fashion.",
"main": "index.js",
"dependencies": {
"git-spawned-stream": "1.0.0",
"debug": "~2.2.0",
"git-spawned-stream": "github:gucong3000/git-spawned-stream#options.input",
"split-transform-stream": "~1.0.0"
},
"devDependencies": {
Expand Down
15 changes: 10 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ describe('git-blame', function() {

it('should delegate with the correct params', function(done) {
var repoPath = '/home/node.git';
var opts = { rev: 'master', file: 'CHANGES.md' };
var opts = {
ignoreWhitespaces: false,
detectMoved: false,
detectCopy: false,
repoPath: repoPath,
};

var gitBlame = proxyquire.load('../', {
'./lib/parser': function(inputStream) {
inputStream.should.eql('git-spawned-stream');

return 'streamingParser';
},
'git-spawned-stream': function(path, args) {
path.should.eql(repoPath);
args.should.eql(['blame', opts.rev, '-p', '--', opts.file]);
'git-spawned-stream': function(args, configs) {
configs.repoPath.should.eql(repoPath);
args.should.eql(['blame', '-p', '--', 'CHANGES.md']);

return 'git-spawned-stream';
}
});

gitBlame(repoPath, opts).should.eql('streamingParser');
gitBlame('CHANGES.md', opts).should.eql('streamingParser');

done();
});
Expand Down