Skip to content

Commit 4891db7

Browse files
committed
Initial commit
0 parents  commit 4891db7

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Alexander Myshov
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# gulp-gitflow-diff
2+
Gulp plugin for fitlering files that differ from some base git branch
3+
4+
This plugin will be useful when you have a pretty large project with adopted git-flow/github-flow organization of development process. And for example you want to lint files on git push hook. Why do you should lint every single file only several of them actuall have been changed? At this moment this plugin will help you. Because it filter only those files that actually have been changed relatively base branch (usually `origin/master`).
5+
6+
# Installation
7+
8+
```sh
9+
$ npm install gulp-gitflow-diff --save-dev
10+
```
11+
12+
# Usage
13+
14+
```js
15+
var gulp = require('gulp');
16+
var gulpPrefixer = require('./gulp-prefixer');
17+
18+
19+
gulp.task('default', function () {
20+
return gulp.src('./src/**/*.js')
21+
.pipe(gulpGitflowDiff({baseBranch: 'master'}))
22+
.pipe(gulp.dest('dest'));
23+
});
24+
```
25+
26+
# License
27+
28+
MIT

index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
var gutil = require('gulp-util');
3+
var through = require('through2');
4+
var execSync = require('child_process').execSync;
5+
6+
function diffBranches(options) {
7+
var filesChanged = [];
8+
9+
if (!options.baseBranch) {
10+
throw new gutil.PluginError('gulp-gitflow-diff', 'baseBranch param is required');
11+
}
12+
13+
var cmd = 'git diff --name-only ' + options.baseBranch + '..HEAD';
14+
filesChanged = execSync(cmd, {encoding: 'utf8'});
15+
filesChanged = filesChanged.split("\n");
16+
// last entry is just empty string
17+
filesChanged.pop();
18+
19+
return through.obj(
20+
function (file, enc, cb) {
21+
if (isFileChanged(file, filesChanged)) {
22+
this.push(file);
23+
}
24+
cb();
25+
}
26+
);
27+
};
28+
29+
function isFileChanged(file, filesChanged) {
30+
var currentFile = file.path.substr(process.cwd().length + 1);
31+
if (filesChanged.indexOf(currentFile) != -1) {
32+
return true;
33+
} else {
34+
return false;
35+
}
36+
}
37+
38+
module.exports = diffBranches;
39+
module.exports.isFileChanged = isFileChanged;

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "gulp-gitflow-diff",
3+
"version": "0.1.0",
4+
"description": "Gulp plugin for fitlering files that differ from some base git branch",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "./node_modules/mocha/bin/mocha"
8+
},
9+
"author": {
10+
"name" : "Alexander Myshov",
11+
"url": "https://github.com/myshov"
12+
},
13+
"license": "MIT",
14+
"dependencies": {
15+
"gulp-util": "^3.0.7",
16+
"through2": "^2.0.1"
17+
},
18+
"devDependencies": {
19+
"mocha": "^2.4.5",
20+
"rewire": "^2.5.1"
21+
},
22+
"engines": {
23+
"node": ">=0.12.0"
24+
},
25+
"keywords": [
26+
"gulpplugin", "git", "filtering"
27+
]
28+
}

test/isFileChanged.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var assert = require('assert');
2+
var rewire = require('rewire');
3+
4+
var gulpGitFlowDiff = rewire('../');
5+
gulpGitFlowDiff.__set__({
6+
process: {
7+
cwd: function() {
8+
return '/home/user/dev'
9+
}
10+
}
11+
});
12+
13+
describe('gulp-giflow-diff isFileChanged', function() {
14+
process.cwd = function() {
15+
return '/home/user/dev'
16+
};
17+
18+
it('should return true if there is file in list of changed files', function(done) {
19+
var fileMocked = {};
20+
fileMocked.path = '/home/user/dev/src/file33.js';
21+
var filesChanged = [
22+
'src/file11.js',
23+
'src/file22.js',
24+
'src/file33.js'
25+
];
26+
var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged);
27+
assert.equal(result, true);
28+
done();
29+
});
30+
31+
it('should return false if there is no file in list of changed files', function(done) {
32+
var fileMocked = {};
33+
fileMocked.path = '/usr/home/user/dev/src/file44.js';
34+
var filesChanged = [
35+
'src/file11.js',
36+
'src/file22.js',
37+
'src/file33.js'
38+
];
39+
var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged);
40+
assert.equal(result, false);
41+
done();
42+
});
43+
});

0 commit comments

Comments
 (0)