diff options
| -rwxr-xr-x | git-remote-gitswarm | 44 | ||||
| -rwxr-xr-x | gitswarmd | 55 |
2 files changed, 99 insertions, 0 deletions
diff --git a/git-remote-gitswarm b/git-remote-gitswarm new file mode 100755 index 0000000..99c97b2 --- /dev/null +++ b/git-remote-gitswarm @@ -0,0 +1,44 @@ +#!/usr/bin/env node + +var exec = require('child_process').exec +var WebTorrent = require('webtorrent') + +// We use console.warn (stderr) because git ignores our writes to stdout. +url = process.argv[3].replace(/^gitswarm:/i, 'git:') + +function die (error) { + console.error(error) + process.exit(1) +} + +exec('git ls-remote ' + url + ' HEAD', function (err, stdout, stderr) { + if (err !== null) + die(err) + + lines = stdout.split('\n') + if (lines.length !== 2) + die("Didn't get back a single HEAD ref: " + lines) + + var line = lines[0].split('\t') + var ref = line[0] + var head = line[1] + if (head !== 'HEAD') + die("Couldn't parse the ref line: " + ref, head) + if (ref.length != 40) + die("Was expecting a 40-byte sha: " + ref) + + console.warn("Okay, we want to get: " + ref) + + var client = new WebTorrent() + var magnetUri = 'magnet:?xt=urn:btih:' + ref + + client.add(magnetUri, function (torrent) { + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + torrent.files.forEach(function (file) { + // Get a url for each file + console.log(file) + }) + }) +}) diff --git a/gitswarmd b/gitswarmd new file mode 100755 index 0000000..960b4d8 --- /dev/null +++ b/gitswarmd @@ -0,0 +1,55 @@ +#!/usr/bin/env node + +var spawn = require('child_process').spawn +var glob = require('glob') +var WebTorrent = require('webtorrent') +var DHT = require('bittorrent-dht') + +function die (error) { + console.error(error) + process.exit(1) +} + +var dht = new DHT({ + bootstrap: ['three.printf.net:6881'] +}) +dht.listen(20000) + +var announcedRefs = {} + +// Spider all */.git dirs and announce all refs. +var repos = glob.sync('*/.git/git-daemon-export-ok') +repos.forEach(function (repo) { + console.log('in repo ' + repo) + repo = repo.replace(/git-daemon-export-ok$/, '') + console.log(repo) + var upload = spawn('git-upload-pack', ['--strict', repo]) + upload.stdout.on('data', function (line) { + var lines = line.toString().split('\n') + lines.forEach(function (line) { + arr = line.toString().split(' ') + if (arr.length == 2) { + var sha = arr[0].toString() + var ref = arr[1].toString() + if (ref.search(/^refs\/heads\//) !== -1 || ref.search(/^refs\/remotes\//) !== -1) { + console.log('Announcing ' + sha + ' for ref ' + ref + ' on repo ' + repo) + announcedRefs[sha] = repo + console.log(announcedRefs) + dht.announce(sha, 20000, function (err) { + if (err !== null) + console.log('Announced ' + sha) + }) + } + } + }) + }) + upload.stdout.on('end', function () { + console.log('end') + }) + upload.on('exit', function (code) { + if (code != 0) + die('Failed: ' + code) + }) +}) + + |
