Method: Rugged::Blob#text

Defined in:
ext/rugged/rugged_blob.c

#text(max_lines = -1, encoding = Encoding.default_external) ⇒ String

Return up to max_lines of text from a blob as a String. If max_lines is less than 0, the full string is returned.

The string is created with the given encoding, defaulting to Encoding.default_external.

When limiting the size of the text with max_lines, the string is expected to have an ASCII-compatible encoding, and is checked for the newline \n character.

Returns:

  • (String)

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/rugged/rugged_blob.c', line 36

static VALUE rb_git_blob_text_GET(int argc, VALUE *argv, VALUE self)
{
	git_blob *blob;
	size_t size;
	const char *content;
	VALUE rb_max_lines, rb_encoding;

	TypedData_Get_Struct(self, git_blob, &rugged_object_type, blob);
	rb_scan_args(argc, argv, "02", &rb_max_lines, &rb_encoding);

	content = git_blob_rawcontent(blob);
	size = git_blob_rawsize(blob);

	if (!NIL_P(rb_max_lines)) {
		size_t i = 0;
		int lines = 0, maxlines;

		Check_Type(rb_max_lines, T_FIXNUM);
		maxlines = FIX2INT(rb_max_lines);

		if (maxlines >= 0) {
			while (i < size && lines < maxlines) {
				if (content[i++] == '\n')
					lines++;
			}
			size = (size_t)i;
		}

	}

	if (!NIL_P(rb_encoding)) {
		return rb_enc_str_new(content, size, rb_to_encoding(rb_encoding));
	}

	return rb_external_str_new(content, size);
}