aboutsummaryrefslogtreecommitdiff
path: root/git-remote-gitswarm
blob: 99c97b27442885583fab17308e4930ab67e1889c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
    })
  })
})