diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..5162563 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,79 @@ +{ + "env": { + "node": true + }, + "extends": [ + "eslint:recommended" + ], + "root": true, + "rules": { + "block-scoped-var": "error", + "camelcase": "error", + "comma-spacing": [ + "error", + { + "after": true, + "before": false + } + ], + "eqeqeq": [ + "error", + "smart" + ], + "indent": [ + "error", + 2, + { + "SwitchCase": 1 + } + ], + "keyword-spacing": [ + "error", + { + "after": true, + "before": true + } + ], + "linebreak-style": "error", + "max-depth": [ + "error", + 5 + ], + "max-statements": [ + "error", + 50 + ], + "new-cap": 0, + "no-console": "off", + "no-extend-native": "error", + "no-unused-expressions": "error", + "quotes": [ + "error", + "single", + { + "avoidEscape": true + } + ], + "semi": [ + "error", + "always", + { + "omitLastInOneLineBlock": false + } + ], + "space-before-blocks": [ + "error", + "always" + ], + "space-infix-ops": "error", + "spaced-comment": [ + "error", + "always" + ], + "strict": [ + "error", + "safe" + ], + "valid-jsdoc": "error" + } +} diff --git a/.gitignore b/.gitignore index 0e13280..641f447 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ results npm-debug.log node_modules *.sublime* +docs/ diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index f96e529..0000000 --- a/.jshintrc +++ /dev/null @@ -1,20 +0,0 @@ -{ - "camelcase": true, - "eqeqeq": true, - "freeze": true, - "indent": 2, - "newcap": false, - "quotmark": "single", - "maxdepth": 5, - "maxstatements": 50, - "eqnull": true, - "funcscope": true, - "strict": true, - "undef": true, - "unused": false, - "node": true, - "mocha": true, - "expr": true, - "laxbreak": true, - "validthis": true -} diff --git a/.npmignore b/.npmignore index 0e13280..cae9861 100644 --- a/.npmignore +++ b/.npmignore @@ -1,17 +1,28 @@ -lib-cov -*.seed -*.log *.csv *.dat +*.gz +*.log *.out *.pid -*.gz +*.seed +*.sublime* .DS_store - -pids +.editorconfig +.eslintrc* +.gitignore +.grunt +.lock-wscript +.node_repl_history +.stylelintrc* +.travis.yml +.vscode +appveyor.yml +coverage +gulpfile.js +lib-cov logs -results - -npm-debug.log node_modules -*.sublime* +npm-debug.log* +pids +results +test diff --git a/.travis.yml b/.travis.yml index 56f4438..bb32474 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ sudo: false language: node_js node_js: - - "4.1.1" - - "0.12" + - "4" + - "7" + - "8" before_install: - - git config --global user.email "you@example.com" - - git config --global user.name "Your Name" + - git config --global user.email "you@example.com" + - git config --global user.name "Your Name" diff --git a/README.md b/README.md index d79e3b2..5a58b00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -#gulp-git +# gulp-git + [![Build Status](https://travis-ci.org/stevelacy/gulp-git.png?branch=master)](https://travis-ci.org/stevelacy/gulp-git) [![NPM version](https://badge.fury.io/js/gulp-git.png)](http://badge.fury.io/js/gulp-git) @@ -135,6 +136,14 @@ gulp.task('push', function(){ }); }); +// Run git push +// branch is the current branch & remote branch to push to +gulp.task('push', function(){ + git.push('origin', function (err) { + if (err) throw err; + }); +}); + // Run git push with options // branch is the remote branch to push to gulp.task('push', function(){ @@ -252,6 +261,13 @@ gulp.task('reset', function(){ }); }); +// Show the formatted git diff +gulp.task('diff', function(){ + gulp.src('./*') + .pipe(git.diff('master', {log: true})) + .pipe(gulp.dest('./diff.out')); +}); + // Git rm a file or folder gulp.task('rm', function(){ return gulp.src('./gruntfile.js') @@ -287,6 +303,12 @@ gulp.task('clean', function() { }); }); +// Get the current branch name + +git.revParse({args:'--abbrev-ref HEAD'}, function (err, branch) { + console.log('current git branch: ' + branch); +}); + // Run gulp's default task gulp.task('default',['add']); ``` @@ -336,7 +358,7 @@ git.clone('https://remote.git', {args: './sub/folder'}, function (err) { Adds files to repo -`opt`: Object (optional) `{args: 'options', quiet: true, maxBuffer: 200 * 1024}` +`opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}` ```js gulp.src('./*') @@ -349,7 +371,7 @@ gulp.src('./*') Commits changes to repo -`message`: String, commit message +`message`: String or array of strings, commit message `opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', maxBuffer: 200 * 1024, quiet: true, disableMessageRequirement: false, disableAppendPaths: false, multiline: false}` @@ -474,7 +496,7 @@ Tags repo with release version, returns all tags when used without arguments `version`: String (optional), tag name -`message`: String (optional), tag message +`message`: String or array of strings (optional), tag message `opt`: Object (optional) `{args: 'options', cwd: '/cwd/path', quiet: true, maxBuffer: 200 * 1024}` diff --git a/examples/gulpfile.js b/examples/gulpfile.js index 16617a8..9ebdfa6 100644 --- a/examples/gulpfile.js +++ b/examples/gulpfile.js @@ -5,14 +5,14 @@ var git = require('../'); // Init a git repo -gulp.task('init', function(){ +gulp.task('init', function() { git.init(); }); // Add files -gulp.task('add', function(){ +gulp.task('add', function() { gulp.src('./*') .pipe(git.add()); }); @@ -20,19 +20,19 @@ gulp.task('add', function(){ // Commit files -gulp.task('commit', function(){ +gulp.task('commit', function() { gulp.src('./*', {buffer:false}) .pipe(git.commit('initial commit')); }); // Commit files with arguments -gulp.task('commitopts', function(){ +gulp.task('commitopts', function() { gulp.src('./*') .pipe(git.commit('initial commit', {args: '-v'})); }); // Commit files using raw arguments, without message checking -gulp.task('commitraw', function(){ +gulp.task('commitraw', function() { gulp.src('./*') .pipe(git.commit(undefined, { args: '-m "initial commit"', @@ -41,19 +41,19 @@ gulp.task('commitraw', function(){ }); // Commit files using raw arguments, without message checking -gulp.task('commitmulti', function(){ +gulp.task('commitmulti', function() { gulp.src('./*') .pipe(git.commit(['initial commit', 'additional message'])); }); // Commit files using the multiline option -gulp.task('commitmultiline', function(){ +gulp.task('commitmultiline', function() { gulp.src('./*') .pipe(git.commit(['initial commit', 'additional message'], { mutiline: true })); }); // Commit files with multiline messages -gulp.task('commitmultiline', function(){ +gulp.task('commitmultiline', function() { gulp.src('./*') .pipe(git.commit('initial commit\nadditional message')); }); @@ -61,38 +61,63 @@ gulp.task('commitmultiline', function(){ // Clone remote repo to current directory ($CWD/git-test) gulp.task('clone', function() { git.clone('https://github.com/stevelacy/git-test', function(err) { - // handle err + if (err) { + console.error(err); + } }); }); // Clone remote repo to sub folder ($CWD/sub/folder/git-test) gulp.task('clonesub', function() { git.clone('https://github.com/stevelacy/git-test', {args: './sub/folder'}, function(err) { - // handle err + if (err) { + console.error(err); + } }); }); +// Lint js files in index before git commit +gulp.task('precommit', function() { + var eslint = require('gulp-eslint'); + // get changes between HEAD and index + return git.diff('--cached', { + args: '-- *.js' + }) + // Read file contents from git index + .pipe(git.catFile()) + // Lint files that different between HEAD and index + .pipe(eslint()) + // Outputs the lint results to the console. + .pipe(eslint.format()) + // To have the process exit with an error code (1) on + .pipe(eslint.failAfterError()); +}); + // Add remote -gulp.task('remote', function(){ +gulp.task('remote', function() { git.addRemote('origin', 'https://github.com/stevelacy/git-test', function (err) { - //if (err) ... + if (err) { + console.error(err); + } }); }); // Push to remote repo -gulp.task('push', function(){ +gulp.task('push', function() { git.push('origin', 'master', function (err) { - //if (err) ... + if (err) { + console.error(err); + } }); }); // Pull from remote repo -gulp.task('pull', function(){ +gulp.task('pull', function() { git.pull('origin', 'master', function (err) { if (err) console.log(err); }); @@ -100,7 +125,7 @@ gulp.task('pull', function(){ // Pull from remote repo with only origin -gulp.task('pull-origin', function(){ +gulp.task('pull-origin', function() { git.pull('origin', function (err) { if (err) console.log(err); }); @@ -108,7 +133,7 @@ gulp.task('pull-origin', function(){ // Pull from all remote branches and tags -gulp.task('pull-all', function(){ +gulp.task('pull-all', function() { git.pull(function (err) { if (err) console.log(err); }); @@ -116,7 +141,7 @@ gulp.task('pull-all', function(){ // Pull from array of branches -gulp.task('pull-array', function(){ +gulp.task('pull-array', function() { git.pull('origin', ['master', 'development'], function (err) { if (err) console.log(err); }); @@ -124,36 +149,48 @@ gulp.task('pull-array', function(){ // Tag the repo -gulp.task('tag', function(){ +gulp.task('tag', function() { git.tag('v1.1.1', 'Version message', function (err) { - //if (err) ... + if (err) { + console.error(err); + } }); }); // Tag the repo WITH signed key -gulp.task('tagsec', function(){ +gulp.task('tagsec', function() { git.tag('v1.1.1', 'Version message with signed key', {signed:true}, function (err) { - //if (err) ... + if (err) { + console.error(err); + } + }); +}); + +gulp.task('tagall', function() { + git.tag(function(err, tags) { + console.log(tags); }); }); -gulp.task('push-tag', function(){ +gulp.task('push-tag', function() { git.push('origin', 'v1.1.1', function (err) { - //if (err) ... + if (err) { + console.error(err); + } }); }); -gulp.task('rm', function(){ +gulp.task('rm', function() { gulp.src('./delete') .pipe(git.rm({args: '-f'})); }); -gulp.task('addSubmodule', function(){ +gulp.task('addSubmodule', function() { git.addSubmodule('https://github.com/stevelacy/git-test', 'git-test', {args: '-b master'}); }); -gulp.task('updateSubmodules', function(){ +gulp.task('updateSubmodules', function() { git.updateSubmodule({args: '--init'}); }); diff --git a/examples/test.js b/examples/test.js deleted file mode 100644 index 2af890e..0000000 --- a/examples/test.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -var that = 'test'; diff --git a/index.js b/index.js index 19a2179..b48e40e 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,32 @@ 'use strict'; - +/** + * git + * @exports gulp-git + * @property {function} add {@link module:gulp-git/lib/add} + * @property {function} addRemote {@link module:gulp-git/lib/addRemote} + * @property {function} addSubmodule {@link module:gulp-git/lib/addSubmodule} + * @property {function} branch {@link module:gulp-git/lib/branch} + * @property {function} catFile {@link module:gulp-git/lib/catFile} + * @property {function} checkout {@link module:gulp-git/lib/checkout} + * @property {function} checkoutFiles {@link module:gulp-git/lib/checkoutFiles} + * @property {function} clean {@link module:gulp-git/lib/clean} + * @property {function} clone {@link module:gulp-git/lib/clone} + * @property {function} commit {@link module:gulp-git/lib/commit} + * @property {function} diff {@link module:gulp-git/lib/diff} + * @property {function} exec {@link module:gulp-git/lib/exec} + * @property {function} fetch {@link module:gulp-git/lib/fetch} + * @property {function} init {@link module:gulp-git/lib/init} + * @property {function} merge {@link module:gulp-git/lib/merge} + * @property {function} pull {@link module:gulp-git/lib/pull} + * @property {function} push {@link module:gulp-git/lib/push} + * @property {function} removeRemote {@link module:gulp-git/lib/removeRemote} + * @property {function} reset {@link module:gulp-git/lib/reset} + * @property {function} revParse {@link module:gulp-git/lib/revParse} + * @property {function} rm {@link module:gulp-git/lib/rm} + * @property {function} stash {@link module:gulp-git/lib/stash} + * @property {function} status {@link module:gulp-git/lib/status} + * @property {function} tag {@link module:gulp-git/lib/tag} + * @property {function} updateSubmodule {@link module:gulp-git/lib/updateSubmodule} + */ var requireDir = require('require-dir'); module.exports = requireDir('./lib'); diff --git a/lib/add.js b/lib/add.js index 8aeaade..76468e7 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,7 +1,7 @@ 'use strict'; var through = require('through2'); -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -25,11 +25,11 @@ module.exports = function (opt) { var cmd = 'git add ' + escape(paths) + ' ' + opt.args; var that = this; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; - exec(cmd, {cwd: cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + exec(cmd, {cwd: cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); files.forEach(that.push.bind(that)); that.emit('end'); cb(); diff --git a/lib/addRemote.js b/lib/addRemote.js index 0a2c77c..501c6ab 100644 --- a/lib/addRemote.js +++ b/lib/addRemote.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -11,18 +11,18 @@ module.exports = function (remote, url, opt, cb) { opt = {}; } if (!cb || typeof cb !== 'function') cb = function () {}; - if (!url) cb(new Error('gulp-git: Repo URL is required git.addRemote("origin", "https://github.com/user/repo.git")')); + if (!url) return cb(new Error('gulp-git: Repo URL is required git.addRemote("origin", "https://github.com/user/repo.git")')); if (!remote) remote = 'origin'; if (!opt) opt = {}; if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git remote add ' + opt.args + ' ' + escape([remote, url]); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err) return cb(err); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/addSubmodule.js b/lib/addSubmodule.js index 8e26f47..5e6b960 100644 --- a/lib/addSubmodule.js +++ b/lib/addSubmodule.js @@ -1,22 +1,22 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; module.exports = function (url, name, opt, cb) { - if (!cb || typeof cb !== 'function') cb = function () {}; - if (!url) cb && cb(new Error('gulp-git: Repo URL is required git.submodule.add("https://github.com/user/repo.git", "repoName")')); - if (!name) name = ''; - if (!opt) opt = {}; - if (!opt.cwd) opt.cwd = process.cwd(); - if (!opt.args) opt.args = ''; + if (!cb || typeof cb !== 'function') cb = function () {}; + if (!url) return cb(new Error('gulp-git: Repo URL is required git.submodule.add("https://github.com/user/repo.git", "repoName")')); + if (!name) name = ''; + if (!opt) opt = {}; + if (!opt.cwd) opt.cwd = process.cwd(); + if (!opt.args) opt.args = ''; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; - var cmd = 'git submodule add ' + opt.args + ' ' + url + ' ' + name; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err && cb) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); - if (cb) cb(); - }); + var cmd = 'git submodule add ' + opt.args + ' ' + url + ' ' + name; + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err && cb) return cb(err); + if (!opt.quiet) log(stdout, stderr); + if (cb) cb(); + }); }; diff --git a/lib/branch.js b/lib/branch.js index 0022540..e0e01a7 100644 --- a/lib/branch.js +++ b/lib/branch.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -19,12 +19,12 @@ module.exports = function (branch, opt, cb) { if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git branch ' + opt.args + ' ' + escape([branch]); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); - cb(); + if (!opt.quiet) log(stdout, stderr); + cb(null, stdout); }); }; diff --git a/lib/catFile.js b/lib/catFile.js new file mode 100644 index 0000000..58d231b --- /dev/null +++ b/lib/catFile.js @@ -0,0 +1,109 @@ +'use strict'; +/** + * catFile + * @module gulp-git/lib/catFile + */ + +var through = require('through2'); +var PluginError = require('plugin-error'); +var spawn = require('child_process').spawn; +var stripBom = require('strip-bom-stream'); + +/** + * get a buffer. + * @callback requestCallback + * @param {buffer} buf + */ + +/** + * Convert stream to buffer + * + * @param {Stream} stream stream that what to read + * @param {readStreamCallback} callback function that receive buffer + * @returns {void} + */ +function readStream(stream, callback) { + var buf; + stream.on('data', function(data) { + if (buf) { + buf = Buffer.concat([buf, data]); + } else { + buf = data; + } + }); + stream.once('finish', function() { + if (buf) { + callback(buf); + } + }); +} + +/** + * @typedef {object} catFileOptions + * @property {boolean} stripBOM {@link https://github.com/gulpjs/vinyl-fs#optionsstripbom} + * @property {boolean} buffer {@link https://github.com/gulpjs/vinyl-fs#optionsbuffer} + */ + +/** + * read vinyl file contents + * @param {catFileOptions} opt [catFileOptions]{@link module:gulp-git/lib/catFile~catFileOptions} + * @returns {stream} stream of vinyl `File` objects. + */ +module.exports = function (opt) { + if (!opt) opt = {}; + if (undefined === opt.stripBOM || null === opt.stripBOM) opt.stripBOM = true; + if (undefined === opt.buffer || null === opt.buffer) opt.buffer = true; + + /** + * transform function of stream {@link https://nodejs.org/docs/latest/api/stream.html#stream_transform_transform_chunk_encoding_callback} + * + * @param {vinyl} file The file to be transformed. + * @param {any} enc encoding type. + * @param {function} cb A callback function (optionally with an error argument and data) to be called after the supplied `file` has been processed. + * @returns {void} + */ + var write = function(file, enc, cb) { + + var hash = file.git && file.git.hash; + + /** + * set file contents and send file to stream + * + * @param {Buffer} contents file contents + * @returns {void} + */ + var sendFile = function(contents) { + if (contents) { + file.contents = contents; + } + return cb(null, file); + }; + + if (!hash || /^0+$/.test(hash)) { + return sendFile(); + } + + var catFile = spawn('git', ['cat-file', 'blob', hash], { + cwd: file.cwd + }); + + var contents = catFile.stdout; + var that = this; + + readStream(catFile.stderr, function(error) { + that.emit('error', new PluginError('gulp-git', 'Command failed: ' + catFile.spawnargs.join(' ').trim() + '\n' + error.toString())); + }); + + if (opt.stripBOM) { + contents = contents.pipe(stripBom()); + } + + if (opt.buffer) { + readStream(contents, sendFile); + } else { + sendFile(contents); + } + }; + var stream = through.obj(write); + return stream; +}; diff --git a/lib/checkout.js b/lib/checkout.js index 2629584..8a3bc18 100644 --- a/lib/checkout.js +++ b/lib/checkout.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -16,12 +16,12 @@ module.exports = function (branch, opt, cb) { if (!opt.args) opt.args = ' '; if (!opt.cwd) opt.cwd = process.cwd(); - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git checkout ' + opt.args + ' ' + escape([branch]); - exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(null); }); }; diff --git a/lib/checkoutFiles.js b/lib/checkoutFiles.js index 2fd419e..31382d2 100644 --- a/lib/checkoutFiles.js +++ b/lib/checkoutFiles.js @@ -1,7 +1,7 @@ 'use strict'; var through = require('through2'); -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -14,11 +14,11 @@ module.exports = function (opt) { var cmd = 'git checkout ' + opt.args + ' ' + escape([file.path]); if (!cb || typeof cb !== 'function') cb = function () {}; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; - exec(cmd, {cwd: file.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if(err) return cb(err); - if(!opt.quiet) gutil.log(stdout, stderr); + exec(cmd, {cwd: file.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err) return cb(err); + if (!opt.quiet) log(stdout, stderr); that.push(file); cb(null); }); diff --git a/lib/clean.js b/lib/clean.js index 35bdd55..b467226 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -1,15 +1,15 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); module.exports = function (paths, opt, cb) { - if(!cb) { - if(typeof opt === 'function') { + if (!cb) { + if (typeof opt === 'function') { // passed in 2 arguments cb = opt; - if(typeof paths === 'object') { + if (typeof paths === 'object') { opt = paths; paths = ''; } @@ -23,18 +23,18 @@ module.exports = function (paths, opt, cb) { } } - if(!opt.cwd) opt.cwd = process.cwd(); - if(!opt.args) opt.args = ' '; + if (!opt.cwd) opt.cwd = process.cwd(); + if (!opt.args) opt.args = ' '; var cmd = 'git clean ' + opt.args + ' ' + (paths.trim() ? (' -- ' + escape(paths)) : ''); - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; return exec(cmd, { cwd: opt.cwd, maxBuffer: maxBuffer }, function (err, stdout, stderr) { - if(err) + if (err) return cb(err); - if(!opt.quiet) - gutil.log(stdout, stderr); + if (!opt.quiet) + log(stdout, stderr); cb(); }); }; diff --git a/lib/clone.js b/lib/clone.js index 15339a5..834cfe7 100644 --- a/lib/clone.js +++ b/lib/clone.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -15,12 +15,12 @@ module.exports = function (remote, opt, cb) { if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git clone ' + escape([remote]) + ' ' + opt.args; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/commit.js b/lib/commit.js index 7a68eb5..f43b481 100644 --- a/lib/commit.js +++ b/lib/commit.js @@ -1,7 +1,7 @@ 'use strict'; var through = require('through2'); -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); var path = require('path'); @@ -17,15 +17,15 @@ module.exports = function(message, opt) { } } if (!opt.cwd) opt.cwd = process.cwd(); - if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; //Default buffer value for child_process.exec + if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; // Default buffer value for child_process.exec if (!opt.args) opt.args = ' '; var files = []; var paths = []; - var write = function(file, enc, cb){ + var write = function(file, enc, cb) { files.push(file); - paths.push(path.relative(opt.cwd, file.path).replace('\\','/')); + paths.push(path.relative(opt.cwd, file.path).replace('\\', '/')); cb(); }; @@ -33,7 +33,7 @@ module.exports = function(message, opt) { return '-m "' + entry + '" '; }; - var flush = function(cb){ + var flush = function(cb) { var writeStdin = false; var cmd = 'git commit '; @@ -54,7 +54,7 @@ module.exports = function(message, opt) { } cmd += messageExpanded + opt.args; } - if(!opt.disableAppendPaths) { + if (!opt.disableAppendPaths) { cmd += ' ' + escape(paths); } } else { @@ -63,7 +63,7 @@ module.exports = function(message, opt) { } else { cmd += '-m "' + message + '" ' + opt.args; } - if(!opt.disableAppendPaths) { + if (!opt.disableAppendPaths) { cmd += ' ' + escape(paths); } } @@ -84,7 +84,7 @@ module.exports = function(message, opt) { var execChildProcess = exec(cmd, opt, function(err, stdout, stderr) { if (err && (String(stdout).indexOf('no changes added to commit') === 0)) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); files.forEach(self.push.bind(self)); self.emit('end'); return cb(); @@ -98,11 +98,11 @@ module.exports = function(message, opt) { // If the user wants, we'll emit data events during exec // they can listen to them with .on('data',function(data){ }); // in their task - if(opt.emitData) { - execChildProcess.stdout.on('data',function(data) { + if (opt.emitData) { + execChildProcess.stdout.on('data', function(data) { self.emit('data', data); }); - execChildProcess.stderr.on('data',function(data) { + execChildProcess.stderr.on('data', function(data) { self.emit('data', data); }); } diff --git a/lib/diff.js b/lib/diff.js new file mode 100644 index 0000000..7d08e7a --- /dev/null +++ b/lib/diff.js @@ -0,0 +1,116 @@ +'use strict'; +/** + * diff + * @module gulp-git/lib/diff + */ + +var Vinyl = require('vinyl'); +var through = require('through2'); +var log = require('fancy-log'); +var path = require('path'); +var exec = require('child_process').exec; +var catFile = require('./catFile'); + +// https://git-scm.com/docs/git-diff#_raw_output_format +var RE_DIFF_RESULT = /\:(\w+)\s+(\w+)\s+(\w+)(?:\.{3})?\s+(\w+)(?:\.{3})?\s+(\w+)(\u0000|\t|\s+)(.+?)(?:\6|\n)(?:([^:]+?)\6)?/g; + +function getReaslt(data) { + var result = []; + if (data && data.length) { + var str = data.toString(); + var match; + RE_DIFF_RESULT.lastIndex = 0; + while ((match = RE_DIFF_RESULT.exec(str))) { + result.push({ + // mode for compare "src" + srcMode: match[1], + // mode for compare "dst" + dstMode: match[2], + // sha1 for compare "src" + srcHash: match[3], + // sha1 for compare "dst" + dstHash: match[4], + // status + status: match[5], + // path for compare "src" + srcPath: match[7], + // path for compare "dst" + dstPath: match[8] || match[7], + }); + } + } + return result; +} + +/** + * @typedef {Object} diffOptions + * @property {string} cwd {@link https://github.com/gulpjs/vinyl-fs#optionscwd} + * @property {string} base {@link https://github.com/gulpjs/vinyl-fs#optionsbase} + * @property {boolean} read {@link https://github.com/gulpjs/vinyl-fs#optionsread} + * @property {boolean} buffer {@link https://github.com/gulpjs/vinyl-fs#optionsbuffer} + * @property {boolean} stripBOM {@link https://github.com/gulpjs/vinyl-fs#optionsstripbom} + * @property {boolean} log show log in console + * @property {string[]} args Command parameter for `git diff` + */ + +/** + * get git diff result as a stream of vinyl `File` objects. + * + * @example +const git = require('gulp-git'); +const eslint = require('gulp-eslint'); +git.diff('--cached', { + args: '-- *.js' +}) +.pipe(eslint()) + * @param {string} compare compare arg for `git diff` + * @param {diffOptions} opt [diffOptions]{@link module:gulp-git/lib/diff~diffOptions} + * @returns {stream} stream of vinyl `File` objects. + */ +module.exports = function (compare, opt) { + if (!opt) opt = {}; + if (!opt.cwd) opt.cwd = process.cwd(); + // https://github.com/gulpjs/vinyl-fs#optionsread + if (undefined === opt.read || null === opt.read) opt.read = true; + if (undefined === opt.log || null === opt.log) opt.log = true; + + var srcStream = through.obj(); + var cmd = compare; + + if (!/--diff-filter=/.test(opt.args)) { + cmd += ' --diff-filter=ACM'; + } + if (opt.args) { + cmd += ' ' + opt.args; + } + exec('git diff --raw -z ' + cmd, {cwd: opt.cwd}, function(err, stdout) { + if (err) return srcStream.emit('error', err); + var files = getReaslt(stdout); + + if (opt.log) { + log('git diff --name-status ' + cmd + '\n' + files.map(function(diff) { + return diff.status + '\t' + diff.dstPath; + }).join('\n')); + } + + files.forEach(function(diff) { + srcStream.write(new Vinyl({ + path: path.resolve(opt.cwd, diff.dstPath), + cwd: opt.cwd, + base: opt.base, + git: { + hash: diff.dstHash, + diff: diff + } + })); + }); + srcStream.end(); + }); + + if (opt.read) { + // read file contents when opt.read is `true` + return srcStream.pipe(catFile(opt)); + } + + return srcStream; +}; diff --git a/lib/exec.js b/lib/exec.js index c4f48da..22221ef 100644 --- a/lib/exec.js +++ b/lib/exec.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; module.exports = function (opt, cb) { @@ -13,16 +13,16 @@ module.exports = function (opt, cb) { if (!opt) opt = { }; if (!opt.log) opt.log = !cb; if (!opt.cwd) opt.cwd = process.cwd(); - if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; //Default buffer value for child_process.exec + if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; // Default buffer value for child_process.exec if (!opt.args) opt.args = ' '; var cmd = 'git ' + opt.args; - return exec(cmd, {cwd : opt.cwd, maxBuffer: opt.maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd : opt.cwd, maxBuffer: opt.maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err, stderr); - if (opt.log && !opt.quiet) gutil.log(cmd + '\n' + stdout, stderr); + if (opt.log && !opt.quiet) log(cmd + '\n' + stdout, stderr); else { - if (!opt.quiet) gutil.log(cmd + ' (log : false)', stderr); + if (!opt.quiet) log(cmd + ' (log : false)', stderr); } cb(err, stdout); }); diff --git a/lib/fetch.js b/lib/fetch.js index 8c204c1..d0a5cdb 100644 --- a/lib/fetch.js +++ b/lib/fetch.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -17,19 +17,19 @@ module.exports = function (remote, branch, opt, cb) { if (!opt.args) opt.args = ' '; if (!remote && opt.args.indexOf('--all') === -1) remote = 'origin'; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git fetch ' + opt.args; var args = []; - if(remote) + if (remote) args.push(remote); - if(branch) + if (branch) args = args.concat(branch); - if(args.length > 0) + if (args.length > 0) cmd += escape(args); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/init.js b/lib/init.js index a027971..141ce32 100644 --- a/lib/init.js +++ b/lib/init.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; module.exports = function (opt, cb) { @@ -14,12 +14,12 @@ module.exports = function (opt, cb) { if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git init ' + opt.args; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); diff --git a/lib/merge.js b/lib/merge.js index 44eeddf..bbd268e 100644 --- a/lib/merge.js +++ b/lib/merge.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -16,12 +16,12 @@ module.exports = function (branch, opt, cb) { if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git merge ' + opt.args + ' ' + escape([branch]); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/pull.js b/lib/pull.js index 8e903c8..b82a15f 100644 --- a/lib/pull.js +++ b/lib/pull.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -10,6 +10,11 @@ module.exports = function (remote, branch, opt, cb) { cb = opt; opt = {}; } + // pull with callback only + if (!cb && typeof remote === 'function') { + cb = remote; + remote = {}; + } if (!cb || typeof cb !== 'function') cb = function () {}; if (!opt) opt = {}; if (!opt.cwd) opt.cwd = process.cwd(); @@ -22,11 +27,11 @@ module.exports = function (remote, branch, opt, cb) { if (branch && typeof branch === 'string' || branch && branch[0]) { cmd += ' ' + escape([].concat(branch)); } - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/push.js b/lib/push.js index 8f76d18..68adf34 100644 --- a/lib/push.js +++ b/lib/push.js @@ -1,32 +1,59 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); +var revParse = require('./revParse'); module.exports = function (remote, branch, opt, cb) { if (!remote) remote = 'origin'; - if (branch === null) { - branch = ''; - } else if (!branch) { - branch = 'master'; + + function setBranch(cb2) { + if (branch && typeof branch === 'function') { + cb = branch; + branch = null; + } + if (branch && Object.prototype.toString.call(branch) === '[object Object]') { + opt = branch; + branch = null; + } + if (!branch) { + revParse({ args: '--abbrev-ref HEAD' }, + function callback(err, out) { + if (err) return cb2(err); + branch = out; + cb2(); + }); + } else { + cb2(); + } } - if (!cb && typeof opt === 'function') { - cb = opt; - opt = {}; + + function flush(err) { + if (err) return cb(err); + if (!cb && typeof opt === 'function') { + cb = opt; + opt = {}; + } + if (!cb || typeof cb !== 'function') cb = function() {}; + if (!opt) opt = {}; + if (!opt.cwd) opt.cwd = process.cwd(); + if (!opt.args) opt.args = ' '; + + var cmd = 'git push ' + escape([].concat(remote, branch)) + ' ' + opt.args; + var maxBuffer = opt.maxBuffer || 200 * 1024; + + return exec(cmd, { + cwd: opt.cwd, + maxBuffer: maxBuffer + }, function(err, stdout, stderr) { + if (err) return cb(err); + if (!opt.quiet) log(stdout, stderr); + cb(); + }); } - if (!cb || typeof cb !== 'function') cb = function () {}; - if (!opt) opt = {}; - if (!opt.cwd) opt.cwd = process.cwd(); - if (!opt.args) opt.args = ' '; - var cmd = 'git push ' + escape([].concat(remote, branch)) + ' ' + opt.args; - var maxBuffer = opt.maxBuffer || 200*1024; + return setBranch(flush); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); - cb(); - }); }; diff --git a/lib/removeRemote.js b/lib/removeRemote.js index 6a3fd9c..3f664ed 100644 --- a/lib/removeRemote.js +++ b/lib/removeRemote.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -10,18 +10,22 @@ module.exports = function (remote, opt, cb) { cb = opt; opt = {}; } + if (!remote || typeof remote !== 'string') { + var error = new Error('gulp-git: remote is required git.removeRemote("origin")'); + if (!cb || typeof cb !== 'function') throw error; + return cb(error); + } if (!cb || typeof cb !== 'function') cb = function () {}; - if (!remote) cb(new Error('gulp-git: remote is required git.removeRemote("origin")')); if (!opt) opt = {}; if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git remote remove ' + opt.args + ' ' + escape([remote]); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err) return cb(err); + if (!opt.quiet) log(stdout, stderr); cb(); }); }; diff --git a/lib/reset.js b/lib/reset.js index a5eb8d7..38e64e7 100644 --- a/lib/reset.js +++ b/lib/reset.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -16,12 +16,12 @@ module.exports = function (commit, opt, cb) { if (!opt.args) opt.args = ' '; if (!commit) commit = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git reset ' + opt.args + ' ' + escape([commit]); - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(); }); diff --git a/lib/revParse.js b/lib/revParse.js index 339e97e..19ad41b 100644 --- a/lib/revParse.js +++ b/lib/revParse.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; /* @@ -23,13 +23,13 @@ module.exports = function (opt, cb) { if (!opt.args) opt.args = ' '; // it will likely not give you what you want if (!opt.cwd) opt.cwd = process.cwd(); - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git rev-parse ' + opt.args; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); if (stdout) stdout = stdout.trim(); // Trim trailing cr-lf - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(err, stdout); // return stdout to the user }); }; diff --git a/lib/rm.js b/lib/rm.js index 6c9e906..6eb116c 100644 --- a/lib/rm.js +++ b/lib/rm.js @@ -1,7 +1,7 @@ 'use strict'; var through = require('through2'); -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -22,13 +22,13 @@ module.exports = function (opt) { var flush = function(cb) { var cwd = opt.cwd || fileCwd; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git rm ' + escape(paths) + ' ' + opt.args; var that = this; - exec(cmd, {cwd: cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + exec(cmd, {cwd: cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err) return cb(err); + if (!opt.quiet) log(stdout, stderr); files.forEach(that.push.bind(that)); cb(); }); diff --git a/lib/stash.js b/lib/stash.js index f0ff313..414cd98 100644 --- a/lib/stash.js +++ b/lib/stash.js @@ -1,8 +1,7 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; -var escape = require('any-shell-escape'); module.exports = function(opt, cb) { @@ -17,12 +16,12 @@ module.exports = function(opt, cb) { if (!opt.args) opt.args = 'save --include-untracked "gulp-stash"'; if (!opt.cwd) opt.cwd = process.cwd(); - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git stash ' + opt.args; exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + if (!opt.quiet) log(stdout, stderr); cb(null); }); diff --git a/lib/status.js b/lib/status.js index b9163ed..c256713 100644 --- a/lib/status.js +++ b/lib/status.js @@ -1,6 +1,6 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; module.exports = function (opt, cb) { @@ -13,12 +13,12 @@ module.exports = function (opt, cb) { if (!opt) opt = {}; if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; //Default buffer value for child_process.exec + if (!opt.maxBuffer) opt.maxBuffer = 200 * 1024; // Default buffer value for child_process.exec var cmd = 'git status ' + opt.args; - return exec(cmd, {cwd : opt.cwd, maxBuffer: opt.maxBuffer}, function(err, stdout, stderr){ + return exec(cmd, {cwd : opt.cwd, maxBuffer: opt.maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err, stderr); - if (!opt.quiet) gutil.log(cmd + '\n' + stdout, stderr); + if (!opt.quiet) log(cmd + '\n' + stdout, stderr); if (cb) cb(err, stdout); }); }; diff --git a/lib/tag.js b/lib/tag.js index cdebb2e..d74fa44 100644 --- a/lib/tag.js +++ b/lib/tag.js @@ -1,6 +1,7 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); +var template = require('lodash.template'); var exec = require('child_process').exec; var escape = require('any-shell-escape'); @@ -16,12 +17,16 @@ module.exports = function (version, message, opt, cb) { message = ''; } if (!cb || typeof cb !== 'function') cb = function () {}; - if (!message) opt.lightWeight = true; else message = escape([message]); if (!opt) opt = {}; + if (!message) { + opt.lightWeight = true; + message = ''; + } + else message = escape([message]); if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var signedarg = opt.signed ? ' -s ' : ' -a '; @@ -32,10 +37,10 @@ module.exports = function (version, message, opt, cb) { } cmd += opt.args + ' ' + escape([version]); } - var templ = gutil.template(cmd, {file: message}); - return exec(templ, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ + var templ = template(cmd)({file: message}); + return exec(templ, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { if (err) return cb(err); - if (!opt.quiet && version !== '') gutil.log(stdout, stderr); + if (!opt.quiet && version !== '') log(stdout, stderr); if (version === '') { stdout = stdout.split('\n'); } diff --git a/lib/updateSubmodule.js b/lib/updateSubmodule.js index bb8cc74..1247935 100644 --- a/lib/updateSubmodule.js +++ b/lib/updateSubmodule.js @@ -1,20 +1,20 @@ 'use strict'; -var gutil = require('gulp-util'); +var log = require('fancy-log'); var exec = require('child_process').exec; -module.exports = function(opt, cb){ +module.exports = function(opt, cb) { if (!cb || typeof cb !== 'function') cb = function () {}; if (!opt) opt = {}; if (!opt.cwd) opt.cwd = process.cwd(); if (!opt.args) opt.args = ' '; - var maxBuffer = opt.maxBuffer || 200*1024; + var maxBuffer = opt.maxBuffer || 200 * 1024; var cmd = 'git submodule update ' + opt.args; - return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr){ - if (err && cb) cb(err); - if (!opt.quiet) gutil.log(stdout, stderr); + return exec(cmd, {cwd: opt.cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) { + if (err && cb) return cb(err); + if (!opt.quiet) log(stdout, stderr); if (cb) cb(); }); }; diff --git a/package.json b/package.json index d12179d..b2f8ec6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gulp-git", "description": "Git plugin for gulp (gulpjs.com)", - "version": "1.15.1", + "version": "2.5.0", "homepage": "http://github.com/stevelacy/gulp-git", "repository": { "type": "git", @@ -11,18 +11,25 @@ "main": "./index.js", "dependencies": { "any-shell-escape": "^0.1.1", - "gulp-util": "^3.0.6", - "require-dir": "^0.1.0", - "through2": "^0.6.5" + "fancy-log": "^1.3.2", + "lodash.template": "^4.4.0", + "plugin-error": "^0.1.2", + "require-dir": "^0.3.2", + "strip-bom-stream": "^3.0.0", + "through2": "^2.0.3", + "vinyl": "^2.0.1" }, "devDependencies": { - "jshint": "^2.8.0", - "mocha": "^2.3.0", - "rimraf": "^2.4.3", - "should": "^7.1.0" + "eslint": "^3.17.0", + "mocha": "^3.2.0", + "mock-require": "^2.0.2", + "rimraf": "^2.6.1", + "should": "^11.2.0" }, "scripts": { - "test": "mocha --reporter spec --timeout 6000 test/main.js && jshint ./index.js ./examples/. ./lib/. ./test/." + "docs": "rimraf docs/* && jsdoc ./index.js ./lib --recurse --destination ./docs", + "lint": "rimraf test/repo test/tmp && eslint ./index.js ./examples/ ./lib/ ./test/", + "test": "mocha --reporter spec --timeout 6000 test/main.js && npm run lint" }, "engines": { "node": ">= 0.9.0" diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 0000000..703ef3b --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,10 @@ +{ + "env": { + "node": true, + "mocha": true + }, + "extends": [ + "../.eslintrc.json" + ], + "root": false +} diff --git a/test/_util.js b/test/_util.js index ede17b5..5ebffb2 100644 --- a/test/_util.js +++ b/test/_util.js @@ -2,17 +2,15 @@ var fs = require('fs'); var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); +var Vinyl = require('vinyl'); var repo = path.join(__dirname, 'repo'); -var fileContents = function(){ - if (fs.existsSync(repo)){ +var fileContents = function() { + if (fs.existsSync(repo)) { return fs.readFileSync('test/repo/testContents.js'); } - else{ + else { fs.mkdirSync(repo); fs.openSync(path.join(repo, 'testContents.js'), 'w'); return fs.readFileSync('test/repo/testContents.js'); @@ -20,7 +18,7 @@ var fileContents = function(){ }; -var testFiles = (function(){ +var testFiles = (function() { var testFiles = []; for (var i = 0; i < 10; i++) { testFiles[i] = { @@ -34,15 +32,15 @@ var testFiles = (function(){ return testFiles; }).call(this); -var testOptionsFiles = (function(){ +var testOptionsFiles = (function() { var testFiles = []; for (var i = 0; i < 12; i++) { - testFiles[i] = { + testFiles[i] = new Vinyl({ base: 'test/repo', cwd: 'test/repo', path: __dirname + '/repo/test.options.' + i + '.js', contents: new Buffer(fileContents()) - }; + }); fs.openSync(testFiles[i].path, 'w'); } return testFiles; @@ -55,14 +53,14 @@ module.exports = { testCommit: path.join(repo, '.git', 'COMMIT_EDITMSG'), testFiles: testFiles, testOptionsFiles: testOptionsFiles, - testSuite: function(){ + testSuite: function() { var testSuite = fs.readdirSync(__dirname); var testFirst = [ 'clone.js', 'init.js', 'add.js', 'commit.js', 'stash.js' ]; // use it also to omit _main & _util files - testFirst.concat('main.js', '_util.js', 'repo').forEach(function(file){ + testFirst.concat('main.js', '_util.js', 'repo').forEach(function(file) { testSuite.splice(testSuite.indexOf(file), 1); }); testSuite.unshift.apply(testSuite, testFirst); diff --git a/test/add.js b/test/add.js index 7c7b324..9a855db 100644 --- a/test/add.js +++ b/test/add.js @@ -1,19 +1,17 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); +var Vinyl = require('vinyl'); -module.exports = function(git, util){ +module.exports = function(git, util) { it('should add files to the git repo', function(done) { - var fakeFile = new gutil.File(util.testFiles[0]); + var fakeFile = new Vinyl(util.testFiles[0]); var gitS = git.add(); - gitS.on('data', function(newFile){ + gitS.on('data', function(newFile) { should.exist(newFile); - fs.stat('test/repo/.git/objects/', function(err, stats){ + fs.stat('test/repo/.git/objects/', function(err) { should.not.exist(err); done(); }); @@ -24,24 +22,24 @@ module.exports = function(git, util){ it('should add multiple files to the git repo', function(done) { var fakeFiles = []; - util.testFiles.forEach(function(name){ - fakeFiles.push( new gutil.File(name) ); + util.testFiles.forEach(function(name) { + fakeFiles.push( new Vinyl(name) ); }); var gitS = git.add(); - gitS.on('data', function(newFile){ + gitS.on('data', function(newFile) { should.exist(newFile); - fs.stat('test/repo/.git/objects/', function(err, stats){ + fs.stat('test/repo/.git/objects/', function(err) { should.not.exist(err); }); }); - fakeFiles.forEach(function(file){ + fakeFiles.forEach(function(file) { gitS.write(file); }); gitS.end(done); }); it('should fire an end event', function(done) { - var fakeFile = new gutil.File(util.testFiles[0]); + var fakeFile = new Vinyl(util.testFiles[0]); var gitS = git.add(); gitS.on('end', function() { diff --git a/test/addRemote.js b/test/addRemote.js index c8c0173..858d53c 100644 --- a/test/addRemote.js +++ b/test/addRemote.js @@ -1,17 +1,13 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { it('should add a Remote to the git repo', function(done) { var opt = {cwd: './test/repo/'}; var origin = 'https://github.com/stevelacy/git-test'; - git.addRemote('origin', origin, opt, function(){ + git.addRemote('origin', origin, opt, function() { fs.statSync('./test/repo/.git/'); fs.readFileSync('./test/repo/.git/config') .toString('utf8') diff --git a/test/branch.js b/test/branch.js index 4d86a12..9cdc602 100644 --- a/test/branch.js +++ b/test/branch.js @@ -1,29 +1,26 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { - it('should create a new branch', function(done){ + it('should create a new branch', function(done) { var opt = {cwd: './test/repo/'}; - git.branch('testBranch', opt, function(){ - fs.stat('test/repo/.git/refs/heads/testBranch', function(err, stats) { + git.branch('testBranch', opt, function() { + fs.stat('test/repo/.git/refs/heads/testBranch', function(err) { should.not.exist(err); done(); }); }); }); - it('should create new branch, checkout and return its name', function(done){ + it('should create new branch, checkout and return its name', function(done) { var opt = {cwd: './test/repo/'}; - git.branch('anotherBranch', opt, function(){ - fs.stat('test/repo/.git/refs/heads/anotherBranch', function(err, stats) { + git.branch('anotherBranch', opt, function() { + fs.stat('test/repo/.git/refs/heads/anotherBranch', function(err) { should.not.exist(err); - git.checkout('anotherBranch', opt, function(){ + git.checkout('anotherBranch', opt, function() { opt = {args: '--abbrev-ref HEAD', cwd: opt.cwd}; git.revParse(opt, function (err, branch) { branch.should.equal('anotherBranch'); diff --git a/test/catFile.js b/test/catFile.js new file mode 100644 index 0000000..e6eb1f8 --- /dev/null +++ b/test/catFile.js @@ -0,0 +1,28 @@ +'use strict'; + +var should = require('should'); +var execSync = require('child_process').execSync; +var Vinyl = require('vinyl'); + +module.exports = function(git) { + it('package.json', function(done) { + if (!/\b(\S{40,})\b/.test(execSync('git ls-files -s -- package.json').toString())) { + return; + } + var hash = RegExp.$1; + + var stream = git.catFile(); + + stream.on('data', function(file) { + should.exist(file.contents); + done(); + }); + + stream.write(new Vinyl({ + path: 'package.json', + git: { + hash: hash + } + })); + }); +}; diff --git a/test/checkout.js b/test/checkout.js index 9f594e5..9236ac3 100644 --- a/test/checkout.js +++ b/test/checkout.js @@ -1,16 +1,11 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { it('should checkout a branch', function(done) { var opt = {cwd: 'test/repo'}; - var fakeFile = new gutil.File(util.testFiles[0]); git.checkout('testBranch', opt, function() { fs.readFileSync('test/repo/.git/HEAD') .toString('utf8') diff --git a/test/clean.js b/test/clean.js index ffe06d5..78df42f 100644 --- a/test/clean.js +++ b/test/clean.js @@ -1,40 +1,38 @@ /* global beforeEach, it, afterEach */ 'use strict'; -var fs = require('fs'), - should = require('should'), - rimraf = require('rimraf'), - exec = require('child_process').exec; - -module.exports = function (git, util) { - var repoPath = './test/tmp'; - - beforeEach(function (done) { - var repo = 'git://github.com/stevelacy/git-test'; - git.clone(repo, { args: repoPath }, done); - }); - - it('should remove an untracked file from the repo', function (done) { - var filePath = repoPath + '/test.txt'; - fs.writeFile(filePath, 'Hello git clean test', function (err) { - if(err) - return done(err); - git.clean({ cwd: repoPath, args: '-f' }, function (err) { - if(err) - return done(err); - fs.stat(filePath, function (err, stats) { - if(err) - return done(); - done(new Error('Failed to remove file')); - }); - }); - }); - }); - - afterEach(function(done){ - rimraf('./test/tmp', function(err){ - if(err) return done(err); - done(); - }); - }); -}; \ No newline at end of file +var fs = require('fs'); +var rimraf = require('rimraf'); + +module.exports = function (git) { + var repoPath = './test/tmp'; + + beforeEach(function (done) { + var repo = 'git://github.com/stevelacy/git-test'; + git.clone(repo, { args: repoPath }, done); + }); + + it('should remove an untracked file from the repo', function (done) { + var filePath = repoPath + '/test.txt'; + fs.writeFile(filePath, 'Hello git clean test', function (err) { + if (err) + return done(err); + git.clean({ cwd: repoPath, args: '-f' }, function (err) { + if (err) + return done(err); + fs.stat(filePath, function (err) { + if (err) + return done(); + done(new Error('Failed to remove file')); + }); + }); + }); + }); + + afterEach(function(done) { + rimraf('./test/tmp', function(err) { + if (err) return done(err); + done(); + }); + }); +}; diff --git a/test/clone.js b/test/clone.js index 9d1b2d3..f7b74ff 100644 --- a/test/clone.js +++ b/test/clone.js @@ -1,28 +1,26 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { - beforeEach(function(done){ + beforeEach(function(done) { var repo = 'git://github.com/stevelacy/gulp-git'; git.clone(repo, {args: './test/tmp'}, done); }); - it('should have cloned project into tmp directory', function(done){ - fs.stat('./test/tmp/.git', function(err, stats){ + it('should have cloned project into tmp directory', function(done) { + fs.stat('./test/tmp/.git', function(err) { should.not.exist(err); done(); }); }); - afterEach(function(done){ - rimraf('./test/tmp', function(err){ - if(err) return done(err); + afterEach(function(done) { + rimraf('./test/tmp', function(err) { + if (err) return done(err); done(); }); }); diff --git a/test/commit.js b/test/commit.js index f10cb22..ec9dc2f 100644 --- a/test/commit.js +++ b/test/commit.js @@ -1,20 +1,17 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); var exec = require('child_process').exec; +var isVinyl = require('vinyl').isVinyl; -module.exports = function(git, util){ +module.exports = function(git, util) { it('should commit a file to the repo', function(done) { var fakeFile = util.testFiles[0]; var opt = {cwd: './test/repo/'}; var gitS = git.commit('initial commit', opt); - gitS.once('finish', function(){ - setTimeout(function(){ + gitS.once('finish', function() { + setTimeout(function() { fs.readFileSync(util.testCommit) .toString('utf8') .should.match(/initial commit/); @@ -42,8 +39,8 @@ module.exports = function(git, util){ var fakeFile = util.testFiles[3]; var opt = {cwd: './test/repo/', args:'-m "initial commit"', disableMessageRequirement: true}; var gitS = git.commit(undefined, opt); - gitS.once('finish', function(){ - setTimeout(function(){ + gitS.once('finish', function() { + setTimeout(function() { fs.readFileSync(util.testCommit) .toString('utf8') .should.match(/initial commit/); @@ -57,12 +54,15 @@ module.exports = function(git, util){ it('should commit a file to the repo when appending paths is disabled', function(done) { var fakeFile = util.testOptionsFiles[4]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/', disableAppendPaths: true}; var gitS = git.commit('initial commit', opt); gitS.on('end', function(err) { - if(err) {console.error(err); } - setTimeout(function(){ + if (err) {console.error(err); } + setTimeout(function() { fs.readFileSync(util.testCommit) .toString('utf8') .should.match(/initial commit/); @@ -71,18 +71,21 @@ module.exports = function(git, util){ }); gitS.write(fakeFile); gitS.end(); - }); + }); }); it('should commit a file to the repo when passing multiple messages', function(done) { var fakeFile = util.testOptionsFiles[5]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/', disableAppendPaths: true}; var gitS = git.commit(['initial commit', 'additional message'], opt); gitS.on('end', function(err) { - if(err) {console.error(err); } - setTimeout(function(){ + if (err) {console.error(err); } + setTimeout(function() { var result = fs.readFileSync(util.testCommit) .toString('utf8'); result.should.match(/initial commit/); @@ -92,18 +95,21 @@ module.exports = function(git, util){ }); gitS.write(fakeFile); gitS.end(); - }); + }); }); it('should commit a file to the repo when passing a message with newlines', function(done) { var fakeFile = util.testOptionsFiles[10]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/', disableAppendPaths: true}; var gitS = git.commit('initial commit\nadditional message', opt); gitS.on('end', function(err) { - if(err) {console.error(err); } - setTimeout(function(){ + if (err) {console.error(err); } + setTimeout(function() { var result = fs.readFileSync(util.testCommit) .toString('utf8'); result.should.match(/initial commit\nadditional message/); @@ -112,18 +118,21 @@ module.exports = function(git, util){ }); gitS.write(fakeFile); gitS.end(); - }); + }); }); it('should commit a file to the repo when passing multiple messages and multiline option', function(done) { var fakeFile = util.testOptionsFiles[11]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/', disableAppendPaths: true, multiline: true}; var gitS = git.commit(['initial commit', 'additional message'], opt); gitS.on('end', function(err) { - if(err) {console.error(err); } - setTimeout(function(){ + if (err) {console.error(err); } + setTimeout(function() { var result = fs.readFileSync(util.testCommit) .toString('utf8'); result.should.match(/initial commit\nadditional message/); @@ -132,48 +141,60 @@ module.exports = function(git, util){ }); gitS.write(fakeFile); gitS.end(); - }); + }); }); it('should not fire a data event by default', function(done) { var fakeFile = util.testOptionsFiles[9]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/'}; var gitS = git.commit('initial commit', opt); var gotData = false; - var wasDone = false; gitS.on('data', function(data) { - gotData = true; + if (!isVinyl(data)) { + console.log(data); + gotData = true; + } }); - gitS.on('end', function() { - gotData.should.be.false; - if(!wasDone) { - done(); - wasDone=true; - } + gitS.once('end', function() { + gotData.should.be.false(); + done(); }); gitS.write(fakeFile); gitS.end(); - }); + }); }); it('should fire a data event if emitData is true', function(done) { var fakeFile = util.testOptionsFiles[6]; exec('git add ' + fakeFile.path, {cwd: './test/repo/'}, - function (error, stdout, stderr) { + function (error) { + if (error) { + return done(error); + } var opt = {cwd: './test/repo/', emitData: true}; var gitS = git.commit('initial commit', opt); - gitS.once('data', function(data) { - if (data) { - done(); + var gotData = false; + + gitS.on('data', function(data) { + if (!isVinyl(data)) { + gotData = true; } }); + + gitS.once('end', function() { + gotData.should.be.true(); + done(); + }); gitS.write(fakeFile); gitS.end(); - }); + }); }); }; diff --git a/test/diff.js b/test/diff.js new file mode 100644 index 0000000..e9afe19 --- /dev/null +++ b/test/diff.js @@ -0,0 +1,56 @@ +'use strict'; + +var should = require('should'); + +module.exports = function(git) { + it('diff files', function(done) { + var diffFile = []; + git.diff('1.12.0...1.13.0').on('data', function (file) { + diffFile.push(file); + }).on('finish', function() { + var subPath = []; + var diffInfo = []; + diffFile.forEach(function(file) { + subPath.push(file.relative.replace(/\\/g, '/')); + diffInfo.push(file.git.diff); + should(file.isNull()).not.be.ok(); + }); + should.deepEqual(subPath, ['LICENSE', 'README.md', 'lib/tag.js', 'package.json']); + should.deepEqual(diffInfo, [ + { srcMode: '100644', + dstMode: '100644', + srcHash: '369b1d7', + dstHash: '3834582', + status: 'M', + srcPath: 'LICENSE', + dstPath: 'LICENSE' + }, + { srcMode: '100644', + dstMode: '100644', + srcHash: '783f6c7', + dstHash: 'd79e3b2', + status: 'M', + srcPath: 'README.md', + dstPath: 'README.md' + }, + { srcMode: '100644', + dstMode: '100644', + srcHash: '3fa85aa', + dstHash: 'cdebb2e', + status: 'M', + srcPath: 'lib/tag.js', + dstPath: 'lib/tag.js' + }, + { srcMode: '100644', + dstMode: '100644', + srcHash: 'f128959', + dstHash: '1ade7a9', + status: 'M', + srcPath: 'package.json', + dstPath: 'package.json' + } + ]); + done(); + }).on('error', done); + }); +}; diff --git a/test/exec.js b/test/exec.js index 1250d27..5447ba8 100644 --- a/test/exec.js +++ b/test/exec.js @@ -1,19 +1,15 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { - it('should git.exec log', function(done){ + it('should git.exec log', function(done) { var opt = {args: 'log', cwd: 'test/repo'}; - git.exec(opt, function(err, stdout){ + git.exec(opt, function(err, stdout) { should(stdout.match(/commit|Author|Date/g)) .have.property('length'); - done(); + done(); }); }); }; diff --git a/test/fetch.js b/test/fetch.js index 01df329..2205279 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -7,21 +7,21 @@ var rimraf = require('rimraf'); var should = require('should'); var exec = require('child_process').exec; -module.exports = function(git, util){ +module.exports = function(git) { - beforeEach(function(done){ + beforeEach(function(done) { var repo = 'git://github.com/stevelacy/git-test'; - git.clone(repo, {args: './test/tmp'}, function(){ - exec('git update-ref -d refs/tags/v1.1.1', {cwd: './test/tmp'}, function(err){ + git.clone(repo, {args: './test/tmp'}, function() { + exec('git update-ref -d refs/tags/v1.1.1', {cwd: './test/tmp'}, function(err) { if (err) return done(err); done(); }); }); }); - it('should fetch a tag from remote origin', function(done){ - git.fetch('origin', '', {cwd: './test/tmp'}, function(){ + it('should fetch a tag from remote origin', function(done) { + git.fetch('origin', '', {cwd: './test/tmp'}, function() { fs.open('./test/tmp/.git/refs/tags/v1.1.1', 'r', function(err, fd) { should.not.exist(err); fs.close(fd, function() { @@ -31,9 +31,9 @@ module.exports = function(git, util){ }); }); - afterEach(function(done){ - rimraf('./test/tmp', function(err){ - if(err) return done(err); + afterEach(function(done) { + rimraf('./test/tmp', function(err) { + if (err) return done(err); done(); }); }); diff --git a/test/init.js b/test/init.js index f456f64..b286598 100644 --- a/test/init.js +++ b/test/init.js @@ -1,19 +1,16 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, testFiles, testCommit){ +module.exports = function(git) { - before(function(done){ + before(function(done) { git.init({cwd: './test/repo/'}, done); }); it('should initialize a empty git repo', function(done) { - fs.stat('test/repo/.git/', function(err, stats) { + fs.stat('test/repo/.git/', function(err) { should.not.exist(err); done(); }); diff --git a/test/main.js b/test/main.js index 0d874c8..3aa1300 100644 --- a/test/main.js +++ b/test/main.js @@ -1,33 +1,34 @@ 'use strict'; +// omit logging +var mock = require('mock-require'); +mock('fancy-log', function() {}); + var path = require('path'); var rimraf = require('rimraf'); -var gutil = require('gulp-util'); var git = require('../'); // just so this file is clean var util = require('./_util'); -// omit logging -gutil.log = function(){}; - - -describe('gulp-git', function(){ +describe('gulp-git', function() { var testSuite = util.testSuite(); - testSuite.forEach(function(file){ + testSuite.forEach(function(file) { var suite = path.basename(file, path.extname(file)); - describe(suite, function(){ + describe(suite, function() { // the actual suite code - require('./'+file)(git, util); + if (/\.js$/.test(file)) { + require('./' + file)(git, util); + } }); }); // wipe - after(function(done){ - rimraf('test/repo', function(err){ - if(err) return done(err); + after(function(done) { + rimraf('test/repo', function(err) { + if (err) return done(err); done(); }); }); diff --git a/test/merge.js b/test/merge.js index d8a0473..c70431e 100644 --- a/test/merge.js +++ b/test/merge.js @@ -1,17 +1,13 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git, util) { - it('should merge branches', function(done){ + it('should merge branches', function(done) { var opt = {cwd: './test/repo'}; - git.merge('testBranch', opt, function(){ - setTimeout(function(){ + git.merge('testBranch', opt, function() { + setTimeout(function() { fs.readFileSync(util.testCommit) .toString('utf8') .should.match(/initial commit/); diff --git a/test/pull.js b/test/pull.js index c1e8c2f..1b84bd5 100644 --- a/test/pull.js +++ b/test/pull.js @@ -1,21 +1,14 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, testFiles, testCommit){ +module.exports = function(git) { - /* - it('should pull from the remote repo', function(done) { - git.pull('origin', 'master', {cwd: "./test/"}, function(){ - should.exist('./test/.git/refs/heads/master'); - done(); - }); + it.skip('should pull from the remote repo', function(done) { + git.pull('origin', 'master', {cwd: './test/'}, function() { + should.exist('./test/.git/refs/heads/master'); + done(); }); - - */ + }); }; diff --git a/test/removeRemote.js b/test/removeRemote.js index ff16196..48cd39f 100644 --- a/test/removeRemote.js +++ b/test/removeRemote.js @@ -1,17 +1,14 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { it('should remove the Remote origin from the git repo', function(done) { var opt = {cwd: './test/repo/'}; - git.removeRemote('origin', opt, function(){ - fs.stat('./test/repo/.git/', function(err, stats){ + git.removeRemote('origin', opt, function() { + fs.stat('./test/repo/.git/', function(err) { should.not.exist(err); fs.readFileSync('./test/repo/.git/config') .toString('utf8') @@ -21,4 +18,12 @@ module.exports = function(git, util){ }); }); + it('should return an error if no remote exists', function(done) { + var opt = {cwd: './test/repo/'}; + git.removeRemote(opt, function(e) { + should(e.message).match('gulp-git: remote is required git.removeRemote("origin")'); + done(); + }); + }); + }; diff --git a/test/rm.js b/test/rm.js index bcc743b..2710736 100644 --- a/test/rm.js +++ b/test/rm.js @@ -1,21 +1,18 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); +var Vinyl = require('vinyl'); -module.exports = function(git, util){ +module.exports = function(git, util) { it('should rm a file', function(done) { var opt = {args: '-f', cwd: 'test/repo'}; - var fakeFile = new gutil.File(util.testFiles[0]); + var fakeFile = new Vinyl(util.testFiles[0]); var gitS = git.rm(opt); gitS.once('data', function (newFile) { - setTimeout(function(){ - fs.exists('test/repo/'+newFile, function(exists) { - exists.should.be.false; + setTimeout(function() { + fs.exists('test/repo/' + newFile, function(exists) { + exists.should.be.false(); }); done(); }, 100); @@ -26,19 +23,19 @@ module.exports = function(git, util){ it('should rm multiple files', function(done) { var fakeFiles = []; - util.testFiles.slice(1).forEach(function(file){ - fakeFiles.push(new gutil.File(file)); + util.testFiles.slice(1).forEach(function(file) { + fakeFiles.push(new Vinyl(file)); }); var opt = {args: '-f', cwd: 'test/repo'}; var gitS = git.rm(opt); gitS.on('data', function (newFile) { - fs.exists('test/repo/'+newFile, function(exists) { - exists.should.be.false; + fs.exists('test/repo/' + newFile, function(exists) { + exists.should.be.false(); }); }); gitS.once('end', done); - fakeFiles.forEach(function(fake){ + fakeFiles.forEach(function(fake) { gitS.write(fake); }); gitS.end(); diff --git a/test/stash.js b/test/stash.js index 0d5e8ad..4cd70db 100644 --- a/test/stash.js +++ b/test/stash.js @@ -1,12 +1,8 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); -var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, util){ +module.exports = function(git) { it('should stash a branch', function(done) { var opt = {cwd: './test/repo'}; @@ -20,8 +16,8 @@ module.exports = function(git, util){ it('should unstash a branch', function(done) { var opt = {cwd: './test/repo', args: 'pop'}; - git.stash(opt, function(){ - fs.open('test/repo/.git/refs/stash', 'r', function(err, fd) { + git.stash(opt, function() { + fs.open('test/repo/.git/refs/stash', 'r', function(err) { err.code.should.be.exactly('ENOENT'); done(); }); diff --git a/test/status.js b/test/status.js index a8da57e..2a42045 100644 --- a/test/status.js +++ b/test/status.js @@ -2,23 +2,22 @@ var fs = require('fs'); var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); +var Vinyl = require('vinyl'); -module.exports = function(git, util){ +module.exports = function(git, util) { - it('should git status --porcelain', function(done){ + it('should git status --porcelain', function(done) { var opt = {args: '--porcelain', cwd: 'test/repo'}; - var fakeFile = new gutil.File(util.testFiles[0]); + var fakeFile = new Vinyl(util.testFiles[0]); var fakeRelative = '?? ' + path.relative(util.repo, fakeFile.path); fs.openSync(fakeFile.path, 'w'); - git.status(opt, function(err, stdout){ + git.status(opt, function(err, stdout) { should(err).be.eql(null); - fs.exists(fakeFile.path, function(exists){ - exists.should.be.true; + fs.exists(fakeFile.path, function(exists) { + exists.should.be.true(); stdout.split('\n') .should.containDeep([fakeRelative]); done(); diff --git a/test/submodule.js b/test/submodule.js index 9875218..8577e1d 100644 --- a/test/submodule.js +++ b/test/submodule.js @@ -1,44 +1,42 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, testFiles, testCommit){ +module.exports = function(git) { - it('should add a submodule to the git repo', function(done){ + it('should add a submodule to the git repo', function(done) { var opt = {cwd: 'test/repo'}; var url = 'https://github.com/stevelacy/git-test'; - git.addSubmodule(url, 'testSubmodule', opt, function(){ + git.addSubmodule(url, 'testSubmodule', opt, function() { fs.readFileSync('test/repo/.gitmodules') .toString('utf8') .should.match(new RegExp(url.replace(/[\/]/g, '\\$&'))); - fs.stat('test/repo/testSubmodule/.git', function(err, stats){ + fs.stat('test/repo/testSubmodule/.git', function(err) { should.not.exist(err); done(); }); }); }); - it('should update submodules', function(done){ + it('should update submodules', function(done) { var args = {cwd: 'test/repo'}; - git.updateSubmodule(args, function(){ - fs.stat('test/repo/testSubmodule/.git', function(err, stats){ + git.updateSubmodule(args, function() { + fs.stat('test/repo/testSubmodule/.git', function(err) { should.not.exist(err); done(); }); }); }); - after(function(done){ - rimraf('test/repo/testSubmodule', function(err){ - if(err) return done(err); + after(function(done) { + rimraf('test/repo/testSubmodule', function(err) { + if (err) return done(err); done(); }); }); diff --git a/test/tag.js b/test/tag.js index dc55ec9..2d20f2b 100644 --- a/test/tag.js +++ b/test/tag.js @@ -1,19 +1,16 @@ 'use strict'; var fs = require('fs'); -var path = require('path'); -var rimraf = require('rimraf'); var should = require('should'); -var gutil = require('gulp-util'); -module.exports = function(git, testFiles, testCommit){ +module.exports = function(git) { // These must be run on a system which has git installed // no pull delay, and has git configured. it('should tag a version of the repo', function(done) { git.tag('v1.2.3', 'message', {cwd: './test/repo/'}, function() { - fs.stat('test/repo/.git/refs/tags/v1.2.3', function(err, stats) { + fs.stat('test/repo/.git/refs/tags/v1.2.3', function(err) { should.not.exist(err); done(); }); @@ -30,10 +27,19 @@ module.exports = function(git, testFiles, testCommit){ it('should tag a version with an empty message', function(done) { git.tag('v3', '', {cwd: './test/repo/'}, function(err) { should.not.exist(err); - fs.stat('test/repo/.git/refs/tags/v3', function(err, stats) { + fs.stat('test/repo/.git/refs/tags/v3', function(err) { should.not.exist(err); done(); }); }); }); + + it('should return all tags', function(done) { + git.tag(function(err, tags) { + should(tags).not.be.null(); + should(tags[0]).not.be.null(); + should(tags.length > 10); + done(); + }); + }); };