-
Notifications
You must be signed in to change notification settings - Fork 262
/
git-remote-gittorrent
executable file
·142 lines (131 loc) · 4.35 KB
/
git-remote-gittorrent
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
#!/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 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 + ' HEAD', 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 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)
}
dht.on('ready', function () {
get_infohash(ref)
})
})
}
function get_infohash (ref) {
// We use console.warn (stderr) because git ignores our writes to stdout.
console.warn('\nOkay, we want to get: ' + chalk.green(ref) + '\n')
process.stdin.setEncoding('utf8')
process.stdin.on('readable', function () {
var chunk = process.stdin.read()
if (chunk === 'capabilities\n') {
process.stdout.write('fetch\n\n')
}
if (chunk === 'list\n') {
process.stdout.write(ref + ' refs/heads/master\n\n')
}
})
process.stdout.on('error', function () {
// stdout was closed
})
var magnetUri = 'magnet:?xt=urn:btih:' + ref
var parsed = magnet(magnetUri)
dht.lookup(parsed.infoHash)
dht.on('peer', function (addr, hash, from) {
swarm.addPeer(addr)
})
var peerId = new Buffer('-WW' + VERSION + '-' + hat(48), 'utf8')
var swarm = new Swarm(parsed.infoHash, peerId)
swarm.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) {
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']
var stream = fs.createWriteStream(targetdir + '/refs/heads/master')
stream.once('open', function (fd) {
stream.write(ref + '\n')
stream.end()
// These writes are actually necessary for git to finish checkout.
process.stdout.write('\n\n')
process.exit()
})
})
})
})
})
})
}