/* * call-seq: * blob.text(max_lines = -1, encoding = Encoding.default_external) -> String * * Return up to +max_lines+ of text from a blob as a +String+. * * In Ruby 1.9.x, the string is created with the given +encoding+, * defaulting to Encoding.default_external. * * In previous versions, the +encoding+ argument is dummy and has no * effect on the returned string. * * 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. */ static VALUE rb_git_blob_text_GET(int argc, VALUE *argv, VALUE self) { #ifdef HAVE_RUBY_ENCODING_H rb_encoding *encoding = rb_default_external_encoding(); #endif git_blob *blob; size_t size; const char *content; VALUE rb_max_lines, rb_encoding; Data_Get_Struct(self, git_blob, 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; } #ifdef HAVE_RUBY_ENCODING_H if (!NIL_P(rb_encoding)) { encoding = rb_to_encoding(rb_encoding); } #endif if (size == 0) return rugged_str_new("", 0, encoding); return rugged_str_new(content, size, encoding); }
static VALUE rb_git_tree_entry_sha_GET(VALUE self) { rugged_tree_entry *tree_entry; char out[40]; Data_Get_Struct(self, rugged_tree_entry, tree_entry); git_oid_fmt(out, git_tree_entry_id(tree_entry->entry)); return rugged_str_new(out, 40, NULL); }
static VALUE rb_git_raw_to_hex(VALUE self, VALUE raw) { git_oid oid; char out[40]; Check_Type(raw, T_STRING); git_oid_mkraw(&oid, RSTRING_PTR(raw)); git_oid_fmt(out, &oid); return rugged_str_new(out, 40, NULL); }