diff options
| author | Chris Ball <chris@printf.net> | 2015-05-19 16:13:09 -0400 |
|---|---|---|
| committer | Chris Ball <chris@printf.net> | 2015-05-19 19:49:30 -0400 |
| commit | 280e54e4e933c53d5a93ddce974d019ff5d717e4 (patch) | |
| tree | b1ee8f7f2d7c23df38e9397db682e3b0c2ef94f5 | |
| parent | f54543f1dd3ca2fb22c3d790016f3449100e57a6 (diff) | |
First working git clone!
| -rwxr-xr-x | git-remote-gitswarm | 49 | ||||
| -rwxr-xr-x | gitswarmd | 17 |
2 files changed, 61 insertions, 5 deletions
diff --git a/git-remote-gitswarm b/git-remote-gitswarm index 98ac48a..6ec6d24 100755 --- a/git-remote-gitswarm +++ b/git-remote-gitswarm @@ -2,9 +2,12 @@ var DHT = require('bittorrent-dht') var exec = require('child_process').exec +var fs = require('fs') var magnet = require('magnet-uri') +var spawn = require('child_process').spawn var Swarm = require('bittorrent-swarm') var ut_gitswarm = require('ut_gitswarm') +var WebTorrent = require('webtorrent') // We use console.warn (stderr) because git ignores our writes to stdout. var url = process.argv[3].replace(/^gitswarm:/i, 'git:') @@ -15,6 +18,7 @@ function die (error) { } exec('git ls-remote ' + url + ' HEAD', function (err, stdout, stderr) { + var targetdir = process.env['GIT_DIR'] if (err !== null) { die(err) } @@ -33,6 +37,20 @@ exec('git ls-remote ' + url + ' HEAD', function (err, stdout, stderr) { } console.warn('Okay, we want to get: ' + ref) + process.stdin.setEncoding('utf8'); + process.stdin.on('readable', function() { + var chunk = process.stdin.read(); + if (chunk === 'capabilities\n') { + process.stdout.write('fetch\n\n') + } + if (chunk === 'list\n') { + process.stdout.write(ref + ' refs/heads/master\n\n') + } + }) + process.stdout.on('error', function () { + // stdout was closed + }) + var dht = new DHT({ bootstrap: ['three.printf.net:6882'] }) @@ -42,17 +60,42 @@ exec('git ls-remote ' + url + ' HEAD', function (err, stdout, stderr) { dht.lookup(parsed.infoHash) }) dht.on('peer', function (addr, hash, from) { - console.error(addr, hash, from) swarm.addPeer(addr) }) var swarm = new Swarm(parsed.infoHash, 'cafebabecafebabecafecafebabecafebabecafe') swarm.on('wire', function (wire) { - console.error('we got a wire') wire.use(ut_gitswarm()) wire.ut_gitswarm.on('handshake', function () { - console.error('in ut_gitswarm on handshake') wire.ut_gitswarm.ask(parsed.infoHash) }) + wire.ut_gitswarm.on('receivedTorrent', function (infoHash) { + var client = new WebTorrent({ + dht: { + bootstrap: ['three.printf.net:6882'] + }, + tracker: false + }) + client.download(infoHash, function (torrent) { + torrent.on('done', function (done) { + var filename = torrent.storage.path + '/' + torrent.files[0].path + var unpack = spawn('git', ['index-pack', '--stdin', '-v', '--fix-thin']) + var stream = fs.createReadStream(filename) + stream.on('open', function () { + stream.pipe(unpack.stdin) + }) + unpack.on('exit', function (code) { + var stream = fs.createWriteStream(targetdir + '/refs/heads/master') + stream.once('open', function (fd) { + stream.write(ref + '\n') + stream.end() + // These writes are actually necessary for git to finish checkout. + process.stdout.write('\n\n') + process.exit() + }) + }) + }) + }) + }) }) }) @@ -1,5 +1,6 @@ #!/usr/bin/env node +var createTorrent = require('create-torrent') var DHT = require('bittorrent-dht') var glob = require('glob') var fs = require('fs') @@ -7,6 +8,8 @@ var net = require('net') var Protocol = require('bittorrent-protocol') var spawn = require('child_process').spawn var ut_gitswarm = require('ut_gitswarm') +var ut_metadata = require('ut_metadata') +var WebTorrent = require('webtorrent') function die (error) { console.error(error) @@ -63,6 +66,7 @@ dht.on('ready', function () { net.createServer(function (socket) { var wire = new Protocol() wire.use(ut_gitswarm()) + wire.use(ut_metadata()) socket.pipe(wire).pipe(socket) wire.on('handshake', function (infoHash, peerId) { console.log('Received handshake for ' + infoHash) @@ -70,7 +74,8 @@ dht.on('ready', function () { }) wire.ut_gitswarm.on('generatePack', function (sha) { console.error('calling git pack-objects') - var filename = fs.createWriteStream(sha + '.pack') + var filename = sha + '.pack' + var stream = fs.createWriteStream(filename) if (!announcedRefs[sha]) { console.error('Asked for an unknown sha!') } @@ -81,9 +86,17 @@ dht.on('ready', function () { console.error('git pack-objects process exited with code ' + code) } else { console.error('Finished writing ' + filename) + var webtorrent = new WebTorrent({ + dht: {bootstrap: ['three.printf.net:6882']}, + tracker: false + }) + webtorrent.seed(filename, function onTorrent (torrent) { + console.error(torrent.infoHash) + wire.ut_gitswarm.sendTorrent(torrent.infoHash) + }) } }) - pack.stdout.pipe(filename) + pack.stdout.pipe(stream) pack.stderr.on('data', function (data) { console.error(data.toString()) }) |
