Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
BREAKING CHANGE: Perform 'push' operations on a temporary copy of the…
Browse files Browse the repository at this point in the history
… repo. Add settings 'root' and 'route'. (#4)
  • Loading branch information
billiegoose authored Apr 7, 2018
1 parent bc38933 commit 71bbae5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -25,8 +29,15 @@ See <https://github.com/isomorphic-git/isomorphic-git/tree/master/__tests__>

## 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)
45 changes: 32 additions & 13 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -36,3 +51,7 @@ module.exports = function factory() {
})).pipe(res);
}
}

factory.$inject = ['config.gitHttpServer']

module.exports = factory
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 71bbae5

Please sign in to comment.