aboutsummaryrefslogtreecommitdiff
path: root/git-remote-gittorrent
blob: 74f846cf700f5fa72ebdded8775efdb7fd08d1eb (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
#!/usr/bin/env node

var Chalk = require('chalk')
var DHT = require('bittorrent-dht')
var exec = require('child_process').exec
var fs = require('fs')
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
})

var remotename = process.argv[2]
var url = process.argv[3]
var matches = url.match(/gittorrent:\/\/([a-f0-9]{40})\/(.*)/)
if (matches) {
  var key = matches[1]
  var reponame = matches[2]
  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'}))
      get_infohash(repos.repositories[reponame].master)
    })
  })
} 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)
    }
    var count = lines.length;
    var refs = []
    lines.forEach(function (line) {
      if (line == '') {
        // Last line: publish
        dht.on('ready', function () {
          var refcount = refs.length
          refs.forEach(function (ref) {
            get_infohash(ref.sha, ref.branch)
            refcount--
            if (refcount == 0) {
              process.stdin.setEncoding('utf8')
              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') {
                  var refcount = refs.length
                    console.warn('list:\n')
                  refs.forEach(function (ref) {
                    process.stdout.write(ref.sha + ' ' + ref.branch + '\n')
                    console.warn(chalk.red(ref.sha) + ' '
                                 + chalk.green(ref.branch))
                    refcount--
                    if (refcount == 0) {
                      process.stdout.write('\n')
                    }
                  })
                } else if (chunk && chunk != '') {
                  console.warn('unhandled command: ' + chunk)
                }
              })
              process.stdout.on('error', function () {
                // stdout was closed
              })
            }
          })
        })
        return
      }

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

}

var swarms = {}  // A dictionary mapping sha's to swarms.
var need = {}    // Sha's we need.
var got = {}     // Sha's we got.
var todo = 0     // The number of sha's in need, not in got.
dht.on('peer', function (addr, hash, from) {
  if (!need[hash] && !got[hash]) {
    todo++
    need[hash] = true
    got[hash] = false
  }
  swarms[hash].addPeer(addr)
})

function get_infohash (ref, branch) {
  if (!branch || branch == 'HEAD' || branch == 'refs/heads/master') {
    branch = 'master'
  } else {
    branch = branch.replace(/^refs\//, '')
    branch = branch.replace(/\/head$/, '')
  }

  // 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:' + ref
  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) + '\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) {
          todo--
          need[ref] = false
          got[ref] = 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) {
            var targetdir = process.env['GIT_DIR'] || './'

            // Make the parent directories; build the path.
            var target = ['remotes', remotename].concat(branch.split('/'))
            var path = targetdir + '/refs/'
            for (var i = 0; i < target.length - 1; i++) {
              path += target[i] + '/'
              if (!fs.existsSync(path)) {
                fs.mkdirSync(path)
              }
            }
            path += target[target.length - 1]  // Add the base name.

            console.warn('writing to: ' + path)
            var stream = fs.createWriteStream(path)
            stream.once('open', function (fd) {
              stream.write(ref + '\n')
              stream.end()
              if (todo == 0) {
                // These writes are actually necessary for git to finish
                // checkout.
                process.stdout.write('\n\n')
                process.exit()
              }
            })
          })
        })
      })
    })
  })
}