Beispiel #1
0
void test_filter_blob__ident(void)
{
	git_oid id;
	git_blob *blob;
	git_buf buf = { 0 };

	cl_git_mkfile("crlf/test.ident", "Some text\n$Id$\nGoes there\n");
	cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));
	git_blob_free(blob);

	cl_git_mkfile("crlf/test.ident", "Some text\n$Id: Any old just you want$\nGoes there\n");
	cl_git_pass(git_blob_create_fromworkdir(&id, g_repo, "test.ident"));
	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", git_blob_rawcontent(blob));

	cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.bin", 1));
	cl_assert_equal_s(
		"Some text\n$Id$\nGoes there\n", buf.ptr);

	cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identcrlf", 1));
	cl_assert_equal_s(
		"Some text\r\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\r\nGoes there\r\n", buf.ptr);

	cl_git_pass(git_blob_filtered_content(&buf, blob, "filter.identlf", 1));
	cl_assert_equal_s(
		"Some text\n$Id: 3164f585d548ac68027d22b104f2d8100b2b6845$\nGoes there\n", buf.ptr);

	git_buf_free(&buf);
	git_blob_free(blob);

}
Beispiel #2
0
void test_filter_custom__order_dependency(void)
{
	git_index *index;
	git_blob *blob;
	git_buf buf = { 0 };

	/* so if ident and reverse are used together, an interesting thing
	 * happens - a reversed "$Id$" string is no longer going to trigger
	 * ident correctly.  When checking out, the filters should be applied
	 * in order CLRF, then ident, then reverse, so ident expansion should
	 * work correctly.  On check in, the content should be reversed, then
	 * ident, then CRLF filtered.  Let's make sure that works...
	 */

	cl_git_mkfile(
		"empty_standard_repo/.gitattributes",
		"hero.*.rev-ident text ident prereverse eol=lf\n");

	cl_git_mkfile(
		"empty_standard_repo/hero.1.rev-ident",
		"This is a test\n$Id$\nHave fun!\n");

	cl_git_mkfile(
		"empty_standard_repo/hero.2.rev-ident",
		"Another test\n$dI$\nCrazy!\n");

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_add_bypath(index, "hero.1.rev-ident"));
	cl_git_pass(git_index_add_bypath(index, "hero.2.rev-ident"));
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Filter chains\n");
	git_index_free(index);

	cl_git_pass(git_blob_lookup(&blob, g_repo,
		& git_index_get_bypath(index, "hero.1.rev-ident", 0)->oid));
	cl_assert_equal_s(
		"\n!nuf evaH\n$dI$\ntset a si sihT", git_blob_rawcontent(blob));
	cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.1.rev-ident", 0));
	/* no expansion because id was reversed at checkin and now at ident
	 * time, reverse is not applied yet */
	cl_assert_equal_s(
		"This is a test\n$Id$\nHave fun!\n", buf.ptr);
	git_blob_free(blob);

	cl_git_pass(git_blob_lookup(&blob, g_repo,
		& git_index_get_bypath(index, "hero.2.rev-ident", 0)->oid));
	cl_assert_equal_s(
		"\n!yzarC\n$Id$\ntset rehtonA", git_blob_rawcontent(blob));
	cl_git_pass(git_blob_filtered_content(&buf, blob, "hero.2.rev-ident", 0));
	/* expansion because reverse was applied at checkin and at ident time,
	 * reverse is not applied yet */
	cl_assert_equal_s(
		"Another test\n$59001fe193103b1016b27027c0c827d036fd0ac8 :dI$\nCrazy!\n", buf.ptr);
	cl_assert_equal_i(0, git_oid_strcmp(
		git_blob_id(blob), "8ca0df630d728c0c72072b6101b301391ef10095"));
	git_blob_free(blob);

	git_buf_free(&buf);
}
Beispiel #3
0
void test_diff_blob__can_compare_text_blobs(void)
{
	git_blob *a, *b, *c;
	git_oid a_oid, b_oid, c_oid;

	/* tests/resources/attr/root_test1 */
	cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8));
	cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4));

	/* tests/resources/attr/root_test2 */
	cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8));
	cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4));

	/* tests/resources/attr/root_test3 */
	cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16));
	cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 16));

	/* Doing the equivalent of a `git diff -U1` on these files */

	/* diff on tests/resources/attr/root_test1 */
	memset(&expected, 0, sizeof(expected));
	cl_git_pass(git_diff_blobs(
		a, NULL, b, NULL, &opts,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
	assert_one_modified(1, 6, 1, 5, 0, &expected);

	/* same diff but use direct buffers */
	memset(&expected, 0, sizeof(expected));
	cl_git_pass(git_diff_buffers(
		git_blob_rawcontent(a), (size_t)git_blob_rawsize(a), NULL,
		git_blob_rawcontent(b), (size_t)git_blob_rawsize(b), NULL, &opts,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
	assert_one_modified(1, 6, 1, 5, 0, &expected);

	/* diff on tests/resources/attr/root_test2 */
	memset(&expected, 0, sizeof(expected));
	cl_git_pass(git_diff_blobs(
		b, NULL, c, NULL, &opts,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
	assert_one_modified(1, 15, 3, 9, 3, &expected);

	/* diff on tests/resources/attr/root_test3 */
	memset(&expected, 0, sizeof(expected));
	cl_git_pass(git_diff_blobs(
		a, NULL, c, NULL, &opts,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
	assert_one_modified(1, 13, 0, 12, 1, &expected);

	memset(&expected, 0, sizeof(expected));
	cl_git_pass(git_diff_blobs(
		c, NULL, d, NULL, &opts,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected));
	assert_one_modified(2, 14, 4, 6, 4, &expected);

	git_blob_free(a);
	git_blob_free(b);
	git_blob_free(c);
}
Beispiel #4
0
int git_diff_file_content__init_from_blob(
	git_diff_file_content *fc,
	git_repository *repo,
	const git_diff_options *opts,
	const git_blob *blob,
	git_diff_file *as_file)
{
	memset(fc, 0, sizeof(*fc));
	fc->repo = repo;
	fc->file = as_file;
	fc->blob = blob;

	if (!blob) {
		fc->flags |= GIT_DIFF_FLAG__NO_DATA;
	} else {
		fc->flags |= GIT_DIFF_FLAG__LOADED;
		fc->file->flags |= GIT_DIFF_FLAG_VALID_OID;
		fc->file->size = git_blob_rawsize(blob);
		fc->file->mode = GIT_FILEMODE_BLOB;
		git_oid_cpy(&fc->file->oid, git_blob_id(blob));

		fc->map.len  = (size_t)fc->file->size;
		fc->map.data = (char *)git_blob_rawcontent(blob);
	}

	return diff_file_content_init_common(fc, opts);
}
Beispiel #5
0
void test_notes_notes__1(void)
{
	git_oid oid, note_oid;
	static git_note *note;
	static git_blob *blob;

	cl_git_pass(git_oid_fromstr(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));

	cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));
	cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));

	cl_git_pass(git_note_read(&note, _repo, NULL, &oid));

	cl_assert_equal_s(git_note_message(note), "hello world\n");
	cl_assert(!git_oid_cmp(git_note_oid(note), &note_oid));

	cl_git_pass(git_blob_lookup(&blob, _repo, &note_oid));
	cl_assert_equal_s(git_note_message(note), git_blob_rawcontent(blob));

	cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &oid, "hello world\n"));
	cl_git_fail(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &oid, "hello world\n"));

	cl_git_pass(git_note_remove(_repo, NULL, _sig, _sig, &oid));
	cl_git_pass(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));

	cl_git_fail(git_note_remove(_repo, NULL, _sig, _sig, &note_oid));
	cl_git_fail(git_note_remove(_repo, "refs/notes/some/namespace", _sig, _sig, &oid));

	git_note_free(note);
	git_blob_free(blob);
}
/*
 *  call-seq:
 *    blob.content(max_bytes=-1) -> string
 *
 *  Return up to +max_bytes+ from the contents of a blob as bytes +String+.
 *  If +max_bytes+ is less than 0, the full string is returned.
 *
 *  This string is tagged with the ASCII-8BIT encoding: the bytes are
 *  returned as-is, since Git is encoding agnostic.
 */
static VALUE rb_git_blob_content_GET(int argc, VALUE *argv, VALUE self)
{
	git_blob *blob;
	size_t size;
	const char *content;
	VALUE rb_max_bytes;

	Data_Get_Struct(self, git_blob, blob);
	rb_scan_args(argc, argv, "01", &rb_max_bytes);

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

	if (!NIL_P(rb_max_bytes)) {
		int maxbytes;

		Check_Type(rb_max_bytes, T_FIXNUM);
		maxbytes = FIX2INT(rb_max_bytes);

		if (maxbytes >= 0 && (size_t)maxbytes < size)
			size = (size_t)maxbytes;
	}

	/*
	 * since we don't really ever know the encoding of a blob
	 * lets default to the binary encoding (ascii-8bit)
	 */
	return rb_str_new(content, size);
}
Beispiel #7
0
static int
Blob_getbuffer(Blob *self, Py_buffer *view, int flags)
{
    return PyBuffer_FillInfo(view, (PyObject *) self,
                             (void *) git_blob_rawcontent(self->blob),
                             git_blob_rawsize(self->blob), 1, flags);
}
Beispiel #8
0
int git_blob__getbuf(git_buf *buffer, git_blob *blob)
{
	return git_buf_set(
		buffer,
		git_blob_rawcontent(blob),
		git_blob_rawsize(blob));
}
Beispiel #9
0
void test_filter_blob__all_crlf(void)
{
	git_blob *blob;
	git_buf buf = { 0 };

	cl_git_pass(git_revparse_single(
		(git_object **)&blob, g_repo, "a9a2e891")); /* all-crlf */

	cl_assert_equal_s(ALL_CRLF_TEXT_RAW, git_blob_rawcontent(blob));

	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1));

	cl_assert_equal_s(ALL_CRLF_TEXT_RAW, buf.ptr);

	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1));

	/* in this case, raw content has crlf in it already */
	cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);

	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));

	/* we never convert CRLF -> LF on platforms that have LF */
	cl_assert_equal_s(ALL_CRLF_TEXT_AS_CRLF, buf.ptr);

	git_buf_free(&buf);
	git_blob_free(blob);
}
Beispiel #10
0
void test_filter_blob__sanitizes(void)
{
	git_blob *blob;
	git_buf buf;

	cl_git_pass(git_revparse_single(
		(git_object **)&blob, g_repo, "e69de29")); /* zero-byte */

	cl_assert_equal_i(0, git_blob_rawsize(blob));
	cl_assert_equal_s("", git_blob_rawcontent(blob));

	memset(&buf, 0, sizeof(git_buf));
	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.bin", 1));
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
	git_buf_free(&buf);

	memset(&buf, 0, sizeof(git_buf));
	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.crlf", 1));
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
	git_buf_free(&buf);

	memset(&buf, 0, sizeof(git_buf));
	cl_git_pass(git_blob_filtered_content(&buf, blob, "file.lf", 1));
	cl_assert_equal_sz(0, buf.size);
	cl_assert_equal_s("", buf.ptr);
	git_buf_free(&buf);

	git_blob_free(blob);
}
/*
 *  call-seq:
 *    blob.sloc -> int
 *
 *  Return the number of non-empty code lines for the blob,
 *  assuming the blob is plaintext (i.e. not binary)
 */
static VALUE rb_git_blob_sloc(VALUE self)
{
	git_blob *blob;
	const char *data, *data_end;
	size_t sloc = 0;

	Data_Get_Struct(self, git_blob, blob);

	data = git_blob_rawcontent(blob);
	data_end = data + git_blob_rawsize(blob);

	if (data == data_end)
		return INT2FIX(0);

	/* go through the whole blob, counting lines
	 * that are not empty */
	while (data < data_end) {
		if (*data++ == '\n') {
			while (data < data_end && isspace(*data))
				data++;

			sloc++;
		}
	}

	/* last line without trailing '\n'? */
	if (data[-1] != '\n')
		sloc++;

	return INT2FIX(sloc);
}
Beispiel #12
0
static int tree_reader_read(
	git_buf *out,
	git_oid *out_id,
	git_filemode_t *out_filemode,
	git_reader *_reader,
	const char *filename)
{
	tree_reader *reader = (tree_reader *)_reader;
	git_tree_entry *tree_entry = NULL;
	git_blob *blob = NULL;
	int error;

	if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 ||
	    (error = git_blob_lookup(&blob, git_tree_owner(reader->tree), git_tree_entry_id(tree_entry))) < 0 ||
	    (error = git_buf_set(out, git_blob_rawcontent(blob), git_blob_rawsize(blob))) < 0)
		goto done;

	if (out_id)
		git_oid_cpy(out_id, git_tree_entry_id(tree_entry));

	if (out_filemode)
		*out_filemode = git_tree_entry_filemode(tree_entry);

done:
	git_blob_free(blob);
	git_tree_entry_free(tree_entry);
	return error;
}
Beispiel #13
0
static void fill_origin_blob(git_blame__origin *o, mmfile_t *file)
{
	memset(file, 0, sizeof(*file));
	if (o->blob) {
		file->ptr = (char*)git_blob_rawcontent(o->blob);
		file->size = (size_t)git_blob_rawsize(o->blob);
	}
}
Beispiel #14
0
static int read_file_git(const char *repo_path, const char *name, void **out, size_t *outlen) {
    int ret, retcode = -1;

    git_repository *repo;
    ret = git_repository_open_bare(&repo, repo_path);
    if(check_lg2(ret, "opening repo"))
        goto out;

    git_object *master;
    ret = git_revparse_single(&master, repo, "master");
    if(check_lg2(ret, "getting master branch"))
        goto out_repo;

    if(git_object_type(master) != GIT_OBJ_COMMIT) {
        debug("master is not a commit");
        goto out_master;
    }

    git_tree *tree;
    ret = git_commit_tree(&tree, (git_commit*)master);
    if(check_lg2(ret, "getting tree from commit"))
        goto out_master;

    const git_tree_entry *entry = git_tree_entry_byname(tree, name);
    if(!entry) {
        debug("entry %s not found", name);
        goto out_tree;
    }

    if(git_tree_entry_type(entry) != GIT_OBJ_BLOB) {
        debug("entry is not a blob");
        goto out_tree;
    }

    git_object *file;
    ret = git_tree_entry_to_object(&file, repo, entry);
    if(check_lg2(ret, "getting file from tree entry"))
        goto out_tree;

    const void *data = git_blob_rawcontent((git_blob*)file);
    *outlen = git_blob_rawsize((git_blob*)file);

    *out = malloc(*outlen);
    memcpy(*out, data, *outlen);

    retcode = 0;

    git_object_free(file);
out_tree:
    git_tree_free(tree);
out_master:
    git_object_free(master);
out_repo:
    git_repository_free(repo);
out:
    return retcode;
}
Beispiel #15
0
void test_index_tests__add_frombuffer(void)
{
	git_index *index;
	git_repository *repo;
        git_index_entry entry;
	const git_index_entry *returned_entry;

	git_oid id1;
	git_blob *blob;

	const char *content = "hey there\n";

	cl_set_cleanup(&cleanup_myrepo, NULL);

	/* Intialize a new repository */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));

	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ echo "hey there" | git hash-object --stdin
	 */
	cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));

	/* Add the new file to the index */
	memset(&entry, 0x0, sizeof(git_index_entry));
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
	cl_git_pass(git_index_add_frombuffer(index, &entry,
		content, strlen(content)));

	/* Wow... it worked! */
	cl_assert(git_index_entrycount(index) == 1);
	returned_entry = git_index_get_byindex(index, 0);

	/* And the built-in hashing mechanism worked as expected */
	cl_assert_equal_oid(&id1, &returned_entry->id);
	/* And mode is the one asked */
	cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);

	/* Test access by path instead of index */
	cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	cl_assert_equal_oid(&id1, &returned_entry->id);

	/* Test the blob is in the repository */
	cl_git_pass(git_blob_lookup(&blob, repo, &id1));
	cl_assert_equal_s(
		content, git_blob_rawcontent(blob));
	git_blob_free(blob);

	git_index_free(index);
	git_repository_free(repo);
}
Beispiel #16
0
static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) {
	git_blob *blob;

	cl_assert_equal_s(git_note_message(note), message);
	cl_assert_equal_oid(git_note_id(note), note_oid);

	cl_git_pass(git_blob_lookup(&blob, _repo, note_oid));
	cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob));

	git_blob_free(blob);
}
Beispiel #17
0
int git_blob_is_binary(const git_blob *blob)
{
	git_buf content = GIT_BUF_INIT;

	assert(blob);

	git_buf_attach_notowned(&content, git_blob_rawcontent(blob),
		min(git_blob_rawsize(blob),
		GIT_FILTER_BYTES_TO_CHECK_NUL));
	return git_buf_text_is_binary(&content);
}
Beispiel #18
0
static Py_ssize_t
Blob_getreadbuffer(Blob *self, Py_ssize_t index, const void **ptr)
{
    if (index != 0) {
        PyErr_SetString(PyExc_SystemError,
                        "accessing non-existent blob segment");
        return -1;
    }
    *ptr = (void *) git_blob_rawcontent(self->blob);
    return git_blob_rawsize(self->blob);
}
Beispiel #19
0
static VALUE rb_git_blob_content_GET(VALUE self)
{
	git_blob *blob;
	int size;

	RUGGED_OBJ_UNWRAP(self, git_blob, blob);
	
	size = git_blob_rawsize(blob);
	if (size == 0)
		return rb_str_new2("");

	return rb_str_new(git_blob_rawcontent(blob), size);
}
Beispiel #20
0
static int note_new(git_note **out, git_oid *note_oid, git_blob *blob)
{
	git_note *note = NULL;

	note = (git_note *)git__malloc(sizeof(git_note));
	GITERR_CHECK_ALLOC(note);

	git_oid_cpy(&note->oid, note_oid);
	note->message = git__strdup((char *)git_blob_rawcontent(blob));
	GITERR_CHECK_ALLOC(note->message);

	*out = note;

	return 0;
}
Beispiel #21
0
static int diff_file_content_load_blob(git_diff_file_content *fc)
{
	int error = 0;
	git_odb_object *odb_obj = NULL;

	if (git_oid_iszero(&fc->file->oid))
		return 0;

	if (fc->file->mode == GIT_FILEMODE_COMMIT)
		return diff_file_content_commit_to_str(fc, false);

	/* if we don't know size, try to peek at object header first */
	if (!fc->file->size) {
		git_odb *odb;
		size_t len;
		git_otype type;

		if (!(error = git_repository_odb__weakptr(&odb, fc->repo))) {
			error = git_odb__read_header_or_object(
				&odb_obj, &len, &type, odb, &fc->file->oid);
			git_odb_free(odb);
		}
		if (error)
			return error;

		fc->file->size = len;
	}

	if (diff_file_content_binary_by_size(fc))
		return 0;

	if (odb_obj != NULL) {
		error = git_object__from_odb_object(
			(git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB);
		git_odb_object_free(odb_obj);
	} else {
		error = git_blob_lookup(
			(git_blob **)&fc->blob, fc->repo, &fc->file->oid);
	}

	if (!error) {
		fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
		fc->map.data = (void *)git_blob_rawcontent(fc->blob);
		fc->map.len  = (size_t)git_blob_rawsize(fc->blob);
	}

	return error;
}
Beispiel #22
0
/*
 * We keep on re-using the membuffer that we use for
 * strings, but the callback function can "steal" it by
 * saving its value and just clear the original.
 */
static void for_each_line(git_blob *blob, line_fn_t *fn, void *fndata)
{
	const char *content = git_blob_rawcontent(blob);
	unsigned int size = git_blob_rawsize(blob);
	struct membuffer str = { 0 };

	while (size) {
		unsigned int n = parse_one_line(content, size, fn, fndata, &str);
		content += n;
		size -= n;

		/* Re-use the allocation, but forget the data */
		str.len = 0;
	}
	free_buffer(&str);
}
Beispiel #23
0
void test_object_blob_filter__unfiltered(void)
{
	int i;
	git_blob *blob;

	for (i = 0; i < CRLF_NUM_TEST_OBJECTS; i++) {
		size_t raw_len = (size_t)g_crlf_raw_len[i];

		cl_git_pass(git_blob_lookup(&blob, g_repo, &g_crlf_oids[i]));

		cl_assert_equal_sz(raw_len, (size_t)git_blob_rawsize(blob));
		cl_assert_equal_i(
			0, memcmp(g_crlf_raw[i], git_blob_rawcontent(blob), raw_len));

		git_blob_free(blob);
	}
}
Beispiel #24
0
static int parse_picture_file(git_repository *repo, const git_tree_entry *entry, const char *name)
{
	/* remember the picture data so we can handle it when all dive data has been loaded
	 * the name of the git file is PIC-<hash> */
	git_blob *blob = git_tree_entry_blob(repo, entry);
	const void *rawdata = git_blob_rawcontent(blob);
	int len = git_blob_rawsize(blob);
	struct picture_entry_list *new_pel = malloc(sizeof(struct picture_entry_list));
	new_pel->next = pel;
	pel = new_pel;
	pel->data = malloc(len);
	memcpy(pel->data, rawdata, len);
	pel->len = len;
	pel->hash = strdup(name + 4);
	git_blob_free(blob);
	return 0;
}
Beispiel #25
0
/*
 *	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);
}
Beispiel #26
0
static int has_cr_in_index(const git_filter_source *src)
{
	git_repository *repo = git_filter_source_repo(src);
	const char *path = git_filter_source_path(src);
	git_index *index;
	const git_index_entry *entry;
	git_blob *blob;
	const void *blobcontent;
	git_off_t blobsize;
	bool found_cr;

	if (!path)
		return false;

	if (git_repository_index__weakptr(&index, repo) < 0) {
		giterr_clear();
		return false;
	}

	if (!(entry = git_index_get_bypath(index, path, 0)) &&
		!(entry = git_index_get_bypath(index, path, 1)))
		return false;

	if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */
		return true;

	if (git_blob_lookup(&blob, repo, &entry->id) < 0)
		return false;

	blobcontent = git_blob_rawcontent(blob);
	blobsize    = git_blob_rawsize(blob);
	if (!git__is_sizet(blobsize))
		blobsize = (size_t)-1;

	found_cr = (blobcontent != NULL &&
		blobsize > 0 &&
		memchr(blobcontent, '\r', (size_t)blobsize) != NULL);

	git_blob_free(blob);
	return found_cr;
}
Beispiel #27
0
static int diff_file_content_load_blob(
	git_diff_file_content *fc,
	git_diff_options *opts)
{
	int error = 0;
	git_odb_object *odb_obj = NULL;

	if (git_oid_iszero(&fc->file->id))
		return 0;

	if (fc->file->mode == GIT_FILEMODE_COMMIT)
		return diff_file_content_commit_to_str(fc, false);

	/* if we don't know size, try to peek at object header first */
	if (!fc->file->size) {
		if ((error = git_diff_file__resolve_zero_size(
				fc->file, &odb_obj, fc->repo)) < 0)
			return error;
	}

	if ((opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
		diff_file_content_binary_by_size(fc))
		return 0;

	if (odb_obj != NULL) {
		error = git_object__from_odb_object(
			(git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJECT_BLOB);
		git_odb_object_free(odb_obj);
	} else {
		error = git_blob_lookup(
			(git_blob **)&fc->blob, fc->repo, &fc->file->id);
	}

	if (!error) {
		fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
		fc->map.data = (void *)git_blob_rawcontent(fc->blob);
		fc->map.len  = (size_t)git_blob_rawsize(fc->blob);
	}

	return error;
}
Beispiel #28
0
int git_diff_file_content__init_from_src(
	git_diff_file_content *fc,
	git_repository *repo,
	const git_diff_options *opts,
	const git_diff_file_content_src *src,
	git_diff_file *as_file)
{
	memset(fc, 0, sizeof(*fc));
	fc->repo = repo;
	fc->file = as_file;

	if (!src->blob && !src->buf) {
		fc->flags |= GIT_DIFF_FLAG__NO_DATA;
	} else {
		fc->flags |= GIT_DIFF_FLAG__LOADED;
		fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
		fc->file->mode = GIT_FILEMODE_BLOB;

		if (src->blob) {
			git_blob_dup((git_blob **)&fc->blob, (git_blob *) src->blob);
			fc->file->size = git_blob_rawsize(src->blob);
			git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
			fc->file->id_abbrev = GIT_OID_HEXSZ;

			fc->map.len  = (size_t)fc->file->size;
			fc->map.data = (char *)git_blob_rawcontent(src->blob);

			fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
		} else {
			fc->file->size = src->buflen;
			git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB);
			fc->file->id_abbrev = GIT_OID_HEXSZ;

			fc->map.len  = src->buflen;
			fc->map.data = (char *)src->buf;
		}
	}

	return diff_file_content_init_common(fc, opts);
}
Beispiel #29
0
void test_merge_trees_automerge__automerge(void)
{
	git_index *index;
	const git_index_entry *entry;
	git_merge_tree_opts opts = GIT_MERGE_TREE_OPTS_INIT;
	git_blob *blob;

	struct merge_index_entry merge_index_entries[] = {
		ADDED_IN_MASTER_INDEX_ENTRY,
		AUTOMERGEABLE_INDEX_ENTRY,
		CHANGED_IN_BRANCH_INDEX_ENTRY,
		CHANGED_IN_MASTER_INDEX_ENTRY,

		{ 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 1, "conflicting.txt" },
		{ 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" },
		{ 0100644, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 3, "conflicting.txt" },

		UNCHANGED_INDEX_ENTRY,
	};

	struct merge_reuc_entry merge_reuc_entries[] = {
		AUTOMERGEABLE_REUC_ENTRY,
		REMOVED_IN_BRANCH_REUC_ENTRY,
		REMOVED_IN_MASTER_REUC_ENTRY
	};

	cl_git_pass(merge_trees_from_branches(&index, repo, "master", THEIRS_AUTOMERGE_BRANCH, &opts));

	cl_assert(merge_test_index(index, merge_index_entries, 8));
	cl_assert(merge_test_reuc(index, merge_reuc_entries, 3));

	cl_assert((entry = git_index_get_bypath(index, "automergeable.txt", 0)) != NULL);
	cl_assert(entry->file_size == strlen(AUTOMERGEABLE_MERGED_FILE));

	cl_git_pass(git_object_lookup((git_object **)&blob, repo, &entry->oid, GIT_OBJ_BLOB));
	cl_assert(memcmp(git_blob_rawcontent(blob), AUTOMERGEABLE_MERGED_FILE, (size_t)entry->file_size) == 0);

	git_index_free(index);
	git_blob_free(blob);
}
Beispiel #30
0
static VALUE rb_git_blob_content_GET(VALUE self)
{
	git_blob *blob;
	int size;

	RUGGED_OBJ_UNWRAP(self, git_blob, blob);
	
	size = git_blob_rawsize(blob);
	if (size == 0)
		return rugged_str_ascii("", 0);

	/*
	 * since we don't really ever know the encoding of a blob
	 * lets default to the binary encoding (ascii-8bit)
	 * If there is a way to tell, we should just pass 0/null here instead
	 *
	 * we're skipping the use of STR_NEW because we don't want our string to
	 * eventually end up converted to Encoding.default_internal because this
	 * string could very well be binary data
	 */
	return rugged_str_ascii(git_blob_rawcontent(blob), size);
}