Skip to content

Commit 71bbae5

Browse files
authored
BREAKING CHANGE: Perform 'push' operations on a temporary copy of the repo. Add settings 'root' and 'route'. (#4)
1 parent bc38933 commit 71bbae5

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ In your `karma.config.js`, add:
1313

1414
```
1515
beforeMiddleware: ['git-http-server'],
16+
gitHttpServer: {
17+
root: '__tests__/__fixtures__',
18+
route: 'git-server'
19+
},
1620
```
1721

18-
Then in your JS code, you can reference git repos on disk via `http://localhost:9876/path/to/repo.git`.
22+
Then in your JS code, you can reference git repos on disk via `http://localhost:9876/git-server/name-of-repo.git`.
1923

2024
This is useful for testing `isomorphic-git` and applications built using it.
2125

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

2630
## Dependencies
2731

32+
- [fixturez](https://github.com/thejameskyle/fixturez): Easily create and maintain test fixtures in the file system
2833
- [git-http-backend](https://github.com/substack/git-http-backend): serve a git repository over http
2934

3035
## License
3136

3237
MIT
38+
39+
## Changelog
40+
41+
1.0.0 - Initial release
42+
43+
2.0.0 - Copy repo on push (so repo stays untouched)

middleware.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,37 @@ var spawn = require('child_process').spawn;
22
var path = require('path').posix;
33
var url = require('url');
44
var backend = require('git-http-backend');
5+
var fixturez = require('fixturez');
56

6-
function getGitDir (req) {
7-
var u = url.parse(req.url)
8-
if (req.method === 'GET' && u.pathname.endsWith('/info/refs')) {
9-
return u.pathname.replace(/\/info\/refs$/, '').replace(/^\//, '')
10-
}
11-
if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-upload-pack-request') {
12-
return u.pathname.replace(/\/git-upload-pack$/, '').replace(/^\//, '')
13-
}
14-
if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-receive-pack-request') {
15-
return u.pathname.replace(/\/git-receive-pack$/, '').replace(/^\//, '')
7+
function factory (config) {
8+
if (!config.root) throw new Error('Missing required "gitHttpServer.root" config option')
9+
if (!config.route) throw new Error('Missing required "gitHttpServer.route" config option')
10+
if (!config.route.startsWith('/')) throw new Error('"gitHttpServer.route" must start with a "/"')
11+
// TODO: Make this configurable in karma.conf.js
12+
var f = fixturez(config.root, {root: process.cwd()})
13+
14+
function getGitDir (req) {
15+
var u = url.parse(req.url)
16+
if (u.pathname.startsWith(config.route)) {
17+
if (req.method === 'GET' && u.pathname.endsWith('/info/refs')) {
18+
let gitdir = u.pathname.replace(config.route, '').replace(/\/info\/refs$/, '').replace(/^\//, '')
19+
let fixtureName = path.basename(gitdir)
20+
return f.find(fixtureName)
21+
}
22+
if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-upload-pack-request') {
23+
let gitdir = u.pathname.replace(config.route, '').replace(/\/git-upload-pack$/, '').replace(/^\//, '')
24+
let fixtureName = path.basename(gitdir)
25+
return f.find(fixtureName)
26+
}
27+
if (req.method === 'POST' && req.headers['content-type'] === 'application/x-git-receive-pack-request') {
28+
let gitdir = u.pathname.replace(config.route, '').replace(/\/git-receive-pack$/, '').replace(/^\//, '')
29+
let fixtureName = path.basename(gitdir)
30+
return f.copy(fixtureName)
31+
}
32+
}
33+
return null
1634
}
17-
return null
18-
}
1935

20-
module.exports = function factory() {
2136
return function middleware (req, res, next) {
2237
var gitdir = getGitDir(req);
2338
if (gitdir == null) return next();
@@ -36,3 +51,7 @@ module.exports = function factory() {
3651
})).pipe(res);
3752
}
3853
}
54+
55+
factory.$inject = ['config.gitHttpServer']
56+
57+
module.exports = factory

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://github.com/isomorphic-git/karma-git-http-server-middleware#readme",
2727
"dependencies": {
28+
"fixturez": "^1.1.0",
2829
"git-http-backend": "^1.0.2"
2930
},
3031
"devDependencies": {

0 commit comments

Comments
 (0)