Skip to content

Commit

Permalink
Merge pull request #11 from relekang/fix/add-support-for-branch-as-ar…
Browse files Browse the repository at this point in the history
…gument

feat: Add support for setting branch as an option
  • Loading branch information
relekang committed Apr 18, 2016
2 parents 367888a + d8affe1 commit e2c4466
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ cat checkstyle-report.xml | lint-filter
eslint . -f checkstyle | lint-filter
```

### Options

```
$ lint-filter -h
Usage: lint-filter [options] <file ...>
Options:
-h, --help output usage information
-V, --version output the version number
-f, --format [format] The output format
-b, --branch [branch] The branch to diff against
```

## Contributing
Firstly, all contributions is super appreciated :sparkles:

Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export default async function main() {
.version(info.version)
.usage('[options] <file ...>')
.option('-f, --format [format]', 'The output format', 'text')
.option('-b, --branch [branch]', 'The branch to diff against')
.parse(process.argv)

const diff = await getDiffInformation()
const diff = await getDiffInformation(program)

if (program.args.length === 0) {
const input = await new Promise(resolve => stdin(resolve))
Expand Down
4 changes: 2 additions & 2 deletions src/utils/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function parseFullDiff(diff) {
, {})
}

export async function getDiffInformation(hash) {
const diffAgainst = hash || await exports.execFile('git', ['merge-base', 'origin/master', 'HEAD'])
export async function getDiffInformation({ branch = 'origin/master', hash } = {}) {
const diffAgainst = hash || await exports.execFile('git', ['merge-base', branch, 'HEAD'])
return parseFullDiff(await exports.execFile('git', ['diff', diffAgainst.trim()]))
}
55 changes: 43 additions & 12 deletions test/utils/git_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,46 @@ test(
t => t.deepEqual(gitUtils.parseDiffRanges('+@@ -8,27 +8,43 @@'), [])
)

test('getDiffInformation(hash) should return object with diff ranges for all files', async (t) => {
t.plan(1)
t.context.sandbox.stub(gitUtils, 'execFile').returns(Promise.resolve(diffFixture))
const diff = await gitUtils.getDiffInformation('1f2d836')

t.deepEqual(diff, {
'lint-filter.js': [[1, 3], [2, 5]],
'src/index.js': [[4, 17]],
'src/utils.js': [[3, 44], [45, 52], [60, 67]],
'test/utils_tests.js': [[40, 65], [74, 122]],
})
})
test.serial(
'getDiffInformation({branch, hash}) should return object with diff ranges for all files',
async (t) => {
t.plan(1)
t.context.sandbox.stub(gitUtils, 'execFile').returns(Promise.resolve(diffFixture))
const diff = await gitUtils.getDiffInformation()

t.deepEqual(diff, {
'lint-filter.js': [[1, 3], [2, 5]],
'src/index.js': [[4, 17]],
'src/utils.js': [[3, 44], [45, 52], [60, 67]],
'test/utils_tests.js': [[40, 65], [74, 122]],
})
}
)

test.serial(
'getDiffInformation({branch, hash}) should use origin/master as default',
async (t) => {
t.plan(1)
t.context.sandbox.stub(gitUtils, 'execFile').returns(Promise.resolve(diffFixture))
await gitUtils.getDiffInformation({})

t.deepEqual(
gitUtils.execFile.getCall(0).args,
['git', ['merge-base', 'origin/master', 'HEAD']]
)
}
)

test.serial(
'getDiffInformation({branch, hash}) should use custom branch when specified',
async (t) => {
t.plan(1)
t.context.sandbox.stub(gitUtils, 'execFile').returns(Promise.resolve(diffFixture))
await gitUtils.getDiffInformation({ branch: 'origin/production' })

t.deepEqual(
gitUtils.execFile.getCall(0).args,
['git', ['merge-base', 'origin/production', 'HEAD']]
)
}
)

0 comments on commit e2c4466

Please sign in to comment.