Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dirmap test finally passes; thread the functionality through repos(di…

…rmap) instead
  • Loading branch information...
commit 485a983d7d5c1a44c2fc994a58b20dda7cdb6566 1 parent 30c3c8f
@substack authored
View
22 index.js
@@ -10,11 +10,15 @@ var onexit = require('./lib/onexit');
module.exports = function (repoDir, opts) {
if (!opts) opts = {};
- return new Git(repoDir, opts);
+ var dirMap = typeof repoDir === 'function'
+ ? repoDir
+ : function (dir) { return path.join(repoDir, dir) }
@jhs
jhs added a note

That is how I do my conditionals.

The ternary operator fascinates me. It is a ridiculous syntax, may as well enjoy it. Nowadays I am into

var dirMap =
  typeof repoDir === 'function'
    ? repoDir
    : function (dir) { return path.join(repoDir, dir) }

See also https://gist.github.com/jhs/1421499 which I do not use but it looks awesome. YMMV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ;
+ return new Git(dirMap, opts);
};
-function Git (repoDir, opts) {
- this.repoDir = repoDir;
+function Git (dirMap, opts) {
+ this.dirMap = dirMap;
this.autoCreate = opts.autoCreate === false ? false : true;
this.checkout = opts.checkout;
}
@@ -22,22 +26,21 @@ function Git (repoDir, opts) {
Git.prototype = new EventEmitter;
Git.prototype.list = function (cb) {
- fs.readdir(this.repoDir, cb);
+ fs.readdir(this.dirMap(), cb);
};
Git.prototype.exists = function (repo, cb) {
- (fs.exists || path.exists)(path.join(this.repoDir, repo), cb);
+ (fs.exists || path.exists)(this.dirMap(repo), cb);
};
Git.prototype.mkdir = function (dir, cb) {
- mkdirp(path.resolve(this.repoDir, dir), cb);
+ mkdirp(this.dirMap(dir), cb);
};
Git.prototype.create = function (repo, cb) {
var self = this;
if (typeof cb !== 'function') cb = function () {};
var cwd = process.cwd();
- if (/\.\.|^\//.test(repo)) return cb('invalid repo name');
self.exists(repo, function (ex) {
if (!ex) self.mkdir(repo, next)
@@ -47,10 +50,11 @@ Git.prototype.create = function (repo, cb) {
function next (err) {
if (err) return cb(err);
- var dir = path.join(self.repoDir, repo);
+ var dir = self.dirMap(repo);
if (self.checkout) {
var ps = spawn('git', [ 'init', dir ]);
- } else {
+ }
+ else {
var ps = spawn('git', [ 'init', '--bare', dir ]);
}
View
18 lib/handle.js
@@ -1,8 +1,6 @@
var url = require('url');
var qs = require('querystring');
var path = require('path');
-var exec = require('child_process').exec;
-var spawn = require('child_process').spawn;
var services = [ 'upload-pack', 'receive-pack' ]
@@ -64,8 +62,8 @@ handlers.push(function (req, res) {
var self = this;
var repo = m[1];
- var next = function () {
- var file = path.join(repopath, 'HEAD');
+ var next = function (x) {
+ var file = self.dirMap(path.join(m[1], 'HEAD'));
(fs.exists || path.exists)(file, function (ex) {
if (ex) fs.createReadStream(file).pipe(res)
else {
@@ -80,10 +78,10 @@ handlers.push(function (req, res) {
var dup = httpDuplex(req, res);
dup.exists = ex;
dup.repo = repo;
- dup.cwd = path.join(self.repoDir, repo)
+ dup.cwd = self.dirMap(repo);
- dup.accept = function () { dup.emit('accept') };
- dup.reject = function (code) { dup.emit('reject', code) };
+ dup.accept = dup.emit.bind(dup, 'accept');
+ dup.reject = dup.emit.bind(dup, 'reject');
dup.once('reject', function (code) {
dup.statusCode = code || 500;
@@ -91,8 +89,8 @@ handlers.push(function (req, res) {
});
if (!ex && self.autoCreate) {
- dup.once('accept', function () {
- self.create(repo, next);
+ dup.once('accept', function (dir) {
+ self.create(dir || repo, next);
});
self.emit('head', dup);
if (!anyListeners) dup.accept();
@@ -131,7 +129,7 @@ handlers.push(function (req, res) {
var action = createAction({
repo : repo,
service : service,
- cwd : path.join(self.repoDir, repo)
+ cwd : self.dirMap(repo)
}, req, res);
action.on('header', function () {
View
15 lib/info.js
@@ -1,5 +1,4 @@
var httpDuplex = require('http-duplex');
-var path = require('path');
var spawn = require('child_process').spawn;
var noCache = require('./no_cache');
@@ -8,24 +7,25 @@ var onexit = require('./onexit');
module.exports = function (opts, req, res) {
var self = opts.repos;
var dup = httpDuplex(req, res);
- dup.cwd = path.join(self.repoDir, opts.repo)
+ dup.cwd = self.dirMap(opts.repo);
dup.repo = opts.repo;
- dup.accept = function () { dup.emit('accept') };
- dup.reject = function (code) { dup.emit('reject', code) };
+ dup.accept = dup.emit.bind(dup, 'accept');
+ dup.reject = dup.emit.bind(dup, 'reject');
dup.once('reject', function (code) {
res.statusCode = code || 500;
res.end();
});
+ var anyListeners = self.listeners('info').length > 0;
+
self.exists(opts.repo, function (ex) {
dup.exists = ex;
- var anyListeners = self.listeners('info').length > 0;
if (!ex && self.autoCreate) {
dup.on('accept', function () {
- self.create(opts.repo, next)
+ self.create(opts.repo, next);
});
self.emit('info', dup);
@@ -49,7 +49,8 @@ module.exports = function (opts, req, res) {
'application/x-git-' + opts.service + '-advertisement'
);
noCache(res);
- serviceRespond(opts.service, path.join(self.repoDir, opts.repo), res);
+ var d = self.dirMap(opts.repo);
+ serviceRespond(opts.service, d, res);
}
}
View
4 lib/service.js
@@ -96,11 +96,11 @@ function Service (opts, req, res) {
inherits(Service, HttpDuplex);
-Service.prototype.accept = function () {
+Service.prototype.accept = function (dir) {
if (this.status !== 'pending') return;
this.status = 'accepted';
- this.emit('accept');
+ this.emit('accept', dir);
};
Service.prototype.reject = function (code, msg) {
View
53 test/dirmap.js
@@ -10,31 +10,40 @@ var http = require('http');
var seq = require('seq');
-test('clone into programatic directories', function (t) {
- t.plan(3);
-
- var repoDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
- var srcDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
- var dstDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
- var targetDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
+var repoDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
+var srcDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
+var dstDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
+var targetDir = '/tmp/' + Math.floor(Math.random() * (1<<30)).toString(16);
+
+fs.mkdirSync(repoDir, 0700);
+fs.mkdirSync(srcDir, 0700);
+fs.mkdirSync(dstDir, 0700);
+fs.mkdirSync(targetDir, 0700);
- fs.mkdirSync(repoDir, 0700);
- fs.mkdirSync(srcDir, 0700);
- fs.mkdirSync(dstDir, 0700);
- fs.mkdirSync(targetDir, 0700);
+var repos;
+var server = http.createServer(function (req, res) {
+ repos.handle(req, res);
+});
+
+test(function (t) {
+ server.listen(0, function () {
+ setTimeout(t.end.bind(t), 1000);
+ });
+});
+
+test('clone into programatic directories', function (t) {
+ t.plan(9);
- var repos = pushover(repoDir);
- var port = Math.floor(Math.random() * ((1<<16) - 1e4)) + 1e4;
- var server = http.createServer(function (req, res) {
- repos.handle(req, res);
+ repos = pushover(function (dir) {
+ t.equal(dir, 'doom');
+ return path.join(targetDir, dir);
});
- server.listen(port);
+ var port = server.address().port;
process.chdir(srcDir);
seq()
.seq(function () {
var ps = spawn('git', [ 'init' ]);
- ps.stderr.pipe(process.stderr, { end : false });
ps.on('exit', this.ok);
})
.seq(function () {
@@ -50,8 +59,10 @@ test('clone into programatic directories', function (t) {
var ps = spawn('git', [
'push', 'http://localhost:' + port + '/doom', 'master'
]);
- ps.stderr.pipe(process.stderr, { end : false });
ps.on('exit', this.ok);
+ ps.on('exit', function (code) {
+ t.equal(code, 0);
+ });
})
.seq(function () {
process.chdir(dstDir);
@@ -59,13 +70,13 @@ test('clone into programatic directories', function (t) {
.on('exit', this.ok)
})
.seq_(function (next) {
- path.exists(dstDir + '/doom/a.txt', function (ex) {
+ exists(dstDir + '/doom/a.txt', function (ex) {
t.ok(ex, 'a.txt exists');
next();
})
})
.seq_(function (next) {
- path.exists(targetDir + '/INFO', function (ex) {
+ exists(targetDir + '/HEAD', function (ex) {
t.ok(ex, 'INFO exists');
next();
})
@@ -75,7 +86,7 @@ test('clone into programatic directories', function (t) {
repos.on('push', function (push) {
t.equal(push.repo, 'doom');
- push.accept(targetDir);
+ push.accept();
});
t.on('end', function () {
Please sign in to comment.
Something went wrong with that request. Please try again.