aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xgit-remote-gittorrent31
-rw-r--r--git.js27
-rwxr-xr-xgittorrentd63
3 files changed, 55 insertions, 66 deletions
diff --git a/git-remote-gittorrent b/git-remote-gittorrent
index aeffc90..ad9825b 100755
--- a/git-remote-gittorrent
+++ b/git-remote-gittorrent
@@ -12,6 +12,7 @@ var ut_gittorrent = require('ut_gittorrent')
var WebTorrent = require('webtorrent')
var zeroFill = require('zero-fill')
var config = require('./config')
+var git = require('./git')
// BitTorrent client version string (used in peer ID).
// Generated from package.json major and minor version. For example:
@@ -99,31 +100,15 @@ if (matches) {
})
} else {
url = url.replace(/^gittorrent:/i, 'git:')
- exec('git ls-remote ' + url, function (err, stdout, stderr) {
- if (err !== null) {
+ var ls = git.ls(url, function (sha, branch) {
+ refs[branch] = sha
+ })
+ ls.on('exit', function (err) {
+ if (err) {
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
+ dht.on('ready', function () {
+ talk_to_git(refs)
})
})
}
diff --git a/git.js b/git.js
new file mode 100644
index 0000000..cd9efe3
--- /dev/null
+++ b/git.js
@@ -0,0 +1,27 @@
+#!/usr/bin/env node
+
+var spawn = require('child_process').spawn
+
+// Returns a process running `git ls-remote <url>` that calls `with_ref` on
+// each parsed reference. The url may point to a local repository.
+function ls (url, with_ref) {
+ var ls = spawn('git', ['ls-remote', url])
+ ls.stdout.on('data', function (lines) {
+ lines.toString().split('\n').forEach(function (line) {
+ if (!line || line === '') {
+ return
+ }
+ line = line.split('\t')
+ var sha = line[0]
+ var branch = line[1]
+ if (sha.length !== 40) {
+ console.warn('[git ls-remote] expected a 40-byte sha: ' + sha + '\n')
+ console.warn('[git ls-remote] on line: ' + line.join('\t'))
+ }
+ with_ref(sha, branch)
+ })
+ })
+ return ls
+}
+
+module.exports = {ls: ls}
diff --git a/gittorrentd b/gittorrentd
index 0ac810d..aa043d1 100755
--- a/gittorrentd
+++ b/gittorrentd
@@ -15,6 +15,7 @@ var ut_metadata = require('ut_metadata')
var WebTorrent = require('webtorrent')
var zeroFill = require('zero-fill')
var config = require('./config')
+var git = require('./git')
// BitTorrent client version string (used in peer ID).
// Generated from package.json major and minor version. For example:
@@ -83,55 +84,31 @@ dht.on('ready', function () {
var reponame = repo.replace(/\/.git\/$/, '')
userProfile.repositories[reponame] = {}
- var upload = spawn('git-upload-pack', ['--strict', repo])
- upload.stdout.on('data', function (line) {
- var lines = line.toString().split('\n')
- lines.forEach(function (line) {
- var arr = line.toString().split(' ')
- if (arr.length < 2) {
- return
- }
- var sha = arr[0].toString()
- // First four chars are git-upload-pack's length-of-line metadata.
- sha = sha.substring(4)
- if (arr.length == 2) {
- var ref = arr[1].toString()
- var branch = ref.match(/^refs\/heads\/(.*)/)
- // FIXME: Can't pull in too many branches.
- // if (!branch) {
- // branch = ref.match(/^refs\/remotes\/(.*)/)
- // }
- if (branch) {
- userProfile.repositories[reponame][ref] = sha
- }
- if (branch && !announcedRefs[sha]) {
- branch = branch[1]
- console.log('Announcing ' + sha + ' for ' + branch + ' on repo ' + repo)
- announcedRefs[sha] = repo
- dht.announce(sha, config.dht.announce, function (err) {
- if (err !== null) {
- console.log('Announced ' + sha)
- }
- })
+ var ls = git.ls(repo, function (sha, ref) {
+ // FIXME: Can't pull in too many branches, so only do heads for now.
+ if (ref !== 'HEAD' && !ref.match(/^refs\/heads\//)) {
+ return
+ }
+ userProfile.repositories[reponame][ref] = sha
+ if (!announcedRefs[sha]) {
+ console.log('Announcing ' + sha + ' for ' + ref + ' on repo ' + repo)
+ announcedRefs[sha] = repo
+ dht.announce(sha, config.dht.announce, function (err) {
+ if (err !== null) {
+ console.log('Announced ' + sha)
}
- } else if (arr.length > 2 && arr[1].search(/^HEAD/) !== -1) {
- // Probably the first line; line[0] has the hash, line[1] has HEAD,
- // and beyond are the supported features.
- userProfile.repositories[reponame]['HEAD'] = sha
- }
- })
- // Callback counting for repos
+ })
+ }
+ })
+ ls.stdout.on('end', function () {
count--
if (count <= 0) {
publish_mutable_key()
}
})
- upload.stdout.on('end', function () {
- console.log('end')
- })
- upload.on('exit', function (code) {
- if (code !== 0) {
- die('Failed: ' + code)
+ ls.on('exit', function (err) {
+ if (err) {
+ die(err)
}
})
})