Skip to content

Commit 03e6a9d

Browse files
Messy: fetch/clone first
1 parent a26e5f8 commit 03e6a9d

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

git-cache-http-server.js

+29-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
var __http = require("http");
21
var __child_process = require("child_process");
2+
var __fs = require("fs");
3+
var __http = require("http");
4+
var __mkdir_p = require("mkdir-p");
35

46
var listen = 8080;
57
var cacheLocation = "/tmp/var/cache/git/";
@@ -26,29 +28,37 @@ __http.createServer(function (req, res) {
2628

2729
var remote = parts[1];
2830
// TODO don't allow arbitrary paths to be constructed with ".."
29-
var local = cacheLocation + remote + ".git";
31+
var local = cacheLocation + remote + ".git/";
3032
console.log("remote: " + remote);
3133
console.log("local: " + local);
3234

3335
if (parts[3] != null) {
34-
// TODO check our cache
35-
// TODO fetch or clone
36-
37-
// respond
38-
res.statusCode = 200;
39-
res.setHeader("Content-Type", "application/x-git-upload-pack-advertisement");
40-
res.setHeader("Cache-Control", "no-cache");
41-
res.write("001e# service=git-upload-pack\n0000");
42-
var up = __child_process.spawn("git-upload-pack", ["--stateless-rpc", "--advertise-refs", local]);
43-
up.stdout.pipe(res);
44-
up.stderr.on("data", function (data) {
45-
console.log("git-upload-pack: stderr: " + data);
46-
});
47-
up.on("exit", function (code) {
48-
console.log("git-upload-pack: exit code: " + code);
49-
if (code != 0) {
50-
res.end();
36+
__fs.stat(local + "objects", function (err, stats) {
37+
// fetch or clone
38+
if (err) {
39+
console.log("mirror: clonning");
40+
__mkdir_p.sync(local);
41+
__child_process.execSync("git clone --quiet --mirror https://" + remote + " " + local);
42+
} else {
43+
console.log("mirror: fetching");
44+
__child_process.execSync("git -C " + local + " fetch --quiet --force");
5145
}
46+
// respond
47+
res.statusCode = 200;
48+
res.setHeader("Content-Type", "application/x-git-upload-pack-advertisement");
49+
res.setHeader("Cache-Control", "no-cache");
50+
res.write("001e# service=git-upload-pack\n0000");
51+
var up = __child_process.spawn("git-upload-pack", ["--stateless-rpc", "--advertise-refs", local]);
52+
up.stdout.pipe(res);
53+
up.stderr.on("data", function (data) {
54+
console.log("git-upload-pack: stderr: " + data);
55+
});
56+
up.on("exit", function (code) {
57+
console.log("git-upload-pack: exit code: " + code);
58+
if (code != 0) {
59+
res.end();
60+
}
61+
});
5262
});
5363
} else {
5464
res.statusCode = 200;

0 commit comments

Comments
 (0)