Beispiel #1
0
void test_diff_parse__patch_roundtrip_succeeds(void)
{
	const char buf1[] = "a\n", buf2[] = "b\n";
	git_buf patchbuf = GIT_BUF_INIT, diffbuf = GIT_BUF_INIT;
	git_patch *patch;
	git_diff *diff;

	cl_git_pass(git_patch_from_buffers(&patch, buf1, strlen(buf1), "obj1", buf2, strlen(buf2), "obj2", NULL));
	cl_git_pass(git_patch_to_buf(&patchbuf, patch));

	cl_git_pass(git_diff_from_buffer(&diff, patchbuf.ptr, patchbuf.size));
	cl_git_pass(git_diff_to_buf(&diffbuf, diff, GIT_DIFF_FORMAT_PATCH));

	cl_assert_equal_s(patchbuf.ptr, diffbuf.ptr);

	git_patch_free(patch);
	git_diff_free(diff);
	git_buf_free(&patchbuf);
	git_buf_free(&diffbuf);
}
Beispiel #2
0
/*
 *  call-seq:
 *    Patch.from_strings(old_content = nil, new_content = nil, options = {}) -> patch
 *
 *  Directly generate a Rugged::Patch from the difference between the content of
 *  the two strings `old_content` and `new_content`.
 *
 *  The following options can be passed in the +options+ Hash:
 *
 *  :old_path ::
 *    An optional string to treat +blob+ as if it had this filename.
 *
 *  :new_path ::
 *    An optional string to treat +other+ as if it had this filename.
 *
 *  Additionally, `options` can also contain all other valid diff options
 *  (see Rugged::Tree#diff for a complete list).
 */
VALUE rb_git_patch_from_strings(int argc, VALUE *argv, VALUE self)
{
    git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
    git_patch *patch;
    char * old_path = NULL, * new_path = NULL;
    VALUE rb_old_buffer, rb_new_buffer, rb_options;

    rb_scan_args(argc, argv, "02:", &rb_old_buffer, &rb_new_buffer, &rb_options);

    if (!NIL_P(rb_options)) {
        VALUE rb_value;

        rb_value = rb_hash_aref(rb_options, CSTR2SYM("old_path"));
        if (!NIL_P(rb_value)) {
            Check_Type(rb_value, T_STRING);
            old_path = StringValueCStr(rb_value);
        }

        rb_value = rb_hash_aref(rb_options, CSTR2SYM("new_path"));
        if (!NIL_P(rb_value)) {
            Check_Type(rb_value, T_STRING);
            new_path = StringValueCStr(rb_value);
        }

        rugged_parse_diff_options(&opts, rb_options);
    }

    rugged_exception_check(git_patch_from_buffers(&patch,
                           NIL_P(rb_old_buffer) ? NULL : StringValuePtr(rb_old_buffer),
                           NIL_P(rb_old_buffer) ? 0 : RSTRING_LEN(rb_old_buffer),
                           old_path,
                           NIL_P(rb_new_buffer) ? NULL : StringValuePtr(rb_new_buffer),
                           NIL_P(rb_new_buffer) ? 0 : RSTRING_LEN(rb_new_buffer),
                           new_path,
                           &opts
                                                 ));

    return rugged_patch_new(self, patch);
}