From dc00488eeaa46a0196e3dc836524de6536a663c0 Mon Sep 17 00:00:00 2001 From: oleg Date: Sat, 10 May 2014 03:27:35 +0400 Subject: [PATCH] add base scm class, mercurial class with tests --- lib/scm/base.js | 72 +++++++++++ lib/scm/index.js | 8 ++ lib/scm/mercurial.js | 53 ++++++++ package.json | 35 ++++++ test/repos/.hg/00changelog.i | Bin 0 -> 57 bytes test/repos/.hg/branch | 1 + test/repos/.hg/cache/branchheads-served | 2 + test/repos/.hg/cache/tags | 2 + test/repos/.hg/dirstate | Bin 0 -> 90 bytes test/repos/.hg/requires | 4 + test/repos/.hg/store/00changelog.i | Bin 0 -> 308 bytes test/repos/.hg/store/00manifest.i | Bin 0 -> 241 bytes test/repos/.hg/store/data/rev0.txt.i | Bin 0 -> 64 bytes test/repos/.hg/store/data/rev1.txt.i | Bin 0 -> 64 bytes test/repos/.hg/store/fncache | 2 + test/repos/.hg/store/phaseroots | 0 test/repos/.hg/store/undo | Bin 0 -> 67 bytes test/repos/.hg/store/undo.phaseroots | 0 test/repos/.hg/undo.bookmarks | 0 test/repos/.hg/undo.branch | 1 + test/repos/.hg/undo.desc | 3 + test/repos/.hg/undo.dirstate | 0 test/repos/mercurial/.hg/00changelog.i | Bin 0 -> 57 bytes .../mercurial/.hg/cache/branchheads-served | 2 + test/repos/mercurial/.hg/cache/tags | 2 + test/repos/mercurial/.hg/dirstate | Bin 0 -> 115 bytes test/repos/mercurial/.hg/last-message.txt | 1 + test/repos/mercurial/.hg/requires | 4 + test/repos/mercurial/.hg/store/00changelog.i | Bin 0 -> 463 bytes test/repos/mercurial/.hg/store/00manifest.i | Bin 0 -> 367 bytes .../repos/mercurial/.hg/store/data/rev0.txt.i | Bin 0 -> 64 bytes .../repos/mercurial/.hg/store/data/rev1.txt.i | Bin 0 -> 64 bytes .../repos/mercurial/.hg/store/data/rev2.txt.i | Bin 0 -> 64 bytes test/repos/mercurial/.hg/store/fncache | 3 + test/repos/mercurial/.hg/store/phaseroots | 1 + test/repos/mercurial/.hg/store/undo | Bin 0 -> 53 bytes .../repos/mercurial/.hg/store/undo.phaseroots | 1 + test/repos/mercurial/.hg/undo.bookmarks | 0 test/repos/mercurial/.hg/undo.branch | 1 + test/repos/mercurial/.hg/undo.desc | 2 + test/repos/mercurial/.hg/undo.dirstate | Bin 0 -> 115 bytes test/repos/mercurial/rev0.txt | 0 test/repos/mercurial/rev1.txt | 0 test/repos/mercurial/rev2.txt | 0 test/repos/rev0.txt | 0 test/repos/rev1.txt | 0 test/scm.js | 116 ++++++++++++++++++ 47 files changed, 316 insertions(+) create mode 100644 lib/scm/base.js create mode 100644 lib/scm/index.js create mode 100644 lib/scm/mercurial.js create mode 100644 package.json create mode 100644 test/repos/.hg/00changelog.i create mode 100644 test/repos/.hg/branch create mode 100644 test/repos/.hg/cache/branchheads-served create mode 100644 test/repos/.hg/cache/tags create mode 100644 test/repos/.hg/dirstate create mode 100644 test/repos/.hg/requires create mode 100644 test/repos/.hg/store/00changelog.i create mode 100644 test/repos/.hg/store/00manifest.i create mode 100644 test/repos/.hg/store/data/rev0.txt.i create mode 100644 test/repos/.hg/store/data/rev1.txt.i create mode 100644 test/repos/.hg/store/fncache create mode 100644 test/repos/.hg/store/phaseroots create mode 100644 test/repos/.hg/store/undo create mode 100644 test/repos/.hg/store/undo.phaseroots create mode 100644 test/repos/.hg/undo.bookmarks create mode 100644 test/repos/.hg/undo.branch create mode 100644 test/repos/.hg/undo.desc create mode 100644 test/repos/.hg/undo.dirstate create mode 100644 test/repos/mercurial/.hg/00changelog.i create mode 100644 test/repos/mercurial/.hg/cache/branchheads-served create mode 100644 test/repos/mercurial/.hg/cache/tags create mode 100644 test/repos/mercurial/.hg/dirstate create mode 100644 test/repos/mercurial/.hg/last-message.txt create mode 100644 test/repos/mercurial/.hg/requires create mode 100644 test/repos/mercurial/.hg/store/00changelog.i create mode 100644 test/repos/mercurial/.hg/store/00manifest.i create mode 100644 test/repos/mercurial/.hg/store/data/rev0.txt.i create mode 100644 test/repos/mercurial/.hg/store/data/rev1.txt.i create mode 100644 test/repos/mercurial/.hg/store/data/rev2.txt.i create mode 100644 test/repos/mercurial/.hg/store/fncache create mode 100644 test/repos/mercurial/.hg/store/phaseroots create mode 100644 test/repos/mercurial/.hg/store/undo create mode 100644 test/repos/mercurial/.hg/store/undo.phaseroots create mode 100644 test/repos/mercurial/.hg/undo.bookmarks create mode 100644 test/repos/mercurial/.hg/undo.branch create mode 100644 test/repos/mercurial/.hg/undo.desc create mode 100644 test/repos/mercurial/.hg/undo.dirstate create mode 100644 test/repos/mercurial/rev0.txt create mode 100644 test/repos/mercurial/rev1.txt create mode 100644 test/repos/mercurial/rev2.txt create mode 100644 test/repos/rev0.txt create mode 100644 test/repos/rev1.txt create mode 100644 test/scm.js diff --git a/lib/scm/base.js b/lib/scm/base.js new file mode 100644 index 0000000..4b6604c --- /dev/null +++ b/lib/scm/base.js @@ -0,0 +1,72 @@ +'use strict'; + +var spawn = require('child_process').spawn, + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits; + +function BaseScm(config) { + var self = this; + self.config = config; + ['src'].forEach(function(key) { + if (key in self.config === false) throw new Error(key + ' is not set'); + self[key] = self.config[key]; + }); + self.cwd = config.cwd; +} + +module.exports = BaseScm; + +inherits(BaseScm, EventEmitter); + +BaseScm.prototype._exec = function(command, args, callback) { + var self = this, + stdout = ''; + var cmd = spawn(command, args, {cwd: this.cwd}); + cmd.stdout.on('data', function(data) { + if (self.isEmit) self.emit('stdout', data); + stdout += data; + }); + cmd.stderr.on('data', function(data) { + callback(new Error('Scm outputs to stderr: ' + data)); + cmd.kill(); + }); + cmd.on('exit', function(code) { + var err = null; + if (code !== 0) err = new Error( + 'Scm command exits with non-zero code: ' + code + ) + callback(err, stdout); + }); + return cmd; +}; + +/** + * Clone repository to the `dst` update to `rev` and set `this.cwd` to `dst` + */ +BaseScm.prototype.clone = function(dst, rev, callback) { +}; + +/** + * Pull changes from remote repository without update + */ +BaseScm.prototype.pull = function(rev, callback) { +}; + +/** + * Returns string id of current revision + */ +BaseScm.prototype.getId = function(callback) { +}; + +/** + * Returns array of changes between revisions + */ +BaseScm.prototype.getChanges = function(rev1, rev2, callback) { +}; + +/** + * Updates to revision + */ +BaseScm.prototype.update = function(rev, callback) { +}; + diff --git a/lib/scm/index.js b/lib/scm/index.js new file mode 100644 index 0000000..dc3046f --- /dev/null +++ b/lib/scm/index.js @@ -0,0 +1,8 @@ +'use strict'; + +var path = require('path'); + +exports.createScm = function(config) { + var Constructor = require(path.join(__dirname, config.type)); + return new Constructor(config); +}; diff --git a/lib/scm/mercurial.js b/lib/scm/mercurial.js new file mode 100644 index 0000000..c0b9d71 --- /dev/null +++ b/lib/scm/mercurial.js @@ -0,0 +1,53 @@ +'use strict'; + +var BaseScm = require('./base'), + inherits = require('util').inherits; + +function MercurialScm(config) { + BaseScm.call(this, config); +} + +module.exports = MercurialScm; + +inherits(MercurialScm, BaseScm); + +MercurialScm.prototype.defaultRev = 'default'; + +MercurialScm.prototype.clone = function(dst, rev, callback) { + var self = this; + this._exec('hg', ['clone', '--rev', rev, this.src, dst], function(err) { + self.cwd = dst; + callback(err); + }); +}; + +MercurialScm.prototype.pull = function(rev, callback) { + this._exec('hg', ['pull', '--rev', rev], callback); +}; + +MercurialScm.prototype.getId = function(callback) { + this._exec('hg', ['id', '--id'], function(err, stdout) { + callback(err, !err && stdout.replace('\n', '')); + }); +}; + +MercurialScm.prototype.getChanges = function(rev1, rev2, callback) { + this._exec('hg', [ + 'log', '--rev', rev2 + ':' + rev1, + '--template', '{node|short};;;{author};;;{date|date};;;{desc}\n' + ], function(err, stdout) { + callback(err, !err && stdout.split('\n').slice(0, -2).map(function(str) { + var parts = str.split(';;;'); + return { + id: parts[0], + author: parts[1], + date: new Date(parts[2]).getTime(), + comment: parts[3] + }; + })); + }); +}; + +MercurialScm.prototype.update = function(rev, callback) { + this._exec('hg', ['up', rev], callback); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..6412bbe --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "nci", + "version": "0.1.0-alpha", + "description": "Continuous integration server written in node.js", + "bin": { + "nci": "bin/nci" + }, + "scripts": { + "test": "mocha --bail --reporter=spec test" + }, + "repository": { + "type": "git", + "url": "git://github.com/okv/nci.git" + }, + "keywords": [ + "continuous", + "integration", + "server", + "ci", + "build" + ], + "author": "Oleg Korobenko ", + "license": "MIT", + "bugs": { + "url": "https://github.com/okv/nci/issues" + }, + "homepage": "https://github.com/okv/nci", + "dependencies": { + "twostep": "0.4.1" + }, + "devDependencies": { + "expect.js": "0.3.1", + "mocha": "1.18.2" + } +} diff --git a/test/repos/.hg/00changelog.i b/test/repos/.hg/00changelog.i new file mode 100644 index 0000000000000000000000000000000000000000..d3a8311050e54c57c5be7cfe169e60a95768812c GIT binary patch literal 57 zcmWN_K?=Yi3hk{$`NzdilCkM`06wQBbHJ6#!rDxY+YCv?w9Nyy>IU5Xk zSP%U7xyU*tTw2<=@0x~E)CHwSU%owmut3>>S(DG!%HHz@Ys^GfUkeW}Moynk{tCqkyZR{D5b%=yWpNy>B1PLiuKW&U!;rO(FSxb1;VNo~Q#>kM9R Q4=?*)dZnyhu0qBW09u!H=Kufz literal 0 HcmV?d00001 diff --git a/test/repos/.hg/store/00manifest.i b/test/repos/.hg/store/00manifest.i new file mode 100644 index 0000000000000000000000000000000000000000..6f4d1ddd7392825d880feaec6cd682d3cb2de9de GIT binary patch literal 241 zcmZQzWME`~0AnC!1Z6Y)hXT$8ybQ-$nWT%uSxn1+K6kD;%)z$kw?(_vN`Al-ab#=ALACj%kF){5LnCYG4$|nP3dG-HQPrLGn6+WD*P;%#K*02?S1MgRZ+ literal 0 HcmV?d00001 diff --git a/test/repos/.hg/store/fncache b/test/repos/.hg/store/fncache new file mode 100644 index 0000000..083cdd8 --- /dev/null +++ b/test/repos/.hg/store/fncache @@ -0,0 +1,2 @@ +data/rev0.txt.i +data/rev1.txt.i diff --git a/test/repos/.hg/store/phaseroots b/test/repos/.hg/store/phaseroots new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/.hg/store/undo b/test/repos/.hg/store/undo new file mode 100644 index 0000000000000000000000000000000000000000..85567a543b1b9c7eb88d6c564220664c53f7551b GIT binary patch literal 67 zcmXpoNX|&iOHa+oPuI(2FyJyU$W6@4OiL{;0drFlOA_^qQp*hVN-9uA4B;XG&axD( literal 0 HcmV?d00001 diff --git a/test/repos/.hg/store/undo.phaseroots b/test/repos/.hg/store/undo.phaseroots new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/.hg/undo.bookmarks b/test/repos/.hg/undo.bookmarks new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/.hg/undo.branch b/test/repos/.hg/undo.branch new file mode 100644 index 0000000..331d858 --- /dev/null +++ b/test/repos/.hg/undo.branch @@ -0,0 +1 @@ +default \ No newline at end of file diff --git a/test/repos/.hg/undo.desc b/test/repos/.hg/undo.desc new file mode 100644 index 0000000..4dae725 --- /dev/null +++ b/test/repos/.hg/undo.desc @@ -0,0 +1,3 @@ +0 +pull +file:///mnt/data/home/oleg/work/repository/git-hub/nci/test/repos/mercurial diff --git a/test/repos/.hg/undo.dirstate b/test/repos/.hg/undo.dirstate new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/mercurial/.hg/00changelog.i b/test/repos/mercurial/.hg/00changelog.i new file mode 100644 index 0000000000000000000000000000000000000000..d3a8311050e54c57c5be7cfe169e60a95768812c GIT binary patch literal 57 zcmWN_K?=Yi3N4 PPzeKci4drSAxHuMVfz~i literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/last-message.txt b/test/repos/mercurial/.hg/last-message.txt new file mode 100644 index 0000000..198b493 --- /dev/null +++ b/test/repos/mercurial/.hg/last-message.txt @@ -0,0 +1 @@ +third revision \ No newline at end of file diff --git a/test/repos/mercurial/.hg/requires b/test/repos/mercurial/.hg/requires new file mode 100644 index 0000000..f634f66 --- /dev/null +++ b/test/repos/mercurial/.hg/requires @@ -0,0 +1,4 @@ +dotencode +fncache +revlogv1 +store diff --git a/test/repos/mercurial/.hg/store/00changelog.i b/test/repos/mercurial/.hg/store/00changelog.i new file mode 100644 index 0000000000000000000000000000000000000000..2408bbcced2f59f0d05c5dbb97b59e58add136ba GIT binary patch literal 463 zcmZQzWME`~fG8jp3uQC>hk{$`NzdilCkM`06wQBbHJ6#!rDxY+YCv?w9Nyy>IU5Xk zSP%U7xyU*tTw2<=@0x~E)CHwSU%owmut3>>S(DG!%HHz@Ys^GfUkeW}Moynk{tCqkyZR{D5b%=yWpNy>B1PLiuKW&U!;rO(FSxb1;VNo~Q#>kM9R z4=?*)dZnyhu0qBWY{eEZ84V;sE@OgXMv%+q)^fPS`#hL>NNdJQro|Jd+kB`u2djn> zK$o4k%?onbf&YF7nPz=zD{N^JTC8$I;`sFQ)>G$PURd=$=wyf}n{&&wZF@B5G9T-? uRw@7R&74zl>&_{Z_I%b^6tTpx!{F5FuzwN_{bn9M+n0a+&Hm`4bT$AYMzQJu literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/store/00manifest.i b/test/repos/mercurial/.hg/store/00manifest.i new file mode 100644 index 0000000000000000000000000000000000000000..9fb881c692c53f68e27776d6ae42f5fdeb116840 GIT binary patch literal 367 zcmZQzWME`~0AnC!1Z6Y)hXT$8ybQ-$nWT%uSxn1+K6kD;%)z$kw?(_vN`Al-ab#=ALACj%kF){5LnCYG4$|nP3dG-H;613&93~Y@Y@-k_kvN qf^64Lv-n=N@9ZZI-wUy87TGk+VNI`us)11;7lW`7&~_uzYzF|B1zG$6 literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/store/data/rev0.txt.i b/test/repos/mercurial/.hg/store/data/rev0.txt.i new file mode 100644 index 0000000000000000000000000000000000000000..2431023a17b1e7b5c96e5db8e264c251f39beab0 GIT binary patch literal 64 ncmZQzWME{#0{%n64&J91ElOJ*kA4VBT`S_X^HQPrLGn6+WD*P;%#K*02?S1MgRZ+ literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/store/data/rev2.txt.i b/test/repos/mercurial/.hg/store/data/rev2.txt.i new file mode 100644 index 0000000000000000000000000000000000000000..8963e924aa74048f6b7d43d6a9335b0c5c27e8a8 GIT binary patch literal 64 ocmZQzWME{#1Wf;-UQPrLGn6+WD*P;%#K*02@&gM*si- literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/store/fncache b/test/repos/mercurial/.hg/store/fncache new file mode 100644 index 0000000..cacbeb0 --- /dev/null +++ b/test/repos/mercurial/.hg/store/fncache @@ -0,0 +1,3 @@ +data/rev0.txt.i +data/rev1.txt.i +data/rev2.txt.i diff --git a/test/repos/mercurial/.hg/store/phaseroots b/test/repos/mercurial/.hg/store/phaseroots new file mode 100644 index 0000000..025c6a7 --- /dev/null +++ b/test/repos/mercurial/.hg/store/phaseroots @@ -0,0 +1 @@ +1 da2762e71e87935198a25b0fceab0a364ad2e6d7 diff --git a/test/repos/mercurial/.hg/store/undo b/test/repos/mercurial/.hg/store/undo new file mode 100644 index 0000000000000000000000000000000000000000..3861be4bd1fede8a05829d7b1a7da55eaa1c61d2 GIT binary patch literal 53 zcmYdEEJ@TaN-Z8UyS=|B-<0}Czy D#Z?d6 literal 0 HcmV?d00001 diff --git a/test/repos/mercurial/.hg/store/undo.phaseroots b/test/repos/mercurial/.hg/store/undo.phaseroots new file mode 100644 index 0000000..025c6a7 --- /dev/null +++ b/test/repos/mercurial/.hg/store/undo.phaseroots @@ -0,0 +1 @@ +1 da2762e71e87935198a25b0fceab0a364ad2e6d7 diff --git a/test/repos/mercurial/.hg/undo.bookmarks b/test/repos/mercurial/.hg/undo.bookmarks new file mode 100644 index 0000000..e69de29 diff --git a/test/repos/mercurial/.hg/undo.branch b/test/repos/mercurial/.hg/undo.branch new file mode 100644 index 0000000..331d858 --- /dev/null +++ b/test/repos/mercurial/.hg/undo.branch @@ -0,0 +1 @@ +default \ No newline at end of file diff --git a/test/repos/mercurial/.hg/undo.desc b/test/repos/mercurial/.hg/undo.desc new file mode 100644 index 0000000..2aa13db --- /dev/null +++ b/test/repos/mercurial/.hg/undo.desc @@ -0,0 +1,2 @@ +2 +commit diff --git a/test/repos/mercurial/.hg/undo.dirstate b/test/repos/mercurial/.hg/undo.dirstate new file mode 100644 index 0000000000000000000000000000000000000000..1a6daad7637d3d246b84094ae10676b454647b1f GIT binary patch literal 115 zcmbR7cwukj