From ca0f11bfa1b8d89e73e40e59c4c1e114c173fc5e Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Wed, 3 Jun 2015 11:39:03 -0400 Subject: git-remote: Don't edit .git/refs manually. Use `$ git update-ref {branch} {sha}` instead. --- git-remote-gittorrent | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/git-remote-gittorrent b/git-remote-gittorrent index 82c22b4..cd55172 100755 --- a/git-remote-gittorrent +++ b/git-remote-gittorrent @@ -3,7 +3,6 @@ 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') @@ -142,34 +141,19 @@ dht.on('peer', function (addr, hash, from) { swarms[hash].addPeer(addr) }) -function write_sha (sha, branch) { - // TODO: use `git update-ref` +function update_ref (sha, branch) { var targetdir = process.env['GIT_DIR'] || '.' - - // Make the parent directories; build the path. - var path = targetdir + '/refs' - var target = ['remotes', remotename].concat(branch.split('/')) - target.forEach(function (segment, i) { - path += '/' + segment - // Call mkdir on every entry segment the last - if (i < target.length - 1 && !fs.existsSync(path)) { - fs.mkdirSync(path) - } - }) - - console.warn('writing to: ' + path) - var stream = fs.createWriteStream(path) - stream.once('open', function (fd) { - stream.write(sha + '\n') - stream.end() - todo-- - if (todo <= 0) { - // These writes are actually necessary for git to finish - // checkout. - process.stdout.write('\n\n') - process.exit() - } - }) + 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) { @@ -215,7 +199,7 @@ function get_infohash (sha, branch) { stream.pipe(unpack.stdin) unpack.stderr.pipe(process.stderr) unpack.on('exit', function (code) { - write_sha(sha, branch) + update_ref(sha, branch) }) }) }) -- cgit v1.2.3