From bb701d9af29bfdcc485eed15d9dc49f5ebfc0bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Fri, 28 Jul 2017 14:02:55 +0800 Subject: [PATCH 1/9] Add eslint --- .eslintignore | 3 ++ .eslintrc.json | 47 +++++++++++++++++++++++++ .jshintrc | 31 ---------------- .npmignore | 1 + generate/templates/templates/nodegit.js | 22 +++++++----- lifecycleScripts/lint.js | 18 ++++++++++ package.json | 9 +++-- test/.eslintrc.json | 7 ++++ 8 files changed, 95 insertions(+), 43 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc.json delete mode 100644 .jshintrc create mode 100644 lifecycleScripts/lint.js create mode 100644 test/.eslintrc.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..805a8d52e --- /dev/null +++ b/.eslintignore @@ -0,0 +1,3 @@ +generate/templates/templates/**/* +test/repos/**/* +dist/**/* diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 000000000..8866c3b61 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,47 @@ +{ + "env": { + "node": true, + "es6": true + }, + "root": true, + "rules": { + "no-cond-assign": [ + "error", + "except-parens" + ], + "curly": [ + "error" + ], + "no-eq-null": [ + "error" + ], + "no-eval": [ + "error" + ], + "wrap-iife": [ + "error" + ], + "max-len": [ + "error", + { + "code": 80, + "ignoreComments": true + } + ], + "no-proto": [ + "error" + ], + "quotes": [ + "error" + ], + "no-undef": [ + "error" + ], + "no-unused-vars": [ + "error" + ], + "no-invalid-this": [ + "error" + ] + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 0fd02f29b..000000000 --- a/.jshintrc +++ /dev/null @@ -1,31 +0,0 @@ -{ - "boss": true, - "curly": true, - "eqnull": true, - "esnext": true, - "evil": true, - "futurehostile": true, - "globals": { - "after": true, - "afterEach": true, - "before": true, - "beforeEach": true, - "define": true, - "describe": true, - "global": true, - "it": true - }, - "immed": false, - "maxlen": 80, - "node": true, - "predef": [ - "Promise", - "Set" - ], - "proto": true, - "quotmark": "double", - "trailing": true, - "undef": true, - "unused": "vars", - "validthis": true -} diff --git a/.npmignore b/.npmignore index 046bdf56a..dc0bc3ed8 100644 --- a/.npmignore +++ b/.npmignore @@ -15,6 +15,7 @@ .gitignore .gitmodules .jshintrc +.eslintrc* .travis.yml appveyor.yml diff --git a/generate/templates/templates/nodegit.js b/generate/templates/templates/nodegit.js index 84bc558b8..463e86b4f 100644 --- a/generate/templates/templates/nodegit.js +++ b/generate/templates/templates/nodegit.js @@ -1,3 +1,5 @@ +/* eslint no-unused-vars: "off" */ + var promisify = require("promisify-node"); var rawApi; @@ -17,7 +19,6 @@ catch (ex) { // Native methods do not return an identifiable function, so we // have to override them here -/* jshint ignore:start */ {% each . as idef %} {% if idef.type != "enum" %} @@ -34,14 +35,18 @@ catch (ex) { var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} = _{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }}; _{{ idef.jsClassName }}.prototype.{{ fn.jsFunctionName }} - = promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}}); + = promisify( + _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} + ); {% else %} var _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} = _{{ idef.jsClassName }}.{{ fn.jsFunctionName }}; _{{ idef.jsClassName }}.{{ fn.jsFunctionName }} - = promisify(_{{ idef.jsClassName }}_{{ fn.jsFunctionName}}); + = promisify( + _{{ idef.jsClassName }}_{{ fn.jsFunctionName}} + ); {% endif %} @@ -58,10 +63,9 @@ _ConvenientPatch.prototype.hunks = promisify(_ConvenientPatch_hunks); var _ConvenientHunk = rawApi.ConvenientHunk; var _ConvenientHunk_lines = _ConvenientHunk.prototype.lines; _ConvenientHunk.prototype.lines = promisify(_ConvenientHunk_lines); -/* jshint ignore:end */ // Set the exports prototype to the raw API. -exports.__proto__ = rawApi; +Object.setPrototypeOf(exports, rawApi) var importExtension = function(name) { try { @@ -91,15 +95,16 @@ require("./enums.js"); importExtension("{{ filename }}"); {% endif %} {% endeach %} -/* jshint ignore:start */ {% each . as idef %} {% if idef.type != "enum" %} {% each idef.functions as fn %} {% if fn.useAsOnRootProto %} // Inherit directly from the original {{idef.jsClassName}} object. - _{{ idef.jsClassName }}.{{ fn.jsFunctionName }}.__proto__ = - _{{ idef.jsClassName }}; + Object.setPrototypeOf( + _{{ idef.jsClassName }}.{{ fn.jsFunctionName }}, + _{{ idef.jsClassName }} + ); // Ensure we're using the correct prototype. _{{ idef.jsClassName }}.{{ fn.jsFunctionName }}.prototype = @@ -113,7 +118,6 @@ require("./enums.js"); {% endeach %} {% endif %} {% endeach %} -/* jshint ignore:end */ // Wrap asynchronous methods to return promises. promisify(exports); diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js new file mode 100644 index 000000000..a53d317a6 --- /dev/null +++ b/lifecycleScripts/lint.js @@ -0,0 +1,18 @@ +const vfs = require("vinyl-fs"); +const eslint = require("gulp-eslint"); +const reporter = require("gulp-reporter"); +vfs.src([ + "**/*.js", + "!node_modules/**/*", + "!dist/**/*", + "!test/repos/**/*" +]) + .pipe(eslint()) + .pipe(reporter({ + // fail only error write before 2017-7-1 + expires: new Date("2017-7-1"), + author: null, + })).resume().on("error", (e) => { + console.error(String(e)) + process.exitCode = -1 + }); diff --git a/package.json b/package.json index 5becdd1c1..ca2cb4160 100644 --- a/package.json +++ b/package.json @@ -48,11 +48,14 @@ "clean-for-publish": "~1.0.2", "combyne": "~0.8.1", "coveralls": "~2.11.4", + "eslint": "^4.3.0", + "gulp-eslint": "^4.0.0", + "gulp-reporter": "^2.1.0", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", - "jshint": "~2.8.0", "lcov-result-merger": "~1.0.2", "mocha": "~2.3.4", + "vinyl-fs": "^2.4.4", "walk": "^2.3.9" }, "vendorDependencies": { @@ -75,7 +78,6 @@ "generateNativeCode": "node generate/scripts/generateNativeCode", "install": "node lifecycleScripts/preinstall && node lifecycleScripts/install", "installDebug": "BUILD_DEBUG=true npm install", - "lint": "jshint lib test/tests test/utils examples lifecycleScripts", "mergecov": "lcov-result-merger 'test/**/*.info' 'test/coverage/merged.lcov' && ./lcov-1.10/bin/genhtml test/coverage/merged.lcov --output-directory test/coverage/report", "mocha": "mocha test/runner test/tests --timeout 15000", "mochaDebug": "mocha --debug-brk test/runner test/tests --timeout 15000", @@ -85,7 +87,8 @@ "rebuildDebug": "node generate && npm run babel && node-gyp configure --debug build", "recompile": "node-gyp configure build", "recompileDebug": "node-gyp configure --debug build", - "test": "npm run lint && node --expose-gc test", + "pretest": "node lifecycleScripts/lint.js", + "test": "node --expose-gc test", "xcodeDebug": "node-gyp configure -- -f xcode" } } diff --git a/test/.eslintrc.json b/test/.eslintrc.json new file mode 100644 index 000000000..5075d5977 --- /dev/null +++ b/test/.eslintrc.json @@ -0,0 +1,7 @@ +{ + "env": { + "mocha": true, + "node": true, + "es6": true + } +} From 5b0ad5fa2c69bb9250b7455abceb841217a86384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Fri, 28 Jul 2017 14:59:24 +0800 Subject: [PATCH 2/9] debug --- .travis.yml | 6 +++--- appveyor.yml | 4 ++-- package.json | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 862d1eb6c..01920d336 100644 --- a/.travis.yml +++ b/.travis.yml @@ -63,7 +63,7 @@ before_install: fi install: - - npm install; + - travis_retry npm install; # This is a random private key used purely for testing. before_script: @@ -78,9 +78,9 @@ before_script: script: if [ -z "$TRAVIS_TAG" ] && [ $TRAVIS_OS_NAME == "linux" ] && [ $NODE_VERSION == "6" ]; then - npm test && npm run cov && npm run coveralls; + travis_retry npm test && npm run cov && npm run coveralls; else - npm test; + travis_retry npm test; fi after_success: diff --git a/appveyor.yml b/appveyor.yml index 9ffa0bd02..eb297f9d8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -39,12 +39,12 @@ install: - ps: Start-Process c:\projects\nodegit\vendor\pageant.exe c:\projects\nodegit\vendor\private.ppk - npm install -g npm - cmd: npm install -g node-gyp - - npm install + - appveyor-retry call npm install test_script: - node --version - npm --version - - cmd: npm test + - appveyor-retry call npm test on_success: - IF %APPVEYOR_REPO_TAG%==true npm install -g node-pre-gyp diff --git a/package.json b/package.json index ca2cb4160..2982c1577 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "coveralls": "~2.11.4", "eslint": "^4.3.0", "gulp-eslint": "^4.0.0", - "gulp-reporter": "^2.1.0", + "gulp-reporter": "github:gucong3000/gulp-reporter#node_4", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", "lcov-result-merger": "~1.0.2", From afacad721b687b87a194d6cbca168b906ffae13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Fri, 28 Jul 2017 15:35:16 +0800 Subject: [PATCH 3/9] test for gulp-reporter v2.1.1 --- lifecycleScripts/lint.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js index a53d317a6..ba4839512 100644 --- a/lifecycleScripts/lint.js +++ b/lifecycleScripts/lint.js @@ -9,8 +9,8 @@ vfs.src([ ]) .pipe(eslint()) .pipe(reporter({ - // fail only error write before 2017-7-1 - expires: new Date("2017-7-1"), + // fail only error write before 2017-7-27 + expires: new Date("2017-7-27"), author: null, })).resume().on("error", (e) => { console.error(String(e)) diff --git a/package.json b/package.json index 2982c1577..5e504d54d 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "coveralls": "~2.11.4", "eslint": "^4.3.0", "gulp-eslint": "^4.0.0", - "gulp-reporter": "github:gucong3000/gulp-reporter#node_4", + "gulp-reporter": "github:gucong3000/gulp-reporter#v2.1.1", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", "lcov-result-merger": "~1.0.2", From 8a741179cadcf3bb290898234bba03f90c5c465e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Fri, 28 Jul 2017 15:53:41 +0800 Subject: [PATCH 4/9] update gulp-reporter --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5e504d54d..3b9c78424 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "coveralls": "~2.11.4", "eslint": "^4.3.0", "gulp-eslint": "^4.0.0", - "gulp-reporter": "github:gucong3000/gulp-reporter#v2.1.1", + "gulp-reporter": "^2.1.1", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", "lcov-result-merger": "~1.0.2", From 420e5829f0848ee2ab1bb947f36da558e823d1a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Mon, 31 Jul 2017 11:16:43 +0800 Subject: [PATCH 5/9] set eslint expires date to 2017-7-31 --- lifecycleScripts/lint.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js index ba4839512..e4d68b977 100644 --- a/lifecycleScripts/lint.js +++ b/lifecycleScripts/lint.js @@ -9,8 +9,8 @@ vfs.src([ ]) .pipe(eslint()) .pipe(reporter({ - // fail only error write before 2017-7-27 - expires: new Date("2017-7-27"), + // fail only error write before 2017-7-31 + expires: new Date("2017-7-31"), author: null, })).resume().on("error", (e) => { console.error(String(e)) From d51f877ba772b76c94e6c4f144f328dba16be9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Wed, 2 Aug 2017 20:42:20 +0800 Subject: [PATCH 6/9] update eslint err fail date config --- lifecycleScripts/lint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js index e4d68b977..0afa1e75c 100644 --- a/lifecycleScripts/lint.js +++ b/lifecycleScripts/lint.js @@ -10,7 +10,7 @@ vfs.src([ .pipe(eslint()) .pipe(reporter({ // fail only error write before 2017-7-31 - expires: new Date("2017-7-31"), + expires: new Date("2017-8-2"), author: null, })).resume().on("error", (e) => { console.error(String(e)) From c6f6d4013a75589552d00757e37d52aa3146d561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Mon, 14 Aug 2017 10:37:52 +0800 Subject: [PATCH 7/9] update deps --- lifecycleScripts/lint.js | 3 ++- package.json | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js index 0afa1e75c..1317eedf5 100644 --- a/lifecycleScripts/lint.js +++ b/lifecycleScripts/lint.js @@ -5,7 +5,8 @@ vfs.src([ "**/*.js", "!node_modules/**/*", "!dist/**/*", - "!test/repos/**/*" + "!test/repos/**/*", + "!.git/**/*" ]) .pipe(eslint()) .pipe(reporter({ diff --git a/package.json b/package.json index 3b9c78424..be42a9dfa 100644 --- a/package.json +++ b/package.json @@ -48,9 +48,9 @@ "clean-for-publish": "~1.0.2", "combyne": "~0.8.1", "coveralls": "~2.11.4", - "eslint": "^4.3.0", + "eslint": "^4.4.1", "gulp-eslint": "^4.0.0", - "gulp-reporter": "^2.1.1", + "gulp-reporter": "^2.2.1", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", "lcov-result-merger": "~1.0.2", From aa8356dc5056b501b11081349fe049abfd474ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Wed, 16 Aug 2017 17:06:28 +0800 Subject: [PATCH 8/9] lint by git flow --- lifecycleScripts/lint.js | 164 ++++++++++++++++++++++++++++++++++++--- package.json | 8 +- 2 files changed, 158 insertions(+), 14 deletions(-) diff --git a/lifecycleScripts/lint.js b/lifecycleScripts/lint.js index 1317eedf5..7b378cfc6 100644 --- a/lifecycleScripts/lint.js +++ b/lifecycleScripts/lint.js @@ -1,19 +1,159 @@ -const vfs = require("vinyl-fs"); const eslint = require("gulp-eslint"); const reporter = require("gulp-reporter"); -vfs.src([ - "**/*.js", - "!node_modules/**/*", - "!dist/**/*", - "!test/repos/**/*", - "!.git/**/*" -]) - .pipe(eslint()) +const { + Repository, + Reference, + Diff, + Blob, + Merge, +} = require("../"); +const through = require("through2"); +const Vinyl = require("vinyl"); +const debug = require("gulp-debug"); + +function getRepository(){ + return Repository.open(process.env.GIT_DIR || ".git"); +} + +function cached() { + return getRepository().then(repository => ( + Promise.all([ + Reference.nameToId(repository, "HEAD").then(head => ( + repository.getCommit(head) + )).then(commit => ( + commit.getTree() + )), + repository.index(), + ]).then(([old_tree, index]) => ( + Diff.treeToIndex(repository, old_tree, index).then(diff => ( + diff.patches() + )).then(arrayConvenientPatch => ( + arrayConvenientPatch.map(convenientPatch => ({ + repository: repository, + index: index, + convenientPatch: convenientPatch, + indexEntry: index.getByPath(convenientPatch.newFile().path()), + })) + )) + )) + )); +} + +function diff() { + return getRepository().then(repository => ( + Promise.all([ + repository.getMasterCommit().catch(()=>( + repository.getBranchCommit("origin/master") + )), + repository.getHeadCommit(), + repository.index(), + ]).then(([masterCommit, headCommit, index]) => ( + Merge.base(repository, masterCommit, headCommit).then(oid => ( + repository.getCommit(oid) + )).then(commit => ( + commit.getTree() + )).then(old_tree => ( + Diff.treeToWorkdirWithIndex(repository, old_tree) + )).then(diff => ( + diff.patches() + )).then(arrayConvenientPatch => ( + arrayConvenientPatch.filter(convenientPatch => ( + !convenientPatch.isDeleted() + )).map(convenientPatch => ({ + repository: repository, + index: index, + convenientPatch: convenientPatch, + indexEntry: index.getByPath(convenientPatch.newFile().path()), + })) + )) + )) + )); +} + +function readBlob(matcher) { + return through.obj(function (file, enc, callback) { + const { + repository, + indexEntry, + } = file.git; + Blob.lookup(repository, indexEntry.id).then(function(blob) { + if(!blob.isBinary() && matcher.test(file.path)) { + file.git.content = blob.content(); + file.contents = file.git.content; + callback(null, file); + } else { + callback(); + } + }).catch(callback); + }); +} + +function updateIndex() { + let index; + return through.obj(function (file, enc, callback) { + if(file.contents.equals(file.git.content)) { + callback(null, file); + return; + } else { + const { + repository, + indexEntry, + } = file.git; + + index = file.git.index; + + indexEntry.id = Blob.createFromBuffer( + repository, + file.contents, + Buffer.byteLength(file.contents) + ); + + index.add(indexEntry).then(() => ( + callback(null, file) + )).catch(callback); + } + }, function (cb) { + // flush function + if(index) { + index.write().then(cb); + } else { + cb(); + } + }); +} + +function toStream(fn) { + var stream = through.obj(); + fn().then(files => { + files.forEach(file => { + stream.push(new Vinyl({ + base: process.cwd(), + cwd: __dirname, + path: file.indexEntry.path, + git: file, + })); + }); + stream.end(); + }).catch(ex => { + stream.emit("error", ex); + }); + return stream; +} + +toStream(process.env.GIT_AUTHOR_DATE ? cached : diff) + .pipe(readBlob(/(jsx?|es\d*)$/)) + .pipe(debug({ + title: "eslint" + })) + .pipe(eslint({ + fix: process.argv.indexOf("--fix") > 0, + })) + .pipe(updateIndex()) .pipe(reporter({ - // fail only error write before 2017-7-31 + // fail only for new error. expires: new Date("2017-8-2"), author: null, })).resume().on("error", (e) => { - console.error(String(e)) - process.exitCode = -1 + console.error(String(e)); + process.exitCode = -1; }); diff --git a/package.json b/package.json index be42a9dfa..3f79db7b8 100644 --- a/package.json +++ b/package.json @@ -49,13 +49,16 @@ "combyne": "~0.8.1", "coveralls": "~2.11.4", "eslint": "^4.4.1", + "gulp-debug": "^3.1.0", "gulp-eslint": "^4.0.0", "gulp-reporter": "^2.2.1", + "husky": "^0.14.3", "istanbul": "~0.3.20", "js-beautify": "~1.5.10", "lcov-result-merger": "~1.0.2", "mocha": "~2.3.4", - "vinyl-fs": "^2.4.4", + "through2": "^2.0.3", + "vinyl": "^2.1.0", "walk": "^2.3.9" }, "vendorDependencies": { @@ -87,7 +90,8 @@ "rebuildDebug": "node generate && npm run babel && node-gyp configure --debug build", "recompile": "node-gyp configure build", "recompileDebug": "node-gyp configure --debug build", - "pretest": "node lifecycleScripts/lint.js", + "precommit": "npm run pretest -- --fix", + "pretest": "node lifecycleScripts/lint", "test": "node --expose-gc test", "xcodeDebug": "node-gyp configure -- -f xcode" } From 66297aa7f3d31f3f3d6c63a01d5722c8b181272a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=BA?= Date: Wed, 16 Aug 2017 17:45:19 +0800 Subject: [PATCH 9/9] update npm scripts --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3f79db7b8..25d7df96b 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,8 @@ "generateNativeCode": "node generate/scripts/generateNativeCode", "install": "node lifecycleScripts/preinstall && node lifecycleScripts/install", "installDebug": "BUILD_DEBUG=true npm install", + "lint": "node lifecycleScripts/lint", + "lint:fix": "npm run lint -- --fix", "mergecov": "lcov-result-merger 'test/**/*.info' 'test/coverage/merged.lcov' && ./lcov-1.10/bin/genhtml test/coverage/merged.lcov --output-directory test/coverage/report", "mocha": "mocha test/runner test/tests --timeout 15000", "mochaDebug": "mocha --debug-brk test/runner test/tests --timeout 15000", @@ -90,8 +92,8 @@ "rebuildDebug": "node generate && npm run babel && node-gyp configure --debug build", "recompile": "node-gyp configure build", "recompileDebug": "node-gyp configure --debug build", - "precommit": "npm run pretest -- --fix", - "pretest": "node lifecycleScripts/lint", + "precommit": "npm run lint:fix", + "pretest": "npm run lint", "test": "node --expose-gc test", "xcodeDebug": "node-gyp configure -- -f xcode" }