Skip to content
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
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var gutil = require('gulp-util');
var through = require('through2');
var execSync = require('child_process').execSync;
var path = require('path');

function diffBranches(options) {
var filesChanged = [];
Expand All @@ -11,13 +12,19 @@ function diffBranches(options) {
}

var cmd = 'git diff --name-only $(git merge-base ' + options.baseBranch + ' HEAD)..HEAD';
filesChanged = execSync(cmd, {encoding: 'utf8'});
filesChanged = filesChanged.split("\n");
if (process.platform === 'win32') {
cmd = 'powershell -NoProfile ' + cmd;
}
const executeResult = execSync(cmd, {encoding: 'utf8'});
if (executeResult) {
filesChanged = executeResult.split("\n").map(filePath => path.resolve(filePath));
}

// last entry is just empty string
filesChanged.pop();

return through.obj(
function (file, enc, cb) {
function (file, _enc, cb) {
if (isFileChanged(file, filesChanged)) {
this.push(file);
}
Expand All @@ -27,8 +34,7 @@ function diffBranches(options) {
};

function isFileChanged(file, filesChanged) {
var currentFile = file.path.substr(process.cwd().length + 1);
if (filesChanged.indexOf(currentFile) != -1) {
if (filesChanged.indexOf(file.path) != -1) {
return true;
} else {
return false;
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"test": "./node_modules/mocha/bin/mocha"
},
"author": {
"name" : "Alexander Myshov",
"url": "https://github.com/myshov"
"name": "Alexander Myshov",
"url": "https://github.com/myshov"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +27,8 @@
"node": ">=0.12.0"
},
"keywords": [
"gulpplugin", "git", "filtering"
"gulpplugin",
"git",
"filtering"
]
}
21 changes: 6 additions & 15 deletions test/isFileChanged.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
var assert = require('assert');
var rewire = require('rewire');
var path = require('path');

var gulpGitFlowDiff = rewire('../');
gulpGitFlowDiff.__set__({
process: {
cwd: function() {
return '/home/user/dev'
}
}
});

describe('gulp-giflow-diff isFileChanged', function() {
process.cwd = function() {
return '/home/user/dev'
};

const basePath = process.cwd();
it('should return true if there is file in list of changed files', function(done) {
var fileMocked = {};
fileMocked.path = '/home/user/dev/src/file33.js';
fileMocked.path = path.join(basePath, 'src/file33.js');
var filesChanged = [
'src/file11.js',
'src/file22.js',
'src/file33.js'
];
].map(filePath => path.join(basePath, filePath));
var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged);
assert.equal(result, true);
done();
});

it('should return false if there is no file in list of changed files', function(done) {
var fileMocked = {};
fileMocked.path = '/usr/home/user/dev/src/file44.js';
fileMocked.path = path.join(basePath, 'src/file44.js');
var filesChanged = [
'src/file11.js',
'src/file22.js',
'src/file33.js'
];
].map(filePath => path.join(basePath, filePath));
var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged);
assert.equal(result, false);
done();
Expand Down