git ssb

0+

cel / pull-git-remote-helper



Commit 76d7bd15ac81df893e05f465fa9200b7c1be2e1e

Handle pushing first commit

Charles Lehner committed on 2/8/2016, 4:36:42 AM
Parent: 89810d797532892ef70d5ee23f3b82c5466e7858

Files changed

index.jschanged
test/run.jschanged
index.jsView
@@ -88,11 +88,14 @@
8888 }
8989
9090 function uploadPack(read) {
9191 return function (abort, cb) {
92- cb('upload pack not implemented')
92+ var lines = packLineDecode(read)
93+ receiveRefs(lines.packLines, function (err, refs) {
94+ console.error('refs', refs, err)
95+ if (err) return cb(err)
96+ })
9397 }
94- // throw new Error('upload pack')
9598 }
9699
97100 function getRefs() {
98101 return pull.values([
@@ -155,15 +158,14 @@
155158 */
156159
157160 // Get a line for each ref that we have. The first line also has capabilities.
158161 // Wrap with packLineEncode.
159-function receivePackHeader(capabilities) {
160- var readRef = getRefs()
162+function receivePackHeader(capabilities, refsSource) {
161163 var first = true
162164 var ended
163165 return function (abort, cb) {
164166 if (ended) return cb(true)
165- readRef(abort, function (end, ref) {
167+ refsSource(abort, function (end, ref) {
166168 ended = end
167169 var name = ref && ref.name
168170 var hash = ref && ref.hash
169171 if (first) {
@@ -190,34 +192,30 @@
190192 })
191193 objectSink(objects)
192194 }
193195
194-function receivePack(read, objectSink) {
196+function receiveRefs(readLine, cb) {
197+ var refs = []
198+ readLine(null, function next(end, line) {
199+ if (end)
200+ cb(end)
201+ else if (line.length == 0)
202+ cb(null, refs)
203+ else {
204+ var args = split2(line.toString('ascii'))
205+ refs.push({
206+ hash: args[0],
207+ name: args[1]
208+ })
209+ readLine(null, next)
210+ }
211+ })
212+}
213+
214+function receivePack(read, objectSink, refsSource) {
195215 var ended
196- var sendRefs = receivePackHeader([])
216+ var sendRefs = receivePackHeader([], refsSource)
197217
198- function receiveRefs(readLine, cb) {
199- var refs = []
200- readLine(null, function next(end, line) {
201- /*
202- if (end === true)
203- cb(new Error('refs line ended early'))
204- else */
205- if (end)
206- cb(end)
207- else if (line === '')
208- cb(null, refs)
209- else {
210- var args = split2(line)
211- refs.push({
212- hash: args[0],
213- name: args[1]
214- })
215- readLine(null, next)
216- }
217- })
218- }
219-
220218 return packLineEncode(
221219 cat([
222220 // send our refs
223221 sendRefs,
@@ -226,10 +224,9 @@
226224 if (abort) return
227225 // receive their refs
228226 var lines = packLineDecode(read)
229227 receiveRefs(lines.packLines, function (err, refs) {
230- if (refs)
231- console.error('refs', refs, err)
228+ // console.error('refs', refs, err)
232229 if (err) return cb(err)
233230 // receive the pack
234231 receiveActualPack(lines.passthrough, objectSink, function (err) {
235232 if (err) return cb(err)
@@ -257,16 +254,17 @@
257254 module.exports = function (opts) {
258255 var ended
259256 var prefix = opts.prefix
260257 var objectSink = opts.objectSink
258+ var refsSource = opts.refsSource || pull.empty()
261259
262260 function handleConnect(cmd, read) {
263261 var args = split2(cmd)
264262 switch (args[0]) {
265263 case 'git-upload-pack':
266264 return prepend('\n', uploadPack(read))
267265 case 'git-receive-pack':
268- return prepend('\n', receivePack(read), objectSink)
266+ return prepend('\n', receivePack(read, objectSink, refsSource))
269267 default:
270268 return pull.error(new Error('Unknown service ' + args[0]))
271269 }
272270 }
test/run.jsView
@@ -2,34 +2,47 @@
22 var tape = require('tape')
33 var path = require('path')
44 var mktemp = require('mktemp')
55 var rimraf = require('rimraf')
6+var fs = require('fs')
67
78 var env = Object.create(process.env)
89 env.PATH = __dirname + ':' + env.PATH
910 var remote = 'test.js://foo'
1011
1112 var tmpDir = mktemp.createDirSync(path.join(require('os').tmpdir(), 'XXXXXXX'))
1213
13-function git(args, cb) {
14+function git() {
15+ var args = [].slice.call(arguments)
16+ var cb = args.pop()
1417 spawn('git', args, {
1518 env: env,
1619 cwd: tmpDir,
1720 stdio: ['ignore', process.stderr, process.stderr]
1821 }).on('close', cb)
1922 }
2023
2124 tape('init repo', function (t) {
22- git(['init'], function (code) {
25+ git('init', function (code) {
2326 t.equals(code, 0, 'inited')
2427 t.end()
2528 })
2629 })
2730
28-tape('push with empty repo', function (t) {
29- git(['push', remote], function (code) {
30- t.equals(code, 0, 'pushed')
31- t.end()
31+tape('make a commit and push', function (t) {
32+ var filename = path.join(tmpDir, 'blah.txt')
33+ fs.writeFile(filename, 'i am a file', function (err) {
34+ t.error(err, 'wrote a file')
35+ git('add', filename, function (code) {
36+ t.equals(code, 0, 'added file')
37+ git('commit', '-mInitial commit', function (code) {
38+ t.equals(code, 0, 'made initial commit')
39+ git('push', '-vv', remote, 'master', function (code) {
40+ t.equals(code, 0, 'pushed')
41+ t.end()
42+ })
43+ })
44+ })
3245 })
3346 })
3447
3548 tape.onFinish(function () {

Built with git-ssb-web