void test_diff_blob__patch_with_freed_blobs(void) { git_oid a_oid, b_oid; git_blob *a, *b; git_patch *p; git_buf buf = GIT_BUF_INIT; /* 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)); cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, NULL)); git_blob_free(a); git_blob_free(b); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s(buf.ptr, BLOB_DIFF); git_patch_free(p); git_buf_free(&buf); }
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); }
size_t py_hex_to_git_oid (PyObject *py_oid, git_oid *oid) { PyObject *py_hex; int err; char *hex; Py_ssize_t len; #if PY_MAJOR_VERSION == 2 /* Bytes (only supported in Python 2) */ if (PyBytes_Check(py_oid)) { err = PyBytes_AsStringAndSize(py_oid, &hex, &len); if (err) return 0; err = git_oid_fromstrn(oid, hex, len); if (err < 0) { PyErr_SetObject(Error_type(err), py_oid); return 0; } return (size_t)len; } #endif /* Unicode */ if (PyUnicode_Check(py_oid)) { py_hex = PyUnicode_AsASCIIString(py_oid); if (py_hex == NULL) return 0; err = PyBytes_AsStringAndSize(py_hex, &hex, &len); if (err) { Py_DECREF(py_hex); return 0; } err = git_oid_fromstrn(oid, hex, len); Py_DECREF(py_hex); if (err < 0) { PyErr_SetObject(Error_type(err), py_oid); return 0; } return (size_t)len; } /* Type error */ PyErr_SetObject(PyExc_TypeError, py_oid); return 0; }
void test_diff_blob__can_compare_two_binary_blobs(void) { git_blob *heart; git_oid h_oid; /* heart.png */ cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8)); cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 8)); cl_git_pass(git_diff_blobs( alien, NULL, heart, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); assert_binary_blobs_comparison(&expected); memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( heart, NULL, alien, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); assert_binary_blobs_comparison(&expected); git_blob_free(heart); }
static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen) { git_oid oid; if (git_oid_fromstrn(&oid, spec, speclen) < 0) return GIT_ENOTFOUND; return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY); }
void test_diff_blob__can_compare_text_blobs_with_patch(void) { git_blob *a, *b, *c; git_oid a_oid, b_oid, c_oid; git_patch *p; /* 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, 8)); /* 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, 8)); /* 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 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts)); assert_patch_matches_blobs(p, a, b, 1, 6, 0, 1, 5, 0); git_patch_free(p); /* diff on tests/resources/attr/root_test2 */ cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts)); assert_patch_matches_blobs(p, b, c, 1, 15, 0, 3, 9, 3); git_patch_free(p); /* diff on tests/resources/attr/root_test3 */ cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts)); assert_patch_matches_blobs(p, a, c, 1, 13, 0, 0, 12, 1); git_patch_free(p); /* one more */ cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts)); assert_patch_matches_blobs(p, c, d, 2, 5, 9, 4, 6, 4); git_patch_free(p); git_blob_free(a); git_blob_free(b); git_blob_free(c); }
void test_diff_blob__initialize(void) { git_oid oid; g_repo = cl_git_sandbox_init("attr"); cl_git_pass(git_diff_init_options(&opts, GIT_DIFF_OPTIONS_VERSION)); opts.context_lines = 1; memset(&expected, 0, sizeof(expected)); /* tests/resources/attr/root_test4.txt */ cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8)); cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 8)); /* alien.png */ cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8)); cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 8)); }
void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void) { const fake_object objs[] = { { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, { NULL, NULL } }; char *hash = "f6ea0495187600e8"; setup_backend(objs); cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash))); cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid)); }
int py_str_to_git_oid(PyObject *py_str, git_oid *oid) { PyObject *py_hex; char *hex_or_bin; int err; Py_ssize_t len; /* Case 1: raw sha */ if (PyString_Check(py_str)) { hex_or_bin = PyString_AsString(py_str); if (hex_or_bin == NULL) return -1; git_oid_fromraw(oid, (const unsigned char*)hex_or_bin); return GIT_OID_HEXSZ; } /* Case 2: hex sha */ if (PyUnicode_Check(py_str)) { py_hex = PyUnicode_AsASCIIString(py_str); if (py_hex == NULL) return -1; err = PyString_AsStringAndSize(py_hex, &hex_or_bin, &len); if (err) { Py_DECREF(py_hex); return -1; } err = git_oid_fromstrn(oid, hex_or_bin, len); Py_DECREF(py_hex); if (err < 0) { PyErr_SetObject(Error_type(err), py_str); return -1; } return len; } /* Type error */ PyErr_Format(PyExc_TypeError, "Git object id must be byte or a text string, not: %.200s", Py_TYPE(py_str)->tp_name); return -1; }
void test_odb_mixed__dup_oid(void) { const char hex[] = "ce013625030ba8dba906f756967f9e9ca394464a"; const char short_hex[] = "ce01362"; git_oid oid; git_odb_object *obj; cl_git_pass(git_oid_fromstr(&oid, hex)); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, GIT_OID_HEXSZ)); git_odb_object_free(obj); cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, GIT_OID_HEXSZ)); cl_git_pass(git_oid_fromstrn(&oid, short_hex, sizeof(short_hex) - 1)); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, sizeof(short_hex) - 1)); git_odb_object_free(obj); cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, sizeof(short_hex) - 1)); }
git_tree *resolve_commit_oid_to_tree( git_repository *repo, const char *partial_oid) { size_t len = strlen(partial_oid); git_oid oid; git_object *obj = NULL; git_tree *tree = NULL; if (git_oid_fromstrn(&oid, partial_oid, len) == 0) git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY); cl_assert(obj); if (git_object_type(obj) == GIT_OBJ_TREE) return (git_tree *)obj; cl_assert(git_object_type(obj) == GIT_OBJ_COMMIT); cl_git_pass(git_commit_tree(&tree, (git_commit *)obj)); git_object_free(obj); return tree; }
void test_diff_blob__can_compare_blob_to_buffer(void) { git_blob *a; git_oid a_oid; const char *a_content = "Hello from the root\n"; const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n"; /* 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, 8)); /* diff from blob a to content of b */ quick_diff_blob_to_str(a, NULL, b_content, 0, NULL); assert_one_modified(1, 6, 1, 5, 0, &expected); /* diff from blob a to content of a */ opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; quick_diff_blob_to_str(a, NULL, a_content, 0, NULL); assert_identical_blobs_comparison(&expected); /* diff from NULL blob to content of a */ memset(&expected, 0, sizeof(expected)); quick_diff_blob_to_str(NULL, NULL, a_content, 0, NULL); assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED); /* diff from blob a to NULL buffer */ memset(&expected, 0, sizeof(expected)); quick_diff_blob_to_str(a, NULL, NULL, 0, NULL); assert_changed_single_one_line_file(&expected, GIT_DELTA_DELETED); /* diff with reverse */ opts.flags ^= GIT_DIFF_REVERSE; memset(&expected, 0, sizeof(expected)); quick_diff_blob_to_str(a, NULL, NULL, 0, NULL); assert_changed_single_one_line_file(&expected, GIT_DELTA_ADDED); git_blob_free(a); }
/* * $ git diff fe773770 a0f7217 * diff --git a/fe773770 b/a0f7217 * index fe77377..a0f7217 100644 * --- a/fe773770 * +++ b/a0f7217 * @@ -1,6 +1,6 @@ * Here is some stuff at the start * * -This should go in one hunk * +This should go in one hunk (first) * * Some additional lines * * @@ -8,7 +8,7 @@ Down here below the other lines * * With even more at the end * * -Followed by a second hunk of stuff * +Followed by a second hunk of stuff (second) * * That happens down here */ void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void) { git_blob *old_d; git_oid old_d_oid; opts.context_lines = 3; /* tests/resources/attr/root_test1 from commit f5b0af1 */ cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8)); cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 8)); /* Test with default inter-hunk-context (not set) => default is 0 */ cl_git_pass(git_diff_blobs( old_d, NULL, d, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); cl_assert_equal_i(2, expected.hunks); /* Test with inter-hunk-context explicitly set to 0 */ opts.interhunk_lines = 0; memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( old_d, NULL, d, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); cl_assert_equal_i(2, expected.hunks); /* Test with inter-hunk-context explicitly set to 1 */ opts.interhunk_lines = 1; memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( old_d, NULL, d, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); cl_assert_equal_i(1, expected.hunks); git_blob_free(old_d); }
static void assert_mergebase_many(const char *expected_sha, int count, ...) { va_list ap; int i; git_oid *oids; git_oid oid, expected; char *partial_oid; git_object *object; oids = git__malloc(count * sizeof(git_oid)); cl_assert(oids != NULL); memset(oids, 0x0, count * sizeof(git_oid)); va_start(ap, count); for (i = 0; i < count; ++i) { partial_oid = va_arg(ap, char *); cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid))); cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJ_COMMIT)); git_oid_cpy(&oids[i], git_object_id(object)); git_object_free(object); } va_end(ap); if (expected_sha == NULL) cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, count, oids)); else { cl_git_pass(git_merge_base_many(&oid, _repo, count, oids)); cl_git_pass(git_oid_fromstr(&expected, expected_sha)); cl_assert(git_oid_cmp(&expected, &oid) == 0); } git__free(oids); }
static int parse_header_oid( git_oid *oid, uint16_t *oid_len, git_patch_parse_ctx *ctx) { size_t len; for (len = 0; len < ctx->parse_ctx.line_len && len < GIT_OID_HEXSZ; len++) { if (!git__isxdigit(ctx->parse_ctx.line[len])) break; } if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_HEXSZ || git_oid_fromstrn(oid, ctx->parse_ctx.line, len) < 0) return git_parse_err("invalid hex formatted object id at line %"PRIuZ, ctx->parse_ctx.line_num); git_parse_advance_chars(&ctx->parse_ctx, len); *oid_len = (uint16_t)len; return 0; }
static void setup_prefix_query( git_odb_expand_id **out_ids, size_t *out_num) { git_odb_expand_id *ids; size_t num, i; num = ARRAY_SIZE(expand_id_test_data); cl_assert((ids = git__calloc(num, sizeof(git_odb_expand_id)))); for (i = 0; i < num; i++) { git_odb_expand_id *id = &ids[i]; size_t len = strlen(expand_id_test_data[i].lookup_id); git_oid_fromstrn(&id->id, expand_id_test_data[i].lookup_id, len); id->length = (unsigned short)len; id->type = expand_id_test_data[i].expected_type; } *out_ids = ids; *out_num = num; }
/** * Lookup an object in a repository * * @param repo S4 class git_repository * @param hex 4 to 40 char hexadecimal string * @return S4 object with lookup */ SEXP git2r_object_lookup(SEXP repo, SEXP hex) { int err; size_t len; SEXP result = R_NilValue; git_object *object = NULL; git_oid oid; git_repository *repository = NULL; if (git2r_error_check_hex_arg(hex)) error("Invalid arguments to git2r_object_lookup"); repository = git2r_repository_open(repo); if (!repository) error(git2r_err_invalid_repository); len = LENGTH(STRING_ELT(hex, 0)); if (GIT_OID_HEXSZ == len) { git_oid_fromstr(&oid, CHAR(STRING_ELT(hex, 0))); err = git_object_lookup(&object, repository, &oid, GIT_OBJ_ANY); if (err < 0) goto cleanup; } else { git_oid_fromstrn(&oid, CHAR(STRING_ELT(hex, 0)), len); err = git_object_lookup_prefix(&object, repository, &oid, len, GIT_OBJ_ANY); if (err < 0) goto cleanup; } switch (git_object_type(object)) { case GIT_OBJ_COMMIT: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_commit"))); git2r_commit_init((git_commit*)object, repo, result); break; case GIT_OBJ_TREE: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_tree"))); git2r_tree_init((git_tree*)object, repo, result); break; case GIT_OBJ_BLOB: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_blob"))); git2r_blob_init((git_blob*)object, repo, result); break; case GIT_OBJ_TAG: PROTECT(result = NEW_OBJECT(MAKE_CLASS("git_tag"))); git2r_tag_init((git_tag*)object, repo, result); break; default: error("Unimplemented"); } cleanup: if (object) git_object_free(object); if (repository) git_repository_free(repository); if (R_NilValue != result) UNPROTECT(1); if (err < 0) error("Error: %s\n", giterr_last()->message); return result; }
void test_odb_mixed__dup_oid_prefix_0(void) { char hex[10]; git_oid oid, found; git_odb_object *obj; /* ambiguous in the same pack file */ strncpy(hex, "dea509d0", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); strncpy(hex, "dea509d09", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); cl_assert_equal_oid(&found, git_odb_object_id(obj)); git_odb_object_free(obj); strncpy(hex, "dea509d0b", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); git_odb_object_free(obj); /* ambiguous in different pack files */ strncpy(hex, "81b5bff5", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); strncpy(hex, "81b5bff5b", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); cl_assert_equal_oid(&found, git_odb_object_id(obj)); git_odb_object_free(obj); strncpy(hex, "81b5bff5f", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); git_odb_object_free(obj); /* ambiguous in pack file and loose */ strncpy(hex, "0ddeaded", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_assert_equal_i( GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); strncpy(hex, "0ddeaded9", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex))); cl_assert_equal_oid(&found, git_odb_object_id(obj)); git_odb_object_free(obj); strncpy(hex, "0ddeadede", sizeof(hex)); cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex))); cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex))); git_odb_object_free(obj); }
int git_oid_fromstr(git_oid *out, const char *str) { return git_oid_fromstrn(out, str, GIT_OID_HEXSZ); }
int git_oid_fromstrp(git_oid *out, const char *str) { return git_oid_fromstrn(out, str, strlen(str)); }
int cmd_rev_list(git_repository *repo, int argc, char **argv) { const char *commit_string = NULL; const char *format = NULL; int i = 0; git_oid commit_oid; git_oid current_oid; char current_oid_str[GIT_OID_HEXSZ+1]; int err = 0; int rc = EXIT_FAILURE; git_commit *commit; git_revwalk *walk; for (i = 1; i < argc; i++) { if (*argv[i] == '-') { if (!prefixcmp(argv[i], "--pretty=")) { format = argv[i] + strlen("--pretty="); } else { fprintf(stderr,"Unknown option \"%s\"\n",argv[i]); goto out; } } else { commit_string = argv[i]; } } if (!commit_string) { fprintf(stderr,"No commit id given!\n"); goto out; } if (format) { fprintf(stderr,"Format \"%s\" not supported!\n",format); goto out; } if ((err = git_oid_fromstrn(&commit_oid,commit_string,strlen(commit_string))) != GIT_OK) goto out; if ((err = git_commit_lookup_prefix(&commit,repo,&commit_oid,strlen(commit_string))) != GIT_OK) goto out; if ((err = git_revwalk_new(&walk, repo)) != GIT_OK) goto out; git_revwalk_sorting(walk, GIT_SORT_TIME); if ((err = git_revwalk_push(walk, &commit_oid)) != GIT_OK) goto out; while ((git_revwalk_next(¤t_oid, walk)) == GIT_OK) { struct git_commit *wcommit; if (git_commit_lookup(&wcommit, repo, ¤t_oid) != 0) continue; git_oid_tostr(current_oid_str,sizeof(current_oid_str),¤t_oid); printf("%s\n",current_oid_str); git_commit_free(wcommit); } git_revwalk_free(walk); out: if (err) libgit_error(); if (commit) git_commit_free(commit); return rc; }
void test_diff_blob__using_path_and_attributes(void) { git_config *cfg; git_blob *bin, *nonbin; git_oid oid; const char *nonbin_content = "Hello from the root\n"; const char *bin_content = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"; size_t bin_len = 33; const char *changed; git_patch *p; git_buf buf = GIT_BUF_INIT; /* set up custom diff drivers and 'diff' attribute mappings for them */ cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "diff.iam_binary.binary", 1)); cl_git_pass(git_config_set_bool(cfg, "diff.iam_text.binary", 0)); cl_git_pass(git_config_set_string( cfg, "diff.iam_alphactx.xfuncname", "^[A-Za-z].*$")); cl_git_pass(git_config_set_bool(cfg, "diff.iam_textalpha.binary", 0)); cl_git_pass(git_config_set_string( cfg, "diff.iam_textalpha.xfuncname", "^[A-Za-z].*$")); cl_git_pass(git_config_set_string( cfg, "diff.iam_numctx.funcname", "^[0-9][0-9]*")); cl_git_pass(git_config_set_bool(cfg, "diff.iam_textnum.binary", 0)); cl_git_pass(git_config_set_string( cfg, "diff.iam_textnum.funcname", "^[0-9][0-9]*")); git_config_free(cfg); cl_git_append2file( "attr/.gitattributes", "\n\n# test_diff_blob__using_path_and_attributes extra\n\n" "*.binary diff=iam_binary\n" "*.textary diff=iam_text\n" "*.alphary diff=iam_alphactx\n" "*.textalphary diff=iam_textalpha\n" "*.textnumary diff=iam_textnum\n" "*.numary diff=iam_numctx\n\n"); opts.context_lines = 0; opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8)); cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8)); /* 20b: "Hello from the root\n" */ cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8)); cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8)); /* 33b: "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\n0123456789\n" */ /* non-binary to reference content */ quick_diff_blob_to_str(nonbin, NULL, nonbin_content, 0, NULL); assert_identical_blobs_comparison(&expected); cl_assert_equal_i(0, expected.files_binary); /* binary to reference content */ quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL); assert_identical_blobs_comparison(&expected); cl_assert_equal_i(1, expected.files_binary); /* add some text */ changed = "Hello from the root\nMore lines\nAnd more\nGo here\n"; quick_diff_blob_to_str(nonbin, NULL, changed, 0, NULL); assert_one_modified(1, 3, 0, 3, 0, &expected); quick_diff_blob_to_str(nonbin, "foo/bar.binary", changed, 0, NULL); cl_assert_equal_i(1, expected.files); cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, expected.files_binary); cl_assert_equal_i(0, expected.hunks); cl_assert_equal_i(0, expected.lines); quick_diff_blob_to_str(nonbin, "foo/bar.textary", changed, 0, NULL); assert_one_modified(1, 3, 0, 3, 0, &expected); quick_diff_blob_to_str(nonbin, "foo/bar.alphary", changed, 0, NULL); assert_one_modified(1, 3, 0, 3, 0, &expected); cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.normal b/zzz.normal\n" "index 45141a7..75b0dbb 100644\n" "--- a/zzz.normal\n" "+++ b/zzz.normal\n" "@@ -1,0 +2,3 @@ Hello from the root\n" "+More lines\n" "+And more\n" "+Go here\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.binary b/zzz.binary\n" "index 45141a7..75b0dbb 100644\n" "Binary files a/zzz.binary and b/zzz.binary differ\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.alphary b/zzz.alphary\n" "index 45141a7..75b0dbb 100644\n" "--- a/zzz.alphary\n" "+++ b/zzz.alphary\n" "@@ -1,0 +2,3 @@ Hello from the root\n" "+More lines\n" "+And more\n" "+Go here\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.numary b/zzz.numary\n" "index 45141a7..75b0dbb 100644\n" "--- a/zzz.numary\n" "+++ b/zzz.numary\n" "@@ -1,0 +2,3 @@\n" "+More lines\n" "+And more\n" "+Go here\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); /* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n" * 33 bytes */ changed = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\nreplace a line\n"; cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.normal", changed, 37, NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.normal b/zzz.normal\n" "index b435cd5..1604519 100644\n" "Binary files a/zzz.normal and b/zzz.normal differ\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textary", changed, 37, NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.textary b/zzz.textary\n" "index b435cd5..1604519 100644\n" "--- a/zzz.textary\n" "+++ b/zzz.textary\n" "@@ -3 +3 @@\n" "-0123456789\n" "+replace a line\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textalphary", changed, 37, NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.textalphary b/zzz.textalphary\n" "index b435cd5..1604519 100644\n" "--- a/zzz.textalphary\n" "+++ b/zzz.textalphary\n" "@@ -3 +3 @@\n" "-0123456789\n" "+replace a line\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textnumary", changed, 37, NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, p)); cl_assert_equal_s( "diff --git a/zzz.textnumary b/zzz.textnumary\n" "index b435cd5..1604519 100644\n" "--- a/zzz.textnumary\n" "+++ b/zzz.textnumary\n" "@@ -3 +3 @@ 0123456789\n" "-0123456789\n" "+replace a line\n", buf.ptr); git_buf_clear(&buf); git_patch_free(p); git_buf_free(&buf); git_blob_free(nonbin); git_blob_free(bin); }
void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) { git_patch *p; git_blob *a; git_oid a_oid; const char *a_content = "Hello from the root\n"; const char *b_content = "Hello from the root\n\nSome additional lines\n\nDown here below\n\n"; size_t tc, ta, td; /* 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, 8)); /* diff from blob a to content of b */ cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, b_content, strlen(b_content), NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0)); cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(1, (int)tc); cl_assert_equal_i(5, (int)ta); cl_assert_equal_i(0, (int)td); git_patch_free(p); /* diff from blob a to content of a */ opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); /* diff from NULL blob to content of a */ cl_git_pass(git_patch_from_blob_and_buffer( &p, NULL, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); /* diff from blob a to NULL buffer */ cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); /* diff with reverse */ opts.flags ^= GIT_DIFF_REVERSE; cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); git_blob_free(a); }
void test_diff_blob__binary_data_comparisons(void) { git_blob *bin, *nonbin; git_oid oid; const char *nonbin_content = "Hello from the root\n"; size_t nonbin_len = 20; const char *bin_content = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"; size_t bin_len = 33; opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_oid_fromstrn(&oid, "45141a79", 8)); cl_git_pass(git_blob_lookup_prefix(&nonbin, g_repo, &oid, 8)); cl_git_pass(git_oid_fromstrn(&oid, "b435cd56", 8)); cl_git_pass(git_blob_lookup_prefix(&bin, g_repo, &oid, 8)); /* non-binary to reference content */ quick_diff_blob_to_str(nonbin, NULL, nonbin_content, nonbin_len, NULL); assert_identical_blobs_comparison(&expected); cl_assert_equal_i(0, expected.files_binary); /* binary to reference content */ quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL); assert_identical_blobs_comparison(&expected); cl_assert_equal_i(1, expected.files_binary); /* non-binary to binary content */ quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL); assert_binary_blobs_comparison(&expected); /* binary to non-binary content */ quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL); assert_binary_blobs_comparison(&expected); /* non-binary to binary blob */ memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( bin, NULL, nonbin, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); assert_binary_blobs_comparison(&expected); /* * repeat with FORCE_TEXT */ opts.flags |= GIT_DIFF_FORCE_TEXT; quick_diff_blob_to_str(bin, NULL, bin_content, bin_len, NULL); assert_identical_blobs_comparison(&expected); quick_diff_blob_to_str(nonbin, NULL, bin_content, bin_len, NULL); assert_one_modified_with_lines(&expected, 4); quick_diff_blob_to_str(bin, NULL, nonbin_content, nonbin_len, NULL); assert_one_modified_with_lines(&expected, 4); memset(&expected, 0, sizeof(expected)); cl_git_pass(git_diff_blobs( bin, NULL, nonbin, NULL, &opts, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expected)); assert_one_modified_with_lines(&expected, 4); /* cleanup */ git_blob_free(bin); git_blob_free(nonbin); }