1
- var __http = require ( "http" ) ;
2
1
var __child_process = require ( "child_process" ) ;
2
+ var __fs = require ( "fs" ) ;
3
+ var __http = require ( "http" ) ;
4
+ var __mkdir_p = require ( "mkdir-p" ) ;
3
5
4
6
var listen = 8080 ;
5
7
var cacheLocation = "/tmp/var/cache/git/" ;
@@ -26,29 +28,37 @@ __http.createServer(function (req, res) {
26
28
27
29
var remote = parts [ 1 ] ;
28
30
// TODO don't allow arbitrary paths to be constructed with ".."
29
- var local = cacheLocation + remote + ".git" ;
31
+ var local = cacheLocation + remote + ".git/ " ;
30
32
console . log ( "remote: " + remote ) ;
31
33
console . log ( "local: " + local ) ;
32
34
33
35
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" ) ;
51
45
}
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
+ } ) ;
52
62
} ) ;
53
63
} else {
54
64
res . statusCode = 200 ;
0 commit comments