aboutsummaryrefslogtreecommitdiff
path: root/git-remote-gitswarm
diff options
context:
space:
mode:
authorChris Ball <chris@printf.net>2015-05-04 14:12:20 -0400
committerChris Ball <chris@printf.net>2015-05-04 14:50:25 -0400
commit3eeaa7bfd8d0a40a175d77203d12edb29d586cd6 (patch)
treeea810c9e87906fa8fadee2977321e043e3b22a88 /git-remote-gitswarm
Initial commits
Diffstat (limited to 'git-remote-gitswarm')
-rwxr-xr-xgit-remote-gitswarm44
1 files changed, 44 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)
+ })
+ })
+})