aboutsummaryrefslogtreecommitdiff
path: root/git-remote-gittorrent
blob: cd55172d3be349583fd50f57a0af37cf9b439e13 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node

var Chalk = require('chalk')
var DHT = require('bittorrent-dht')
var exec = require('child_process').exec
var hat = require('hat')
var magnet = require('magnet-uri')
var prettyjson = require('prettyjson')
var spawn = require('child_process').spawn
var Swarm = require('bittorrent-swarm')
var ut_gittorrent = require('ut_gittorrent')
var WebTorrent = require('webtorrent')
var zeroFill = require('zero-fill')

// BitTorrent client version string (used in peer ID).
// Generated from package.json major and minor version. For example:
//   '0.16.1' -> '0016'
//   '1.2.5' -> '0102'
//
var VERSION = require('./package.json').version
  .match(/([0-9]+)/g).slice(0, 2).map(zeroFill(2)).join('')

function die (error) {
  console.error(error)
  process.exit(1)
}

// Gotta enable color manually because stdout isn't a tty.
var chalk = new Chalk.constructor({enabled: true});

var bootstrap = ['dht.gittorrent.org:6881', 'core.gittorrent.org:6881']
var dht = new DHT({
  bootstrap: bootstrap
})

// After building a dictionary of references (sha's to branch names), responds
// to git's "list" and "fetch" commands.
function talk_to_git (refs) {
  process.stdin.setEncoding('utf8')
  var didFetch = false
  process.stdin.on('readable', function () {
    var chunk = process.stdin.read()
    if (chunk === 'capabilities\n') {
      process.stdout.write('fetch\n\n')
    } else if (chunk === 'list\n') {
      Object.keys(refs).forEach(function (branch, i) {
        process.stdout.write(refs[branch] + ' ' + branch + '\n')
      })
      process.stdout.write('\n')
    } else if (chunk && chunk.search(/^fetch/) !== -1) {
      didFetch = true
      chunk.split(/\n/).forEach(function (line) {
        if (line === '') {
          return
        }
        // Format: "fetch sha branch"
        line = line.split(/\s/)
        get_infohash(line[1], line[2])
      })
    } else if (chunk && chunk !== '' && chunk !== '\n') {
      console.warn('unhandled command: "' + chunk + '"')
    }
    if (chunk === '\n') {
      process.stdout.write('\n')
      if (!didFetch) {
        // If git already has all the refs it needs, we should exit now.
        process.exit()
      }
      return
    }
  })
  process.stdout.on('error', function () {
    // stdout was closed
  })
}

var remotename = process.argv[2]
var url = process.argv[3]
var matches = url.match(/gittorrent:\/\/([a-f0-9]{40})\/(.*)/)
var refs = {}  // Maps branch names to sha's.
if (matches) {
  var key = matches[1]
  var reponame = matches[2]
  if (remotename.search(/^gittorrent:\/\//) !== -1) {
    remotename = key
  }
  dht.on('ready', function () {
    var val = new Buffer(key, 'hex')
    dht.get(val, function (err, res) {
      if (err) {
        return console.error(err)
      }
      var json = res.v.toString()
      var repos = JSON.parse(json)
      console.warn('\nMutable key ' + chalk.green(key) + ' returned:\n' +
                   prettyjson.render(repos, {keysColor: 'yellow', valuesColor: 'green'}))
      talk_to_git(repos.repositories[reponame])
    })
  })
} else {
  url = url.replace(/^gittorrent:/i, 'git:')
  exec('git ls-remote ' + url, function (err, stdout, stderr) {
    if (err !== null) {
      die(err)
    }
    var lines = stdout.split('\n')
    if (lines.length < 2) {
      die("Didn't get back a single HEAD ref: " + lines)
    }
    lines.forEach(function (line) {
      if (line === '') {
        // Last line: publish
        dht.on('ready', function () {
          talk_to_git(refs)
        })
        return
      }

      line = line.split('\t')
      var sha = line[0]
      var branch = line[1]
      if (sha.length !== 40) {
        console.warn('Was expecting a 40-byte sha: ' + sha + '\n')
        console.warn('on line: ' + line.join('\t'))
      }
      refs[branch] = sha
    })
  })

}

var swarms = {}  // A dictionary mapping sha's to swarms.
var got = {}     // Sha's we got.
var todo = 0     // The number of sha's we have yet to fetch. We will not exit
                 // until this equals zero.
dht.on('peer', function (addr, hash, from) {
  if (!(hash in got)) {
    todo++  // We only want to wait on a file if a peer has it.
    got[hash] = false
  }
  swarms[hash].addPeer(addr)
})

function update_ref (sha, branch) {
  var targetdir = process.env['GIT_DIR'] || '.'
  branch = remotename + '/' + branch
  spawn('git', ['update-ref', branch, sha])
  console.warn('git update-ref ' + chalk.yellow(branch) + ' ' +
               chalk.green(sha))
  todo--
  if (todo <= 0) {
    // These writes are actually necessary for git to finish
    // checkout.
    process.stdout.write('\n\n')
    process.exit()
  }
}

function get_infohash (sha, branch) {
  branch = branch.replace(/^refs\/(heads\/)?/, '')
  branch = branch.replace(/\/head$/, '')

  // Prevent starting a redundant lookups
  if (sha in got) {
    return
  }

  // We use console.warn (stderr) because git ignores our writes to stdout.
  console.warn('\nOkay, we want to get ' + chalk.yellow(branch) + ': ' +
               chalk.green(sha) + '\n')

  var magnetUri = 'magnet:?xt=urn:btih:' + sha
  var parsed = magnet(magnetUri)
  dht.lookup(parsed.infoHash)

  var peerId = new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8')
  swarms[parsed.infoHash] = new Swarm(parsed.infoHash, peerId)
  swarms[parsed.infoHash].on('wire', function (wire, addr) {
    console.warn('Adding swarm peer: ' + chalk.green(addr) + ' for ' +
                 chalk.red(parsed.infoHash) + '\n')
    wire.use(ut_gittorrent())
    wire.ut_gittorrent.on('handshake', function () {
      wire.ut_gittorrent.ask(parsed.infoHash)
    })
    wire.ut_gittorrent.on('receivedTorrent', function (infoHash) {
      var client = new WebTorrent({
        dht: {
          bootstrap: bootstrap
        },
        tracker: false
      })
      client.download(infoHash, function (torrent) {
        console.warn('Downloading git pack with infohash: ' + chalk.green(infoHash) + '\n')
        torrent.on('done', function (done) {
          got[sha] = true

          var stream = torrent.files[0].createReadStream()
          var unpack = spawn('git', ['index-pack', '--stdin', '-v', '--fix-thin'])
          stream.pipe(unpack.stdin)
          unpack.stderr.pipe(process.stderr)
          unpack.on('exit', function (code) {
            update_ref(sha, branch)
          })
        })
      })
    })
  })
}