Method: Rugged::Blob.from_io

Defined in:
ext/rugged/rugged_blob.c

.from_io(repository, io[, hint_path]) ⇒ Object

Write a loose blob to the repository from an IO provider of data.

The repository can be bare or not.

The data provider io should respond to a read(size) method. Generally any instance of a class based on Ruby’s IO class should work(ex. File). On each read call it should return a String with maximum size of size.

NOTE: If an exception is raised in the io object’s read method, no blob will be created.

Provided the hint_path parameter is given, its value will help to determine what git filters should be applied to the object before it can be placed to the object database.

File.open('/path/to/file') do |file|
  Blob.from_io(repo, file, 'hint/blob.h') #=> '42cab3c0cde61e2b5a2392e1eadbeffa20ffa171'
end
[View source]

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'ext/rugged/rugged_blob.c', line 244

static VALUE rb_git_blob_from_io(int argc, VALUE *argv, VALUE klass)
{
	VALUE rb_repo, rb_io, rb_hint_path, rb_buffer, rb_read_args[2];
	const char * hint_path = NULL;
	git_writestream *stream;
	int error = 0, exception = 0, max_length = 4096;
	git_oid oid;
	git_repository *repo;

	rb_scan_args(argc, argv, "21", &rb_repo, &rb_io, &rb_hint_path);

	rugged_check_repo(rb_repo);
	Data_Get_Struct(rb_repo, git_repository, repo);

	if (!NIL_P(rb_hint_path)) {
		FilePathValue(rb_hint_path);
		hint_path = StringValueCStr(rb_hint_path);
	}

	error = git_blob_create_fromstream(&stream, repo, hint_path);
	if (error)
		goto cleanup;

	rb_read_args[0] = rb_io;
	rb_read_args[1] = INT2FIX(max_length);

	do {
		rb_buffer = rb_protect(rb_read_check, (VALUE)rb_read_args, &exception);

		if (exception)
			goto cleanup;

		if (NIL_P(rb_buffer))
			break;

		error = stream->write(stream, RSTRING_PTR(rb_buffer), RSTRING_LEN(rb_buffer));
		if (error)
			goto cleanup;
	} while (RSTRING_LEN(rb_buffer) == max_length);

	error = git_blob_create_fromstream_commit(&oid, stream);

cleanup:

	if (exception)
		rb_jump_tag(exception);

	rugged_exception_check(error);

	return rugged_create_oid(&oid);
}