Skip to content

Commit

Permalink
can stream blob data
Browse files Browse the repository at this point in the history
  • Loading branch information
iamwilhelm committed Mar 6, 2014
1 parent 6531a0a commit 06e335a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/blob.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ module.exports = class Blob
#
# callback - Receives `(err, data)`.
#
# Warning, this only returns files less than 200k, the standard buffer size for
# node's exec(). If you need to get bigger files, you should use dataStream() to
# get a stream for the file's data
#
data: (callback) ->
@repo.git "cat-file", {p: true}, @id
, (err, stdout, stderr) ->
return callback err, stdout

# Public: Get the blob contents as a stream
#
# returns - [dataStream, errstream]
#
# Usage:
# [blobstream, _] = blob.dataStream()
# blobstream.pipe(res)
#
dataStream: () ->
streams = @repo.git.streamCmd "cat-file", {p: true}, [@id]
return streams

toString: ->
"#<Blob '#{@id}'>"
15 changes: 14 additions & 1 deletion src/git.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fs = require 'fs'
{exec} = require 'child_process'
{exec, spawn} = require 'child_process'

module.exports = Git = (git_dir, dot_git) ->
dot_git ||= "#{git_dir}/.git"
Expand All @@ -21,6 +21,19 @@ module.exports = Git = (git_dir, dot_git) ->
git.cmd = (command, options, args, callback) ->
git command, options, args, callback

# Public: stream results of git command
#
# This is used for large files that you'd need to stream.
#
# returns [outstream, errstream]
#
git.streamCmd = (command, options, args) ->
options ?= {}
options = options_to_argv options
args ?= []
allargs = [command].concat(options).concat(args)
process = spawn Git.bin, allargs, {cwd: git_dir, encoding: 'binary'}
return [process.stdout, process.stderr]

# Public: Get a list of the remote names.
#
Expand Down
33 changes: 33 additions & 0 deletions test/blob.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,36 @@ describe "Blob", ->
data.should.be.type "string"
data.should.include "!!!"

describe "#dataStream", ->
describe "of a file off the root", ->
repo = git "#{__dirname}/fixtures/branched"
data = ""
before (done) ->
repo.tree().blobs (err, blobs) ->
[dataStream, _] = blobs[0].dataStream()
dataStream.on 'data', (buf) ->
data += buf.toString()
.on 'end', ->
done()

it "is a string", ->
data.should.be.type "string"
data.should.include "Bla"

describe "of a file in a subdir", ->
repo = git "#{__dirname}/fixtures/branched"
data = ""
before (done) ->
repo.tree().trees (err, trees) ->
trees[0].blobs (err, blobs) ->
[dataStream, _] = blobs[0].dataStream()
dataStream.on 'data', (buf) ->
data += buf.toString()
.on 'end', ->
done()

it "is a string", ->
data.should.be.type "string"
data.should.include "!!!"


0 comments on commit 06e335a

Please sign in to comment.