From 71bbae5cb02a55926150a4cf74d3c333b8127342 Mon Sep 17 00:00:00 2001 From: William Hilton Date: Fri, 6 Apr 2018 23:52:50 -0400 Subject: [PATCH] BREAKING CHANGE: Perform 'push' operations on a temporary copy of the repo. Add settings 'root' and 'route'. (#4) --- README.md | 13 ++++++++++++- middleware.js | 45 ++++++++++++++++++++++++++++++++------------- package.json | 1 + 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8b0530f..9a2ff4e 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,13 @@ In your `karma.config.js`, add: ``` beforeMiddleware: ['git-http-server'], + gitHttpServer: { + root: '__tests__/__fixtures__', + route: 'git-server' + }, ``` -Then in your JS code, you can reference git repos on disk via `http://localhost:9876/path/to/repo.git`. +Then in your JS code, you can reference git repos on disk via `http://localhost:9876/git-server/name-of-repo.git`. This is useful for testing `isomorphic-git` and applications built using it. @@ -25,8 +29,15 @@ See ## Dependencies +- [fixturez](https://github.com/thejameskyle/fixturez): Easily create and maintain test fixtures in the file system - [git-http-backend](https://github.com/substack/git-http-backend): serve a git repository over http ## License MIT + +## Changelog + +1.0.0 - Initial release + +2.0.0 - Copy repo on push (so repo stays untouched) diff --git a/middleware.js b/middleware.js index 32846a0..0bc680f 100644 --- a/middleware.js +++ b/middleware.js @@ -2,22 +2,37 @@ var spawn = require('child_process').spawn; var path = require('path').posix; var url = require('url'); var backend = require('git-http-backend'); +var fixturez = require('fixturez'); -function getGitDir (req) { - var u = url.parse(req.url) - if (req.method === 'GET' && u.pathname.endsWith('/info/refs')) { - return u.pathname.replace(/\/info\/refs$/, '').replace(/^\//, '') - } - if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-upload-pack-request') { - return u.pathname.replace(/\/git-upload-pack$/, '').replace(/^\//, '') - } - if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-receive-pack-request') { - return u.pathname.replace(/\/git-receive-pack$/, '').replace(/^\//, '') +function factory (config) { + if (!config.root) throw new Error('Missing required "gitHttpServer.root" config option') + if (!config.route) throw new Error('Missing required "gitHttpServer.route" config option') + if (!config.route.startsWith('/')) throw new Error('"gitHttpServer.route" must start with a "/"') + // TODO: Make this configurable in karma.conf.js + var f = fixturez(config.root, {root: process.cwd()}) + + function getGitDir (req) { + var u = url.parse(req.url) + if (u.pathname.startsWith(config.route)) { + if (req.method === 'GET' && u.pathname.endsWith('/info/refs')) { + let gitdir = u.pathname.replace(config.route, '').replace(/\/info\/refs$/, '').replace(/^\//, '') + let fixtureName = path.basename(gitdir) + return f.find(fixtureName) + } + if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-upload-pack-request') { + let gitdir = u.pathname.replace(config.route, '').replace(/\/git-upload-pack$/, '').replace(/^\//, '') + let fixtureName = path.basename(gitdir) + return f.find(fixtureName) + } + if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-receive-pack-request') { + let gitdir = u.pathname.replace(config.route, '').replace(/\/git-receive-pack$/, '').replace(/^\//, '') + let fixtureName = path.basename(gitdir) + return f.copy(fixtureName) + } + } + return null } - return null -} -module.exports = function factory() { return function middleware (req, res, next) { var gitdir = getGitDir(req); if (gitdir == null) return next(); @@ -36,3 +51,7 @@ module.exports = function factory() { })).pipe(res); } } + +factory.$inject = ['config.gitHttpServer'] + +module.exports = factory \ No newline at end of file diff --git a/package.json b/package.json index e7225e4..4b69e66 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "homepage": "https://github.com/isomorphic-git/karma-git-http-server-middleware#readme", "dependencies": { + "fixturez": "^1.1.0", "git-http-backend": "^1.0.2" }, "devDependencies": {