python_code
stringlengths
0
1.8M
repo_name
stringclasses
7 values
file_path
stringlengths
5
99
#include "clar.h" #include "clar_libgit2.h" #include "futils.h" #include "git2/cherrypick.h" #include "../merge/merge_helpers.h" #define TEST_REPO_PATH "cherrypick" static git_repository *repo; static git_index *repo_index; /* Fixture setup and teardown */ void test_cherrypick_workdir__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); } void test_cherrypick_workdir__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } /* git reset --hard d3d77487660ee3c0194ee01dc5eaf478782b1c7e * git cherry-pick cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick 964ea3da044d9083181a88ba6701de9e35778bf4 * git cherry-pick a43a050c588d4e92f11a6b139680923e9728477d */ void test_cherrypick_workdir__automerge(void) { git_oid head_oid; git_signature *signature = NULL; size_t i; const char *cherrypick_oids[] = { "cfc4f0999a8367568e049af4f72e452d40828a15", "964ea3da044d9083181a88ba6701de9e35778bf4", "a43a050c588d4e92f11a6b139680923e9728477d", }; struct merge_index_entry merge_index_entries[] = { { 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" }, { 0100644, "a661b5dec1004e2c62654ded3762370c27cf266b", 0, "file2.txt" }, { 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" }, { 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" }, { 0100644, "bd8fc3c59fb52d3c8b5907ace7defa5803f82419", 0, "file2.txt" }, { 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" }, { 0100644, "f06427bee380364bc7e0cb26a9245158e4726ce0", 0, "file1.txt" }, { 0100644, "bd8fc3c59fb52d3c8b5907ace7defa5803f82419", 0, "file2.txt" }, { 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" }, }; cl_git_pass(git_signature_new(&signature, "Picker", "[email protected]", time(NULL), 0)); git_oid__fromstr(&head_oid, "d3d77487660ee3c0194ee01dc5eaf478782b1c7e", GIT_OID_SHA1); for (i = 0; i < 3; ++i) { git_commit *head = NULL, *commit = NULL; git_oid cherry_oid, cherrypicked_oid, cherrypicked_tree_oid; git_tree *cherrypicked_tree = NULL; cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, cherrypick_oids[i], GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, NULL)); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD")); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); cl_git_pass(git_index_write_tree(&cherrypicked_tree_oid, repo_index)); cl_git_pass(git_tree_lookup(&cherrypicked_tree, repo, &cherrypicked_tree_oid)); cl_git_pass(git_commit_create(&cherrypicked_oid, repo, "HEAD", signature, signature, NULL, "Cherry picked!", cherrypicked_tree, 1, (const git_commit **)&head)); cl_assert(merge_test_index(repo_index, merge_index_entries + i * 3, 3)); git_oid_cpy(&head_oid, &cherrypicked_oid); git_tree_free(cherrypicked_tree); git_commit_free(head); git_commit_free(commit); } git_signature_free(signature); } /* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick a43a050c588d4e92f11a6b139680923e9728477d*/ void test_cherrypick_workdir__empty_result(void) { git_oid head_oid; git_signature *signature = NULL; git_commit *head = NULL, *commit = NULL; git_oid cherry_oid; const char *cherrypick_oid = "a43a050c588d4e92f11a6b139680923e9728477d"; struct merge_index_entry merge_index_entries[] = { { 0100644, "19c5c7207054604b69c84d08a7571ef9672bb5c2", 0, "file1.txt" }, { 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 0, "file2.txt" }, { 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 0, "file3.txt" }, }; cl_git_pass(git_signature_new(&signature, "Picker", "[email protected]", time(NULL), 0)); git_oid__fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15", GIT_OID_SHA1); /* Create an untracked file that should not conflict */ cl_git_mkfile(TEST_REPO_PATH "/file4.txt", ""); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/file4.txt")); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, cherrypick_oid, GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, NULL)); /* The resulting tree should not have changed, the change was already on HEAD */ cl_assert(merge_test_index(repo_index, merge_index_entries, 3)); git_commit_free(head); git_commit_free(commit); git_signature_free(signature); } /* git reset --hard bafbf6912c09505ac60575cd43d3f2aba3bd84d8 * git cherry-pick e9b63f3655b2ad80c0ff587389b5a9589a3a7110 */ void test_cherrypick_workdir__conflicts(void) { git_commit *head = NULL, *commit = NULL; git_oid head_oid, cherry_oid; git_str conflicting_buf = GIT_STR_INIT, mergemsg_buf = GIT_STR_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" }, { 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 1, "file2.txt" }, { 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 2, "file2.txt" }, { 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 3, "file2.txt" }, { 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 1, "file3.txt" }, { 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 2, "file3.txt" }, { 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt" }, }; git_oid__fromstr(&head_oid, "bafbf6912c09505ac60575cd43d3f2aba3bd84d8", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "e9b63f3655b2ad80c0ff587389b5a9589a3a7110", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, NULL)); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD")); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(merge_test_index(repo_index, merge_index_entries, 7)); cl_git_pass(git_futils_readbuffer(&mergemsg_buf, TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(strcmp(git_str_cstr(&mergemsg_buf), "Change all files\n" \ "\n" \ "#Conflicts:\n" \ "#\tfile2.txt\n" \ "#\tfile3.txt\n") == 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/file2.txt")); cl_assert(strcmp(git_str_cstr(&conflicting_buf), "!File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "File 2!!\n" \ "File 2\n" \ "File 2\n" \ "File 2\n" \ "<<<<<<< HEAD\n" \ "File 2\n" \ "=======\n" \ "File 2!\n" \ "File 2\n" \ "File 2!\n" \ ">>>>>>> e9b63f3... Change all files\n") == 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/file3.txt")); cl_assert(strcmp(git_str_cstr(&conflicting_buf), "!File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "File 3!!\n" \ "File 3\n" \ "File 3\n" \ "File 3\n" \ "<<<<<<< HEAD\n" \ "=======\n" \ "File 3!\n" \ "File 3!\n" \ ">>>>>>> e9b63f3... Change all files\n") == 0); git_commit_free(commit); git_commit_free(head); git_str_dispose(&mergemsg_buf); git_str_dispose(&conflicting_buf); } /* git reset --hard bafbf6912c09505ac60575cd43d3f2aba3bd84d8 * git cherry-pick -X ours e9b63f3655b2ad80c0ff587389b5a9589a3a7110 */ void test_cherrypick_workdir__conflict_use_ours(void) { git_commit *head = NULL, *commit = NULL; git_oid head_oid, cherry_oid; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" }, { 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 1, "file2.txt" }, { 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 2, "file2.txt" }, { 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 3, "file2.txt" }, { 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 1, "file3.txt" }, { 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 2, "file3.txt" }, { 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt" }, }; struct merge_index_entry merge_filesystem_entries[] = { { 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" }, { 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 0, "file2.txt" }, { 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 0, "file3.txt" }, }; /* leave the index in a conflicted state, but checkout "ours" to the workdir */ opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS; git_oid__fromstr(&head_oid, "bafbf6912c09505ac60575cd43d3f2aba3bd84d8", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "e9b63f3655b2ad80c0ff587389b5a9589a3a7110", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_index_entries, 7)); cl_assert(merge_test_workdir(repo, merge_filesystem_entries, 3)); /* resolve conflicts in the index by taking "ours" */ opts.merge_opts.file_favor = GIT_MERGE_FILE_FAVOR_OURS; cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_filesystem_entries, 3)); cl_assert(merge_test_workdir(repo, merge_filesystem_entries, 3)); git_commit_free(commit); git_commit_free(head); } /* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick 2a26c7e88b285613b302ba76712bc998863f3cbc */ void test_cherrypick_workdir__rename(void) { git_commit *head, *commit; git_oid head_oid, cherry_oid; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "19c5c7207054604b69c84d08a7571ef9672bb5c2", 0, "file1.txt" }, { 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 0, "file2.txt" }, { 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 0, "file3.txt.renamed" }, }; opts.merge_opts.flags |= GIT_MERGE_FIND_RENAMES; opts.merge_opts.rename_threshold = 50; git_oid__fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "2a26c7e88b285613b302ba76712bc998863f3cbc", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_index_entries, 3)); git_commit_free(commit); git_commit_free(head); } /* git reset --hard 44cd2ed2052c9c68f9a439d208e9614dc2a55c70 * git cherry-pick 2a26c7e88b285613b302ba76712bc998863f3cbc */ void test_cherrypick_workdir__both_renamed(void) { git_commit *head, *commit; git_oid head_oid, cherry_oid; git_str mergemsg_buf = GIT_STR_INIT; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "19c5c7207054604b69c84d08a7571ef9672bb5c2", 0, "file1.txt" }, { 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 0, "file2.txt" }, { 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 1, "file3.txt" }, { 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt.renamed" }, { 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 2, "file3.txt.renamed_on_branch" }, }; opts.merge_opts.flags |= GIT_MERGE_FIND_RENAMES; opts.merge_opts.rename_threshold = 50; git_oid__fromstr(&head_oid, "44cd2ed2052c9c68f9a439d208e9614dc2a55c70", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "2a26c7e88b285613b302ba76712bc998863f3cbc", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_index_entries, 5)); cl_git_pass(git_futils_readbuffer(&mergemsg_buf, TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(strcmp(git_str_cstr(&mergemsg_buf), "Renamed file3.txt -> file3.txt.renamed\n" \ "\n" \ "#Conflicts:\n" \ "#\tfile3.txt\n" \ "#\tfile3.txt.renamed\n" \ "#\tfile3.txt.renamed_on_branch\n") == 0); git_str_dispose(&mergemsg_buf); git_commit_free(commit); git_commit_free(head); } void test_cherrypick_workdir__nonmerge_fails_mainline_specified(void) { git_reference *head; git_commit *commit; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJECT_COMMIT)); opts.mainline = 1; cl_must_fail(git_cherrypick(repo, commit, &opts)); cl_assert(!git_fs_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD")); cl_assert(!git_fs_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); git_reference_free(head); git_commit_free(commit); } /* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick abe4603bc7cd5b8167a267e0e2418fd2348f8cff */ void test_cherrypick_workdir__merge_fails_without_mainline_specified(void) { git_commit *head, *commit; git_oid head_oid, cherry_oid; git_oid__fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_must_fail(git_cherrypick(repo, commit, NULL)); cl_assert(!git_fs_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD")); cl_assert(!git_fs_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG")); git_commit_free(commit); git_commit_free(head); } /* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick -m1 abe4603bc7cd5b8167a267e0e2418fd2348f8cff */ void test_cherrypick_workdir__merge_first_parent(void) { git_commit *head, *commit; git_oid head_oid, cherry_oid; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "f90f9dcbdac2cce5cc166346160e19cb693ef4e8", 0, "file1.txt" }, { 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 0, "file2.txt" }, { 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 0, "file3.txt" }, }; opts.mainline = 1; git_oid__fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_index_entries, 3)); git_commit_free(commit); git_commit_free(head); } /* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15 * git cherry-pick -m2 abe4603bc7cd5b8167a267e0e2418fd2348f8cff */ void test_cherrypick_workdir__merge_second_parent(void) { git_commit *head, *commit; git_oid head_oid, cherry_oid; git_cherrypick_options opts = GIT_CHERRYPICK_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "487434cace79238a7091e2220611d4f20a765690", 0, "file1.txt" }, { 0100644, "e5183bfd18e3a0a691fadde2f0d5610b73282d31", 0, "file2.txt" }, { 0100644, "409a1bec58bf35348e8b62b72bb9c1f45cf5a587", 0, "file3.txt" }, }; opts.mainline = 2; git_oid__fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&head, repo, &head_oid)); cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL)); git_oid__fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid)); cl_git_pass(git_cherrypick(repo, commit, &opts)); cl_assert(merge_test_index(repo_index, merge_index_entries, 3)); git_commit_free(commit); git_commit_free(head); }
libgit2-main
tests/libgit2/cherrypick/workdir.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "merge_helpers.h" #include "conflict_data.h" #include "refs.h" #include "futils.h" #include "diff_xdiff.h" #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" static git_repository *repo; static git_index *repo_index; /* Fixture setup and teardown */ void test_merge_files__initialize(void) { git_config *cfg; repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); git_config_free(cfg); } void test_merge_files__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } void test_merge_files__automerge_from_bufs(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_result result = {0}; const char *expected = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; ancestor.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100755; theirs.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; theirs.size = strlen(theirs.ptr); theirs.path = "testfile.txt"; theirs.mode = 0100755; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, 0)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("testfile.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(strlen(expected), result.len); cl_assert_equal_strn(expected, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__automerge_use_best_path_and_mode(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_result result = {0}; const char *expected = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; ancestor.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100644; theirs.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; theirs.size = strlen(theirs.ptr); theirs.path = "theirs.txt"; theirs.mode = 0100755; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, 0)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("theirs.txt", result.path); cl_assert_equal_i(0100644, result.mode); cl_assert_equal_i(strlen(expected), result.len); cl_assert_equal_strn(expected, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__conflict_from_bufs(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_result result = {0}; const char *expected = "<<<<<<< testfile.txt\nAloha!\nOurs.\n=======\nHi!\nTheirs.\n>>>>>>> theirs.txt\n"; size_t expected_len = strlen(expected); ancestor.ptr = "Hello!\nAncestor!\n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Aloha!\nOurs.\n"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100644; theirs.ptr = "Hi!\nTheirs.\n"; theirs.size = strlen(theirs.ptr); theirs.path = "theirs.txt"; theirs.mode = 0100755; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, NULL)); cl_assert_equal_i(0, result.automergeable); cl_assert_equal_s("theirs.txt", result.path); cl_assert_equal_i(0100644, result.mode); cl_assert_equal_i(expected_len, result.len); cl_assert_equal_strn(expected, result.ptr, expected_len); git_merge_file_result_free(&result); } void test_merge_files__automerge_from_index(void) { git_merge_file_result result = {0}; git_index_entry ancestor, ours, theirs; git_oid__fromstr(&ancestor.id, "6212c31dab5e482247d7977e4f0dd3601decf13b", GIT_OID_SHA1); ancestor.path = "automergeable.txt"; ancestor.mode = 0100644; git_oid__fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1); ours.path = "automergeable.txt"; ours.mode = 0100755; git_oid__fromstr(&theirs.id, "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe", GIT_OID_SHA1); theirs.path = "newname.txt"; theirs.mode = 0100644; cl_git_pass(git_merge_file_from_index(&result, repo, &ancestor, &ours, &theirs, 0)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("newname.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(strlen(AUTOMERGEABLE_MERGED_FILE), result.len); cl_assert_equal_strn(AUTOMERGEABLE_MERGED_FILE, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__automerge_whitespace_eol(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; const char *expected = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; ancestor.ptr = "0 \n1\n2\n3\n4\n5\n6\n7\n8\n9\n10 \n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Zero\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100755; theirs.ptr = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\nTen\n"; theirs.size = strlen(theirs.ptr); theirs.path = "testfile.txt"; theirs.mode = 0100755; opts.flags |= GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("testfile.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(strlen(expected), result.len); cl_assert_equal_strn(expected, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__automerge_whitespace_change(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; const char *expected = "Zero\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\nTen\n"; ancestor.ptr = "0\n1\n2\n3\n4\n5 XXX\n6YYY\n7\n8\n9\n10\n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Zero\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\n10\n"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100755; theirs.ptr = "0\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\nTen\n"; theirs.size = strlen(theirs.ptr); theirs.path = "testfile.txt"; theirs.mode = 0100755; opts.flags |= GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("testfile.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(strlen(expected), result.len); cl_assert_equal_strn(expected, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__doesnt_add_newline(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; const char *expected = "Zero\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\nTen"; ancestor.ptr = "0\n1\n2\n3\n4\n5 XXX\n6YYY\n7\n8\n9\n10"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "testfile.txt"; ancestor.mode = 0100755; ours.ptr = "Zero\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\n10"; ours.size = strlen(ours.ptr); ours.path = "testfile.txt"; ours.mode = 0100755; theirs.ptr = "0\n1\n2\n3\n4\n5 XXX\n6 YYY\n7\n8\n9\nTen"; theirs.size = strlen(theirs.ptr); theirs.path = "testfile.txt"; theirs.mode = 0100755; opts.flags |= GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("testfile.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(strlen(expected), result.len); cl_assert_equal_strn(expected, result.ptr, result.len); git_merge_file_result_free(&result); } void test_merge_files__skips_large_files(void) { git_merge_file_input ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; ours.size = GIT_XDIFF_MAX_SIZE + 1; ours.path = "testfile.txt"; ours.mode = 0100755; theirs.size = GIT_XDIFF_MAX_SIZE + 1; theirs.path = "testfile.txt"; theirs.mode = 0100755; cl_git_pass(git_merge_file(&result, NULL, &ours, &theirs, &opts)); cl_assert_equal_i(0, result.automergeable); git_merge_file_result_free(&result); } void test_merge_files__skips_binaries(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_result result = {0}; ancestor.ptr = "ance\0stor\0"; ancestor.size = 10; ancestor.path = "ancestor.txt"; ancestor.mode = 0100755; ours.ptr = "foo\0bar\0"; ours.size = 8; ours.path = "ours.txt"; ours.mode = 0100755; theirs.ptr = "bar\0foo\0"; theirs.size = 8; theirs.path = "theirs.txt"; theirs.mode = 0100644; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, NULL)); cl_assert_equal_i(0, result.automergeable); git_merge_file_result_free(&result); } void test_merge_files__handles_binaries_when_favored(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; ancestor.ptr = "ance\0stor\0"; ancestor.size = 10; ancestor.path = "ancestor.txt"; ancestor.mode = 0100755; ours.ptr = "foo\0bar\0"; ours.size = 8; ours.path = "ours.txt"; ours.mode = 0100755; theirs.ptr = "bar\0foo\0"; theirs.size = 8; theirs.path = "theirs.txt"; theirs.mode = 0100644; opts.favor = GIT_MERGE_FILE_FAVOR_OURS; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(1, result.automergeable); cl_assert_equal_s("ours.txt", result.path); cl_assert_equal_i(0100755, result.mode); cl_assert_equal_i(ours.size, result.len); cl_assert(memcmp(result.ptr, ours.ptr, ours.size) == 0); git_merge_file_result_free(&result); } void test_merge_files__crlf_conflict_markers_for_crlf_files(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; const char *expected = "<<<<<<< file.txt\r\nThis file\r\ndoes, too.\r\n" "=======\r\nAnd so does\r\nthis one.\r\n>>>>>>> file.txt\r\n"; size_t expected_len = strlen(expected); const char *expected_diff3 = "<<<<<<< file.txt\r\nThis file\r\ndoes, too.\r\n" "||||||| file.txt\r\nThis file has\r\nCRLF line endings.\r\n" "=======\r\nAnd so does\r\nthis one.\r\n>>>>>>> file.txt\r\n"; size_t expected_diff3_len = strlen(expected_diff3); ancestor.ptr = "This file has\r\nCRLF line endings.\r\n"; ancestor.size = 35; ancestor.path = "file.txt"; ancestor.mode = 0100644; ours.ptr = "This file\r\ndoes, too.\r\n"; ours.size = 23; ours.path = "file.txt"; ours.mode = 0100644; theirs.ptr = "And so does\r\nthis one.\r\n"; theirs.size = 24; theirs.path = "file.txt"; theirs.mode = 0100644; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(0, result.automergeable); cl_assert_equal_i(expected_len, result.len); cl_assert(memcmp(expected, result.ptr, expected_len) == 0); git_merge_file_result_free(&result); opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(0, result.automergeable); cl_assert_equal_i(expected_diff3_len, result.len); cl_assert(memcmp(expected_diff3, result.ptr, expected_len) == 0); git_merge_file_result_free(&result); } void test_merge_files__conflicts_in_zdiff3(void) { git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT, ours = GIT_MERGE_FILE_INPUT_INIT, theirs = GIT_MERGE_FILE_INPUT_INIT; git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT; git_merge_file_result result = {0}; const char *expected_zdiff3 = "1,\nfoo,\nbar,\n" \ "<<<<<<< file.txt\n" \ "||||||| file.txt\n# add more here\n" \ "=======\nquux,\nwoot,\n" \ ">>>>>>> file.txt\nbaz,\n3,\n"; size_t expected_zdiff3_len = strlen(expected_zdiff3); ancestor.ptr = "1,\n# add more here\n3,\n"; ancestor.size = strlen(ancestor.ptr); ancestor.path = "file.txt"; ancestor.mode = 0100644; ours.ptr = "1,\nfoo,\nbar,\nbaz,\n3,\n"; ours.size = strlen(ours.ptr); ours.path = "file.txt"; ours.mode = 0100644; theirs.ptr = "1,\nfoo,\nbar,\nquux,\nwoot,\nbaz,\n3,\n"; theirs.size = strlen(theirs.ptr); theirs.path = "file.txt"; theirs.mode = 0100644; opts.flags |= GIT_MERGE_FILE_STYLE_ZDIFF3; cl_git_pass(git_merge_file(&result, &ancestor, &ours, &theirs, &opts)); cl_assert_equal_i(0, result.automergeable); cl_assert_equal_i(expected_zdiff3_len, result.len); cl_assert(memcmp(expected_zdiff3, result.ptr, expected_zdiff3_len) == 0); git_merge_file_result_free(&result); }
libgit2-main
tests/libgit2/merge/files.c
/* NOTE: this is the implementation for both merge/trees/analysis.c and merge/workdir/analysis.c You probably want to make changes to both files. */ #include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "git2/annotated_commit.h" #include "git2/sys/index.h" #include "merge.h" #include "merge_helpers.h" #include "refs.h" #include "posix.h" #define TEST_REPO_PATH "merge-resolve" #define UPTODATE_BRANCH "master" #define PREVIOUS_BRANCH "previous" #define FASTFORWARD_BRANCH "ff_branch" #define FASTFORWARD_ID "fd89f8cffb663ac89095a0f9764902e93ceaca6a" #define NOFASTFORWARD_BRANCH "branch" #define NOFASTFORWARD_ID "7cb63eed597130ba4abb87b3e544b85021905520" static git_repository *sandbox; static git_repository *repo; void test_merge_analysis__initialize_with_bare_repository(void) { sandbox = cl_git_sandbox_init(TEST_REPO_PATH); cl_git_pass(git_repository_open_ext(&repo, git_repository_path(sandbox), GIT_REPOSITORY_OPEN_BARE, NULL)); } void test_merge_analysis__initialize_with_nonbare_repository(void) { sandbox = cl_git_sandbox_init(TEST_REPO_PATH); cl_git_pass(git_repository_open_ext(&repo, git_repository_workdir(sandbox), 0, NULL)); } void test_merge_analysis__cleanup(void) { git_repository_free(repo); cl_git_sandbox_cleanup(); } static void analysis_from_branch( git_merge_analysis_t *merge_analysis, git_merge_preference_t *merge_pref, const char *our_branchname, const char *their_branchname) { git_str our_refname = GIT_STR_INIT; git_str their_refname = GIT_STR_INIT; git_reference *our_ref; git_reference *their_ref; git_annotated_commit *their_head; if (our_branchname != NULL) { cl_git_pass(git_str_printf(&our_refname, "%s%s", GIT_REFS_HEADS_DIR, our_branchname)); cl_git_pass(git_reference_lookup(&our_ref, repo, git_str_cstr(&our_refname))); } else { cl_git_pass(git_reference_lookup(&our_ref, repo, GIT_HEAD_FILE)); } cl_git_pass(git_str_printf(&their_refname, "%s%s", GIT_REFS_HEADS_DIR, their_branchname)); cl_git_pass(git_reference_lookup(&their_ref, repo, git_str_cstr(&their_refname))); cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref)); cl_git_pass(git_merge_analysis_for_ref(merge_analysis, merge_pref, repo, our_ref, (const git_annotated_commit **)&their_head, 1)); git_str_dispose(&our_refname); git_str_dispose(&their_refname); git_annotated_commit_free(their_head); git_reference_free(our_ref); git_reference_free(their_ref); } void test_merge_analysis__fastforward(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis); } void test_merge_analysis__no_fastforward(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis); } void test_merge_analysis__uptodate(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, NULL, UPTODATE_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis); } void test_merge_analysis__uptodate_merging_prev_commit(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, NULL, PREVIOUS_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis); } void test_merge_analysis__unborn(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; git_str master = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&master, git_repository_path(repo), "refs/heads/master")); cl_must_pass(p_unlink(git_str_cstr(&master))); analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_FASTFORWARD|GIT_MERGE_ANALYSIS_UNBORN, merge_analysis); git_str_dispose(&master); } void test_merge_analysis__fastforward_with_config_noff(void) { git_config *config; git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.ff", "false")); analysis_from_branch(&merge_analysis, &merge_pref, NULL, FASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL|GIT_MERGE_ANALYSIS_FASTFORWARD, merge_analysis); cl_assert_equal_i(GIT_MERGE_PREFERENCE_NO_FASTFORWARD, (merge_pref & GIT_MERGE_PREFERENCE_NO_FASTFORWARD)); git_config_free(config); } void test_merge_analysis__no_fastforward_with_config_ffonly(void) { git_config *config; git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.ff", "only")); analysis_from_branch(&merge_analysis, &merge_pref, NULL, NOFASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis); cl_assert_equal_i(GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY, (merge_pref & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY)); git_config_free(config); } void test_merge_analysis__between_uptodate_refs(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, NOFASTFORWARD_BRANCH, PREVIOUS_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_UP_TO_DATE, merge_analysis); } void test_merge_analysis__between_noff_refs(void) { git_merge_analysis_t merge_analysis; git_merge_preference_t merge_pref; analysis_from_branch(&merge_analysis, &merge_pref, "branch", FASTFORWARD_BRANCH); cl_assert_equal_i(GIT_MERGE_ANALYSIS_NORMAL, merge_analysis); }
libgit2-main
tests/libgit2/merge/analysis.c
#include "clar_libgit2.h" static git_repository *g_repo; void test_merge_annotated_commit__initialize(void) { g_repo = cl_git_sandbox_init("testrepo"); } void test_merge_annotated_commit__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_annotated_commit__lookup_annotated_tag(void) { git_annotated_commit *commit; git_reference *ref; cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/tags/test")); cl_git_pass(git_annotated_commit_from_ref(&commit, g_repo, ref)); git_annotated_commit_free(commit); git_reference_free(ref); }
libgit2-main
tests/libgit2/merge/annotated_commit.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #define TEST_REPO_PATH "merge-resolve" #define BRANCH_ID "7cb63eed597130ba4abb87b3e544b85021905520" #define AUTOMERGEABLE_IDSTR "f2e1550a0c9e53d5811175864a29536642ae3821" static git_repository *repo; static git_index *repo_index; static git_oid automergeable_id; static void test_drivers_register(void); static void test_drivers_unregister(void); void test_merge_driver__initialize(void) { git_config *cfg; repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); git_oid__fromstr(&automergeable_id, AUTOMERGEABLE_IDSTR, GIT_OID_SHA1); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false)); test_drivers_register(); git_config_free(cfg); } void test_merge_driver__cleanup(void) { test_drivers_unregister(); git_index_free(repo_index); cl_git_sandbox_cleanup(); } struct test_merge_driver { git_merge_driver base; int initialized; int shutdown; }; static int test_driver_init(git_merge_driver *s) { struct test_merge_driver *self = (struct test_merge_driver *)s; self->initialized = 1; return 0; } static void test_driver_shutdown(git_merge_driver *s) { struct test_merge_driver *self = (struct test_merge_driver *)s; self->shutdown = 1; } static int test_driver_apply( git_merge_driver *s, const char **path_out, uint32_t *mode_out, git_buf *merged_out, const char *filter_name, const git_merge_driver_source *src) { git_str str = GIT_STR_INIT; int error; GIT_UNUSED(s); GIT_UNUSED(src); *path_out = "applied.txt"; *mode_out = GIT_FILEMODE_BLOB; error = git_str_printf(&str, "This is the `%s` driver.\n", filter_name); merged_out->ptr = str.ptr; merged_out->size = str.size; merged_out->reserved = 0; return error; } static struct test_merge_driver test_driver_custom = { { GIT_MERGE_DRIVER_VERSION, test_driver_init, test_driver_shutdown, test_driver_apply, }, 0, 0, }; static struct test_merge_driver test_driver_wildcard = { { GIT_MERGE_DRIVER_VERSION, test_driver_init, test_driver_shutdown, test_driver_apply, }, 0, 0, }; static void test_drivers_register(void) { cl_git_pass(git_merge_driver_register("custom", &test_driver_custom.base)); cl_git_pass(git_merge_driver_register("*", &test_driver_wildcard.base)); } static void test_drivers_unregister(void) { cl_git_pass(git_merge_driver_unregister("custom")); cl_git_pass(git_merge_driver_unregister("*")); } static void set_gitattributes_to(const char *driver) { git_str line = GIT_STR_INIT; if (driver && strcmp(driver, "")) git_str_printf(&line, "automergeable.txt merge=%s\n", driver); else if (driver) git_str_printf(&line, "automergeable.txt merge\n"); else git_str_printf(&line, "automergeable.txt -merge\n"); cl_assert(!git_str_oom(&line)); cl_git_mkfile(TEST_REPO_PATH "/.gitattributes", line.ptr); git_str_dispose(&line); } static void merge_branch(void) { git_oid their_id; git_annotated_commit *their_head; cl_git_pass(git_oid__fromstr(&their_id, BRANCH_ID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL)); git_annotated_commit_free(their_head); } void test_merge_driver__custom(void) { const char *expected = "This is the `custom` driver.\n"; set_gitattributes_to("custom"); merge_branch(); cl_assert_equal_file(expected, strlen(expected), TEST_REPO_PATH "/applied.txt"); } void test_merge_driver__wildcard(void) { const char *expected = "This is the `foobar` driver.\n"; set_gitattributes_to("foobar"); merge_branch(); cl_assert_equal_file(expected, strlen(expected), TEST_REPO_PATH "/applied.txt"); } void test_merge_driver__shutdown_is_called(void) { test_driver_custom.initialized = 0; test_driver_custom.shutdown = 0; test_driver_wildcard.initialized = 0; test_driver_wildcard.shutdown = 0; /* run the merge with the custom driver */ set_gitattributes_to("custom"); merge_branch(); /* unregister the drivers, ensure their shutdown function is called */ test_drivers_unregister(); /* since the `custom` driver was used, it should have been initialized and * shutdown, but the wildcard driver was not used at all and should not * have been initialized or shutdown. */ cl_assert(test_driver_custom.initialized); cl_assert(test_driver_custom.shutdown); cl_assert(!test_driver_wildcard.initialized); cl_assert(!test_driver_wildcard.shutdown); test_drivers_register(); } static int defer_driver_apply( git_merge_driver *s, const char **path_out, uint32_t *mode_out, git_buf *merged_out, const char *filter_name, const git_merge_driver_source *src) { GIT_UNUSED(s); GIT_UNUSED(path_out); GIT_UNUSED(mode_out); GIT_UNUSED(merged_out); GIT_UNUSED(filter_name); GIT_UNUSED(src); return GIT_PASSTHROUGH; } static struct test_merge_driver test_driver_defer_apply = { { GIT_MERGE_DRIVER_VERSION, test_driver_init, test_driver_shutdown, defer_driver_apply, }, 0, 0, }; void test_merge_driver__apply_can_defer(void) { const git_index_entry *idx; cl_git_pass(git_merge_driver_register("defer", &test_driver_defer_apply.base)); set_gitattributes_to("defer"); merge_branch(); cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0))); cl_assert_equal_oid(&automergeable_id, &idx->id); git_merge_driver_unregister("defer"); } static int conflict_driver_apply( git_merge_driver *s, const char **path_out, uint32_t *mode_out, git_buf *merged_out, const char *filter_name, const git_merge_driver_source *src) { GIT_UNUSED(s); GIT_UNUSED(path_out); GIT_UNUSED(mode_out); GIT_UNUSED(merged_out); GIT_UNUSED(filter_name); GIT_UNUSED(src); return GIT_EMERGECONFLICT; } static struct test_merge_driver test_driver_conflict_apply = { { GIT_MERGE_DRIVER_VERSION, test_driver_init, test_driver_shutdown, conflict_driver_apply, }, 0, 0, }; void test_merge_driver__apply_can_conflict(void) { const git_index_entry *ancestor, *ours, *theirs; cl_git_pass(git_merge_driver_register("conflict", &test_driver_conflict_apply.base)); set_gitattributes_to("conflict"); merge_branch(); cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs, repo_index, "automergeable.txt")); git_merge_driver_unregister("conflict"); } void test_merge_driver__default_can_be_specified(void) { git_oid their_id; git_annotated_commit *their_head; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; const char *expected = "This is the `custom` driver.\n"; merge_opts.default_driver = "custom"; cl_git_pass(git_oid__fromstr(&their_id, BRANCH_ID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, &merge_opts, NULL)); git_annotated_commit_free(their_head); cl_assert_equal_file(expected, strlen(expected), TEST_REPO_PATH "/applied.txt"); } void test_merge_driver__honors_builtin_mergedefault(void) { const git_index_entry *ancestor, *ours, *theirs; cl_repo_set_string(repo, "merge.default", "binary"); merge_branch(); cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs, repo_index, "automergeable.txt")); } void test_merge_driver__honors_custom_mergedefault(void) { const char *expected = "This is the `custom` driver.\n"; cl_repo_set_string(repo, "merge.default", "custom"); merge_branch(); cl_assert_equal_file(expected, strlen(expected), TEST_REPO_PATH "/applied.txt"); } void test_merge_driver__mergedefault_deferring_falls_back_to_text(void) { const git_index_entry *idx; cl_git_pass(git_merge_driver_register("defer", &test_driver_defer_apply.base)); cl_repo_set_string(repo, "merge.default", "defer"); merge_branch(); cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0))); cl_assert_equal_oid(&automergeable_id, &idx->id); git_merge_driver_unregister("defer"); } void test_merge_driver__set_forces_text(void) { const git_index_entry *idx; /* `merge` without specifying a driver indicates `text` */ set_gitattributes_to(""); cl_repo_set_string(repo, "merge.default", "custom"); merge_branch(); cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0))); cl_assert_equal_oid(&automergeable_id, &idx->id); } void test_merge_driver__unset_forces_binary(void) { const git_index_entry *ancestor, *ours, *theirs; /* `-merge` without specifying a driver indicates `binary` */ set_gitattributes_to(NULL); cl_repo_set_string(repo, "merge.default", "custom"); merge_branch(); cl_git_pass(git_index_conflict_get(&ancestor, &ours, &theirs, repo_index, "automergeable.txt")); } void test_merge_driver__not_configured_driver_falls_back(void) { const git_index_entry *idx; test_drivers_unregister(); /* `merge` without specifying a driver indicates `text` */ set_gitattributes_to("notfound"); merge_branch(); cl_assert((idx = git_index_get_bypath(repo_index, "automergeable.txt", 0))); cl_assert_equal_oid(&automergeable_id, &idx->id); test_drivers_register(); }
libgit2-main
tests/libgit2/merge/driver.c
#include "clar_libgit2.h" #include "futils.h" #include "refs.h" #include "tree.h" #include "merge_helpers.h" #include "merge.h" #include "index.h" #include "git2/merge.h" #include "git2/sys/index.h" #include "git2/annotated_commit.h" int merge_trees_from_branches( git_index **index, git_repository *repo, const char *ours_name, const char *theirs_name, git_merge_options *opts) { git_commit *our_commit, *their_commit, *ancestor_commit = NULL; git_tree *our_tree, *their_tree, *ancestor_tree = NULL; git_oid our_oid, their_oid, ancestor_oid; git_str branch_buf = GIT_STR_INIT; int error; git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours_name); cl_git_pass(git_reference_name_to_id(&our_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); git_str_clear(&branch_buf); git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs_name); cl_git_pass(git_reference_name_to_id(&their_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&their_commit, repo, &their_oid)); error = git_merge_base(&ancestor_oid, repo, git_commit_id(our_commit), git_commit_id(their_commit)); if (error != GIT_ENOTFOUND) { cl_git_pass(error); cl_git_pass(git_commit_lookup(&ancestor_commit, repo, &ancestor_oid)); cl_git_pass(git_commit_tree(&ancestor_tree, ancestor_commit)); } cl_git_pass(git_commit_tree(&our_tree, our_commit)); cl_git_pass(git_commit_tree(&their_tree, their_commit)); error = git_merge_trees(index, repo, ancestor_tree, our_tree, their_tree, opts); git_str_dispose(&branch_buf); git_tree_free(our_tree); git_tree_free(their_tree); git_tree_free(ancestor_tree); git_commit_free(our_commit); git_commit_free(their_commit); git_commit_free(ancestor_commit); return error; } int merge_commits_from_branches( git_index **index, git_repository *repo, const char *ours_name, const char *theirs_name, git_merge_options *opts) { git_commit *our_commit, *their_commit; git_oid our_oid, their_oid; git_str branch_buf = GIT_STR_INIT; int error; git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours_name); cl_git_pass(git_reference_name_to_id(&our_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); git_str_clear(&branch_buf); git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs_name); cl_git_pass(git_reference_name_to_id(&their_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&their_commit, repo, &their_oid)); error = git_merge_commits(index, repo, our_commit, their_commit, opts); git_str_dispose(&branch_buf); git_commit_free(our_commit); git_commit_free(their_commit); return error; } int merge_branches(git_repository *repo, const char *ours_branch, const char *theirs_branch, git_merge_options *merge_opts, git_checkout_options *checkout_opts) { git_reference *head_ref, *theirs_ref; git_annotated_commit *theirs_head; git_checkout_options head_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; head_checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; cl_git_pass(git_reference_symbolic_create(&head_ref, repo, "HEAD", ours_branch, 1, NULL)); cl_git_pass(git_checkout_head(repo, &head_checkout_opts)); cl_git_pass(git_reference_lookup(&theirs_ref, repo, theirs_branch)); cl_git_pass(git_annotated_commit_from_ref(&theirs_head, repo, theirs_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&theirs_head, 1, merge_opts, checkout_opts)); git_reference_free(head_ref); git_reference_free(theirs_ref); git_annotated_commit_free(theirs_head); return 0; } void merge__dump_index_entries(git_vector *index_entries) { size_t i; const git_index_entry *index_entry; printf ("\nINDEX [%"PRIuZ"]:\n", index_entries->length); for (i = 0; i < index_entries->length; i++) { index_entry = index_entries->contents[i]; printf("%o ", index_entry->mode); printf("%s ", git_oid_allocfmt(&index_entry->id)); printf("%d ", git_index_entry_stage(index_entry)); printf("%s ", index_entry->path); printf("\n"); } printf("\n"); } void merge__dump_names(git_index *index) { size_t i; const git_index_name_entry *conflict_name; for (i = 0; i < git_index_name_entrycount(index); i++) { conflict_name = git_index_name_get_byindex(index, i); printf("%s %s %s\n", conflict_name->ancestor, conflict_name->ours, conflict_name->theirs); } printf("\n"); } void merge__dump_reuc(git_index *index) { size_t i; const git_index_reuc_entry *reuc; printf ("\nREUC:\n"); for (i = 0; i < git_index_reuc_entrycount(index); i++) { reuc = git_index_reuc_get_byindex(index, i); printf("%s ", reuc->path); printf("%o ", reuc->mode[0]); printf("%s\n", git_oid_allocfmt(&reuc->oid[0])); printf(" %o ", reuc->mode[1]); printf(" %s\n", git_oid_allocfmt(&reuc->oid[1])); printf(" %o ", reuc->mode[2]); printf(" %s ", git_oid_allocfmt(&reuc->oid[2])); printf("\n"); } printf("\n"); } static int index_entry_eq_merge_index_entry(const struct merge_index_entry *expected, const git_index_entry *actual) { git_oid expected_oid; bool test_oid; if (strlen(expected->oid_str) != 0) { cl_git_pass(git_oid__fromstr(&expected_oid, expected->oid_str, GIT_OID_SHA1)); test_oid = 1; } else test_oid = 0; if (actual->mode != expected->mode || (test_oid && git_oid_cmp(&actual->id, &expected_oid) != 0) || git_index_entry_stage(actual) != expected->stage) return 0; if (actual->mode == 0 && (actual->path != NULL || strlen(expected->path) > 0)) return 0; if (actual->mode != 0 && (strcmp(actual->path, expected->path) != 0)) return 0; return 1; } static int name_entry_eq(const char *expected, const char *actual) { if (strlen(expected) == 0) return (actual == NULL) ? 1 : 0; return (strcmp(expected, actual) == 0) ? 1 : 0; } static int name_entry_eq_merge_name_entry(const struct merge_name_entry *expected, const git_index_name_entry *actual) { if (name_entry_eq(expected->ancestor_path, actual->ancestor) == 0 || name_entry_eq(expected->our_path, actual->ours) == 0 || name_entry_eq(expected->their_path, actual->theirs) == 0) return 0; return 1; } static int index_conflict_data_eq_merge_diff(const struct merge_index_conflict_data *expected, git_merge_diff *actual) { if (!index_entry_eq_merge_index_entry(&expected->ancestor.entry, &actual->ancestor_entry) || !index_entry_eq_merge_index_entry(&expected->ours.entry, &actual->our_entry) || !index_entry_eq_merge_index_entry(&expected->theirs.entry, &actual->their_entry)) return 0; if (expected->ours.status != actual->our_status || expected->theirs.status != actual->their_status) return 0; return 1; } int merge_test_merge_conflicts(git_vector *conflicts, const struct merge_index_conflict_data expected[], size_t expected_len) { git_merge_diff *actual; size_t i; if (conflicts->length != expected_len) return 0; for (i = 0; i < expected_len; i++) { actual = conflicts->contents[i]; if (!index_conflict_data_eq_merge_diff(&expected[i], actual)) return 0; } return 1; } int merge_test_index(git_index *index, const struct merge_index_entry expected[], size_t expected_len) { size_t i; const git_index_entry *index_entry; /* merge__dump_index_entries(&index->entries); */ if (git_index_entrycount(index) != expected_len) return 0; for (i = 0; i < expected_len; i++) { if ((index_entry = git_index_get_byindex(index, i)) == NULL) return 0; if (!index_entry_eq_merge_index_entry(&expected[i], index_entry)) return 0; } return 1; } int merge_test_names(git_index *index, const struct merge_name_entry expected[], size_t expected_len) { size_t i; const git_index_name_entry *name_entry; /* dump_names(index); */ if (git_index_name_entrycount(index) != expected_len) return 0; for (i = 0; i < expected_len; i++) { if ((name_entry = git_index_name_get_byindex(index, i)) == NULL) return 0; if (! name_entry_eq_merge_name_entry(&expected[i], name_entry)) return 0; } return 1; } int merge_test_reuc(git_index *index, const struct merge_reuc_entry expected[], size_t expected_len) { size_t i; const git_index_reuc_entry *reuc_entry; git_oid expected_oid; /* dump_reuc(index); */ if (git_index_reuc_entrycount(index) != expected_len) return 0; for (i = 0; i < expected_len; i++) { if ((reuc_entry = git_index_reuc_get_byindex(index, i)) == NULL) return 0; if (strcmp(reuc_entry->path, expected[i].path) != 0 || reuc_entry->mode[0] != expected[i].ancestor_mode || reuc_entry->mode[1] != expected[i].our_mode || reuc_entry->mode[2] != expected[i].their_mode) return 0; if (expected[i].ancestor_mode > 0) { cl_git_pass(git_oid__fromstr(&expected_oid, expected[i].ancestor_oid_str, GIT_OID_SHA1)); if (git_oid_cmp(&reuc_entry->oid[0], &expected_oid) != 0) return 0; } if (expected[i].our_mode > 0) { cl_git_pass(git_oid__fromstr(&expected_oid, expected[i].our_oid_str, GIT_OID_SHA1)); if (git_oid_cmp(&reuc_entry->oid[1], &expected_oid) != 0) return 0; } if (expected[i].their_mode > 0) { cl_git_pass(git_oid__fromstr(&expected_oid, expected[i].their_oid_str, GIT_OID_SHA1)); if (git_oid_cmp(&reuc_entry->oid[2], &expected_oid) != 0) return 0; } } return 1; } static int dircount(void *payload, git_str *pathbuf) { size_t *entries = payload; size_t len = git_str_len(pathbuf); if (len < 5 || strcmp(pathbuf->ptr + (git_str_len(pathbuf) - 5), "/.git") != 0) (*entries)++; return 0; } int merge_test_workdir(git_repository *repo, const struct merge_index_entry expected[], size_t expected_len) { size_t actual_len = 0, i; git_oid actual_oid, expected_oid; git_str wd = GIT_STR_INIT; git_str_puts(&wd, repo->workdir); git_fs_path_direach(&wd, 0, dircount, &actual_len); if (actual_len != expected_len) return 0; for (i = 0; i < expected_len; i++) { git_blob_create_from_workdir(&actual_oid, repo, expected[i].path); git_oid__fromstr(&expected_oid, expected[i].oid_str, GIT_OID_SHA1); if (git_oid_cmp(&actual_oid, &expected_oid) != 0) return 0; } git_str_dispose(&wd); return 1; }
libgit2-main
tests/libgit2/merge/merge_helpers.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "refs.h" #include "futils.h" #include "git2/sys/index.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" /* Fixture setup and teardown */ void test_merge_trees_trivial__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_trivial__cleanup(void) { cl_git_sandbox_cleanup(); } static int merge_trivial(git_index **index, const char *ours, const char *theirs) { git_commit *our_commit, *their_commit, *ancestor_commit; git_tree *our_tree, *their_tree, *ancestor_tree; git_oid our_oid, their_oid, ancestor_oid; git_str branch_buf = GIT_STR_INIT; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours); cl_git_pass(git_reference_name_to_id(&our_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); git_str_clear(&branch_buf); git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs); cl_git_pass(git_reference_name_to_id(&their_oid, repo, branch_buf.ptr)); cl_git_pass(git_commit_lookup(&their_commit, repo, &their_oid)); cl_git_pass(git_merge_base(&ancestor_oid, repo, git_commit_id(our_commit), git_commit_id(their_commit))); cl_git_pass(git_commit_lookup(&ancestor_commit, repo, &ancestor_oid)); cl_git_pass(git_commit_tree(&ancestor_tree, ancestor_commit)); cl_git_pass(git_commit_tree(&our_tree, our_commit)); cl_git_pass(git_commit_tree(&their_tree, their_commit)); cl_git_pass(git_merge_trees(index, repo, ancestor_tree, our_tree, their_tree, &opts)); git_str_dispose(&branch_buf); git_tree_free(our_tree); git_tree_free(their_tree); git_tree_free(ancestor_tree); git_commit_free(our_commit); git_commit_free(their_commit); git_commit_free(ancestor_commit); return 0; } static int merge_trivial_conflict_entrycount(git_index *index) { const git_index_entry *entry; int count = 0; size_t i; for (i = 0; i < git_index_entrycount(index); i++) { cl_assert(entry = git_index_get_byindex(index, i)); if (git_index_entry_is_conflict(entry)) count++; } return count; } /* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote */ void test_merge_trees_trivial__2alt(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-2alt", "trivial-2alt-branch")); cl_assert(entry = git_index_get_bypath(result, "new-in-branch.txt", 0)); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head */ void test_merge_trees_trivial__3alt(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-3alt", "trivial-3alt-branch")); cl_assert(entry = git_index_get_bypath(result, "new-in-3alt.txt", 0)); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 4: ancest:(empty)^, head:head, remote:remote = result:no merge */ void test_merge_trees_trivial__4(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-4", "trivial-4-branch")); cl_assert((entry = git_index_get_bypath(result, "new-and-different.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 2); cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 2)); cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 3)); git_index_free(result); } /* 5ALT: ancest:*, head:head, remote:head = result:head */ void test_merge_trees_trivial__5alt_1(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-5alt-1", "trivial-5alt-1-branch")); cl_assert(entry = git_index_get_bypath(result, "new-and-same.txt", 0)); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 5ALT: ancest:*, head:head, remote:head = result:head */ void test_merge_trees_trivial__5alt_2(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-5alt-2", "trivial-5alt-2-branch")); cl_assert(entry = git_index_get_bypath(result, "modified-to-same.txt", 0)); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */ void test_merge_trees_trivial__6(void) { git_index *result; const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial(&result, "trivial-6", "trivial-6-branch")); cl_assert((entry = git_index_get_bypath(result, "removed-in-both.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 1); cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-both.txt")); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */ void test_merge_trees_trivial__8(void) { git_index *result; const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial(&result, "trivial-8", "trivial-8-branch")); cl_assert((entry = git_index_get_bypath(result, "removed-in-8.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 1); cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-8.txt")); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge */ void test_merge_trees_trivial__7(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-7", "trivial-7-branch")); cl_assert((entry = git_index_get_bypath(result, "removed-in-7.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 2); cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 1)); cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 3)); git_index_free(result); } /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */ void test_merge_trees_trivial__10(void) { git_index *result; const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial(&result, "trivial-10", "trivial-10-branch")); cl_assert((entry = git_index_get_bypath(result, "removed-in-10-branch.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 1); cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-10-branch.txt")); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge */ void test_merge_trees_trivial__9(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-9", "trivial-9-branch")); cl_assert((entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 2); cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 1)); cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 2)); git_index_free(result); } /* 13: ancest:ancest+, head:head, remote:ancest = result:head */ void test_merge_trees_trivial__13(void) { git_index *result; const git_index_entry *entry; git_oid expected_oid; cl_git_pass(merge_trivial(&result, "trivial-13", "trivial-13-branch")); cl_assert(entry = git_index_get_bypath(result, "modified-in-13.txt", 0)); cl_git_pass(git_oid__fromstr(&expected_oid, "1cff9ec6a47a537380dedfdd17c9e76d74259a2b", GIT_OID_SHA1)); cl_assert_equal_oid(&expected_oid, &entry->id); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */ void test_merge_trees_trivial__14(void) { git_index *result; const git_index_entry *entry; git_oid expected_oid; cl_git_pass(merge_trivial(&result, "trivial-14", "trivial-14-branch")); cl_assert(entry = git_index_get_bypath(result, "modified-in-14-branch.txt", 0)); cl_git_pass(git_oid__fromstr(&expected_oid, "26153a3ff3649b6c2bb652d3f06878c6e0a172f9", GIT_OID_SHA1)); cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 0); git_index_free(result); } /* 11: ancest:ancest+, head:head, remote:remote = result:no merge */ void test_merge_trees_trivial__11(void) { git_index *result; const git_index_entry *entry; cl_git_pass(merge_trivial(&result, "trivial-11", "trivial-11-branch")); cl_assert((entry = git_index_get_bypath(result, "modified-in-both.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(result) == 0); cl_assert(merge_trivial_conflict_entrycount(result) == 3); cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 1)); cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 2)); cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 3)); git_index_free(result); }
libgit2-main
tests/libgit2/merge/trees/trivial.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "futils.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define DF_SIDE1_BRANCH "df_side1" #define DF_SIDE2_BRANCH "df_side2" /* Fixture setup and teardown */ void test_merge_trees_modeconflict__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_modeconflict__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_modeconflict__df_conflict(void) { git_index *index; struct merge_index_entry merge_index_entries[] = { { 0100644, "49130a28ef567af9a6a6104c38773fedfa5f9742", 2, "dir-10" }, { 0100644, "6c06dcd163587c2cc18be44857e0b71116382aeb", 3, "dir-10" }, { 0100644, "43aafd43bea779ec74317dc361f45ae3f532a505", 0, "dir-6" }, { 0100644, "a031a28ae70e33a641ce4b8a8f6317f1ab79dee4", 3, "dir-7" }, { 0100644, "5012fd565b1393bdfda1805d4ec38ce6619e1fd1", 1, "dir-7/file.txt" }, { 0100644, "a5563304ddf6caba25cb50323a2ea6f7dbfcadca", 2, "dir-7/file.txt" }, { 0100644, "e9ad6ec3e38364a3d07feda7c4197d4d845c53b5", 0, "dir-8" }, { 0100644, "3ef4d30382ca33fdeba9fda895a99e0891ba37aa", 2, "dir-9" }, { 0100644, "fc4c636d6515e9e261f9260dbcf3cc6eca97ea08", 1, "dir-9/file.txt" }, { 0100644, "76ab0e2868197ec158ddd6c78d8a0d2fd73d38f9", 3, "dir-9/file.txt" }, { 0100644, "5c2411f8075f48a6b2fdb85ebc0d371747c4df15", 0, "file-1/new" }, { 0100644, "a39a620dae5bc8b4e771cd4d251b7d080401a21e", 1, "file-2" }, { 0100644, "d963979c237d08b6ba39062ee7bf64c7d34a27f8", 2, "file-2" }, { 0100644, "5c341ead2ba6f2af98ce5ec3fe84f6b6d2899c0d", 0, "file-2/new" }, { 0100644, "9efe7723802d4305142eee177e018fee1572c4f4", 0, "file-3/new" }, { 0100644, "bacac9b3493509aa15e1730e1545fc0919d1dae0", 1, "file-4" }, { 0100644, "7663fce0130db092936b137cabd693ec234eb060", 3, "file-4" }, { 0100644, "e49f917b448d1340b31d76e54ba388268fd4c922", 0, "file-4/new" }, { 0100644, "cab2cf23998b40f1af2d9d9a756dc9e285a8df4b", 2, "file-5/new" }, { 0100644, "f5504f36e6f4eb797a56fc5bac6c6c7f32969bf2", 3, "file-5/new" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, DF_SIDE1_BRANCH, DF_SIDE2_BRANCH, NULL)); cl_assert(merge_test_index(index, merge_index_entries, 20)); git_index_free(index); }
libgit2-main
tests/libgit2/merge/trees/modeconflict.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" static git_repository *repo; #define TEST_REPO_PATH "merge-recursive" void test_merge_trees_recursive__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_recursive__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_recursive__one_base_commit(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "dea7215f259b2cced87d1bda6c72f8b4ce37a2ff", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchA-1", "branchA-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__one_base_commit_norecursive(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "dea7215f259b2cced87d1bda6c72f8b4ce37a2ff", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" }, }; opts.flags |= GIT_MERGE_NO_RECURSIVE; cl_git_pass(merge_commits_from_branches(&index, repo, "branchA-1", "branchA-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__two_base_commits(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "666ffdfcf1eaa5641fa31064bf2607327e843c09", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchB-1", "branchB-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__two_base_commits_norecursive(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "cb49ad76147f5f9439cbd6133708b76142660660", 1, "veal.txt" }, { 0100644, "b2a81ead9e722af0099fccfb478cea88eea749a2", 2, "veal.txt" }, { 0100644, "4e21d2d63357bde5027d1625f5ec6b430cdeb143", 3, "veal.txt" }, }; opts.flags |= GIT_MERGE_NO_RECURSIVE; cl_git_pass(merge_commits_from_branches(&index, repo, "branchB-1", "branchB-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } void test_merge_trees_recursive__two_levels_of_multiple_bases(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "15faa0c9991f2d65686e844651faa2ff9827887b", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchC-1", "branchC-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__two_levels_of_multiple_bases_norecursive(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "b2a81ead9e722af0099fccfb478cea88eea749a2", 1, "veal.txt" }, { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 2, "veal.txt" }, { 0100644, "68a2e1ee61a23a4728fe6b35580fbbbf729df370", 3, "veal.txt" }, }; opts.flags |= GIT_MERGE_NO_RECURSIVE; cl_git_pass(merge_commits_from_branches(&index, repo, "branchC-1", "branchC-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } void test_merge_trees_recursive__three_levels_of_multiple_bases(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "d55e5dc038c52f1a36548625bcb666cbc06db9e6", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchD-2", "branchD-1", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__three_levels_of_multiple_bases_norecursive(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 1, "veal.txt" }, { 0100644, "f1b44c04989a3a1c14b036cfadfa328d53a7bc5e", 2, "veal.txt" }, { 0100644, "5e8747f5200fac0f945a07daf6163ca9cb1a8da9", 3, "veal.txt" }, }; opts.flags |= GIT_MERGE_NO_RECURSIVE; cl_git_pass(merge_commits_from_branches(&index, repo, "branchD-2", "branchD-1", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } void test_merge_trees_recursive__three_base_commits(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4f7269b07c76d02755d75ccaf05c0b4c36cdc6c", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchE-1", "branchE-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } void test_merge_trees_recursive__three_base_commits_norecursive(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "9e12bce04446d097ae1782967a5888c2e2a0d35b", 1, "gravy.txt" }, { 0100644, "d8dd349b78f19a4ebe3357bacb8138f00bf5ed41", 2, "gravy.txt" }, { 0100644, "e50fbbd701458757bdfe9815f58ed717c588d1b5", 3, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" }, }; opts.flags |= GIT_MERGE_NO_RECURSIVE; cl_git_pass(merge_commits_from_branches(&index, repo, "branchE-1", "branchE-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } void test_merge_trees_recursive__conflict(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "fa567f568ed72157c0c617438d077695b99d9aac", 1, "veal.txt" }, { 0100644, "21950d5e4e4d1a871b4dfcf72ecb6b9c162c434e", 2, "veal.txt" }, { 0100644, "3855170cef875708da06ab9ad7fc6a73b531cda1", 3, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchF-1", "branchF-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } /* * Branch G-1 and G-2 have three common ancestors (815b5a1, ad2ace9, 483065d). * The merge-base of the first two has two common ancestors (723181f, a34e5a1) * which themselves have two common ancestors (8f35f30, 3a3f5a6), which * finally has a common ancestor of 7c7bf85. This virtual merge base will * be computed and merged with 483065d which also has a common ancestor of * 7c7bf85. */ void test_merge_trees_recursive__oh_so_many_levels_of_recursion(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "7c7e08f9559d9e1551b91e1cf68f1d0066109add", 0, "oyster.txt" }, { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchG-1", "branchG-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } /* Branch H-1 and H-2 have two common ancestors (aa9e263, 6ef31d3). The two * ancestors themselves conflict. */ void test_merge_trees_recursive__conflicting_merge_base(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "cfc01b0976122eae42a82064440bbf534eddd7a0", 1, "veal.txt" }, { 0100644, "d604c75019c282144bdbbf3fd3462ba74b240efc", 2, "veal.txt" }, { 0100644, "37a5054a9f9b4628e3924c5cb8f2147c6e2a3efc", 3, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchH-1", "branchH-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } /* Branch H-1 and H-2 have two common ancestors (aa9e263, 6ef31d3). The two * ancestors themselves conflict. The generated common ancestor file will * have diff3 style conflicts inside it. */ void test_merge_trees_recursive__conflicting_merge_base_with_diff3(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "0b01d2f70a1c6b9ab60c382f3f9cdc8173da6736", 1, "veal.txt" }, { 0100644, "37a5054a9f9b4628e3924c5cb8f2147c6e2a3efc", 2, "veal.txt" }, { 0100644, "d604c75019c282144bdbbf3fd3462ba74b240efc", 3, "veal.txt" }, }; opts.file_flags |= GIT_MERGE_FILE_STYLE_DIFF3; cl_git_pass(merge_commits_from_branches(&index, repo, "branchH-2", "branchH-1", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } /* Branch I-1 and I-2 have two common ancestors (aa9e263, 6ef31d3). The two * ancestors themselves conflict, but when each was merged, the conflicts were * resolved identically, thus merging I-1 into I-2 does not conflict. */ void test_merge_trees_recursive__conflicting_merge_base_since_resolved(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "a02d4fd126e0cc8fb46ee48cf38bad36d44f2dbc", 0, "veal.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchI-1", "branchI-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); } /* There are multiple levels of criss-cross merges, and multiple recursive * merges would create a common ancestor that allows the merge to complete * successfully. Test that we can build a single virtual base, then stop, * which will produce a conflicting merge. */ void test_merge_trees_recursive__recursionlimit(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "53217e8ac3f52bccf7603b8fff0ed0f4817f9bb7", 1, "veal.txt" }, { 0100644, "898d12687fb35be271c27c795a6b32c8b51da79e", 2, "veal.txt" }, { 0100644, "68a2e1ee61a23a4728fe6b35580fbbbf729df370", 3, "veal.txt" }, }; opts.recursion_limit = 1; cl_git_pass(merge_commits_from_branches(&index, repo, "branchC-1", "branchC-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 8)); git_index_free(index); } /* There are multiple levels of criss-cross merges. This ensures * that the virtual merge base parents are compared in the same * order as git. If the base parents are created in the order as * git does, then the file `targetfile.txt` is automerged. If not, * `targetfile.txt` will be in conflict due to the virtual merge * base. */ void test_merge_trees_recursive__merge_base_for_virtual_commit(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "1bde1883de4977ea3e664b315da951d1f614c3b1", 0, "targetfile.txt" }, { 0100644, "b7de2b52ba055688061355fad1599a5d214ce8f8", 1, "version.txt" }, { 0100644, "358efd6f589384fa8baf92234db9c7899a53916e", 2, "version.txt" }, { 0100644, "a664873b1c0b9a1ed300f8644dde536fdaa3a34f", 3, "version.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchJ-1", "branchJ-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 4)); git_index_free(index); } /* This test is the same as above, but the graph is constructed such * that the 1st-recursion merge bases of the two heads are * in a different order. */ void test_merge_trees_recursive__merge_base_for_virtual_commit_2(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "4a06b258fed8a4d15967ec4253ae7366b70f727d", 0, "targetfile.txt" }, { 0100644, "b6bd0f9952f396e757d3f91e08c59a7e91707201", 1, "version.txt" }, { 0100644, "f0856993e005c0d8ed2dc7cdc222cc1d89fb3c77", 2, "version.txt" }, { 0100644, "2cba583804a4a6fad1baf97c959be447238d1489", 3, "version.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "branchK-1", "branchK-2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 4)); git_index_free(index); }
libgit2-main
tests/libgit2/merge/trees/recursive.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "../conflict_data.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" void test_merge_trees_commits__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_commits__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_commits__automerge(void) { git_index *index; const git_index_entry *entry; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; git_blob *blob; struct merge_index_entry merge_index_entries[] = { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, { 0100644, "f2e1550a0c9e53d5811175864a29536642ae3821", 0, "automergeable.txt" }, { 0100644, "4eb04c9e79e88f6640d01ff5b25ca2a60764f216", 0, "changed-in-branch.txt" }, { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 1, "conflicting.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 3, "conflicting.txt" }, { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, }; struct merge_reuc_entry merge_reuc_entries[] = { { "automergeable.txt", 0100644, 0100644, 0100644, \ "6212c31dab5e482247d7977e4f0dd3601decf13b", \ "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", \ "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe" }, { "removed-in-branch.txt", 0100644, 0100644, 0, \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "" }, { "removed-in-master.txt", 0100644, 0, 0100644, \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", \ "", \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "master", "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->id, GIT_OBJECT_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); } void test_merge_trees_commits__no_ancestor(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 2, "automergeable.txt" }, { 0100644, "d07ec190c306ec690bac349e87d01c4358e49bb2", 3, "automergeable.txt" }, { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "4b253da36a0ae8bfce63aeabd8c5b58429925594", 3, "conflicting.txt" }, { 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, "new-in-unrelated1.txt" }, { 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, "new-in-unrelated2.txt" }, { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, }; cl_git_pass(merge_commits_from_branches(&index, repo, "master", "unrelated", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 11)); git_index_free(index); } void test_merge_trees_commits__df_conflict(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "49130a28ef567af9a6a6104c38773fedfa5f9742", 2, "dir-10" }, { 0100644, "6c06dcd163587c2cc18be44857e0b71116382aeb", 3, "dir-10" }, { 0100644, "43aafd43bea779ec74317dc361f45ae3f532a505", 0, "dir-6" }, { 0100644, "a031a28ae70e33a641ce4b8a8f6317f1ab79dee4", 3, "dir-7" }, { 0100644, "5012fd565b1393bdfda1805d4ec38ce6619e1fd1", 1, "dir-7/file.txt" }, { 0100644, "a5563304ddf6caba25cb50323a2ea6f7dbfcadca", 2, "dir-7/file.txt" }, { 0100644, "e9ad6ec3e38364a3d07feda7c4197d4d845c53b5", 0, "dir-8" }, { 0100644, "3ef4d30382ca33fdeba9fda895a99e0891ba37aa", 2, "dir-9" }, { 0100644, "fc4c636d6515e9e261f9260dbcf3cc6eca97ea08", 1, "dir-9/file.txt" }, { 0100644, "76ab0e2868197ec158ddd6c78d8a0d2fd73d38f9", 3, "dir-9/file.txt" }, { 0100644, "5c2411f8075f48a6b2fdb85ebc0d371747c4df15", 0, "file-1/new" }, { 0100644, "a39a620dae5bc8b4e771cd4d251b7d080401a21e", 1, "file-2" }, { 0100644, "d963979c237d08b6ba39062ee7bf64c7d34a27f8", 2, "file-2" }, { 0100644, "5c341ead2ba6f2af98ce5ec3fe84f6b6d2899c0d", 0, "file-2/new" }, { 0100644, "9efe7723802d4305142eee177e018fee1572c4f4", 0, "file-3/new" }, { 0100644, "bacac9b3493509aa15e1730e1545fc0919d1dae0", 1, "file-4" }, { 0100644, "7663fce0130db092936b137cabd693ec234eb060", 3, "file-4" }, { 0100644, "e49f917b448d1340b31d76e54ba388268fd4c922", 0, "file-4/new" }, { 0100644, "cab2cf23998b40f1af2d9d9a756dc9e285a8df4b", 2, "file-5/new" }, { 0100644, "f5504f36e6f4eb797a56fc5bac6c6c7f32969bf2", 3, "file-5/new" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, "df_side1", "df_side2", &opts)); cl_assert(merge_test_index(index, merge_index_entries, 20)); git_index_free(index); } void test_merge_trees_commits__fail_on_conflict(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; opts.flags |= GIT_MERGE_FAIL_ON_CONFLICT; cl_git_fail_with(GIT_EMERGECONFLICT, merge_trees_from_branches(&index, repo, "df_side1", "df_side2", &opts)); cl_git_fail_with(GIT_EMERGECONFLICT, merge_commits_from_branches(&index, repo, "master", "unrelated", &opts)); cl_git_fail_with(GIT_EMERGECONFLICT, merge_commits_from_branches(&index, repo, "master", "branch", &opts)); }
libgit2-main
tests/libgit2/merge/trees/commits.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "futils.h" #include "../merge_helpers.h" #include "../conflict_data.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" #define THEIRS_AUTOMERGE_BRANCH "branch" #define THEIRS_UNRELATED_BRANCH "unrelated" #define THEIRS_UNRELATED_OID "55b4e4687e7a0d9ca367016ed930f385d4022e6f" #define THEIRS_UNRELATED_PARENT "d6cf6c7741b3316826af1314042550c97ded1d50" #define OURS_DIRECTORY_FILE "df_side1" #define THEIRS_DIRECTORY_FILE "df_side2" /* Non-conflicting files, index entries are common to every merge operation */ #define ADDED_IN_MASTER_INDEX_ENTRY \ { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" } #define AUTOMERGEABLE_INDEX_ENTRY \ { 0100644, "f2e1550a0c9e53d5811175864a29536642ae3821", 0, "automergeable.txt" } #define CHANGED_IN_BRANCH_INDEX_ENTRY \ { 0100644, "4eb04c9e79e88f6640d01ff5b25ca2a60764f216", 0, "changed-in-branch.txt" } #define CHANGED_IN_MASTER_INDEX_ENTRY \ { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" } #define UNCHANGED_INDEX_ENTRY \ { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" } /* Expected REUC entries */ #define AUTOMERGEABLE_REUC_ENTRY \ { "automergeable.txt", 0100644, 0100644, 0100644, \ "6212c31dab5e482247d7977e4f0dd3601decf13b", \ "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", \ "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe" } #define CONFLICTING_REUC_ENTRY \ { "conflicting.txt", 0100644, 0100644, 0100644, \ "d427e0b2e138501a3d15cc376077a3631e15bd46", \ "4e886e602529caa9ab11d71f86634bd1b6e0de10", \ "2bd0a343aeef7a2cf0d158478966a6e587ff3863" } #define REMOVED_IN_BRANCH_REUC_ENTRY \ { "removed-in-branch.txt", 0100644, 0100644, 0, \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "" } #define REMOVED_IN_MASTER_REUC_ENTRY \ { "removed-in-master.txt", 0100644, 0, 0100644, \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", \ "", \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5" } /* Fixture setup and teardown */ void test_merge_trees_automerge__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_automerge__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_automerge__automerge(void) { git_index *index; const git_index_entry *entry; git_merge_options opts = GIT_MERGE_OPTIONS_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->id, GIT_OBJECT_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); } void test_merge_trees_automerge__favor_ours(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; 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, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY, }; opts.file_favor = GIT_MERGE_FILE_FAVOR_OURS; cl_git_pass(merge_trees_from_branches(&index, repo, "master", THEIRS_AUTOMERGE_BRANCH, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); cl_assert(merge_test_reuc(index, merge_reuc_entries, 4)); git_index_free(index); } void test_merge_trees_automerge__favor_theirs(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; 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, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY, }; opts.file_favor = GIT_MERGE_FILE_FAVOR_THEIRS; cl_git_pass(merge_trees_from_branches(&index, repo, "master", THEIRS_AUTOMERGE_BRANCH, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 6)); cl_assert(merge_test_reuc(index, merge_reuc_entries, 4)); git_index_free(index); } void test_merge_trees_automerge__unrelated(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 2, "automergeable.txt" }, { 0100644, "d07ec190c306ec690bac349e87d01c4358e49bb2", 3, "automergeable.txt" }, { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "4b253da36a0ae8bfce63aeabd8c5b58429925594", 3, "conflicting.txt" }, { 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, "new-in-unrelated1.txt" }, { 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, "new-in-unrelated2.txt" }, { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, "master", THEIRS_UNRELATED_BRANCH, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 11)); git_index_free(index); }
libgit2-main
tests/libgit2/merge/trees/automerge.c
#include "clar_libgit2.h" #include "git2/tree.h" #include "merge.h" #include "../merge_helpers.h" #include "diff.h" #include "diff_tform.h" #include "git2/sys/hashsig.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define TREE_OID_ANCESTOR "0d52e3a556e189ba0948ae56780918011c1b167d" #define TREE_OID_MASTER "1f81433e3161efbf250576c58fede7f6b836f3d3" #define TREE_OID_BRANCH "eea9286df54245fea72c5b557291470eb825f38f" #define TREE_OID_RENAMES1 "f5f9dd5886a6ee20272be0aafc790cba43b31931" #define TREE_OID_RENAMES2 "5fbfbdc04b4eca46f54f4853a3c5a1dce28f5165" #define TREE_OID_DF_ANCESTOR "b8a3a806d3950e8c0a03a34f234a92eff0e2c68d" #define TREE_OID_DF_SIDE1 "ee1d6f164893c1866a323f072eeed36b855656be" #define TREE_OID_DF_SIDE2 "6178885b38fe96e825ac0f492c0a941f288b37f6" #define TREE_OID_RENAME_CONFLICT_ANCESTOR "476dbb3e207313d1d8aaa120c6ad204bf1295e53" #define TREE_OID_RENAME_CONFLICT_OURS "c4efe31e9decccc8b2b4d3df9aac2cdfe2995618" #define TREE_OID_RENAME_CONFLICT_THEIRS "9e7f4359c469f309b6057febf4c6e80742cbed5b" void test_merge_trees_treediff__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_treediff__cleanup(void) { cl_git_sandbox_cleanup(); } static void test_find_differences( const char *ancestor_oidstr, const char *ours_oidstr, const char *theirs_oidstr, struct merge_index_conflict_data *treediff_conflict_data, size_t treediff_conflict_data_len) { git_merge_diff_list *merge_diff_list = git_merge_diff_list__alloc(repo); git_oid ancestor_oid, ours_oid, theirs_oid; git_tree *ancestor_tree, *ours_tree, *theirs_tree; git_iterator *ancestor_iter, *ours_iter, *theirs_iter; git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; opts.flags |= GIT_MERGE_FIND_RENAMES; opts.target_limit = 1000; opts.rename_threshold = 50; opts.metric = git__malloc(sizeof(git_diff_similarity_metric)); cl_assert(opts.metric != NULL); opts.metric->file_signature = git_diff_find_similar__hashsig_for_file; opts.metric->buffer_signature = git_diff_find_similar__hashsig_for_buf; opts.metric->free_signature = git_diff_find_similar__hashsig_free; opts.metric->similarity = git_diff_find_similar__calc_similarity; opts.metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE; cl_git_pass(git_oid__fromstr(&ancestor_oid, ancestor_oidstr, GIT_OID_SHA1)); cl_git_pass(git_oid__fromstr(&ours_oid, ours_oidstr, GIT_OID_SHA1)); cl_git_pass(git_oid__fromstr(&theirs_oid, theirs_oidstr, GIT_OID_SHA1)); cl_git_pass(git_tree_lookup(&ancestor_tree, repo, &ancestor_oid)); cl_git_pass(git_tree_lookup(&ours_tree, repo, &ours_oid)); cl_git_pass(git_tree_lookup(&theirs_tree, repo, &theirs_oid)); iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE; cl_git_pass(git_iterator_for_tree(&ancestor_iter, ancestor_tree, &iter_opts)); cl_git_pass(git_iterator_for_tree(&ours_iter, ours_tree, &iter_opts)); cl_git_pass(git_iterator_for_tree(&theirs_iter, theirs_tree, &iter_opts)); cl_git_pass(git_merge_diff_list__find_differences(merge_diff_list, ancestor_iter, ours_iter, theirs_iter)); cl_git_pass(git_merge_diff_list__find_renames(repo, merge_diff_list, &opts)); /* dump_merge_index(merge_index); */ cl_assert(treediff_conflict_data_len == merge_diff_list->conflicts.length); cl_assert(merge_test_merge_conflicts(&merge_diff_list->conflicts, treediff_conflict_data, treediff_conflict_data_len)); git_iterator_free(ancestor_iter); git_iterator_free(ours_iter); git_iterator_free(theirs_iter); git_tree_free(ancestor_tree); git_tree_free(ours_tree); git_tree_free(theirs_tree); git_merge_diff_list__free(merge_diff_list); git__free(opts.metric); } void test_merge_trees_treediff__simple(void) { struct merge_index_conflict_data treediff_conflict_data[] = { { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE }, { { { 0100644, "6212c31dab5e482247d7977e4f0dd3601decf13b", 0, "automergeable.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 0, "automergeable.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe", 0, "automergeable.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_BOTH_MODIFIED }, { { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "4eb04c9e79e88f6640d01ff5b25ca2a60764f216", 0, "changed-in-branch.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_NONE }, { { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE }, { { { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 0, "conflicting.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 0, "conflicting.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_BOTH_MODIFIED }, { { { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_NONE }, { { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE }, }; test_find_differences(TREE_OID_ANCESTOR, TREE_OID_MASTER, TREE_OID_BRANCH, treediff_conflict_data, 7); } void test_merge_trees_treediff__df_conflicts(void) { struct merge_index_conflict_data treediff_conflict_data[] = { { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "49130a28ef567af9a6a6104c38773fedfa5f9742", 0, "dir-10" }, GIT_DELTA_ADDED }, { { 0100644, "6c06dcd163587c2cc18be44857e0b71116382aeb", 0, "dir-10" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_BOTH_ADDED, }, { { { 0100644, "242591eb280ee9eeb2ce63524b9a8b9bc4cb515d", 0, "dir-10/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_BOTH_DELETED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "43aafd43bea779ec74317dc361f45ae3f532a505", 0, "dir-6" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "cf8c5cc8a85a1ff5a4ba51e0bc7cf5665669924d", 0, "dir-6/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "cf8c5cc8a85a1ff5a4ba51e0bc7cf5665669924d", 0, "dir-6/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "a031a28ae70e33a641ce4b8a8f6317f1ab79dee4", 0, "dir-7" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_DIRECTORY_FILE, }, { { { 0100644, "5012fd565b1393bdfda1805d4ec38ce6619e1fd1", 0, "dir-7/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "a5563304ddf6caba25cb50323a2ea6f7dbfcadca", 0, "dir-7/file.txt" }, GIT_DELTA_MODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_DF_CHILD, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "e9ad6ec3e38364a3d07feda7c4197d4d845c53b5", 0, "dir-8" }, GIT_DELTA_ADDED }, { {0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "f20c9063fa0bda9a397c96947a7b687305c49753", 0, "dir-8/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "f20c9063fa0bda9a397c96947a7b687305c49753", 0, "dir-8/file.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "3ef4d30382ca33fdeba9fda895a99e0891ba37aa", 0, "dir-9" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_DIRECTORY_FILE, }, { { { 0100644, "fc4c636d6515e9e261f9260dbcf3cc6eca97ea08", 0, "dir-9/file.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "76ab0e2868197ec158ddd6c78d8a0d2fd73d38f9", 0, "dir-9/file.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_DF_CHILD, }, { { { 0100644, "1e4ff029aee68d0d69ef9eb6efa6cbf1ec732f99", 0, "file-1" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "1e4ff029aee68d0d69ef9eb6efa6cbf1ec732f99", 0, "file-1" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "5c2411f8075f48a6b2fdb85ebc0d371747c4df15", 0, "file-1/new" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "a39a620dae5bc8b4e771cd4d251b7d080401a21e", 0, "file-2" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "d963979c237d08b6ba39062ee7bf64c7d34a27f8", 0, "file-2" }, GIT_DELTA_MODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_DIRECTORY_FILE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "5c341ead2ba6f2af98ce5ec3fe84f6b6d2899c0d", 0, "file-2/new" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_DF_CHILD, }, { { { 0100644, "032ebc5ab85d9553bb187d3cd40875ff23a63ed0", 0, "file-3" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "032ebc5ab85d9553bb187d3cd40875ff23a63ed0", 0, "file-3" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "9efe7723802d4305142eee177e018fee1572c4f4", 0, "file-3/new" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "bacac9b3493509aa15e1730e1545fc0919d1dae0", 0, "file-4" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "7663fce0130db092936b137cabd693ec234eb060", 0, "file-4" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_DIRECTORY_FILE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "e49f917b448d1340b31d76e54ba388268fd4c922", 0, "file-4/new" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_DF_CHILD, }, { { { 0100644, "ac4045f965119e6998f4340ed0f411decfb3ec05", 0, "file-5" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_BOTH_DELETED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "cab2cf23998b40f1af2d9d9a756dc9e285a8df4b", 0, "file-5/new" }, GIT_DELTA_ADDED }, { { 0100644, "f5504f36e6f4eb797a56fc5bac6c6c7f32969bf2", 0, "file-5/new" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_BOTH_ADDED, }, }; test_find_differences(TREE_OID_DF_ANCESTOR, TREE_OID_DF_SIDE1, TREE_OID_DF_SIDE2, treediff_conflict_data, 20); } void test_merge_trees_treediff__strict_renames(void) { struct merge_index_conflict_data treediff_conflict_data[] = { { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "6212c31dab5e482247d7977e4f0dd3601decf13b", 0, "automergeable.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 0, "automergeable.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "6212c31dab5e482247d7977e4f0dd3601decf13b", 0, "automergeable.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 0, "conflicting.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 0, "conflicting.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "renamed-in-branch.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "renamed.txt" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "copied.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_NONE, }, }; test_find_differences(TREE_OID_ANCESTOR, TREE_OID_MASTER, TREE_OID_RENAMES1, treediff_conflict_data, 8); } void test_merge_trees_treediff__rename_conflicts(void) { struct merge_index_conflict_data treediff_conflict_data[] = { { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-rewritten-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 0, "0b-rewritten-in-ours.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 0, "0b-rewritten-in-ours.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_BOTH_MODIFIED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-rewritten-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 0, "0c-rewritten-in-theirs.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 0, "0c-rewritten-in-theirs.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_BOTH_MODIFIED, }, { { { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 0, "1a-renamed-in-ours-edited-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, GIT_DELTA_RENAMED }, { { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-renamed-in-ours-edited-in-theirs.txt" }, GIT_DELTA_MODIFIED }, GIT_MERGE_DIFF_RENAMED_MODIFIED, }, { { { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-renamed-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, GIT_DELTA_RENAMED }, { { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-renamed-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 0, "1b-renamed-in-theirs-edited-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-renamed-in-theirs-edited-in-ours.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_RENAMED_MODIFIED, }, { { { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-renamed-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-renamed-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-renamed-in-both.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, GIT_DELTA_RENAMED }, { { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_BOTH_RENAMED, }, { { { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-renamed-in-ours-deleted-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, GIT_DELTA_RENAMED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_RENAMED_DELETED, }, { { { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-renamed-in-theirs-deleted-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_RENAMED_DELETED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 0, "4a-newname-in-ours-added-in-theirs.txt" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_RENAMED_ADDED, }, { { { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-renamed-in-ours-added-in-theirs.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-newname-in-ours-added-in-theirs.txt" }, GIT_DELTA_RENAMED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, GIT_MERGE_DIFF_RENAMED_ADDED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 0, "4b-newname-in-theirs-added-in-ours.txt" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_RENAMED_ADDED, }, { { { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 0, "4b-renamed-in-theirs-added-in-ours.txt" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 0, "4b-newname-in-theirs-added-in-ours.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_RENAMED_ADDED, }, { { { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "5-both-renamed-1-to-2.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "5-both-renamed-1-to-2-ours.txt" }, GIT_DELTA_RENAMED }, { { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "5-both-renamed-1-to-2-theirs.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2, }, { { { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "6-both-renamed-side-1.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "6-both-renamed.txt" }, GIT_DELTA_RENAMED }, { { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "6-both-renamed-side-1.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1, }, { { { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "6-both-renamed-side-2.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "6-both-renamed-side-2.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "6-both-renamed.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1, }, }; test_find_differences(TREE_OID_RENAME_CONFLICT_ANCESTOR, TREE_OID_RENAME_CONFLICT_OURS, TREE_OID_RENAME_CONFLICT_THEIRS, treediff_conflict_data, 18); } void test_merge_trees_treediff__best_renames(void) { struct merge_index_conflict_data treediff_conflict_data[] = { { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, GIT_DELTA_ADDED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "6212c31dab5e482247d7977e4f0dd3601decf13b", 0, "automergeable.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 0, "automergeable.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "45299c1ca5e07bba1fd90843056fb559f96b1f5a", 0, "renamed-90.txt" }, GIT_DELTA_RENAMED }, GIT_MERGE_DIFF_RENAMED_MODIFIED, }, { { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 0, "conflicting.txt" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, GIT_DELTA_MODIFIED }, { { 0100644, "d427e0b2e138501a3d15cc376077a3631e15bd46", 0, "conflicting.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_NONE, }, { { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" },GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_DELETED }, { { 0100644, "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", 0, "removed-in-master.txt" }, GIT_DELTA_UNMODIFIED }, GIT_MERGE_DIFF_MODIFIED_DELETED, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "5843febcb23480df0b5edb22a21c59c772bb8e29", 0, "renamed-50.txt" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, { { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0, "", 0, "" }, GIT_DELTA_UNMODIFIED }, { { 0100644, "a77a56a49f8f3ae242e02717f18ebbc60c5cc543", 0, "renamed-75.txt" }, GIT_DELTA_ADDED }, GIT_MERGE_DIFF_NONE, }, }; test_find_differences(TREE_OID_ANCESTOR, TREE_OID_MASTER, TREE_OID_RENAMES2, treediff_conflict_data, 7); }
libgit2-main
tests/libgit2/merge/trees/treediff.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "futils.h" static git_repository *repo; #define TEST_REPO_PATH "merge-whitespace" #define BRANCH_A_EOL "branch_a_eol" #define BRANCH_B_EOL "branch_b_eol" #define BRANCH_A_CHANGE "branch_a_change" #define BRANCH_B_CHANGE "branch_b_change" /* Fixture setup and teardown */ void test_merge_trees_whitespace__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_whitespace__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_whitespace__conflict(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "4026a6c83f39c56881c9ac62e7582db9e3d33a4f", 1, "test.txt" }, { 0100644, "c3b1fb31424c98072542cc8e42b48c92e52f494a", 2, "test.txt" }, { 0100644, "262f67de0de2e535a59ae1bc3c739601e98c354d", 3, "test.txt" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_A_EOL, BRANCH_B_EOL, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 3)); git_index_free(index); } void test_merge_trees_whitespace__eol(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ee3c2aac8e03224c323b58ecb1f9eef616745467", 0, "test.txt" }, }; opts.file_flags |= GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL; cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_A_EOL, BRANCH_B_EOL, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 1)); git_index_free(index); } void test_merge_trees_whitespace__change(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "a827eab4fd66ab37a6ebcfaa7b7e341abfd55947", 0, "test.txt" }, }; opts.file_flags |= GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE; cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_A_CHANGE, BRANCH_B_CHANGE, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 1)); git_index_free(index); }
libgit2-main
tests/libgit2/merge/trees/whitespace.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "futils.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define BRANCH_RENAME_OURS "rename_conflict_ours" #define BRANCH_RENAME_THEIRS "rename_conflict_theirs" /* Fixture setup and teardown */ void test_merge_trees_renames__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_trees_renames__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_trees_renames__index(void) { git_index *index; git_merge_options *opts = NULL; struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" }, { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" }, { 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" }, { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" }, { 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 2, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 1, "3a-renamed-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 3, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 1, "3b-renamed-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 1, "4a-renamed-in-ours-added-in-theirs.txt" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 1, "4b-renamed-in-theirs-added-in-ours.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 1, "5a-renamed-in-ours-added-in-theirs.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 3, "5a-renamed-in-ours-added-in-theirs.txt" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 1, "5b-renamed-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 2, "5b-renamed-in-theirs-added-in-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 2, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 3, "6-both-renamed-1-to-2-theirs.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 1, "6-both-renamed-1-to-2.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 1, "7-both-renamed-side-1.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 3, "7-both-renamed-side-1.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 1, "7-both-renamed-side-2.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 2, "7-both-renamed-side-2.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" }, }; struct merge_name_entry merge_name_entries[] = { { "3a-renamed-in-ours-deleted-in-theirs.txt", "3a-newname-in-ours-deleted-in-theirs.txt", "" }, { "3b-renamed-in-theirs-deleted-in-ours.txt", "", "3b-newname-in-theirs-deleted-in-ours.txt", }, { "4a-renamed-in-ours-added-in-theirs.txt", "4a-newname-in-ours-added-in-theirs.txt", "", }, { "4b-renamed-in-theirs-added-in-ours.txt", "", "4b-newname-in-theirs-added-in-ours.txt", }, { "5a-renamed-in-ours-added-in-theirs.txt", "5a-newname-in-ours-added-in-theirs.txt", "5a-renamed-in-ours-added-in-theirs.txt", }, { "5b-renamed-in-theirs-added-in-ours.txt", "5b-renamed-in-theirs-added-in-ours.txt", "5b-newname-in-theirs-added-in-ours.txt", }, { "6-both-renamed-1-to-2.txt", "6-both-renamed-1-to-2-ours.txt", "6-both-renamed-1-to-2-theirs.txt", }, { "7-both-renamed-side-1.txt", "7-both-renamed.txt", "7-both-renamed-side-1.txt", }, { "7-both-renamed-side-2.txt", "7-both-renamed-side-2.txt", "7-both-renamed.txt", }, }; struct merge_reuc_entry merge_reuc_entries[] = { { "1a-newname-in-ours-edited-in-theirs.txt", 0, 0100644, 0, "", "c3d02eeef75183df7584d8d13ac03053910c1301", "" }, { "1a-newname-in-ours.txt", 0, 0100644, 0, "", "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", "" }, { "1a-renamed-in-ours-edited-in-theirs.txt", 0100644, 0, 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", "", "0d872f8e871a30208305978ecbf9e66d864f1638" }, { "1a-renamed-in-ours.txt", 0100644, 0, 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", "", "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb" }, { "1b-newname-in-theirs-edited-in-ours.txt", 0, 0, 0100644, "", "", "241a1005cd9b980732741b74385b891142bcba28" }, { "1b-newname-in-theirs.txt", 0, 0, 0100644, "", "", "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136" }, { "1b-renamed-in-theirs-edited-in-ours.txt", 0100644, 0100644, 0, "241a1005cd9b980732741b74385b891142bcba28", "ed9523e62e453e50dd9be1606af19399b96e397a", "" }, { "1b-renamed-in-theirs.txt", 0100644, 0100644, 0, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", "" }, { "2-newname-in-both.txt", 0, 0100644, 0100644, "", "178940b450f238a56c0d75b7955cb57b38191982", "178940b450f238a56c0d75b7955cb57b38191982" }, { "2-renamed-in-both.txt", 0100644, 0, 0, "178940b450f238a56c0d75b7955cb57b38191982", "", "" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_RENAME_OURS, BRANCH_RENAME_THEIRS, opts)); cl_assert(merge_test_index(index, merge_index_entries, 41)); cl_assert(merge_test_names(index, merge_name_entries, 9)); cl_assert(merge_test_reuc(index, merge_reuc_entries, 10)); git_index_free(index); } void test_merge_trees_renames__no_rename_index(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 1, "0b-rewritten-in-ours.txt" }, { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 2, "0b-rewritten-in-ours.txt" }, { 0100644, "b2d399ae15224e1d58066e3c8df70ce37de7a656", 3, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 1, "0c-rewritten-in-theirs.txt" }, { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 2, "0c-rewritten-in-theirs.txt" }, { 0100644, "712ebba6669ea847d9829e4f1059d6c830c8b531", 3, "0c-rewritten-in-theirs.txt" }, { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "c3d02eeef75183df7584d8d13ac03053910c1301", 1, "1a-renamed-in-ours-edited-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 3, "1a-renamed-in-ours-edited-in-theirs.txt" }, { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "241a1005cd9b980732741b74385b891142bcba28", 1, "1b-renamed-in-theirs-edited-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 2, "1b-renamed-in-theirs-edited-in-ours.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 2, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 3, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 2, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 3, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 2, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 3, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 2, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 3, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-theirs.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 2, "7-both-renamed.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 3, "7-both-renamed.txt" }, }; opts.flags &= ~GIT_MERGE_FIND_RENAMES; cl_git_pass(merge_trees_from_branches(&index, repo, BRANCH_RENAME_OURS, BRANCH_RENAME_THEIRS, &opts)); cl_assert(merge_test_index(index, merge_index_entries, 32)); git_index_free(index); } void test_merge_trees_renames__submodules(void) { git_index *index; git_merge_options *opts = NULL; struct merge_index_entry merge_index_entries[] = { { 0100644, "cd3e8d4aa06bdc781f264171030bc28f2b370fee", 0, ".gitmodules" }, { 0100644, "4dd1ef7569b18d92d93c0a35bb6b93049137b355", 1, "file.txt" }, { 0100644, "a2d8d1824c68541cca94ffb90f79291eba495921", 2, "file.txt" }, { 0100644, "63ec604d491161ddafdae4179843c26d54bd999a", 3, "file.txt" }, { 0160000, "0000000000000000000000000000000000000001", 1, "submodule1" }, { 0160000, "0000000000000000000000000000000000000002", 3, "submodule1" }, { 0160000, "0000000000000000000000000000000000000003", 0, "submodule2" }, }; cl_git_pass(merge_trees_from_branches(&index, repo, "submodule_rename1", "submodule_rename2", opts)); cl_assert(merge_test_index(index, merge_index_entries, 7)); git_index_free(index); } void test_merge_trees_renames__cache_recomputation(void) { git_oid blob, binary, ancestor_oid, theirs_oid, ours_oid; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; git_str path = GIT_STR_INIT; git_treebuilder *builder; git_tree *ancestor_tree, *their_tree, *our_tree; git_index *index; size_t blob_size; void *data; size_t i; cl_git_pass(git_oid__fromstr(&blob, "a2d8d1824c68541cca94ffb90f79291eba495921", GIT_OID_SHA1)); /* * Create a 50MB blob that consists of NUL bytes only. It is important * that this blob is of a special format, most importantly it cannot * contain more than four non-consecutive newlines or NUL bytes. This * is because of git_hashsig's inner workings where all files with less * than four "lines" are deemed to small. */ blob_size = 50 * 1024 * 1024; cl_assert(data = git__calloc(blob_size, 1)); cl_git_pass(git_blob_create_from_buffer(&binary, repo, data, blob_size)); /* * Create the common ancestor, which has 1000 dummy blobs and the binary * blob. The dummy blobs serve as potential rename targets for the * dummy blob. */ cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); for (i = 0; i < 1000; i++) { cl_git_pass(git_str_printf(&path, "%"PRIuZ".txt", i)); cl_git_pass(git_treebuilder_insert(NULL, builder, path.ptr, &blob, GIT_FILEMODE_BLOB)); git_str_clear(&path); } cl_git_pass(git_treebuilder_insert(NULL, builder, "original.bin", &binary, GIT_FILEMODE_BLOB)); cl_git_pass(git_treebuilder_write(&ancestor_oid, builder)); /* We now the binary blob in our tree. */ cl_git_pass(git_treebuilder_remove(builder, "original.bin")); cl_git_pass(git_treebuilder_insert(NULL, builder, "renamed.bin", &binary, GIT_FILEMODE_BLOB)); cl_git_pass(git_treebuilder_write(&ours_oid, builder)); git_treebuilder_free(builder); /* And move everything into a subdirectory in their tree. */ cl_git_pass(git_treebuilder_new(&builder, repo, NULL)); cl_git_pass(git_treebuilder_insert(NULL, builder, "subdir", &ancestor_oid, GIT_FILEMODE_TREE)); cl_git_pass(git_treebuilder_write(&theirs_oid, builder)); /* * Now merge ancestor, ours and theirs. As `git_hashsig` refuses to * create a hash signature for the 50MB binary file, we historically * didn't cache the hashsig computation for it. As a result, we now * started looking up the 50MB blob and scanning it at least 1000 * times, which takes a long time. * * The number of 1000 blobs is chosen in such a way that it's * noticeable when the bug creeps in again, as it takes around 12 * minutes on my machine to compute the following merge. */ opts.target_limit = 5000; cl_git_pass(git_tree_lookup(&ancestor_tree, repo, &ancestor_oid)); cl_git_pass(git_tree_lookup(&their_tree, repo, &theirs_oid)); cl_git_pass(git_tree_lookup(&our_tree, repo, &ours_oid)); cl_git_pass(git_merge_trees(&index, repo, ancestor_tree, our_tree, their_tree, &opts)); git_treebuilder_free(builder); git_str_dispose(&path); git_index_free(index); git_tree_free(ancestor_tree); git_tree_free(their_tree); git_tree_free(our_tree); git__free(data); }
libgit2-main
tests/libgit2/merge/trees/renames.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "git2/sys/index.h" #include "merge.h" #include "../merge_helpers.h" #include "refs.h" #include "futils.h" static git_repository *repo; static git_index *repo_index; #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" /* Fixture setup and teardown */ void test_merge_workdir_trivial__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); } void test_merge_workdir_trivial__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } static int merge_trivial(const char *ours, const char *theirs) { git_str branch_buf = GIT_STR_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; git_reference *our_ref, *their_ref; git_annotated_commit *their_heads[1]; checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE; git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours); cl_git_pass(git_reference_symbolic_create(&our_ref, repo, "HEAD", branch_buf.ptr, 1, NULL)); cl_git_pass(git_checkout_head(repo, &checkout_opts)); git_str_clear(&branch_buf); git_str_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs); cl_git_pass(git_reference_lookup(&their_ref, repo, branch_buf.ptr)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, their_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, NULL, NULL)); git_str_dispose(&branch_buf); git_reference_free(our_ref); git_reference_free(their_ref); git_annotated_commit_free(their_heads[0]); return 0; } static size_t merge_trivial_conflict_entrycount(void) { const git_index_entry *entry; size_t count = 0; size_t i; for (i = 0; i < git_index_entrycount(repo_index); i++) { cl_assert(entry = git_index_get_byindex(repo_index, i)); if (git_index_entry_is_conflict(entry)) count++; } return count; } /* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote */ void test_merge_workdir_trivial__2alt(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-2alt", "trivial-2alt-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "new-in-branch.txt", 0)); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head */ void test_merge_workdir_trivial__3alt(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-3alt", "trivial-3alt-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "new-in-3alt.txt", 0)); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 4: ancest:(empty)^, head:head, remote:remote = result:no merge */ void test_merge_workdir_trivial__4(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-4", "trivial-4-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "new-and-different.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 2); cl_assert(entry = git_index_get_bypath(repo_index, "new-and-different.txt", 2)); cl_assert(entry = git_index_get_bypath(repo_index, "new-and-different.txt", 3)); } /* 5ALT: ancest:*, head:head, remote:head = result:head */ void test_merge_workdir_trivial__5alt_1(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-5alt-1", "trivial-5alt-1-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "new-and-same.txt", 0)); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 5ALT: ancest:*, head:head, remote:head = result:head */ void test_merge_workdir_trivial__5alt_2(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-5alt-2", "trivial-5alt-2-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "modified-to-same.txt", 0)); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */ void test_merge_workdir_trivial__6(void) { const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial("trivial-6", "trivial-6-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-both.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 1); cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-both.txt")); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */ void test_merge_workdir_trivial__8(void) { const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial("trivial-8", "trivial-8-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-8.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 1); cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-8.txt")); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge */ void test_merge_workdir_trivial__7(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-7", "trivial-7-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 2); cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 1)); cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-7.txt", 3)); } /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */ void test_merge_workdir_trivial__10(void) { const git_index_entry *entry; const git_index_reuc_entry *reuc; cl_git_pass(merge_trivial("trivial-10", "trivial-10-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-10-branch.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 1); cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "removed-in-10-branch.txt")); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge */ void test_merge_workdir_trivial__9(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-9", "trivial-9-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 2); cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 1)); cl_assert(entry = git_index_get_bypath(repo_index, "removed-in-9-branch.txt", 2)); } /* 13: ancest:ancest+, head:head, remote:ancest = result:head */ void test_merge_workdir_trivial__13(void) { const git_index_entry *entry; git_oid expected_oid; cl_git_pass(merge_trivial("trivial-13", "trivial-13-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-13.txt", 0)); cl_git_pass(git_oid__fromstr(&expected_oid, "1cff9ec6a47a537380dedfdd17c9e76d74259a2b", GIT_OID_SHA1)); cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */ void test_merge_workdir_trivial__14(void) { const git_index_entry *entry; git_oid expected_oid; cl_git_pass(merge_trivial("trivial-14", "trivial-14-branch")); cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-14-branch.txt", 0)); cl_git_pass(git_oid__fromstr(&expected_oid, "26153a3ff3649b6c2bb652d3f06878c6e0a172f9", GIT_OID_SHA1)); cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 0); } /* 11: ancest:ancest+, head:head, remote:remote = result:no merge */ void test_merge_workdir_trivial__11(void) { const git_index_entry *entry; cl_git_pass(merge_trivial("trivial-11", "trivial-11-branch")); cl_assert((entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 0)) == NULL); cl_assert(git_index_reuc_entrycount(repo_index) == 0); cl_assert(merge_trivial_conflict_entrycount() == 3); cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 1)); cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 2)); cl_assert(entry = git_index_get_bypath(repo_index, "modified-in-both.txt", 3)); }
libgit2-main
tests/libgit2/merge/workdir/trivial.c
#include "clar_libgit2.h" #include "git2/merge.h" #include "merge.h" #include "index.h" #include "../merge_helpers.h" #include "posix.h" #define TEST_REPO_PATH "merge-resolve" #define MERGE_BRANCH_OID "7cb63eed597130ba4abb87b3e544b85021905520" #define AUTOMERGEABLE_MERGED_FILE \ "this file is changed in master\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is automergeable\n" \ "this file is changed in branch\n" #define CHANGED_IN_BRANCH_FILE \ "changed in branch\n" static git_repository *repo; static git_index *repo_index; static char *unaffected[][4] = { { "added-in-master.txt", NULL }, { "changed-in-master.txt", NULL }, { "unchanged.txt", NULL }, { "added-in-master.txt", "changed-in-master.txt", NULL }, { "added-in-master.txt", "unchanged.txt", NULL }, { "changed-in-master.txt", "unchanged.txt", NULL }, { "added-in-master.txt", "changed-in-master.txt", "unchanged.txt", NULL }, { "new_file.txt", NULL }, { "new_file.txt", "unchanged.txt", NULL }, { NULL }, }; static char *affected[][5] = { { "automergeable.txt", NULL }, { "changed-in-branch.txt", NULL }, { "conflicting.txt", NULL }, { "removed-in-branch.txt", NULL }, { "automergeable.txt", "changed-in-branch.txt", NULL }, { "automergeable.txt", "conflicting.txt", NULL }, { "automergeable.txt", "removed-in-branch.txt", NULL }, { "changed-in-branch.txt", "conflicting.txt", NULL }, { "changed-in-branch.txt", "removed-in-branch.txt", NULL }, { "conflicting.txt", "removed-in-branch.txt", NULL }, { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", NULL }, { "automergeable.txt", "changed-in-branch.txt", "removed-in-branch.txt", NULL }, { "automergeable.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, { "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, { "automergeable.txt", "changed-in-branch.txt", "conflicting.txt", "removed-in-branch.txt", NULL }, { NULL }, }; static char *result_contents[4][6] = { { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, NULL, NULL }, { "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL }, { "automergeable.txt", AUTOMERGEABLE_MERGED_FILE, "changed-in-branch.txt", CHANGED_IN_BRANCH_FILE, NULL, NULL }, { NULL } }; void test_merge_workdir_dirty__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); } void test_merge_workdir_dirty__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } static void set_core_autocrlf_to(git_repository *repo, bool value) { git_config *cfg; cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value)); git_config_free(cfg); } static int merge_branch(void) { git_oid their_oids[1]; git_annotated_commit *their_head; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; int error; cl_git_pass(git_oid__fromstr(&their_oids[0], MERGE_BRANCH_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_oids[0])); checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; error = git_merge(repo, (const git_annotated_commit **)&their_head, 1, &merge_opts, &checkout_opts); git_annotated_commit_free(their_head); return error; } static void write_files(char *files[]) { char *filename; git_str path = GIT_STR_INIT, content = GIT_STR_INIT; size_t i; for (i = 0, filename = files[i]; filename; filename = files[++i]) { git_str_clear(&path); git_str_clear(&content); git_str_printf(&path, "%s/%s", TEST_REPO_PATH, filename); git_str_printf(&content, "This is a dirty file in the working directory!\n\n" "It will not be staged! Its filename is %s.\n", filename); cl_git_mkfile(path.ptr, content.ptr); } git_str_dispose(&path); git_str_dispose(&content); } static void hack_index(char *files[]) { char *filename; struct stat statbuf; git_str path = GIT_STR_INIT; git_index_entry *entry; struct p_timeval times[2]; time_t now; size_t i; /* Update the index to suggest that checkout placed these files on * disk, keeping the object id but updating the cache, which will * emulate a Git implementation's different filter. * * We set the file's timestamp to before now to pretend that * it was an old checkout so we don't trigger the racy * protections would would check the content. */ now = time(NULL); times[0].tv_sec = now - 5; times[0].tv_usec = 0; times[1].tv_sec = now - 5; times[1].tv_usec = 0; for (i = 0, filename = files[i]; filename; filename = files[++i]) { git_str_clear(&path); cl_assert(entry = (git_index_entry *) git_index_get_bypath(repo_index, filename, 0)); cl_git_pass(git_str_printf(&path, "%s/%s", TEST_REPO_PATH, filename)); cl_git_pass(p_utimes(path.ptr, times)); cl_git_pass(p_stat(path.ptr, &statbuf)); entry->ctime.seconds = (int32_t)statbuf.st_ctime; entry->mtime.seconds = (int32_t)statbuf.st_mtime; #if defined(GIT_USE_NSEC) entry->ctime.nanoseconds = statbuf.st_ctime_nsec; entry->mtime.nanoseconds = statbuf.st_mtime_nsec; #else entry->ctime.nanoseconds = 0; entry->mtime.nanoseconds = 0; #endif entry->dev = statbuf.st_dev; entry->ino = statbuf.st_ino; entry->uid = statbuf.st_uid; entry->gid = statbuf.st_gid; entry->file_size = (uint32_t)statbuf.st_size; } git_str_dispose(&path); } static void stage_random_files(char *files[]) { char *filename; size_t i; write_files(files); for (i = 0, filename = files[i]; filename; filename = files[++i]) cl_git_pass(git_index_add_bypath(repo_index, filename)); } static void stage_content(char *content[]) { git_reference *head; git_object *head_object; git_str path = GIT_STR_INIT; char *filename, *text; size_t i; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL)); for (i = 0, filename = content[i], text = content[++i]; filename && text; filename = content[++i], text = content[++i]) { git_str_clear(&path); cl_git_pass(git_str_printf(&path, "%s/%s", TEST_REPO_PATH, filename)); cl_git_mkfile(path.ptr, text); cl_git_pass(git_index_add_bypath(repo_index, filename)); } git_object_free(head_object); git_reference_free(head); git_str_dispose(&path); } static int merge_dirty_files(char *dirty_files[]) { git_reference *head; git_object *head_object; int error; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL)); write_files(dirty_files); error = merge_branch(); git_object_free(head_object); git_reference_free(head); return error; } static int merge_differently_filtered_files(char *files[]) { git_reference *head; git_object *head_object; int error; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL)); /* Emulate checkout with a broken or misconfigured filter: modify some * files on-disk and then update the index with the updated file size * and time, as if some filter applied them. These files should not be * treated as dirty since we created them. * * (Make sure to update the index stamp to defeat racy-git protections * trying to sanity check the files in the index; those would rehash the * files, showing them as dirty, the exact mechanism we're trying to avoid.) */ write_files(files); hack_index(files); cl_git_pass(git_index_write(repo_index)); error = merge_branch(); git_object_free(head_object); git_reference_free(head); return error; } static int merge_staged_files(char *staged_files[]) { stage_random_files(staged_files); return merge_branch(); } void test_merge_workdir_dirty__unaffected_dirty_files_allowed(void) { char **files; size_t i; for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i]) cl_git_pass(merge_dirty_files(files)); } void test_merge_workdir_dirty__unstaged_deletes_maintained(void) { git_reference *head; git_object *head_object; cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, head_object, GIT_RESET_HARD, NULL)); cl_git_pass(p_unlink("merge-resolve/unchanged.txt")); cl_git_pass(merge_branch()); git_object_free(head_object); git_reference_free(head); } void test_merge_workdir_dirty__affected_dirty_files_disallowed(void) { char **files; size_t i; for (i = 0, files = affected[i]; files[0]; files = affected[++i]) cl_git_fail(merge_dirty_files(files)); } void test_merge_workdir_dirty__staged_files_in_index_disallowed(void) { char **files; size_t i; for (i = 0, files = unaffected[i]; files[0]; files = unaffected[++i]) cl_git_fail(merge_staged_files(files)); for (i = 0, files = affected[i]; files[0]; files = affected[++i]) cl_git_fail(merge_staged_files(files)); } void test_merge_workdir_dirty__identical_staged_files_allowed(void) { char **content; size_t i; set_core_autocrlf_to(repo, false); for (i = 0, content = result_contents[i]; content[0]; content = result_contents[++i]) { stage_content(content); cl_git_pass(git_index_write(repo_index)); cl_git_pass(merge_branch()); } } void test_merge_workdir_dirty__honors_cache(void) { char **files; size_t i; for (i = 0, files = affected[i]; files[0]; files = affected[++i]) cl_git_pass(merge_differently_filtered_files(files)); }
libgit2-main
tests/libgit2/merge/workdir/dirty.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define SUBMODULE_MAIN_BRANCH "submodules" #define SUBMODULE_OTHER_BRANCH "submodules-branch" #define SUBMODULE_OTHER2_BRANCH "submodules-branch2" #define SUBMODULE_DELETE_BRANCH "delete-submodule" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" /* Fixture setup and teardown */ void test_merge_workdir_submodules__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_workdir_submodules__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_workdir_submodules__automerge(void) { git_reference *our_ref, *their_ref; git_commit *our_commit; git_annotated_commit *their_head; git_index *index; struct merge_index_entry merge_index_entries[] = { { 0100644, "caff6b7d44973f53e3e0cf31d0d695188b19aec6", 0, ".gitmodules" }, { 0100644, "950a663a6a7b2609eed1ed1ba9f41eb1a3192a9f", 0, "file1.txt" }, { 0100644, "343e660b9cb4bee5f407c2e33fcb9df24d9407a4", 0, "file2.txt" }, { 0160000, "d3d806a4bef96889117fd7ebac0e3cb5ec152932", 1, "submodule" }, { 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 2, "submodule" }, { 0160000, "ae39c77c70cb6bad18bb471912460c4e1ba0f586", 3, "submodule" }, }; cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH)); cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref))); cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL)); cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(merge_test_index(index, merge_index_entries, 6)); git_index_free(index); git_annotated_commit_free(their_head); git_commit_free(our_commit); git_reference_free(their_ref); git_reference_free(our_ref); } void test_merge_workdir_submodules__take_changed(void) { git_reference *our_ref, *their_ref; git_commit *our_commit; git_annotated_commit *their_head; git_index *index; struct merge_index_entry merge_index_entries[] = { { 0100644, "caff6b7d44973f53e3e0cf31d0d695188b19aec6", 0, ".gitmodules" }, { 0100644, "b438ff23300b2e0f80b84a6f30140dfa91e71423", 0, "file1.txt" }, { 0100644, "f27fbafdfa6693f8f7a5128506fe3e338dbfcad2", 0, "file2.txt" }, { 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 0, "submodule" }, }; cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH)); cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref))); cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL)); cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(merge_test_index(index, merge_index_entries, 4)); git_index_free(index); git_annotated_commit_free(their_head); git_commit_free(our_commit); git_reference_free(their_ref); git_reference_free(our_ref); } void test_merge_workdir_submodules__update_delete_conflict(void) { git_reference *our_ref, *their_ref; git_commit *our_commit; git_annotated_commit *their_head; git_index *index; struct merge_index_entry merge_index_entries[] = { { 0100644, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 0, ".gitmodules" }, { 0100644, "5887a5e516c53bd58efb0f02ec6aa031b6fe9ad7", 0, "file1.txt" }, { 0100644, "4218670ab81cc219a9f94befb5c5dad90ec52648", 0, "file2.txt" }, { 0160000, "d3d806a4bef96889117fd7ebac0e3cb5ec152932", 1, "submodule"}, { 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 3, "submodule" }, }; cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_DELETE_BRANCH)); cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref))); cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL)); cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(merge_test_index(index, merge_index_entries, 5)); git_index_free(index); git_annotated_commit_free(their_head); git_commit_free(our_commit); git_reference_free(their_ref); git_reference_free(our_ref); }
libgit2-main
tests/libgit2/merge/workdir/submodules.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "refs.h" #include "futils.h" static git_repository *repo; static git_index *repo_index; #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" #define ORIG_HEAD "bd593285fc7fe4ca18ccdbabf027f5d689101452" #define THEIRS_SIMPLE_BRANCH "branch" #define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520" #define OCTO1_BRANCH "octo1" #define OCTO1_OID "16f825815cfd20a07a75c71554e82d8eede0b061" #define OCTO2_BRANCH "octo2" #define OCTO2_OID "158dc7bedb202f5b26502bf3574faa7f4238d56c" #define OCTO3_BRANCH "octo3" #define OCTO3_OID "50ce7d7d01217679e26c55939eef119e0c93e272" #define OCTO4_BRANCH "octo4" #define OCTO4_OID "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae" #define OCTO5_BRANCH "octo5" #define OCTO5_OID "e4f618a2c3ed0669308735727df5ebf2447f022f" /* Fixture setup and teardown */ void test_merge_workdir_setup__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); } void test_merge_workdir_setup__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } static bool test_file_contents(const char *filename, const char *expected) { git_str file_path_buf = GIT_STR_INIT, file_buf = GIT_STR_INIT; bool equals; git_str_joinpath(&file_path_buf, git_repository_path(repo), filename); cl_git_pass(git_futils_readbuffer(&file_buf, file_path_buf.ptr)); equals = (strcmp(file_buf.ptr, expected) == 0); git_str_dispose(&file_path_buf); git_str_dispose(&file_buf); return equals; } static void write_file_contents(const char *filename, const char *output) { git_str file_path_buf = GIT_STR_INIT; git_str_joinpath(&file_path_buf, git_repository_path(repo), filename); cl_git_rewritefile(file_path_buf.ptr, output); git_str_dispose(&file_path_buf); } /* git merge --no-ff octo1 */ void test_merge_workdir_setup__one_branch(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } /* git merge --no-ff 16f825815cfd20a07a75c71554e82d8eede0b061 */ void test_merge_workdir_setup__one_oid(void) { git_oid our_oid; git_oid octo1_oid; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } /* git merge octo1 octo2 */ void test_merge_workdir_setup__two_branches(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git merge octo1 octo2 octo3 */ void test_merge_workdir_setup__three_branches(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_reference *octo3_ref; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_reference_free(octo3_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 158dc7bedb202f5b26502bf3574faa7f4238d56c 50ce7d7d01217679e26c55939eef119e0c93e272 */ void test_merge_workdir_setup__three_oids(void) { git_oid our_oid; git_oid octo1_oid; git_oid octo2_oid; git_oid octo3_oid; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO2_OID "'; commit '" OCTO3_OID "'\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c */ void test_merge_workdir_setup__branches_and_oids_1(void) { git_oid our_oid; git_reference *octo1_ref; git_oid octo2_oid; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'; commit '" OCTO2_OID "'\n")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git merge octo1 158dc7bedb202f5b26502bf3574faa7f4238d56c octo3 54269b3f6ec3d7d4ede24dd350dd5d605495c3ae */ void test_merge_workdir_setup__branches_and_oids_2(void) { git_oid our_oid; git_reference *octo1_ref; git_oid octo2_oid; git_reference *octo3_ref; git_oid octo4_oid; git_annotated_commit *our_head, *their_heads[4]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo2_oid)); cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref)); cl_git_pass(git_oid__fromstr(&octo4_oid, OCTO4_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[3], repo, &octo4_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "'; commit '" OCTO2_OID "'; commit '" OCTO4_OID "'\n")); git_reference_free(octo1_ref); git_reference_free(octo3_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); git_annotated_commit_free(their_heads[3]); } /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 */ void test_merge_workdir_setup__branches_and_oids_3(void) { git_oid our_oid; git_oid octo1_oid; git_reference *octo2_ref; git_oid octo3_oid; git_reference *octo4_ref; git_annotated_commit *our_head, *their_heads[4]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid)); cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "'; commit '" OCTO3_OID "'\n")); git_reference_free(octo2_ref); git_reference_free(octo4_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); git_annotated_commit_free(their_heads[3]); } /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 octo2 50ce7d7d01217679e26c55939eef119e0c93e272 octo4 octo5 */ void test_merge_workdir_setup__branches_and_oids_4(void) { git_oid our_oid; git_oid octo1_oid; git_reference *octo2_ref; git_oid octo3_oid; git_reference *octo4_ref; git_reference *octo5_ref; git_annotated_commit *our_head, *their_heads[5]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_oid)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo3_oid)); cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref)); cl_git_pass(git_reference_lookup(&octo5_ref, repo, GIT_REFS_HEADS_DIR OCTO5_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[4], repo, octo5_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 5)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n" OCTO5_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; branches '" OCTO2_BRANCH "', '" OCTO4_BRANCH "' and '" OCTO5_BRANCH "'; commit '" OCTO3_OID "'\n")); git_reference_free(octo2_ref); git_reference_free(octo4_ref); git_reference_free(octo5_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); git_annotated_commit_free(their_heads[3]); git_annotated_commit_free(their_heads[4]); } /* git merge octo1 octo1 octo1 */ void test_merge_workdir_setup__three_same_branches(void) { git_oid our_oid; git_reference *octo1_1_ref; git_reference *octo1_2_ref; git_reference *octo1_3_ref; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_1_ref)); cl_git_pass(git_reference_lookup(&octo1_2_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo1_2_ref)); cl_git_pass(git_reference_lookup(&octo1_3_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo1_3_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO1_BRANCH "' and '" OCTO1_BRANCH "'\n")); git_reference_free(octo1_1_ref); git_reference_free(octo1_2_ref); git_reference_free(octo1_3_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } /* git merge 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 16f825815cfd20a07a75c71554e82d8eede0b061 */ void test_merge_workdir_setup__three_same_oids(void) { git_oid our_oid; git_oid octo1_1_oid; git_oid octo1_2_oid; git_oid octo1_3_oid; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &octo1_1_oid)); cl_git_pass(git_oid__fromstr(&octo1_2_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[1], repo, &octo1_2_oid)); cl_git_pass(git_oid__fromstr(&octo1_3_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[2], repo, &octo1_3_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO1_OID "\n" OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge commit '" OCTO1_OID "'; commit '" OCTO1_OID "'; commit '" OCTO1_OID "'\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } static int create_remote_tracking_branch(const char *branch_name, const char *oid_str) { int error = 0; git_str remotes_path = GIT_STR_INIT, origin_path = GIT_STR_INIT, filename = GIT_STR_INIT, data = GIT_STR_INIT; if ((error = git_str_puts(&remotes_path, git_repository_path(repo))) < 0 || (error = git_str_puts(&remotes_path, GIT_REFS_REMOTES_DIR)) < 0) goto done; if (!git_fs_path_exists(git_str_cstr(&remotes_path)) && (error = p_mkdir(git_str_cstr(&remotes_path), 0777)) < 0) goto done; if ((error = git_str_puts(&origin_path, git_str_cstr(&remotes_path))) < 0 || (error = git_str_puts(&origin_path, "origin")) < 0) goto done; if (!git_fs_path_exists(git_str_cstr(&origin_path)) && (error = p_mkdir(git_str_cstr(&origin_path), 0777)) < 0) goto done; if ((error = git_str_puts(&filename, git_str_cstr(&origin_path))) < 0 || (error = git_str_puts(&filename, "/")) < 0 || (error = git_str_puts(&filename, branch_name)) < 0 || (error = git_str_puts(&data, oid_str)) < 0 || (error = git_str_puts(&data, "\n")) < 0) goto done; cl_git_rewritefile(git_str_cstr(&filename), git_str_cstr(&data)); done: git_str_dispose(&remotes_path); git_str_dispose(&origin_path); git_str_dispose(&filename); git_str_dispose(&data); return error; } /* git merge refs/remotes/origin/octo1 */ void test_merge_workdir_setup__remote_tracking_one_branch(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } /* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 */ void test_merge_workdir_setup__remote_tracking_two_branches(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID)); cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "' and 'refs/remotes/origin/" OCTO2_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git merge refs/remotes/origin/octo1 refs/remotes/origin/octo2 refs/remotes/origin/octo3 */ void test_merge_workdir_setup__remote_tracking_three_branches(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_reference *octo3_ref; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID)); cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID)); cl_git_pass(create_remote_tracking_branch(OCTO3_BRANCH, OCTO3_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO3_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge remote-tracking branches 'refs/remotes/origin/" OCTO1_BRANCH "', 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO3_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_reference_free(octo3_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } /* git merge octo1 refs/remotes/origin/octo2 */ void test_merge_workdir_setup__normal_branch_and_remote_tracking_branch(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO2_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git merge refs/remotes/origin/octo1 octo2 */ void test_merge_workdir_setup__remote_tracking_branch_and_normal_branch(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(create_remote_tracking_branch(OCTO1_BRANCH, OCTO1_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO2_BRANCH "', remote-tracking branch 'refs/remotes/origin/" OCTO1_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git merge octo1 refs/remotes/origin/octo2 octo3 refs/remotes/origin/octo4 */ void test_merge_workdir_setup__two_remote_tracking_branch_and_two_normal_branches(void) { git_oid our_oid; git_reference *octo1_ref; git_reference *octo2_ref; git_reference *octo3_ref; git_reference *octo4_ref; git_annotated_commit *our_head, *their_heads[4]; cl_git_pass(create_remote_tracking_branch(OCTO2_BRANCH, OCTO2_OID)); cl_git_pass(create_remote_tracking_branch(OCTO4_BRANCH, OCTO4_OID)); cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_reference_lookup(&octo2_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO2_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[1], repo, octo2_ref)); cl_git_pass(git_reference_lookup(&octo3_ref, repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[2], repo, octo3_ref)); cl_git_pass(git_reference_lookup(&octo4_ref, repo, GIT_REFS_REMOTES_DIR "origin/" OCTO4_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[3], repo, octo4_ref)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "', remote-tracking branches 'refs/remotes/origin/" OCTO2_BRANCH "' and 'refs/remotes/origin/" OCTO4_BRANCH "'\n")); git_reference_free(octo1_ref); git_reference_free(octo2_ref); git_reference_free(octo3_ref); git_reference_free(octo4_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); git_annotated_commit_free(their_heads[3]); } /* git pull origin branch octo1 */ void test_merge_workdir_setup__pull_one(void) { git_oid our_oid; git_oid octo1_1_oid; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_1_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 1)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch 'octo1' of http://remote.url/repo.git\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } /* git pull origin octo1 octo2 */ void test_merge_workdir_setup__pull_two(void) { git_oid our_oid; git_oid octo1_oid; git_oid octo2_oid; git_annotated_commit *our_head, *their_heads[2]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 2)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO2_BRANCH "' of http://remote.url/repo.git\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); } /* git pull origin octo1 octo2 octo3 */ void test_merge_workdir_setup__pull_three(void) { git_oid our_oid; git_oid octo1_oid; git_oid octo2_oid; git_oid octo3_oid; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &octo1_oid)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.url/repo.git", &octo2_oid)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.url/repo.git", &octo3_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "', '" OCTO2_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.url/repo.git\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } void test_merge_workdir_setup__three_remotes(void) { git_oid our_oid; git_oid octo1_oid; git_oid octo2_oid; git_oid octo3_oid; git_annotated_commit *our_head, *their_heads[3]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.third/repo.git", &octo3_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 3)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "' of http://remote.first/repo.git, branch '" OCTO2_BRANCH "' of http://remote.second/repo.git, branch '" OCTO3_BRANCH "' of http://remote.third/repo.git\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); } void test_merge_workdir_setup__two_remotes(void) { git_oid our_oid; git_oid octo1_oid; git_oid octo2_oid; git_oid octo3_oid; git_oid octo4_oid; git_annotated_commit *our_head, *their_heads[4]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_oid__fromstr(&octo1_oid, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.first/repo.git", &octo1_oid)); cl_git_pass(git_oid__fromstr(&octo2_oid, OCTO2_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[1], repo, GIT_REFS_HEADS_DIR OCTO2_BRANCH, "http://remote.second/repo.git", &octo2_oid)); cl_git_pass(git_oid__fromstr(&octo3_oid, OCTO3_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[2], repo, GIT_REFS_HEADS_DIR OCTO3_BRANCH, "http://remote.first/repo.git", &octo3_oid)); cl_git_pass(git_oid__fromstr(&octo4_oid, OCTO4_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&their_heads[3], repo, GIT_REFS_HEADS_DIR OCTO4_BRANCH, "http://remote.second/repo.git", &octo4_oid)); cl_git_pass(git_merge__setup(repo, our_head, (const git_annotated_commit **)their_heads, 4)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branches '" OCTO1_BRANCH "' and '" OCTO3_BRANCH "' of http://remote.first/repo.git, branches '" OCTO2_BRANCH "' and '" OCTO4_BRANCH "' of http://remote.second/repo.git\n")); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); git_annotated_commit_free(their_heads[1]); git_annotated_commit_free(their_heads[2]); git_annotated_commit_free(their_heads[3]); } void test_merge_workdir_setup__id_from_head(void) { git_oid expected_id; const git_oid *id; git_reference *ref; git_annotated_commit *heads[3]; cl_git_pass(git_oid__fromstr(&expected_id, OCTO1_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_from_fetchhead(&heads[0], repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH, "http://remote.url/repo.git", &expected_id)); id = git_annotated_commit_id(heads[0]); cl_assert_equal_i(1, git_oid_equal(id, &expected_id)); cl_git_pass(git_annotated_commit_lookup(&heads[1], repo, &expected_id)); id = git_annotated_commit_id(heads[1]); cl_assert_equal_i(1, git_oid_equal(id, &expected_id)); cl_git_pass(git_reference_lookup(&ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&heads[2], repo, ref)); id = git_annotated_commit_id(heads[2]); cl_assert_equal_i(1, git_oid_equal(id, &expected_id)); git_reference_free(ref); git_annotated_commit_free(heads[0]); git_annotated_commit_free(heads[1]); git_annotated_commit_free(heads[2]); } struct annotated_commit_cb_data { const char **oid_str; unsigned int len; unsigned int i; }; static int annotated_commit_foreach_cb(const git_oid *oid, void *payload) { git_oid expected_oid; struct annotated_commit_cb_data *cb_data = payload; git_oid__fromstr(&expected_oid, cb_data->oid_str[cb_data->i], GIT_OID_SHA1); cl_assert(git_oid_cmp(&expected_oid, oid) == 0); cb_data->i++; return 0; } void test_merge_workdir_setup__head_notfound(void) { int error; cl_git_fail((error = git_repository_mergehead_foreach(repo, annotated_commit_foreach_cb, NULL))); cl_assert(error == GIT_ENOTFOUND); } void test_merge_workdir_setup__head_invalid_oid(void) { int error; write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n"); cl_git_fail((error = git_repository_mergehead_foreach(repo, annotated_commit_foreach_cb, NULL))); cl_assert(error == -1); } void test_merge_workdir_setup__head_foreach_nonewline(void) { int error; write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID); cl_git_fail((error = git_repository_mergehead_foreach(repo, annotated_commit_foreach_cb, NULL))); cl_assert(error == -1); } void test_merge_workdir_setup__head_foreach_one(void) { const char *expected = THEIRS_SIMPLE_OID; struct annotated_commit_cb_data cb_data = { &expected, 1 }; write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n"); cl_git_pass(git_repository_mergehead_foreach(repo, annotated_commit_foreach_cb, &cb_data)); cl_assert(cb_data.i == cb_data.len); } void test_merge_workdir_setup__head_foreach_octopus(void) { const char *expected[] = { THEIRS_SIMPLE_OID, OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID }; struct annotated_commit_cb_data cb_data = { expected, 6 }; write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n" OCTO1_OID "\n" OCTO2_OID "\n" OCTO3_OID "\n" OCTO4_OID "\n" OCTO5_OID "\n"); cl_git_pass(git_repository_mergehead_foreach(repo, annotated_commit_foreach_cb, &cb_data)); cl_assert(cb_data.i == cb_data.len); } void test_merge_workdir_setup__retained_after_success(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL)); cl_assert(test_file_contents(GIT_MERGE_HEAD_FILE, OCTO1_OID "\n")); cl_assert(test_file_contents(GIT_ORIG_HEAD_FILE, ORIG_HEAD "\n")); cl_assert(test_file_contents(GIT_MERGE_MODE_FILE, "no-ff")); cl_assert(test_file_contents(GIT_MERGE_MSG_FILE, "Merge branch '" OCTO1_BRANCH "'\n")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_setup__removed_after_failure(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_write2file("merge-resolve/.git/index.lock", "foo\n", 4, O_RDWR|O_CREAT, 0666); cl_git_fail(git_merge( repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL)); cl_assert(!git_fs_path_exists("merge-resolve/.git/" GIT_MERGE_HEAD_FILE)); cl_assert(!git_fs_path_exists("merge-resolve/.git/" GIT_MERGE_MODE_FILE)); cl_assert(!git_fs_path_exists("merge-resolve/.git/" GIT_MERGE_MSG_FILE)); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_setup__unlocked_after_success(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_pass(git_merge( repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL)); cl_assert(!git_fs_path_exists("merge-resolve/.git/index.lock")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_setup__unlocked_after_conflict(void) { git_oid our_oid; git_reference *octo1_ref; git_annotated_commit *our_head, *their_heads[1]; cl_git_pass(git_oid__fromstr(&our_oid, ORIG_HEAD, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&our_head, repo, &our_oid)); cl_git_pass(git_reference_lookup(&octo1_ref, repo, GIT_REFS_HEADS_DIR OCTO1_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&their_heads[0], repo, octo1_ref)); cl_git_rewritefile("merge-resolve/new-in-octo1.txt", "Conflicting file!\n\nMerge will fail!\n"); cl_git_fail(git_merge( repo, (const git_annotated_commit **)&their_heads[0], 1, NULL, NULL)); cl_assert(!git_fs_path_exists("merge-resolve/.git/index.lock")); git_reference_free(octo1_ref); git_annotated_commit_free(our_head); git_annotated_commit_free(their_heads[0]); }
libgit2-main
tests/libgit2/merge/workdir/setup.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "../conflict_data.h" static git_repository *repo; #define TEST_REPO_PATH "merge-recursive" void test_merge_workdir_recursive__initialize(void) { repo = cl_git_sandbox_init(TEST_REPO_PATH); } void test_merge_workdir_recursive__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_workdir_recursive__writes_conflict_with_virtual_base(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; git_str conflicting_buf = GIT_STR_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "fa567f568ed72157c0c617438d077695b99d9aac", 1, "veal.txt" }, { 0100644, "21950d5e4e4d1a871b4dfcf72ecb6b9c162c434e", 2, "veal.txt" }, { 0100644, "3855170cef875708da06ab9ad7fc6a73b531cda1", 3, "veal.txt" }, }; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR "branchF-1", GIT_REFS_HEADS_DIR "branchF-2", &opts, NULL)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(merge_test_index(index, merge_index_entries, 8)); cl_git_pass(git_futils_readbuffer(&conflicting_buf, "merge-recursive/veal.txt")); cl_assert_equal_s(CONFLICTING_RECURSIVE_F1_TO_F2, conflicting_buf.ptr); git_index_free(index); git_str_dispose(&conflicting_buf); } void test_merge_workdir_recursive__conflicting_merge_base_with_diff3(void) { git_index *index; git_merge_options opts = GIT_MERGE_OPTIONS_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; git_str conflicting_buf = GIT_STR_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" }, { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, { 0100644, "0b01d2f70a1c6b9ab60c382f3f9cdc8173da6736", 1, "veal.txt" }, { 0100644, "37a5054a9f9b4628e3924c5cb8f2147c6e2a3efc", 2, "veal.txt" }, { 0100644, "d604c75019c282144bdbbf3fd3462ba74b240efc", 3, "veal.txt" }, }; opts.file_flags |= GIT_MERGE_FILE_STYLE_DIFF3; checkout_opts.checkout_strategy |= GIT_CHECKOUT_CONFLICT_STYLE_DIFF3; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR "branchH-2", GIT_REFS_HEADS_DIR "branchH-1", &opts, &checkout_opts)); cl_git_pass(git_repository_index(&index, repo)); cl_assert(merge_test_index(index, merge_index_entries, 8)); cl_git_pass(git_futils_readbuffer(&conflicting_buf, "merge-recursive/veal.txt")); cl_assert_equal_s(CONFLICTING_RECURSIVE_H2_TO_H1_WITH_DIFF3, conflicting_buf.ptr); git_index_free(index); git_str_dispose(&conflicting_buf); }
libgit2-main
tests/libgit2/merge/workdir/recursive.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "../conflict_data.h" #include "refs.h" #include "futils.h" static git_repository *repo; static git_index *repo_index; #define TEST_REPO_PATH "merge-resolve" #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" #define THEIRS_SIMPLE_BRANCH "branch" #define THEIRS_SIMPLE_OID "7cb63eed597130ba4abb87b3e544b85021905520" #define THEIRS_UNRELATED_BRANCH "unrelated" #define THEIRS_UNRELATED_OID "55b4e4687e7a0d9ca367016ed930f385d4022e6f" #define THEIRS_UNRELATED_PARENT "d6cf6c7741b3316826af1314042550c97ded1d50" #define OURS_DIRECTORY_FILE "df_side1" #define THEIRS_DIRECTORY_FILE "fc90237dc4891fa6c69827fc465632225e391618" /* Non-conflicting files, index entries are common to every merge operation */ #define ADDED_IN_MASTER_INDEX_ENTRY \ { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, \ "added-in-master.txt" } #define AUTOMERGEABLE_INDEX_ENTRY \ { 0100644, "f2e1550a0c9e53d5811175864a29536642ae3821", 0, \ "automergeable.txt" } #define CHANGED_IN_BRANCH_INDEX_ENTRY \ { 0100644, "4eb04c9e79e88f6640d01ff5b25ca2a60764f216", 0, \ "changed-in-branch.txt" } #define CHANGED_IN_MASTER_INDEX_ENTRY \ { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, \ "changed-in-master.txt" } #define UNCHANGED_INDEX_ENTRY \ { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, \ "unchanged.txt" } /* Unrelated files */ #define UNRELATED_NEW1 \ { 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, \ "new-in-unrelated1.txt" } #define UNRELATED_NEW2 \ { 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, \ "new-in-unrelated2.txt" } /* Expected REUC entries */ #define AUTOMERGEABLE_REUC_ENTRY \ { "automergeable.txt", 0100644, 0100644, 0100644, \ "6212c31dab5e482247d7977e4f0dd3601decf13b", \ "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", \ "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe" } #define CONFLICTING_REUC_ENTRY \ { "conflicting.txt", 0100644, 0100644, 0100644, \ "d427e0b2e138501a3d15cc376077a3631e15bd46", \ "4e886e602529caa9ab11d71f86634bd1b6e0de10", \ "2bd0a343aeef7a2cf0d158478966a6e587ff3863" } #define REMOVED_IN_BRANCH_REUC_ENTRY \ { "removed-in-branch.txt", 0100644, 0100644, 0, \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "dfe3f22baa1f6fce5447901c3086bae368de6bdd", \ "" } #define REMOVED_IN_MASTER_REUC_ENTRY \ { "removed-in-master.txt", 0100644, 0, 0100644, \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5", \ "", \ "5c3b68a71fc4fa5d362fd3875e53137c6a5ab7a5" } /* Fixture setup and teardown */ void test_merge_workdir_simple__initialize(void) { git_config *cfg; repo = cl_git_sandbox_init(TEST_REPO_PATH); git_repository_index(&repo_index, repo); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); git_config_free(cfg); } void test_merge_workdir_simple__cleanup(void) { git_index_free(repo_index); cl_git_sandbox_cleanup(); } static void merge_simple_branch(int merge_file_favor, int addl_checkout_strategy) { git_oid their_oids[1]; git_annotated_commit *their_heads[1]; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; cl_git_pass(git_oid__fromstr(&their_oids[0], THEIRS_SIMPLE_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &their_oids[0])); merge_opts.file_favor = merge_file_favor; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS | addl_checkout_strategy; cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, &merge_opts, &checkout_opts)); git_annotated_commit_free(their_heads[0]); } static void set_core_autocrlf_to(git_repository *repo, bool value) { git_config *cfg; cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", value)); git_config_free(cfg); } void test_merge_workdir_simple__automerge(void) { git_index *index; const git_index_entry *entry; git_str automergeable_buf = GIT_STR_INIT; 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 }; set_core_autocrlf_to(repo, false); merge_simple_branch(0, 0); cl_git_pass(git_futils_readbuffer(&automergeable_buf, TEST_REPO_PATH "/automergeable.txt")); cl_assert(strcmp(automergeable_buf.ptr, AUTOMERGEABLE_MERGED_FILE) == 0); git_str_dispose(&automergeable_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_repository_index(&index, repo); cl_assert((entry = git_index_get_bypath(index, "automergeable.txt", 0)) != NULL); cl_assert(entry->file_size == strlen(AUTOMERGEABLE_MERGED_FILE)); git_index_free(index); } void test_merge_workdir_simple__index_reload(void) { git_repository *tmp_repo; git_annotated_commit *their_heads[1]; git_oid their_oid; git_index_entry entry = {{0}}; git_index *tmp_index; cl_git_pass(git_repository_open(&tmp_repo, git_repository_workdir(repo))); cl_git_pass(git_repository_index(&tmp_index, tmp_repo)); cl_git_pass(git_index_read(repo_index, 0)); entry.mode = GIT_FILEMODE_BLOB; cl_git_pass(git_oid__fromstr(&entry.id, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", GIT_OID_SHA1)); entry.path = "automergeable.txt"; cl_git_pass(git_index_add(repo_index, &entry)); cl_git_pass(git_index_add_bypath(tmp_index, "automergeable.txt")); cl_git_pass(git_index_write(tmp_index)); cl_git_pass(git_oid__fromstr(&their_oid, THEIRS_SIMPLE_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &their_oid)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, NULL, NULL)); git_index_free(tmp_index); git_repository_free(tmp_repo); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_simple__automerge_crlf(void) { #ifdef GIT_WIN32 git_index *index; const git_index_entry *entry; git_str automergeable_buf = GIT_STR_INIT; 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 }; set_core_autocrlf_to(repo, true); merge_simple_branch(0, 0); cl_git_pass(git_futils_readbuffer(&automergeable_buf, TEST_REPO_PATH "/automergeable.txt")); cl_assert(strcmp(automergeable_buf.ptr, AUTOMERGEABLE_MERGED_FILE_CRLF) == 0); git_str_dispose(&automergeable_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_repository_index(&index, repo); cl_assert((entry = git_index_get_bypath(index, "automergeable.txt", 0)) != NULL); cl_assert(entry->file_size == strlen(AUTOMERGEABLE_MERGED_FILE_CRLF)); git_index_free(index); #endif /* GIT_WIN32 */ } void test_merge_workdir_simple__mergefile(void) { git_str conflicting_buf = GIT_STR_INIT, mergemsg_buf = GIT_STR_INIT; 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 }; set_core_autocrlf_to(repo, false); merge_simple_branch(0, 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_MERGE_FILE) == 0); cl_git_pass(git_futils_readbuffer(&mergemsg_buf, TEST_REPO_PATH "/.git/MERGE_MSG")); cl_assert(strcmp(git_str_cstr(&mergemsg_buf), "Merge commit '7cb63eed597130ba4abb87b3e544b85021905520'\n" \ "\n" \ "#Conflicts:\n" \ "#\tconflicting.txt\n") == 0); git_str_dispose(&conflicting_buf); git_str_dispose(&mergemsg_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); } void test_merge_workdir_simple__diff3(void) { git_str conflicting_buf = GIT_STR_INIT; 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 }; set_core_autocrlf_to(repo, false); merge_simple_branch(0, GIT_CHECKOUT_CONFLICT_STYLE_DIFF3); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_DIFF3_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); } void test_merge_workdir_simple__zdiff3(void) { git_str conflicting_buf = GIT_STR_INIT; 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 }; set_core_autocrlf_to(repo, false); merge_simple_branch(0, GIT_CHECKOUT_CONFLICT_STYLE_ZDIFF3); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert_equal_s(CONFLICTING_ZDIFF3_FILE, conflicting_buf.ptr); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); } void test_merge_workdir_simple__union(void) { git_str conflicting_buf = GIT_STR_INIT; 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, "72cdb057b340205164478565e91eb71647e66891", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY }; set_core_autocrlf_to(repo, false); merge_simple_branch(GIT_MERGE_FILE_FAVOR_UNION, 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_UNION_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 6)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 4)); } void test_merge_workdir_simple__gitattributes_union(void) { git_str conflicting_buf = GIT_STR_INIT; 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, "72cdb057b340205164478565e91eb71647e66891", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY }; set_core_autocrlf_to(repo, false); cl_git_mkfile(TEST_REPO_PATH "/.gitattributes", "conflicting.txt merge=union\n"); merge_simple_branch(GIT_MERGE_FILE_FAVOR_NORMAL, 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_UNION_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 6)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 4)); } void test_merge_workdir_simple__diff3_from_config(void) { git_config *config; git_str conflicting_buf = GIT_STR_INIT; 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(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.conflictstyle", "diff3")); set_core_autocrlf_to(repo, false); merge_simple_branch(0, 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_DIFF3_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_config_free(config); } void test_merge_workdir_simple__zdiff3_from_config(void) { git_config *config; git_str conflicting_buf = GIT_STR_INIT; 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(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.conflictstyle", "zdiff3")); set_core_autocrlf_to(repo, false); merge_simple_branch(0, 0); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_ZDIFF3_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_config_free(config); } void test_merge_workdir_simple__merge_overrides_config(void) { git_config *config; git_str conflicting_buf = GIT_STR_INIT; 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(git_repository_config(&config, repo)); cl_git_pass(git_config_set_string(config, "merge.conflictstyle", "diff3")); set_core_autocrlf_to(repo, false); merge_simple_branch(0, GIT_CHECKOUT_CONFLICT_STYLE_MERGE); cl_git_pass(git_futils_readbuffer(&conflicting_buf, TEST_REPO_PATH "/conflicting.txt")); cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_MERGE_FILE) == 0); git_str_dispose(&conflicting_buf); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); git_config_free(config); } void test_merge_workdir_simple__checkout_ours(void) { 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 }; merge_simple_branch(0, GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS); cl_assert(merge_test_index(repo_index, merge_index_entries, 8)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3)); cl_assert(git_fs_path_exists(TEST_REPO_PATH "/conflicting.txt")); } void test_merge_workdir_simple__favor_ours(void) { 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, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY, }; merge_simple_branch(GIT_MERGE_FILE_FAVOR_OURS, 0); cl_assert(merge_test_index(repo_index, merge_index_entries, 6)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 4)); } void test_merge_workdir_simple__favor_theirs(void) { 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, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", 0, "conflicting.txt" }, UNCHANGED_INDEX_ENTRY, }; struct merge_reuc_entry merge_reuc_entries[] = { AUTOMERGEABLE_REUC_ENTRY, CONFLICTING_REUC_ENTRY, REMOVED_IN_BRANCH_REUC_ENTRY, REMOVED_IN_MASTER_REUC_ENTRY, }; merge_simple_branch(GIT_MERGE_FILE_FAVOR_THEIRS, 0); cl_assert(merge_test_index(repo_index, merge_index_entries, 6)); cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 4)); } void test_merge_workdir_simple__directory_file(void) { git_reference *head; git_oid their_oids[1], head_commit_id; git_annotated_commit *their_heads[1]; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_commit *head_commit; struct merge_index_entry merge_index_entries[] = { { 0100644, "49130a28ef567af9a6a6104c38773fedfa5f9742", 2, "dir-10" }, { 0100644, "6c06dcd163587c2cc18be44857e0b71116382aeb", 3, "dir-10" }, { 0100644, "43aafd43bea779ec74317dc361f45ae3f532a505", 0, "dir-6" }, { 0100644, "a031a28ae70e33a641ce4b8a8f6317f1ab79dee4", 3, "dir-7" }, { 0100644, "5012fd565b1393bdfda1805d4ec38ce6619e1fd1", 1, "dir-7/file.txt" }, { 0100644, "a5563304ddf6caba25cb50323a2ea6f7dbfcadca", 2, "dir-7/file.txt" }, { 0100644, "e9ad6ec3e38364a3d07feda7c4197d4d845c53b5", 0, "dir-8" }, { 0100644, "3ef4d30382ca33fdeba9fda895a99e0891ba37aa", 2, "dir-9" }, { 0100644, "fc4c636d6515e9e261f9260dbcf3cc6eca97ea08", 1, "dir-9/file.txt" }, { 0100644, "76ab0e2868197ec158ddd6c78d8a0d2fd73d38f9", 3, "dir-9/file.txt" }, { 0100644, "5c2411f8075f48a6b2fdb85ebc0d371747c4df15", 0, "file-1/new" }, { 0100644, "a39a620dae5bc8b4e771cd4d251b7d080401a21e", 1, "file-2" }, { 0100644, "d963979c237d08b6ba39062ee7bf64c7d34a27f8", 2, "file-2" }, { 0100644, "5c341ead2ba6f2af98ce5ec3fe84f6b6d2899c0d", 0, "file-2/new" }, { 0100644, "9efe7723802d4305142eee177e018fee1572c4f4", 0, "file-3/new" }, { 0100644, "bacac9b3493509aa15e1730e1545fc0919d1dae0", 1, "file-4" }, { 0100644, "7663fce0130db092936b137cabd693ec234eb060", 3, "file-4" }, { 0100644, "e49f917b448d1340b31d76e54ba388268fd4c922", 0, "file-4/new" }, { 0100644, "cab2cf23998b40f1af2d9d9a756dc9e285a8df4b", 2, "file-5/new" }, { 0100644, "f5504f36e6f4eb797a56fc5bac6c6c7f32969bf2", 3, "file-5/new" }, }; cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_DIR OURS_DIRECTORY_FILE, 1, NULL)); cl_git_pass(git_reference_name_to_id(&head_commit_id, repo, GIT_HEAD_FILE)); cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_id)); cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL)); cl_git_pass(git_oid__fromstr(&their_oids[0], THEIRS_DIRECTORY_FILE, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &their_oids[0])); merge_opts.file_favor = 0; cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, &merge_opts, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 20)); git_reference_free(head); git_commit_free(head_commit); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_simple__unrelated(void) { git_oid their_oids[1]; git_annotated_commit *their_heads[1]; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 0, "automergeable.txt" }, { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 0, "conflicting.txt" }, { 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, "new-in-unrelated1.txt" }, { 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, "new-in-unrelated2.txt" }, { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, }; cl_git_pass(git_oid__fromstr(&their_oids[0], THEIRS_UNRELATED_PARENT, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &their_oids[0])); merge_opts.file_favor = 0; cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, &merge_opts, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 9)); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_simple__unrelated_with_conflicts(void) { git_oid their_oids[1]; git_annotated_commit *their_heads[1]; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" }, { 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 2, "automergeable.txt" }, { 0100644, "d07ec190c306ec690bac349e87d01c4358e49bb2", 3, "automergeable.txt" }, { 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" }, { 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" }, { 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" }, { 0100644, "4b253da36a0ae8bfce63aeabd8c5b58429925594", 3, "conflicting.txt" }, { 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, "new-in-unrelated1.txt" }, { 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, "new-in-unrelated2.txt" }, { 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" }, { 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" }, }; cl_git_pass(git_oid__fromstr(&their_oids[0], THEIRS_UNRELATED_OID, GIT_OID_SHA1)); cl_git_pass(git_annotated_commit_lookup(&their_heads[0], repo, &their_oids[0])); merge_opts.file_favor = 0; cl_git_pass(git_merge(repo, (const git_annotated_commit **)their_heads, 1, &merge_opts, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 11)); git_annotated_commit_free(their_heads[0]); } void test_merge_workdir_simple__binary(void) { git_oid our_oid, their_oid, our_file_oid; git_commit *our_commit; git_annotated_commit *their_head; const git_index_entry *binary_entry; struct merge_index_entry merge_index_entries[] = { { 0100644, "1c51d885170f57a0c4e8c69ff6363d91a5b51f85", 1, "binary" }, { 0100644, "23ed141a6ae1e798b2f721afedbe947c119111ba", 2, "binary" }, { 0100644, "836b8b82b26cab22eaaed8820877c76d6c8bca19", 3, "binary" }, }; cl_git_pass(git_oid__fromstr(&our_oid, "cc338e4710c9b257106b8d16d82f86458d5beaf1", GIT_OID_SHA1)); cl_git_pass(git_oid__fromstr(&their_oid, "ad01aebfdf2ac13145efafe3f9fcf798882f1730", GIT_OID_SHA1)); cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid)); cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL)); cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_oid)); cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL)); cl_assert(merge_test_index(repo_index, merge_index_entries, 3)); cl_git_pass(git_index_add_bypath(repo_index, "binary")); cl_assert((binary_entry = git_index_get_bypath(repo_index, "binary", 0)) != NULL); cl_git_pass(git_oid__fromstr(&our_file_oid, "23ed141a6ae1e798b2f721afedbe947c119111ba", GIT_OID_SHA1)); cl_assert(git_oid_cmp(&binary_entry->id, &our_file_oid) == 0); git_annotated_commit_free(their_head); git_commit_free(our_commit); }
libgit2-main
tests/libgit2/merge/workdir/simple.c
#include "clar_libgit2.h" #include "git2/repository.h" #include "git2/merge.h" #include "merge.h" #include "../merge_helpers.h" #include "futils.h" #include "refs.h" static git_repository *repo; #define TEST_REPO_PATH "merge-resolve" #define BRANCH_RENAME_OURS "rename_conflict_ours" #define BRANCH_RENAME_THEIRS "rename_conflict_theirs" /* Fixture setup and teardown */ void test_merge_workdir_renames__initialize(void) { git_config *cfg; repo = cl_git_sandbox_init(TEST_REPO_PATH); /* Ensure that the user's merge.conflictstyle doesn't interfere */ cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_set_string(cfg, "merge.conflictstyle", "merge")); git_config_free(cfg); } void test_merge_workdir_renames__cleanup(void) { cl_git_sandbox_cleanup(); } void test_merge_workdir_renames__renames(void) { git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "8aac75de2a34b4d340bf62a6e58197269cb55797", 0, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "7edc726325da726751a4195e434e4377b0f67f9a", 0, "0c-rewritten-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-newname-in-ours-added-in-theirs.txt~HEAD" }, { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 0, "4a-newname-in-ours-added-in-theirs.txt~rename_conflict_theirs" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 0, "4b-newname-in-theirs-added-in-ours.txt~HEAD" }, { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 0, "4b-newname-in-theirs-added-in-ours.txt~rename_conflict_theirs" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 0, "5a-newname-in-ours-added-in-theirs.txt~HEAD" }, { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 0, "5a-newname-in-ours-added-in-theirs.txt~rename_conflict_theirs" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 0, "5b-newname-in-theirs-added-in-ours.txt~HEAD" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 0, "5b-newname-in-theirs-added-in-ours.txt~rename_conflict_theirs" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-theirs.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "7-both-renamed.txt~HEAD" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "7-both-renamed.txt~rename_conflict_theirs" }, }; merge_opts.flags |= GIT_MERGE_FIND_RENAMES; merge_opts.rename_threshold = 50; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR BRANCH_RENAME_OURS, GIT_REFS_HEADS_DIR BRANCH_RENAME_THEIRS, &merge_opts, NULL)); cl_assert(merge_test_workdir(repo, merge_index_entries, 24)); } void test_merge_workdir_renames__ours(void) { git_index *index; git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "e376fbdd06ebf021c92724da9f26f44212734e3e", 0, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "efc9121fdedaf08ba180b53ebfbcf71bd488ed09", 0, "0c-rewritten-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 0, "4b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 0, "5a-newname-in-ours-added-in-theirs.txt" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 0, "5b-newname-in-theirs-added-in-ours.txt" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 0, "5b-renamed-in-theirs-added-in-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "7-both-renamed-side-2.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "7-both-renamed.txt" }, }; merge_opts.flags |= GIT_MERGE_FIND_RENAMES; merge_opts.rename_threshold = 50; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR BRANCH_RENAME_OURS, GIT_REFS_HEADS_DIR BRANCH_RENAME_THEIRS, &merge_opts, &checkout_opts)); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_write(index)); cl_assert(merge_test_workdir(repo, merge_index_entries, 20)); git_index_free(index); } void test_merge_workdir_renames__similar(void) { git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT; /* * Note: this differs slightly from the core git merge result - there, 4a is * tracked as a rename/delete instead of a rename/add and the theirs side * is not placed in workdir in any form. */ struct merge_index_entry merge_index_entries[] = { { 0100644, "68c6c84b091926c7d90aa6a79b2bc3bb6adccd8e", 0, "0a-no-change.txt" }, { 0100644, "f0ce2b8e4986084d9b308fb72709e414c23eb5e6", 0, "0b-duplicated-in-ours.txt" }, { 0100644, "8aac75de2a34b4d340bf62a6e58197269cb55797", 0, "0b-rewritten-in-ours.txt" }, { 0100644, "2f56120107d680129a5d9791b521cb1e73a2ed31", 0, "0c-duplicated-in-theirs.txt" }, { 0100644, "7edc726325da726751a4195e434e4377b0f67f9a", 0, "0c-rewritten-in-theirs.txt" }, { 0100644, "0d872f8e871a30208305978ecbf9e66d864f1638", 0, "1a-newname-in-ours-edited-in-theirs.txt" }, { 0100644, "d0d4594e16f2e19107e3fa7ea63e7aaaff305ffb", 0, "1a-newname-in-ours.txt" }, { 0100644, "ed9523e62e453e50dd9be1606af19399b96e397a", 0, "1b-newname-in-theirs-edited-in-ours.txt" }, { 0100644, "2b5f1f181ee3b58ea751f5dd5d8f9b445520a136", 0, "1b-newname-in-theirs.txt" }, { 0100644, "178940b450f238a56c0d75b7955cb57b38191982", 0, "2-newname-in-both.txt" }, { 0100644, "18cb316b1cefa0f8a6946f0e201a8e1a6f845ab9", 0, "3a-newname-in-ours-deleted-in-theirs.txt" }, { 0100644, "36219b49367146cb2e6a1555b5a9ebd4d0328495", 0, "3b-newname-in-theirs-deleted-in-ours.txt" }, { 0100644, "227792b52aaa0b238bea00ec7e509b02623f168c", 0, "4a-newname-in-ours-added-in-theirs.txt~HEAD" }, { 0100644, "8b5b53cb2aa9ceb1139f5312fcfa3cc3c5a47c9a", 0, "4a-newname-in-ours-added-in-theirs.txt~rename_conflict_theirs" }, { 0100644, "de872ee3618b894992e9d1e18ba2ebe256a112f9", 0, "4b-newname-in-theirs-added-in-ours.txt~HEAD" }, { 0100644, "98d52d07c0b0bbf2b46548f6aa521295c2cb55db", 0, "4b-newname-in-theirs-added-in-ours.txt~rename_conflict_theirs" }, { 0100644, "d3719a5ae8e4d92276b5313ce976f6ee5af2b436", 0, "5a-newname-in-ours-added-in-theirs.txt~HEAD" }, { 0100644, "98ba4205fcf31f5dd93c916d35fe3f3b3d0e6714", 0, "5a-newname-in-ours-added-in-theirs.txt~rename_conflict_theirs" }, { 0100644, "385c8a0f26ddf79e9041e15e17dc352ed2c4cced", 0, "5b-newname-in-theirs-added-in-ours.txt~HEAD" }, { 0100644, "63247125386de9ec90a27ad36169307bf8a11a38", 0, "5b-newname-in-theirs-added-in-ours.txt~rename_conflict_theirs" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-ours.txt" }, { 0100644, "d8fa77b6833082c1ea36b7828a582d4c43882450", 0, "6-both-renamed-1-to-2-theirs.txt" }, { 0100644, "b42712cfe99a1a500b2a51fe984e0b8a7702ba11", 0, "7-both-renamed.txt~HEAD" }, { 0100644, "b69fe837e4cecfd4c9a40cdca7c138468687df07", 0, "7-both-renamed.txt~rename_conflict_theirs" }, }; merge_opts.flags |= GIT_MERGE_FIND_RENAMES; merge_opts.rename_threshold = 50; cl_git_pass(merge_branches(repo, GIT_REFS_HEADS_DIR BRANCH_RENAME_OURS, GIT_REFS_HEADS_DIR BRANCH_RENAME_THEIRS, &merge_opts, NULL)); cl_assert(merge_test_workdir(repo, merge_index_entries, 24)); }
libgit2-main
tests/libgit2/merge/workdir/renames.c
#include "clar_libgit2.h" #include "filebuf.h" #include "futils.h" #include "posix.h" #define TEST_CONFIG "git-test-config" static git_buf buf = GIT_BUF_INIT; void test_config_stress__initialize(void) { git_filebuf file = GIT_FILEBUF_INIT; cl_git_pass(git_filebuf_open(&file, TEST_CONFIG, 0, 0666)); git_filebuf_printf(&file, "[color]\n\tui = auto\n"); git_filebuf_printf(&file, "[core]\n\teditor = \n"); cl_git_pass(git_filebuf_commit(&file)); } void test_config_stress__cleanup(void) { git_buf_dispose(&buf); p_unlink(TEST_CONFIG); } void test_config_stress__dont_break_on_invalid_input(void) { git_config *config; cl_assert(git_fs_path_exists(TEST_CONFIG)); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_get_string_buf(&buf, config, "color.ui")); cl_git_pass(git_config_get_string_buf(&buf, config, "core.editor")); git_config_free(config); } static void assert_config_value(git_config *config, const char *key, const char *value) { git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, config, key)); cl_assert_equal_s(value, buf.ptr); } void test_config_stress__comments(void) { git_config *config; cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config12"))); assert_config_value(config, "some.section.test2", "hello"); assert_config_value(config, "some.section.test3", "welcome"); assert_config_value(config, "some.section.other", "hello! \" ; ; ; "); assert_config_value(config, "some.section.other2", "cool! \" # # # "); assert_config_value(config, "some.section.multi", "hi, this is a ; multiline comment # with ;\n special chars and other stuff !@#"); assert_config_value(config, "some.section.multi2", "good, this is a ; multiline comment # with ;\n special chars and other stuff !@#"); assert_config_value(config, "some.section.back", "this is \ba phrase"); assert_config_value(config, "some.section.dollar", "some $sign"); assert_config_value(config, "some.section.multiquotes", "!ls x ls # comment2 $HOME"); assert_config_value(config, "some.section.multiquotes2", "!ls x ls \"# comment2 $HOME\""); assert_config_value(config, "some.section.multiquotes3", "hi # ho there are # more quotes"); assert_config_value(config, "some.section.quotecomment", "hi # ho there are # more"); git_config_free(config); } void test_config_stress__escape_subsection_names(void) { git_config *config; cl_assert(git_fs_path_exists("git-test-config")); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_set_string(config, "some.sec\\tion.other", "foo")); git_config_free(config); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); assert_config_value(config, "some.sec\\tion.other", "foo"); git_config_free(config); } void test_config_stress__trailing_backslash(void) { git_config *config; const char *path = "C:\\iam\\some\\windows\\path\\"; cl_assert(git_fs_path_exists("git-test-config")); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_set_string(config, "windows.path", path)); git_config_free(config); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); assert_config_value(config, "windows.path", path); git_config_free(config); } void test_config_stress__complex(void) { git_config *config; const char *path = "./config-immediate-multiline"; cl_git_mkfile(path, "[imm]\n multi = \"\\\nfoo\""); cl_git_pass(git_config_open_ondisk(&config, path)); assert_config_value(config, "imm.multi", "foo"); git_config_free(config); } void test_config_stress__quick_write(void) { git_config *config_w, *config_r; const char *path = "./config-quick-write"; const char *key = "quick.write"; int32_t i; /* Create an external writer for one instance with the other one */ cl_git_pass(git_config_open_ondisk(&config_w, path)); cl_git_pass(git_config_open_ondisk(&config_r, path)); /* Write and read in the same second (repeat to increase the chance of it happening) */ for (i = 0; i < 10; i++) { int32_t val; cl_git_pass(git_config_set_int32(config_w, key, i)); cl_msleep(1); cl_git_pass(git_config_get_int32(&val, config_r, key)); cl_assert_equal_i(i, val); } git_config_free(config_r); git_config_free(config_w); } static int foreach_cb(const git_config_entry *entry, void *payload) { if (!strcmp(entry->name, "key.value")) { *(char **)payload = git__strdup(entry->value); return 0; } return -1; } void test_config_stress__foreach_refreshes(void) { git_config *config_w, *config_r; char *value = NULL; cl_git_pass(git_config_open_ondisk(&config_w, "./cfg")); cl_git_pass(git_config_open_ondisk(&config_r, "./cfg")); cl_git_pass(git_config_set_string(config_w, "key.value", "1")); cl_git_pass(git_config_foreach_match(config_r, "key.value", foreach_cb, &value)); cl_assert_equal_s(value, "1"); git_config_free(config_r); git_config_free(config_w); git__free(value); } void test_config_stress__foreach_refreshes_snapshot(void) { git_config *config, *snapshot; char *value = NULL; cl_git_pass(git_config_open_ondisk(&config, "./cfg")); cl_git_pass(git_config_set_string(config, "key.value", "1")); cl_git_pass(git_config_snapshot(&snapshot, config)); cl_git_pass(git_config_foreach_match(snapshot, "key.value", foreach_cb, &value)); cl_assert_equal_s(value, "1"); git_config_free(snapshot); git_config_free(config); git__free(value); } void test_config_stress__huge_section_with_many_values(void) { git_config *config; if (!cl_is_env_set("GITTEST_INVASIVE_SPEED")) cl_skip(); /* * The config file is structured in such a way that is * has a section header that is approximately 500kb of * size followed by 40k entries. While the resulting * configuration file itself is roughly 650kb in size and * thus considered to be rather small, in the past we'd * balloon to more than 20GB of memory (20000x500kb) * while parsing the file. It thus was a trivial way to * cause an out-of-memory situation and thus cause denial * of service, e.g. via gitmodules. */ cl_git_pass(git_config_open_ondisk(&config, cl_fixture("config/config-oom"))); git_config_free(config); }
libgit2-main
tests/libgit2/config/stress.c
#include "clar_libgit2.h" #include "futils.h" #include "repository.h" #ifdef GIT_WIN32 # define ROOT_PREFIX "C:" #else # define ROOT_PREFIX #endif static git_repository *_repo; void test_config_conditionals__initialize(void) { _repo = cl_git_sandbox_init("empty_standard_repo"); } void test_config_conditionals__cleanup(void) { cl_git_sandbox_cleanup(); } static void assert_condition_includes(const char *keyword, const char *path, bool expected) { git_buf value = GIT_BUF_INIT; git_str buf = GIT_STR_INIT; git_config *cfg; cl_git_pass(git_str_printf(&buf, "[includeIf \"%s:%s\"]\n", keyword, path)); cl_git_pass(git_str_puts(&buf, "path = other\n")); cl_git_mkfile("empty_standard_repo/.git/config", buf.ptr); cl_git_mkfile("empty_standard_repo/.git/other", "[foo]\nbar=baz\n"); _repo = cl_git_sandbox_reopen(); git_str_dispose(&buf); cl_git_pass(git_repository_config(&cfg, _repo)); if (expected) { cl_git_pass(git_config_get_string_buf(&value, cfg, "foo.bar")); cl_assert_equal_s("baz", value.ptr); } else { cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string_buf(&value, cfg, "foo.bar")); } git_str_dispose(&buf); git_buf_dispose(&value); git_config_free(cfg); } static char *sandbox_path(git_str *buf, const char *suffix) { char *path = p_realpath(clar_sandbox_path(), NULL); cl_assert(path); cl_git_pass(git_str_attach(buf, path, 0)); cl_git_pass(git_str_joinpath(buf, buf->ptr, suffix)); return buf->ptr; } void test_config_conditionals__gitdir(void) { git_str path = GIT_STR_INIT; assert_condition_includes("gitdir", ROOT_PREFIX "/", true); assert_condition_includes("gitdir", "empty_stand", false); assert_condition_includes("gitdir", "empty_stand/", false); assert_condition_includes("gitdir", "empty_stand/.git", false); assert_condition_includes("gitdir", "empty_stand/.git/", false); assert_condition_includes("gitdir", "empty_stand*/", true); assert_condition_includes("gitdir", "empty_stand*/.git", true); assert_condition_includes("gitdir", "empty_stand*/.git/", false); assert_condition_includes("gitdir", "empty_standard_repo", false); assert_condition_includes("gitdir", "empty_standard_repo/", true); assert_condition_includes("gitdir", "empty_standard_repo/.git", true); assert_condition_includes("gitdir", "empty_standard_repo/.git/", false); assert_condition_includes("gitdir", "./", false); assert_condition_includes("gitdir", ROOT_PREFIX "/nonexistent", false); assert_condition_includes("gitdir", ROOT_PREFIX "/empty_standard_repo", false); assert_condition_includes("gitdir", "~/empty_standard_repo", false); assert_condition_includes("gitdir", sandbox_path(&path, "/"), true); assert_condition_includes("gitdir", sandbox_path(&path, "/*"), false); assert_condition_includes("gitdir", sandbox_path(&path, "/**"), true); assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo"), false); assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo/"), true); assert_condition_includes("gitdir", sandbox_path(&path, "empty_standard_repo/"), true); assert_condition_includes("gitdir", sandbox_path(&path, "Empty_Standard_Repo"), false); assert_condition_includes("gitdir", sandbox_path(&path, "Empty_Standard_Repo/"), false); git_str_dispose(&path); } void test_config_conditionals__gitdir_i(void) { git_str path = GIT_STR_INIT; assert_condition_includes("gitdir/i", sandbox_path(&path, "empty_standard_repo/"), true); assert_condition_includes("gitdir/i", sandbox_path(&path, "EMPTY_STANDARD_REPO/"), true); git_str_dispose(&path); } void test_config_conditionals__invalid_conditional_fails(void) { assert_condition_includes("foobar", ".git", false); } static void set_head(git_repository *repo, const char *name) { cl_git_pass(git_repository_create_head(git_repository_path(repo), name)); } void test_config_conditionals__onbranch(void) { assert_condition_includes("onbranch", "master", true); assert_condition_includes("onbranch", "m*", true); assert_condition_includes("onbranch", "*", true); assert_condition_includes("onbranch", "master/", false); assert_condition_includes("onbranch", "foo", false); set_head(_repo, "foo"); assert_condition_includes("onbranch", "master", false); assert_condition_includes("onbranch", "foo", true); assert_condition_includes("onbranch", "f*o", true); set_head(_repo, "dir/ref"); assert_condition_includes("onbranch", "dir/ref", true); assert_condition_includes("onbranch", "dir/", true); assert_condition_includes("onbranch", "dir/*", true); assert_condition_includes("onbranch", "dir/**", true); assert_condition_includes("onbranch", "**", true); assert_condition_includes("onbranch", "dir", false); assert_condition_includes("onbranch", "dir*", false); set_head(_repo, "dir/subdir/ref"); assert_condition_includes("onbranch", "dir/subdir/", true); assert_condition_includes("onbranch", "dir/subdir/*", true); assert_condition_includes("onbranch", "dir/subdir/ref", true); assert_condition_includes("onbranch", "dir/", true); assert_condition_includes("onbranch", "dir/**", true); assert_condition_includes("onbranch", "**", true); assert_condition_includes("onbranch", "dir", false); assert_condition_includes("onbranch", "dir*", false); assert_condition_includes("onbranch", "dir/*", false); } void test_config_conditionals__empty(void) { git_buf value = GIT_BUF_INIT; git_str buf = GIT_STR_INIT; git_config *cfg; cl_git_pass(git_str_puts(&buf, "[includeIf]\n")); cl_git_pass(git_str_puts(&buf, "path = other\n")); cl_git_mkfile("empty_standard_repo/.git/config", buf.ptr); cl_git_mkfile("empty_standard_repo/.git/other", "[foo]\nbar=baz\n"); _repo = cl_git_sandbox_reopen(); git_str_dispose(&buf); cl_git_pass(git_repository_config(&cfg, _repo)); cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string_buf(&value, cfg, "foo.bar")); git_str_dispose(&buf); git_buf_dispose(&value); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/conditionals.c
#include "clar_libgit2.h" #include "config_backend.h" static git_config_backend *backend; void test_config_memory__initialize(void) { backend = NULL; } void test_config_memory__cleanup(void) { git_config_backend_free(backend); } static void assert_config_contains(git_config_backend *backend, const char *name, const char *value) { git_config_entry *entry; cl_git_pass(git_config_backend_get_string(&entry, backend, name)); cl_assert_equal_s(entry->value, value); } struct expected_entry { const char *name; const char *value; int seen; }; static int contains_all_cb(const git_config_entry *entry, void *payload) { struct expected_entry *entries = (struct expected_entry *) payload; int i; for (i = 0; entries[i].name; i++) { if (strcmp(entries[i].name, entry->name) || strcmp(entries[i].value , entry->value)) continue; if (entries[i].seen) cl_fail("Entry seen more than once"); entries[i].seen = 1; return 0; } cl_fail("Unexpected entry"); return -1; } static void assert_config_contains_all(git_config_backend *backend, struct expected_entry *entries) { int i; cl_git_pass(git_config_backend_foreach(backend, contains_all_cb, entries)); for (i = 0; entries[i].name; i++) cl_assert(entries[i].seen); } static void setup_backend(const char *cfg) { cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg))); cl_git_pass(git_config_backend_open(backend, 0, NULL)); } void test_config_memory__write_operations_fail(void) { setup_backend(""); cl_git_fail(git_config_backend_set_string(backend, "general.foo", "var")); cl_git_fail(git_config_backend_delete(backend, "general.foo")); cl_git_fail(git_config_backend_lock(backend)); cl_git_fail(git_config_backend_unlock(backend, 0)); } void test_config_memory__simple(void) { setup_backend( "[general]\n" "foo=bar\n"); assert_config_contains(backend, "general.foo", "bar"); } void test_config_memory__malformed_fails_to_open(void) { const char *cfg = "[general\n" "foo=bar\n"; cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg))); cl_git_fail(git_config_backend_open(backend, 0, NULL)); } void test_config_memory__multiple_vars(void) { setup_backend( "[general]\n" "foo=bar\n" "key=value\n"); assert_config_contains(backend, "general.foo", "bar"); assert_config_contains(backend, "general.key", "value"); } void test_config_memory__multiple_sections(void) { setup_backend( "[general]\n" "foo=bar\n" "\n" "[other]\n" "key=value\n"); assert_config_contains(backend, "general.foo", "bar"); assert_config_contains(backend, "other.key", "value"); } void test_config_memory__multivar_gets_correct_string(void) { setup_backend( "[general]\n" "foo=bar1\n" "foo=bar2\n"); assert_config_contains(backend, "general.foo", "bar2"); } void test_config_memory__foreach_sees_multivar(void) { struct expected_entry entries[] = { { "general.foo", "bar1", 0 }, { "general.foo", "bar2", 0 }, { NULL, NULL, 0 }, }; setup_backend( "[general]\n" "foo=bar1\n" "foo=bar2\n"); assert_config_contains_all(backend, entries); }
libgit2-main
tests/libgit2/config/memory.c
#include "clar_libgit2.h" #include "config.h" static git_repository *g_repo = NULL; static git_config *g_config = NULL; void test_config_rename__initialize(void) { g_repo = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_config(&g_config, g_repo)); } void test_config_rename__cleanup(void) { git_config_free(g_config); g_config = NULL; cl_git_sandbox_cleanup(); g_repo = NULL; } void test_config_rename__can_rename(void) { git_config_entry *ce; cl_git_pass(git_config_get_entry( &ce, g_config, "branch.track-local.remote")); cl_assert_equal_s(".", ce->value); git_config_entry_free(ce); cl_git_fail(git_config_get_entry( &ce, g_config, "branch.local-track.remote")); cl_git_pass(git_config_rename_section( g_repo, "branch.track-local", "branch.local-track")); cl_git_pass(git_config_get_entry( &ce, g_config, "branch.local-track.remote")); cl_assert_equal_s(".", ce->value); git_config_entry_free(ce); cl_git_fail(git_config_get_entry( &ce, g_config, "branch.track-local.remote")); } void test_config_rename__prevent_overwrite(void) { git_config_entry *ce; cl_git_pass(git_config_set_string( g_config, "branch.local-track.remote", "yellow")); cl_git_pass(git_config_get_entry( &ce, g_config, "branch.local-track.remote")); cl_assert_equal_s("yellow", ce->value); git_config_entry_free(ce); cl_git_pass(git_config_rename_section( g_repo, "branch.track-local", "branch.local-track")); cl_git_pass(git_config_get_entry( &ce, g_config, "branch.local-track.remote")); cl_assert_equal_s(".", ce->value); git_config_entry_free(ce); /* so, we don't currently prevent overwrite... */ /* { const git_error *err; cl_assert((err = git_error_last()) != NULL); cl_assert(err->message != NULL); } */ } static void assert_invalid_config_section_name( git_repository *repo, const char *name) { cl_git_fail_with( git_config_rename_section(repo, "branch.remoteless", name), GIT_EINVALIDSPEC); } void test_config_rename__require_a_valid_new_name(void) { assert_invalid_config_section_name(g_repo, ""); assert_invalid_config_section_name(g_repo, "bra\nch"); assert_invalid_config_section_name(g_repo, "branc#"); assert_invalid_config_section_name(g_repo, "bra\nch.duh"); assert_invalid_config_section_name(g_repo, "branc#.duh"); }
libgit2-main
tests/libgit2/config/rename.c
#include "clar_libgit2.h" #include "config.h" static git_config *cfg; void test_config_validkeyname__initialize(void) { cl_fixture_sandbox("config/config10"); cl_git_pass(git_config_open_ondisk(&cfg, "config10")); } void test_config_validkeyname__cleanup(void) { git_config_free(cfg); cfg = NULL; cl_fixture_cleanup("config10"); } static void assert_invalid_config_key_name(const char *name) { git_buf buf = GIT_BUF_INIT; cl_git_fail_with(git_config_get_string_buf(&buf, cfg, name), GIT_EINVALIDSPEC); cl_git_fail_with(git_config_set_string(cfg, name, "42"), GIT_EINVALIDSPEC); cl_git_fail_with(git_config_delete_entry(cfg, name), GIT_EINVALIDSPEC); cl_git_fail_with(git_config_get_multivar_foreach(cfg, name, "*", NULL, NULL), GIT_EINVALIDSPEC); cl_git_fail_with(git_config_set_multivar(cfg, name, "*", "42"), GIT_EINVALIDSPEC); } void test_config_validkeyname__accessing_requires_a_valid_name(void) { assert_invalid_config_key_name(""); assert_invalid_config_key_name("."); assert_invalid_config_key_name(".."); assert_invalid_config_key_name("core."); assert_invalid_config_key_name("d#ff.dirstat.lines"); assert_invalid_config_key_name("diff.dirstat.lines#"); assert_invalid_config_key_name("dif\nf.dirstat.lines"); assert_invalid_config_key_name("dif.dir\nstat.lines"); assert_invalid_config_key_name("dif.dirstat.li\nes"); }
libgit2-main
tests/libgit2/config/validkeyname.c
#include "clar_libgit2.h" #include "fs_path.h" static git_buf buf = GIT_BUF_INIT; void test_config_read__cleanup(void) { git_buf_dispose(&buf); } void test_config_read__simple_read(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config0"))); cl_git_pass(git_config_get_int32(&i, cfg, "core.repositoryformatversion")); cl_assert(i == 0); cl_git_pass(git_config_get_bool(&i, cfg, "core.filemode")); cl_assert(i == 1); cl_git_pass(git_config_get_bool(&i, cfg, "core.bare")); cl_assert(i == 0); cl_git_pass(git_config_get_bool(&i, cfg, "core.logallrefupdates")); cl_assert(i == 1); git_config_free(cfg); } void test_config_read__case_sensitive(void) { git_config *cfg; int i; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config1"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.that.other")); cl_assert_equal_s("true", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.other")); cl_assert_equal_s("yes", buf.ptr); cl_git_pass(git_config_get_bool(&i, cfg, "this.that.other")); cl_assert(i == 1); cl_git_pass(git_config_get_bool(&i, cfg, "this.That.other")); cl_assert(i == 1); /* This one doesn't exist */ cl_must_fail(git_config_get_bool(&i, cfg, "this.thaT.other")); git_config_free(cfg); } /* * If \ is the last non-space character on the line, we read the next * one, separating each line with SP. */ void test_config_read__multiline_value(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config2"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "this.That.and")); cl_assert_equal_s("one one one two two three three", buf.ptr); git_config_free(cfg); } static void clean_test_config(void *unused) { GIT_UNUSED(unused); cl_fixture_cleanup("./testconfig"); } void test_config_read__multiline_value_and_eof(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[header]\n key1 = foo\\\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1")); cl_assert_equal_s("foo", buf.ptr); git_config_free(cfg); } void test_config_read__multiline_eof(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[header]\n key1 = \\\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "header.key1")); cl_assert_equal_s("", buf.ptr); git_config_free(cfg); } /* * This kind of subsection declaration is case-insensitive */ void test_config_read__subsection_header(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config3"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "section.subsection.var")); cl_assert_equal_s("hello", buf.ptr); /* The subsection is transformed to lower-case */ cl_must_fail(git_config_get_string_buf(&buf, cfg, "section.subSectIon.var")); git_config_free(cfg); } void test_config_read__lone_variable(void) { git_config *cfg; int i; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config4"))); cl_git_fail(git_config_get_int32(&i, cfg, "some.section.variable")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variable")); cl_assert_equal_s("", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variable")); cl_assert(i == 1); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.section.variableeq")); cl_assert_equal_s("", buf.ptr); cl_git_pass(git_config_get_bool(&i, cfg, "some.section.variableeq")); cl_assert(i == 0); git_config_free(cfg); } void test_config_read__number_suffixes(void) { git_config *cfg; int64_t i; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config5"))); cl_git_pass(git_config_get_int64(&i, cfg, "number.simple")); cl_assert(i == 1); cl_git_pass(git_config_get_int64(&i, cfg, "number.k")); cl_assert(i == 1 * 1024); cl_git_pass(git_config_get_int64(&i, cfg, "number.kk")); cl_assert(i == 1 * 1024); cl_git_pass(git_config_get_int64(&i, cfg, "number.m")); cl_assert(i == 1 * 1024 * 1024); cl_git_pass(git_config_get_int64(&i, cfg, "number.mm")); cl_assert(i == 1 * 1024 * 1024); cl_git_pass(git_config_get_int64(&i, cfg, "number.g")); cl_assert(i == 1 * 1024 * 1024 * 1024); cl_git_pass(git_config_get_int64(&i, cfg, "number.gg")); cl_assert(i == 1 * 1024 * 1024 * 1024); git_config_free(cfg); } void test_config_read__blank_lines(void) { git_config *cfg; int i; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config6"))); cl_git_pass(git_config_get_bool(&i, cfg, "valid.subsection.something")); cl_assert(i == 1); cl_git_pass(git_config_get_bool(&i, cfg, "something.else.something")); cl_assert(i == 0); git_config_free(cfg); } void test_config_read__invalid_ext_headers(void) { git_config *cfg; cl_must_fail(git_config_open_ondisk(&cfg, cl_fixture("config/config7"))); } void test_config_read__empty_files(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config8"))); git_config_free(cfg); } void test_config_read__symbol_headers(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config20"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "valid.[subsection].something")); cl_assert_equal_s("a", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec.[subsec]/child.parent")); cl_assert_equal_s("grand", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec2.[subsec2]/child2.type")); cl_assert_equal_s("dvcs", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec3.escape\"quote.vcs")); cl_assert_equal_s("git", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "sec4.escaping\\slash.lib")); cl_assert_equal_s("git2", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_read__multiline_multiple_quoted_comment_chars(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config21"))); git_config_free(cfg); } void test_config_read__multiline_multiple_quoted_quote_at_beginning_of_line(void) { git_config* cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config22"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m")); cl_assert_equal_s("cmd ;; ;; bar", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m2")); cl_assert_equal_s("'; ; something '", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_read__header_in_last_line(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config10"))); git_config_free(cfg); } void test_config_read__prefixes(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.ab.url")); cl_assert_equal_s("http://example.com/git/ab", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "remote.abba.url")); cl_assert_equal_s("http://example.com/git/abba", buf.ptr); git_config_free(cfg); } void test_config_read__escaping_quotes(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config13"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.editor")); cl_assert_equal_s("\"C:/Program Files/Nonsense/bah.exe\" \"--some option\"", buf.ptr); git_config_free(cfg); } void test_config_read__invalid_escape_sequence(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[header]\n key1 = \\\\\\;\n key2 = value2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } static int count_cfg_entries_and_compare_levels( const git_config_entry *entry, void *payload) { int *count = payload; if (!strcmp(entry->value, "7") || !strcmp(entry->value, "17")) cl_assert(entry->level == GIT_CONFIG_LEVEL_GLOBAL); else cl_assert(entry->level == GIT_CONFIG_LEVEL_SYSTEM); (*count)++; return 0; } static int cfg_callback_countdown(const git_config_entry *entry, void *payload) { int *count = payload; GIT_UNUSED(entry); (*count)--; if (*count == 0) return -100; return 0; } void test_config_read__foreach(void) { git_config *cfg; int count, ret; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); count = 0; cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count)); cl_assert_equal_i(7, count); count = 3; cl_git_fail(ret = git_config_foreach(cfg, cfg_callback_countdown, &count)); cl_assert_equal_i(-100, ret); git_config_free(cfg); } void test_config_read__iterator(void) { const char *keys[] = { "core.dummy2", "core.verylong", "core.dummy", "remote.ab.url", "remote.abba.url", "core.dummy2", "core.global" }; git_config *cfg; git_config_iterator *iter; git_config_entry *entry; int count, ret; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); count = 0; cl_git_pass(git_config_iterator_new(&iter, cfg)); while ((ret = git_config_next(&entry, iter)) == 0) { cl_assert_equal_s(entry->name, keys[count]); count++; } git_config_iterator_free(iter); cl_assert_equal_i(GIT_ITEROVER, ret); cl_assert_equal_i(7, count); count = 3; cl_git_pass(git_config_iterator_new(&iter, cfg)); git_config_iterator_free(iter); git_config_free(cfg); } static int count_cfg_entries(const git_config_entry *entry, void *payload) { int *count = payload; GIT_UNUSED(entry); (*count)++; return 0; } void test_config_read__foreach_match(void) { git_config *cfg; int count; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); count = 0; cl_git_pass( git_config_foreach_match(cfg, "core.*", count_cfg_entries, &count)); cl_assert_equal_i(3, count); count = 0; cl_git_pass( git_config_foreach_match(cfg, "remote\\.ab.*", count_cfg_entries, &count)); cl_assert_equal_i(2, count); count = 0; cl_git_pass( git_config_foreach_match(cfg, ".*url$", count_cfg_entries, &count)); cl_assert_equal_i(2, count); count = 0; cl_git_pass( git_config_foreach_match(cfg, ".*dummy.*", count_cfg_entries, &count)); cl_assert_equal_i(2, count); count = 0; cl_git_pass( git_config_foreach_match(cfg, ".*nomatch.*", count_cfg_entries, &count)); cl_assert_equal_i(0, count); git_config_free(cfg); } static void check_glob_iter(git_config *cfg, const char *regexp, int expected) { git_config_iterator *iter; git_config_entry *entry; int count, error; cl_git_pass(git_config_iterator_glob_new(&iter, cfg, regexp)); count = 0; while ((error = git_config_next(&entry, iter)) == 0) count++; cl_assert_equal_i(GIT_ITEROVER, error); cl_assert_equal_i(expected, count); git_config_iterator_free(iter); } void test_config_read__iterator_invalid_glob(void) { git_config *cfg; git_config_iterator *iter; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); cl_git_fail(git_config_iterator_glob_new(&iter, cfg, "*")); git_config_free(cfg); } void test_config_read__iterator_glob(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config9"))); check_glob_iter(cfg, "core.*", 3); check_glob_iter(cfg, "remote\\.ab.*", 2); check_glob_iter(cfg, ".*url$", 2); check_glob_iter(cfg, ".*dummy.*", 2); check_glob_iter(cfg, ".*nomatch.*", 0); git_config_free(cfg); } void test_config_read__whitespace_not_required_around_assignment(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config14"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "a.b")); cl_assert_equal_s("c", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "d.e")); cl_assert_equal_s("f", buf.ptr); git_config_free(cfg); } void test_config_read__read_git_config_entry(void) { git_config *cfg; git_config_entry *entry; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_get_entry(&entry, cfg, "core.dummy2")); cl_assert_equal_s("core.dummy2", entry->name); cl_assert_equal_s("42", entry->value); cl_assert_equal_i(GIT_CONFIG_LEVEL_SYSTEM, entry->level); git_config_entry_free(entry); git_config_free(cfg); } /* * At the beginning of the test: * - config9 has: core.dummy2=42 * - config15 has: core.dummy2=7 * - config16 has: core.dummy2=28 */ void test_config_read__local_config_overrides_global_config_overrides_system_config(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); cl_assert_equal_i(28, i); git_config_free(cfg); cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); cl_assert_equal_i(7, i); git_config_free(cfg); } /* * At the beginning of the test: * - config9 has: core.global does not exist * - config15 has: core.global=17 * - config16 has: core.global=29 * * And also: * - config9 has: core.system does not exist * - config15 has: core.system does not exist * - config16 has: core.system=11 */ void test_config_read__fallback_from_local_to_global_and_from_global_to_system(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_get_int32(&i, cfg, "core.global")); cl_assert_equal_i(17, i); cl_git_pass(git_config_get_int32(&i, cfg, "core.system")); cl_assert_equal_i(11, i); git_config_free(cfg); } void test_config_read__parent_dir_is_file(void) { git_config *cfg; int count; cl_git_pass(git_config_new(&cfg)); /* * Verify we can add non-existing files when the parent directory is not * a directory. */ cl_git_pass(git_config_add_file_ondisk(cfg, "/dev/null/.gitconfig", GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); count = 0; cl_git_pass(git_config_foreach(cfg, count_cfg_entries_and_compare_levels, &count)); cl_assert_equal_i(0, count); git_config_free(cfg); } /* * At the beginning of the test, config18 has: * int32global = 28 * int64global = 9223372036854775803 * boolglobal = true * stringglobal = I'm a global config value! * * And config19 has: * int32global = -1 * int64global = -2 * boolglobal = false * stringglobal = don't find me! * */ void test_config_read__simple_read_from_specific_level(void) { git_config *cfg, *cfg_specific; int i; int64_t l, expected = +9223372036854775803; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), GIT_CONFIG_LEVEL_SYSTEM, NULL, 0)); cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_get_int32(&i, cfg_specific, "core.int32global")); cl_assert_equal_i(28, i); cl_git_pass(git_config_get_int64(&l, cfg_specific, "core.int64global")); cl_assert(l == expected); cl_git_pass(git_config_get_bool(&i, cfg_specific, "core.boolglobal")); cl_assert_equal_b(true, i); cl_git_pass(git_config_get_string_buf(&buf, cfg_specific, "core.stringglobal")); cl_assert_equal_s("I'm a global config value!", buf.ptr); git_config_free(cfg_specific); git_config_free(cfg); } void test_config_read__can_load_and_parse_an_empty_config_file(void) { git_config *cfg; int i; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", ""); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_int32(&i, cfg, "nope.neither")); git_config_free(cfg); } void test_config_read__corrupt_header(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[sneaky ] \"quoted closing quote mark\\\""); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__corrupt_header2(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[unclosed \"bracket\"\n lib = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__corrupt_header3(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[unclosed \"slash\\\"]\n lib = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__invalid_key_chars(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[foo]\n has_underscore = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_rewritefile("./testconfig", "[foo]\n has/slash = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_rewritefile("./testconfig", "[foo]\n has+plus = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_rewritefile("./testconfig", "[no_key]\n = git2\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__lone_variable_with_trailing_whitespace(void) { git_config *cfg; int b; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[foo]\n lonevariable \n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_bool(&b, cfg, "foo.lonevariable")); cl_assert_equal_b(true, b); git_config_free(cfg); } void test_config_read__override_variable(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some] var = one\nvar = two"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s("two", buf.ptr); git_config_free(cfg); } void test_config_read__path(void) { git_config *cfg; git_buf path = GIT_BUF_INIT; git_buf old_path = GIT_BUF_INIT; git_str home_path = GIT_STR_INIT; git_str expected_path = GIT_STR_INIT; cl_git_pass(p_mkdir("fakehome", 0777)); cl_git_pass(git_fs_path_prettify(&home_path, "fakehome", NULL)); cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &old_path)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, home_path.ptr)); cl_git_mkfile("./testconfig", "[some]\n path = ~/somefile"); cl_git_pass(git_fs_path_join_unrooted(&expected_path, "somefile", home_path.ptr, NULL)); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~/"); cl_git_pass(git_fs_path_join_unrooted(&expected_path, "", home_path.ptr, NULL)); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~"); cl_git_pass(git_str_sets(&expected_path, home_path.ptr)); cl_git_pass(git_config_get_path(&path, cfg, "some.path")); cl_assert_equal_s(expected_path.ptr, path.ptr); git_buf_dispose(&path); cl_git_mkfile("./testconfig", "[some]\n path = ~user/foo"); cl_git_fail(git_config_get_path(&path, cfg, "some.path")); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, old_path.ptr)); git_buf_dispose(&old_path); git_str_dispose(&home_path); git_str_dispose(&expected_path); git_config_free(cfg); } void test_config_read__crlf_style_line_endings(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); git_buf_dispose(&buf); } void test_config_read__trailing_crlf(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some]\r\n var = value\r\n\r\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); git_buf_dispose(&buf); } void test_config_read__bom(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some]\n var = value\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); git_buf_dispose(&buf); } void test_config_read__arbitrary_whitespace_before_subsection(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some \t \"subsection\"]\n var = value\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.subsection.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); git_buf_dispose(&buf); } void test_config_read__no_whitespace_after_subsection(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some \"subsection\" ]\n var = value\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__invalid_space_section(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some section]\n var = value\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__invalid_quoted_first_section(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[\"some\"]\n var = value\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__invalid_unquoted_subsection(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub section]\n var = value\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__invalid_quoted_third_section(void) { git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "\xEF\xBB\xBF[some sub \"section\"]\n var = value\n"); cl_git_fail(git_config_open_ondisk(&cfg, "./testconfig")); git_config_free(cfg); } void test_config_read__unreadable_file_ignored(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; int ret; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value"); cl_git_pass(p_chmod("./testconfig", 0)); ret = git_config_open_ondisk(&cfg, "./test/config"); cl_assert(ret == 0 || ret == GIT_ENOTFOUND); git_config_free(cfg); git_buf_dispose(&buf); } void test_config_read__single_line(void) { git_buf buf = GIT_BUF_INIT; git_config *cfg; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"] var = value"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s(buf.ptr, "value"); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.OtheR.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); cl_git_mkfile("./testconfig", "[some] var = value\n[some \"OtheR\"]var = value"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.var")); cl_assert_equal_s(buf.ptr, "value"); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "some.OtheR.var")); cl_assert_equal_s(buf.ptr, "value"); git_config_free(cfg); git_buf_dispose(&buf); } static int read_nosection_cb(const git_config_entry *entry, void *payload) { int *seen = (int*)payload; if (strcmp(entry->name, "key") == 0) { (*seen)++; } return 0; } /* This would ideally issue a warning, if we had a way to do so. */ void test_config_read__nosection(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; int seen = 0; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-nosection"))); /* * Given a key with no section, we do not allow reading it, * but we do include it in an iteration over the config * store. This appears to match how git's own APIs (and * git-config(1)) behave. */ cl_git_fail_with(git_config_get_string_buf(&buf, cfg, "key"), GIT_EINVALIDSPEC); cl_git_pass(git_config_foreach(cfg, read_nosection_cb, &seen)); cl_assert_equal_i(seen, 1); git_buf_dispose(&buf); git_config_free(cfg); } enum { MAP_TRUE = 0, MAP_FALSE = 1, MAP_ALWAYS = 2 }; static git_configmap _test_map1[] = { {GIT_CONFIGMAP_STRING, "always", MAP_ALWAYS}, {GIT_CONFIGMAP_FALSE, NULL, MAP_FALSE}, {GIT_CONFIGMAP_TRUE, NULL, MAP_TRUE}, }; static git_configmap _test_map2[] = { {GIT_CONFIGMAP_INT32, NULL, 0}, }; void test_config_read__get_mapped(void) { git_config *cfg; int val; int known_good; cl_set_cleanup(&clean_test_config, NULL); cl_git_mkfile("./testconfig", "[header]\n" " key1 = 1\n" " key2 = true\n" " key3\n" " key4 = always\n" " key5 = false\n" " key6 = 0\n" " key7 = never\n" " key8 = On\n" " key9 = off\n"); cl_git_pass(git_config_open_ondisk(&cfg, "./testconfig")); /* check parsing bool and string */ cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_TRUE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key2", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_TRUE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key3", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_TRUE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key8", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_TRUE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key4", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_ALWAYS); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key5", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_FALSE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_FALSE); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key9", _test_map1, ARRAY_SIZE(_test_map1))); cl_assert_equal_i(val, MAP_FALSE); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map1, ARRAY_SIZE(_test_map1))); /* check parsing int values */ cl_git_pass(git_config_get_mapped(&val, cfg, "header.key1", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key1")); cl_assert_equal_i(val, known_good); cl_git_pass(git_config_get_mapped(&val, cfg, "header.key6", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_pass(git_config_get_int32(&known_good, cfg, "header.key6")); cl_assert_equal_i(val, known_good); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key2", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key3", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key4", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key5", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key7", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key8", _test_map2, ARRAY_SIZE(_test_map2))); cl_git_fail(git_config_get_mapped(&val, cfg, "header.key9", _test_map2, ARRAY_SIZE(_test_map2))); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/read.c
#include "clar_libgit2.h" #include "config_backend.h" static git_config *cfg; static git_config *snapshot; void test_config_snapshot__cleanup(void) { git_config_free(cfg); cfg = NULL; git_config_free(snapshot); snapshot = NULL; } void test_config_snapshot__create_snapshot(void) { int32_t i; cl_git_mkfile("config", "[old]\nvalue = 5\n"); cl_git_pass(git_config_open_ondisk(&cfg, "config")); cl_git_pass(git_config_get_int32(&i, cfg, "old.value")); cl_assert_equal_i(5, i); cl_git_pass(git_config_snapshot(&snapshot, cfg)); /* Change the value on the file itself (simulate external process) */ cl_git_mkfile("config", "[old]\nvalue = 56\n"); cl_git_pass(git_config_get_int32(&i, cfg, "old.value")); cl_assert_equal_i(56, i); cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); cl_assert_equal_i(5, i); /* Change the value on the file itself (simulate external process) */ cl_git_mkfile("config", "[old]\nvalue = 999\n"); /* Old snapshot should still have the old value */ cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); cl_assert_equal_i(5, i); /* New snapshot should see new value */ git_config_free(snapshot); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); cl_assert_equal_i(999, i); cl_git_pass(p_unlink("config")); } static int count_me(const git_config_entry *entry, void *payload) { int *n = (int *) payload; GIT_UNUSED(entry); (*n)++; return 0; } void test_config_snapshot__multivar(void) { int count; count = 0; cl_git_mkfile("config", "[old]\nvalue = 5\nvalue = 6\n"); cl_git_pass(git_config_open_ondisk(&cfg, "config")); cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count)); cl_assert_equal_i(2, count); count = 0; cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count)); cl_assert_equal_i(2, count); cl_git_pass(p_unlink("config")); } void test_config_snapshot__includes(void) { int i; cl_git_mkfile("including", "[include]\npath = included"); cl_git_mkfile("included", "[section]\nkey = 1\n"); cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); cl_assert_equal_i(i, 1); /* Rewrite "included" config */ cl_git_mkfile("included", "[section]\nkey = 11\n"); /* Assert that the live config changed, but snapshot remained the same */ cl_git_pass(git_config_get_int32(&i, cfg, "section.key")); cl_assert_equal_i(i, 11); cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); cl_assert_equal_i(i, 1); cl_git_pass(p_unlink("including")); cl_git_pass(p_unlink("included")); } void test_config_snapshot__snapshot(void) { git_config *snapshot_snapshot; int i; cl_git_mkfile("configfile", "[section]\nkey = 1\n"); cl_git_pass(git_config_open_ondisk(&cfg, "configfile")); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_snapshot(&snapshot_snapshot, snapshot)); cl_git_pass(git_config_get_int32(&i, snapshot_snapshot, "section.key")); cl_assert_equal_i(i, 1); git_config_free(snapshot_snapshot); cl_git_pass(p_unlink("configfile")); } void test_config_snapshot__snapshot_from_in_memony(void) { const char *configuration = "[section]\nkey = 1\n"; git_config_backend *backend; int i; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_backend_from_string(&backend, configuration, strlen(configuration))); cl_git_pass(git_config_add_backend(cfg, backend, 0, NULL, 0)); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); cl_assert_equal_i(i, 1); }
libgit2-main
tests/libgit2/config/snapshot.c
#include "clar_libgit2.h" static const char *_name = "remote.ab.url"; void test_config_multivar__initialize(void) { cl_fixture_sandbox("config"); } void test_config_multivar__cleanup(void) { cl_fixture_cleanup("config"); } static int mv_read_cb(const git_config_entry *entry, void *data) { int *n = (int *) data; if (!strcmp(entry->name, _name)) (*n)++; return 0; } void test_config_multivar__foreach(void) { git_config *cfg; int n = 0; cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config11"))); cl_git_pass(git_config_foreach(cfg, mv_read_cb, &n)); cl_assert(n == 2); git_config_free(cfg); } static int cb(const git_config_entry *entry, void *data) { int *n = (int *) data; GIT_UNUSED(entry); (*n)++; return 0; } static void check_get_multivar_foreach( git_config *cfg, int expected, int expected_patterned) { int n = 0; if (expected > 0) { cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(expected, n); } else { cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); } n = 0; if (expected_patterned > 0) { cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "example", cb, &n)); cl_assert_equal_i(expected_patterned, n); } else { cl_assert_equal_i(GIT_ENOTFOUND, git_config_get_multivar_foreach(cfg, _name, "example", cb, &n)); } } static void check_get_multivar(git_config *cfg, int expected) { git_config_iterator *iter; git_config_entry *entry; int n = 0; cl_git_pass(git_config_multivar_iterator_new(&iter, cfg, _name, NULL)); while (git_config_next(&entry, iter) == 0) n++; cl_assert_equal_i(expected, n); git_config_iterator_free(iter); } void test_config_multivar__get(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); check_get_multivar_foreach(cfg, 2, 1); /* add another that has the _name entry */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config9", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1)); check_get_multivar_foreach(cfg, 3, 2); /* add another that does not have the _name entry */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config0", GIT_CONFIG_LEVEL_GLOBAL, NULL, 1)); check_get_multivar_foreach(cfg, 3, 2); /* add another that does not have the _name entry at the end */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config1", GIT_CONFIG_LEVEL_APP, NULL, 1)); check_get_multivar_foreach(cfg, 3, 2); /* drop original file */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config2", GIT_CONFIG_LEVEL_LOCAL, NULL, 1)); check_get_multivar_foreach(cfg, 1, 1); /* drop other file with match */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config3", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1)); check_get_multivar_foreach(cfg, 0, 0); /* reload original file (add different place in order) */ cl_git_pass(git_config_add_file_ondisk(cfg, "config/config11", GIT_CONFIG_LEVEL_SYSTEM, NULL, 1)); check_get_multivar_foreach(cfg, 2, 1); check_get_multivar(cfg, 2); git_config_free(cfg); } void test_config_multivar__add(void) { git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); cl_git_pass(git_config_set_multivar(cfg, _name, "non-existent", "git://git.otherplace.org/libgit2")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(n, 3); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n)); cl_assert_equal_i(n, 1); git_config_free(cfg); /* We know it works in memory, let's see if the file is written correctly */ cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(n, 3); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n)); cl_assert_equal_i(n, 1); git_config_free(cfg); } void test_config_multivar__add_new(void) { const char *var = "a.brand.new"; git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); cl_git_pass(git_config_set_multivar(cfg, var, "$^", "variable")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, var, NULL, cb, &n)); cl_assert_equal_i(n, 1); git_config_free(cfg); } void test_config_multivar__replace(void) { git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert(n == 2); cl_git_pass(git_config_set_multivar(cfg, _name, "github", "git://git.otherplace.org/libgit2")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert(n == 2); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert(n == 2); git_config_free(cfg); } void test_config_multivar__replace_multiple(void) { git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); cl_git_pass(git_config_set_multivar(cfg, _name, "git://", "git://git.otherplace.org/libgit2")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n)); cl_assert_equal_i(n, 2); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, "otherplace", cb, &n)); cl_assert_equal_i(n, 2); git_config_free(cfg); } void test_config_multivar__delete(void) { git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(2, n); cl_git_pass(git_config_delete_multivar(cfg, _name, "github")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(1, n); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert_equal_i(1, n); git_config_free(cfg); } void test_config_multivar__delete_multiple(void) { git_config *cfg; int n; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_pass(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n)); cl_assert(n == 2); cl_git_pass(git_config_delete_multivar(cfg, _name, "git")); n = 0; cl_git_fail_with(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n), GIT_ENOTFOUND); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); n = 0; cl_git_fail_with(git_config_get_multivar_foreach(cfg, _name, NULL, cb, &n), GIT_ENOTFOUND); git_config_free(cfg); } void test_config_multivar__delete_notfound(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, "config/config11")); cl_git_fail_with(git_config_delete_multivar(cfg, "remote.ab.noturl", "git"), GIT_ENOTFOUND); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/multivar.c
#include "clar_libgit2.h" #include "config_backend.h" #include "config.h" #include "path.h" static git_config *cfg; void test_config_readonly__initialize(void) { cl_git_pass(git_config_new(&cfg)); } void test_config_readonly__cleanup(void) { git_config_free(cfg); cfg = NULL; } void test_config_readonly__writing_to_readonly_fails(void) { git_config_backend *backend; cl_git_pass(git_config_backend_from_file(&backend, "global")); backend->readonly = 1; cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_fail_with(GIT_ENOTFOUND, git_config_set_string(cfg, "foo.bar", "baz")); cl_assert(!git_fs_path_exists("global")); } void test_config_readonly__writing_to_cfg_with_rw_precedence_succeeds(void) { git_config_backend *backend; cl_git_pass(git_config_backend_from_file(&backend, "global")); backend->readonly = 1; cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_backend_from_file(&backend, "local")); cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz")); cl_assert(git_fs_path_exists("local")); cl_assert(!git_fs_path_exists("global")); cl_git_pass(p_unlink("local")); } void test_config_readonly__writing_to_cfg_with_ro_precedence_succeeds(void) { git_config_backend *backend; cl_git_pass(git_config_backend_from_file(&backend, "local")); backend->readonly = 1; cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_backend_from_file(&backend, "global")); cl_git_pass(git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_set_string(cfg, "foo.bar", "baz")); cl_assert(!git_fs_path_exists("local")); cl_assert(git_fs_path_exists("global")); cl_git_pass(p_unlink("global")); }
libgit2-main
tests/libgit2/config/readonly.c
#include "clar_libgit2.h" void test_config_add__initialize(void) { cl_fixture_sandbox("config/config10"); } void test_config_add__cleanup(void) { cl_fixture_cleanup("config10"); } void test_config_add__to_existing_section(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_open_ondisk(&cfg, "config10")); cl_git_pass(git_config_set_int32(cfg, "empty.tmp", 5)); cl_git_pass(git_config_get_int32(&i, cfg, "empty.tmp")); cl_assert(i == 5); cl_git_pass(git_config_delete_entry(cfg, "empty.tmp")); git_config_free(cfg); } void test_config_add__to_new_section(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_open_ondisk(&cfg, "config10")); cl_git_pass(git_config_set_int32(cfg, "section.tmp", 5)); cl_git_pass(git_config_get_int32(&i, cfg, "section.tmp")); cl_assert(i == 5); cl_git_pass(git_config_delete_entry(cfg, "section.tmp")); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/add.c
#include "clar_libgit2.h" void test_config_configlevel__adding_the_same_level_twice_returns_EEXISTS(void) { int error; git_config *cfg; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config9"), GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config15"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); error = git_config_add_file_ondisk(cfg, cl_fixture("config/config16"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0); cl_git_fail(error); cl_assert_equal_i(GIT_EEXISTS, error); git_config_free(cfg); } void test_config_configlevel__can_replace_a_config_file_at_an_existing_level(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), GIT_CONFIG_LEVEL_LOCAL, NULL, 1)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), GIT_CONFIG_LEVEL_LOCAL, NULL, 1)); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.stringglobal")); cl_assert_equal_s("don't find me!", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_configlevel__can_read_from_a_single_level_focused_file_after_parent_config_has_been_freed(void) { git_config *cfg; git_config *single_level_cfg; git_buf buf = {0}; cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config18"), GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, cl_fixture("config/config19"), GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_open_level(&single_level_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL)); git_config_free(cfg); cl_git_pass(git_config_get_string_buf(&buf, single_level_cfg, "core.stringglobal")); cl_assert_equal_s("don't find me!", buf.ptr); git_buf_dispose(&buf); git_config_free(single_level_cfg); } void test_config_configlevel__fetching_a_level_from_an_empty_compound_config_returns_ENOTFOUND(void) { git_config *cfg; git_config *local_cfg; cl_git_pass(git_config_new(&cfg)); cl_assert_equal_i(GIT_ENOTFOUND, git_config_open_level(&local_cfg, cfg, GIT_CONFIG_LEVEL_LOCAL)); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/configlevel.c
#include "clar_libgit2.h" #include "futils.h" static git_config *cfg; static git_buf buf; void test_config_include__initialize(void) { cfg = NULL; memset(&buf, 0, sizeof(git_buf)); } void test_config_include__cleanup(void) { git_config_free(cfg); git_buf_dispose(&buf); } void test_config_include__relative(void) { cl_git_pass(git_config_open_ondisk(&cfg, cl_fixture("config/config-include"))); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz")); cl_assert_equal_s("huzzah", buf.ptr); } void test_config_include__absolute(void) { git_str cfgdata = GIT_STR_INIT; cl_git_pass(git_str_printf(&cfgdata, "[include]\npath = %s/config-included", cl_fixture("config"))); cl_git_mkfile("config-include-absolute", cfgdata.ptr); git_str_dispose(&cfgdata); cl_git_pass(git_config_open_ondisk(&cfg, "config-include-absolute")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz")); cl_assert_equal_s("huzzah", buf.ptr); cl_git_pass(p_unlink("config-include-absolute")); } void test_config_include__homedir(void) { cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config"))); cl_git_mkfile("config-include-homedir", "[include]\npath = ~/config-included"); cl_git_pass(git_config_open_ondisk(&cfg, "config-include-homedir")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz")); cl_assert_equal_s("huzzah", buf.ptr); cl_sandbox_set_search_path_defaults(); cl_git_pass(p_unlink("config-include-homedir")); } /* We need to pretend that the variables were defined where the file was included */ void test_config_include__ordering(void) { cl_git_mkfile("included", "[foo \"bar\"]\nbaz = hurrah\nfrotz = hiya"); cl_git_mkfile("including", "[foo \"bar\"]\nfrotz = hello\n" "[include]\npath = included\n" "[foo \"bar\"]\nbaz = huzzah\n"); cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.frotz")); cl_assert_equal_s("hiya", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar.baz")); cl_assert_equal_s("huzzah", buf.ptr); cl_git_pass(p_unlink("included")); cl_git_pass(p_unlink("including")); } /* We need to pretend that the variables were defined where the file was included */ void test_config_include__depth(void) { cl_git_mkfile("a", "[include]\npath = b"); cl_git_mkfile("b", "[include]\npath = a"); cl_git_fail(git_config_open_ondisk(&cfg, "a")); cl_git_pass(p_unlink("a")); cl_git_pass(p_unlink("b")); } void test_config_include__empty_path_sanely_handled(void) { cl_git_mkfile("a", "[include]\npath"); cl_git_pass(git_config_open_ondisk(&cfg, "a")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "include.path")); cl_assert_equal_s("", buf.ptr); cl_git_pass(p_unlink("a")); } void test_config_include__missing(void) { cl_git_mkfile("including", "[include]\npath = nonexistentfile\n[foo]\nbar = baz"); git_error_clear(); cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_assert(git_error_last() == NULL); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_assert_equal_s("baz", buf.ptr); cl_git_pass(p_unlink("including")); } void test_config_include__missing_homedir(void) { cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, cl_fixture("config"))); cl_git_mkfile("including", "[include]\npath = ~/.nonexistentfile\n[foo]\nbar = baz"); git_error_clear(); cl_git_pass(git_config_open_ondisk(&cfg, "including")); cl_assert(git_error_last() == NULL); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_assert_equal_s("baz", buf.ptr); cl_sandbox_set_search_path_defaults(); cl_git_pass(p_unlink("including")); } #define replicate10(s) s s s s s s s s s s void test_config_include__depth2(void) { const char *content = "[include]\n" replicate10(replicate10("path=bottom\n")); cl_git_mkfile("top-level", "[include]\npath = middle\n[foo]\nbar = baz"); cl_git_mkfile("middle", content); cl_git_mkfile("bottom", "[foo]\nbar2 = baz2"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_assert_equal_s("baz", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar2")); cl_assert_equal_s("baz2", buf.ptr); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("middle")); cl_git_pass(p_unlink("bottom")); } void test_config_include__removing_include_removes_values(void) { cl_git_mkfile("top-level", "[include]\npath = included"); cl_git_mkfile("included", "[foo]\nbar = value"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_mkfile("top-level", ""); cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); } void test_config_include__rewriting_include_refreshes_values(void) { cl_git_mkfile("top-level", "[include]\npath = first\n[include]\npath = second"); cl_git_mkfile("first", "[first]\nfoo = bar"); cl_git_mkfile("second", "[second]\nfoo = bar"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_mkfile("first", "[first]\nother = value"); cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "first.other")); cl_assert_equal_s(buf.ptr, "value"); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("first")); cl_git_pass(p_unlink("second")); } void test_config_include__rewriting_include_twice_refreshes_values(void) { cl_git_mkfile("top-level", "[include]\npath = included"); cl_git_mkfile("included", "[foo]\nbar = first-value"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.bar")); git_buf_dispose(&buf); cl_git_mkfile("included", "[foo]\nother = value2"); cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.bar")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.other")); cl_assert_equal_s(buf.ptr, "value2"); git_buf_dispose(&buf); cl_git_mkfile("included", "[foo]\nanother = bar"); cl_git_fail(git_config_get_string_buf(&buf, cfg, "foo.other")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "foo.another")); cl_assert_equal_s(buf.ptr, "bar"); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); } void test_config_include__included_variables_cannot_be_deleted(void) { cl_git_mkfile("top-level", "[include]\npath = included\n"); cl_git_mkfile("included", "[foo]\nbar = value"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_fail(git_config_delete_entry(cfg, "foo.bar")); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); } void test_config_include__included_variables_cannot_be_modified(void) { cl_git_mkfile("top-level", "[include]\npath = included\n"); cl_git_mkfile("included", "[foo]\nbar = value"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_fail(git_config_set_string(cfg, "foo.bar", "other-value")); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); } void test_config_include__variables_in_included_override_including(void) { int i; cl_git_mkfile("top-level", "[foo]\nbar = 1\n[include]\npath = included"); cl_git_mkfile("included", "[foo]\nbar = 2"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar")); cl_assert_equal_i(i, 2); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); } void test_config_include__variables_in_including_override_included(void) { int i; cl_git_mkfile("top-level", "[include]\npath = included\n[foo]\nbar = 1"); cl_git_mkfile("included", "[foo]\nbar = 2"); cl_git_pass(git_config_open_ondisk(&cfg, "top-level")); cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar")); cl_assert_equal_i(i, 1); cl_git_pass(p_unlink("top-level")); cl_git_pass(p_unlink("included")); }
libgit2-main
tests/libgit2/config/include.c
#include "clar_libgit2.h" #include "config_helpers.h" #include "repository.h" void assert_config_entry_existence( git_repository *repo, const char *name, bool is_supposed_to_exist) { git_config *config; git_config_entry *entry = NULL; int result; cl_git_pass(git_repository_config__weakptr(&config, repo)); result = git_config_get_entry(&entry, config, name); git_config_entry_free(entry); if (is_supposed_to_exist) cl_git_pass(result); else cl_assert_equal_i(GIT_ENOTFOUND, result); } void assert_config_entry_value( git_repository *repo, const char *name, const char *expected_value) { git_config *config; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_repository_config__weakptr(&config, repo)); cl_git_pass(git_config_get_string_buf(&buf, config, name)); cl_assert_equal_s(expected_value, buf.ptr); git_buf_dispose(&buf); } static int count_config_entries_cb( const git_config_entry *entry, void *payload) { int *how_many = (int *)payload; GIT_UNUSED(entry); (*how_many)++; return 0; } int count_config_entries_match(git_repository *repo, const char *pattern) { git_config *config; int how_many = 0; cl_git_pass(git_repository_config(&config, repo)); cl_assert_equal_i(0, git_config_foreach_match( config, pattern, count_config_entries_cb, &how_many)); git_config_free(config); return how_many; }
libgit2-main
tests/libgit2/config/config_helpers.c
#include "clar_libgit2.h" #include "futils.h" void test_config_global__initialize(void) { git_str path = GIT_STR_INIT; cl_git_pass(git_futils_mkdir_r("home", 0777)); cl_git_pass(git_fs_path_prettify(&path, "home", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr)); cl_git_pass(git_futils_mkdir_r("xdg/git", 0777)); cl_git_pass(git_fs_path_prettify(&path, "xdg/git", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr)); cl_git_pass(git_futils_mkdir_r("etc", 0777)); cl_git_pass(git_fs_path_prettify(&path, "etc", NULL)); cl_git_pass(git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr)); git_str_dispose(&path); } void test_config_global__cleanup(void) { cl_sandbox_set_search_path_defaults(); cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES)); cl_git_pass(git_futils_rmdir_r("xdg", NULL, GIT_RMDIR_REMOVE_FILES)); cl_git_pass(git_futils_rmdir_r("etc", NULL, GIT_RMDIR_REMOVE_FILES)); } void test_config_global__open_global(void) { git_config *cfg, *global, *selected, *dummy; int32_t value; cl_git_mkfile("home/.gitconfig", "[global]\n test = 4567\n"); cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_get_int32(&value, cfg, "global.test")); cl_assert_equal_i(4567, value); cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_get_int32(&value, global, "global.test")); cl_assert_equal_i(4567, value); cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG)); cl_git_pass(git_config_open_global(&selected, cfg)); cl_git_pass(git_config_get_int32(&value, selected, "global.test")); cl_assert_equal_i(4567, value); git_config_free(selected); git_config_free(global); git_config_free(cfg); } void test_config_global__open_symlinked_global(void) { #ifndef GIT_WIN32 git_config *cfg; int32_t value; cl_git_mkfile("home/.gitconfig.linked", "[global]\n test = 4567\n"); cl_must_pass(symlink(".gitconfig.linked", "home/.gitconfig")); cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_get_int32(&value, cfg, "global.test")); cl_assert_equal_i(4567, value); git_config_free(cfg); #endif } void test_config_global__lock_missing_global_config(void) { git_config *cfg; git_config_entry *entry; git_transaction *transaction; (void)p_unlink("home/.gitconfig"); /* No global config */ cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_lock(&transaction, cfg)); cl_git_pass(git_config_set_string(cfg, "assertion.fail", "boom")); cl_git_pass(git_transaction_commit(transaction)); git_transaction_free(transaction); /* cfg is updated */ cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail")); cl_assert_equal_s("boom", entry->value); git_config_entry_free(entry); git_config_free(cfg); /* We can reread the new value from the global config */ cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail")); cl_assert_equal_s("boom", entry->value); git_config_entry_free(entry); git_config_free(cfg); } void test_config_global__open_xdg(void) { git_config *cfg, *xdg, *selected; const char *str = "teststring"; const char *key = "this.variable"; git_buf buf = {0}; cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n"); cl_git_pass(git_config_open_default(&cfg)); cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG)); cl_git_pass(git_config_open_global(&selected, cfg)); cl_git_pass(git_config_set_string(xdg, key, str)); cl_git_pass(git_config_get_string_buf(&buf, selected, key)); cl_assert_equal_s(str, buf.ptr); git_buf_dispose(&buf); git_config_free(selected); git_config_free(xdg); git_config_free(cfg); } void test_config_global__open_programdata(void) { git_config *cfg; git_repository *repo; git_buf dir_path = GIT_BUF_INIT; git_str config_path = GIT_STR_INIT; git_buf var_contents = GIT_BUF_INIT; if (cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE")) cl_skip(); cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_PROGRAMDATA, &dir_path)); if (!git_fs_path_isdir(dir_path.ptr)) cl_git_pass(p_mkdir(dir_path.ptr, 0777)); cl_git_pass(git_str_joinpath(&config_path, dir_path.ptr, "config")); cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr)); cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level")); git_str_dispose(&config_path); git_config_free(cfg); git_config_open_default(&cfg); cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var")); cl_assert_equal_s("even higher level", var_contents.ptr); git_config_free(cfg); git_buf_dispose(&var_contents); cl_git_pass(git_repository_init(&repo, "./foo.git", true)); cl_git_pass(git_repository_config(&cfg, repo)); cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var")); cl_assert_equal_s("even higher level", var_contents.ptr); git_config_free(cfg); git_buf_dispose(&dir_path); git_buf_dispose(&var_contents); git_repository_free(repo); cl_fixture_cleanup("./foo.git"); }
libgit2-main
tests/libgit2/config/global.c
#include "clar_libgit2.h" #include "git2/sys/config.h" void test_config_backend__checks_version(void) { git_config *cfg; git_config_backend backend = GIT_CONFIG_BACKEND_INIT; const git_error *err; backend.version = 1024; cl_git_pass(git_config_new(&cfg)); cl_git_fail(git_config_add_backend(cfg, &backend, 0, NULL, false)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); git_error_clear(); backend.version = 1024; cl_git_fail(git_config_add_backend(cfg, &backend, 0, NULL, false)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/backend.c
#include "clar_libgit2.h" #include "futils.h" #include "git2/sys/config.h" #include "config.h" void test_config_write__initialize(void) { cl_fixture_sandbox("config/config9"); cl_fixture_sandbox("config/config15"); cl_fixture_sandbox("config/config17"); cl_fixture_sandbox("config/config22"); } void test_config_write__cleanup(void) { cl_fixture_cleanup("config9"); cl_fixture_cleanup("config15"); cl_fixture_cleanup("config17"); cl_fixture_cleanup("config22"); } void test_config_write__replace_value(void) { git_config *cfg; int i; int64_t l, expected = +9223372036854775803; /* By freeing the config, we make sure we flush the values */ cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy")); cl_assert(i == 5); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_int64(cfg, "core.verylong", expected)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_int64(&l, cfg, "core.verylong")); cl_assert(l == expected); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_must_fail(git_config_get_int32(&i, cfg, "core.verylong")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_int64(cfg, "core.verylong", 1)); git_config_free(cfg); } void test_config_write__delete_value(void) { git_config *cfg; int32_t i; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_delete_entry(cfg, "core.dummy")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_assert(git_config_get_int32(&i, cfg, "core.dummy") == GIT_ENOTFOUND); cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1)); git_config_free(cfg); } /* * At the beginning of the test: * - config9 has: core.dummy2=42 * - config15 has: core.dummy2=7 */ void test_config_write__delete_value_at_specific_level(void) { git_config *cfg, *cfg_specific; int32_t i; cl_git_pass(git_config_open_ondisk(&cfg, "config15")); cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2")); cl_assert(i == 7); git_config_free(cfg); cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, "config9", GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, "config15", GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_delete_entry(cfg_specific, "core.dummy2")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config15")); cl_assert(git_config_get_int32(&i, cfg, "core.dummy2") == GIT_ENOTFOUND); cl_git_pass(git_config_set_int32(cfg, "core.dummy2", 7)); git_config_free(cfg_specific); git_config_free(cfg); } /* * This test exposes a bug where duplicate empty section headers could prevent * deletion of config entries. */ void test_config_write__delete_value_with_duplicate_header(void) { const char *file_name = "config-duplicate-header"; const char *entry_name = "remote.origin.url"; git_config *cfg; git_config_entry *entry; /* This config can occur after removing and re-adding the origin remote */ const char *file_content = "[remote \"origin\"]\n" \ "[branch \"master\"]\n" \ " remote = \"origin\"\n" \ "[remote \"origin\"]\n" \ " url = \"foo\"\n"; /* Write the test config and make sure the expected entry exists */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_get_entry(&entry, cfg, entry_name)); /* Delete that entry */ cl_git_pass(git_config_delete_entry(cfg, entry_name)); /* Reopen the file and make sure the entry no longer exists */ git_config_entry_free(entry); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_fail(git_config_get_entry(&entry, cfg, entry_name)); /* Cleanup */ git_config_entry_free(entry); git_config_free(cfg); } /* * This test exposes a bug where duplicate section headers could cause * config_write to add a new entry when one already exists. */ void test_config_write__add_value_with_duplicate_header(void) { const char *file_name = "config-duplicate-insert"; const char *entry_name = "foo.c"; const char *old_val = "old"; const char *new_val = "new"; const char *str; git_config *cfg, *snapshot; /* c = old should be replaced by c = new. * The bug causes c = new to be inserted under the first 'foo' header. */ const char *file_content = "[foo]\n" \ " a = b\n" \ "[other]\n" \ " a = b\n" \ "[foo]\n" \ " c = old\n"; /* Write the test config */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); /* make sure the expected entry (foo.c) exists */ cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_string(&str, snapshot, entry_name)); cl_assert_equal_s(old_val, str); git_config_free(snapshot); /* Try setting foo.c to something else */ cl_git_pass(git_config_set_string(cfg, entry_name, new_val)); git_config_free(cfg); /* Reopen the file and make sure the new value was set */ cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_snapshot(&snapshot, cfg)); cl_git_pass(git_config_get_string(&str, snapshot, entry_name)); cl_assert_equal_s(new_val, str); /* Cleanup */ git_config_free(snapshot); git_config_free(cfg); } void test_config_write__overwrite_value_with_duplicate_header(void) { const char *file_name = "config-duplicate-header"; const char *entry_name = "remote.origin.url"; git_config *cfg; git_config_entry *entry; /* This config can occur after removing and re-adding the origin remote */ const char *file_content = "[remote \"origin\"]\n" \ "[branch \"master\"]\n" \ " remote = \"origin\"\n" \ "[remote \"origin\"]\n" \ " url = \"foo\"\n"; /* Write the test config and make sure the expected entry exists */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_get_entry(&entry, cfg, entry_name)); /* Update that entry */ cl_git_pass(git_config_set_string(cfg, entry_name, "newurl")); /* Reopen the file and make sure the entry was updated */ git_config_entry_free(entry); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_get_entry(&entry, cfg, entry_name)); cl_assert_equal_s("newurl", entry->value); /* Cleanup */ git_config_entry_free(entry); git_config_free(cfg); } static int multivar_cb(const git_config_entry *entry, void *data) { int *n = (int *)data; cl_assert_equal_s(entry->value, "newurl"); (*n)++; return 0; } void test_config_write__overwrite_multivar_within_duplicate_header(void) { const char *file_name = "config-duplicate-header"; const char *entry_name = "remote.origin.url"; git_config *cfg; git_config_entry *entry; int n = 0; /* This config can occur after removing and re-adding the origin remote */ const char *file_content = "[remote \"origin\"]\n" \ " url = \"bar\"\n" \ "[branch \"master\"]\n" \ " remote = \"origin\"\n" \ "[remote \"origin\"]\n" \ " url = \"foo\"\n"; /* Write the test config and make sure the expected entry exists */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_get_entry(&entry, cfg, entry_name)); /* Update that entry */ cl_git_pass(git_config_set_multivar(cfg, entry_name, ".*", "newurl")); git_config_entry_free(entry); git_config_free(cfg); /* Reopen the file and make sure the entry was updated */ cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_get_multivar_foreach(cfg, entry_name, NULL, multivar_cb, &n)); cl_assert_equal_i(2, n); /* Cleanup */ git_config_free(cfg); } void test_config_write__write_subsection(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "my.own.var", "works")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "my.own.var")); cl_assert_equal_s("works", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_write__delete_inexistent(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_assert(git_config_delete_entry(cfg, "core.imaginary") == GIT_ENOTFOUND); git_config_free(cfg); } void test_config_write__value_containing_quotes(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); /* The code path for values that already exist is different, check that one as well */ cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this also \"has\" quotes", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this also \"has\" quotes", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_write__escape_value(void) { git_config *cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes and \t", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.somevar")); cl_assert_equal_s("this \"has\" quotes and \t", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_write__add_value_at_specific_level(void) { git_config *cfg, *cfg_specific; int i; int64_t l, expected = +9223372036854775803; git_buf buf = GIT_BUF_INIT; /* open config15 as global level config file */ cl_git_pass(git_config_new(&cfg)); cl_git_pass(git_config_add_file_ondisk(cfg, "config9", GIT_CONFIG_LEVEL_LOCAL, NULL, 0)); cl_git_pass(git_config_add_file_ondisk(cfg, "config15", GIT_CONFIG_LEVEL_GLOBAL, NULL, 0)); cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL)); cl_git_pass(git_config_set_int32(cfg_specific, "core.int32global", 28)); cl_git_pass(git_config_set_int64(cfg_specific, "core.int64global", expected)); cl_git_pass(git_config_set_bool(cfg_specific, "core.boolglobal", true)); cl_git_pass(git_config_set_string(cfg_specific, "core.stringglobal", "I'm a global config value!")); git_config_free(cfg_specific); git_config_free(cfg); /* open config15 as local level config file */ cl_git_pass(git_config_open_ondisk(&cfg, "config15")); cl_git_pass(git_config_get_int32(&i, cfg, "core.int32global")); cl_assert_equal_i(28, i); cl_git_pass(git_config_get_int64(&l, cfg, "core.int64global")); cl_assert(l == expected); cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal")); cl_assert_equal_b(true, i); cl_git_pass(git_config_get_string_buf(&buf, cfg, "core.stringglobal")); cl_assert_equal_s("I'm a global config value!", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); } void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void) { git_config *cfg; int i; cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_set_int32(cfg, "core.newline", 7)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_get_int32(&i, cfg, "core.newline")); cl_assert_equal_i(7, i); git_config_free(cfg); } void test_config_write__add_section_at_file_with_no_clrf_at_the_end(void) { git_config *cfg; int i; cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_set_int32(cfg, "diff.context", 10)); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_get_int32(&i, cfg, "diff.context")); cl_assert_equal_i(10, i); git_config_free(cfg); } void test_config_write__add_value_which_needs_quotes(void) { git_config *cfg, *base; const char* str1; const char* str2; const char* str3; const char* str4; const char* str5; cl_git_pass(git_config_open_ondisk(&cfg, "config17")); cl_git_pass(git_config_set_string(cfg, "core.startwithspace", " Something")); cl_git_pass(git_config_set_string(cfg, "core.endwithspace", "Something ")); cl_git_pass(git_config_set_string(cfg, "core.containscommentchar1", "some#thing")); cl_git_pass(git_config_set_string(cfg, "core.containscommentchar2", "some;thing")); cl_git_pass(git_config_set_string(cfg, "core.startwhithsapceandcontainsdoublequote", " some\"thing")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&base, "config17")); cl_git_pass(git_config_snapshot(&cfg, base)); cl_git_pass(git_config_get_string(&str1, cfg, "core.startwithspace")); cl_assert_equal_s(" Something", str1); cl_git_pass(git_config_get_string(&str2, cfg, "core.endwithspace")); cl_assert_equal_s("Something ", str2); cl_git_pass(git_config_get_string(&str3, cfg, "core.containscommentchar1")); cl_assert_equal_s("some#thing", str3); cl_git_pass(git_config_get_string(&str4, cfg, "core.containscommentchar2")); cl_assert_equal_s("some;thing", str4); cl_git_pass(git_config_get_string(&str5, cfg, "core.startwhithsapceandcontainsdoublequote")); cl_assert_equal_s(" some\"thing", str5); git_config_free(cfg); git_config_free(base); } void test_config_write__can_set_a_value_to_NULL(void) { git_repository *repository; git_config *config; repository = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_config(&config, repository)); cl_git_fail(git_config_set_string(config, "a.b.c", NULL)); git_config_free(config); cl_git_sandbox_cleanup(); } void test_config_write__can_set_an_empty_value(void) { git_repository *repository; git_config *config; git_buf buf = {0}; repository = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_config(&config, repository)); cl_git_pass(git_config_set_string(config, "core.somevar", "")); cl_git_pass(git_config_get_string_buf(&buf, config, "core.somevar")); cl_assert_equal_s("", buf.ptr); git_buf_dispose(&buf); git_config_free(config); cl_git_sandbox_cleanup(); } void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void) { git_config *cfg; cl_git_pass(git_config_open_ondisk(&cfg, "config9")); cl_git_mkfile("config9.lock", "[core]\n"); cl_git_fail_with(git_config_set_string(cfg, "core.dump", "boom"), GIT_ELOCKED); git_config_free(cfg); } void test_config_write__outside_change(void) { int32_t tmp; git_config *cfg; const char *filename = "config-ext-change"; cl_git_mkfile(filename, "[old]\nvalue = 5\n"); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); /* Change the value on the file itself (simulate external process) */ cl_git_mkfile(filename, "[old]\nvalue = 6\n"); cl_git_pass(git_config_set_int32(cfg, "new.value", 7)); cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value")); cl_assert_equal_i(6, tmp); git_config_free(cfg); } #define FOO_COMMENT \ "; another comment!\n" #define SECTION_FOO \ "\n" \ " \n" \ " [section \"foo\"] \n" \ " # here's a comment\n" \ "\tname = \"value\"\n" \ " name2 = \"value2\"\n" \ #define SECTION_FOO_WITH_COMMENT SECTION_FOO FOO_COMMENT #define SECTION_BAR \ "[section \"bar\"]\t\n" \ "\t \n" \ " barname=\"value\"\n" void test_config_write__preserves_whitespace_and_comments(void) { const char *file_name = "config-duplicate-header"; const char *n; git_config *cfg; git_str newfile = GIT_STR_INIT; /* This config can occur after removing and re-adding the origin remote */ const char *file_content = SECTION_FOO_WITH_COMMENT SECTION_BAR; /* Write the test config and make sure the expected entry exists */ cl_git_mkfile(file_name, file_content); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_set_string(cfg, "section.foo.other", "otherval")); cl_git_pass(git_config_set_string(cfg, "newsection.newname", "new_value")); /* Ensure that we didn't needlessly mangle the config file */ cl_git_pass(git_futils_readbuffer(&newfile, file_name)); n = newfile.ptr; cl_assert_equal_strn(SECTION_FOO, n, strlen(SECTION_FOO)); n += strlen(SECTION_FOO); cl_assert_equal_strn("\tother = otherval\n", n, strlen("\tother = otherval\n")); n += strlen("\tother = otherval\n"); cl_assert_equal_strn(FOO_COMMENT, n, strlen(FOO_COMMENT)); n += strlen(FOO_COMMENT); cl_assert_equal_strn(SECTION_BAR, n, strlen(SECTION_BAR)); n += strlen(SECTION_BAR); cl_assert_equal_s("[newsection]\n\tnewname = new_value\n", n); git_str_dispose(&newfile); git_config_free(cfg); } void test_config_write__preserves_entry_with_name_only(void) { const char *file_name = "config-empty-value"; git_config *cfg; git_str newfile = GIT_STR_INIT; /* Write the test config and make sure the expected entry exists */ cl_git_mkfile(file_name, "[section \"foo\"]\n\tname\n"); cl_git_pass(git_config_open_ondisk(&cfg, file_name)); cl_git_pass(git_config_set_string(cfg, "newsection.newname", "new_value")); cl_git_pass(git_config_set_string(cfg, "section.foo.other", "otherval")); cl_git_pass(git_futils_readbuffer(&newfile, file_name)); cl_assert_equal_s("[section \"foo\"]\n\tname\n\tother = otherval\n[newsection]\n\tnewname = new_value\n", newfile.ptr); git_str_dispose(&newfile); git_config_free(cfg); } void test_config_write__to_empty_file(void) { git_config *cfg; const char *filename = "config-file"; git_str result = GIT_STR_INIT; cl_git_mkfile(filename, ""); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_set_string(cfg, "section.name", "value")); git_config_free(cfg); cl_git_pass(git_futils_readbuffer(&result, "config-file")); cl_assert_equal_s("[section]\n\tname = value\n", result.ptr); git_str_dispose(&result); } void test_config_write__to_file_with_only_comment(void) { git_config *cfg; const char *filename = "config-file"; git_str result = GIT_STR_INIT; cl_git_mkfile(filename, "\n\n"); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_set_string(cfg, "section.name", "value")); git_config_free(cfg); cl_git_pass(git_futils_readbuffer(&result, "config-file")); cl_assert_equal_s("\n\n[section]\n\tname = value\n", result.ptr); git_str_dispose(&result); } void test_config_write__locking(void) { git_config *cfg, *cfg2; git_config_entry *entry; git_transaction *tx; const char *filename = "locked-file"; /* Open the config and lock it */ cl_git_mkfile(filename, "[section]\n\tname = value\n"); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_get_entry(&entry, cfg, "section.name")); cl_assert_equal_s("value", entry->value); git_config_entry_free(entry); cl_git_pass(git_config_lock(&tx, cfg)); /* Change entries in the locked backend */ cl_git_pass(git_config_set_string(cfg, "section.name", "other value")); cl_git_pass(git_config_set_string(cfg, "section2.name3", "more value")); /* We can see that the file we read from hasn't changed */ cl_git_pass(git_config_open_ondisk(&cfg2, filename)); cl_git_pass(git_config_get_entry(&entry, cfg2, "section.name")); cl_assert_equal_s("value", entry->value); git_config_entry_free(entry); cl_git_fail_with(GIT_ENOTFOUND, git_config_get_entry(&entry, cfg2, "section2.name3")); git_config_free(cfg2); /* And we also get the old view when we read from the locked config */ cl_git_pass(git_config_get_entry(&entry, cfg, "section.name")); cl_assert_equal_s("value", entry->value); git_config_entry_free(entry); cl_git_fail_with(GIT_ENOTFOUND, git_config_get_entry(&entry, cfg, "section2.name3")); cl_git_pass(git_transaction_commit(tx)); git_transaction_free(tx); /* Now that we've unlocked it, we should see both updates */ cl_git_pass(git_config_get_entry(&entry, cfg, "section.name")); cl_assert_equal_s("other value", entry->value); git_config_entry_free(entry); cl_git_pass(git_config_get_entry(&entry, cfg, "section2.name3")); cl_assert_equal_s("more value", entry->value); git_config_entry_free(entry); git_config_free(cfg); /* We should also see the changes after reopening the config */ cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_get_entry(&entry, cfg, "section.name")); cl_assert_equal_s("other value", entry->value); git_config_entry_free(entry); cl_git_pass(git_config_get_entry(&entry, cfg, "section2.name3")); cl_assert_equal_s("more value", entry->value); git_config_entry_free(entry); git_config_free(cfg); } void test_config_write__repeated(void) { const char *filename = "config-repeated"; git_config *cfg; git_str result = GIT_STR_INIT; const char *expected = "[sample \"prefix\"]\n\ \tsetting1 = someValue1\n\ \tsetting2 = someValue2\n\ \tsetting3 = someValue3\n\ \tsetting4 = someValue4\n\ "; cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_set_string(cfg, "sample.prefix.setting1", "someValue1")); cl_git_pass(git_config_set_string(cfg, "sample.prefix.setting2", "someValue2")); cl_git_pass(git_config_set_string(cfg, "sample.prefix.setting3", "someValue3")); cl_git_pass(git_config_set_string(cfg, "sample.prefix.setting4", "someValue4")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_futils_readbuffer(&result, filename)); cl_assert_equal_s(expected, result.ptr); git_str_dispose(&result); git_config_free(cfg); } void test_config_write__preserve_case(void) { const char *filename = "config-preserve-case"; git_config *cfg; git_str result = GIT_STR_INIT; const char *expected = "[sOMe]\n" \ "\tThInG = foo\n" \ "\tOtheR = thing\n"; cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_config_set_string(cfg, "sOMe.ThInG", "foo")); cl_git_pass(git_config_set_string(cfg, "SomE.OtheR", "thing")); git_config_free(cfg); cl_git_pass(git_config_open_ondisk(&cfg, filename)); cl_git_pass(git_futils_readbuffer(&result, filename)); cl_assert_equal_s(expected, result.ptr); git_str_dispose(&result); git_config_free(cfg); } void test_config_write__write_config_file_with_multi_line_value(void) { git_config* cfg; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_config_open_ondisk(&cfg, "config22")); cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m")); cl_assert_equal_s("cmd ;; ;; bar", buf.ptr); cl_git_pass(git_config_set_string(cfg, "sOMe.ThInG", "foo")); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, cfg, "alias.m")); cl_assert_equal_s("cmd ;; ;; bar", buf.ptr); git_buf_dispose(&buf); git_config_free(cfg); }
libgit2-main
tests/libgit2/config/write.c
#include "clar_libgit2.h" #include "filebuf.h" #include "futils.h" #include "posix.h" #define TEST_CONFIG "git-new-config" void test_config_new__write_new_config(void) { git_config *config; git_buf buf = GIT_BUF_INIT; cl_git_mkfile(TEST_CONFIG, ""); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_set_string(config, "color.ui", "auto")); cl_git_pass(git_config_set_string(config, "core.editor", "ed")); git_config_free(config); cl_git_pass(git_config_open_ondisk(&config, TEST_CONFIG)); cl_git_pass(git_config_get_string_buf(&buf, config, "color.ui")); cl_assert_equal_s("auto", buf.ptr); git_buf_dispose(&buf); cl_git_pass(git_config_get_string_buf(&buf, config, "core.editor")); cl_assert_equal_s("ed", buf.ptr); git_buf_dispose(&buf); git_config_free(config); cl_must_pass(p_unlink(TEST_CONFIG)); }
libgit2-main
tests/libgit2/config/new.c
#include "clar_libgit2.h" #include "posix.h" #include "path.h" #include "futils.h" static git_repository *g_repo = NULL; void test_ignore_path__initialize(void) { g_repo = cl_git_sandbox_init("attr"); } void test_ignore_path__cleanup(void) { cl_git_sandbox_cleanup(); g_repo = NULL; } static void assert_is_ignored_( bool expected, const char *filepath, const char *file, const char *func, int line) { int is_ignored = 0; cl_git_expect( git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), 0, file, func, line); clar__assert_equal( file, func, line, "expected != is_ignored", 1, "%d", (int)(expected != 0), (int)(is_ignored != 0)); } #define assert_is_ignored(expected, filepath) \ assert_is_ignored_(expected, filepath, __FILE__, __func__, __LINE__) void test_ignore_path__honor_temporary_rules(void) { cl_git_rewritefile("attr/.gitignore", "/NewFolder\n/NewFolder/NewFolder"); assert_is_ignored(false, "File.txt"); assert_is_ignored(true, "NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); } void test_ignore_path__allow_root(void) { cl_git_rewritefile("attr/.gitignore", "/"); assert_is_ignored(false, "File.txt"); assert_is_ignored(false, "NewFolder"); assert_is_ignored(false, "NewFolder/NewFolder"); assert_is_ignored(false, "NewFolder/NewFolder/File.txt"); } void test_ignore_path__ignore_space(void) { cl_git_rewritefile("attr/.gitignore", "/\n\n/NewFolder \n/NewFolder/NewFolder"); assert_is_ignored(false, "File.txt"); assert_is_ignored(true, "NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); } void test_ignore_path__intermittent_space(void) { cl_git_rewritefile("attr/.gitignore", "foo bar\n"); assert_is_ignored(false, "foo"); assert_is_ignored(false, "bar"); assert_is_ignored(true, "foo bar"); } void test_ignore_path__trailing_space(void) { cl_git_rewritefile( "attr/.gitignore", "foo \n" "bar \n" ); assert_is_ignored(true, "foo"); assert_is_ignored(false, "foo "); assert_is_ignored(true, "bar"); assert_is_ignored(false, "bar "); assert_is_ignored(false, "bar "); } void test_ignore_path__escaped_trailing_spaces(void) { cl_git_rewritefile( "attr/.gitignore", "foo\\ \n" "bar\\ \\ \n" "baz \\ \n" "qux\\ \n" ); assert_is_ignored(false, "foo"); assert_is_ignored(true, "foo "); assert_is_ignored(false, "bar"); assert_is_ignored(false, "bar "); assert_is_ignored(true, "bar "); assert_is_ignored(true, "baz "); assert_is_ignored(false, "baz "); assert_is_ignored(true, "qux "); assert_is_ignored(false, "qux"); assert_is_ignored(false, "qux "); } void test_ignore_path__ignore_dir(void) { cl_git_rewritefile("attr/.gitignore", "dir/\n"); assert_is_ignored(true, "dir"); assert_is_ignored(true, "dir/file"); } void test_ignore_path__ignore_dir_with_trailing_space(void) { cl_git_rewritefile("attr/.gitignore", "dir/ \n"); assert_is_ignored(true, "dir"); assert_is_ignored(true, "dir/file"); } void test_ignore_path__ignore_root(void) { cl_git_rewritefile("attr/.gitignore", "/\n\n/NewFolder\n/NewFolder/NewFolder"); assert_is_ignored(false, "File.txt"); assert_is_ignored(true, "NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); } void test_ignore_path__full_paths(void) { cl_git_rewritefile("attr/.gitignore", "Folder/*/Contained"); assert_is_ignored(true, "Folder/Middle/Contained"); assert_is_ignored(false, "Folder/Middle/More/More/Contained"); cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained"); assert_is_ignored(true, "Folder/Middle/Contained"); assert_is_ignored(true, "Folder/Middle/More/More/Contained"); cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained/*/Child"); assert_is_ignored(true, "Folder/Middle/Contained/Happy/Child"); assert_is_ignored(false, "Folder/Middle/Contained/Not/Happy/Child"); assert_is_ignored(true, "Folder/Middle/More/More/Contained/Happy/Child"); assert_is_ignored(false, "Folder/Middle/More/More/Contained/Not/Happy/Child"); } void test_ignore_path__more_starstar_cases(void) { cl_must_pass(p_unlink("attr/.gitignore")); cl_git_mkfile( "attr/dir/.gitignore", "sub/**/*.html\n"); assert_is_ignored(false, "aaa.html"); assert_is_ignored(false, "dir"); assert_is_ignored(false, "dir/sub"); assert_is_ignored(true, "dir/sub/sub2/aaa.html"); assert_is_ignored(true, "dir/sub/aaa.html"); assert_is_ignored(false, "dir/aaa.html"); assert_is_ignored(false, "sub"); assert_is_ignored(false, "sub/aaa.html"); assert_is_ignored(false, "sub/sub2/aaa.html"); } void test_ignore_path__leading_stars(void) { cl_git_rewritefile( "attr/.gitignore", "*/onestar\n" "**/twostars\n" "*/parent1/kid1/*\n" "**/parent2/kid2/*\n"); assert_is_ignored(true, "dir1/onestar"); assert_is_ignored(true, "dir1/onestar/child"); /* in ignored dir */ assert_is_ignored(false, "dir1/dir2/onestar"); assert_is_ignored(true, "dir1/twostars"); assert_is_ignored(true, "dir1/twostars/child"); /* in ignored dir */ assert_is_ignored(true, "dir1/dir2/twostars"); assert_is_ignored(true, "dir1/dir2/twostars/child"); /* in ignored dir */ assert_is_ignored(true, "dir1/dir2/dir3/twostars"); assert_is_ignored(true, "dir1/parent1/kid1/file"); assert_is_ignored(true, "dir1/parent1/kid1/file/inside/parent"); assert_is_ignored(false, "dir1/dir2/parent1/kid1/file"); assert_is_ignored(false, "dir1/parent1/file"); assert_is_ignored(false, "dir1/kid1/file"); assert_is_ignored(true, "dir1/parent2/kid2/file"); assert_is_ignored(true, "dir1/parent2/kid2/file/inside/parent"); assert_is_ignored(true, "dir1/dir2/parent2/kid2/file"); assert_is_ignored(true, "dir1/dir2/dir3/parent2/kid2/file"); assert_is_ignored(false, "dir1/parent2/file"); assert_is_ignored(false, "dir1/kid2/file"); } void test_ignore_path__globs_and_path_delimiters(void) { cl_git_rewritefile("attr/.gitignore", "foo/bar/**"); assert_is_ignored(true, "foo/bar/baz"); assert_is_ignored(true, "foo/bar/baz/quux"); cl_git_rewritefile("attr/.gitignore", "_*/"); assert_is_ignored(true, "sub/_test/a/file"); assert_is_ignored(false, "test_folder/file"); assert_is_ignored(true, "_test/file"); assert_is_ignored(true, "_test/a/file"); cl_git_rewritefile("attr/.gitignore", "**/_*/"); assert_is_ignored(true, "sub/_test/a/file"); assert_is_ignored(false, "test_folder/file"); assert_is_ignored(true, "_test/file"); assert_is_ignored(true, "_test/a/file"); cl_git_rewritefile("attr/.gitignore", "**/_*/foo/bar/*ux"); assert_is_ignored(true, "sub/_test/foo/bar/qux/file"); assert_is_ignored(true, "_test/foo/bar/qux/file"); assert_is_ignored(true, "_test/foo/bar/crux/file"); assert_is_ignored(false, "_test/foo/bar/code/file"); } void test_ignore_path__globs_without_star(void) { cl_git_rewritefile( "attr/.gitignore", "*.foo\n" "**.bar\n" ); assert_is_ignored(true, ".foo"); assert_is_ignored(true, "xyz.foo"); assert_is_ignored(true, ".bar"); assert_is_ignored(true, "x.bar"); assert_is_ignored(true, "xyz.bar"); assert_is_ignored(true, "test/.foo"); assert_is_ignored(true, "test/x.foo"); assert_is_ignored(true, "test/xyz.foo"); assert_is_ignored(true, "test/.bar"); assert_is_ignored(true, "test/x.bar"); assert_is_ignored(true, "test/xyz.bar"); } void test_ignore_path__skip_gitignore_directory(void) { cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder"); cl_must_pass(p_unlink("attr/.gitignore")); cl_assert(!git_fs_path_exists("attr/.gitignore")); p_mkdir("attr/.gitignore", 0777); cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n"); assert_is_ignored(false, "File.txt"); assert_is_ignored(true, "NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder"); assert_is_ignored(true, "NewFolder/NewFolder/File.txt"); } void test_ignore_path__subdirectory_gitignore(void) { cl_must_pass(p_unlink("attr/.gitignore")); cl_assert(!git_fs_path_exists("attr/.gitignore")); cl_git_mkfile( "attr/.gitignore", "file1\n"); cl_git_mkfile( "attr/dir/.gitignore", "file2/\n"); assert_is_ignored(true, "file1"); assert_is_ignored(true, "dir/file1"); assert_is_ignored(true, "dir/file2/actual_file"); /* in ignored dir */ assert_is_ignored(false, "dir/file3"); } void test_ignore_path__expand_tilde_to_homedir(void) { git_config *cfg; assert_is_ignored(false, "example.global_with_tilde"); cl_fake_home(); /* construct fake home with fake global excludes */ cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n"); cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude")); git_config_free(cfg); git_attr_cache_flush(g_repo); /* must reset to pick up change */ assert_is_ignored(true, "example.global_with_tilde"); cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES)); cl_fake_home_cleanup(NULL); git_attr_cache_flush(g_repo); /* must reset to pick up change */ assert_is_ignored(false, "example.global_with_tilde"); } /* Ensure that the .gitignore in the subdirectory only affects * items in the subdirectory. */ void test_ignore_path__gitignore_in_subdir(void) { cl_git_rmfile("attr/.gitignore"); cl_must_pass(p_mkdir("attr/dir1", 0777)); cl_must_pass(p_mkdir("attr/dir1/dir2", 0777)); cl_must_pass(p_mkdir("attr/dir1/dir2/dir3", 0777)); cl_git_mkfile("attr/dir1/dir2/dir3/.gitignore", "dir1/\ndir1/subdir/"); assert_is_ignored(false, "dir1/file"); assert_is_ignored(false, "dir1/dir2/file"); assert_is_ignored(false, "dir1/dir2/dir3/file"); assert_is_ignored(true, "dir1/dir2/dir3/dir1/file"); assert_is_ignored(true, "dir1/dir2/dir3/dir1/subdir/foo"); if (cl_repo_get_bool(g_repo, "core.ignorecase")) { cl_git_mkfile("attr/dir1/dir2/dir3/.gitignore", "DiR1/\nDiR1/subdir/\n"); assert_is_ignored(false, "dir1/file"); assert_is_ignored(false, "dir1/dir2/file"); assert_is_ignored(false, "dir1/dir2/dir3/file"); assert_is_ignored(true, "dir1/dir2/dir3/dir1/file"); assert_is_ignored(true, "dir1/dir2/dir3/dir1/subdir/foo"); } } /* Ensure that files do not match folder cases */ void test_ignore_path__dont_ignore_files_for_folder(void) { cl_git_rmfile("attr/.gitignore"); cl_git_mkfile("attr/dir/.gitignore", "test/\n"); /* Create "test" as a file; ensure it is not ignored. */ cl_git_mkfile("attr/dir/test", "This is a file."); assert_is_ignored(false, "dir/test"); if (cl_repo_get_bool(g_repo, "core.ignorecase")) assert_is_ignored(false, "dir/TeSt"); /* Create "test" as a directory; ensure it is ignored. */ cl_git_rmfile("attr/dir/test"); cl_must_pass(p_mkdir("attr/dir/test", 0777)); assert_is_ignored(true, "dir/test"); if (cl_repo_get_bool(g_repo, "core.ignorecase")) assert_is_ignored(true, "dir/TeSt"); /* Remove "test" entirely; ensure it is not ignored. * (As it doesn't exist, it is not a directory.) */ cl_must_pass(p_rmdir("attr/dir/test")); assert_is_ignored(false, "dir/test"); if (cl_repo_get_bool(g_repo, "core.ignorecase")) assert_is_ignored(false, "dir/TeSt"); } void test_ignore_path__symlink_to_outside(void) { #ifdef GIT_WIN32 cl_skip(); #endif cl_git_rewritefile("attr/.gitignore", "symlink\n"); cl_git_mkfile("target", "target"); cl_git_pass(p_symlink("../target", "attr/symlink")); assert_is_ignored(true, "symlink"); assert_is_ignored(true, "lala/../symlink"); } void test_ignore_path__test(void) { cl_git_rewritefile("attr/.gitignore", "/*/\n" "!/src\n"); assert_is_ignored(false, "src/foo.c"); assert_is_ignored(false, "src/foo/foo.c"); assert_is_ignored(false, "README.md"); assert_is_ignored(true, "dist/foo.o"); assert_is_ignored(true, "bin/foo"); } void test_ignore_path__unignore_dir_succeeds(void) { cl_git_rewritefile("attr/.gitignore", "*.c\n" "!src/*.c\n"); assert_is_ignored(false, "src/foo.c"); assert_is_ignored(true, "src/foo/foo.c"); } void test_ignore_path__case_insensitive_unignores_previous_rule(void) { git_config *cfg; cl_git_rewritefile("attr/.gitignore", "/case\n" "!/Case/\n"); cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "core.ignorecase", true)); cl_must_pass(p_mkdir("attr/case", 0755)); cl_git_mkfile("attr/case/file", "content"); assert_is_ignored(false, "case/file"); } void test_ignore_path__case_sensitive_unignore_does_nothing(void) { git_config *cfg; cl_git_rewritefile("attr/.gitignore", "/case\n" "!/Case/\n"); cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "core.ignorecase", false)); cl_must_pass(p_mkdir("attr/case", 0755)); cl_git_mkfile("attr/case/file", "content"); assert_is_ignored(true, "case/file"); } void test_ignore_path__ignored_subdirfiles_with_subdir_rule(void) { cl_git_rewritefile( "attr/.gitignore", "dir/*\n" "!dir/sub1/sub2/**\n"); assert_is_ignored(true, "dir/a.test"); assert_is_ignored(true, "dir/sub1/a.test"); assert_is_ignored(true, "dir/sub1/sub2"); } void test_ignore_path__ignored_subdirfiles_with_negations(void) { cl_git_rewritefile( "attr/.gitignore", "dir/*\n" "!dir/a.test\n"); assert_is_ignored(false, "dir/a.test"); assert_is_ignored(true, "dir/b.test"); assert_is_ignored(true, "dir/sub1/c.test"); } void test_ignore_path__negative_directory_rules_only_match_directories(void) { cl_git_rewritefile( "attr/.gitignore", "*\n" "!/**/\n" "!*.keep\n" "!.gitignore\n" ); assert_is_ignored(true, "src"); assert_is_ignored(true, "src/A"); assert_is_ignored(false, "src/"); assert_is_ignored(false, "src/A.keep"); assert_is_ignored(false, ".gitignore"); } void test_ignore_path__escaped_character(void) { cl_git_rewritefile("attr/.gitignore", "\\c\n"); assert_is_ignored(true, "c"); assert_is_ignored(false, "\\c"); } void test_ignore_path__escaped_newline(void) { cl_git_rewritefile( "attr/.gitignore", "\\\nnewline\n" ); assert_is_ignored(true, "\nnewline"); } void test_ignore_path__escaped_glob(void) { cl_git_rewritefile("attr/.gitignore", "\\*\n"); assert_is_ignored(true, "*"); assert_is_ignored(false, "foo"); } void test_ignore_path__escaped_comments(void) { cl_git_rewritefile( "attr/.gitignore", "#foo\n" "\\#bar\n" "\\##baz\n" "\\#\\\\#qux\n" ); assert_is_ignored(false, "#foo"); assert_is_ignored(true, "#bar"); assert_is_ignored(false, "\\#bar"); assert_is_ignored(true, "##baz"); assert_is_ignored(false, "\\##baz"); assert_is_ignored(true, "#\\#qux"); assert_is_ignored(false, "##qux"); assert_is_ignored(false, "\\##qux"); } void test_ignore_path__escaped_slash(void) { cl_git_rewritefile( "attr/.gitignore", "\\\\\n" "\\\\preceding\n" "inter\\\\mittent\n" "trailing\\\\\n" ); #ifndef GIT_WIN32 assert_is_ignored(true, "\\"); assert_is_ignored(true, "\\preceding"); #endif assert_is_ignored(true, "inter\\mittent"); assert_is_ignored(true, "trailing\\"); } void test_ignore_path__escaped_space(void) { cl_git_rewritefile( "attr/.gitignore", "foo\\\\ \n" "bar\\\\\\ \n"); assert_is_ignored(true, "foo\\"); assert_is_ignored(false, "foo\\ "); assert_is_ignored(false, "foo\\\\ "); assert_is_ignored(false, "foo\\\\"); assert_is_ignored(true, "bar\\ "); assert_is_ignored(false, "bar\\\\"); assert_is_ignored(false, "bar\\\\ "); assert_is_ignored(false, "bar\\\\\\"); assert_is_ignored(false, "bar\\\\\\ "); } void test_ignore_path__invalid_pattern(void) { cl_git_rewritefile("attr/.gitignore", "["); assert_is_ignored(false, "[f"); assert_is_ignored(false, "f"); } void test_ignore_path__negative_prefix_rule(void) { cl_git_rewritefile("attr/.gitignore", "ff*\n!f\n"); assert_is_ignored(true, "fff"); assert_is_ignored(true, "ff"); assert_is_ignored(false, "f"); } void test_ignore_path__negative_more_specific(void) { cl_git_rewritefile("attr/.gitignore", "*.txt\n!/dir/test.txt\n"); assert_is_ignored(true, "test.txt"); assert_is_ignored(false, "dir/test.txt"); assert_is_ignored(true, "outer/dir/test.txt"); }
libgit2-main
tests/libgit2/ignore/path.c
#include "clar_libgit2.h" #include "futils.h" #include "git2/attr.h" #include "ignore.h" #include "attr.h" #include "status/status_helpers.h" static git_repository *g_repo = NULL; void test_ignore_status__initialize(void) { } void test_ignore_status__cleanup(void) { cl_git_sandbox_cleanup(); } static void assert_ignored_( bool expected, const char *filepath, const char *file, const char *func, int line) { int is_ignored = 0; cl_git_expect( git_status_should_ignore(&is_ignored, g_repo, filepath), 0, file, func, line); clar__assert( (expected != 0) == (is_ignored != 0), file, func, line, "expected != is_ignored", filepath, 1); } #define assert_ignored(expected, filepath) \ assert_ignored_(expected, filepath, __FILE__, __func__, __LINE__) #define assert_is_ignored(filepath) \ assert_ignored_(true, filepath, __FILE__, __func__, __LINE__) #define refute_is_ignored(filepath) \ assert_ignored_(false, filepath, __FILE__, __func__, __LINE__) void test_ignore_status__0(void) { struct { const char *path; int expected; } test_cases[] = { /* pattern "ign" from .gitignore */ { "file", 0 }, { "ign", 1 }, { "sub", 0 }, { "sub/file", 0 }, { "sub/ign", 1 }, { "sub/ign/file", 1 }, { "sub/ign/sub", 1 }, { "sub/ign/sub/file", 1 }, { "sub/sub", 0 }, { "sub/sub/file", 0 }, { "sub/sub/ign", 1 }, { "sub/sub/sub", 0 }, /* pattern "dir/" from .gitignore */ { "dir", 1 }, { "dir/", 1 }, { "sub/dir", 1 }, { "sub/dir/", 1 }, { "sub/dir/file", 1 }, /* contained in ignored parent */ { "sub/sub/dir", 0 }, /* dir is not actually a dir, but a file */ { NULL, 0 } }, *one_test; g_repo = cl_git_sandbox_init("attr"); for (one_test = test_cases; one_test->path != NULL; one_test++) assert_ignored(one_test->expected, one_test->path); /* confirm that ignore files were cached */ cl_assert(git_attr_cache__is_cached( g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/exclude")); cl_assert(git_attr_cache__is_cached( g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitignore")); } void test_ignore_status__1(void) { g_repo = cl_git_sandbox_init("attr"); cl_git_rewritefile("attr/.gitignore", "/*.txt\n/dir/\n"); git_attr_cache_flush(g_repo); assert_is_ignored("root_test4.txt"); refute_is_ignored("sub/subdir_test2.txt"); assert_is_ignored("dir"); assert_is_ignored("dir/"); refute_is_ignored("sub/dir"); refute_is_ignored("sub/dir/"); } void test_ignore_status__empty_repo_with_gitignore_rewrite(void) { status_entry_single st; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile( "empty_standard_repo/look-ma.txt", "I'm going to be ignored!"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); cl_assert(st.count == 1); cl_assert(st.status == GIT_STATUS_WT_NEW); cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); cl_assert(st.status == GIT_STATUS_WT_NEW); refute_is_ignored("look-ma.txt"); cl_git_rewritefile("empty_standard_repo/.gitignore", "*.nomatch\n"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); cl_assert(st.count == 2); cl_assert(st.status == GIT_STATUS_WT_NEW); cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); cl_assert(st.status == GIT_STATUS_WT_NEW); refute_is_ignored("look-ma.txt"); cl_git_rewritefile("empty_standard_repo/.gitignore", "*.txt\n"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); cl_assert(st.count == 2); cl_assert(st.status == GIT_STATUS_IGNORED); cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt")); cl_assert(st.status == GIT_STATUS_IGNORED); assert_is_ignored("look-ma.txt"); } void test_ignore_status__ignore_pattern_contains_space(void) { unsigned int flags; const mode_t mode = 0777; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_rewritefile("empty_standard_repo/.gitignore", "foo bar.txt\n"); cl_git_mkfile( "empty_standard_repo/foo bar.txt", "I'm going to be ignored!"); cl_git_pass(git_status_file(&flags, g_repo, "foo bar.txt")); cl_assert(flags == GIT_STATUS_IGNORED); cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", mode)); cl_git_mkfile("empty_standard_repo/foo/look-ma.txt", "I'm not going to be ignored!"); cl_git_pass(git_status_file(&flags, g_repo, "foo/look-ma.txt")); cl_assert(flags == GIT_STATUS_WT_NEW); } void test_ignore_status__ignore_pattern_ignorecase(void) { unsigned int flags; bool ignore_case; git_index *index; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_rewritefile("empty_standard_repo/.gitignore", "a.txt\n"); cl_git_mkfile("empty_standard_repo/A.txt", "Differs in case"); cl_git_pass(git_repository_index(&index, g_repo)); ignore_case = (git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0; git_index_free(index); cl_git_pass(git_status_file(&flags, g_repo, "A.txt")); cl_assert(flags == ignore_case ? GIT_STATUS_IGNORED : GIT_STATUS_WT_NEW); } void test_ignore_status__subdirectories(void) { status_entry_single st; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile( "empty_standard_repo/ignore_me", "I'm going to be ignored!"); cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); cl_assert_equal_i(2, st.count); cl_assert(st.status == GIT_STATUS_IGNORED); cl_git_pass(git_status_file(&st.status, g_repo, "ignore_me")); cl_assert(st.status == GIT_STATUS_IGNORED); assert_is_ignored("ignore_me"); /* I've changed libgit2 so that the behavior here now differs from * core git but seems to make more sense. In core git, the following * items are skipped completed, even if --ignored is passed to status. * It you mirror these steps and run "git status -uall --ignored" then * you will not see "test/ignore_me/" in the results. * * However, we had a couple reports of this as a bug, plus there is a * similar circumstance where we were differing for core git when you * used a rooted path for an ignore, so I changed this behavior. */ cl_git_pass(git_futils_mkdir_r( "empty_standard_repo/test/ignore_me", 0775)); cl_git_mkfile( "empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!"); cl_git_mkfile( "empty_standard_repo/test/ignore_me/file2", "Me, too!"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st)); cl_assert_equal_i(3, st.count); cl_git_pass(git_status_file(&st.status, g_repo, "test/ignore_me/file")); cl_assert(st.status == GIT_STATUS_IGNORED); assert_is_ignored("test/ignore_me/file"); } static void make_test_data(const char *reponame, const char **files) { const char **scan; size_t repolen = strlen(reponame) + 1; g_repo = cl_git_sandbox_init(reponame); for (scan = files; *scan != NULL; ++scan) { cl_git_pass(git_futils_mkdir_relative( *scan + repolen, reponame, 0777, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST, NULL)); cl_git_mkfile(*scan, "contents"); } } static const char *test_repo_1 = "empty_standard_repo"; static const char *test_files_1[] = { "empty_standard_repo/dir/a/ignore_me", "empty_standard_repo/dir/b/ignore_me", "empty_standard_repo/dir/ignore_me", "empty_standard_repo/ignore_also/file", "empty_standard_repo/ignore_me", "empty_standard_repo/test/ignore_me/file", "empty_standard_repo/test/ignore_me/file2", "empty_standard_repo/test/ignore_me/and_me/file", NULL }; void test_ignore_status__subdirectories_recursion(void) { /* Let's try again with recursing into ignored dirs turned on */ git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *paths_r[] = { ".gitignore", "dir/a/ignore_me", "dir/b/ignore_me", "dir/ignore_me", "ignore_also/file", "ignore_me", "test/ignore_me/and_me/file", "test/ignore_me/file", "test/ignore_me/file2", }; static const unsigned int statuses_r[] = { GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, }; static const char *paths_nr[] = { ".gitignore", "dir/a/ignore_me", "dir/b/ignore_me", "dir/ignore_me", "ignore_also/", "ignore_me", "test/ignore_me/", }; static const unsigned int statuses_nr[] = { GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, }; make_test_data(test_repo_1, test_files_1); cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n/ignore_also\n"); memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 9; counts.expected_paths = paths_r; counts.expected_statuses = statuses_r; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 7; counts.expected_paths = paths_nr; counts.expected_statuses = statuses_nr; opts.flags = GIT_STATUS_OPT_DEFAULTS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } void test_ignore_status__subdirectories_not_at_root(void) { git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *paths_1[] = { "dir/.gitignore", "dir/a/ignore_me", "dir/b/ignore_me", "dir/ignore_me", "ignore_also/file", "ignore_me", "test/.gitignore", "test/ignore_me/and_me/file", "test/ignore_me/file", "test/ignore_me/file2", }; static const unsigned int statuses_1[] = { GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, }; make_test_data(test_repo_1, test_files_1); cl_git_rewritefile("empty_standard_repo/dir/.gitignore", "ignore_me\n/ignore_also\n"); cl_git_rewritefile("empty_standard_repo/test/.gitignore", "and_me\n"); memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 10; counts.expected_paths = paths_1; counts.expected_statuses = statuses_1; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } void test_ignore_status__leading_slash_ignores(void) { git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *paths_2[] = { "dir/.gitignore", "dir/a/ignore_me", "dir/b/ignore_me", "dir/ignore_me", "ignore_also/file", "ignore_me", "test/.gitignore", "test/ignore_me/and_me/file", "test/ignore_me/file", "test/ignore_me/file2", }; static const unsigned int statuses_2[] = { GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, }; make_test_data(test_repo_1, test_files_1); cl_fake_home(); cl_git_mkfile("home/.gitignore", "/ignore_me\n"); { git_config *cfg; cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string( cfg, "core.excludesfile", "~/.gitignore")); git_config_free(cfg); } cl_git_rewritefile("empty_standard_repo/.git/info/exclude", "/ignore_also\n"); cl_git_rewritefile("empty_standard_repo/dir/.gitignore", "/ignore_me\n"); cl_git_rewritefile("empty_standard_repo/test/.gitignore", "/and_me\n"); memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 10; counts.expected_paths = paths_2; counts.expected_statuses = statuses_2; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } void test_ignore_status__multiple_leading_slash(void) { static const char *test_files[] = { "empty_standard_repo/a.test", "empty_standard_repo/b.test", "empty_standard_repo/c.test", "empty_standard_repo/d.test", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "a.test\n" "/b.test\n" "//c.test\n" "///d.test\n"); assert_is_ignored("a.test"); assert_is_ignored("b.test"); refute_is_ignored("c.test"); refute_is_ignored("d.test"); } void test_ignore_status__contained_dir_with_matching_name(void) { static const char *test_files[] = { "empty_standard_repo/subdir_match/aaa/subdir_match/file", "empty_standard_repo/subdir_match/zzz_ignoreme", NULL }; static const char *expected_paths[] = { "subdir_match/.gitignore", "subdir_match/aaa/subdir_match/file", "subdir_match/zzz_ignoreme", }; static const unsigned int expected_statuses[] = { GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED }; git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/subdir_match/.gitignore", "*_ignoreme\n"); refute_is_ignored("subdir_match/aaa/subdir_match/file"); assert_is_ignored("subdir_match/zzz_ignoreme"); memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 3; counts.expected_paths = expected_paths; counts.expected_statuses = expected_statuses; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } void test_ignore_status__trailing_slash_star(void) { static const char *test_files[] = { "empty_standard_repo/file", "empty_standard_repo/subdir/file", "empty_standard_repo/subdir/sub2/sub3/file", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/subdir/.gitignore", "/**/*\n"); refute_is_ignored("file"); assert_is_ignored("subdir/sub2/sub3/file"); assert_is_ignored("subdir/file"); } void test_ignore_status__adding_internal_ignores(void) { g_repo = cl_git_sandbox_init("empty_standard_repo"); refute_is_ignored("one.txt"); refute_is_ignored("two.bar"); cl_git_pass(git_ignore_add_rule(g_repo, "*.nomatch\n")); refute_is_ignored("one.txt"); refute_is_ignored("two.bar"); cl_git_pass(git_ignore_add_rule(g_repo, "*.txt\n")); assert_is_ignored("one.txt"); refute_is_ignored("two.bar"); cl_git_pass(git_ignore_add_rule(g_repo, "*.bar\n")); assert_is_ignored("one.txt"); assert_is_ignored("two.bar"); cl_git_pass(git_ignore_clear_internal_rules(g_repo)); refute_is_ignored("one.txt"); refute_is_ignored("two.bar"); cl_git_pass(git_ignore_add_rule( g_repo, "multiple\n*.rules\n# comment line\n*.bar\n")); refute_is_ignored("one.txt"); assert_is_ignored("two.bar"); } void test_ignore_status__add_internal_as_first_thing(void) { const char *add_me = "\n#################\n## Eclipse\n#################\n\n*.pydevproject\n.project\n.metadata\nbin/\ntmp/\n*.tmp\n\n"; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_pass(git_ignore_add_rule(g_repo, add_me)); assert_is_ignored("one.tmp"); refute_is_ignored("two.bar"); } void test_ignore_status__internal_ignores_inside_deep_paths(void) { const char *add_me = "Debug\nthis/is/deep\npatterned*/dir\n"; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_pass(git_ignore_add_rule(g_repo, add_me)); assert_is_ignored("Debug"); assert_is_ignored("and/Debug"); assert_is_ignored("really/Debug/this/file"); assert_is_ignored("Debug/what/I/say"); refute_is_ignored("and/NoDebug"); refute_is_ignored("NoDebug/this"); refute_is_ignored("please/NoDebug/this"); assert_is_ignored("this/is/deep"); /* pattern containing slash gets FNM_PATHNAME so all slashes must match */ refute_is_ignored("and/this/is/deep"); assert_is_ignored("this/is/deep/too"); /* pattern containing slash gets FNM_PATHNAME so all slashes must match */ refute_is_ignored("but/this/is/deep/and/ignored"); refute_is_ignored("this/is/not/deep"); refute_is_ignored("is/this/not/as/deep"); refute_is_ignored("this/is/deepish"); refute_is_ignored("xthis/is/deep"); } void test_ignore_status__automatically_ignore_bad_files(void) { g_repo = cl_git_sandbox_init("empty_standard_repo"); assert_is_ignored(".git"); assert_is_ignored("this/file/."); assert_is_ignored("path/../funky"); refute_is_ignored("path/whatever.c"); cl_git_pass(git_ignore_add_rule(g_repo, "*.c\n")); assert_is_ignored(".git"); assert_is_ignored("this/file/."); assert_is_ignored("path/../funky"); assert_is_ignored("path/whatever.c"); cl_git_pass(git_ignore_clear_internal_rules(g_repo)); assert_is_ignored(".git"); assert_is_ignored("this/file/."); assert_is_ignored("path/../funky"); refute_is_ignored("path/whatever.c"); } void test_ignore_status__filenames_with_special_prefixes_do_not_interfere_with_status_retrieval(void) { status_entry_single st; char *test_cases[] = { "!file", "#blah", "[blah]", "[attr]", "[attr]blah", NULL }; int i; for (i = 0; *(test_cases + i) != NULL; i++) { git_str file = GIT_STR_INIT; char *file_name = *(test_cases + i); git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_pass(git_str_joinpath(&file, "empty_standard_repo", file_name)); cl_git_mkfile(git_str_cstr(&file), "Please don't ignore me!"); memset(&st, 0, sizeof(st)); cl_git_pass(git_status_foreach(repo, cb_status__single, &st)); cl_assert(st.count == 1); cl_assert(st.status == GIT_STATUS_WT_NEW); cl_git_pass(git_status_file(&st.status, repo, file_name)); cl_assert(st.status == GIT_STATUS_WT_NEW); cl_git_sandbox_cleanup(); git_str_dispose(&file); } } void test_ignore_status__issue_1766_negated_ignores(void) { unsigned int status; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_pass(git_futils_mkdir_r( "empty_standard_repo/a", 0775)); cl_git_mkfile( "empty_standard_repo/a/.gitignore", "*\n!.gitignore\n"); cl_git_mkfile( "empty_standard_repo/a/ignoreme", "I should be ignored\n"); refute_is_ignored("a/.gitignore"); assert_is_ignored("a/ignoreme"); cl_git_pass(git_futils_mkdir_r( "empty_standard_repo/b", 0775)); cl_git_mkfile( "empty_standard_repo/b/.gitignore", "*\n!.gitignore\n"); cl_git_mkfile( "empty_standard_repo/b/ignoreme", "I should be ignored\n"); refute_is_ignored("b/.gitignore"); assert_is_ignored("b/ignoreme"); /* shouldn't have changed results from first couple either */ refute_is_ignored("a/.gitignore"); assert_is_ignored("a/ignoreme"); /* status should find the two ignore files and nothing else */ cl_git_pass(git_status_file(&status, g_repo, "a/.gitignore")); cl_assert_equal_i(GIT_STATUS_WT_NEW, (int)status); cl_git_pass(git_status_file(&status, g_repo, "a/ignoreme")); cl_assert_equal_i(GIT_STATUS_IGNORED, (int)status); cl_git_pass(git_status_file(&status, g_repo, "b/.gitignore")); cl_assert_equal_i(GIT_STATUS_WT_NEW, (int)status); cl_git_pass(git_status_file(&status, g_repo, "b/ignoreme")); cl_assert_equal_i(GIT_STATUS_IGNORED, (int)status); { git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *paths[] = { "a/.gitignore", "a/ignoreme", "b/.gitignore", "b/ignoreme", }; static const unsigned int statuses[] = { GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, }; memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 4; counts.expected_paths = paths; counts.expected_statuses = statuses; opts.flags = GIT_STATUS_OPT_DEFAULTS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } } static void add_one_to_index(const char *file) { git_index *index; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_add_bypath(index, file)); git_index_free(index); } /* Some further broken scenarios that have been reported */ void test_ignore_status__more_breakage(void) { static const char *test_files[] = { "empty_standard_repo/d1/pfx-d2/d3/d4/d5/tracked", "empty_standard_repo/d1/pfx-d2/d3/d4/d5/untracked", "empty_standard_repo/d1/pfx-d2/d3/d4/untracked", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "/d1/pfx-*\n" "!/d1/pfx-d2/\n" "/d1/pfx-d2/*\n" "!/d1/pfx-d2/d3/\n" "/d1/pfx-d2/d3/*\n" "!/d1/pfx-d2/d3/d4/\n"); add_one_to_index("d1/pfx-d2/d3/d4/d5/tracked"); { git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *files[] = { ".gitignore", "d1/pfx-d2/d3/d4/d5/tracked", "d1/pfx-d2/d3/d4/d5/untracked", "d1/pfx-d2/d3/d4/untracked", }; static const unsigned int statuses[] = { GIT_STATUS_WT_NEW, GIT_STATUS_INDEX_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, }; memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 4; counts.expected_paths = files; counts.expected_statuses = statuses; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_INCLUDE_IGNORED | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } refute_is_ignored("d1/pfx-d2/d3/d4/d5/tracked"); refute_is_ignored("d1/pfx-d2/d3/d4/d5/untracked"); refute_is_ignored("d1/pfx-d2/d3/d4/untracked"); } void test_ignore_status__negative_ignores_inside_ignores(void) { static const char *test_files[] = { "empty_standard_repo/top/mid/btm/tracked", "empty_standard_repo/top/mid/btm/untracked", "empty_standard_repo/zoo/bar", "empty_standard_repo/zoo/foo/bar", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "top\n" "!top/mid/btm\n" "zoo/*\n" "!zoo/bar\n" "!zoo/foo/bar\n"); add_one_to_index("top/mid/btm/tracked"); { git_status_options opts = GIT_STATUS_OPTIONS_INIT; status_entry_counts counts; static const char *files[] = { ".gitignore", "top/mid/btm/tracked", "top/mid/btm/untracked", "zoo/bar", "zoo/foo/bar", }; static const unsigned int statuses[] = { GIT_STATUS_WT_NEW, GIT_STATUS_INDEX_NEW, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, }; memset(&counts, 0x0, sizeof(status_entry_counts)); counts.expected_entry_count = 5; counts.expected_paths = files; counts.expected_statuses = statuses; opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_INCLUDE_IGNORED | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS; cl_git_pass(git_status_foreach_ext( g_repo, &opts, cb_status__normal, &counts)); cl_assert_equal_i(counts.expected_entry_count, counts.entry_count); cl_assert_equal_i(0, counts.wrong_status_flags_count); cl_assert_equal_i(0, counts.wrong_sorted_path); } assert_is_ignored("top/mid/btm/tracked"); assert_is_ignored("top/mid/btm/untracked"); refute_is_ignored("foo/bar"); } void test_ignore_status__negative_ignores_in_slash_star(void) { git_status_options status_opts = GIT_STATUS_OPTIONS_INIT; git_status_list *list; int found_look_ma = 0, found_what_about = 0; size_t i; static const char *test_files[] = { "empty_standard_repo/bin/look-ma.txt", "empty_standard_repo/bin/what-about-me.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "bin/*\n" "!bin/w*\n"); assert_is_ignored("bin/look-ma.txt"); refute_is_ignored("bin/what-about-me.txt"); status_opts.flags = GIT_STATUS_OPT_DEFAULTS; cl_git_pass(git_status_list_new(&list, g_repo, &status_opts)); for (i = 0; i < git_status_list_entrycount(list); i++) { const git_status_entry *entry = git_status_byindex(list, i); if (!strcmp("bin/look-ma.txt", entry->index_to_workdir->new_file.path)) found_look_ma = 1; if (!strcmp("bin/what-about-me.txt", entry->index_to_workdir->new_file.path)) found_what_about = 1; } git_status_list_free(list); cl_assert(found_look_ma); cl_assert(found_what_about); } void test_ignore_status__negative_ignores_without_trailing_slash_inside_ignores(void) { git_status_options status_opts = GIT_STATUS_OPTIONS_INIT; git_status_list *list; int found_parent_file = 0, found_parent_child1_file = 0, found_parent_child2_file = 0; size_t i; static const char *test_files[] = { "empty_standard_repo/parent/file.txt", "empty_standard_repo/parent/force.txt", "empty_standard_repo/parent/child1/file.txt", "empty_standard_repo/parent/child2/file.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "parent/*\n" "!parent/force.txt\n" "!parent/child1\n" "!parent/child2/\n"); add_one_to_index("parent/force.txt"); assert_is_ignored("parent/file.txt"); refute_is_ignored("parent/force.txt"); refute_is_ignored("parent/child1/file.txt"); refute_is_ignored("parent/child2/file.txt"); status_opts.flags = GIT_STATUS_OPT_DEFAULTS; cl_git_pass(git_status_list_new(&list, g_repo, &status_opts)); for (i = 0; i < git_status_list_entrycount(list); i++) { const git_status_entry *entry = git_status_byindex(list, i); if (!entry->index_to_workdir) continue; if (!strcmp("parent/file.txt", entry->index_to_workdir->new_file.path)) found_parent_file = 1; if (!strcmp("parent/force.txt", entry->index_to_workdir->new_file.path)) found_parent_file = 1; if (!strcmp("parent/child1/file.txt", entry->index_to_workdir->new_file.path)) found_parent_child1_file = 1; if (!strcmp("parent/child2/file.txt", entry->index_to_workdir->new_file.path)) found_parent_child2_file = 1; } git_status_list_free(list); cl_assert(found_parent_file); cl_assert(found_parent_child1_file); cl_assert(found_parent_child2_file); } void test_ignore_status__negative_directory_ignores(void) { static const char *test_files[] = { "empty_standard_repo/parent/child1/bar.txt", "empty_standard_repo/parent/child2/bar.txt", "empty_standard_repo/parent/child3/foo.txt", "empty_standard_repo/parent/child4/bar.txt", "empty_standard_repo/parent/nested/child5/bar.txt", "empty_standard_repo/parent/nested/child6/bar.txt", "empty_standard_repo/parent/nested/child7/bar.txt", "empty_standard_repo/padded_parent/child8/bar.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "foo.txt\n" "parent/child1\n" "parent/child2\n" "parent/child4\n" "parent/nested/child5\n" "nested/child6\n" "nested/child7\n" "padded_parent/child8\n" /* test simple exact match */ "!parent/child1\n" /* test negating file without negating dir */ "!parent/child2/bar.txt\n" /* test negative pattern on dir with its content * being ignored */ "!parent/child3\n" /* test with partial match at end */ "!child4\n" /* test with partial match with '/' at end */ "!nested/child5\n" /* test with complete match */ "!nested/child6\n" /* test with trailing '/' */ "!child7/\n" /* test with partial dir match */ "!_parent/child8\n"); refute_is_ignored("parent/child1/bar.txt"); assert_is_ignored("parent/child2/bar.txt"); assert_is_ignored("parent/child3/foo.txt"); refute_is_ignored("parent/child4/bar.txt"); assert_is_ignored("parent/nested/child5/bar.txt"); refute_is_ignored("parent/nested/child6/bar.txt"); refute_is_ignored("parent/nested/child7/bar.txt"); assert_is_ignored("padded_parent/child8/bar.txt"); } void test_ignore_status__unignore_entry_in_ignored_dir(void) { static const char *test_files[] = { "empty_standard_repo/bar.txt", "empty_standard_repo/parent/bar.txt", "empty_standard_repo/parent/child/bar.txt", "empty_standard_repo/nested/parent/child/bar.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "bar.txt\n" "!parent/child/bar.txt\n"); assert_is_ignored("bar.txt"); assert_is_ignored("parent/bar.txt"); refute_is_ignored("parent/child/bar.txt"); assert_is_ignored("nested/parent/child/bar.txt"); } void test_ignore_status__do_not_unignore_basename_prefix(void) { static const char *test_files[] = { "empty_standard_repo/foo_bar.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "foo_bar.txt\n" "!bar.txt\n"); assert_is_ignored("foo_bar.txt"); } void test_ignore_status__filename_with_cr(void) { int ignored; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile("empty_standard_repo/.gitignore", "Icon\r\r\n"); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r")); cl_assert_equal_i(1, ignored); cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\n"); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn")); cl_assert_equal_i(1, ignored); cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\r\n"); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn")); cl_assert_equal_i(1, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn\r")); cl_assert_equal_i(0, ignored); cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\r\r\n"); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn\r")); cl_assert_equal_i(1, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r")); cl_assert_equal_i(0, ignored); cl_git_mkfile("empty_standard_repo/.gitignore", "Icon\r\n"); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r")); cl_assert_equal_i(0, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon")); cl_assert_equal_i(1, ignored); } void test_ignore_status__subdir_doesnt_match_above(void) { int ignored, icase = 0, error; git_config *cfg; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_pass(git_repository_config_snapshot(&cfg, g_repo)); error = git_config_get_bool(&icase, cfg, "core.ignorecase"); git_config_free(cfg); if (error == GIT_ENOTFOUND) error = 0; cl_git_pass(error); cl_git_pass(p_mkdir("empty_standard_repo/src", 0777)); cl_git_pass(p_mkdir("empty_standard_repo/src/src", 0777)); cl_git_mkfile("empty_standard_repo/src/.gitignore", "src\n"); cl_git_mkfile("empty_standard_repo/.gitignore", ""); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/test.txt")); cl_assert_equal_i(0, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/src/test.txt")); cl_assert_equal_i(1, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/foo/test.txt")); cl_assert_equal_i(0, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "SRC/src/test.txt")); cl_assert_equal_i(icase, ignored); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/SRC/test.txt")); cl_assert_equal_i(icase, ignored); } void test_ignore_status__negate_exact_previous(void) { int ignored; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile("empty_standard_repo/.gitignore", "*.com\ntags\n!tags/\n.buildpath"); cl_git_mkfile("empty_standard_repo/.buildpath", ""); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, ".buildpath")); cl_assert_equal_i(1, ignored); } void test_ignore_status__negate_starstar(void) { int ignored; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile("empty_standard_repo/.gitignore", "code/projects/**/packages/*\n" "!code/projects/**/packages/repositories.config"); cl_git_pass(git_futils_mkdir_r("empty_standard_repo/code/projects/foo/bar/packages", 0777)); cl_git_mkfile("empty_standard_repo/code/projects/foo/bar/packages/repositories.config", ""); cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "code/projects/foo/bar/packages/repositories.config")); cl_assert_equal_i(0, ignored); } void test_ignore_status__ignore_all_toplevel_dirs_include_files(void) { static const char *test_files[] = { "empty_standard_repo/README.md", "empty_standard_repo/src/main.c", "empty_standard_repo/src/foo/foo.c", "empty_standard_repo/dist/foo.o", "empty_standard_repo/dist/main.o", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "/*/\n" "!/src\n"); assert_is_ignored("dist/foo.o"); assert_is_ignored("dist/main.o"); refute_is_ignored("README.md"); refute_is_ignored("src/foo.c"); refute_is_ignored("src/foo/foo.c"); } void test_ignore_status__subdir_ignore_all_toplevel_dirs_include_files(void) { static const char *test_files[] = { "empty_standard_repo/project/README.md", "empty_standard_repo/project/src/main.c", "empty_standard_repo/project/src/foo/foo.c", "empty_standard_repo/project/dist/foo.o", "empty_standard_repo/project/dist/main.o", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/project/.gitignore", "/*/\n" "!/src\n"); assert_is_ignored("project/dist/foo.o"); assert_is_ignored("project/dist/main.o"); refute_is_ignored("project/src/foo.c"); refute_is_ignored("project/src/foo/foo.c"); refute_is_ignored("project/README.md"); } void test_ignore_status__subdir_ignore_everything_except_certain_files(void) { static const char *test_files[] = { "empty_standard_repo/project/README.md", "empty_standard_repo/project/some_file", "empty_standard_repo/project/src/main.c", "empty_standard_repo/project/src/foo/foo.c", "empty_standard_repo/project/dist/foo.o", "empty_standard_repo/project/dist/main.o", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/project/.gitignore", "/*\n" "!/src\n" "!README.md\n"); assert_is_ignored("project/some_file"); assert_is_ignored("project/dist/foo.o"); assert_is_ignored("project/dist/main.o"); refute_is_ignored("project/README.md"); refute_is_ignored("project/src/foo.c"); refute_is_ignored("project/src/foo/foo.c"); } void test_ignore_status__deeper(void) { const char *test_files[] = { "empty_standard_repo/foo.data", "empty_standard_repo/bar.data", "empty_standard_repo/dont_ignore/foo.data", "empty_standard_repo/dont_ignore/bar.data", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile("empty_standard_repo/.gitignore", "*.data\n" "!dont_ignore/*.data\n"); assert_is_ignored("foo.data"); assert_is_ignored("bar.data"); refute_is_ignored("dont_ignore/foo.data"); refute_is_ignored("dont_ignore/bar.data"); } void test_ignore_status__unignored_dir_with_ignored_contents(void) { static const char *test_files[] = { "empty_standard_repo/dir/a.test", "empty_standard_repo/dir/subdir/a.test", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "*.test\n" "!dir/*\n"); refute_is_ignored("dir/a.test"); assert_is_ignored("dir/subdir/a.test"); } void test_ignore_status__unignored_subdirs(void) { static const char *test_files[] = { "empty_standard_repo/dir/a.test", "empty_standard_repo/dir/subdir/a.test", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "dir/*\n" "!dir/*/\n"); assert_is_ignored("dir/a.test"); refute_is_ignored("dir/subdir/a.test"); } void test_ignore_status__skips_bom(void) { static const char *test_files[] = { "empty_standard_repo/a.test", "empty_standard_repo/b.test", "empty_standard_repo/c.test", "empty_standard_repo/foo.txt", "empty_standard_repo/bar.txt", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", "\xEF\xBB\xBF*.test\n"); assert_is_ignored("a.test"); assert_is_ignored("b.test"); assert_is_ignored("c.test"); refute_is_ignored("foo.txt"); refute_is_ignored("bar.txt"); } void test_ignore_status__leading_spaces_are_significant(void) { static const char *test_files[] = { "empty_standard_repo/a.test", "empty_standard_repo/b.test", "empty_standard_repo/c.test", "empty_standard_repo/d.test", NULL }; make_test_data("empty_standard_repo", test_files); cl_git_mkfile( "empty_standard_repo/.gitignore", " a.test\n" "# this is a comment\n" "b.test\n" "\tc.test\n" " # not a comment\n" "d.test\n"); refute_is_ignored("a.test"); assert_is_ignored(" a.test"); refute_is_ignored("# this is a comment"); assert_is_ignored("b.test"); refute_is_ignored("c.test"); assert_is_ignored("\tc.test"); assert_is_ignored(" # not a comment"); assert_is_ignored("d.test"); } void test_ignore_status__override_nested_wildcard_unignore(void) { git_repository *repo = cl_git_sandbox_init("empty_standard_repo"); git_status_list *statuslist; git_status_options opts = GIT_STATUS_OPTIONS_INIT; const git_status_entry *status; cl_git_pass(git_futils_mkdir_r("empty_standard_repo/dir", 0777)); cl_git_pass(git_futils_mkdir_r("empty_standard_repo/dir/subdir", 0777)); cl_git_mkfile("empty_standard_repo/.gitignore", "a.test\n"); cl_git_mkfile("empty_standard_repo/dir/.gitignore", "!*.test\n"); cl_git_mkfile("empty_standard_repo/dir/subdir/.gitignore", "a.test\n"); cl_git_mkfile("empty_standard_repo/dir/a.test", "pong"); cl_git_mkfile("empty_standard_repo/dir/subdir/a.test", "pong"); opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; cl_git_pass(git_status_list_new(&statuslist, repo, &opts)); cl_assert_equal_sz(4, git_status_list_entrycount(statuslist)); status = git_status_byindex(statuslist, 0); cl_assert(status != NULL); cl_assert_equal_s(".gitignore", status->index_to_workdir->old_file.path); cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status); status = git_status_byindex(statuslist, 1); cl_assert(status != NULL); cl_assert_equal_s("dir/.gitignore", status->index_to_workdir->old_file.path); cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status); status = git_status_byindex(statuslist, 2); cl_assert(status != NULL); cl_assert_equal_s("dir/a.test", status->index_to_workdir->old_file.path); cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status); status = git_status_byindex(statuslist, 3); cl_assert(status != NULL); cl_assert_equal_s("dir/subdir/.gitignore", status->index_to_workdir->old_file.path); cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status); git_status_list_free(statuslist); }
libgit2-main
tests/libgit2/ignore/status.c
#include "clar_libgit2.h" #include "path.h" void test_path_validate__cleanup(void) { cl_git_sandbox_cleanup(); } void test_path_validate__length(void) { cl_must_pass(git_path_validate_length(NULL, "/foo/bar")); cl_must_pass(git_path_validate_length(NULL, "C:\\Foo\\Bar")); cl_must_pass(git_path_validate_length(NULL, "\\\\?\\C:\\Foo\\Bar")); cl_must_pass(git_path_validate_length(NULL, "\\\\?\\C:\\Foo\\Bar")); cl_must_pass(git_path_validate_length(NULL, "\\\\?\\UNC\\server\\C$\\folder")); #ifdef GIT_WIN32 /* * In the absence of a repo configuration, 259 character paths * succeed. >= 260 character paths fail. */ cl_must_pass(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\ok.txt")); cl_must_pass(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\260.txt")); cl_must_fail(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\longer_than_260.txt")); /* count characters, not bytes */ cl_must_pass(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\\260.txt")); cl_must_fail(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\\long.txt")); #else cl_must_pass(git_path_validate_length(NULL, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/ok.txt")); cl_must_pass(git_path_validate_length(NULL, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/260.txt")); cl_must_pass(git_path_validate_length(NULL, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/longer_than_260.txt")); cl_must_pass(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\\260.txt")); cl_must_pass(git_path_validate_length(NULL, "C:\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\aaaaaaaaa\\\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\xc2\xa2\\long.txt")); #endif } void test_path_validate__length_with_core_longpath(void) { #ifdef GIT_WIN32 git_repository *repo; git_config *config; repo = cl_git_sandbox_init("empty_bare.git"); cl_git_pass(git_repository_open(&repo, "empty_bare.git")); cl_git_pass(git_repository_config(&config, repo)); /* fail by default */ cl_must_fail(git_path_validate_length(repo, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/longer_than_260.txt")); /* set core.longpaths explicitly on */ cl_git_pass(git_config_set_bool(config, "core.longpaths", 1)); cl_must_pass(git_path_validate_length(repo, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/longer_than_260.txt")); /* set core.longpaths explicitly off */ cl_git_pass(git_config_set_bool(config, "core.longpaths", 0)); cl_must_fail(git_path_validate_length(repo, "/c/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/aaaaaaaaa/longer_than_260.txt")); git_config_free(config); git_repository_free(repo); #endif }
libgit2-main
tests/libgit2/path/validate.c
#include "clar_libgit2.h" #include "transports/smart.h" enum expected_status { PARSE_SUCCESS, PARSE_FAILURE }; static void assert_flush_parses(const char *line) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_FLUSH); cl_assert_equal_strn(endptr, line + 4, linelen - 4); git_pkt_free((git_pkt *) pkt); } static void assert_data_pkt_parses(const char *line, const char *expected_data, size_t expected_len) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_data *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_DATA); cl_assert_equal_i(pkt->len, expected_len); cl_assert_equal_strn(pkt->data, expected_data, expected_len); git_pkt_free((git_pkt *) pkt); } static void assert_sideband_progress_parses(const char *line, const char *expected_data, size_t expected_len) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_progress *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_PROGRESS); cl_assert_equal_i(pkt->len, expected_len); cl_assert_equal_strn(pkt->data, expected_data, expected_len); git_pkt_free((git_pkt *) pkt); } static void assert_error_parses(const char *line, const char *expected_error, size_t expected_len) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_err *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_ERR); cl_assert_equal_i(pkt->len, expected_len); cl_assert_equal_strn(pkt->error, expected_error, expected_len); git_pkt_free((git_pkt *) pkt); } static void assert_ack_parses(const char *line, const char *expected_oid, enum git_ack_status expected_status) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_ack *pkt; git_oid oid; cl_git_pass(git_oid__fromstr(&oid, expected_oid, GIT_OID_SHA1)); cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_ACK); cl_assert_equal_oid(&pkt->oid, &oid); cl_assert_equal_i(pkt->status, expected_status); git_pkt_free((git_pkt *) pkt); } static void assert_nak_parses(const char *line) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_NAK); cl_assert_equal_strn(endptr, line + 7, linelen - 7); git_pkt_free((git_pkt *) pkt); } static void assert_comment_parses(const char *line, const char *expected_comment) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_comment *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_COMMENT); cl_assert_equal_strn(pkt->comment, expected_comment, strlen(expected_comment)); git_pkt_free((git_pkt *) pkt); } static void assert_ok_parses(const char *line, const char *expected_ref) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_ok *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_OK); cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref)); git_pkt_free((git_pkt *) pkt); } static void assert_unpack_parses(const char *line, bool ok) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_unpack *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_UNPACK); cl_assert_equal_i(pkt->unpack_ok, ok); git_pkt_free((git_pkt *) pkt); } static void assert_ng_parses(const char *line, const char *expected_ref, const char *expected_msg) { size_t linelen = strlen(line) + 1; const char *endptr; git_pkt_ng *pkt; cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_NG); cl_assert_equal_strn(pkt->ref, expected_ref, strlen(expected_ref)); cl_assert_equal_strn(pkt->msg, expected_msg, strlen(expected_msg)); git_pkt_free((git_pkt *) pkt); } #define assert_ref_parses(line, expected_oid, expected_ref, expected_capabilities) \ assert_ref_parses_(line, sizeof(line), expected_oid, expected_ref, expected_capabilities) static void assert_ref_parses_(const char *line, size_t linelen, const char *expected_oid, const char *expected_ref, const char *expected_capabilities) { const char *endptr; git_pkt_ref *pkt; git_oid oid; cl_git_pass(git_oid__fromstr(&oid, expected_oid, GIT_OID_SHA1)); cl_git_pass(git_pkt_parse_line((git_pkt **) &pkt, &endptr, line, linelen)); cl_assert_equal_i(pkt->type, GIT_PKT_REF); cl_assert_equal_oid(&pkt->head.oid, &oid); cl_assert_equal_strn(pkt->head.name, expected_ref, strlen(expected_ref)); if (expected_capabilities) cl_assert_equal_strn(pkt->capabilities, expected_capabilities, strlen(expected_capabilities)); else cl_assert_equal_p(NULL, pkt->capabilities); git_pkt_free((git_pkt *) pkt); } static void assert_pkt_fails(const char *line) { const char *endptr; git_pkt *pkt; cl_git_fail(git_pkt_parse_line(&pkt, &endptr, line, strlen(line) + 1)); } void test_transports_smart_packet__parsing_garbage_fails(void) { assert_pkt_fails("0foobar"); assert_pkt_fails("00foobar"); assert_pkt_fails("000foobar"); assert_pkt_fails("0001"); assert_pkt_fails(""); assert_pkt_fails("0"); assert_pkt_fails("0i00"); assert_pkt_fails("f"); } void test_transports_smart_packet__flush_parses(void) { assert_flush_parses("0000"); assert_flush_parses("0000foobar"); } void test_transports_smart_packet__data_pkt(void) { assert_pkt_fails("000foobar"); assert_pkt_fails("0001o"); assert_pkt_fails("0001\1"); assert_data_pkt_parses("0005\1", "", 0); assert_pkt_fails("0009\1o"); assert_data_pkt_parses("0009\1data", "data", 4); assert_data_pkt_parses("000a\1data", "data", 5); } void test_transports_smart_packet__sideband_progress_pkt(void) { assert_pkt_fails("0001\2"); assert_sideband_progress_parses("0005\2", "", 0); assert_pkt_fails("0009\2o"); assert_sideband_progress_parses("0009\2data", "data", 4); assert_sideband_progress_parses("000a\2data", "data", 5); } void test_transports_smart_packet__sideband_err_pkt(void) { assert_pkt_fails("0001\3"); assert_error_parses("0005\3", "", 0); assert_pkt_fails("0009\3o"); assert_error_parses("0009\3data", "data", 4); assert_error_parses("000a\3data", "data", 5); } void test_transports_smart_packet__ack_pkt(void) { assert_ack_parses("0030ACK 0000000000000000000000000000000000000000", "0000000000000000000000000000000000000000", 0); assert_ack_parses("0039ACK 0000000000000000000000000000000000000000 continue", "0000000000000000000000000000000000000000", GIT_ACK_CONTINUE); assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 common", "0000000000000000000000000000000000000000", GIT_ACK_COMMON); assert_ack_parses("0037ACK 0000000000000000000000000000000000000000 ready", "0000000000000000000000000000000000000000", GIT_ACK_READY); /* these should fail as they don't have OIDs */ assert_pkt_fails("0007ACK"); assert_pkt_fails("0008ACK "); /* this one is missing a space and should thus fail */ assert_pkt_fails("0036ACK00000000000000000x0000000000000000000000 ready"); /* the following ones have invalid OIDs and should thus fail */ assert_pkt_fails("0037ACK 00000000000000000x0000000000000000000000 ready"); assert_pkt_fails("0036ACK 000000000000000000000000000000000000000 ready"); assert_pkt_fails("0036ACK 00000000000000000x0000000000000000000000ready"); /* this one has an invalid status and should thus fail */ assert_pkt_fails("0036ACK 0000000000000000000000000000000000000000 read"); } void test_transports_smart_packet__nak_pkt(void) { assert_nak_parses("0007NAK"); assert_pkt_fails("0007NaK"); assert_pkt_fails("0007nak"); assert_nak_parses("0007NAKfoobar"); assert_pkt_fails("0007nakfoobar"); assert_pkt_fails("0007 NAK"); } void test_transports_smart_packet__error_pkt(void) { assert_pkt_fails("0007ERR"); assert_pkt_fails("0008ERRx"); assert_error_parses("0008ERR ", "", 0); assert_error_parses("000EERR ERRMSG", "ERRMSG", 6); } void test_transports_smart_packet__comment_pkt(void) { assert_comment_parses("0005#", ""); assert_comment_parses("000B#foobar", "#fooba"); assert_comment_parses("000C#foobar", "#foobar"); assert_comment_parses("001A#this is a comment\nfoo", "#this is a comment\nfoo"); } void test_transports_smart_packet__ok_pkt(void) { assert_pkt_fails("0007ok\n"); assert_ok_parses("0007ok ", ""); assert_ok_parses("0008ok \n", ""); assert_ok_parses("0008ok x", "x"); assert_ok_parses("0009ok x\n", "x"); assert_pkt_fails("001OK ref/foo/bar"); assert_ok_parses("0012ok ref/foo/bar", "ref/foo/bar"); assert_pkt_fails("0013OK ref/foo/bar\n"); assert_ok_parses("0013ok ref/foo/bar\n", "ref/foo/bar"); } void test_transports_smart_packet__ng_pkt(void) { /* TODO: same as for ok pkt */ assert_pkt_fails("0007ng\n"); assert_pkt_fails("0008ng \n"); assert_pkt_fails("000Bng ref\n"); assert_pkt_fails("000Bng ref\n"); /* TODO: is this a valid packet line? Probably not. */ assert_ng_parses("000Ang x\n", "", "x"); assert_ng_parses("000Fng ref msg\n", "ref", "msg"); assert_ng_parses("000Fng ref msg\n", "ref", "msg"); } void test_transports_smart_packet__unpack_pkt(void) { assert_unpack_parses("000Dunpack ok", 1); assert_unpack_parses("000Dunpack ng error-msg", 0); /* TODO: the following tests should fail */ assert_unpack_parses("000Aunpack", 0); assert_unpack_parses("0011unpack foobar", 0); assert_unpack_parses("0010unpack ng ok", 0); assert_unpack_parses("0010unpack okfoo", 1); } void test_transports_smart_packet__ref_pkt(void) { assert_pkt_fails("002C0000000000000000000000000000000000000000"); assert_pkt_fails("002D0000000000000000000000000000000000000000\n"); assert_pkt_fails("00300000000000000000000000000000000000000000HEAD"); assert_pkt_fails("004800000000x0000000000000000000000000000000 refs/heads/master\0multi_ack"); assert_ref_parses( "003F0000000000000000000000000000000000000000 refs/heads/master\0", "0000000000000000000000000000000000000000", "refs/heads/master", ""); assert_ref_parses( "00480000000000000000000000000000000000000000 refs/heads/master\0multi_ack", "0000000000000000000000000000000000000000", "refs/heads/master", "multi_ack"); assert_ref_parses( "00460000000000000000000000000000000000000000 refs/heads/master\0one two", "0000000000000000000000000000000000000000", "refs/heads/master", "one two"); assert_ref_parses( "00310000000000000000000000000000000000000000 HEAD", "0000000000000000000000000000000000000000", "HEAD", NULL); assert_pkt_fails("0031000000000000000000000000000000000000000 HEAD"); assert_ref_parses( "00360000000000000000000000000000000000000000 HEAD HEAD", "0000000000000000000000000000000000000000", "HEAD HEAD", NULL); }
libgit2-main
tests/libgit2/transports/smart/packet.c
#include "clar_libgit2.h" #include "git2/sys/stream.h" #include "streams/tls.h" #include "streams/socket.h" #include "stream.h" void test_stream_deprecated__cleanup(void) { cl_git_pass(git_stream_register(GIT_STREAM_TLS | GIT_STREAM_STANDARD, NULL)); } #ifndef GIT_DEPRECATE_HARD static git_stream test_stream; static int ctor_called; static int test_stream_init(git_stream **out, const char *host, const char *port) { GIT_UNUSED(host); GIT_UNUSED(port); ctor_called = 1; *out = &test_stream; return 0; } #endif void test_stream_deprecated__register_tls(void) { #ifndef GIT_DEPRECATE_HARD git_stream *stream; int error; ctor_called = 0; cl_git_pass(git_stream_register_tls(test_stream_init)); cl_git_pass(git_tls_stream_new(&stream, "localhost", "443")); cl_assert_equal_i(1, ctor_called); cl_assert_equal_p(&test_stream, stream); ctor_called = 0; stream = NULL; cl_git_pass(git_stream_register_tls(NULL)); error = git_tls_stream_new(&stream, "localhost", "443"); /* * We don't have TLS support enabled, or we're on Windows, * which has no arbitrary TLS stream support. */ #if defined(GIT_WIN32) || !defined(GIT_HTTPS) cl_git_fail_with(-1, error); #else cl_git_pass(error); #endif cl_assert_equal_i(0, ctor_called); cl_assert(&test_stream != stream); git_stream_free(stream); #endif }
libgit2-main
tests/libgit2/stream/deprecated.c
#include "clar_libgit2.h" #include "git2/sys/stream.h" #include "streams/tls.h" #include "streams/socket.h" #include "stream.h" static git_stream test_stream; static int ctor_called; void test_stream_registration__cleanup(void) { cl_git_pass(git_stream_register(GIT_STREAM_TLS | GIT_STREAM_STANDARD, NULL)); } static int test_stream_init(git_stream **out, const char *host, const char *port) { GIT_UNUSED(host); GIT_UNUSED(port); ctor_called = 1; *out = &test_stream; return 0; } static int test_stream_wrap(git_stream **out, git_stream *in, const char *host) { GIT_UNUSED(in); GIT_UNUSED(host); ctor_called = 1; *out = &test_stream; return 0; } void test_stream_registration__insecure(void) { git_stream *stream; git_stream_registration registration = {0}; registration.version = 1; registration.init = test_stream_init; registration.wrap = test_stream_wrap; ctor_called = 0; cl_git_pass(git_stream_register(GIT_STREAM_STANDARD, &registration)); cl_git_pass(git_socket_stream_new(&stream, "localhost", "80")); cl_assert_equal_i(1, ctor_called); cl_assert_equal_p(&test_stream, stream); ctor_called = 0; stream = NULL; cl_git_pass(git_stream_register(GIT_STREAM_STANDARD, NULL)); cl_git_pass(git_socket_stream_new(&stream, "localhost", "80")); cl_assert_equal_i(0, ctor_called); cl_assert(&test_stream != stream); git_stream_free(stream); } void test_stream_registration__tls(void) { git_stream *stream; git_stream_registration registration = {0}; int error; registration.version = 1; registration.init = test_stream_init; registration.wrap = test_stream_wrap; ctor_called = 0; cl_git_pass(git_stream_register(GIT_STREAM_TLS, &registration)); cl_git_pass(git_tls_stream_new(&stream, "localhost", "443")); cl_assert_equal_i(1, ctor_called); cl_assert_equal_p(&test_stream, stream); ctor_called = 0; stream = NULL; cl_git_pass(git_stream_register(GIT_STREAM_TLS, NULL)); error = git_tls_stream_new(&stream, "localhost", "443"); /* We don't have TLS support enabled, or we're on Windows, * which has no arbitrary TLS stream support. */ #if defined(GIT_WIN32) || !defined(GIT_HTTPS) cl_git_fail_with(-1, error); #else cl_git_pass(error); #endif cl_assert_equal_i(0, ctor_called); cl_assert(&test_stream != stream); git_stream_free(stream); } void test_stream_registration__both(void) { git_stream *stream; git_stream_registration registration = {0}; registration.version = 1; registration.init = test_stream_init; registration.wrap = test_stream_wrap; cl_git_pass(git_stream_register(GIT_STREAM_STANDARD | GIT_STREAM_TLS, &registration)); ctor_called = 0; cl_git_pass(git_tls_stream_new(&stream, "localhost", "443")); cl_assert_equal_i(1, ctor_called); cl_assert_equal_p(&test_stream, stream); ctor_called = 0; cl_git_pass(git_socket_stream_new(&stream, "localhost", "80")); cl_assert_equal_i(1, ctor_called); cl_assert_equal_p(&test_stream, stream); }
libgit2-main
tests/libgit2/stream/registration.c
#include "clar_libgit2.h" #include "posix.h" #include "reset_helpers.h" #include "path.h" static git_repository *repo; static git_object *target; void test_reset_mixed__initialize(void) { repo = cl_git_sandbox_init("attr"); target = NULL; } void test_reset_mixed__cleanup(void) { git_object_free(target); target = NULL; cl_git_sandbox_cleanup(); } void test_reset_mixed__cannot_reset_in_a_bare_repository(void) { git_repository *bare; cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); cl_assert(git_repository_is_bare(bare) == true); cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO)); cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED, NULL)); git_repository_free(bare); } void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void) { unsigned int status; cl_git_pass(git_status_file(&status, repo, "macro_bad")); cl_assert(status == GIT_STATUS_CURRENT); cl_git_pass(git_revparse_single(&target, repo, "605812a")); cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL)); cl_git_pass(git_status_file(&status, repo, "macro_bad")); cl_assert(status == GIT_STATUS_WT_NEW); } void test_reset_mixed__reflog_is_correct(void) { git_str buf = GIT_STR_INIT; git_annotated_commit *annotated; const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context"; reflog_check(repo, "HEAD", 9, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 9, "[email protected]", exp_msg); /* Branch not moving, no reflog entry */ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL)); reflog_check(repo, "HEAD", 9, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 9, "[email protected]", exp_msg); git_object_free(target); target = NULL; /* Moved branch, expect id in message */ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); git_str_clear(&buf); cl_git_pass(git_str_printf(&buf, "reset: moving to %s", git_oid_tostr_s(git_object_id(target)))); cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL)); reflog_check(repo, "HEAD", 10, NULL, git_str_cstr(&buf)); reflog_check(repo, "refs/heads/master", 10, NULL, git_str_cstr(&buf)); git_str_dispose(&buf); /* Moved branch, expect revspec in message */ exp_msg = "reset: moving to HEAD~^{commit}"; cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}")); cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_MIXED, NULL)); reflog_check(repo, "HEAD", 11, NULL, exp_msg); reflog_check(repo, "refs/heads/master", 11, NULL, exp_msg); git_annotated_commit_free(annotated); }
libgit2-main
tests/libgit2/reset/mixed.c
#include "clar_libgit2.h" #include "posix.h" #include "reset_helpers.h" #include "path.h" static git_repository *_repo; static git_object *_target; static git_strarray _pathspecs; static git_index *_index; static void initialize(const char *repo_name) { _repo = cl_git_sandbox_init(repo_name); cl_git_pass(git_repository_index(&_index, _repo)); _target = NULL; _pathspecs.strings = NULL; _pathspecs.count = 0; } void test_reset_default__initialize(void) { } void test_reset_default__cleanup(void) { git_object_free(_target); _target = NULL; git_index_free(_index); _index = NULL; cl_git_sandbox_cleanup(); } static void assert_content_in_index( git_strarray *pathspecs, bool should_exist, git_strarray *expected_shas) { size_t i, pos; int error; for (i = 0; i < pathspecs->count; i++) { error = git_index_find(&pos, _index, pathspecs->strings[i]); if (should_exist) { const git_index_entry *entry; cl_assert(error != GIT_ENOTFOUND); entry = git_index_get_byindex(_index, pos); cl_assert(entry != NULL); if (!expected_shas) continue; cl_git_pass(git_oid_streq(&entry->id, expected_shas->strings[i])); } else cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND); } } void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_from_the_index(void) { char *paths[] = { "staged_changes", "staged_new_file" }; initialize("status"); _pathspecs.strings = paths; _pathspecs.count = 2; assert_content_in_index(&_pathspecs, true, NULL); cl_git_pass(git_reset_default(_repo, NULL, &_pathspecs)); assert_content_in_index(&_pathspecs, false, NULL); } /* * $ git ls-files --cached -s --abbrev=7 -- "staged*" * 100644 55d316c 0 staged_changes * 100644 a6be623 0 staged_changes_file_deleted * ... * * $ git reset 0017bd4 -- staged_changes staged_changes_file_deleted * Unstaged changes after reset: * ... * * $ git ls-files --cached -s --abbrev=7 -- "staged*" * 100644 32504b7 0 staged_changes * 100644 061d42a 0 staged_changes_file_deleted * ... */ void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_entries(void) { git_strarray before, after; char *paths[] = { "staged_changes", "staged_changes_file_deleted" }; char *before_shas[] = { "55d316c9ba708999f1918e9677d01dfcae69c6b9", "a6be623522ce87a1d862128ac42672604f7b468b" }; char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c", "061d42a44cacde5726057b67558821d95db96f19" }; initialize("status"); _pathspecs.strings = paths; _pathspecs.count = 2; before.strings = before_shas; before.count = 2; after.strings = after_shas; after.count = 2; cl_git_pass(git_revparse_single(&_target, _repo, "0017bd4")); assert_content_in_index(&_pathspecs, true, &before); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, true, &after); } /* * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt * 100644 1f85ca5 1 conflicts-one.txt * 100644 6aea5f2 2 conflicts-one.txt * 100644 516bd85 3 conflicts-one.txt * * $ git reset 9a05ccb -- conflicts-one.txt * Unstaged changes after reset: * ... * * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt * 100644 1f85ca5 0 conflicts-one.txt * */ void test_reset_default__resetting_filepaths_clears_previous_conflicts(void) { const git_index_entry *conflict_entry[3]; git_strarray after; char *paths[] = { "conflicts-one.txt" }; char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" }; initialize("mergedrepo"); _pathspecs.strings = paths; _pathspecs.count = 1; after.strings = after_shas; after.count = 1; cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt")); cl_git_pass(git_revparse_single(&_target, _repo, "9a05ccb")); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, true, &after); cl_assert_equal_i(GIT_ENOTFOUND, git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt")); } /* $ git reset HEAD -- "I_am_not_there.txt" "me_neither.txt" Unstaged changes after reset: ... */ void test_reset_default__resetting_unknown_filepaths_does_not_fail(void) { char *paths[] = { "I_am_not_there.txt", "me_neither.txt" }; initialize("status"); _pathspecs.strings = paths; _pathspecs.count = 2; assert_content_in_index(&_pathspecs, false, NULL); cl_git_pass(git_revparse_single(&_target, _repo, "HEAD")); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, false, NULL); } void test_reset_default__staged_rename_reset_delete(void) { git_index_entry entry; const git_index_entry *existing; char *paths[] = { "new.txt" }; initialize("testrepo2"); existing = git_index_get_bypath(_index, "new.txt", 0); cl_assert(existing); memcpy(&entry, existing, sizeof(entry)); cl_git_pass(git_index_remove_bypath(_index, "new.txt")); entry.path = "renamed.txt"; cl_git_pass(git_index_add(_index, &entry)); _pathspecs.strings = paths; _pathspecs.count = 1; assert_content_in_index(&_pathspecs, false, NULL); cl_git_pass(git_revparse_single(&_target, _repo, "HEAD")); cl_git_pass(git_reset_default(_repo, _target, &_pathspecs)); assert_content_in_index(&_pathspecs, true, NULL); }
libgit2-main
tests/libgit2/reset/default.c
#include "clar_libgit2.h" #include "posix.h" #include "reset_helpers.h" #include "path.h" #include "repo/repo_helpers.h" static git_repository *repo; static git_object *target; void test_reset_soft__initialize(void) { repo = cl_git_sandbox_init("testrepo.git"); } void test_reset_soft__cleanup(void) { git_object_free(target); target = NULL; cl_git_sandbox_cleanup(); } static void assert_reset_soft(bool should_be_detached) { git_oid oid; cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); cl_assert(git_repository_head_detached(repo) == should_be_detached); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); cl_assert(git_repository_head_detached(repo) == should_be_detached); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); } void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void) { assert_reset_soft(false); } void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void) { git_repository_detach_head(repo); assert_reset_soft(true); } void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void) { git_oid oid; char raw_head_oid[GIT_OID_SHA1_HEXSIZE + 1]; cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); git_oid_fmt(raw_head_oid, &oid); raw_head_oid[GIT_OID_SHA1_HEXSIZE] = '\0'; cl_git_pass(git_revparse_single(&target, repo, raw_head_oid)); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_pass(git_oid_streq(&oid, raw_head_oid)); } void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void) { git_oid oid; /* b25fa35 is a tag, pointing to another tag which points to commit e90810b */ cl_git_pass(git_revparse_single(&target, repo, "b25fa35")); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); cl_assert(git_repository_head_detached(repo) == false); cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD")); cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO)); } void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void) { /* 53fc32d is the tree of commit e90810b */ cl_git_pass(git_revparse_single(&target, repo, "53fc32d")); cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL)); git_object_free(target); /* 521d87c is an annotated tag pointing to a blob */ cl_git_pass(git_revparse_single(&target, repo, "521d87c")); cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL)); } void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void) { git_reference *head; cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); make_head_unborn(repo, NON_EXISTING_HEAD); cl_assert_equal_i(true, git_repository_head_unborn(repo)); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); cl_assert_equal_i(false, git_repository_head_unborn(repo)); cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD)); cl_assert_equal_i(0, git_oid_streq(git_reference_target(head), KNOWN_COMMIT_IN_BARE_REPO)); git_reference_free(head); } void test_reset_soft__fails_when_merging(void) { git_str merge_head_path = GIT_STR_INIT; cl_git_pass(git_repository_detach_head(repo)); cl_git_pass(git_str_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); cl_git_mkfile(git_str_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO)); cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL)); cl_git_pass(p_unlink(git_str_cstr(&merge_head_path))); git_str_dispose(&merge_head_path); } void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void) { git_index *index; git_reference *head; git_str merge_head_path = GIT_STR_INIT; cl_git_sandbox_cleanup(); repo = cl_git_sandbox_init("mergedrepo"); cl_git_pass(git_str_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); cl_git_pass(p_unlink(git_str_cstr(&merge_head_path))); git_str_dispose(&merge_head_path); cl_git_pass(git_repository_index(&index, repo)); cl_assert_equal_i(true, git_index_has_conflicts(index)); git_index_free(index); cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_reference_peel(&target, head, GIT_OBJECT_COMMIT)); git_reference_free(head); cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL)); } void test_reset_soft__reflog_is_correct(void) { git_annotated_commit *annotated; const char *exp_msg = "checkout: moving from br2 to master"; const char *master_msg = "commit: checking in"; reflog_check(repo, "HEAD", 7, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 2, "[email protected]", master_msg); /* Branch not moving, no reflog entry */ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); reflog_check(repo, "HEAD", 7, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 2, "[email protected]", master_msg); git_object_free(target); /* Moved branch, expect id in message */ exp_msg = "reset: moving to be3563ae3f795b2b4353bcce3a527ad0a4f7f644"; cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL)); reflog_check(repo, "HEAD", 8, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 3, NULL, exp_msg); /* Moved branch, expect message with annotated string */ exp_msg = "reset: moving to HEAD~^{commit}"; cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}")); cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_SOFT, NULL)); reflog_check(repo, "HEAD", 9, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg); git_annotated_commit_free(annotated); }
libgit2-main
tests/libgit2/reset/soft.c
#include "clar_libgit2.h" #include "reset_helpers.h" void reflog_check(git_repository *repo, const char *refname, size_t exp_count, const char *exp_email, const char *exp_msg) { git_reflog *log; const git_reflog_entry *entry; GIT_UNUSED(exp_email); cl_git_pass(git_reflog_read(&log, repo, refname)); cl_assert_equal_i(exp_count, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); if (exp_msg) cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry)); git_reflog_free(log); }
libgit2-main
tests/libgit2/reset/reset_helpers.c
#include "clar_libgit2.h" #include "posix.h" #include "reset_helpers.h" #include "path.h" #include "futils.h" static git_repository *repo; static git_object *target; void test_reset_hard__initialize(void) { repo = cl_git_sandbox_init("status"); target = NULL; } void test_reset_hard__cleanup(void) { if (target != NULL) { git_object_free(target); target = NULL; } cl_git_sandbox_cleanup(); } static int strequal_ignore_eol(const char *exp, const char *str) { while (*exp && *str) { if (*exp != *str) { while (*exp == '\r' || *exp == '\n') ++exp; while (*str == '\r' || *str == '\n') ++str; if (*exp != *str) return false; } else { exp++; str++; } } return (!*exp && !*str); } void test_reset_hard__resetting_reverts_modified_files(void) { git_str path = GIT_STR_INIT, content = GIT_STR_INIT; int i; static const char *files[4] = { "current_file", "modified_file", "staged_new_file", "staged_changes_modified_file" }; static const char *before[4] = { "current_file\n", "modified_file\nmodified_file\n", "staged_new_file\n", "staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n" }; static const char *after[4] = { "current_file\n", "modified_file\n", NULL, "staged_changes_modified_file\n" }; const char *wd = git_repository_workdir(repo); cl_assert(wd); for (i = 0; i < 4; ++i) { cl_git_pass(git_str_joinpath(&path, wd, files[i])); cl_git_pass(git_futils_readbuffer(&content, path.ptr)); cl_assert_equal_s(before[i], content.ptr); } cl_git_pass(git_revparse_single(&target, repo, "26a125e")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); for (i = 0; i < 4; ++i) { cl_git_pass(git_str_joinpath(&path, wd, files[i])); if (after[i]) { cl_git_pass(git_futils_readbuffer(&content, path.ptr)); cl_assert(strequal_ignore_eol(after[i], content.ptr)); } else { cl_assert(!git_fs_path_exists(path.ptr)); } } git_str_dispose(&content); git_str_dispose(&path); } void test_reset_hard__cannot_reset_in_a_bare_repository(void) { git_repository *bare; cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git"))); cl_assert(git_repository_is_bare(bare) == true); cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO)); cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL)); git_repository_free(bare); } static void index_entry_init(git_index *index, int side, git_oid *oid) { git_index_entry entry; memset(&entry, 0x0, sizeof(git_index_entry)); entry.path = "conflicting_file"; GIT_INDEX_ENTRY_STAGE_SET(&entry, side); entry.mode = 0100644; git_oid_cpy(&entry.id, oid); cl_git_pass(git_index_add(index, &entry)); } static void unmerged_index_init(git_index *index, int entries) { int write_ancestor = 1; int write_ours = 2; int write_theirs = 4; git_oid ancestor, ours, theirs; git_oid__fromstr(&ancestor, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a", GIT_OID_SHA1); git_oid__fromstr(&ours, "32504b727382542f9f089e24fddac5e78533e96c", GIT_OID_SHA1); git_oid__fromstr(&theirs, "061d42a44cacde5726057b67558821d95db96f19", GIT_OID_SHA1); cl_git_rewritefile("status/conflicting_file", "conflicting file\n"); if (entries & write_ancestor) index_entry_init(index, 1, &ancestor); if (entries & write_ours) index_entry_init(index, 2, &ours); if (entries & write_theirs) index_entry_init(index, 3, &theirs); } void test_reset_hard__resetting_reverts_unmerged(void) { git_index *index; int entries; /* Ensure every permutation of non-zero stage entries results in the * path being cleaned up. */ for (entries = 1; entries < 8; entries++) { cl_git_pass(git_repository_index(&index, repo)); unmerged_index_init(index, entries); cl_git_pass(git_index_write(index)); cl_git_pass(git_revparse_single(&target, repo, "26a125e")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); cl_assert(git_fs_path_exists("status/conflicting_file") == 0); git_object_free(target); target = NULL; git_index_free(index); } } void test_reset_hard__cleans_up_merge(void) { git_str merge_head_path = GIT_STR_INIT, merge_msg_path = GIT_STR_INIT, merge_mode_path = GIT_STR_INIT, orig_head_path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD")); cl_git_mkfile(git_str_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n"); cl_git_pass(git_str_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG")); cl_git_mkfile(git_str_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n"); cl_git_pass(git_str_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE")); cl_git_mkfile(git_str_cstr(&merge_mode_path), ""); cl_git_pass(git_str_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD")); cl_git_mkfile(git_str_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6"); cl_git_pass(git_revparse_single(&target, repo, "0017bd4")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); cl_assert(!git_fs_path_exists(git_str_cstr(&merge_head_path))); cl_assert(!git_fs_path_exists(git_str_cstr(&merge_msg_path))); cl_assert(!git_fs_path_exists(git_str_cstr(&merge_mode_path))); cl_assert(git_fs_path_exists(git_str_cstr(&orig_head_path))); cl_git_pass(p_unlink(git_str_cstr(&orig_head_path))); git_str_dispose(&merge_head_path); git_str_dispose(&merge_msg_path); git_str_dispose(&merge_mode_path); git_str_dispose(&orig_head_path); } void test_reset_hard__reflog_is_correct(void) { git_str buf = GIT_STR_INIT; git_annotated_commit *annotated; const char *exp_msg = "commit: Add a file which name should appear before the " "\"subdir/\" folder while being dealt with by the treewalker"; reflog_check(repo, "HEAD", 3, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 3, "[email protected]", exp_msg); /* Branch not moving, no reflog entry */ cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}")); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); reflog_check(repo, "HEAD", 3, "[email protected]", exp_msg); reflog_check(repo, "refs/heads/master", 3, "[email protected]", exp_msg); git_object_free(target); /* Moved branch, expect id in message */ cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}")); cl_git_pass(git_str_printf(&buf, "reset: moving to %s", git_oid_tostr_s(git_object_id(target)))); cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL)); reflog_check(repo, "HEAD", 4, NULL, git_str_cstr(&buf)); reflog_check(repo, "refs/heads/master", 4, NULL, git_str_cstr(&buf)); git_str_dispose(&buf); /* Moved branch, expect revspec in message */ exp_msg = "reset: moving to HEAD~^{commit}"; cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}")); cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_HARD, NULL)); reflog_check(repo, "HEAD", 5, NULL, exp_msg); reflog_check(repo, "refs/heads/master", 5, NULL, exp_msg); git_annotated_commit_free(annotated); } void test_reset_hard__switch_file_to_dir(void) { git_index_entry entry = {{ 0 }}; git_index *idx; git_odb *odb; git_object *commit; git_tree *tree; git_signature *sig; git_oid src_tree_id, tgt_tree_id; git_oid src_id, tgt_id; cl_git_pass(git_repository_odb(&odb, repo)); cl_git_pass(git_odb_write(&entry.id, odb, "", 0, GIT_OBJECT_BLOB)); git_odb_free(odb); entry.mode = GIT_FILEMODE_BLOB; cl_git_pass(git_index_new(&idx)); cl_git_pass(git_signature_now(&sig, "foo", "bar")); /* Create the old tree */ entry.path = "README"; cl_git_pass(git_index_add(idx, &entry)); entry.path = "dir"; cl_git_pass(git_index_add(idx, &entry)); cl_git_pass(git_index_write_tree_to(&src_tree_id, idx, repo)); cl_git_pass(git_index_clear(idx)); cl_git_pass(git_tree_lookup(&tree, repo, &src_tree_id)); cl_git_pass(git_commit_create(&src_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL)); git_tree_free(tree); /* Create the new tree */ entry.path = "README"; cl_git_pass(git_index_add(idx, &entry)); entry.path = "dir/FILE"; cl_git_pass(git_index_add(idx, &entry)); cl_git_pass(git_index_write_tree_to(&tgt_tree_id, idx, repo)); cl_git_pass(git_tree_lookup(&tree, repo, &tgt_tree_id)); cl_git_pass(git_commit_create(&tgt_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL)); git_tree_free(tree); git_index_free(idx); git_signature_free(sig); /* Let's go to a known state of the src commit with the file named 'dir' */ cl_git_pass(git_object_lookup(&commit, repo, &src_id, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL)); git_object_free(commit); /* And now we move over to the commit with the directory named 'dir' */ cl_git_pass(git_object_lookup(&commit, repo, &tgt_id, GIT_OBJECT_COMMIT)); cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL)); git_object_free(commit); }
libgit2-main
tests/libgit2/reset/hard.c
#include "clar_libgit2.h" #include "date.h" void test_date_rfc2822__format_rfc2822_no_offset(void) { git_time t = {1397031663, 0}; git_str buf = GIT_STR_INIT; cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset)); cl_assert_equal_s("Wed, 9 Apr 2014 08:21:03 +0000", buf.ptr); git_str_dispose(&buf); } void test_date_rfc2822__format_rfc2822_positive_offset(void) { git_time t = {1397031663, 120}; git_str buf = GIT_STR_INIT; cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset)); cl_assert_equal_s("Wed, 9 Apr 2014 10:21:03 +0200", buf.ptr); git_str_dispose(&buf); } void test_date_rfc2822__format_rfc2822_negative_offset(void) { git_time t = {1397031663, -120}; git_str buf = GIT_STR_INIT; cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset)); cl_assert_equal_s("Wed, 9 Apr 2014 06:21:03 -0200", buf.ptr); git_str_dispose(&buf); }
libgit2-main
tests/libgit2/date/rfc2822.c
#include "clar_libgit2.h" #include "date.h" void test_date_date__overflow(void) { #ifdef __LP64__ git_time_t d2038, d2039; /* This is expected to fail on a 32-bit machine. */ cl_git_pass(git_date_parse(&d2038, "2038-1-1")); cl_git_pass(git_date_parse(&d2039, "2039-1-1")); cl_assert(d2038 < d2039); #endif } void test_date_date__invalid_date(void) { git_time_t d; cl_git_fail(git_date_parse(&d, "")); cl_git_fail(git_date_parse(&d, "NEITHER_INTEGER_NOR_DATETIME")); }
libgit2-main
tests/libgit2/date/date.c
#include "clar_libgit2.h" #include "path.h" #include "remote.h" static const char* tagger_name = "Vicent Marti"; static const char* tagger_email = "[email protected]"; static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n"; static int transfer_cb(const git_indexer_progress *stats, void *payload) { int *callcount = (int*)payload; GIT_UNUSED(stats); (*callcount)++; return 0; } static void cleanup_local_repo(void *path) { cl_fixture_cleanup((char *)path); } void test_network_fetchlocal__cleanup(void) { cl_git_sandbox_cleanup(); } void test_network_fetchlocal__complete(void) { git_repository *repo; git_remote *origin; int callcount = 0; git_strarray refnames = {0}; const char *url = cl_git_fixture_url("testrepo.git"); git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(20, (int)refnames.count); cl_assert(callcount > 0); git_strarray_dispose(&refnames); git_remote_free(origin); git_repository_free(repo); } void test_network_fetchlocal__prune(void) { git_repository *repo; git_remote *origin; int callcount = 0; git_strarray refnames = {0}; git_reference *ref; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(20, (int)refnames.count); cl_assert(callcount > 0); git_strarray_dispose(&refnames); git_remote_free(origin); cl_git_pass(git_reference_lookup(&ref, remote_repo, "refs/heads/br2")); cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_remote_prune(origin, &options.callbacks)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(19, (int)refnames.count); git_strarray_dispose(&refnames); git_remote_free(origin); cl_git_pass(git_reference_lookup(&ref, remote_repo, "refs/heads/packed")); cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_remote_prune(origin, &options.callbacks)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(18, (int)refnames.count); git_strarray_dispose(&refnames); git_remote_free(origin); git_repository_free(repo); } static int update_tips_fail_on_call(const char *ref, const git_oid *old, const git_oid *new, void *data) { GIT_UNUSED(ref); GIT_UNUSED(old); GIT_UNUSED(new); GIT_UNUSED(data); cl_fail("update tips called"); return 0; } static void assert_ref_exists(git_repository *repo, const char *name) { git_reference *ref; cl_git_pass(git_reference_lookup(&ref, repo, name)); git_reference_free(ref); } void test_network_fetchlocal__prune_overlapping(void) { git_repository *repo; git_remote *origin; int callcount = 0; git_strarray refnames = {0}; git_reference *ref; git_config *config; git_oid target; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_git_pass(git_reference_lookup(&ref, remote_repo, "refs/heads/master")); git_oid_cpy(&target, git_reference_target(ref)); git_reference_free(ref); cl_git_pass(git_reference_create(&ref, remote_repo, "refs/pull/42/head", &target, 1, NULL)); git_reference_free(ref); cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_bool(config, "remote.origin.prune", true)); cl_git_pass(git_config_set_multivar(config, "remote.origin.fetch", "^$", "refs/pull/*/head:refs/remotes/origin/pr/*")); git_remote_free(origin); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); assert_ref_exists(repo, "refs/remotes/origin/master"); assert_ref_exists(repo, "refs/remotes/origin/pr/42"); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(21, (int)refnames.count); git_strarray_dispose(&refnames); cl_git_pass(git_config_delete_multivar(config, "remote.origin.fetch", "refs")); cl_git_pass(git_config_set_multivar(config, "remote.origin.fetch", "^$", "refs/pull/*/head:refs/remotes/origin/pr/*")); cl_git_pass(git_config_set_multivar(config, "remote.origin.fetch", "^$", "refs/heads/*:refs/remotes/origin/*")); git_remote_free(origin); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); options.callbacks.update_tips = update_tips_fail_on_call; cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); assert_ref_exists(repo, "refs/remotes/origin/master"); assert_ref_exists(repo, "refs/remotes/origin/pr/42"); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(21, (int)refnames.count); git_strarray_dispose(&refnames); cl_git_pass(git_config_delete_multivar(config, "remote.origin.fetch", "refs")); cl_git_pass(git_config_set_multivar(config, "remote.origin.fetch", "^$", "refs/heads/*:refs/remotes/origin/*")); cl_git_pass(git_config_set_multivar(config, "remote.origin.fetch", "^$", "refs/pull/*/head:refs/remotes/origin/pr/*")); git_remote_free(origin); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); options.callbacks.update_tips = update_tips_fail_on_call; cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); git_config_free(config); git_strarray_dispose(&refnames); git_remote_free(origin); git_repository_free(repo); } void test_network_fetchlocal__fetchprune(void) { git_repository *repo; git_remote *origin; int callcount = 0; git_strarray refnames = {0}; git_reference *ref; git_config *config; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(20, (int)refnames.count); cl_assert(callcount > 0); git_strarray_dispose(&refnames); git_remote_free(origin); cl_git_pass(git_reference_lookup(&ref, remote_repo, "refs/heads/br2")); cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_remote_prune(origin, &options.callbacks)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(19, (int)refnames.count); git_strarray_dispose(&refnames); git_remote_free(origin); cl_git_pass(git_reference_lookup(&ref, remote_repo, "refs/heads/packed")); cl_git_pass(git_reference_delete(ref)); git_reference_free(ref); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_bool(config, "remote.origin.prune", 1)); git_config_free(config); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_assert_equal_i(1, git_remote_prune_refs(origin)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(18, (int)refnames.count); git_strarray_dispose(&refnames); git_remote_free(origin); git_repository_free(repo); } void test_network_fetchlocal__prune_tag(void) { git_repository *repo; git_remote *origin; int callcount = 0; git_reference *ref; git_config *config; git_oid tag_id; git_signature *tagger; git_object *obj; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); git_remote_free(origin); cl_git_pass(git_revparse_single(&obj, repo, "origin/master")); cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/origin/fake-remote", git_object_id(obj), 1, NULL)); git_reference_free(ref); /* create signature */ cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); cl_git_pass( git_tag_create(&tag_id, repo, "some-tag", obj, tagger, tagger_message, 0) ); git_signature_free(tagger); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_bool(config, "remote.origin.prune", 1)); git_config_free(config); cl_git_pass(git_remote_lookup(&origin, repo, GIT_REMOTE_ORIGIN)); cl_assert_equal_i(1, git_remote_prune_refs(origin)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); assert_ref_exists(repo, "refs/tags/some-tag"); cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, repo, "refs/remotes/origin/fake-remote")); git_object_free(obj); git_remote_free(origin); git_repository_free(repo); } void test_network_fetchlocal__partial(void) { git_repository *repo = cl_git_sandbox_init("partial-testrepo"); git_remote *origin; int callcount = 0; git_strarray refnames = {0}; const char *url; git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; options.callbacks.payload = &callcount; cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(1, (int)refnames.count); url = cl_git_fixture_url("testrepo.git"); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_fetch(origin, NULL, &options, NULL)); git_strarray_dispose(&refnames); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(21, (int)refnames.count); /* 18 remote + 1 local */ cl_assert(callcount > 0); git_strarray_dispose(&refnames); git_remote_free(origin); } static int remote_mirror_cb(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) { int error; git_remote *remote; GIT_UNUSED(payload); if ((error = git_remote_create_with_fetchspec(&remote, repo, name, url, "+refs/*:refs/*")) < 0) return error; *out = remote; return 0; } void test_network_fetchlocal__clone_into_mirror(void) { git_clone_options opts = GIT_CLONE_OPTIONS_INIT; git_repository *repo; git_reference *ref; opts.bare = true; opts.remote_cb = remote_mirror_cb; cl_git_pass(git_clone(&repo, cl_git_fixture_url("testrepo.git"), "./foo.git", &opts)); cl_git_pass(git_reference_lookup(&ref, repo, "HEAD")); cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(ref)); cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(ref)); git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/test/master")); git_reference_free(ref); git_repository_free(repo); cl_fixture_cleanup("./foo.git"); } void test_network_fetchlocal__all_refs(void) { git_repository *repo; git_remote *remote; git_reference *ref; char *allrefs = "+refs/*:refs/*"; git_strarray refspecs = { &allrefs, 1, }; cl_git_pass(git_repository_init(&repo, "./foo.git", true)); cl_git_pass(git_remote_create_anonymous(&remote, repo, cl_git_fixture_url("testrepo.git"))); cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, NULL)); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/test/master")); git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, repo, "refs/tags/test")); git_reference_free(ref); git_remote_free(remote); git_repository_free(repo); cl_fixture_cleanup("./foo.git"); } void test_network_fetchlocal__multi_remotes(void) { git_repository *repo = cl_git_sandbox_init("testrepo.git"); git_remote *test, *test2; git_strarray refnames = {0}; git_fetch_options options = GIT_FETCH_OPTIONS_INIT; options.callbacks.transfer_progress = transfer_cb; cl_git_pass(git_remote_set_url(repo, "test", cl_git_fixture_url("testrepo.git"))); cl_git_pass(git_remote_lookup(&test, repo, "test")); cl_git_pass(git_remote_fetch(test, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(35, (int)refnames.count); git_strarray_dispose(&refnames); cl_git_pass(git_remote_set_url(repo, "test_with_pushurl", cl_git_fixture_url("testrepo.git"))); cl_git_pass(git_remote_lookup(&test2, repo, "test_with_pushurl")); cl_git_pass(git_remote_fetch(test2, NULL, &options, NULL)); cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(48, (int)refnames.count); git_strarray_dispose(&refnames); git_remote_free(test); git_remote_free(test2); } static int sideband_cb(const char *str, int len, void *payload) { int *count = (int *) payload; GIT_UNUSED(str); GIT_UNUSED(len); (*count)++; return 0; } void test_network_fetchlocal__call_progress(void) { git_repository *repo; git_remote *remote; git_fetch_options options = GIT_FETCH_OPTIONS_INIT; int callcount = 0; cl_git_pass(git_repository_init(&repo, "foo.git", true)); cl_set_cleanup(cleanup_local_repo, "foo.git"); cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "origin", cl_git_fixture_url("testrepo.git"), "+refs/heads/*:refs/heads/*")); options.callbacks.sideband_progress = sideband_cb; options.callbacks.payload = &callcount; cl_git_pass(git_remote_fetch(remote, NULL, &options, NULL)); cl_assert(callcount != 0); git_remote_free(remote); git_repository_free(repo); } void test_network_fetchlocal__prune_load_remote_prune_config(void) { git_repository *repo; git_remote *origin; git_config *config; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_bool(config, "remote.origin.prune", 1)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_assert_equal_i(1, git_remote_prune_refs(origin)); git_config_free(config); git_remote_free(origin); git_repository_free(repo); } void test_network_fetchlocal__prune_load_fetch_prune_config(void) { git_repository *repo; git_remote *origin; git_config *config; git_repository *remote_repo = cl_git_sandbox_init("testrepo.git"); const char *url = cl_git_path_url(git_repository_path(remote_repo)); cl_set_cleanup(&cleanup_local_repo, "foo"); cl_git_pass(git_repository_init(&repo, "foo", true)); cl_git_pass(git_repository_config(&config, repo)); cl_git_pass(git_config_set_bool(config, "fetch.prune", 1)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_assert_equal_i(1, git_remote_prune_refs(origin)); git_config_free(config); git_remote_free(origin); git_repository_free(repo); } static int update_tips_error(const char *ref, const git_oid *old, const git_oid *new, void *data) { int *callcount = (int *) data; GIT_UNUSED(ref); GIT_UNUSED(old); GIT_UNUSED(new); (*callcount)++; return -1; } void test_network_fetchlocal__update_tips_error_is_propagated(void) { git_repository *repo; git_reference_iterator *iterator; git_reference *ref; git_remote *remote; git_fetch_options options = GIT_FETCH_OPTIONS_INIT; int callcount = 0; cl_git_pass(git_repository_init(&repo, "foo.git", true)); cl_set_cleanup(cleanup_local_repo, "foo.git"); cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "origin", cl_git_fixture_url("testrepo.git"), "+refs/heads/*:refs/remotes/update-tips/*")); options.callbacks.update_tips = update_tips_error; options.callbacks.payload = &callcount; cl_git_fail(git_remote_fetch(remote, NULL, &options, NULL)); cl_assert_equal_i(1, callcount); cl_git_pass(git_reference_iterator_glob_new(&iterator, repo, "refs/remotes/update-tips/**/")); cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&ref, iterator)); git_reference_iterator_free(iterator); git_remote_free(remote); git_repository_free(repo); }
libgit2-main
tests/libgit2/network/fetchlocal.c
#include "clar_libgit2.h" #include "refspec.h" #include "remote.h" static void assert_refspec(unsigned int direction, const char *input, bool is_expected_to_be_valid) { git_refspec refspec; int error; error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH); git_refspec__dispose(&refspec); if (is_expected_to_be_valid) cl_assert_equal_i(0, error); else cl_assert_equal_i(GIT_EINVALIDSPEC, error); } void test_network_refspecs__parsing(void) { /* Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh */ assert_refspec(GIT_DIRECTION_PUSH, "", false); assert_refspec(GIT_DIRECTION_PUSH, ":", true); assert_refspec(GIT_DIRECTION_PUSH, "::", false); assert_refspec(GIT_DIRECTION_PUSH, "+:", true); assert_refspec(GIT_DIRECTION_FETCH, "", true); assert_refspec(GIT_DIRECTION_PUSH, ":", true); assert_refspec(GIT_DIRECTION_FETCH, "::", false); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz/*", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz", false); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads:refs/remotes/frotz/*", false); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); /* * These have invalid LHS, but we do not have a formal "valid sha-1 * expression syntax checker" so they are not checked with the current * code. They will be caught downstream anyway, but we may want to * have tighter check later... */ /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); */ /*assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); */ assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz/*", true); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads:refs/remotes/frotz/*", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); assert_refspec(GIT_DIRECTION_PUSH, "master~1:refs/remotes/frotz/backup", true); assert_refspec(GIT_DIRECTION_FETCH, "master~1:refs/remotes/frotz/backup", false); assert_refspec(GIT_DIRECTION_PUSH, "HEAD~4:refs/remotes/frotz/new", true); assert_refspec(GIT_DIRECTION_FETCH, "HEAD~4:refs/remotes/frotz/new", false); assert_refspec(GIT_DIRECTION_PUSH, "HEAD", true); assert_refspec(GIT_DIRECTION_FETCH, "HEAD", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol", false); assert_refspec(GIT_DIRECTION_PUSH, "HEAD:", false); assert_refspec(GIT_DIRECTION_FETCH, "HEAD:", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol:", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol:", false); assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/deleteme", true); assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD-to-me", true); assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/delete me", false); assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD to me", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", true); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", true); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*g*/for-linus:refs/remotes/mine/*", false); assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); assert_refspec(GIT_DIRECTION_FETCH, "master", true); assert_refspec(GIT_DIRECTION_PUSH, "master", true); assert_refspec(GIT_DIRECTION_FETCH, "refs/pull/*/head:refs/remotes/origin/pr/*", true); } static void assert_valid_transform(const char *refspec, const char *name, const char *result) { git_refspec spec; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_refspec__parse(&spec, refspec, true)); cl_git_pass(git_refspec_transform(&buf, &spec, name)); cl_assert_equal_s(result, buf.ptr); git_buf_dispose(&buf); git_refspec__dispose(&spec); } void test_network_refspecs__transform_mid_star(void) { assert_valid_transform("refs/pull/*/head:refs/remotes/origin/pr/*", "refs/pull/23/head", "refs/remotes/origin/pr/23"); assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/master", "refs/remotes/origin/master"); assert_valid_transform("refs/heads/*:refs/remotes/origin/*", "refs/heads/user/feature", "refs/remotes/origin/user/feature"); assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/master", "refs/heads/master"); assert_valid_transform("refs/heads/*:refs/heads/*", "refs/heads/user/feature", "refs/heads/user/feature"); assert_valid_transform("refs/*:refs/*", "refs/heads/master", "refs/heads/master"); } void test_network_refspecs__transform_loosened_star(void) { assert_valid_transform("refs/heads/branch-*:refs/remotes/origin/branch-*", "refs/heads/branch-a", "refs/remotes/origin/branch-a"); assert_valid_transform("refs/heads/branch-*/head:refs/remotes/origin/branch-*/head", "refs/heads/branch-a/head", "refs/remotes/origin/branch-a/head"); } void test_network_refspecs__transform_nested_star(void) { assert_valid_transform("refs/heads/x*x/for-linus:refs/remotes/mine/*", "refs/heads/xbranchx/for-linus", "refs/remotes/mine/branch"); } void test_network_refspecs__no_dst(void) { assert_valid_transform("refs/heads/master:", "refs/heads/master", ""); } static void assert_invalid_transform(const char *refspec, const char *name) { git_refspec spec; git_buf buf = GIT_BUF_INIT; git_refspec__parse(&spec, refspec, true); cl_git_fail(git_refspec_transform(&buf, &spec, name)); git_buf_dispose(&buf); git_refspec__dispose(&spec); } void test_network_refspecs__invalid(void) { assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "master"); assert_invalid_transform("refs/heads/*:refs/remotes/origin/*", "refs/headz/master"); } static void assert_invalid_rtransform(const char *refspec, const char *name) { git_refspec spec; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_refspec__parse(&spec, refspec, true)); cl_git_fail(git_refspec_rtransform(&buf, &spec, name)); git_buf_dispose(&buf); git_refspec__dispose(&spec); } void test_network_refspecs__invalid_reverse(void) { assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "master"); assert_invalid_rtransform("refs/heads/*:refs/remotes/origin/*", "refs/remotes/o/master"); } void test_network_refspecs__matching(void) { git_refspec spec; cl_git_pass(git_refspec__parse(&spec, ":", false)); cl_assert_equal_s(":", spec.string); cl_assert_equal_s("", spec.src); cl_assert_equal_s("", spec.dst); git_refspec__dispose(&spec); } void test_network_refspecs__parse_free(void) { git_refspec *spec = NULL; cl_git_fail(git_refspec_parse(&spec, "", 0)); cl_git_fail(git_refspec_parse(&spec, ":::", 0)); cl_git_pass(git_refspec_parse(&spec, "HEAD:", 1)); cl_assert(spec != NULL); git_refspec_free(spec); }
libgit2-main
tests/libgit2/network/refspecs.c
#include "clar_libgit2.h" #include "netops.h" void test_network_matchhost__match(void) { cl_git_pass(gitno__match_host("*.example.org", "www.example.org")); cl_git_pass(gitno__match_host("*.foo.example.org", "www.foo.example.org")); cl_git_fail(gitno__match_host("*.foo.example.org", "foo.example.org")); cl_git_fail(gitno__match_host("*.foo.example.org", "www.example.org")); cl_git_fail(gitno__match_host("*.example.org", "example.org")); cl_git_fail(gitno__match_host("*.example.org", "www.foo.example.org")); cl_git_fail(gitno__match_host("*.example.org", "blah.www.www.example.org")); }
libgit2-main
tests/libgit2/network/matchhost.c
#include "clar_libgit2.h" #include "git2/cred_helpers.h" void test_network_cred__stock_userpass_validates_args(void) { git_credential_userpass_payload payload = {0}; cl_git_fail(git_credential_userpass(NULL, NULL, NULL, 0, NULL)); payload.username = "user"; cl_git_fail(git_credential_userpass(NULL, NULL, NULL, 0, &payload)); payload.username = NULL; payload.username = "pass"; cl_git_fail(git_credential_userpass(NULL, NULL, NULL, 0, &payload)); } void test_network_cred__stock_userpass_validates_that_method_is_allowed(void) { git_credential *cred; git_credential_userpass_payload payload = {"user", "pass"}; cl_git_fail(git_credential_userpass(&cred, NULL, NULL, 0, &payload)); cl_git_pass(git_credential_userpass(&cred, NULL, NULL, GIT_CREDENTIAL_USERPASS_PLAINTEXT, &payload)); git_credential_free(cred); } void test_network_cred__stock_userpass_properly_handles_username_in_url(void) { git_credential *cred; git_credential_userpass_payload payload = {"alice", "password"}; cl_git_pass(git_credential_userpass(&cred, NULL, NULL, GIT_CREDENTIAL_USERPASS_PLAINTEXT, &payload)); cl_assert_equal_s("alice", git_credential_get_username(cred)); git_credential_free(cred); cl_git_pass(git_credential_userpass(&cred, NULL, "bob", GIT_CREDENTIAL_USERPASS_PLAINTEXT, &payload)); cl_assert_equal_s("alice", git_credential_get_username(cred)); git_credential_free(cred); payload.username = NULL; cl_git_pass(git_credential_userpass(&cred, NULL, "bob", GIT_CREDENTIAL_USERPASS_PLAINTEXT, &payload)); cl_assert_equal_s("bob", git_credential_get_username(cred)); git_credential_free(cred); }
libgit2-main
tests/libgit2/network/cred.c
#include "clar_libgit2.h" #include "net.h" void test_network_url_valid__test(void) { cl_assert(git_net_str_is_url("http://example.com/")); cl_assert(git_net_str_is_url("file://localhost/tmp/foo/")); cl_assert(git_net_str_is_url("ssh://user@host:42/tmp")); cl_assert(git_net_str_is_url("git+ssh://user@host:42/tmp")); cl_assert(git_net_str_is_url("ssh+git://user@host:42/tmp")); cl_assert(git_net_str_is_url("https://user:[email protected]/foo/bar")); cl_assert(!git_net_str_is_url("host:foo.git")); cl_assert(!git_net_str_is_url("host:/foo.git")); cl_assert(!git_net_str_is_url("[host:42]:/foo.git")); cl_assert(!git_net_str_is_url("[user@host:42]:/foo.git")); }
libgit2-main
tests/libgit2/network/url/valid.c
#include "clar_libgit2.h" #include "net.h" #include "netops.h" static git_net_url conndata; void test_network_url_redirect__initialize(void) { memset(&conndata, 0, sizeof(conndata)); } void test_network_url_redirect__cleanup(void) { git_net_url_dispose(&conndata); } void test_network_url_redirect__redirect_http(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com/foo/bar/baz")); cl_git_pass(git_net_url_apply_redirect(&conndata, "http://example.com/foo/bar/baz", false, "bar/baz")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/foo/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__redirect_ssl(void) { cl_git_pass(git_net_url_parse(&conndata, "https://example.com/foo/bar/baz")); cl_git_pass(git_net_url_apply_redirect(&conndata, "https://example.com/foo/bar/baz", false, "bar/baz")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/foo/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__redirect_leaves_root_path(void) { cl_git_pass(git_net_url_parse(&conndata, "https://example.com/foo/bar/baz")); cl_git_pass(git_net_url_apply_redirect(&conndata, "https://example.com/foo/bar/baz", false, "/foo/bar/baz")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__redirect_encoded_username_password(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user%2fname:pass%40word%zyx%[email protected]/foo/bar/baz")); cl_git_pass(git_net_url_apply_redirect(&conndata, "https://user%2fname:pass%40word%zyx%[email protected]/foo/bar/baz", false, "bar/baz")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/foo/"); cl_assert_equal_s(conndata.username, "user/name"); cl_assert_equal_s(conndata.password, "pass@word%zyx%v"); } void test_network_url_redirect__redirect_cross_host_allowed(void) { cl_git_pass(git_net_url_parse(&conndata, "https://bar.com/bar/baz")); cl_git_pass(git_net_url_apply_redirect(&conndata, "https://foo.com/bar/baz", true, NULL)); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "foo.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/bar/baz"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__redirect_cross_host_denied(void) { cl_git_pass(git_net_url_parse(&conndata, "https://bar.com/bar/baz")); cl_git_fail_with(git_net_url_apply_redirect(&conndata, "https://foo.com/bar/baz", false, NULL), -1); } void test_network_url_redirect__redirect_http_downgrade_denied(void) { cl_git_pass(git_net_url_parse(&conndata, "https://foo.com/bar/baz")); cl_git_fail_with(git_net_url_apply_redirect(&conndata, "http://foo.com/bar/baz", true, NULL), -1); } void test_network_url_redirect__redirect_relative(void) { cl_git_pass(git_net_url_parse(&conndata, "http://foo.com/bar/baz/biff")); cl_git_pass(git_net_url_apply_redirect(&conndata, "/zap/baz/biff?bam", true, NULL)); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "foo.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__redirect_relative_ssl(void) { cl_git_pass(git_net_url_parse(&conndata, "https://foo.com/bar/baz/biff")); cl_git_pass(git_net_url_apply_redirect(&conndata, "/zap/baz/biff?bam", true, NULL)); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "foo.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/zap/baz/biff?bam"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); } void test_network_url_redirect__service_query_no_query_params_in_location(void) { cl_git_pass(git_net_url_parse(&conndata, "https://foo.com/bar/info/refs?service=git-upload-pack")); cl_git_pass(git_net_url_apply_redirect(&conndata, "/baz/info/refs", true, "/info/refs?service=git-upload-pack")); cl_assert_equal_s(conndata.path, "/baz"); } void test_network_url_redirect__service_query_with_query_params_in_location(void) { cl_git_pass(git_net_url_parse(&conndata, "https://foo.com/bar/info/refs?service=git-upload-pack")); cl_git_pass(git_net_url_apply_redirect(&conndata, "/baz/info/refs?service=git-upload-pack", true, "/info/refs?service=git-upload-pack")); cl_assert_equal_s(conndata.path, "/baz"); }
libgit2-main
tests/libgit2/network/url/redirect.c
#include "clar_libgit2.h" #include "net.h" static git_net_url conndata; void test_network_url_scp__initialize(void) { memset(&conndata, 0, sizeof(conndata)); } void test_network_url_scp__cleanup(void) { git_net_url_dispose(&conndata); } /* Hostname */ void test_network_url_scp__hostname_trivial(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__hostname_bracketed(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__hostname_root(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "example.com:/")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__hostname_user(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[email protected]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__hostname_user_bracketed(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[[email protected]]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__hostname_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[example.com:42]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "42"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__hostname_user_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[[email protected]:42]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "42"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__ipv4_trivial(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "192.168.99.88:/resource/a/b/c")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "192.168.99.88"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/a/b/c"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__ipv4_bracketed(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88]:/resource/a/b/c")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "192.168.99.88"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/a/b/c"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__ipv4_user(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[email protected]:/resource/a/b/c")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "192.168.99.88"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/a/b/c"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__ipv4_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[192.168.99.88:1111]:/resource/a/b/c")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "192.168.99.88"); cl_assert_equal_s(conndata.port, "1111"); cl_assert_equal_s(conndata.path, "/resource/a/b/c"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__ipv4_user_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[[email protected]:1111]:/resource/a/b/c")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "192.168.99.88"); cl_assert_equal_s(conndata.port, "1111"); cl_assert_equal_s(conndata.path, "/resource/a/b/c"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__ipv6_trivial(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:/resource/foo")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/foo"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__ipv6_user(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:/resource/foo")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/foo"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__ipv6_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "99"); cl_assert_equal_s(conndata.path, "/resource/foo"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__ipv6_user_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[git@[fe80::dcad:beff:fe00:0001]:99]:/resource/foo")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "99"); cl_assert_equal_s(conndata.path, "/resource/foo"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_scp__hexhost_and_port(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[fe:22]:/resource/foo")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "fe"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "/resource/foo"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__malformed_ipv6_one(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "fe80::dcad:beff:fe00:0001]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "fe80"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, ":dcad:beff:fe00:0001]:/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__malformed_ipv6_two(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001]:42]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "42]:/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__malformed_ipv6_with_user(void) { cl_git_pass(git_net_url_parse_scp(&conndata, "git@[fe80::dcad:beff:fe00:0001]:42]:/resource")); cl_assert_equal_s(conndata.scheme, "ssh"); cl_assert_equal_s(conndata.host, "[fe80::dcad:beff:fe00:0001]"); cl_assert_equal_s(conndata.port, "22"); cl_assert_equal_s(conndata.path, "42]:/resource"); cl_assert_equal_s(conndata.username, "git"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_scp__invalid_addresses(void) { /* Path is required */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "example.com")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "example.com:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[example.com:42]:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[[email protected]:42]:")); /* Host is required */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, ":")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, ":foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "git@:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[]:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "git@[]:")); /* User is required if specified */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "@example.com:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "@:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[@localhost:22]:foo")); /* Port is required in brackets */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[example.com:]:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[[email protected]:]:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[fe:]:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[@localhost]:foo")); /* Extra brackets are disallowed */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[git@[[fe80::dcad:beff:fe00:0001]]:42]:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[[git@[fe80::dcad:beff:fe00:0001]]:42]:foo")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[[git@[fe80::dcad:beff:fe00:0001]:42]]:foo")); /* Closing bracket missing */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[fe80::dcad:beff:fe00:0001:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[[fe80::dcad:beff:fe00:0001]:42:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[git@[fe80::dcad:beff:fe00:0001]:42:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse_scp(&conndata, "[git@[fe80::dcad:beff:fe00:0001:42]:/resource")); /* Invalid character inside address */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "[fe8o::dcad:beff:fe00:0001]:/resource")); }
libgit2-main
tests/libgit2/network/url/scp.c
#include "clar_libgit2.h" #include "net.h" static git_net_url conndata; void test_network_url_parse__initialize(void) { memset(&conndata, 0, sizeof(conndata)); } void test_network_url_parse__cleanup(void) { git_net_url_dispose(&conndata); } /* Hostname */ void test_network_url_parse__hostname_trivial(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com/")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_implied_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_implied_root_custom_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com:42")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "42"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__hostname_implied_root_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com:")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_encoded_password(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:pass%2fis%[email protected]:1234/")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "hostname.com"); cl_assert_equal_s(conndata.port, "1234"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass/is@bad"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__hostname_user(void) { cl_git_pass(git_net_url_parse(&conndata, "https://[email protected]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_user_pass(void) { /* user:[email protected]/resource */ cl_git_pass(git_net_url_parse(&conndata, "https://user:[email protected]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_port(void) { /* hostname.tld:port/resource */ cl_git_pass(git_net_url_parse(&conndata, "https://example.com:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__hostname_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://example.com:/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__hostname_user_port(void) { /* [email protected]:port/resource */ cl_git_pass(git_net_url_parse(&conndata, "https://[email protected]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__hostname_user_pass_port(void) { /* user:[email protected]:port/resource */ cl_git_pass(git_net_url_parse(&conndata, "https://user:[email protected]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "example.com"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } /* IPv4 addresses */ void test_network_url_parse__ipv4_trivial(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1/")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_implied_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_implied_root_custom_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:42")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "42"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv4_implied_root_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_encoded_password(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:pass%2fis%[email protected]:1234/")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "1234"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass/is@bad"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv4_user(void) { cl_git_pass(git_net_url_parse(&conndata, "https://[email protected]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_user_pass(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:[email protected]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://192.168.1.1:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv4_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://192.168.1.1:/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv4_user_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://[email protected]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv4_user_pass_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:[email protected]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "192.168.1.1"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } /* IPv6 addresses */ void test_network_url_parse__ipv6_trivial(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]/")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_implied_root(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_implied_root_custom_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:42")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "42"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv6_implied_root_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_encoded_password(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001]:1234/")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "1234"); cl_assert_equal_s(conndata.path, "/"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass/is@bad"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv6_user(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user@[fe80::dcad:beff:fe00:0001]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_user_pass(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:pass@[fe80::dcad:beff:fe00:0001]/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "443"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://[fe80::dcad:beff:fe00:0001]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv6_empty_port(void) { cl_git_pass(git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001]:/resource")); cl_assert_equal_s(conndata.scheme, "http"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "80"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_p(conndata.username, NULL); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 1); } void test_network_url_parse__ipv6_user_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user@[fe80::dcad:beff:fe00:0001]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_p(conndata.password, NULL); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv6_user_pass_port(void) { cl_git_pass(git_net_url_parse(&conndata, "https://user:pass@[fe80::dcad:beff:fe00:0001]:9191/resource")); cl_assert_equal_s(conndata.scheme, "https"); cl_assert_equal_s(conndata.host, "fe80::dcad:beff:fe00:0001"); cl_assert_equal_s(conndata.port, "9191"); cl_assert_equal_s(conndata.path, "/resource"); cl_assert_equal_s(conndata.username, "user"); cl_assert_equal_s(conndata.password, "pass"); cl_assert_equal_i(git_net_url_is_default_port(&conndata), 0); } void test_network_url_parse__ipv6_invalid_addresses(void) { /* Opening bracket missing */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]:42")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001]:1234/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@fe80::dcad:beff:fe00:0001]/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@fe80::dcad:beff:fe00:0001]/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://fe80::dcad:beff:fe00:0001]:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001]:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@fe80::dcad:beff:fe00:0001]:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@fe80::dcad:beff:fe00:0001]:9191/resource")); /* Closing bracket missing */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001:42")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass%2fis%40bad@[fe80::dcad:beff:fe00:0001:1234/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@[fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@[fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://[fe80::dcad:beff:fe00:0001:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe80::dcad:beff:fe00:0001:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@[fe80::dcad:beff:fe00:0001:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@[fe80::dcad:beff:fe00:0001:9191/resource")); /* Both brackets missing */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001:42")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001:")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass%2fis%40bad@fe80::dcad:beff:fe00:0001:1234/")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@fe80::dcad:beff:fe00:0001/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://fe80::dcad:beff:fe00:0001:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://fe80::dcad:beff:fe00:0001:/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user@fe80::dcad:beff:fe00:0001:9191/resource")); cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "https://user:pass@fe80::dcad:beff:fe00:0001:9191/resource")); /* Invalid character inside address */ cl_git_fail_with(GIT_EINVALIDSPEC, git_net_url_parse(&conndata, "http://[fe8o::dcad:beff:fe00:0001]/resource")); }
libgit2-main
tests/libgit2/network/url/parse.c
#include "clar_libgit2.h" #include "net.h" struct url_pattern { const char *url; const char *pattern; bool matches; }; void test_network_url_pattern__single(void) { git_net_url url; size_t i; struct url_pattern url_patterns[] = { /* Wildcard matches */ { "https://example.com/", "", false }, { "https://example.com/", "*", true }, /* Literal and wildcard matches */ { "https://example.com/", "example.com", true }, { "https://example.com/", ".example.com", true }, { "https://example.com/", "*.example.com", true }, { "https://www.example.com/", "www.example.com", true }, { "https://www.example.com/", ".example.com", true }, { "https://www.example.com/", "*.example.com", true }, /* Literal and wildcard failures */ { "https://example.com/", "example.org", false }, { "https://example.com/", ".example.org", false }, { "https://example.com/", "*.example.org", false }, { "https://foo.example.com/", "www.example.com", false }, /* * A port in the pattern is optional; if no port is * present, it matches *all* ports. */ { "https://example.com/", "example.com:443", true }, { "https://example.com/", "example.com:80", false }, { "https://example.com:1443/", "example.com", true }, /* Failures with similar prefix/suffix */ { "https://texample.com/", "example.com", false }, { "https://example.com/", "mexample.com", false }, { "https://example.com:44/", "example.com:443", false }, { "https://example.com:443/", "example.com:44", false }, }; for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); cl_assert_(git_net_url_matches_pattern(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); git_net_url_dispose(&url); } } void test_network_url_pattern__list(void) { git_net_url url; size_t i; struct url_pattern url_patterns[] = { /* Wildcard matches */ { "https://example.com/", "", false }, { "https://example.com/", "*", true }, { "https://example.com/", ",example.com,", true }, { "https://example.com/", "foo,,example.com,,bar", true }, { "https://example.com/", "foo,,zzz,,*,,bar", true }, /* Literals */ { "https://example.com/", "example.com", true }, { "https://example.com/", "foo.bar,example.com", true }, { "https://example.com/", "foo.bar", false }, { "https://example.com/", "foo.bar,example.org", false }, { "https://www.example.com/", "foo.example.com,www.example.com,bar.example.com", true }, { "https://www.example.com/", "foo.example.com,baz.example.com,bar.example.com", false }, { "https://foo.example.com/", "www.example.com", false }, { "https://foo.example.com/", "bar.example.com,www.example.com,", false }, /* Wildcards */ { "https://example.com/", ".example.com", true }, { "https://example.com/", "*.example.com", true }, { "https://example.com/", "foo.com,bar.com,.example.com", true }, { "https://example.com/", ".foo.com,.bar.com,.example.com", true }, { "https://example.com/", ".foo.com,.bar.com,asdf.com", false }, { "https://example.com/", "*.foo,*.bar,*.example.com,*.asdf", true }, { "https://example.com/", "*.foo,*.bar,*.asdf", false }, /* Ports! */ { "https://example.com/", "example.com:443", true }, { "https://example.com/", "example.com:42,example.com:443,example.com:99", true }, { "https://example.com/", "example.com:42,example.com:80,example.org:443", false }, { "https://example.com:1443/", "example.com", true }, { "https://example.com:44/", "example.com:443", false }, { "https://example.com:443/", "example.com:44", false }, }; for (i = 0; i < ARRAY_SIZE(url_patterns); i++) { cl_git_pass(git_net_url_parse(&url, url_patterns[i].url)); cl_assert_(git_net_url_matches_pattern_list(&url, url_patterns[i].pattern) == url_patterns[i].matches, url_patterns[i].pattern); git_net_url_dispose(&url); } }
libgit2-main
tests/libgit2/network/url/pattern.c
#include "clar_libgit2.h" #include "net.h" #include "netops.h" static git_net_url source, target; void test_network_url_joinpath__initialize(void) { memset(&source, 0, sizeof(source)); memset(&target, 0, sizeof(target)); } void test_network_url_joinpath__cleanup(void) { git_net_url_dispose(&source); git_net_url_dispose(&target); } void test_network_url_joinpath__target_paths_and_queries(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b")); cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); cl_assert_equal_s(target.path, "/a/b/c/d"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); cl_assert_equal_s(target.path, "/a/b/c/d"); cl_assert_equal_s(target.query, "foo"); git_net_url_dispose(&target); } void test_network_url_joinpath__source_query_removed(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com/a/b?query&one&two")); cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d")); cl_assert_equal_s(target.path, "/a/b/c/d"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/c/d?foo")); cl_assert_equal_s(target.path, "/a/b/c/d"); cl_assert_equal_s(target.query, "foo"); git_net_url_dispose(&target); } void test_network_url_joinpath__source_lacks_path(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com")); cl_git_pass(git_net_url_joinpath(&target, &source, "/")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); } void test_network_url_joinpath__source_is_slash(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com/")); cl_git_pass(git_net_url_joinpath(&target, &source, "/")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); } void test_network_url_joinpath__source_has_query(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com?query")); cl_git_pass(git_net_url_joinpath(&target, &source, "/")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "")); cl_assert_equal_s(target.path, "/"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/asdf?hello")); cl_assert_equal_s(target.path, "/asdf"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); cl_git_pass(git_net_url_joinpath(&target, &source, "/foo/bar?hello")); cl_assert_equal_s(target.path, "/foo/bar"); cl_assert_equal_s(target.query, "hello"); git_net_url_dispose(&target); } void test_network_url_joinpath__empty_query_ignored(void) { cl_git_pass(git_net_url_parse(&source, "http://example.com/foo")); cl_git_pass(git_net_url_joinpath(&target, &source, "/bar/baz?")); cl_assert_equal_s(target.path, "/foo/bar/baz"); cl_assert_equal_p(target.query, NULL); git_net_url_dispose(&target); }
libgit2-main
tests/libgit2/network/url/joinpath.c
#include "clar_libgit2.h" #include "config/config_helpers.h" #include "repository.h" static git_repository *_repo; static const char *_remote_name = "test"; void test_network_remote_rename__initialize(void) { _repo = cl_git_sandbox_init("testrepo.git"); } void test_network_remote_rename__cleanup(void) { cl_git_sandbox_cleanup(); } void test_network_remote_rename__renaming_a_remote_moves_related_configuration_section(void) { git_strarray problems = {0}; assert_config_entry_existence(_repo, "remote.test.fetch", true); assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); assert_config_entry_existence(_repo, "remote.test.fetch", false); assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true); } void test_network_remote_rename__renaming_a_remote_updates_branch_related_configuration_entries(void) { git_strarray problems = {0}; assert_config_entry_value(_repo, "branch.master.remote", "test"); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); assert_config_entry_value(_repo, "branch.master.remote", "just/renamed"); } void test_network_remote_rename__renaming_a_remote_updates_default_fetchrefspec(void) { git_strarray problems = {0}; cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*"); } void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void) { git_config *config; git_remote *remote; git_strarray problems = {0}; cl_git_pass(git_repository_config__weakptr(&config, _repo)); cl_git_pass(git_config_delete_entry(config, "remote.test.fetch")); cl_git_pass(git_remote_lookup(&remote, _repo, "test")); git_remote_free(remote); assert_config_entry_existence(_repo, "remote.test.fetch", false); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); } void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void) { git_config *config; git_remote *remote; git_strarray problems = {0}; cl_git_pass(git_repository_config__weakptr(&config, _repo)); cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*")); cl_git_pass(git_remote_lookup(&remote, _repo, "test")); git_remote_free(remote); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(1, problems.count); cl_assert_equal_s("+refs/*:refs/*", problems.strings[0]); git_strarray_dispose(&problems); assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*"); git_strarray_dispose(&problems); } void test_network_remote_rename__new_name_can_contain_dots(void) { git_strarray problems = {0}; cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just.renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); assert_config_entry_existence(_repo, "remote.just.renamed.fetch", true); } void test_network_remote_rename__new_name_must_conform_to_reference_naming_conventions(void) { git_strarray problems = {0}; cl_assert_equal_i( GIT_EINVALIDSPEC, git_remote_rename(&problems, _repo, _remote_name, "new@{name")); } void test_network_remote_rename__renamed_name_is_persisted(void) { git_remote *renamed; git_repository *another_repo; git_strarray problems = {0}; cl_git_fail(git_remote_lookup(&renamed, _repo, "just/renamed")); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); cl_git_pass(git_repository_open(&another_repo, "testrepo.git")); cl_git_pass(git_remote_lookup(&renamed, _repo, "just/renamed")); git_remote_free(renamed); git_repository_free(another_repo); } void test_network_remote_rename__cannot_overwrite_an_existing_remote(void) { git_strarray problems = {0}; cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _repo, _remote_name, "test")); cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _repo, _remote_name, "test_with_pushurl")); } void test_network_remote_rename__renaming_a_remote_moves_the_underlying_reference(void) { git_reference *underlying; git_strarray problems = {0}; cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed")); cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); git_reference_free(underlying); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "just/renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master")); git_reference_free(underlying); } void test_network_remote_rename__overwrite_ref_in_target(void) { git_oid id; char idstr[GIT_OID_SHA1_HEXSIZE + 1] = {0}; git_reference *ref; git_branch_t btype; git_branch_iterator *iter; git_strarray problems = {0}; cl_git_pass(git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1)); cl_git_pass(git_reference_create(&ref, _repo, "refs/remotes/renamed/master", &id, 1, NULL)); git_reference_free(ref); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); /* make sure there's only one remote-tracking branch */ cl_git_pass(git_branch_iterator_new(&iter, _repo, GIT_BRANCH_REMOTE)); cl_git_pass(git_branch_next(&ref, &btype, iter)); cl_assert_equal_s("refs/remotes/renamed/master", git_reference_name(ref)); git_oid_fmt(idstr, git_reference_target(ref)); cl_assert_equal_s("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", idstr); git_reference_free(ref); cl_git_fail_with(GIT_ITEROVER, git_branch_next(&ref, &btype, iter)); git_branch_iterator_free(iter); } void test_network_remote_rename__nonexistent_returns_enotfound(void) { git_strarray problems = {0}; int err = git_remote_rename(&problems, _repo, "nonexistent", "renamed"); cl_assert_equal_i(GIT_ENOTFOUND, err); } void test_network_remote_rename__symref_head(void) { int error; git_reference *ref; git_branch_t btype; git_branch_iterator *iter; git_strarray problems = {0}; char idstr[GIT_OID_SHA1_HEXSIZE + 1] = {0}; git_vector refs; cl_git_pass(git_reference_symbolic_create(&ref, _repo, "refs/remotes/test/HEAD", "refs/remotes/test/master", 0, NULL)); git_reference_free(ref); cl_git_pass(git_remote_rename(&problems, _repo, _remote_name, "renamed")); cl_assert_equal_i(0, problems.count); git_strarray_dispose(&problems); cl_git_pass(git_vector_init(&refs, 2, (git_vector_cmp) git_reference_cmp)); cl_git_pass(git_branch_iterator_new(&iter, _repo, GIT_BRANCH_REMOTE)); while ((error = git_branch_next(&ref, &btype, iter)) == 0) { cl_git_pass(git_vector_insert(&refs, ref)); } cl_assert_equal_i(GIT_ITEROVER, error); git_vector_sort(&refs); cl_assert_equal_i(2, refs.length); ref = git_vector_get(&refs, 0); cl_assert_equal_s("refs/remotes/renamed/HEAD", git_reference_name(ref)); cl_assert_equal_s("refs/remotes/renamed/master", git_reference_symbolic_target(ref)); git_reference_free(ref); ref = git_vector_get(&refs, 1); cl_assert_equal_s("refs/remotes/renamed/master", git_reference_name(ref)); git_oid_fmt(idstr, git_reference_target(ref)); cl_assert_equal_s("be3563ae3f795b2b4353bcce3a527ad0a4f7f644", idstr); git_reference_free(ref); git_vector_free(&refs); cl_git_fail_with(GIT_ITEROVER, git_branch_next(&ref, &btype, iter)); git_branch_iterator_free(iter); }
libgit2-main
tests/libgit2/network/remote/rename.c
#include "clar_libgit2.h" static int is_valid_name(const char *name) { int valid = 0; cl_git_pass(git_remote_name_is_valid(&valid, name)); return valid; } void test_network_remote_isvalidname__can_detect_invalid_formats(void) { cl_assert_equal_i(false, is_valid_name("/")); cl_assert_equal_i(false, is_valid_name("//")); cl_assert_equal_i(false, is_valid_name(".lock")); cl_assert_equal_i(false, is_valid_name("a.lock")); cl_assert_equal_i(false, is_valid_name("/no/leading/slash")); cl_assert_equal_i(false, is_valid_name("no/trailing/slash/")); } void test_network_remote_isvalidname__wont_hopefully_choke_on_valid_formats(void) { cl_assert_equal_i(true, is_valid_name("webmatrix")); cl_assert_equal_i(true, is_valid_name("yishaigalatzer/rules")); }
libgit2-main
tests/libgit2/network/remote/isvalidname.c
#include "clar_libgit2.h" #include "git2/sys/commit.h" #include "oid.h" static git_remote *_remote; static git_repository *_repo, *_dummy; void test_network_remote_push__initialize(void) { cl_fixture_sandbox("testrepo.git"); git_repository_open(&_repo, "testrepo.git"); /* We need a repository to have a remote */ cl_git_pass(git_repository_init(&_dummy, "dummy.git", true)); cl_git_pass(git_remote_create(&_remote, _dummy, "origin", cl_git_path_url("testrepo.git"))); } void test_network_remote_push__cleanup(void) { git_remote_free(_remote); _remote = NULL; git_repository_free(_repo); _repo = NULL; git_repository_free(_dummy); _dummy = NULL; cl_fixture_cleanup("testrepo.git"); cl_fixture_cleanup("dummy.git"); } static int negotiation_cb(const git_push_update **updates, size_t len, void *payload) { const git_push_update *expected = payload; cl_assert_equal_i(1, len); cl_assert_equal_s(expected->src_refname, updates[0]->src_refname); cl_assert_equal_s(expected->dst_refname, updates[0]->dst_refname); cl_assert_equal_oid(&expected->src, &updates[0]->src); cl_assert_equal_oid(&expected->dst, &updates[0]->dst); return 0; } void test_network_remote_push__delete_notification(void) { git_push_options opts = GIT_PUSH_OPTIONS_INIT; git_reference *ref; git_push_update expected; char *refspec = ":refs/heads/master"; const git_strarray refspecs = { &refspec, 1, }; cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/master")); expected.src_refname = ""; expected.dst_refname = "refs/heads/master"; git_oid_clear(&expected.dst, GIT_OID_SHA1); git_oid_cpy(&expected.src, git_reference_target(ref)); opts.callbacks.push_negotiation = negotiation_cb; opts.callbacks.payload = &expected; cl_git_pass(git_remote_push(_remote, &refspecs, &opts)); git_reference_free(ref); cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, _repo, "refs/heads/master")); } static void create_dummy_commit(git_reference **out, git_repository *repo) { git_index *index; git_oid tree_id, commit_id; git_signature *sig; cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_write_tree(&tree_id, index)); git_index_free(index); cl_git_pass(git_signature_now(&sig, "Pusher Joe", "pjoe")); cl_git_pass(git_commit_create_from_ids(&commit_id, repo, NULL, sig, sig, NULL, "Empty tree\n", &tree_id, 0, NULL)); cl_git_pass(git_reference_create(out, repo, "refs/heads/empty-tree", &commit_id, true, "commit yo")); git_signature_free(sig); } void test_network_remote_push__create_notification(void) { git_push_options opts = GIT_PUSH_OPTIONS_INIT; git_reference *ref; git_push_update expected; char *refspec = "refs/heads/empty-tree"; const git_strarray refspecs = { &refspec, 1, }; create_dummy_commit(&ref, _dummy); expected.src_refname = "refs/heads/empty-tree"; expected.dst_refname = "refs/heads/empty-tree"; git_oid_cpy(&expected.dst, git_reference_target(ref)); git_oid_clear(&expected.src, GIT_OID_SHA1); opts.callbacks.push_negotiation = negotiation_cb; opts.callbacks.payload = &expected; cl_git_pass(git_remote_push(_remote, &refspecs, &opts)); git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/empty-tree")); git_reference_free(ref); }
libgit2-main
tests/libgit2/network/remote/push.c
#include "clar_libgit2.h" #include "path.h" #include "posix.h" #include "git2/sys/repository.h" static git_repository *repo; static git_str file_path_buf = GIT_STR_INIT; static git_remote *remote; static char *push_refspec_strings[] = { "refs/heads/master", }; static git_strarray push_array = { push_refspec_strings, 1, }; void test_network_remote_local__initialize(void) { cl_git_pass(git_repository_init(&repo, "remotelocal/", 0)); cl_git_pass(git_repository_set_ident(repo, "Foo Bar", "[email protected]")); cl_assert(repo != NULL); } void test_network_remote_local__cleanup(void) { git_str_dispose(&file_path_buf); git_remote_free(remote); remote = NULL; git_repository_free(repo); repo = NULL; cl_fixture_cleanup("remotelocal"); } static void connect_to_local_repository(const char *local_repository) { git_str_sets(&file_path_buf, cl_git_path_url(local_repository)); cl_git_pass(git_remote_create_anonymous(&remote, repo, git_str_cstr(&file_path_buf))); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); } void test_network_remote_local__connected(void) { connect_to_local_repository(cl_fixture("testrepo.git")); cl_assert(git_remote_connected(remote)); git_remote_disconnect(remote); cl_assert(!git_remote_connected(remote)); } void test_network_remote_local__retrieve_advertised_references(void) { const git_remote_head **refs; size_t refs_len; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_ls(&refs, &refs_len, remote)); cl_assert_equal_i(refs_len, 30); } void test_network_remote_local__retrieve_advertised_before_connect(void) { const git_remote_head **refs; size_t refs_len = 0; git_str_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git"))); cl_git_pass(git_remote_create_anonymous(&remote, repo, git_str_cstr(&file_path_buf))); cl_git_fail(git_remote_ls(&refs, &refs_len, remote)); } void test_network_remote_local__retrieve_advertised_references_after_disconnect(void) { const git_remote_head **refs; size_t refs_len; connect_to_local_repository(cl_fixture("testrepo.git")); git_remote_disconnect(remote); cl_git_pass(git_remote_ls(&refs, &refs_len, remote)); cl_assert_equal_i(refs_len, 30); } void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void) { const git_remote_head **refs; size_t refs_len; cl_fixture_sandbox("testrepo.git"); cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git")); connect_to_local_repository("spaced testrepo.git"); cl_git_pass(git_remote_ls(&refs, &refs_len, remote)); cl_assert_equal_i(refs_len, 30); git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ remote = NULL; cl_fixture_cleanup("spaced testrepo.git"); } void test_network_remote_local__nested_tags_are_completely_peeled(void) { const git_remote_head **refs; size_t refs_len, i; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_ls(&refs, &refs_len, remote)); for (i = 0; i < refs_len; i++) { if (!strcmp(refs[i]->name, "refs/tags/test^{}")) cl_git_pass(git_oid_streq(&refs[i]->oid, "e90810b8df3e80c413d903f631643c716887138d")); } } void test_network_remote_local__shorthand_fetch_refspec0(void) { char *refspec_strings[] = { "master:remotes/sloppy/master", "master:boh/sloppy/master", }; git_strarray array = { refspec_strings, 2, }; git_reference *ref; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, repo, "refs/heads/boh/sloppy/master")); git_reference_free(ref); } void test_network_remote_local__shorthand_fetch_refspec1(void) { char *refspec_strings[] = { "master", "hard_tag", }; git_strarray array = { refspec_strings, 2, }; git_reference *ref; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/origin/master")); cl_git_fail(git_reference_lookup(&ref, repo, "refs/tags/hard_tag")); } void test_network_remote_local__tagopt(void) { git_reference *ref; git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; cl_git_pass(git_remote_create(&remote, repo, "tagopt", cl_git_path_url(cl_fixture("testrepo.git")))); fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_ALL; cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL)); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master")); git_reference_free(ref); cl_git_pass(git_reference_lookup(&ref, repo, "refs/tags/hard_tag")); git_reference_free(ref); fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO; cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL)); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/tagopt/master")); git_reference_free(ref); } void test_network_remote_local__push_to_bare_remote(void) { char *refspec_strings[] = { "master:master", }; git_strarray array = { refspec_strings, 1, }; /* Should be able to push to a bare remote */ git_remote *localremote; /* Get some commits */ connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); /* Set up an empty bare repo to push into */ { git_repository *localbarerepo; cl_git_pass(git_repository_init(&localbarerepo, "./localbare.git", 1)); git_repository_free(localbarerepo); } /* Connect to the bare repo */ cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localbare.git")); cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL)); /* Try to push */ cl_git_pass(git_remote_upload(localremote, &push_array, NULL)); /* Clean up */ git_remote_free(localremote); cl_fixture_cleanup("localbare.git"); } void test_network_remote_local__push_to_bare_remote_with_file_url(void) { char *refspec_strings[] = { "master:master", }; git_strarray array = { refspec_strings, 1, }; /* Should be able to push to a bare remote */ git_remote *localremote; const char *url; /* Get some commits */ connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); /* Set up an empty bare repo to push into */ { git_repository *localbarerepo; cl_git_pass(git_repository_init(&localbarerepo, "./localbare.git", 1)); git_repository_free(localbarerepo); } /* Create a file URL */ url = cl_git_path_url("./localbare.git"); /* Connect to the bare repo */ cl_git_pass(git_remote_create_anonymous(&localremote, repo, url)); cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL)); /* Try to push */ cl_git_pass(git_remote_upload(localremote, &push_array, NULL)); /* Clean up */ git_remote_free(localremote); cl_fixture_cleanup("localbare.git"); } void test_network_remote_local__push_to_non_bare_remote(void) { char *refspec_strings[] = { "master:master", }; git_strarray array = { refspec_strings, 1, }; /* Shouldn't be able to push to a non-bare remote */ git_remote *localremote; git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT; /* Get some commits */ connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, &fetch_opts, NULL)); /* Set up an empty non-bare repo to push into */ { git_repository *remoterepo = NULL; cl_git_pass(git_repository_init(&remoterepo, "localnonbare", 0)); git_repository_free(remoterepo); } /* Connect to the bare repo */ cl_git_pass(git_remote_create_anonymous(&localremote, repo, "./localnonbare")); cl_git_pass(git_remote_connect(localremote, GIT_DIRECTION_PUSH, NULL, NULL, NULL)); /* Try to push */ cl_git_fail_with(GIT_EBAREREPO, git_remote_upload(localremote, &push_array, NULL)); /* Clean up */ git_remote_free(localremote); cl_fixture_cleanup("localbare.git"); } void test_network_remote_local__fetch(void) { char *refspec_strings[] = { "master:remotes/sloppy/master", }; git_strarray array = { refspec_strings, 1, }; git_reflog *log; const git_reflog_entry *entry; git_reference *ref; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!")); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); git_reference_free(ref); cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); cl_assert_equal_i(1, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); cl_assert_equal_s("[email protected]", git_reflog_entry_committer(entry)->email); cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry)); git_reflog_free(log); } void test_network_remote_local__reflog(void) { char *refspec_strings[] = { "master:remotes/sloppy/master", }; git_strarray array = { refspec_strings, 1, }; git_reflog *log; const git_reflog_entry *entry; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, "UPDAAAAAATE!!")); cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); cl_assert_equal_i(1, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); cl_assert_equal_s("[email protected]", git_reflog_entry_committer(entry)->email); cl_assert_equal_s("UPDAAAAAATE!!", git_reflog_entry_message(entry)); git_reflog_free(log); } void test_network_remote_local__fetch_default_reflog_message(void) { char *refspec_strings[] = { "master:remotes/sloppy/master", }; git_strarray array = { refspec_strings, 1, }; git_reflog *log; const git_reflog_entry *entry; char expected_reflog_msg[1024]; connect_to_local_repository(cl_fixture("testrepo.git")); cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); cl_assert_equal_i(1, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); cl_assert_equal_s("[email protected]", git_reflog_entry_committer(entry)->email); sprintf(expected_reflog_msg, "fetch %s", git_remote_url(remote)); cl_assert_equal_s(expected_reflog_msg, git_reflog_entry_message(entry)); git_reflog_free(log); } void test_network_remote_local__opportunistic_update(void) { git_reference *ref; char *refspec_strings[] = { "master", }; git_strarray array = { refspec_strings, 1, }; /* this remote has a passive refspec of "refs/heads/<star>:refs/remotes/origin/<star>" */ cl_git_pass(git_remote_create(&remote, repo, "origin", cl_git_fixture_url("testrepo.git"))); /* and we pass the active refspec "master" */ cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL)); /* and we expect that to update our copy of origin's master */ cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/origin/master")); git_reference_free(ref); } void test_network_remote_local__update_tips_for_new_remote(void) { git_repository *src_repo; git_repository *dst_repo; git_remote *new_remote; git_reference* branch; /* Copy test repo */ cl_fixture_sandbox("testrepo.git"); cl_git_pass(git_repository_open(&src_repo, "testrepo.git")); /* Set up an empty bare repo to push into */ cl_git_pass(git_repository_init(&dst_repo, "./localbare.git", 1)); /* Push to bare repo */ cl_git_pass(git_remote_create(&new_remote, src_repo, "bare", "./localbare.git")); cl_git_pass(git_remote_push(new_remote, &push_array, NULL)); /* Make sure remote branch has been created */ cl_git_pass(git_branch_lookup(&branch, src_repo, "bare/master", GIT_BRANCH_REMOTE)); git_reference_free(branch); git_remote_free(new_remote); git_repository_free(dst_repo); cl_fixture_cleanup("localbare.git"); git_repository_free(src_repo); cl_fixture_cleanup("testrepo.git"); } void test_network_remote_local__push_delete(void) { git_repository *src_repo; git_repository *dst_repo; git_remote *remote; git_reference *ref; char *spec_push[] = { "refs/heads/master" }; char *spec_delete[] = { ":refs/heads/master" }; git_strarray specs = { spec_push, 1, }; src_repo = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_init(&dst_repo, "target.git", 1)); cl_git_pass(git_remote_create(&remote, src_repo, "origin", "./target.git")); /* Push the master branch and verify it's there */ cl_git_pass(git_remote_push(remote, &specs, NULL)); cl_git_pass(git_reference_lookup(&ref, dst_repo, "refs/heads/master")); git_reference_free(ref); specs.strings = spec_delete; cl_git_pass(git_remote_push(remote, &specs, NULL)); cl_git_fail(git_reference_lookup(&ref, dst_repo, "refs/heads/master")); git_remote_free(remote); git_repository_free(dst_repo); cl_fixture_cleanup("target.git"); cl_git_sandbox_cleanup(); } void test_network_remote_local__anonymous_remote_inmemory_repo(void) { git_repository *inmemory; git_remote *remote; git_str_sets(&file_path_buf, cl_git_path_url(cl_fixture("testrepo.git"))); cl_git_pass(git_repository_new(&inmemory)); cl_git_pass(git_remote_create_anonymous(&remote, inmemory, git_str_cstr(&file_path_buf))); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); cl_assert(git_remote_connected(remote)); git_remote_disconnect(remote); git_remote_free(remote); git_repository_free(inmemory); }
libgit2-main
tests/libgit2/network/remote/local.c
#include "clar_libgit2.h" #include "refspec.h" #include "remote.h" static git_remote *g_remote; static git_repository *g_repo_a, *g_repo_b; void test_network_remote_defaultbranch__initialize(void) { g_repo_a = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_repository_init(&g_repo_b, "repo-b.git", true)); cl_git_pass(git_remote_create(&g_remote, g_repo_b, "origin", git_repository_path(g_repo_a))); } void test_network_remote_defaultbranch__cleanup(void) { git_remote_free(g_remote); git_repository_free(g_repo_b); cl_git_sandbox_cleanup(); cl_fixture_cleanup("repo-b.git"); } static void assert_default_branch(const char *should) { git_buf name = GIT_BUF_INIT; cl_git_pass(git_remote_connect(g_remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); cl_git_pass(git_remote_default_branch(&name, g_remote)); cl_assert_equal_s(should, name.ptr); git_buf_dispose(&name); } void test_network_remote_defaultbranch__master(void) { assert_default_branch("refs/heads/master"); } void test_network_remote_defaultbranch__master_does_not_win(void) { cl_git_pass(git_repository_set_head(g_repo_a, "refs/heads/not-good")); assert_default_branch("refs/heads/not-good"); } void test_network_remote_defaultbranch__master_on_detached(void) { cl_git_pass(git_repository_detach_head(g_repo_a)); assert_default_branch("refs/heads/master"); } void test_network_remote_defaultbranch__no_default_branch(void) { git_remote *remote_b; const git_remote_head **heads; size_t len; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_remote_create(&remote_b, g_repo_b, "self", git_repository_path(g_repo_b))); cl_git_pass(git_remote_connect(remote_b, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); cl_git_pass(git_remote_ls(&heads, &len, remote_b)); cl_assert_equal_i(0, len); cl_git_fail_with(GIT_ENOTFOUND, git_remote_default_branch(&buf, remote_b)); git_remote_free(remote_b); } void test_network_remote_defaultbranch__detached_sharing_nonbranch_id(void) { git_oid id, id_cloned; git_reference *ref; git_buf buf = GIT_BUF_INIT; git_repository *cloned_repo; cl_git_pass(git_reference_name_to_id(&id, g_repo_a, "HEAD")); cl_git_pass(git_repository_detach_head(g_repo_a)); cl_git_pass(git_reference_remove(g_repo_a, "refs/heads/master")); cl_git_pass(git_reference_remove(g_repo_a, "refs/heads/not-good")); cl_git_pass(git_reference_create(&ref, g_repo_a, "refs/foo/bar", &id, 1, NULL)); git_reference_free(ref); cl_git_pass(git_remote_connect(g_remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); cl_git_fail_with(GIT_ENOTFOUND, git_remote_default_branch(&buf, g_remote)); cl_git_pass(git_clone(&cloned_repo, git_repository_path(g_repo_a), "./local-detached", NULL)); cl_assert(git_repository_head_detached(cloned_repo)); cl_git_pass(git_reference_name_to_id(&id_cloned, g_repo_a, "HEAD")); cl_assert(git_oid_equal(&id, &id_cloned)); git_repository_free(cloned_repo); } void test_network_remote_defaultbranch__unborn_HEAD_with_branches(void) { git_reference *ref; git_repository *cloned_repo; cl_git_pass(git_reference_symbolic_create(&ref, g_repo_a, "HEAD", "refs/heads/i-dont-exist", 1, NULL)); git_reference_free(ref); cl_git_pass(git_clone(&cloned_repo, git_repository_path(g_repo_a), "./semi-empty", NULL)); cl_assert(git_repository_head_unborn(cloned_repo)); git_repository_free(cloned_repo); }
libgit2-main
tests/libgit2/network/remote/defaultbranch.c
#include "clar_libgit2.h" #include "config/config_helpers.h" #include "refspec.h" #include "remote.h" static git_remote *_remote; static git_repository *_repo; static const git_refspec *_refspec; void test_network_remote_remotes__initialize(void) { _repo = cl_git_sandbox_init("testrepo.git"); cl_git_pass(git_remote_lookup(&_remote, _repo, "test")); _refspec = git_remote_get_refspec(_remote, 0); cl_assert(_refspec != NULL); } void test_network_remote_remotes__cleanup(void) { git_remote_free(_remote); _remote = NULL; cl_git_sandbox_cleanup(); } void test_network_remote_remotes__parsing(void) { git_str url = GIT_STR_INIT; git_remote *_remote2 = NULL; cl_assert_equal_s(git_remote_name(_remote), "test"); cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); cl_assert(git_remote_pushurl(_remote) == NULL); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL)); cl_assert_equal_s(url.ptr, "git://github.com/libgit2/libgit2"); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL)); cl_assert_equal_s(url.ptr, "git://github.com/libgit2/libgit2"); cl_git_pass(git_remote_lookup(&_remote2, _repo, "test_with_pushurl")); cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl"); cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2"); cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2"); cl_git_pass(git_remote__urlfordirection(&url, _remote2, GIT_DIRECTION_FETCH, NULL)); cl_assert_equal_s(url.ptr, "git://github.com/libgit2/fetchlibgit2"); cl_git_pass(git_remote__urlfordirection(&url, _remote2, GIT_DIRECTION_PUSH, NULL)); cl_assert_equal_s(url.ptr, "git://github.com/libgit2/pushlibgit2"); git_remote_free(_remote2); git_str_dispose(&url); } static int remote_ready_callback(git_remote *remote, int direction, void *payload) { if (direction == GIT_DIRECTION_PUSH) { const char *url = git_remote_pushurl(remote); cl_assert_equal_p(url, NULL);; cl_assert_equal_s(payload, "payload"); return git_remote_set_instance_pushurl(remote, "push_url"); } if (direction == GIT_DIRECTION_FETCH) { const char *url = git_remote_url(remote); cl_assert_equal_s(url, "git://github.com/libgit2/libgit2"); cl_assert_equal_s(payload, "payload"); return git_remote_set_instance_url(remote, "fetch_url"); } return -1; } void test_network_remote_remotes__remote_ready(void) { git_str url = GIT_STR_INIT; git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; callbacks.remote_ready = remote_ready_callback; callbacks.payload = "payload"; cl_assert_equal_s(git_remote_name(_remote), "test"); cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); cl_assert(git_remote_pushurl(_remote) == NULL); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, &callbacks)); cl_assert_equal_s(url.ptr, "fetch_url"); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, &callbacks)); cl_assert_equal_s(url.ptr, "push_url"); git_str_dispose(&url); } #ifndef GIT_DEPRECATE_HARD static int urlresolve_callback(git_buf *url_resolved, const char *url, int direction, void *payload) { int error = -1; cl_assert(strcmp(url, "git://github.com/libgit2/libgit2") == 0); cl_assert(strcmp(payload, "payload") == 0); cl_assert(url_resolved->size == 0); if (direction == GIT_DIRECTION_PUSH) error = git_buf_set(url_resolved, "pushresolve", strlen("pushresolve") + 1); if (direction == GIT_DIRECTION_FETCH) error = git_buf_set(url_resolved, "fetchresolve", strlen("fetchresolve") + 1); return error; } #endif void test_network_remote_remotes__urlresolve(void) { #ifndef GIT_DEPRECATE_HARD git_str url = GIT_STR_INIT; git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; callbacks.resolve_url = urlresolve_callback; callbacks.payload = "payload"; cl_assert_equal_s(git_remote_name(_remote), "test"); cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); cl_assert(git_remote_pushurl(_remote) == NULL); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, &callbacks)); cl_assert_equal_s(url.ptr, "fetchresolve"); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, &callbacks)); cl_assert_equal_s(url.ptr, "pushresolve"); git_str_dispose(&url); #endif } #ifndef GIT_DEPRECATE_HARD static int urlresolve_passthrough_callback(git_buf *url_resolved, const char *url, int direction, void *payload) { GIT_UNUSED(url_resolved); GIT_UNUSED(url); GIT_UNUSED(direction); GIT_UNUSED(payload); return GIT_PASSTHROUGH; } #endif void test_network_remote_remotes__urlresolve_passthrough(void) { #ifndef GIT_DEPRECATE_HARD git_str url = GIT_STR_INIT; const char *orig_url = "git://github.com/libgit2/libgit2"; git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; callbacks.resolve_url = urlresolve_passthrough_callback; cl_assert_equal_s(git_remote_name(_remote), "test"); cl_assert_equal_s(git_remote_url(_remote), orig_url); cl_assert(git_remote_pushurl(_remote) == NULL); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, &callbacks)); cl_assert_equal_s(url.ptr, orig_url); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, &callbacks)); cl_assert_equal_s(url.ptr, orig_url); git_str_dispose(&url); #endif } void test_network_remote_remotes__instance_url(void) { git_str url = GIT_STR_INIT; const char *orig_url = "git://github.com/libgit2/libgit2"; cl_assert_equal_s(git_remote_name(_remote), "test"); cl_assert_equal_s(git_remote_url(_remote), orig_url); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL)); cl_assert_equal_s(url.ptr, orig_url); git_str_clear(&url); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL)); cl_assert_equal_s(url.ptr, orig_url); git_str_clear(&url); /* Setting the instance url updates the fetch and push URLs */ git_remote_set_instance_url(_remote, "https://github.com/new/remote/url"); cl_assert_equal_s(git_remote_url(_remote), "https://github.com/new/remote/url"); cl_assert_equal_p(git_remote_pushurl(_remote), NULL); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL)); cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url"); git_str_clear(&url); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL)); cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url"); git_str_clear(&url); /* Setting the instance push url updates only the push URL */ git_remote_set_instance_pushurl(_remote, "https://github.com/new/push/url"); cl_assert_equal_s(git_remote_url(_remote), "https://github.com/new/remote/url"); cl_assert_equal_s(git_remote_pushurl(_remote), "https://github.com/new/push/url"); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_FETCH, NULL)); cl_assert_equal_s(url.ptr, "https://github.com/new/remote/url"); git_str_clear(&url); cl_git_pass(git_remote__urlfordirection(&url, _remote, GIT_DIRECTION_PUSH, NULL)); cl_assert_equal_s(url.ptr, "https://github.com/new/push/url"); git_str_clear(&url); git_str_dispose(&url); } void test_network_remote_remotes__pushurl(void) { const char *name = git_remote_name(_remote); git_remote *mod; cl_git_pass(git_remote_set_pushurl(_repo, name, "git://github.com/libgit2/notlibgit2")); cl_git_pass(git_remote_lookup(&mod, _repo, name)); cl_assert_equal_s(git_remote_pushurl(mod), "git://github.com/libgit2/notlibgit2"); git_remote_free(mod); cl_git_pass(git_remote_set_pushurl(_repo, name, NULL)); cl_git_pass(git_remote_lookup(&mod, _repo, name)); cl_assert(git_remote_pushurl(mod) == NULL); git_remote_free(mod); } void test_network_remote_remotes__error_when_not_found(void) { git_remote *r; cl_git_fail_with(git_remote_lookup(&r, _repo, "does-not-exist"), GIT_ENOTFOUND); cl_assert(git_error_last() != NULL); cl_assert(git_error_last()->klass == GIT_ERROR_CONFIG); } void test_network_remote_remotes__error_when_no_push_available(void) { git_remote *r; git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; char *specs = { "refs/heads/master", }; git_strarray arr = { &specs, 1, }; cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git"))); callbacks.transport = git_transport_local; cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH, &callbacks, NULL, NULL)); /* Make sure that push is really not available */ r->transport->push = NULL; cl_git_fail_with(-1, git_remote_upload(r, &arr, NULL)); git_remote_free(r); } void test_network_remote_remotes__refspec_parsing(void) { cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*"); } void test_network_remote_remotes__add_fetchspec(void) { size_t size; size = git_remote_refspec_count(_remote); cl_git_pass(git_remote_add_fetch(_repo, "test", "refs/*:refs/*")); size++; git_remote_free(_remote); cl_git_pass(git_remote_lookup(&_remote, _repo, "test")); cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote)); _refspec = git_remote_get_refspec(_remote, size - 1); cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*"); cl_assert_equal_b(_refspec->push, false); cl_git_fail_with(GIT_EINVALIDSPEC, git_remote_add_fetch(_repo, "test", "refs/*/foo/*:refs/*")); } void test_network_remote_remotes__dup(void) { git_strarray array; git_remote *dup; cl_git_pass(git_remote_dup(&dup, _remote)); cl_assert_equal_s(git_remote_name(dup), git_remote_name(_remote)); cl_assert_equal_s(git_remote_url(dup), git_remote_url(_remote)); cl_assert_equal_s(git_remote_pushurl(dup), git_remote_pushurl(_remote)); cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote)); cl_assert_equal_i(1, (int)array.count); cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array.strings[0]); git_strarray_dispose(&array); cl_git_pass(git_remote_get_push_refspecs(&array, _remote)); cl_assert_equal_i(0, (int)array.count); git_strarray_dispose(&array); git_remote_free(dup); } void test_network_remote_remotes__add_pushspec(void) { size_t size; size = git_remote_refspec_count(_remote); cl_git_pass(git_remote_add_push(_repo, "test", "refs/*:refs/*")); size++; git_remote_free(_remote); cl_git_pass(git_remote_lookup(&_remote, _repo, "test")); cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote)); _refspec = git_remote_get_refspec(_remote, size - 1); cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*"); cl_assert_equal_b(_refspec->push, true); } void test_network_remote_remotes__fnmatch(void) { cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master")); cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch")); } void test_network_remote_remotes__transform(void) { git_buf ref = GIT_BUF_INIT; cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master")); cl_assert_equal_s(ref.ptr, "refs/remotes/test/master"); git_buf_dispose(&ref); } void test_network_remote_remotes__transform_destination_to_source(void) { git_buf ref = GIT_BUF_INIT; cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master")); cl_assert_equal_s(ref.ptr, "refs/heads/master"); git_buf_dispose(&ref); } void test_network_remote_remotes__missing_refspecs(void) { git_config *cfg; git_remote_free(_remote); _remote = NULL; cl_git_pass(git_repository_config(&cfg, _repo)); cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); cl_git_pass(git_remote_lookup(&_remote, _repo, "specless")); git_config_free(cfg); } void test_network_remote_remotes__nonmatch_upstream_refspec(void) { git_config *config; git_remote *remote; char *specstr[] = { "refs/tags/*:refs/tags/*", }; git_strarray specs = { specstr, 1, }; cl_git_pass(git_remote_create(&remote, _repo, "taggy", git_repository_path(_repo))); /* * Set the current branch's upstream remote to a dummy ref so we call into the code * which tries to check for the current branch's upstream in the refspecs */ cl_git_pass(git_repository_config(&config, _repo)); cl_git_pass(git_config_set_string(config, "branch.master.remote", "taggy")); cl_git_pass(git_config_set_string(config, "branch.master.merge", "refs/heads/foo")); cl_git_pass(git_remote_fetch(remote, &specs, NULL, NULL)); git_remote_free(remote); } void test_network_remote_remotes__list(void) { git_strarray list; git_config *cfg; cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 5); git_strarray_dispose(&list); cl_git_pass(git_repository_config(&cfg, _repo)); /* Create a new remote */ cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); /* Update a remote (previously without any url/pushurl entry) */ cl_git_pass(git_config_set_string(cfg, "remote.no-remote-url.pushurl", "http://example.com")); cl_git_pass(git_remote_list(&list, _repo)); cl_assert(list.count == 7); git_strarray_dispose(&list); git_config_free(cfg); } void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void) { git_remote_free(_remote); _remote = NULL; cl_assert_equal_i(GIT_ENOTFOUND, git_remote_lookup(&_remote, _repo, "just-left-few-minutes-ago")); } void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void) { git_remote_free(_remote); _remote = NULL; cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_lookup(&_remote, _repo, "Inv@{id")); } /* * $ git remote add addtest http://github.com/libgit2/libgit2 * * $ cat .git/config * [...] * [remote "addtest"] * url = http://github.com/libgit2/libgit2 * fetch = +refs/heads/\*:refs/remotes/addtest/\* */ void test_network_remote_remotes__add(void) { git_remote_free(_remote); _remote = NULL; cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2")); cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote)); git_remote_free(_remote); _remote = NULL; cl_git_pass(git_remote_lookup(&_remote, _repo, "addtest")); cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote)); _refspec = git_vector_get(&_remote->refspecs, 0); cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec)); cl_assert(git_refspec_force(_refspec) == 1); cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec)); cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2"); } void test_network_remote_remotes__tagopt(void) { const char *name = git_remote_name(_remote); git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_ALL); assert_config_entry_value(_repo, "remote.test.tagopt", "--tags"); git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_NONE); assert_config_entry_value(_repo, "remote.test.tagopt", "--no-tags"); git_remote_set_autotag(_repo, name, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); assert_config_entry_existence(_repo, "remote.test.tagopt", false); } void test_network_remote_remotes__can_load_with_an_empty_url(void) { git_remote *remote = NULL; cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-url")); cl_assert(remote->url == NULL); cl_assert(remote->pushurl == NULL); cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); cl_assert(git_error_last() != NULL); cl_assert(git_error_last()->klass == GIT_ERROR_INVALID); git_remote_free(remote); } void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void) { git_remote *remote = NULL; cl_git_pass(git_remote_lookup(&remote, _repo, "empty-remote-pushurl")); cl_assert(remote->url == NULL); cl_assert(remote->pushurl == NULL); cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL)); git_remote_free(remote); } void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void) { git_remote *remote = NULL; cl_git_fail_with( git_remote_lookup(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND); } static const char *fetch_refspecs[] = { "+refs/heads/*:refs/remotes/origin/*", "refs/tags/*:refs/tags/*", "+refs/pull/*:refs/pull/*", }; static const char *push_refspecs[] = { "refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*", "refs/notes/*:refs/notes/*", }; void test_network_remote_remotes__query_refspecs(void) { git_remote *remote; git_strarray array; int i; cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "query", "git://github.com/libgit2/libgit2", NULL)); git_remote_free(remote); for (i = 0; i < 3; i++) { cl_git_pass(git_remote_add_fetch(_repo, "query", fetch_refspecs[i])); cl_git_pass(git_remote_add_push(_repo, "query", push_refspecs[i])); } cl_git_pass(git_remote_lookup(&remote, _repo, "query")); cl_git_pass(git_remote_get_fetch_refspecs(&array, remote)); for (i = 0; i < 3; i++) { cl_assert_equal_s(fetch_refspecs[i], array.strings[i]); } git_strarray_dispose(&array); cl_git_pass(git_remote_get_push_refspecs(&array, remote)); for (i = 0; i < 3; i++) { cl_assert_equal_s(push_refspecs[i], array.strings[i]); } git_strarray_dispose(&array); git_remote_free(remote); git_remote_delete(_repo, "test"); }
libgit2-main
tests/libgit2/network/remote/remotes.c
#include "clar_libgit2.h" #include "config/config_helpers.h" #include "repository.h" static git_repository *_repo; void test_network_remote_delete__initialize(void) { _repo = cl_git_sandbox_init("testrepo.git"); } void test_network_remote_delete__cleanup(void) { cl_git_sandbox_cleanup(); } void test_network_remote_delete__remove_remote_tracking_branches(void) { git_reference *ref; cl_git_pass(git_remote_delete(_repo, "test")); cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, _repo, "refs/remotes/test/master")); } void test_network_remote_delete__remove_remote_configuration_settings(void) { cl_assert(count_config_entries_match(_repo, "remote\\.test\\.+") > 0); cl_git_pass(git_remote_delete(_repo, "test")); cl_assert_equal_i(0, count_config_entries_match(_repo, "remote\\.test\\.+")); } void test_network_remote_delete__remove_branch_upstream_configuration_settings(void) { assert_config_entry_existence(_repo, "branch.mergeless.remote", true); assert_config_entry_existence(_repo, "branch.master.remote", true); cl_git_pass(git_remote_delete(_repo, "test")); assert_config_entry_existence(_repo, "branch.mergeless.remote", false); assert_config_entry_existence(_repo, "branch.mergeless.merge", false); assert_config_entry_existence(_repo, "branch.master.remote", false); assert_config_entry_existence(_repo, "branch.master.merge", false); }
libgit2-main
tests/libgit2/network/remote/delete.c
#include "clar_libgit2.h" #include <git2.h> #include <git2/sys/commit_graph.h> #include "commit_graph.h" #include "futils.h" void test_graph_commitgraph__parse(void) { git_repository *repo; struct git_commit_graph_file *file; struct git_commit_graph_entry e, parent; git_oid id; git_str commit_graph_path = GIT_STR_INIT; cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_str_joinpath(&commit_graph_path, git_repository_path(repo), "objects/info/commit-graph")); cl_git_pass(git_commit_graph_file_open(&file, git_str_cstr(&commit_graph_path))); cl_assert_equal_i(git_commit_graph_file_needs_refresh(file, git_str_cstr(&commit_graph_path)), 0); cl_git_pass(git_oid__fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_find(&e, file, &id, GIT_OID_SHA1_HEXSIZE)); cl_assert_equal_oid(&e.sha1, &id); cl_git_pass(git_oid__fromstr(&id, "418382dff1ffb8bdfba833f4d8bbcde58b1e7f47", GIT_OID_SHA1)); cl_assert_equal_oid(&e.tree_oid, &id); cl_assert_equal_i(e.generation, 1); cl_assert_equal_i(e.commit_time, UINT64_C(1273610423)); cl_assert_equal_i(e.parent_count, 0); cl_git_pass(git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_find(&e, file, &id, GIT_OID_SHA1_HEXSIZE)); cl_assert_equal_oid(&e.sha1, &id); cl_assert_equal_i(e.generation, 5); cl_assert_equal_i(e.commit_time, UINT64_C(1274813907)); cl_assert_equal_i(e.parent_count, 2); cl_git_pass(git_oid__fromstr(&id, "9fd738e8f7967c078dceed8190330fc8648ee56a", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_parent(&parent, file, &e, 0)); cl_assert_equal_oid(&parent.sha1, &id); cl_assert_equal_i(parent.generation, 4); cl_git_pass(git_oid__fromstr(&id, "c47800c7266a2be04c571c04d5a6614691ea99bd", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_parent(&parent, file, &e, 1)); cl_assert_equal_oid(&parent.sha1, &id); cl_assert_equal_i(parent.generation, 3); git_commit_graph_file_free(file); git_repository_free(repo); git_str_dispose(&commit_graph_path); } void test_graph_commitgraph__parse_octopus_merge(void) { git_repository *repo; struct git_commit_graph_file *file; struct git_commit_graph_entry e, parent; git_oid id; git_str commit_graph_path = GIT_STR_INIT; cl_git_pass(git_repository_open(&repo, cl_fixture("merge-recursive/.gitted"))); cl_git_pass(git_str_joinpath(&commit_graph_path, git_repository_path(repo), "objects/info/commit-graph")); cl_git_pass(git_commit_graph_file_open(&file, git_str_cstr(&commit_graph_path))); cl_git_pass(git_oid__fromstr(&id, "d71c24b3b113fd1d1909998c5bfe33b86a65ee03", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_find(&e, file, &id, GIT_OID_SHA1_HEXSIZE)); cl_assert_equal_oid(&e.sha1, &id); cl_git_pass(git_oid__fromstr(&id, "348f16ffaeb73f319a75cec5b16a0a47d2d5e27c", GIT_OID_SHA1)); cl_assert_equal_oid(&e.tree_oid, &id); cl_assert_equal_i(e.generation, 7); cl_assert_equal_i(e.commit_time, UINT64_C(1447083009)); cl_assert_equal_i(e.parent_count, 3); cl_git_pass(git_oid__fromstr(&id, "ad2ace9e15f66b3d1138922e6ffdc3ea3f967fa6", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_parent(&parent, file, &e, 0)); cl_assert_equal_oid(&parent.sha1, &id); cl_assert_equal_i(parent.generation, 6); cl_git_pass(git_oid__fromstr(&id, "483065df53c0f4a02cdc6b2910b05d388fc17ffb", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_parent(&parent, file, &e, 1)); cl_assert_equal_oid(&parent.sha1, &id); cl_assert_equal_i(parent.generation, 2); cl_git_pass(git_oid__fromstr(&id, "815b5a1c80ca749d705c7aa0cb294a00cbedd340", GIT_OID_SHA1)); cl_git_pass(git_commit_graph_entry_parent(&parent, file, &e, 2)); cl_assert_equal_oid(&parent.sha1, &id); cl_assert_equal_i(parent.generation, 6); git_commit_graph_file_free(file); git_repository_free(repo); git_str_dispose(&commit_graph_path); } void test_graph_commitgraph__writer(void) { git_repository *repo; git_commit_graph_writer *w = NULL; git_revwalk *walk; git_commit_graph_writer_options opts = GIT_COMMIT_GRAPH_WRITER_OPTIONS_INIT; git_buf cgraph = GIT_BUF_INIT; git_str expected_cgraph = GIT_STR_INIT, path = GIT_STR_INIT; cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/info")); cl_git_pass(git_commit_graph_writer_new(&w, git_str_cstr(&path))); /* This is equivalent to `git commit-graph write --reachable`. */ cl_git_pass(git_revwalk_new(&walk, repo)); cl_git_pass(git_revwalk_push_glob(walk, "refs/*")); cl_git_pass(git_commit_graph_writer_add_revwalk(w, walk)); git_revwalk_free(walk); cl_git_pass(git_commit_graph_writer_dump(&cgraph, w, &opts)); cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/info/commit-graph")); cl_git_pass(git_futils_readbuffer(&expected_cgraph, git_str_cstr(&path))); cl_assert_equal_i(cgraph.size, git_str_len(&expected_cgraph)); cl_assert_equal_i(memcmp(cgraph.ptr, git_str_cstr(&expected_cgraph), cgraph.size), 0); git_buf_dispose(&cgraph); git_str_dispose(&expected_cgraph); git_str_dispose(&path); git_commit_graph_writer_free(w); git_repository_free(repo); }
libgit2-main
tests/libgit2/graph/commitgraph.c
#include "clar_libgit2.h" #include <git2.h> #include "commit_graph.h" #include "bitvec.h" #include "vector.h" static git_repository *repo; #define TEST_REPO_PATH "merge-recursive" void test_graph_reachable_from_any__initialize(void) { git_oid oid; git_commit *commit; repo = cl_git_sandbox_init(TEST_REPO_PATH); git_oid__fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, repo, &oid)); cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL)); git_commit_free(commit); } void test_graph_reachable_from_any__cleanup(void) { cl_git_sandbox_cleanup(); } void test_graph_reachable_from_any__returns_correct_result(void) { git_object *branchA1, *branchA2, *branchB1, *branchB2, *branchC1, *branchC2, *branchH1, *branchH2; git_oid descendants[7]; cl_git_pass(git_revparse_single(&branchA1, repo, "branchA-1")); cl_git_pass(git_revparse_single(&branchA2, repo, "branchA-2")); cl_git_pass(git_revparse_single(&branchB1, repo, "branchB-1")); cl_git_pass(git_revparse_single(&branchB2, repo, "branchB-2")); cl_git_pass(git_revparse_single(&branchC1, repo, "branchC-1")); cl_git_pass(git_revparse_single(&branchC2, repo, "branchC-2")); cl_git_pass(git_revparse_single(&branchH1, repo, "branchH-1")); cl_git_pass(git_revparse_single(&branchH2, repo, "branchH-2")); cl_assert_equal_i( git_graph_reachable_from_any( repo, git_object_id(branchH1), git_object_id(branchA1), 1), 0); cl_assert_equal_i( git_graph_reachable_from_any( repo, git_object_id(branchH1), git_object_id(branchA2), 1), 0); cl_git_pass(git_oid_cpy(&descendants[0], git_object_id(branchA1))); cl_git_pass(git_oid_cpy(&descendants[1], git_object_id(branchA2))); cl_git_pass(git_oid_cpy(&descendants[2], git_object_id(branchB1))); cl_git_pass(git_oid_cpy(&descendants[3], git_object_id(branchB2))); cl_git_pass(git_oid_cpy(&descendants[4], git_object_id(branchC1))); cl_git_pass(git_oid_cpy(&descendants[5], git_object_id(branchC2))); cl_git_pass(git_oid_cpy(&descendants[6], git_object_id(branchH2))); cl_assert_equal_i( git_graph_reachable_from_any(repo, git_object_id(branchH2), descendants, 6), 0); cl_assert_equal_i( git_graph_reachable_from_any(repo, git_object_id(branchH2), descendants, 7), 1); git_object_free(branchA1); git_object_free(branchA2); git_object_free(branchB1); git_object_free(branchB2); git_object_free(branchC1); git_object_free(branchC2); git_object_free(branchH1); git_object_free(branchH2); } struct exhaustive_state { git_odb *db; git_vector commits; }; /** Get all commits from the repository. */ static int exhaustive_commits(const git_oid *id, void *payload) { struct exhaustive_state *mc = (struct exhaustive_state *)payload; size_t header_len; git_object_t header_type; int error = 0; error = git_odb_read_header(&header_len, &header_type, mc->db, id); if (error < 0) return error; if (header_type == GIT_OBJECT_COMMIT) { git_commit *commit = NULL; cl_git_pass(git_commit_lookup(&commit, repo, id)); cl_git_pass(git_vector_insert(&mc->commits, commit)); } return 0; } /** Compare the `git_oid`s of two `git_commit` objects. */ static int commit_id_cmp(const void *a, const void *b) { return git_oid_cmp( git_commit_id((const git_commit *)a), git_commit_id((const git_commit *)b)); } /** Find a `git_commit` whose ID matches the provided `git_oid` key. */ static int id_commit_id_cmp(const void *key, const void *commit) { return git_oid_cmp((const git_oid *)key, git_commit_id((const git_commit *)commit)); } void test_graph_reachable_from_any__exhaustive(void) { struct exhaustive_state mc = { .db = NULL, .commits = GIT_VECTOR_INIT, }; size_t child_idx, commit_count; size_t n_descendants; git_commit *child_commit; git_bitvec reachable; cl_git_pass(git_repository_odb(&mc.db, repo)); cl_git_pass(git_odb_foreach(mc.db, &exhaustive_commits, &mc)); git_vector_set_cmp(&mc.commits, commit_id_cmp); git_vector_sort(&mc.commits); cl_git_pass(git_bitvec_init( &reachable, git_vector_length(&mc.commits) * git_vector_length(&mc.commits))); commit_count = git_vector_length(&mc.commits); git_vector_foreach (&mc.commits, child_idx, child_commit) { unsigned int parent_i; /* We treat each commit as being able to reach itself. */ git_bitvec_set(&reachable, child_idx * commit_count + child_idx, true); for (parent_i = 0; parent_i < git_commit_parentcount(child_commit); ++parent_i) { size_t parent_idx = -1; cl_git_pass(git_vector_bsearch2( &parent_idx, &mc.commits, id_commit_id_cmp, git_commit_parent_id(child_commit, parent_i))); /* We have established that parent_idx is reachable from child_idx */ git_bitvec_set(&reachable, parent_idx * commit_count + child_idx, true); } } /* Floyd-Warshall */ { size_t i, j, k; for (k = 0; k < commit_count; ++k) { for (i = 0; i < commit_count; ++i) { if (!git_bitvec_get(&reachable, i * commit_count + k)) continue; for (j = 0; j < commit_count; ++j) { if (!git_bitvec_get(&reachable, k * commit_count + j)) continue; git_bitvec_set(&reachable, i * commit_count + j, true); } } } } /* Try 1000 subsets of 1 through 10 entries each. */ srand(0x223ddc4b); for (n_descendants = 1; n_descendants < 10; ++n_descendants) { size_t test_iteration; git_oid descendants[10]; for (test_iteration = 0; test_iteration < 1000; ++test_iteration) { size_t descendant_i; size_t child_idx, parent_idx; int expected_reachable = false, actual_reachable; git_commit *child_commit, *parent_commit; parent_idx = rand() % commit_count; parent_commit = (git_commit *)git_vector_get(&mc.commits, parent_idx); for (descendant_i = 0; descendant_i < n_descendants; ++descendant_i) { child_idx = rand() % commit_count; child_commit = (git_commit *)git_vector_get(&mc.commits, child_idx); expected_reachable |= git_bitvec_get( &reachable, parent_idx * commit_count + child_idx); git_oid_cpy(&descendants[descendant_i], git_commit_id(child_commit)); } actual_reachable = git_graph_reachable_from_any( repo, git_commit_id(parent_commit), descendants, n_descendants); if (actual_reachable != expected_reachable) { git_str error_message_buf = GIT_STR_INIT; char parent_oidbuf[9] = {0}, child_oidbuf[9] = {0}; cl_git_pass(git_oid_nfmt( parent_oidbuf, 8, git_commit_id(parent_commit))); git_str_printf(&error_message_buf, "git_graph_reachable_from_any(\"%s\", %zu, " "{", parent_oidbuf, n_descendants); for (descendant_i = 0; descendant_i < n_descendants; ++descendant_i) { cl_git_pass( git_oid_nfmt(child_oidbuf, 8, &descendants[descendant_i])); git_str_printf(&error_message_buf, " \"%s\"", child_oidbuf); } git_str_printf(&error_message_buf, " }) = %d, expected = %d", actual_reachable, expected_reachable); cl_check_(actual_reachable == expected_reachable, git_str_cstr(&error_message_buf)); } } } git_vector_foreach (&mc.commits, child_idx, child_commit) git_commit_free(child_commit); git_bitvec_free(&reachable); git_vector_free(&mc.commits); git_odb_free(mc.db); }
libgit2-main
tests/libgit2/graph/reachable_from_any.c
#include "clar_libgit2.h" static git_repository *_repo; static git_commit *commit; static size_t ahead; static size_t behind; void test_graph_ahead_behind__initialize(void) { git_oid oid; cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); cl_git_pass(git_oid__fromstr(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1)); cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); } void test_graph_ahead_behind__cleanup(void) { git_commit_free(commit); commit = NULL; git_repository_free(_repo); _repo = NULL; } void test_graph_ahead_behind__returns_correct_result(void) { git_oid oid; git_oid oid2; git_commit *other; cl_git_pass(git_oid__fromstr(&oid, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1)); cl_git_pass(git_oid__fromstr(&oid2, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1)); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &oid, &oid2)); cl_assert_equal_sz(2, ahead); cl_assert_equal_sz(6, behind); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, git_commit_id(commit), git_commit_id(commit))); cl_assert_equal_sz(ahead, behind); cl_git_pass(git_commit_nth_gen_ancestor(&other, commit, 1)); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, git_commit_id(commit), git_commit_id(other))); cl_assert_equal_sz(ahead, behind + 2); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, git_commit_id(other), git_commit_id(commit))); cl_assert_equal_sz(ahead + 2, behind); git_commit_free(other); cl_git_pass(git_commit_nth_gen_ancestor(&other, commit, 3)); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, git_commit_id(commit), git_commit_id(other))); cl_assert_equal_sz(ahead, behind + 4); cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, git_commit_id(other), git_commit_id(commit))); cl_assert_equal_sz(ahead + 4, behind); git_commit_free(other); }
libgit2-main
tests/libgit2/graph/ahead_behind.c
#include "clar_libgit2.h" static git_repository *_repo; static git_commit *commit; void test_graph_descendant_of__initialize(void) { git_oid oid; cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git"))); git_oid__fromstr(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); } void test_graph_descendant_of__cleanup(void) { git_commit_free(commit); commit = NULL; git_repository_free(_repo); _repo = NULL; } void test_graph_descendant_of__returns_correct_result(void) { git_commit *other; cl_assert_equal_i(0, git_graph_descendant_of(_repo, git_commit_id(commit), git_commit_id(commit))); cl_git_pass(git_commit_nth_gen_ancestor(&other, commit, 1)); cl_assert_equal_i(1, git_graph_descendant_of(_repo, git_commit_id(commit), git_commit_id(other))); cl_assert_equal_i(0, git_graph_descendant_of(_repo, git_commit_id(other), git_commit_id(commit))); git_commit_free(other); cl_git_pass(git_commit_nth_gen_ancestor(&other, commit, 3)); cl_assert_equal_i(1, git_graph_descendant_of(_repo, git_commit_id(commit), git_commit_id(other))); cl_assert_equal_i(0, git_graph_descendant_of(_repo, git_commit_id(other), git_commit_id(commit))); git_commit_free(other); } void test_graph_descendant_of__nopath(void) { git_oid oid; git_oid__fromstr(&oid, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1); cl_assert_equal_i(0, git_graph_descendant_of(_repo, git_commit_id(commit), &oid)); }
libgit2-main
tests/libgit2/graph/descendant_of.c
#include "clar_libgit2.h" #include "worktree_helpers.h" void cleanup_fixture_worktree(worktree_fixture *fixture) { if (!fixture) return; if (fixture->repo) { git_repository_free(fixture->repo); fixture->repo = NULL; } if (fixture->worktree) { git_repository_free(fixture->worktree); fixture->worktree = NULL; } if (fixture->reponame) cl_fixture_cleanup(fixture->reponame); if (fixture->worktreename) cl_fixture_cleanup(fixture->worktreename); } void setup_fixture_worktree(worktree_fixture *fixture) { if (fixture->reponame) fixture->repo = cl_git_sandbox_init(fixture->reponame); if (fixture->worktreename) fixture->worktree = cl_git_sandbox_init(fixture->worktreename); }
libgit2-main
tests/libgit2/worktree/worktree_helpers.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #include "reflog.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" #define REFLOG "refs/heads/testrepo-worktree" #define REFLOG_MESSAGE "reflog message" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); void test_worktree_reflog__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_reflog__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_reflog__read_worktree_HEAD(void) { git_reflog *reflog; const git_reflog_entry *entry; cl_git_pass(git_reflog_read(&reflog, fixture.worktree, "HEAD")); cl_assert_equal_i(1, git_reflog_entrycount(reflog)); entry = git_reflog_entry_byindex(reflog, 0); cl_assert(entry != NULL); cl_assert_equal_s("checkout: moving from 099fabac3a9ea935598528c27f866e34089c2eff to testrepo-worktree", git_reflog_entry_message(entry)); git_reflog_free(reflog); } void test_worktree_reflog__read_parent_HEAD(void) { git_reflog *reflog; cl_git_pass(git_reflog_read(&reflog, fixture.repo, "HEAD")); /* there is no logs/HEAD in the parent repo */ cl_assert_equal_i(0, git_reflog_entrycount(reflog)); git_reflog_free(reflog); } void test_worktree_reflog__read(void) { git_reflog *reflog; const git_reflog_entry *entry; cl_git_pass(git_reflog_read(&reflog, fixture.worktree, REFLOG)); cl_assert_equal_i(git_reflog_entrycount(reflog), 1); entry = git_reflog_entry_byindex(reflog, 0); cl_assert(entry != NULL); cl_assert_equal_s(git_reflog_entry_message(entry), "branch: Created from HEAD"); git_reflog_free(reflog); } void test_worktree_reflog__append_then_read(void) { git_reflog *reflog, *parent_reflog; const git_reflog_entry *entry; git_reference *head; git_signature *sig; const git_oid *oid; cl_git_pass(git_repository_head(&head, fixture.worktree)); cl_assert((oid = git_reference_target(head)) != NULL); cl_git_pass(git_signature_now(&sig, "foo", "foo@bar")); cl_git_pass(git_reflog_read(&reflog, fixture.worktree, REFLOG)); cl_git_pass(git_reflog_append(reflog, oid, sig, REFLOG_MESSAGE)); git_reflog_write(reflog); cl_git_pass(git_reflog_read(&parent_reflog, fixture.repo, REFLOG)); entry = git_reflog_entry_byindex(parent_reflog, 0); cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0); cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0); git_reference_free(head); git_signature_free(sig); git_reflog_free(reflog); git_reflog_free(parent_reflog); }
libgit2-main
tests/libgit2/worktree/reflog.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #include "submodule/submodule_helpers.h" #define COMMON_REPO "testrepo.git" #define WORKTREE_REPO "worktree" static git_repository *g_repo; void test_worktree_bare__initialize(void) { g_repo = cl_git_sandbox_init(COMMON_REPO); cl_assert_equal_i(1, git_repository_is_bare(g_repo)); cl_assert_equal_i(0, git_repository_is_worktree(g_repo)); } void test_worktree_bare__cleanup(void) { cl_fixture_cleanup(WORKTREE_REPO); cl_git_sandbox_cleanup(); } void test_worktree_bare__list(void) { git_strarray wts; cl_git_pass(git_worktree_list(&wts, g_repo)); cl_assert_equal_i(wts.count, 0); git_strarray_dispose(&wts); } void test_worktree_bare__add(void) { git_worktree *wt; git_repository *wtrepo; git_strarray wts; cl_git_pass(git_worktree_add(&wt, g_repo, "name", WORKTREE_REPO, NULL)); cl_git_pass(git_worktree_list(&wts, g_repo)); cl_assert_equal_i(wts.count, 1); cl_git_pass(git_worktree_validate(wt)); cl_git_pass(git_repository_open(&wtrepo, WORKTREE_REPO)); cl_assert_equal_i(0, git_repository_is_bare(wtrepo)); cl_assert_equal_i(1, git_repository_is_worktree(wtrepo)); git_strarray_dispose(&wts); git_worktree_free(wt); git_repository_free(wtrepo); } void test_worktree_bare__repository_path(void) { git_worktree *wt; git_repository *wtrepo; cl_git_pass(git_worktree_add(&wt, g_repo, "name", WORKTREE_REPO, NULL)); cl_assert_equal_s(git_worktree_path(wt), cl_git_sandbox_path(0, WORKTREE_REPO, NULL)); cl_git_pass(git_repository_open(&wtrepo, WORKTREE_REPO)); cl_assert_equal_s(git_repository_path(wtrepo), cl_git_sandbox_path(1, COMMON_REPO, "worktrees", "name", NULL)); cl_assert_equal_s(git_repository_commondir(g_repo), git_repository_commondir(wtrepo)); cl_assert_equal_s(git_repository_workdir(wtrepo), cl_git_sandbox_path(1, WORKTREE_REPO, NULL)); git_repository_free(wtrepo); git_worktree_free(wt); }
libgit2-main
tests/libgit2/worktree/bare.c
#include "clar_libgit2.h" #include "path.h" #include "refs.h" #include "worktree.h" #include "worktree_helpers.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); void test_worktree_refs__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_refs__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_refs__list(void) { git_strarray refs, wtrefs; unsigned i, j; int error = 0; cl_git_pass(git_reference_list(&refs, fixture.repo)); cl_git_pass(git_reference_list(&wtrefs, fixture.worktree)); if (refs.count != wtrefs.count) { error = GIT_ERROR; goto exit; } for (i = 0; i < refs.count; i++) { int found = 0; for (j = 0; j < wtrefs.count; j++) { if (!strcmp(refs.strings[i], wtrefs.strings[j])) { found = 1; break; } } if (!found) { error = GIT_ERROR; goto exit; } } exit: git_strarray_dispose(&refs); git_strarray_dispose(&wtrefs); cl_git_pass(error); } void test_worktree_refs__read_head(void) { git_reference *head; cl_git_pass(git_repository_head(&head, fixture.worktree)); git_reference_free(head); } void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void) { git_reference *head; cl_git_pass(git_repository_head(&head, fixture.repo)); cl_git_fail(git_repository_set_head(fixture.worktree, git_reference_name(head))); git_reference_free(head); } void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void) { git_reference *head; cl_git_pass(git_repository_head(&head, fixture.worktree)); cl_git_fail(git_repository_set_head(fixture.repo, git_reference_name(head))); git_reference_free(head); } void test_worktree_refs__set_head_works_for_current_HEAD(void) { git_reference *head; cl_git_pass(git_repository_head(&head, fixture.repo)); cl_git_pass(git_repository_set_head(fixture.repo, git_reference_name(head))); git_reference_free(head); } void test_worktree_refs__set_head_fails_when_already_checked_out(void) { cl_git_fail(git_repository_set_head(fixture.repo, "refs/heads/testrepo-worktree")); } void test_worktree_refs__delete_fails_for_checked_out_branch(void) { git_reference *branch; cl_git_pass(git_branch_lookup(&branch, fixture.repo, "testrepo-worktree", GIT_BRANCH_LOCAL)); cl_git_fail(git_branch_delete(branch)); git_reference_free(branch); } void test_worktree_refs__delete_succeeds_after_pruning_worktree(void) { git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_reference *branch; git_worktree *worktree; opts.flags = GIT_WORKTREE_PRUNE_VALID; cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename)); cl_git_pass(git_worktree_prune(worktree, &opts)); git_worktree_free(worktree); cl_git_pass(git_branch_lookup(&branch, fixture.repo, "testrepo-worktree", GIT_BRANCH_LOCAL)); cl_git_pass(git_branch_delete(branch)); git_reference_free(branch); } void test_worktree_refs__delete_unrelated_branch_on_worktree(void) { git_reference *branch; cl_git_pass(git_branch_lookup(&branch, fixture.worktree, "merge-conflict", GIT_BRANCH_LOCAL)); cl_git_pass(git_branch_delete(branch)); git_reference_free(branch); } void test_worktree_refs__delete_unrelated_branch_on_parent(void) { git_reference *branch; cl_git_pass(git_branch_lookup(&branch, fixture.repo, "merge-conflict", GIT_BRANCH_LOCAL)); cl_git_pass(git_branch_delete(branch)); git_reference_free(branch); } void test_worktree_refs__renaming_reference_updates_worktree_heads(void) { git_reference *head, *branch, *renamed; cl_git_pass(git_branch_lookup(&branch, fixture.repo, "testrepo-worktree", GIT_BRANCH_LOCAL)); cl_git_pass(git_reference_rename(&renamed, branch, "refs/heads/renamed", 0, NULL)); cl_git_pass(git_reference_lookup(&head, fixture.worktree, GIT_HEAD_FILE)); cl_assert_equal_i(git_reference_type(head), GIT_REFERENCE_SYMBOLIC); cl_assert_equal_s(git_reference_symbolic_target(head), "refs/heads/renamed"); git_reference_free(head); git_reference_free(branch); git_reference_free(renamed); } void test_worktree_refs__creating_refs_uses_commondir(void) { git_reference *head, *branch, *lookup; git_commit *commit; git_str refpath = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&refpath, git_repository_commondir(fixture.worktree), "refs/heads/testbranch")); cl_assert(!git_fs_path_exists(refpath.ptr)); cl_git_pass(git_repository_head(&head, fixture.worktree)); cl_git_pass(git_commit_lookup(&commit, fixture.worktree, git_reference_target(head))); cl_git_pass(git_branch_create(&branch, fixture.worktree, "testbranch", commit, 0)); cl_git_pass(git_branch_lookup(&lookup, fixture.worktree, "testbranch", GIT_BRANCH_LOCAL)); cl_assert(git_reference_cmp(branch, lookup) == 0); cl_assert(git_fs_path_exists(refpath.ptr)); git_reference_free(lookup); git_reference_free(branch); git_reference_free(head); git_commit_free(commit); git_str_dispose(&refpath); }
libgit2-main
tests/libgit2/worktree/refs.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #include "submodule/submodule_helpers.h" #include "repository.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); void test_worktree_repository__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_repository__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_repository__head(void) { git_reference *ref, *head; cl_git_pass(git_reference_lookup(&ref, fixture.repo, "refs/heads/testrepo-worktree")); cl_git_pass(git_repository_head_for_worktree(&head, fixture.repo, "testrepo-worktree")); cl_assert(git_reference_cmp(ref, head) == 0); cl_assert(git_reference_owner(ref) == fixture.repo); git_reference_free(ref); git_reference_free(head); } void test_worktree_repository__head_fails_for_invalid_worktree(void) { git_reference *head = NULL; cl_git_fail(git_repository_head_for_worktree(&head, fixture.repo, "invalid")); cl_assert(head == NULL); } void test_worktree_repository__head_detached(void) { git_reference *ref, *head; cl_git_pass(git_reference_lookup(&ref, fixture.repo, "refs/heads/testrepo-worktree")); cl_git_pass(git_repository_set_head_detached(fixture.worktree, &ref->target.oid)); cl_assert(git_repository_head_detached(fixture.worktree)); cl_assert(git_repository_head_detached_for_worktree(fixture.repo, "testrepo-worktree")); cl_git_pass(git_repository_head_for_worktree(&head, fixture.repo, "testrepo-worktree")); cl_assert_equal_oid(&ref->target.oid, &head->target.oid); git_reference_free(ref); git_reference_free(head); } void test_worktree_repository__head_detached_fails_for_invalid_worktree(void) { git_reference *head = NULL; cl_git_fail(git_repository_head_detached_for_worktree(fixture.repo, "invalid")); cl_assert(head == NULL); }
libgit2-main
tests/libgit2/worktree/repository.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); void test_worktree_config__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_config__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_config__open(void) { git_config *cfg; cl_git_pass(git_repository_config(&cfg, fixture.worktree)); cl_assert(cfg != NULL); git_config_free(cfg); } void test_worktree_config__set(void) { git_config *cfg; int32_t val; cl_git_pass(git_repository_config(&cfg, fixture.worktree)); cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5)); git_config_free(cfg); /* * reopen to verify configuration has been set in the * common dir */ cl_git_pass(git_repository_config(&cfg, fixture.repo)); cl_git_pass(git_config_get_int32(&val, cfg, "core.dummy")); cl_assert_equal_i(val, 5); git_config_free(cfg); }
libgit2-main
tests/libgit2/worktree/config.c
#include "clar_libgit2.h" #include "repository.h" #include "worktree.h" #include "worktree_helpers.h" #define WORKTREE_PARENT "submodules-worktree-parent" #define WORKTREE_CHILD "submodules-worktree-child" static worktree_fixture parent = WORKTREE_FIXTURE_INIT("submodules", WORKTREE_PARENT); static worktree_fixture child = WORKTREE_FIXTURE_INIT(NULL, WORKTREE_CHILD); void test_worktree_submodule__initialize(void) { setup_fixture_worktree(&parent); cl_git_pass(p_rename( "submodules/testrepo/.gitted", "submodules/testrepo/.git")); setup_fixture_worktree(&child); } void test_worktree_submodule__cleanup(void) { cleanup_fixture_worktree(&child); cleanup_fixture_worktree(&parent); } void test_worktree_submodule__submodule_worktree_parent(void) { cl_assert(git_repository_path(parent.worktree) != NULL); cl_assert(git_repository_workdir(parent.worktree) != NULL); cl_assert(!parent.repo->is_worktree); cl_assert(parent.worktree->is_worktree); } void test_worktree_submodule__submodule_worktree_child(void) { cl_assert(!parent.repo->is_worktree); cl_assert(parent.worktree->is_worktree); cl_assert(child.worktree->is_worktree); } void test_worktree_submodule__open_discovered_submodule_worktree(void) { git_buf path = GIT_BUF_INIT; git_repository *repo; cl_git_pass(git_repository_discover(&path, git_repository_workdir(child.worktree), false, NULL)); cl_git_pass(git_repository_open(&repo, path.ptr)); cl_assert_equal_s(git_repository_workdir(child.worktree), git_repository_workdir(repo)); git_buf_dispose(&path); git_repository_free(repo); } void test_worktree_submodule__resolve_relative_url(void) { git_str wt_path = GIT_STR_INIT; git_buf sm_relative_path = GIT_BUF_INIT, wt_relative_path = GIT_BUF_INIT; git_repository *repo; git_worktree *wt; cl_git_pass(git_futils_mkdir("subdir", 0755, GIT_MKDIR_PATH)); cl_git_pass(git_fs_path_prettify_dir(&wt_path, "subdir", NULL)); cl_git_pass(git_str_joinpath(&wt_path, wt_path.ptr, "wt")); /* Open child repository, which is a submodule */ cl_git_pass(git_repository_open(&child.repo, WORKTREE_CHILD)); /* Create worktree of submodule repository */ cl_git_pass(git_worktree_add(&wt, child.repo, "subdir", wt_path.ptr, NULL)); cl_git_pass(git_repository_open_from_worktree(&repo, wt)); cl_git_pass(git_submodule_resolve_url(&sm_relative_path, repo, "../" WORKTREE_CHILD)); cl_git_pass(git_submodule_resolve_url(&wt_relative_path, child.repo, "../" WORKTREE_CHILD)); cl_assert_equal_s(sm_relative_path.ptr, wt_relative_path.ptr); git_worktree_free(wt); git_repository_free(repo); git_str_dispose(&wt_path); git_buf_dispose(&sm_relative_path); git_buf_dispose(&wt_relative_path); }
libgit2-main
tests/libgit2/worktree/submodule.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #include "submodule/submodule_helpers.h" #include "checkout.h" #include "repository.h" #include "worktree.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); void test_worktree_worktree__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_worktree__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_worktree__list(void) { git_strarray wts; cl_git_pass(git_worktree_list(&wts, fixture.repo)); cl_assert_equal_i(wts.count, 1); cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); git_strarray_dispose(&wts); } void test_worktree_worktree__list_with_invalid_worktree_dirs(void) { const char *filesets[3][2] = { { "gitdir", "commondir" }, { "gitdir", "HEAD" }, { "HEAD", "commondir" }, }; git_str path = GIT_STR_INIT; git_strarray wts; size_t i, j, len; cl_git_pass(git_str_joinpath(&path, fixture.repo->commondir, "worktrees/invalid")); cl_git_pass(p_mkdir(path.ptr, 0755)); len = path.size; for (i = 0; i < ARRAY_SIZE(filesets); i++) { for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) { git_str_truncate(&path, len); cl_git_pass(git_str_joinpath(&path, path.ptr, filesets[i][j])); cl_git_pass(p_close(p_creat(path.ptr, 0644))); } cl_git_pass(git_worktree_list(&wts, fixture.worktree)); cl_assert_equal_i(wts.count, 1); cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); git_strarray_dispose(&wts); for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) { git_str_truncate(&path, len); cl_git_pass(git_str_joinpath(&path, path.ptr, filesets[i][j])); p_unlink(path.ptr); } } git_str_dispose(&path); } void test_worktree_worktree__list_in_worktree_repo(void) { git_strarray wts; cl_git_pass(git_worktree_list(&wts, fixture.worktree)); cl_assert_equal_i(wts.count, 1); cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); git_strarray_dispose(&wts); } void test_worktree_worktree__list_without_worktrees(void) { git_repository *repo; git_strarray wts; repo = cl_git_sandbox_init("testrepo2"); cl_git_pass(git_worktree_list(&wts, repo)); cl_assert_equal_i(wts.count, 0); git_repository_free(repo); } void test_worktree_worktree__lookup(void) { git_worktree *wt; git_str gitdir_path = GIT_STR_INIT; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_str_joinpath(&gitdir_path, fixture.repo->commondir, "worktrees/testrepo-worktree/")); cl_assert_equal_s(wt->gitdir_path, gitdir_path.ptr); cl_assert_equal_s(wt->parent_path, fixture.repo->workdir); cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink); cl_assert_equal_s(wt->commondir_path, fixture.repo->gitdir); cl_assert_equal_s(wt->commondir_path, fixture.repo->commondir); git_str_dispose(&gitdir_path); git_worktree_free(wt); } void test_worktree_worktree__lookup_nonexistent_worktree(void) { git_worktree *wt; cl_git_fail(git_worktree_lookup(&wt, fixture.repo, "nonexistent")); cl_assert_equal_p(wt, NULL); } void test_worktree_worktree__open(void) { git_worktree *wt; git_repository *repo; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_repository_open_from_worktree(&repo, wt)); cl_assert_equal_s(git_repository_workdir(repo), git_repository_workdir(fixture.worktree)); git_repository_free(repo); git_worktree_free(wt); } void test_worktree_worktree__open_invalid_commondir(void) { git_worktree *wt; git_repository *repo; git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; cl_git_pass(git_str_sets(&buf, "/path/to/nonexistent/commondir")); cl_git_pass(git_str_joinpath(&path, fixture.repo->commondir, "worktrees/testrepo-worktree/commondir")); cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644)); cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_fail(git_repository_open_from_worktree(&repo, wt)); git_str_dispose(&buf); git_str_dispose(&path); git_worktree_free(wt); } void test_worktree_worktree__open_invalid_gitdir(void) { git_worktree *wt; git_repository *repo; git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; cl_git_pass(git_str_sets(&buf, "/path/to/nonexistent/gitdir")); cl_git_pass(git_str_joinpath(&path, fixture.repo->commondir, "worktrees/testrepo-worktree/gitdir")); cl_git_pass(git_futils_writebuffer(&buf, path.ptr, O_RDWR, 0644)); cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_fail(git_repository_open_from_worktree(&repo, wt)); git_str_dispose(&buf); git_str_dispose(&path); git_worktree_free(wt); } void test_worktree_worktree__open_invalid_parent(void) { git_worktree *wt; git_repository *repo; git_str buf = GIT_STR_INIT; cl_git_pass(git_str_sets(&buf, "/path/to/nonexistent/gitdir")); cl_git_pass(git_futils_writebuffer(&buf, fixture.worktree->gitlink, O_RDWR, 0644)); cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_fail(git_repository_open_from_worktree(&repo, wt)); git_str_dispose(&buf); git_worktree_free(wt); } void test_worktree_worktree__init(void) { git_worktree *wt; git_repository *repo; git_reference *branch; git_str path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-new")); cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL)); /* Open and verify created repo */ cl_git_pass(git_repository_open(&repo, path.ptr)); cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-new/") == 0); cl_git_pass(git_branch_lookup(&branch, repo, "worktree-new", GIT_BRANCH_LOCAL)); git_str_dispose(&path); git_worktree_free(wt); git_reference_free(branch); git_repository_free(repo); } void test_worktree_worktree__add_locked(void) { git_worktree *wt; git_repository *repo; git_reference *branch; git_str path = GIT_STR_INIT; git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT; opts.lock = 1; cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-locked")); cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-locked", path.ptr, &opts)); /* Open and verify created repo */ cl_assert(git_worktree_is_locked(NULL, wt)); cl_git_pass(git_repository_open(&repo, path.ptr)); cl_assert(git__suffixcmp(git_repository_workdir(repo), "worktree-locked/") == 0); cl_git_pass(git_branch_lookup(&branch, repo, "worktree-locked", GIT_BRANCH_LOCAL)); git_str_dispose(&path); git_worktree_free(wt); git_reference_free(branch); git_repository_free(repo); } void test_worktree_worktree__init_existing_branch(void) { git_reference *head, *branch; git_commit *commit; git_worktree *wt; git_str path = GIT_STR_INIT; cl_git_pass(git_repository_head(&head, fixture.repo)); cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid)); cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-new", commit, false)); cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-new")); cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL)); git_str_dispose(&path); git_commit_free(commit); git_reference_free(head); git_reference_free(branch); } void test_worktree_worktree__add_with_explicit_branch(void) { git_reference *head, *branch, *wthead; git_commit *commit; git_worktree *wt; git_repository *wtrepo; git_str path = GIT_STR_INIT; git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT; cl_git_pass(git_repository_head(&head, fixture.repo)); cl_git_pass(git_commit_lookup(&commit, fixture.repo, &head->target.oid)); cl_git_pass(git_branch_create(&branch, fixture.repo, "worktree-with-ref", commit, false)); opts.ref = branch; cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-with-different-name")); cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-with-different-name", path.ptr, &opts)); cl_git_pass(git_repository_open_from_worktree(&wtrepo, wt)); cl_git_pass(git_repository_head(&wthead, wtrepo)); cl_assert_equal_s(git_reference_name(wthead), "refs/heads/worktree-with-ref"); git_str_dispose(&path); git_commit_free(commit); git_reference_free(head); git_reference_free(branch); git_reference_free(wthead); git_repository_free(wtrepo); git_worktree_free(wt); } void test_worktree_worktree__add_no_checkout(void) { git_worktree *wt; git_repository *wtrepo; git_index *index; git_str path = GIT_STR_INIT; git_worktree_add_options opts = GIT_WORKTREE_ADD_OPTIONS_INIT; opts.checkout_options.checkout_strategy = GIT_CHECKOUT_NONE; cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-no-checkout")); cl_git_pass(git_worktree_add(&wt, fixture.repo, "worktree-no-checkout", path.ptr, &opts)); cl_git_pass(git_repository_open(&wtrepo, path.ptr)); cl_git_pass(git_repository_index(&index, wtrepo)); cl_assert_equal_i(git_index_entrycount(index), 0); git_str_dispose(&path); git_worktree_free(wt); git_index_free(index); git_repository_free(wtrepo); } void test_worktree_worktree__init_existing_worktree(void) { git_worktree *wt; git_str path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../worktree-new")); cl_git_fail(git_worktree_add(&wt, fixture.repo, "testrepo-worktree", path.ptr, NULL)); cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert_equal_s(wt->gitlink_path, fixture.worktree->gitlink); git_str_dispose(&path); git_worktree_free(wt); } void test_worktree_worktree__init_existing_path(void) { const char *wtfiles[] = { "HEAD", "commondir", "gitdir", "index" }; git_worktree *wt; git_str path = GIT_STR_INIT; unsigned i; /* Delete files to verify they have not been created by * the init call */ for (i = 0; i < ARRAY_SIZE(wtfiles); i++) { cl_git_pass(git_str_joinpath(&path, fixture.worktree->gitdir, wtfiles[i])); cl_git_pass(p_unlink(path.ptr)); } cl_git_pass(git_str_joinpath(&path, fixture.repo->workdir, "../testrepo-worktree")); cl_git_fail(git_worktree_add(&wt, fixture.repo, "worktree-new", path.ptr, NULL)); /* Verify files have not been re-created */ for (i = 0; i < ARRAY_SIZE(wtfiles); i++) { cl_git_pass(git_str_joinpath(&path, fixture.worktree->gitdir, wtfiles[i])); cl_assert(!git_fs_path_exists(path.ptr)); } git_str_dispose(&path); } void test_worktree_worktree__init_submodule(void) { git_repository *repo, *sm, *wt; git_worktree *worktree; git_str path = GIT_STR_INIT; cleanup_fixture_worktree(&fixture); repo = setup_fixture_submod2(); cl_git_pass(git_str_joinpath(&path, repo->workdir, "sm_unchanged")); cl_git_pass(git_repository_open(&sm, path.ptr)); cl_git_pass(git_str_joinpath(&path, repo->workdir, "../worktree/")); cl_git_pass(git_worktree_add(&worktree, sm, "repo-worktree", path.ptr, NULL)); cl_git_pass(git_repository_open_from_worktree(&wt, worktree)); cl_git_pass(git_fs_path_prettify_dir(&path, path.ptr, NULL)); cl_assert_equal_s(path.ptr, wt->workdir); cl_git_pass(git_fs_path_prettify_dir(&path, sm->commondir, NULL)); cl_assert_equal_s(sm->commondir, wt->commondir); cl_git_pass(git_str_joinpath(&path, sm->gitdir, "worktrees/repo-worktree/")); cl_assert_equal_s(path.ptr, wt->gitdir); git_str_dispose(&path); git_worktree_free(worktree); git_repository_free(sm); git_repository_free(wt); } void test_worktree_worktree__validate(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_validate(wt)); git_worktree_free(wt); } void test_worktree_worktree__name(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert_equal_s(git_worktree_name(wt), "testrepo-worktree"); git_worktree_free(wt); } void test_worktree_worktree__path(void) { git_worktree *wt; git_str expected_path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&expected_path, clar_sandbox_path(), "testrepo-worktree")); cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert_equal_s(git_worktree_path(wt), expected_path.ptr); git_str_dispose(&expected_path); git_worktree_free(wt); } void test_worktree_worktree__validate_invalid_commondir(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); git__free(wt->commondir_path); wt->commondir_path = "/path/to/invalid/commondir"; cl_git_fail(git_worktree_validate(wt)); wt->commondir_path = NULL; git_worktree_free(wt); } void test_worktree_worktree__validate_invalid_gitdir(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); git__free(wt->gitdir_path); wt->gitdir_path = "/path/to/invalid/gitdir"; cl_git_fail(git_worktree_validate(wt)); wt->gitdir_path = NULL; git_worktree_free(wt); } void test_worktree_worktree__validate_invalid_parent(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); git__free(wt->parent_path); wt->parent_path = "/path/to/invalid/parent"; cl_git_fail(git_worktree_validate(wt)); wt->parent_path = NULL; git_worktree_free(wt); } void test_worktree_worktree__lock_with_reason(void) { git_worktree *wt; git_buf reason = GIT_BUF_INIT; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert(!git_worktree_is_locked(NULL, wt)); cl_git_pass(git_worktree_lock(wt, "because")); cl_assert(git_worktree_is_locked(&reason, wt) > 0); cl_assert_equal_s(reason.ptr, "because"); cl_assert(wt->locked); git_buf_dispose(&reason); git_worktree_free(wt); } void test_worktree_worktree__lock_without_reason(void) { git_worktree *wt; git_buf reason = GIT_BUF_INIT; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert(!git_worktree_is_locked(NULL, wt)); cl_git_pass(git_worktree_lock(wt, NULL)); cl_assert(git_worktree_is_locked(&reason, wt) > 0); cl_assert_equal_i(reason.size, 0); cl_assert(wt->locked); git_buf_dispose(&reason); git_worktree_free(wt); } void test_worktree_worktree__unlock_unlocked_worktree(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_assert(!git_worktree_is_locked(NULL, wt)); cl_assert_equal_i(1, git_worktree_unlock(wt)); cl_assert(!wt->locked); git_worktree_free(wt); } void test_worktree_worktree__unlock_locked_worktree(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_lock(wt, NULL)); cl_assert(git_worktree_is_locked(NULL, wt)); cl_assert_equal_i(0, git_worktree_unlock(wt)); cl_assert(!wt->locked); git_worktree_free(wt); } void test_worktree_worktree__prune_without_opts_fails(void) { git_worktree *wt; git_repository *repo; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_fail(git_worktree_prune(wt, NULL)); /* Assert the repository is still valid */ cl_git_pass(git_repository_open_from_worktree(&repo, wt)); git_worktree_free(wt); git_repository_free(repo); } void test_worktree_worktree__prune_valid(void) { git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_worktree *wt; git_repository *repo; opts.flags = GIT_WORKTREE_PRUNE_VALID; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_prune(wt, &opts)); /* Assert the repository is not valid anymore */ cl_git_fail(git_repository_open_from_worktree(&repo, wt)); git_worktree_free(wt); git_repository_free(repo); } void test_worktree_worktree__prune_locked(void) { git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_worktree *wt; git_repository *repo; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_lock(wt, NULL)); opts.flags = GIT_WORKTREE_PRUNE_VALID; cl_git_fail(git_worktree_prune(wt, &opts)); /* Assert the repository is still valid */ cl_git_pass(git_repository_open_from_worktree(&repo, wt)); opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_LOCKED; cl_git_pass(git_worktree_prune(wt, &opts)); git_worktree_free(wt); git_repository_free(repo); } void test_worktree_worktree__prune_gitdir_only(void) { git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_worktree *wt; opts.flags = GIT_WORKTREE_PRUNE_VALID; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_prune(wt, &opts)); cl_assert(!git_fs_path_exists(wt->gitdir_path)); cl_assert(git_fs_path_exists(wt->gitlink_path)); git_worktree_free(wt); } void test_worktree_worktree__prune_worktree(void) { git_worktree_prune_options opts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_worktree *wt; opts.flags = GIT_WORKTREE_PRUNE_VALID|GIT_WORKTREE_PRUNE_WORKING_TREE; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); cl_git_pass(git_worktree_prune(wt, &opts)); cl_assert(!git_fs_path_exists(wt->gitdir_path)); cl_assert(!git_fs_path_exists(wt->gitlink_path)); git_worktree_free(wt); } static int foreach_worktree_cb(git_repository *worktree, void *payload) { int *counter = (int *)payload; switch (*counter) { case 0: cl_assert_equal_s(git_repository_path(fixture.repo), git_repository_path(worktree)); cl_assert(!git_repository_is_worktree(worktree)); break; case 1: cl_assert_equal_s(git_repository_path(fixture.worktree), git_repository_path(worktree)); cl_assert(git_repository_is_worktree(worktree)); break; default: cl_fail("more worktrees found than expected"); } (*counter)++; return 0; } void test_worktree_worktree__foreach_worktree_lists_all_worktrees(void) { int counter = 0; cl_git_pass(git_repository_foreach_worktree(fixture.repo, foreach_worktree_cb, &counter)); } void test_worktree_worktree__validate_invalid_worktreedir(void) { git_worktree *wt; cl_git_pass(git_worktree_lookup(&wt, fixture.repo, "testrepo-worktree")); p_rename("testrepo-worktree", "testrepo-worktree-tmp"); cl_git_fail(git_worktree_validate(wt)); p_rename("testrepo-worktree-tmp", "testrepo-worktree"); git_worktree_free(wt); }
libgit2-main
tests/libgit2/worktree/worktree.c
#include "clar_libgit2.h" #include "repository.h" #include "worktree.h" #include "worktree_helpers.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir) { cl_assert(wt->is_worktree); cl_assert_equal_s(wt->workdir, cl_git_sandbox_path(1, wtdir, NULL)); cl_assert_equal_s(wt->gitlink, cl_git_sandbox_path(0, wtdir, ".git", NULL)); cl_assert_equal_s(wt->gitdir, cl_git_sandbox_path(1, parentdir, ".git", "worktrees", wtdir, NULL)); } void test_worktree_open__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_open__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_open__repository(void) { assert_worktree_valid(fixture.worktree, COMMON_REPO, WORKTREE_REPO); } void test_worktree_open__repository_through_workdir(void) { git_repository *wt; cl_git_pass(git_repository_open(&wt, WORKTREE_REPO)); assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO); git_repository_free(wt); } void test_worktree_open__repository_through_gitlink(void) { git_repository *wt; cl_git_pass(git_repository_open(&wt, WORKTREE_REPO "/.git")); assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO); git_repository_free(wt); } void test_worktree_open__repository_through_gitdir(void) { git_str gitdir_path = GIT_STR_INIT; git_repository *wt; cl_git_pass(git_str_joinpath(&gitdir_path, COMMON_REPO, ".git")); cl_git_pass(git_str_joinpath(&gitdir_path, gitdir_path.ptr, "worktrees")); cl_git_pass(git_str_joinpath(&gitdir_path, gitdir_path.ptr, "testrepo-worktree")); cl_git_pass(git_repository_open(&wt, gitdir_path.ptr)); assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO); git_str_dispose(&gitdir_path); git_repository_free(wt); } void test_worktree_open__open_discovered_worktree(void) { git_buf path = GIT_BUF_INIT; git_repository *repo; cl_git_pass(git_repository_discover(&path, git_repository_workdir(fixture.worktree), false, NULL)); cl_git_pass(git_repository_open(&repo, path.ptr)); cl_assert_equal_s(git_repository_workdir(fixture.worktree), git_repository_workdir(repo)); git_buf_dispose(&path); git_repository_free(repo); } void test_worktree_open__repository_with_nonexistent_parent(void) { git_repository *repo; cleanup_fixture_worktree(&fixture); cl_fixture_sandbox(WORKTREE_REPO); cl_git_pass(p_chdir(WORKTREE_REPO)); cl_git_pass(cl_rename(".gitted", ".git")); cl_git_pass(p_chdir("..")); cl_git_fail(git_repository_open(&repo, WORKTREE_REPO)); cl_fixture_cleanup(WORKTREE_REPO); } void test_worktree_open__open_from_repository(void) { git_worktree *opened, *lookedup; cl_git_pass(git_worktree_open_from_repository(&opened, fixture.worktree)); cl_git_pass(git_worktree_lookup(&lookedup, fixture.repo, WORKTREE_REPO)); cl_assert_equal_s(opened->name, lookedup->name); cl_assert_equal_s(opened->gitdir_path, lookedup->gitdir_path); cl_assert_equal_s(opened->gitlink_path, lookedup->gitlink_path); cl_assert_equal_s(opened->parent_path, lookedup->parent_path); cl_assert_equal_s(opened->commondir_path, lookedup->commondir_path); cl_assert_equal_i(opened->locked, lookedup->locked); git_worktree_free(opened); git_worktree_free(lookedup); } void test_worktree_open__open_from_nonworktree_fails(void) { git_worktree *wt; cl_git_fail(git_worktree_open_from_repository(&wt, fixture.repo)); }
libgit2-main
tests/libgit2/worktree/open.c
#include "clar_libgit2.h" #include "worktree_helpers.h" #include "merge/merge_helpers.h" #define COMMON_REPO "testrepo" #define WORKTREE_REPO "testrepo-worktree" #define MASTER_BRANCH "refs/heads/master" #define CONFLICT_BRANCH "refs/heads/merge-conflict" #define CONFLICT_BRANCH_FILE_TXT \ "<<<<<<< HEAD\n" \ "hi\n" \ "bye!\n" \ "=======\n" \ "conflict\n" \ ">>>>>>> merge-conflict\n" \ static worktree_fixture fixture = WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); static const char *merge_files[] = { GIT_MERGE_HEAD_FILE, GIT_ORIG_HEAD_FILE, GIT_MERGE_MODE_FILE, GIT_MERGE_MSG_FILE, }; void test_worktree_merge__initialize(void) { setup_fixture_worktree(&fixture); } void test_worktree_merge__cleanup(void) { cleanup_fixture_worktree(&fixture); } void test_worktree_merge__merge_head(void) { git_reference *theirs_ref, *ref; git_annotated_commit *theirs; cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref)); cl_git_pass(git_merge(fixture.worktree, (const git_annotated_commit **)&theirs, 1, NULL, NULL)); cl_git_pass(git_reference_lookup(&ref, fixture.worktree, GIT_MERGE_HEAD_FILE)); git_reference_free(ref); git_reference_free(theirs_ref); git_annotated_commit_free(theirs); } void test_worktree_merge__merge_setup(void) { git_reference *ours_ref, *theirs_ref; git_annotated_commit *ours, *theirs; git_str path = GIT_STR_INIT; unsigned i; cl_git_pass(git_reference_lookup(&ours_ref, fixture.worktree, MASTER_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&ours, fixture.worktree, ours_ref)); cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref)); cl_git_pass(git_merge__setup(fixture.worktree, ours, (const git_annotated_commit **)&theirs, 1)); for (i = 0; i < ARRAY_SIZE(merge_files); i++) { cl_git_pass(git_str_joinpath(&path, fixture.worktree->gitdir, merge_files[i])); cl_assert(git_fs_path_exists(path.ptr)); } git_str_dispose(&path); git_reference_free(ours_ref); git_reference_free(theirs_ref); git_annotated_commit_free(ours); git_annotated_commit_free(theirs); } void test_worktree_merge__merge_conflict(void) { git_str path = GIT_STR_INIT, buf = GIT_STR_INIT; git_reference *theirs_ref; git_annotated_commit *theirs; git_index *index; const git_index_entry *entry; size_t i, conflicts = 0; cl_git_pass(git_reference_lookup(&theirs_ref, fixture.worktree, CONFLICT_BRANCH)); cl_git_pass(git_annotated_commit_from_ref(&theirs, fixture.worktree, theirs_ref)); cl_git_pass(git_merge(fixture.worktree, (const git_annotated_commit **)&theirs, 1, NULL, NULL)); cl_git_pass(git_repository_index(&index, fixture.worktree)); for (i = 0; i < git_index_entrycount(index); i++) { cl_assert(entry = git_index_get_byindex(index, i)); if (git_index_entry_is_conflict(entry)) conflicts++; } cl_assert_equal_sz(conflicts, 3); git_reference_free(theirs_ref); git_annotated_commit_free(theirs); git_index_free(index); cl_git_pass(git_str_joinpath(&path, fixture.worktree->workdir, "branch_file.txt")); cl_git_pass(git_futils_readbuffer(&buf, path.ptr)); cl_assert_equal_s(buf.ptr, CONFLICT_BRANCH_FILE_TXT); git_str_dispose(&path); git_str_dispose(&buf); }
libgit2-main
tests/libgit2/worktree/merge.c
#include "clar_libgit2.h" #include "futils.h" #include "pack.h" #include "hash.h" #include "iterator.h" #include "vector.h" #include "posix.h" #include "hash.h" static git_repository *_repo; static git_revwalk *_revwalker; static git_packbuilder *_packbuilder; static git_indexer *_indexer; static git_vector _commits; static int _commits_is_initialized; static git_indexer_progress _stats; extern bool git_disable_pack_keep_file_checks; void test_pack_packbuilder__initialize(void) { _repo = cl_git_sandbox_init("testrepo.git"); cl_git_pass(p_chdir("testrepo.git")); cl_git_pass(git_revwalk_new(&_revwalker, _repo)); cl_git_pass(git_packbuilder_new(&_packbuilder, _repo)); cl_git_pass(git_vector_init(&_commits, 0, NULL)); _commits_is_initialized = 1; memset(&_stats, 0, sizeof(_stats)); p_fsync__cnt = 0; } void test_pack_packbuilder__cleanup(void) { git_oid *o; unsigned int i; cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0)); cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, false)); if (_commits_is_initialized) { _commits_is_initialized = 0; git_vector_foreach(&_commits, i, o) { git__free(o); } git_vector_free(&_commits); } git_packbuilder_free(_packbuilder); _packbuilder = NULL; git_revwalk_free(_revwalker); _revwalker = NULL; git_indexer_free(_indexer); _indexer = NULL; cl_git_pass(p_chdir("..")); cl_git_sandbox_cleanup(); _repo = NULL; } static void seed_packbuilder(void) { git_oid oid, *o; unsigned int i; git_revwalk_sorting(_revwalker, GIT_SORT_TIME); cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD")); while (git_revwalk_next(&oid, _revwalker) == 0) { o = git__malloc(sizeof(git_oid)); cl_assert(o != NULL); git_oid_cpy(o, &oid); cl_git_pass(git_vector_insert(&_commits, o)); } git_vector_foreach(&_commits, i, o) { cl_git_pass(git_packbuilder_insert(_packbuilder, o, NULL)); } git_vector_foreach(&_commits, i, o) { git_object *obj; cl_git_pass(git_object_lookup(&obj, _repo, o, GIT_OBJECT_COMMIT)); cl_git_pass(git_packbuilder_insert_tree(_packbuilder, git_commit_tree_id((git_commit *)obj))); git_object_free(obj); } } static int feed_indexer(void *ptr, size_t len, void *payload) { git_indexer_progress *stats = (git_indexer_progress *)payload; return git_indexer_append(_indexer, ptr, len, stats); } void test_pack_packbuilder__create_pack(void) { git_indexer_progress stats; git_str buf = GIT_STR_INIT, path = GIT_STR_INIT; git_hash_ctx ctx; unsigned char hash[GIT_HASH_SHA1_SIZE]; char hex[(GIT_HASH_SHA1_SIZE * 2) + 1]; seed_packbuilder(); cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL)); cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats)); cl_git_pass(git_indexer_commit(_indexer, &stats)); git_str_printf(&path, "pack-%s.pack", git_indexer_name(_indexer)); /* * By default, packfiles are created with only one thread. * Therefore we can predict the object ordering and make sure * we create exactly the same pack as git.git does when *not* * reusing existing deltas (as libgit2). * * $ cd tests/resources/testrepo.git * $ git rev-list --objects HEAD | \ * git pack-objects -q --no-reuse-delta --threads=1 pack * $ sha1sum pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack * 5d410bdf97cf896f9007681b92868471d636954b * */ cl_git_pass(git_futils_readbuffer(&buf, git_str_cstr(&path))); cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1)); cl_git_pass(git_hash_update(&ctx, buf.ptr, buf.size)); cl_git_pass(git_hash_final(hash, &ctx)); git_hash_ctx_cleanup(&ctx); git_str_dispose(&path); git_str_dispose(&buf); git_hash_fmt(hex, hash, GIT_HASH_SHA1_SIZE); cl_assert_equal_s(hex, "5d410bdf97cf896f9007681b92868471d636954b"); } void test_pack_packbuilder__get_name(void) { seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0, NULL, NULL)); cl_assert_equal_s("7f5fa362c664d68ba7221259be1cbd187434b2f0", git_packbuilder_name(_packbuilder)); } void test_pack_packbuilder__write_default_path(void) { seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, NULL, 0, NULL, NULL)); cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx")); cl_assert(git_fs_path_exists("objects/pack/pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack")); } static void test_write_pack_permission(mode_t given, mode_t expected) { struct stat statbuf; mode_t mask, os_mask; seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, ".", given, NULL, NULL)); /* Windows does not return group/user bits from stat, * files are never executable. */ #ifdef GIT_WIN32 os_mask = 0600; #else os_mask = 0777; #endif mask = p_umask(0); p_umask(mask); cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.idx", &statbuf)); cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask); cl_git_pass(p_stat("pack-7f5fa362c664d68ba7221259be1cbd187434b2f0.pack", &statbuf)); cl_assert_equal_i(statbuf.st_mode & os_mask, (expected & ~mask) & os_mask); } void test_pack_packbuilder__permissions_standard(void) { test_write_pack_permission(0, GIT_PACK_FILE_MODE); } void test_pack_packbuilder__permissions_readonly(void) { test_write_pack_permission(0444, 0444); } void test_pack_packbuilder__permissions_readwrite(void) { test_write_pack_permission(0666, 0666); } void test_pack_packbuilder__does_not_fsync_by_default(void) { seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); cl_assert_equal_sz(0, p_fsync__cnt); } /* We fsync the packfile and index. On non-Windows, we also fsync * the parent directories. */ #ifdef GIT_WIN32 static int expected_fsyncs = 2; #else static int expected_fsyncs = 4; #endif void test_pack_packbuilder__fsync_global_setting(void) { cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1)); p_fsync__cnt = 0; seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt); } void test_pack_packbuilder__fsync_repo_setting(void) { cl_repo_set_bool(_repo, "core.fsyncObjectFiles", true); p_fsync__cnt = 0; seed_packbuilder(); cl_git_pass(git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL)); cl_assert_equal_sz(expected_fsyncs, p_fsync__cnt); } static int foreach_cb(void *buf, size_t len, void *payload) { git_indexer *idx = (git_indexer *) payload; cl_git_pass(git_indexer_append(idx, buf, len, &_stats)); return 0; } void test_pack_packbuilder__foreach(void) { git_indexer *idx; seed_packbuilder(); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); cl_git_pass(git_indexer_commit(idx, &_stats)); git_indexer_free(idx); } static int foreach_cancel_cb(void *buf, size_t len, void *payload) { git_indexer *idx = (git_indexer *)payload; cl_git_pass(git_indexer_append(idx, buf, len, &_stats)); return (_stats.total_objects > 2) ? -1111 : 0; } void test_pack_packbuilder__foreach_with_cancel(void) { git_indexer *idx; seed_packbuilder(); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); cl_git_fail_with( git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111); git_indexer_free(idx); } void test_pack_packbuilder__keep_file_check(void) { assert(!git_disable_pack_keep_file_checks); cl_git_pass(git_libgit2_opts(GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, true)); assert(git_disable_pack_keep_file_checks); }
libgit2-main
tests/libgit2/pack/packbuilder.c
#include "clar_libgit2.h" #include <git2.h> #include "futils.h" #include "hash.h" #include "iterator.h" #include "vector.h" #include "posix.h" /* * This is a packfile with three objects. The second is a delta which * depends on the third, which is also a delta. */ static const unsigned char out_of_order_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76, 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x75, 0x01, 0xd7, 0x71, 0x36, 0x66, 0xf4, 0xde, 0x82, 0x27, 0x76, 0xc7, 0x62, 0x2c, 0x10, 0xf1, 0xb0, 0x7d, 0xe2, 0x80, 0xdc, 0x78, 0x9c, 0x63, 0x62, 0x62, 0x62, 0xb7, 0x03, 0x00, 0x00, 0x69, 0x00, 0x4c, 0xde, 0x7d, 0xaa, 0xe4, 0x19, 0x87, 0x58, 0x80, 0x61, 0x09, 0x9a, 0x33, 0xca, 0x7a, 0x31, 0x92, 0x6f, 0xae, 0x66, 0x75 }; static const unsigned int out_of_order_pack_len = 112; /* * Packfile with two objects. The second is a delta against an object * which is not in the packfile */ static const unsigned char thin_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x32, 0x78, 0x9c, 0x63, 0x67, 0x00, 0x00, 0x00, 0x10, 0x00, 0x08, 0x76, 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x42, 0x52, 0x3a, 0x6f, 0x39, 0xd1, 0xfe, 0x66, 0x68, 0x6b, 0xa5, 0xe5, 0xe2, 0x97, 0xac, 0x94, 0x6c, 0x76, 0x0b, 0x04 }; static const unsigned int thin_pack_len = 78; /* * Packfile with one object. It references an object which is not in the * packfile and has a corrupt length (states the deltified stream is 1 byte * long, where it is actually 6). */ static const unsigned char corrupt_thin_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x71, 0xe6, 0x8f, 0xe8, 0x12, 0x9b, 0x54, 0x6b, 0x10, 0x1a, 0xee, 0x95, 0x10, 0xc5, 0x32, 0x8e, 0x7f, 0x21, 0xca, 0x1d, 0x18, 0x78, 0x9c, 0x63, 0x62, 0x66, 0x4e, 0xcb, 0xcf, 0x07, 0x00, 0x02, 0xac, 0x01, 0x4d, 0x07, 0x67, 0x03, 0xc5, 0x40, 0x99, 0x49, 0xb1, 0x3b, 0x7d, 0xae, 0x9b, 0x0e, 0xdd, 0xde, 0xc6, 0x76, 0x43, 0x24, 0x64 }; static const unsigned int corrupt_thin_pack_len = 67; /* * Packfile with a missing trailer. */ static const unsigned char missing_trailer_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x50, 0xf4, 0x3b, }; static const unsigned int missing_trailer_pack_len = 12; /* * Packfile that causes the packfile stream to open in a way in which it leaks * the stream reader. */ static const unsigned char leaky_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0xf4, 0xbd, 0x51, 0x51, 0x51, 0x51, 0x51, 0x72, 0x65, 0x41, 0x4b, 0x63, 0x5f, 0x64, 0x65, 0x70, 0x74, 0x68, 0xbd, 0x41, 0x4b }; static const unsigned int leaky_pack_len = 33; /* * Packfile with a three objects. The first one is a tree referencing two blobs, * the second object is one of those blobs. The second blob is missing. */ unsigned char incomplete_pack[] = { 0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0xae, 0x03, 0x78, 0x9c, 0x33, 0x34, 0x30, 0x30, 0x33, 0x31, 0x51, 0x48, 0x4a, 0x2c, 0x62, 0x08, 0x17, 0x3b, 0x15, 0xd9, 0x7e, 0xfa, 0x67, 0x6d, 0xf6, 0x56, 0x4f, 0x85, 0x7d, 0xcb, 0xd6, 0xde, 0x53, 0xd1, 0x6d, 0x7f, 0x66, 0x08, 0x91, 0x4e, 0xcb, 0xcf, 0x67, 0x50, 0xad, 0x39, 0x9a, 0xa2, 0xb3, 0x71, 0x41, 0xc8, 0x87, 0x9e, 0x13, 0xf6, 0xba, 0x53, 0xec, 0xc2, 0xfe, 0xda, 0xed, 0x9b, 0x09, 0x00, 0xe8, 0xc8, 0x19, 0xab, 0x34, 0x78, 0x9c, 0x4b, 0x4a, 0x2c, 0xe2, 0x02, 0x00, 0x03, 0x9d, 0x01, 0x40, 0x4b, 0x72, 0xa2, 0x6f, 0xb6, 0x88, 0x2d, 0x6c, 0xa5, 0x07, 0xb2, 0xa5, 0x45, 0xe8, 0xdb, 0xe6, 0x53, 0xb3, 0x52, 0xe2 }; unsigned int incomplete_pack_len = 115; static const unsigned char base_obj[] = { 07, 076 }; static const unsigned int base_obj_len = 2; void test_pack_indexer__out_of_order(void) { git_indexer *idx = 0; git_indexer_progress stats = { 0 }; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); cl_git_pass(git_indexer_append( idx, out_of_order_pack, out_of_order_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 3); cl_assert_equal_i(stats.received_objects, 3); cl_assert_equal_i(stats.indexed_objects, 3); git_indexer_free(idx); } void test_pack_indexer__missing_trailer(void) { git_indexer *idx = 0; git_indexer_progress stats = { 0 }; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); cl_git_pass(git_indexer_append( idx, missing_trailer_pack, missing_trailer_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert(git_error_last() != NULL); cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_INDEXER); git_indexer_free(idx); } void test_pack_indexer__leaky(void) { git_indexer *idx = 0; git_indexer_progress stats = { 0 }; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); cl_git_pass(git_indexer_append( idx, leaky_pack, leaky_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert(git_error_last() != NULL); cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_INDEXER); git_indexer_free(idx); } void test_pack_indexer__fix_thin(void) { git_indexer *idx = NULL; git_indexer_progress stats = { 0 }; git_repository *repo; git_odb *odb; git_oid id, should_id; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); /* Store the missing base into your ODB so the indexer can fix the pack */ cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB)); git_oid__fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18", GIT_OID_SHA1); cl_assert_equal_oid(&should_id, &id); cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 2); cl_assert_equal_i(stats.received_objects, 2); cl_assert_equal_i(stats.indexed_objects, 2); cl_assert_equal_i(stats.local_objects, 1); cl_assert_equal_s("fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13", git_indexer_name(idx)); git_indexer_free(idx); git_odb_free(odb); git_repository_free(repo); /* * The pack's name/hash only tells us what objects there are, * so we need to go through the packfile again in order to * figure out whether we calculated the trailer correctly. */ { unsigned char buffer[128]; int fd; ssize_t read; struct stat st; const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack"; fd = p_open(name, O_RDONLY); cl_assert(fd != -1); cl_git_pass(p_stat(name, &st)); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); read = p_read(fd, buffer, sizeof(buffer)); cl_assert(read != -1); p_close(fd); cl_git_pass(git_indexer_append(idx, buffer, read, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 3); cl_assert_equal_i(stats.received_objects, 3); cl_assert_equal_i(stats.indexed_objects, 3); cl_assert_equal_i(stats.local_objects, 0); git_indexer_free(idx); } } void test_pack_indexer__corrupt_length(void) { git_indexer *idx = NULL; git_indexer_progress stats = { 0 }; git_repository *repo; git_odb *odb; git_oid id, should_id; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); /* Store the missing base into your ODB so the indexer can fix the pack */ cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB)); git_oid__fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18", GIT_OID_SHA1); cl_assert_equal_oid(&should_id, &id); cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); cl_git_pass(git_indexer_append( idx, corrupt_thin_pack, corrupt_thin_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert(git_error_last() != NULL); cl_assert_equal_i(git_error_last()->klass, GIT_ERROR_ZLIB); git_indexer_free(idx); git_odb_free(odb); git_repository_free(repo); } void test_pack_indexer__incomplete_pack_fails_with_strict(void) { git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; git_indexer *idx = 0; git_indexer_progress stats = { 0 }; opts.verify = 1; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); cl_git_pass(git_indexer_append( idx, incomplete_pack, incomplete_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 2); cl_assert_equal_i(stats.received_objects, 2); cl_assert_equal_i(stats.indexed_objects, 2); git_indexer_free(idx); } void test_pack_indexer__out_of_order_with_connectivity_checks(void) { git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; git_indexer *idx = 0; git_indexer_progress stats = { 0 }; opts.verify = 1; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); cl_git_pass(git_indexer_append( idx, out_of_order_pack, out_of_order_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 3); cl_assert_equal_i(stats.received_objects, 3); cl_assert_equal_i(stats.indexed_objects, 3); git_indexer_free(idx); } static int find_tmp_file_recurs(void *opaque, git_str *path) { int error = 0; git_str *first_tmp_file = opaque; struct stat st; if ((error = p_lstat_posixly(path->ptr, &st)) < 0) return error; if (S_ISDIR(st.st_mode)) return git_fs_path_direach(path, 0, find_tmp_file_recurs, opaque); /* This is the template that's used in git_futils_mktmp. */ if (strstr(git_str_cstr(path), "_git2_") != NULL) return git_str_sets(first_tmp_file, git_str_cstr(path)); return 0; } void test_pack_indexer__no_tmp_files(void) { git_indexer *idx = NULL; git_str path = GIT_STR_INIT; git_str first_tmp_file = GIT_STR_INIT; /* Precondition: there are no temporary files. */ cl_git_pass(git_str_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_str_dispose(&path); cl_assert(git_str_len(&first_tmp_file) == 0); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); git_indexer_free(idx); cl_git_pass(git_str_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_str_dispose(&path); cl_assert(git_str_len(&first_tmp_file) == 0); git_str_dispose(&first_tmp_file); }
libgit2-main
tests/libgit2/pack/indexer.c
#include "clar_libgit2.h" #include <git2.h> #include "strmap.h" #include "mwindow.h" #include "pack.h" extern git_strmap *git__pack_cache; void test_pack_sharing__open_two_repos(void) { git_repository *repo1, *repo2; git_object *obj1, *obj2; git_oid id; size_t pos; void *data; int error; cl_git_pass(git_repository_open(&repo1, cl_fixture("testrepo.git"))); cl_git_pass(git_repository_open(&repo2, cl_fixture("testrepo.git"))); git_oid__fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1); cl_git_pass(git_object_lookup(&obj1, repo1, &id, GIT_OBJECT_ANY)); cl_git_pass(git_object_lookup(&obj2, repo2, &id, GIT_OBJECT_ANY)); pos = 0; while ((error = git_strmap_iterate(&data, git__pack_cache, &pos, NULL)) == 0) { struct git_pack_file *pack = (struct git_pack_file *) data; cl_assert_equal_i(2, pack->refcount.val); } cl_assert_equal_i(3, git_strmap_size(git__pack_cache)); git_object_free(obj1); git_object_free(obj2); git_repository_free(repo1); git_repository_free(repo2); /* we don't want to keep the packs open after the repos go away */ cl_assert_equal_i(0, git_strmap_size(git__pack_cache)); }
libgit2-main
tests/libgit2/pack/sharing.c
#include "clar_libgit2.h" #include "pool.h" #include <git2.h> #include "git2/sys/commit.h" #include "git2/sys/mempack.h" static size_t original_mwindow_file_limit = 0; void test_pack_threadsafety__initialize(void) { size_t open_mwindow_files = 1; cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, open_mwindow_files)); } void test_pack_threadsafety__cleanup(void) { cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, original_mwindow_file_limit)); } #ifdef GIT_THREADS static void *get_status(void *arg) { const char *repo_path = (const char *)arg; git_repository *repo; git_status_list *status; cl_git_pass(git_repository_open(&repo, repo_path)); cl_git_pass(git_status_list_new(&status, repo, NULL)); git_status_list_free(status); git_repository_free(repo); return NULL; } #endif void test_pack_threadsafety__open_repo_in_multiple_threads(void) { #ifdef GIT_THREADS const char *repo_path = cl_fixture("../.."); git_repository *repo; git_thread threads[8]; size_t i; /* If we can't open the libgit2 repo or if it isn't a full repo * with proper history, just skip this test */ if (git_repository_open(&repo, repo_path) < 0) cl_skip(); if (git_repository_is_shallow(repo)) cl_skip(); git_repository_free(repo); for (i = 0; i < ARRAY_SIZE(threads); i++) git_thread_create(&threads[i], get_status, (void *)repo_path); for (i = 0; i < ARRAY_SIZE(threads); i++) git_thread_join(&threads[i], NULL); #else cl_skip(); #endif }
libgit2-main
tests/libgit2/pack/threadsafety.c
#include "clar_libgit2.h" #include "mwindow.h" #include <git2.h> #include "git2/sys/commit.h" #include "git2/sys/mempack.h" static size_t expected_open_mwindow_files = 0; static size_t original_mwindow_file_limit = 0; extern git_mutex git__mwindow_mutex; extern git_mwindow_ctl git_mwindow__mem_ctl; void test_pack_filelimit__initialize_tiny(void) { expected_open_mwindow_files = 1; cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, expected_open_mwindow_files)); } void test_pack_filelimit__initialize_medium(void) { expected_open_mwindow_files = 10; cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, expected_open_mwindow_files)); } void test_pack_filelimit__initialize_unlimited(void) { expected_open_mwindow_files = 15; cl_git_pass(git_libgit2_opts(GIT_OPT_GET_MWINDOW_FILE_LIMIT, &original_mwindow_file_limit)); cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, 0)); } void test_pack_filelimit__cleanup(void) { git_str path = GIT_STR_INIT; cl_git_pass(git_libgit2_opts(GIT_OPT_SET_MWINDOW_FILE_LIMIT, original_mwindow_file_limit)); cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "repo.git")); cl_fixture_cleanup(path.ptr); git_str_dispose(&path); } /* * Create a packfile with one commit, one tree, and two blobs. The first blob * (README.md) has the same content in all commits, but the second one * (file.txt) has a different content in each commit. */ static void create_packfile_commit( git_repository *repo, git_oid *out_commit_id, git_oid *parent_id, size_t commit_index, size_t commit_count) { git_str file_contents = GIT_STR_INIT; git_treebuilder *treebuilder; git_packbuilder *packbuilder; git_signature *s; git_oid oid, tree_id, commit_id; const git_oid *parents[] = { parent_id }; size_t parent_count = parent_id ? 1 : 0; cl_git_pass(git_treebuilder_new(&treebuilder, repo, NULL)); cl_git_pass(git_blob_create_from_buffer(&oid, repo, "", 0)); cl_git_pass(git_treebuilder_insert(NULL, treebuilder, "README.md", &oid, 0100644)); cl_git_pass(git_str_printf(&file_contents, "Commit %zd/%zd", commit_index, commit_count)); cl_git_pass(git_blob_create_from_buffer(&oid, repo, file_contents.ptr, file_contents.size)); cl_git_pass(git_treebuilder_insert(NULL, treebuilder, "file.txt", &oid, 0100644)); cl_git_pass(git_treebuilder_write(&tree_id, treebuilder)); cl_git_pass(git_signature_now(&s, "alice", "[email protected]")); cl_git_pass(git_commit_create_from_ids(&commit_id, repo, "refs/heads/master", s, s, NULL, file_contents.ptr, &tree_id, parent_count, parents)); cl_git_pass(git_packbuilder_new(&packbuilder, repo)); cl_git_pass(git_packbuilder_insert_commit(packbuilder, &commit_id)); cl_git_pass(git_packbuilder_write(packbuilder, NULL, 0, NULL, NULL)); cl_git_pass(git_oid_cpy(out_commit_id, &commit_id)); git_str_dispose(&file_contents); git_treebuilder_free(treebuilder); git_packbuilder_free(packbuilder); git_signature_free(s); } void test_pack_filelimit__open_repo_with_multiple_packfiles(void) { git_str path = GIT_STR_INIT; git_mwindow_ctl *ctl = &git_mwindow__mem_ctl; git_repository *repo; git_revwalk *walk; git_oid id, *parent_id = NULL; size_t i; const size_t commit_count = 16; unsigned int open_windows; /* * Create a repository and populate it with 16 commits, each in its own * packfile. */ cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "repo.git")); cl_git_pass(git_repository_init(&repo, path.ptr, true)); for (i = 0; i < commit_count; ++i) { create_packfile_commit(repo, &id, parent_id, i + 1, commit_count); parent_id = &id; } cl_git_pass(git_revwalk_new(&walk, repo)); cl_git_pass(git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL)); cl_git_pass(git_revwalk_push_ref(walk, "refs/heads/master")); /* Walking the repository requires eventually opening each of the packfiles. */ i = 0; while (git_revwalk_next(&id, walk) == 0) ++i; cl_assert_equal_i(commit_count, i); cl_git_pass(git_mutex_lock(&git__mwindow_mutex)); /* * Adding an assert while holding a lock will cause the whole process to * deadlock. Copy the value and do the assert after releasing the lock. */ open_windows = ctl->open_windows; cl_git_pass(git_mutex_unlock(&git__mwindow_mutex)); cl_assert_equal_i(expected_open_mwindow_files, open_windows); git_str_dispose(&path); git_revwalk_free(walk); git_repository_free(repo); }
libgit2-main
tests/libgit2/pack/filelimit.c
#include "clar_libgit2.h" #include <git2.h> #include <git2/sys/midx.h> #include "futils.h" #include "midx.h" void test_pack_midx__parse(void) { git_repository *repo; struct git_midx_file *idx; struct git_midx_entry e; git_oid id; git_str midx_path = GIT_STR_INIT; cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index")); cl_git_pass(git_midx_open(&idx, git_str_cstr(&midx_path))); cl_assert_equal_i(git_midx_needs_refresh(idx, git_str_cstr(&midx_path)), 0); cl_git_pass(git_oid__fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5", GIT_OID_SHA1)); cl_git_pass(git_midx_entry_find(&e, idx, &id, GIT_OID_SHA1_HEXSIZE)); cl_assert_equal_oid(&e.sha1, &id); cl_assert_equal_s( (const char *)git_vector_get(&idx->packfile_names, e.pack_index), "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx"); git_midx_free(idx); git_repository_free(repo); git_str_dispose(&midx_path); } void test_pack_midx__lookup(void) { git_repository *repo; git_commit *commit; git_oid id; cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_oid__fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5", GIT_OID_SHA1)); cl_git_pass(git_commit_lookup_prefix(&commit, repo, &id, GIT_OID_SHA1_HEXSIZE)); cl_assert_equal_s(git_commit_message(commit), "packed commit one\n"); git_commit_free(commit); git_repository_free(repo); } void test_pack_midx__writer(void) { git_repository *repo; git_midx_writer *w = NULL; git_buf midx = GIT_BUF_INIT; git_str expected_midx = GIT_STR_INIT, path = GIT_STR_INIT; cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack")); cl_git_pass(git_midx_writer_new(&w, git_str_cstr(&path))); cl_git_pass(git_midx_writer_add(w, "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx")); cl_git_pass(git_midx_writer_add(w, "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.idx")); cl_git_pass(git_midx_writer_add(w, "pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")); cl_git_pass(git_midx_writer_dump(&midx, w)); cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack/multi-pack-index")); cl_git_pass(git_futils_readbuffer(&expected_midx, git_str_cstr(&path))); cl_assert_equal_i(midx.size, git_str_len(&expected_midx)); cl_assert_equal_strn(midx.ptr, git_str_cstr(&expected_midx), midx.size); git_buf_dispose(&midx); git_str_dispose(&expected_midx); git_str_dispose(&path); git_midx_writer_free(w); git_repository_free(repo); } void test_pack_midx__odb_create(void) { git_repository *repo; git_odb *odb; git_clone_options opts = GIT_CLONE_OPTIONS_INIT; git_str midx = GIT_STR_INIT, expected_midx = GIT_STR_INIT, midx_path = GIT_STR_INIT; struct stat st; opts.bare = true; opts.local = GIT_CLONE_LOCAL; cl_git_pass(git_clone(&repo, cl_fixture("testrepo/.gitted"), "./clone.git", &opts)); cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index")); cl_git_fail(p_stat(git_str_cstr(&midx_path), &st)); cl_git_pass(git_repository_odb(&odb, repo)); cl_git_pass(git_odb_write_multi_pack_index(odb)); git_odb_free(odb); cl_git_pass(p_stat(git_str_cstr(&midx_path), &st)); cl_git_pass(git_futils_readbuffer(&expected_midx, cl_fixture("testrepo.git/objects/pack/multi-pack-index"))); cl_git_pass(git_futils_readbuffer(&midx, git_str_cstr(&midx_path))); cl_assert_equal_i(git_str_len(&midx), git_str_len(&expected_midx)); cl_assert_equal_strn(git_str_cstr(&midx), git_str_cstr(&expected_midx), git_str_len(&midx)); git_repository_free(repo); git_str_dispose(&midx); git_str_dispose(&midx_path); git_str_dispose(&expected_midx); cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES)); }
libgit2-main
tests/libgit2/pack/midx.c
#include "clar_libgit2.h" static void assert_trailers(const char *message, git_message_trailer *trailers) { git_message_trailer_array arr; size_t i; int rc = git_message_trailers(&arr, message); cl_assert_equal_i(0, rc); for(i=0; i<arr.count; i++) { cl_assert_equal_s(arr.trailers[i].key, trailers[i].key); cl_assert_equal_s(arr.trailers[i].value, trailers[i].value); } cl_assert_equal_i(0, rc); git_message_trailer_array_free(&arr); } void test_message_trailer__simple(void) { git_message_trailer trailers[] = { {"Signed-off-by", "[email protected]"}, {"Signed-off-by", "[email protected]"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Signed-off-by: [email protected]\n" "Signed-off-by: [email protected]\n" , trailers); } void test_message_trailer__no_whitespace(void) { git_message_trailer trailers[] = { {"Key", "value"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key:value\n" , trailers); } void test_message_trailer__extra_whitespace(void) { git_message_trailer trailers[] = { {"Key", "value"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key : value\n" , trailers); } void test_message_trailer__no_newline(void) { git_message_trailer trailers[] = { {"Key", "value"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key: value" , trailers); } void test_message_trailer__not_last_paragraph(void) { git_message_trailer trailers[] = { {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key: value\n" "\n" "More stuff\n" , trailers); } void test_message_trailer__conflicts(void) { git_message_trailer trailers[] = { {"Key", "value"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key: value\n" "\n" "Conflicts:\n" "\tfoo.c\n" , trailers); } void test_message_trailer__patch(void) { git_message_trailer trailers[] = { {"Key", "value"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Key: value\n" "\n" "---\n" "More: stuff\n" , trailers); } void test_message_trailer__continuation(void) { git_message_trailer trailers[] = { {"A", "b\n c"}, {"D", "e\n f: g h"}, {"I", "j"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "A: b\n" " c\n" "D: e\n" " f: g h\n" "I: j\n" , trailers); } void test_message_trailer__invalid(void) { git_message_trailer trailers[] = { {"Signed-off-by", "[email protected]"}, {"Another", "trailer"}, {NULL, NULL}, }; assert_trailers( "Message\n" "\n" "Signed-off-by: [email protected]\n" "Not a trailer\n" "Another: trailer\n" , trailers); }
libgit2-main
tests/libgit2/message/trailer.c
#include "clar_libgit2.h" #include "userdiff.h" static git_regexp regex; void test_diff_userdiff__cleanup(void) { git_regexp_dispose(&regex); } void test_diff_userdiff__compile_userdiff_regexps(void) { size_t idx; for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) { git_diff_driver_definition ddef = builtin_defs[idx]; cl_git_pass(git_regexp_compile(&regex, ddef.fns, ddef.flags)); git_regexp_dispose(&regex); cl_git_pass(git_regexp_compile(&regex, ddef.words, 0)); git_regexp_dispose(&regex); } }
libgit2-main
tests/libgit2/diff/userdiff.c
#include "clar_libgit2.h" #include "diff_helpers.h" #include "repository.h" #include "diff_driver.h" static git_repository *g_repo = NULL; void test_diff_drivers__initialize(void) { } void test_diff_drivers__cleanup(void) { cl_git_sandbox_cleanup(); g_repo = NULL; } static void overwrite_filemode(const char *expected, git_buf *actual) { size_t offset; char *found; found = strstr(expected, "100644"); if (!found) return; offset = ((const char *)found) - expected; if (actual->size < offset + 6) return; if (memcmp(&actual->ptr[offset], "100644", 6) != 0) memcpy(&actual->ptr[offset], "100644", 6); } void test_diff_drivers__patterns(void) { git_config *cfg; const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"; git_tree *one; git_diff *diff; git_patch *patch; git_buf actual = GIT_BUF_INIT; const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n"; const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; g_repo = cl_git_sandbox_init("renames"); one = resolve_commit_oid_to_tree(g_repo, one_sha); /* no diff */ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(0, (int)git_diff_num_deltas(diff)); git_diff_free(diff); /* default diff */ cl_git_append2file("renames/untimely.txt", "\r\nSome new stuff\r\n"); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected0, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); /* attribute diff set to false */ cl_git_rewritefile("renames/.gitattributes", "untimely.txt -diff\n"); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected1, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); /* attribute diff set to unconfigured value (should use default) */ cl_git_rewritefile("renames/.gitattributes", "untimely.txt diff=kipling0\n"); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected0, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); /* let's define that driver */ cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 1)); git_config_free(cfg); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected1, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); /* let's use a real driver with some regular expressions */ git_diff_driver_registry_free(g_repo->diff_drivers); g_repo->diff_drivers = NULL; cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0)); cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H.*$")); git_config_free(cfg); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected2, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); git_tree_free(one); } void test_diff_drivers__long_lines(void) { const char *base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non nisi ligula. Ut viverra enim sed lobortis suscipit.\nPhasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissim risus. Suspendisse at nisi quis turpis fringilla rutrum id sit amet nulla.\nNam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\nMauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\nAliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n"; git_index *idx; git_diff *diff; git_patch *patch; git_buf actual = GIT_BUF_INIT; const char *expected = "diff --git a/longlines.txt b/longlines.txt\nindex c1ce6ef..0134431 100644\n--- a/longlines.txt\n+++ b/longlines.txt\n@@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi\n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n+newline\n+newline\n"; g_repo = cl_git_sandbox_init("empty_standard_repo"); cl_git_mkfile("empty_standard_repo/longlines.txt", base); cl_git_pass(git_repository_index(&idx, g_repo)); cl_git_pass(git_index_add_bypath(idx, "longlines.txt")); cl_git_pass(git_index_write(idx)); git_index_free(idx); cl_git_append2file("empty_standard_repo/longlines.txt", "newline\nnewline\n"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); cl_assert_equal_sz(1, git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); /* if chmod not supported, overwrite mode bits since anything is possible */ overwrite_filemode(expected, &actual); cl_assert_equal_s(expected, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); } void test_diff_drivers__builtins(void) { git_diff *diff; git_patch *patch; git_str file = GIT_STR_INIT, expected = GIT_STR_INIT; git_buf actual = GIT_BUF_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_vector files = GIT_VECTOR_INIT; size_t i; char *path, *extension; g_repo = cl_git_sandbox_init("userdiff"); cl_git_pass(git_fs_path_dirload(&files, "userdiff/files", 9, 0)); opts.interhunk_lines = 1; opts.context_lines = 1; opts.pathspec.count = 1; git_vector_foreach(&files, i, path) { if (git__prefixcmp(path, "files/file.")) continue; extension = path + strlen("files/file."); opts.pathspec.strings = &path; /* do diff with no special driver */ cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_sz(1, git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); git_str_sets(&expected, "userdiff/expected/nodriver/diff."); git_str_puts(&expected, extension); cl_git_pass(git_futils_readbuffer(&expected, expected.ptr)); overwrite_filemode(expected.ptr, &actual); cl_assert_equal_s(expected.ptr, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); /* do diff with driver */ { FILE *fp = fopen("userdiff/.gitattributes", "w"); fprintf(fp, "*.%s diff=%s\n", extension, extension); fclose(fp); } cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_sz(1, git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); git_str_sets(&expected, "userdiff/expected/driver/diff."); git_str_puts(&expected, extension); cl_git_pass(git_futils_readbuffer(&expected, expected.ptr)); overwrite_filemode(expected.ptr, &actual); cl_assert_equal_s(expected.ptr, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); git__free(path); } git_buf_dispose(&actual); git_str_dispose(&file); git_str_dispose(&expected); git_vector_free(&files); } void test_diff_drivers__invalid_pattern(void) { git_config *cfg; git_index *idx; git_diff *diff; git_patch *patch; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; g_repo = cl_git_sandbox_init("userdiff"); cl_git_mkfile("userdiff/.gitattributes", "*.storyboard diff=storyboard\n"); cl_git_pass(git_repository_config__weakptr(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "diff.storyboard.xfuncname", "<!--(.*?)-->")); cl_git_mkfile("userdiff/dummy.storyboard", ""); cl_git_pass(git_repository_index__weakptr(&idx, g_repo)); cl_git_pass(git_index_add_bypath(idx, "dummy.storyboard")); cl_git_mkfile("userdiff/dummy.storyboard", "some content\n"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); git_patch_free(patch); git_diff_free(diff); }
libgit2-main
tests/libgit2/diff/drivers.c
#include "clar_libgit2.h" #include "git2/sys/repository.h" #include "diff_helpers.h" #include "diff.h" #include "repository.h" static git_repository *g_repo = NULL; void test_diff_patch__initialize(void) { } void test_diff_patch__cleanup(void) { cl_git_sandbox_cleanup(); } #define EXPECTED_HEADER "diff --git a/subdir.txt b/subdir.txt\n" \ "deleted file mode 100644\n" \ "index e8ee89e..0000000\n" \ "--- a/subdir.txt\n" \ "+++ /dev/null\n" #define EXPECTED_HUNK "@@ -1,2 +0,0 @@\n" #define UTF8_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\n" #define UTF8_TRUNCATED_A_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\n" #define UTF8_TRUNCATED_L_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\n" static int check_removal_cb( const git_diff_delta *delta, const git_diff_hunk *hunk, const git_diff_line *line, void *payload) { switch (line->origin) { case GIT_DIFF_LINE_FILE_HDR: cl_assert_equal_s(EXPECTED_HEADER, line->content); cl_assert(hunk == NULL); goto check_delta; case GIT_DIFF_LINE_HUNK_HDR: cl_assert_equal_s(EXPECTED_HUNK, line->content); goto check_hunk; case GIT_DIFF_LINE_CONTEXT: case GIT_DIFF_LINE_DELETION: if (payload != NULL) return *(int *)payload; goto check_hunk; default: /* unexpected code path */ return -1; } check_hunk: cl_assert(hunk != NULL); cl_assert_equal_i(1, hunk->old_start); cl_assert_equal_i(2, hunk->old_lines); cl_assert_equal_i(0, hunk->new_start); cl_assert_equal_i(0, hunk->new_lines); check_delta: cl_assert_equal_s("subdir.txt", delta->old_file.path); cl_assert_equal_s("subdir.txt", delta->new_file.path); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); return 0; } void test_diff_patch__can_properly_display_the_removal_of_a_file(void) { /* * $ git diff 26a125e..735b6a2 * diff --git a/subdir.txt b/subdir.txt * deleted file mode 100644 * index e8ee89e..0000000 * --- a/subdir.txt * +++ /dev/null * @@ -1,2 +0,0 @@ * -Is it a bird? * -Is it a plane? */ const char *one_sha = "26a125e"; const char *another_sha = "735b6a2"; git_tree *one, *another; git_diff *diff; g_repo = cl_git_sandbox_init("status"); one = resolve_commit_oid_to_tree(g_repo, one_sha); another = resolve_commit_oid_to_tree(g_repo, another_sha); cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); cl_git_pass(git_diff_print( diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, NULL)); git_diff_free(diff); git_tree_free(another); git_tree_free(one); } void test_diff_patch__can_cancel_diff_print(void) { const char *one_sha = "26a125e"; const char *another_sha = "735b6a2"; git_tree *one, *another; git_diff *diff; int fail_with; g_repo = cl_git_sandbox_init("status"); one = resolve_commit_oid_to_tree(g_repo, one_sha); another = resolve_commit_oid_to_tree(g_repo, another_sha); cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); fail_with = -2323; cl_git_fail_with(git_diff_print( diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with), fail_with); fail_with = 45; cl_git_fail_with(git_diff_print( diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with), fail_with); git_diff_free(diff); git_tree_free(another); git_tree_free(one); } void test_diff_patch__to_string(void) { const char *one_sha = "26a125e"; const char *another_sha = "735b6a2"; git_tree *one, *another; git_diff *diff; git_patch *patch; git_buf buf = GIT_BUF_INIT; const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n"; g_repo = cl_git_sandbox_init("status"); one = resolve_commit_oid_to_tree(g_repo, one_sha); another = resolve_commit_oid_to_tree(g_repo, another_sha); cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected, buf.ptr); cl_assert_equal_sz(31, git_patch_size(patch, 0, 0, 0)); cl_assert_equal_sz(31, git_patch_size(patch, 1, 0, 0)); cl_assert_equal_sz(31 + 16, git_patch_size(patch, 1, 1, 0)); cl_assert_equal_sz(strlen(expected), git_patch_size(patch, 1, 1, 1)); git_buf_dispose(&buf); git_patch_free(patch); git_diff_free(diff); git_tree_free(another); git_tree_free(one); } void test_diff_patch__config_options(void) { const char *one_sha = "26a125e"; /* current HEAD */ git_tree *one; git_config *cfg; git_diff *diff; git_patch *patch; git_buf buf = GIT_BUF_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; char *onefile = "staged_changes_modified_file"; const char *expected1 = "diff --git c/staged_changes_modified_file i/staged_changes_modified_file\nindex 70bd944..906ee77 100644\n--- c/staged_changes_modified_file\n+++ i/staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n"; const char *expected2 = "diff --git i/staged_changes_modified_file w/staged_changes_modified_file\nindex 906ee77..011c344 100644\n--- i/staged_changes_modified_file\n+++ w/staged_changes_modified_file\n@@ -1,2 +1,3 @@\n staged_changes_modified_file\n staged_changes_modified_file\n+staged_changes_modified_file\n"; const char *expected3 = "diff --git staged_changes_modified_file staged_changes_modified_file\nindex 906ee77..011c344 100644\n--- staged_changes_modified_file\n+++ staged_changes_modified_file\n@@ -1,2 +1,3 @@\n staged_changes_modified_file\n staged_changes_modified_file\n+staged_changes_modified_file\n"; const char *expected4 = "diff --git staged_changes_modified_file staged_changes_modified_file\nindex 70bd9443ada0..906ee7711f4f 100644\n--- staged_changes_modified_file\n+++ staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n"; g_repo = cl_git_sandbox_init("status"); cl_git_pass(git_repository_config(&cfg, g_repo)); one = resolve_commit_oid_to_tree(g_repo, one_sha); opts.pathspec.count = 1; opts.pathspec.strings = &onefile; cl_git_pass(git_config_set_string(cfg, "diff.mnemonicprefix", "true")); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected1, buf.ptr); git_buf_dispose(&buf); git_patch_free(patch); git_diff_free(diff); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected2, buf.ptr); git_buf_dispose(&buf); git_patch_free(patch); git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.noprefix", "true")); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected3, buf.ptr); git_buf_dispose(&buf); git_patch_free(patch); git_diff_free(diff); cl_git_pass(git_config_set_int32(cfg, "core.abbrev", 12)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected4, buf.ptr); git_buf_dispose(&buf); git_patch_free(patch); git_diff_free(diff); git_buf_dispose(&buf); git_tree_free(one); git_config_free(cfg); } void test_diff_patch__hunks_have_correct_line_numbers(void) { git_config *cfg; git_tree *head; git_diff_options opt = GIT_DIFF_OPTIONS_INIT; git_diff *diff; git_patch *patch; const git_diff_delta *delta; const git_diff_hunk *hunk; const git_diff_line *line; size_t hunklen; git_str old_content = GIT_STR_INIT, actual = GIT_STR_INIT; const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n"; g_repo = cl_git_sandbox_init("renames"); cl_git_pass(git_config_new(&cfg)); git_repository_set_config(g_repo, cfg); git_config_free(cfg); git_repository_reinit_filesystem(g_repo, false); cl_git_pass( git_futils_readbuffer(&old_content, "renames/songof7cities.txt")); cl_git_rewritefile("renames/songof7cities.txt", new_content); cl_git_pass(git_repository_head_tree(&head, g_repo)); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, &opt)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i(2, (int)git_patch_num_hunks(patch)); /* check hunk 0 */ cl_git_pass( git_patch_get_hunk(&hunk, &hunklen, patch, 0)); cl_assert_equal_i(18, (int)hunklen); cl_assert_equal_i(6, (int)hunk->old_start); cl_assert_equal_i(15, (int)hunk->old_lines); cl_assert_equal_i(6, (int)hunk->new_start); cl_assert_equal_i(9, (int)hunk->new_lines); cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 0)); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 0)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("Ivory their outposts were--the guardrooms of them gilded,\n", actual.ptr); cl_assert_equal_i(6, line->old_lineno); cl_assert_equal_i(6, line->new_lineno); cl_assert_equal_i(-1, line->content_offset); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("All the world went softly when it walked before my Cities--\n", actual.ptr); cl_assert_equal_i(9, line->old_lineno); cl_assert_equal_i(-1, line->new_lineno); cl_assert_equal_i(252, line->content_offset); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 12)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("This is some new text;\n", actual.ptr); cl_assert_equal_i(-1, line->old_lineno); cl_assert_equal_i(9, line->new_lineno); cl_assert_equal_i(252, line->content_offset); /* check hunk 1 */ cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 1)); cl_assert_equal_i(18, (int)hunklen); cl_assert_equal_i(31, (int)hunk->old_start); cl_assert_equal_i(15, (int)hunk->old_lines); cl_assert_equal_i(25, (int)hunk->new_start); cl_assert_equal_i(9, (int)hunk->new_lines); cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 1)); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 0)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("My rulers and their treasure and their unborn populations,\n", actual.ptr); cl_assert_equal_i(31, line->old_lineno); cl_assert_equal_i(25, line->new_lineno); cl_assert_equal_i(-1, line->content_offset); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("The Daughters of the Palace whom they cherished in my Cities,\n", actual.ptr); cl_assert_equal_i(34, line->old_lineno); cl_assert_equal_i(-1, line->new_lineno); cl_assert_equal_i(1468, line->content_offset); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 12)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("Another replacement;\n", actual.ptr); cl_assert_equal_i(-1, line->old_lineno); cl_assert_equal_i(28, line->new_lineno); cl_assert_equal_i(1066, line->content_offset); git_patch_free(patch); git_diff_free(diff); /* Let's check line numbers when there is no newline */ git_str_rtrim(&old_content); cl_git_rewritefile("renames/songof7cities.txt", old_content.ptr); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, &opt)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(patch)); /* check hunk 0 */ cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 0)); cl_assert_equal_i(6, (int)hunklen); cl_assert_equal_i(46, (int)hunk->old_start); cl_assert_equal_i(4, (int)hunk->old_lines); cl_assert_equal_i(46, (int)hunk->new_start); cl_assert_equal_i(4, (int)hunk->new_lines); cl_assert_equal_i(6, (int)git_patch_num_lines_in_hunk(patch, 0)); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 1)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("And the horses and the chariots fleeing from them as of old!\n", actual.ptr); cl_assert_equal_i(47, line->old_lineno); cl_assert_equal_i(47, line->new_lineno); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 2)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("\n", actual.ptr); cl_assert_equal_i(48, line->old_lineno); cl_assert_equal_i(48, line->new_lineno); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s(" -- Rudyard Kipling\n", actual.ptr); cl_assert_equal_i(49, line->old_lineno); cl_assert_equal_i(-1, line->new_lineno); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 4)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s(" -- Rudyard Kipling", actual.ptr); cl_assert_equal_i(-1, line->old_lineno); cl_assert_equal_i(49, line->new_lineno); cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 5)); cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)line->origin); cl_git_pass(git_str_set(&actual, line->content, line->content_len)); cl_assert_equal_s("\n\\ No newline at end of file\n", actual.ptr); cl_assert_equal_i(-1, line->old_lineno); cl_assert_equal_i(49, line->new_lineno); git_patch_free(patch); git_diff_free(diff); git_str_dispose(&actual); git_str_dispose(&old_content); git_tree_free(head); } static void check_single_patch_stats( git_repository *repo, size_t hunks, size_t adds, size_t dels, size_t ctxt, size_t *sizes, const char *expected) { git_diff *diff; git_patch *patch; const git_diff_delta *delta; size_t actual_ctxt, actual_adds, actual_dels; cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i((int)hunks, (int)git_patch_num_hunks(patch)); cl_git_pass( git_patch_line_stats( &actual_ctxt, &actual_adds, &actual_dels, patch) ); cl_assert_equal_sz(ctxt, actual_ctxt); cl_assert_equal_sz(adds, actual_adds); cl_assert_equal_sz(dels, actual_dels); if (expected != NULL) { git_buf buf = GIT_BUF_INIT; cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected, buf.ptr); git_buf_dispose(&buf); cl_assert_equal_sz( strlen(expected), git_patch_size(patch, 1, 1, 1)); } if (sizes) { if (sizes[0]) cl_assert_equal_sz(sizes[0], git_patch_size(patch, 0, 0, 0)); if (sizes[1]) cl_assert_equal_sz(sizes[1], git_patch_size(patch, 1, 0, 0)); if (sizes[2]) cl_assert_equal_sz(sizes[2], git_patch_size(patch, 1, 1, 0)); } /* walk lines in hunk with basic sanity checks */ for (; hunks > 0; --hunks) { size_t i, max_i; const git_diff_line *line; int last_new_lineno = -1, last_old_lineno = -1; max_i = git_patch_num_lines_in_hunk(patch, hunks - 1); for (i = 0; i < max_i; ++i) { int expected = 1; cl_git_pass( git_patch_get_line_in_hunk(&line, patch, hunks - 1, i)); if (line->origin == GIT_DIFF_LINE_ADD_EOFNL || line->origin == GIT_DIFF_LINE_DEL_EOFNL || line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL) expected = 0; if (line->old_lineno >= 0) { if (last_old_lineno >= 0) cl_assert_equal_i( expected, line->old_lineno - last_old_lineno); last_old_lineno = line->old_lineno; } if (line->new_lineno >= 0) { if (last_new_lineno >= 0) cl_assert_equal_i( expected, line->new_lineno - last_new_lineno); last_new_lineno = line->new_lineno; } } } git_patch_free(patch); git_diff_free(diff); } void test_diff_patch__line_counts_with_eofnl(void) { git_config *cfg; git_str content = GIT_STR_INIT; const char *end; git_index *index; const char *expected = /* below is pasted output of 'git diff' with fn context removed */ "diff --git a/songof7cities.txt b/songof7cities.txt\n" "index 378a7d9..3d0154e 100644\n" "--- a/songof7cities.txt\n" "+++ b/songof7cities.txt\n" "@@ -42,7 +42,7 @@ With peoples undefeated of the dark, enduring blood.\n" " \n" " To the sound of trumpets shall their seed restore my Cities\n" " Wealthy and well-weaponed, that once more may I behold\n" "-All the world go softly when it walks before my Cities,\n" "+#All the world go softly when it walks before my Cities,\n" " And the horses and the chariots fleeing from them as of old!\n" " \n" " -- Rudyard Kipling\n" "\\ No newline at end of file\n"; size_t expected_sizes[3] = { 115, 119 + 115 + 114, 119 + 115 + 114 + 71 }; g_repo = cl_git_sandbox_init("renames"); cl_git_pass(git_config_new(&cfg)); git_repository_set_config(g_repo, cfg); git_config_free(cfg); git_repository_reinit_filesystem(g_repo, false); cl_git_pass(git_futils_readbuffer(&content, "renames/songof7cities.txt")); /* remove first line */ end = git_str_cstr(&content) + git_str_find(&content, '\n') + 1; git_str_consume(&content, end); cl_git_rewritefile("renames/songof7cities.txt", content.ptr); check_single_patch_stats(g_repo, 1, 0, 1, 3, NULL, NULL); /* remove trailing whitespace */ git_str_rtrim(&content); cl_git_rewritefile("renames/songof7cities.txt", content.ptr); check_single_patch_stats(g_repo, 2, 1, 2, 6, NULL, NULL); /* add trailing whitespace */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_write(index)); git_index_free(index); cl_git_pass(git_str_putc(&content, '\n')); cl_git_rewritefile("renames/songof7cities.txt", content.ptr); check_single_patch_stats(g_repo, 1, 1, 1, 3, NULL, NULL); /* no trailing whitespace as context line */ { /* walk back a couple lines, make space and insert char */ char *scan = content.ptr + content.size; int i; for (i = 0; i < 5; ++i) { for (--scan; scan > content.ptr && *scan != '\n'; --scan) /* seek to prev \n */; } cl_assert(scan > content.ptr); /* overwrite trailing \n with right-shifted content */ memmove(scan + 1, scan, content.size - (scan - content.ptr) - 1); /* insert '#' char into space we created */ scan[1] = '#'; } cl_git_rewritefile("renames/songof7cities.txt", content.ptr); check_single_patch_stats( g_repo, 1, 1, 1, 6, expected_sizes, expected); git_str_dispose(&content); } void test_diff_patch__can_strip_bad_utf8(void) { const char *a = "A " UTF8_HUNK_HEADER " B\n" " C\n" " D\n" " E\n" " F\n" " G\n" " H\n" " I\n" " J\n" " K\n" "L " UTF8_HUNK_HEADER " M\n" " N\n" " O\n" " P\n" " Q\n" " R\n" " S\n" " T\n" " U\n" " V\n"; const char *b = "A " UTF8_HUNK_HEADER " B\n" " C\n" " D\n" " E modified\n" " F\n" " G\n" " H\n" " I\n" " J\n" " K\n" "L " UTF8_HUNK_HEADER " M\n" " N\n" " O\n" " P modified\n" " Q\n" " R\n" " S\n" " T\n" " U\n" " V\n"; const char *expected = "diff --git a/file b/file\n" "index d0647c4..7827ce5 100644\n" "--- a/file\n" "+++ b/file\n" "@@ -2,7 +2,7 @@ A " UTF8_TRUNCATED_A_HUNK_HEADER " B\n" " C\n" " D\n" "- E\n" "+ E modified\n" " F\n" " G\n" " H\n" "@@ -13,7 +13,7 @@ L " UTF8_TRUNCATED_L_HUNK_HEADER " M\n" " N\n" " O\n" "- P\n" "+ P modified\n" " Q\n" " R\n" " S\n"; git_diff_options opts; git_patch *patch; git_buf buf = GIT_BUF_INIT; cl_git_pass(git_diff_options_init(&opts, GIT_DIFF_OPTIONS_VERSION)); cl_git_pass(git_patch_from_buffers(&patch, a, strlen(a), NULL, b, strlen(b), NULL, &opts)); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected, buf.ptr); git_patch_free(patch); git_buf_dispose(&buf); }
libgit2-main
tests/libgit2/diff/patch.c
#include "clar_libgit2.h" #include "diff_helpers.h" static git_repository *g_repo = NULL; void test_diff_rename__initialize(void) { g_repo = cl_git_sandbox_init("renames"); cl_repo_set_bool(g_repo, "core.autocrlf", false); } void test_diff_rename__cleanup(void) { cl_git_sandbox_cleanup(); } #define INITIAL_COMMIT "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2" #define COPY_RENAME_COMMIT "2bc7f351d20b53f1c72c16c4b036e491c478c49a" #define REWRITE_COPY_COMMIT "1c068dee5790ef1580cfc4cd670915b48d790084" #define RENAME_MODIFICATION_COMMIT "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13" #define REWRITE_DELETE_COMMIT "84d8efa38af7ace2b302de0adbda16b1f1cd2e1b" #define DELETE_RENAME_COMMIT "be053a189b0bbde545e0a3f59ce00b46ad29ce0d" #define BREAK_REWRITE_BASE_COMMIT "db98035f715427eef1f5e17f03e1801c05301e9e" #define BREAK_REWRITE_COMMIT "7e7bfb88ba9bc65fd700fee1819cf1c317aafa56" /* * Renames repo has: * * commit 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 - * serving.txt (25 lines) * sevencities.txt (50 lines) * commit 2bc7f351d20b53f1c72c16c4b036e491c478c49a - * serving.txt -> sixserving.txt (rename, no change, 100% match) * sevencities.txt -> sevencities.txt (no change) * sevencities.txt -> songofseven.txt (copy, no change, 100% match) * commit 1c068dee5790ef1580cfc4cd670915b48d790084 * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split) * sixserving.txt -> sixserving.txt (indentation change) * sixserving.txt -> ikeepsix.txt (copy, add title, >80% match) * sevencities.txt (no change) * commit 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 * songofseven.txt -> untimely.txt (rename, convert to crlf) * ikeepsix.txt -> ikeepsix.txt (reorder sections in file) * sixserving.txt -> sixserving.txt (whitespace change - not just indent) * sevencities.txt -> songof7cities.txt (rename, small text changes) * commit 84d8efa38af7ace2b302de0adbda16b1f1cd2e1b * songof7cities.txt -> songof7citie.txt (major rewrite, <20% match) * ikeepsix.txt -> (deleted) * untimely.txt (no change) * sixserving.txt (no change) * commit be053a189b0bbde545e0a3f59ce00b46ad29ce0d * ikeepsix.txt -> (deleted) * songof7cities.txt -> ikeepsix.txt (rename, 100% match) * untimely.txt (no change) * sixserving.txt (no change) */ void test_diff_rename__match_oid(void) { const char *old_sha = INITIAL_COMMIT; const char *new_sha = COPY_RENAME_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); /* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate * --find-copies-harder during rename transformion... */ diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); /* git diff --no-renames \ * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); /* git diff 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a * don't use NULL opts to avoid config `diff.renames` contamination */ opts.flags = GIT_DIFF_FIND_RENAMES; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); /* git diff --find-copies-harder \ * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); /* git diff --find-copies-harder -M100 -B100 \ * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED | GIT_DIFF_FIND_EXACT_MATCH_ONLY; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__checks_options_version(void) { const char *old_sha = INITIAL_COMMIT; const char *new_sha = COPY_RENAME_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; const git_error *err; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.version = 0; cl_git_fail(git_diff_find_similar(diff, &opts)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); git_error_clear(); opts.version = 1024; cl_git_fail(git_diff_find_similar(diff, &opts)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__not_exact_match(void) { const char *sha0 = COPY_RENAME_COMMIT; const char *sha1 = REWRITE_COPY_COMMIT; const char *sha2 = RENAME_MODIFICATION_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; /* == Changes ===================================================== * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split) * sixserving.txt -> sixserving.txt (indentation change) * sixserving.txt -> ikeepsix.txt (copy, add title, >80% match) * sevencities.txt (no change) */ old_tree = resolve_commit_oid_to_tree(g_repo, sha0); new_tree = resolve_commit_oid_to_tree(g_repo, sha1); /* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate * --find-copies-harder during rename transformion... */ diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); /* git diff --no-renames \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ * 1c068dee5790ef1580cfc4cd670915b48d790084 */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); /* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ * 1c068dee5790ef1580cfc4cd670915b48d790084 * * must not pass NULL for opts because it will pick up environment * values for "diff.renames" and test won't be consistent. */ opts.flags = GIT_DIFF_FIND_RENAMES; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); git_diff_free(diff); /* git diff -M -C \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ * 1c068dee5790ef1580cfc4cd670915b48d790084 */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ * 1c068dee5790ef1580cfc4cd670915b48d790084 */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL; opts.break_rewrite_threshold = 70; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); /* == Changes ===================================================== * songofseven.txt -> untimely.txt (rename, convert to crlf) * ikeepsix.txt -> ikeepsix.txt (reorder sections in file) * sixserving.txt -> sixserving.txt (whitespace - not just indent) * sevencities.txt -> songof7cities.txt (rename, small text changes) */ git_tree_free(old_tree); old_tree = new_tree; new_tree = resolve_commit_oid_to_tree(g_repo, sha2); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); /* git diff --no-renames \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ * 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); git_diff_free(diff); /* git diff -M -C \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ * 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ * 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 * with libgit2 default similarity comparison... */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); /* the default match algorithm is going to find the internal * whitespace differences in the lines of sixserving.txt to be * significant enough that this will decide to split it into an ADD * and a DELETE */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ * 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 * with ignore_space whitespace comparison */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_IGNORE_WHITESPACE; cl_git_pass(git_diff_find_similar(diff, &opts)); /* Ignoring whitespace, this should no longer split sixserver.txt */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__test_small_files(void) { git_index *index; git_reference *head_reference; git_commit *head_commit; git_tree *head_tree; git_tree *commit_tree; git_signature *signature; git_diff *diff; git_oid oid; const git_diff_delta *delta; git_diff_options diff_options = GIT_DIFF_OPTIONS_INIT; git_diff_find_options find_options = GIT_DIFF_FIND_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_mkfile("renames/small.txt", "Hello World!\n"); cl_git_pass(git_index_add_bypath(index, "small.txt")); cl_git_pass(git_repository_head(&head_reference, g_repo)); cl_git_pass(git_reference_peel((git_object**)&head_commit, head_reference, GIT_OBJECT_COMMIT)); cl_git_pass(git_commit_tree(&head_tree, head_commit)); cl_git_pass(git_index_write_tree(&oid, index)); cl_git_pass(git_tree_lookup(&commit_tree, g_repo, &oid)); cl_git_pass(git_signature_new(&signature, "Rename", "[email protected]", 1404157834, 0)); cl_git_pass(git_commit_create(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit)); cl_git_mkfile("renames/copy.txt", "Hello World!\n"); cl_git_rmfile("renames/small.txt"); diff_options.flags = GIT_DIFF_INCLUDE_UNTRACKED; cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, commit_tree, &diff_options)); find_options.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_FOR_UNTRACKED; cl_git_pass(git_diff_find_similar(diff, &find_options)); cl_assert_equal_i(git_diff_num_deltas(diff), 1); delta = git_diff_get_delta(diff, 0); cl_assert_equal_i(delta->status, GIT_DELTA_RENAMED); cl_assert_equal_s(delta->old_file.path, "small.txt"); cl_assert_equal_s(delta->new_file.path, "copy.txt"); git_diff_free(diff); git_signature_free(signature); git_tree_free(commit_tree); git_tree_free(head_tree); git_commit_free(head_commit); git_reference_free(head_reference); git_index_free(index); } void test_diff_rename__working_directory_changes(void) { const char *sha0 = COPY_RENAME_COMMIT; const char *blobsha = "66311f5cfbe7836c27510a3ba2f43e282e2c8bba"; git_oid id; git_tree *tree; git_blob *blob; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; git_str old_content = GIT_STR_INIT, content = GIT_STR_INIT;; tree = resolve_commit_oid_to_tree(g_repo, sha0); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED; /* $ git cat-file -p 2bc7f351d20b53f1c72c16c4b036e491c478c49a^{tree} 100644 blob 66311f5cfbe7836c27510a3ba2f43e282e2c8bba sevencities.txt 100644 blob ad0a8e55a104ac54a8a29ed4b84b49e76837a113 sixserving.txt 100644 blob 66311f5cfbe7836c27510a3ba2f43e282e2c8bba songofseven.txt $ for f in *.txt; do echo `git hash-object -t blob $f` $f done eaf4a3e3bfe68585e90cada20736ace491cd100b ikeepsix.txt f90d4fc20ecddf21eebe6a37e9225d244339d2b5 sixserving.txt 4210ffd5c390b21dd5483375e75288dea9ede512 songof7cities.txt 9a69d960ae94b060f56c2a8702545e2bb1abb935 untimely.txt */ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); /* git diff --no-renames 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); /* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); /* rewrite files in the working directory with / without CRLF changes */ cl_git_pass( git_futils_readbuffer(&old_content, "renames/songof7cities.txt")); cl_git_pass( git_str_lf_to_crlf(&content, &old_content)); cl_git_pass( git_futils_writebuffer(&content, "renames/songof7cities.txt", 0, 0)); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); /* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); /* try a different whitespace option */ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE; opts.rename_threshold = 70; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); /* try a different matching option */ cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_EXACT_MATCH_ONLY; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); git_diff_free(diff); /* again with exact match blob */ cl_git_pass(git_oid__fromstr(&id, blobsha, GIT_OID_SHA1)); cl_git_pass(git_blob_lookup(&blob, g_repo, &id)); cl_git_pass(git_str_set( &content, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob))); cl_git_rewritefile("renames/songof7cities.txt", content.ptr); git_blob_free(blob); cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts)); opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_EXACT_MATCH_ONLY; cl_git_pass(git_diff_find_similar(diff, &opts)); /* fprintf(stderr, "\n\n"); diff_print_raw(stderr, diff); */ memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); git_tree_free(tree); git_str_dispose(&content); git_str_dispose(&old_content); } void test_diff_rename__patch(void) { const char *sha0 = COPY_RENAME_COMMIT; const char *sha1 = REWRITE_COPY_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; git_patch *patch; const git_diff_delta *delta; git_buf buf = GIT_BUF_INIT; const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n"; old_tree = resolve_commit_oid_to_tree(g_repo, sha0); new_tree = resolve_commit_oid_to_tree(g_repo, sha1); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES; cl_git_pass(git_diff_find_similar(diff, &opts)); /* == Changes ===================================================== * sixserving.txt -> ikeepsix.txt (copy, add title, >80% match) * sevencities.txt (no change) * sixserving.txt -> sixserving.txt (indentation change) * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split) */ cl_assert_equal_i(4, (int)git_diff_num_deltas(diff)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status); cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_equal_s(expected, buf.ptr); git_buf_dispose(&buf); git_patch_free(patch); cl_assert((delta = git_diff_get_delta(diff, 1)) != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, (int)delta->status); cl_assert((delta = git_diff_get_delta(diff, 2)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert((delta = git_diff_get_delta(diff, 3)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__file_exchange(void) { git_str c1 = GIT_STR_INIT, c2 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt")); cl_git_pass(git_futils_readbuffer(&c2, "renames/songof7cities.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/songof7cities.txt", 0, 0)); cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); git_str_dispose(&c2); } void test_diff_rename__file_exchange_three(void) { git_str c1 = GIT_STR_INIT, c2 = GIT_STR_INIT, c3 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt")); cl_git_pass(git_futils_readbuffer(&c2, "renames/songof7cities.txt")); cl_git_pass(git_futils_readbuffer(&c3, "renames/ikeepsix.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/ikeepsix.txt", 0, 0)); cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0)); cl_git_pass(git_futils_writebuffer(&c3, "renames/songof7cities.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt")); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); git_str_dispose(&c2); git_str_dispose(&c3); } void test_diff_rename__file_partial_exchange(void) { git_str c1 = GIT_STR_INIT, c2 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; int i; cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/songof7cities.txt", 0, 0)); for (i = 0; i < 100; ++i) cl_git_pass(git_str_puts(&c2, "this is not the content you are looking for\n")); cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); git_str_dispose(&c2); } void test_diff_rename__rename_and_copy_from_same_source(void) { git_str c1 = GIT_STR_INIT, c2 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; /* put the first 2/3 of file into one new place * and the second 2/3 of file into another new place */ cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt")); cl_git_pass(git_str_set(&c2, c1.ptr, c1.size)); git_str_truncate(&c1, c1.size * 2 / 3); git_str_consume(&c2, ((char *)c2.ptr) + (c2.size / 3)); cl_git_pass(git_futils_writebuffer(&c1, "renames/song_a.txt", 0, 0)); cl_git_pass(git_futils_writebuffer(&c2, "renames/song_b.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "song_a.txt")); cl_git_pass(git_index_add_bypath(index, "song_b.txt")); diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNMODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(6, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNMODIFIED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); git_str_dispose(&c2); } void test_diff_rename__from_deleted_to_split(void) { git_str c1 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; /* old file is missing, new file is actually old file renamed */ cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_remove_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNMODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNMODIFIED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); } struct rename_expected { size_t len; unsigned int *status; const char **sources; const char **targets; size_t idx; }; static int test_names_expected(const git_diff_delta *delta, float progress, void *p) { struct rename_expected *expected = p; GIT_UNUSED(progress); cl_assert(expected->idx < expected->len); cl_assert_equal_i(delta->status, expected->status[expected->idx]); cl_assert(git__strcmp(expected->sources[expected->idx], delta->old_file.path) == 0); cl_assert(git__strcmp(expected->targets[expected->idx], delta->new_file.path) == 0); expected->idx++; return 0; } void test_diff_rename__rejected_match_can_match_others(void) { git_reference *head, *selfsimilar; git_index *index; git_tree *tree; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; git_str one = GIT_STR_INIT, two = GIT_STR_INIT; unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED }; const char *sources[] = { "Class1.cs", "Class2.cs" }; const char *targets[] = { "ClassA.cs", "ClassB.cs" }; struct rename_expected expect = { 2, status, sources, targets }; char *ptr; opts.checkout_strategy = GIT_CHECKOUT_FORCE; findopts.flags = GIT_DIFF_FIND_RENAMES; cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_git_pass(git_reference_symbolic_set_target( &selfsimilar, head, "refs/heads/renames_similar", NULL)); cl_git_pass(git_checkout_head(g_repo, &opts)); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_futils_readbuffer(&one, "renames/Class1.cs")); cl_git_pass(git_futils_readbuffer(&two, "renames/Class2.cs")); cl_git_pass(p_unlink("renames/Class1.cs")); cl_git_pass(p_unlink("renames/Class2.cs")); cl_git_pass(git_index_remove_bypath(index, "Class1.cs")); cl_git_pass(git_index_remove_bypath(index, "Class2.cs")); cl_assert(ptr = strstr(one.ptr, "Class1")); ptr[5] = 'A'; cl_assert(ptr = strstr(two.ptr, "Class2")); ptr[5] = 'B'; cl_git_pass( git_futils_writebuffer(&one, "renames/ClassA.cs", O_RDWR|O_CREAT, 0777)); cl_git_pass( git_futils_writebuffer(&two, "renames/ClassB.cs", O_RDWR|O_CREAT, 0777)); cl_git_pass(git_index_add_bypath(index, "ClassA.cs")); cl_git_pass(git_index_add_bypath(index, "ClassB.cs")); cl_git_pass(git_index_write(index)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass( git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); cl_git_pass(git_diff_foreach( diff, test_names_expected, NULL, NULL, NULL, &expect)); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); git_reference_free(selfsimilar); git_str_dispose(&one); git_str_dispose(&two); } static void write_similarity_file_two(const char *filename, size_t b_lines) { git_str contents = GIT_STR_INIT; size_t i; for (i = 0; i < b_lines; i++) git_str_printf(&contents, "%02d - bbbbb\r\n", (int)(i+1)); for (i = b_lines; i < 50; i++) git_str_printf(&contents, "%02d - aaaaa%s", (int)(i+1), (i == 49 ? "" : "\r\n")); cl_git_pass( git_futils_writebuffer(&contents, filename, O_RDWR|O_CREAT, 0777)); git_str_dispose(&contents); } void test_diff_rename__rejected_match_can_match_others_two(void) { git_reference *head, *selfsimilar; git_index *index; git_tree *tree; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED }; const char *sources[] = { "a.txt", "b.txt" }; const char *targets[] = { "c.txt", "d.txt" }; struct rename_expected expect = { 2, status, sources, targets }; opts.checkout_strategy = GIT_CHECKOUT_FORCE; findopts.flags = GIT_DIFF_FIND_RENAMES; cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_git_pass(git_reference_symbolic_set_target( &selfsimilar, head, "refs/heads/renames_similar_two", NULL)); cl_git_pass(git_checkout_head(g_repo, &opts)); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(p_unlink("renames/a.txt")); cl_git_pass(p_unlink("renames/b.txt")); cl_git_pass(git_index_remove_bypath(index, "a.txt")); cl_git_pass(git_index_remove_bypath(index, "b.txt")); write_similarity_file_two("renames/c.txt", 7); write_similarity_file_two("renames/d.txt", 8); cl_git_pass(git_index_add_bypath(index, "c.txt")); cl_git_pass(git_index_add_bypath(index, "d.txt")); cl_git_pass(git_index_write(index)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass( git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); cl_git_pass(git_diff_foreach( diff, test_names_expected, NULL, NULL, NULL, &expect)); cl_assert(expect.idx > 0); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); git_reference_free(selfsimilar); } void test_diff_rename__rejected_match_can_match_others_three(void) { git_reference *head, *selfsimilar; git_index *index; git_tree *tree; git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; /* Both cannot be renames from a.txt */ unsigned int status[] = { GIT_DELTA_ADDED, GIT_DELTA_RENAMED }; const char *sources[] = { "0001.txt", "a.txt" }; const char *targets[] = { "0001.txt", "0002.txt" }; struct rename_expected expect = { 2, status, sources, targets }; opts.checkout_strategy = GIT_CHECKOUT_FORCE; findopts.flags = GIT_DIFF_FIND_RENAMES; cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_git_pass(git_reference_symbolic_set_target( &selfsimilar, head, "refs/heads/renames_similar_two", NULL)); cl_git_pass(git_checkout_head(g_repo, &opts)); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(p_unlink("renames/a.txt")); cl_git_pass(git_index_remove_bypath(index, "a.txt")); write_similarity_file_two("renames/0001.txt", 7); write_similarity_file_two("renames/0002.txt", 0); cl_git_pass(git_index_add_bypath(index, "0001.txt")); cl_git_pass(git_index_add_bypath(index, "0002.txt")); cl_git_pass(git_index_write(index)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass( git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); cl_git_pass(git_diff_foreach( diff, test_names_expected, NULL, NULL, NULL, &expect)); cl_assert(expect.idx == expect.len); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); git_reference_free(selfsimilar); } void test_diff_rename__can_rename_from_rewrite(void) { git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED }; const char *sources[] = { "ikeepsix.txt", "songof7cities.txt" }; const char *targets[] = { "songof7cities.txt", "this-is-a-rename.txt" }; struct rename_expected expect = { 2, status, sources, targets }; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(p_rename("renames/songof7cities.txt", "renames/this-is-a-rename.txt")); cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/songof7cities.txt")); cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt")); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_add_bypath(index, "this-is-a-rename.txt")); cl_git_pass(git_index_write(index)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass( git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); findopts.flags |= GIT_DIFF_FIND_AND_BREAK_REWRITES | GIT_DIFF_FIND_REWRITES | GIT_DIFF_FIND_RENAMES_FROM_REWRITES; cl_git_pass(git_diff_find_similar(diff, &findopts)); cl_git_pass(git_diff_foreach( diff, test_names_expected, NULL, NULL, NULL, &expect)); cl_assert(expect.idx == expect.len); git_diff_free(diff); git_tree_free(tree); git_index_free(index); } void test_diff_rename__case_changes_are_split(void) { git_index *index; git_tree *tree; git_diff *diff = NULL; diff_expects exp; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/IKEEPSIX.txt")); cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt")); cl_git_pass(git_index_add_bypath(index, "IKEEPSIX.txt")); cl_git_pass(git_index_write(index)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, NULL)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_index_free(index); git_tree_free(tree); } void test_diff_rename__unmodified_can_be_renamed(void) { git_index *index; git_tree *tree; git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt")); cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt")); cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt")); cl_git_pass(git_index_write(index)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_index_free(index); git_tree_free(tree); } void test_diff_rename__rewrite_on_single_file(void) { git_index *index; git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED; findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED | GIT_DIFF_FIND_AND_BREAK_REWRITES | GIT_DIFF_FIND_RENAMES_FROM_REWRITES; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_rewritefile("renames/ikeepsix.txt", "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n" \ "This is enough content for the file to be rewritten.\n"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); git_index_free(index); } void test_diff_rename__can_find_copy_to_split(void) { git_str c1 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]); opts.flags = GIT_DIFF_FIND_ALL; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); } void test_diff_rename__can_delete_unmodified_deltas(void) { git_str c1 = GIT_STR_INIT; git_index *index; git_tree *tree; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt")); cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0)); cl_git_pass( git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, tree)); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]); opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_REMOVE_UNMODIFIED; cl_git_pass(git_diff_find_similar(diff, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_str_dispose(&c1); } void test_diff_rename__matches_config_behavior(void) { const char *sha0 = INITIAL_COMMIT; const char *sha1 = COPY_RENAME_COMMIT; const char *sha2 = REWRITE_COPY_COMMIT; git_tree *tree0, *tree1, *tree2; git_config *cfg; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; opts.flags = GIT_DIFF_FIND_BY_CONFIG; tree0 = resolve_commit_oid_to_tree(g_repo, sha0); tree1 = resolve_commit_oid_to_tree(g_repo, sha1); tree2 = resolve_commit_oid_to_tree(g_repo, sha2); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; cl_git_pass(git_repository_config(&cfg, g_repo)); /* diff.renames = false; no rename detection should happen */ cl_git_pass(git_config_set_bool(cfg, "diff.renames", false)); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree0, tree1, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); git_diff_free(diff); /* diff.renames = true; should act like -M */ cl_git_pass(git_config_set_bool(cfg, "diff.renames", true)); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree0, tree1, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(3, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); /* diff.renames = copies; should act like -M -C */ cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies")); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree1, tree2, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); /* NULL find options is the same as GIT_DIFF_FIND_BY_CONFIG */ cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree1, tree2, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, NULL)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); /* Cleanup */ git_tree_free(tree0); git_tree_free(tree1); git_tree_free(tree2); git_config_free(cfg); } void test_diff_rename__can_override_thresholds_when_obeying_config(void) { const char *sha1 = COPY_RENAME_COMMIT; const char *sha2 = REWRITE_COPY_COMMIT; git_tree *tree1, *tree2; git_config *cfg; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; tree1 = resolve_commit_oid_to_tree(g_repo, sha1); tree2 = resolve_commit_oid_to_tree(g_repo, sha2); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; opts.flags = GIT_DIFF_FIND_BY_CONFIG; cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies")); git_config_free(cfg); /* copy threshold = 96%, should see creation of ikeepsix.txt */ opts.copy_threshold = 96; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree1, tree2, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); git_diff_free(diff); /* copy threshold = 20%, should see sixserving.txt => ikeepsix.txt */ opts.copy_threshold = 20; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree1, tree2, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(4, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); git_diff_free(diff); /* Cleanup */ git_tree_free(tree1); git_tree_free(tree2); } void test_diff_rename__by_config_doesnt_mess_with_whitespace_settings(void) { const char *sha1 = REWRITE_COPY_COMMIT; const char *sha2 = RENAME_MODIFICATION_COMMIT; git_tree *tree1, *tree2; git_config *cfg; git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; tree1 = resolve_commit_oid_to_tree(g_repo, sha1); tree2 = resolve_commit_oid_to_tree(g_repo, sha2); diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; opts.flags = GIT_DIFF_FIND_BY_CONFIG; cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies")); git_config_free(cfg); /* Don't ignore whitespace; this should find a change in sixserving.txt */ opts.flags |= 0 | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE; cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, tree1, tree2, &diffopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_find_similar(diff, &opts)); cl_git_pass(git_diff_foreach(diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(5, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); git_diff_free(diff); /* Cleanup */ git_tree_free(tree1); git_tree_free(tree2); } static void expect_files_renamed(const char *one, const char *two, uint32_t whitespace_flags) { git_index *index; git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED; findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED | GIT_DIFF_FIND_AND_BREAK_REWRITES | GIT_DIFF_FIND_RENAMES_FROM_REWRITES | whitespace_flags; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_rewritefile("renames/ikeepsix.txt", one); cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt")); cl_git_rmfile("renames/ikeepsix.txt"); cl_git_rewritefile("renames/ikeepsix2.txt", two); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); git_diff_free(diff); git_index_free(index); } /* test some variations on empty and blank files */ void test_diff_rename__empty_files_renamed(void) { /* empty files are identical when ignoring whitespace or not */ expect_files_renamed("", "", GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE); expect_files_renamed("", "", GIT_DIFF_FIND_IGNORE_WHITESPACE); } /* test that blank files are similar when ignoring whitespace */ void test_diff_rename__blank_files_renamed_when_ignoring_whitespace(void) { expect_files_renamed("", "\n\n", GIT_DIFF_FIND_IGNORE_WHITESPACE); expect_files_renamed("", "\r\n\r\n", GIT_DIFF_FIND_IGNORE_WHITESPACE); expect_files_renamed("\r\n\r\n", "\n\n\n", GIT_DIFF_FIND_IGNORE_WHITESPACE); expect_files_renamed(" ", "\n\n", GIT_DIFF_FIND_IGNORE_WHITESPACE); expect_files_renamed(" \n \n", "\n\n", GIT_DIFF_FIND_IGNORE_WHITESPACE); } /* blank files are not similar when whitespace is not ignored */ static void expect_files_not_renamed(const char *one, const char *two, uint32_t whitespace_flags) { git_index *index; git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED; findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED | whitespace_flags; cl_git_pass(git_repository_index(&index, g_repo)); cl_git_rewritefile("renames/ikeepsix.txt", one); cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt")); cl_git_rmfile("renames/ikeepsix.txt"); cl_git_rewritefile("renames/ikeepsix2.txt", two); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts)); cl_git_pass(git_diff_find_similar(diff, &findopts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(2, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); git_index_free(index); } /* test that blank files are similar when ignoring renames */ void test_diff_rename__blank_files_not_renamed_when_not_ignoring_whitespace(void) { expect_files_not_renamed("", "\r\n\r\n\r\n", GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE); expect_files_not_renamed("", "\n\n\n\n", GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE); expect_files_not_renamed("\n\n\n\n", "\r\n\r\n\r\n", GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE); } /* test that 100% renames and copies emit the correct patch file * git diff --find-copies-harder -M100 -B100 \ * 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a */ void test_diff_rename__identical(void) { const char *old_sha = INITIAL_COMMIT; const char *new_sha = COPY_RENAME_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT; git_buf diff_buf = GIT_BUF_INIT; const char *expected = "diff --git a/serving.txt b/sixserving.txt\n" "similarity index 100%\n" "rename from serving.txt\n" "rename to sixserving.txt\n" "diff --git a/sevencities.txt b/songofseven.txt\n" "similarity index 100%\n" "copy from sevencities.txt\n" "copy to songofseven.txt\n"; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); diff_opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; find_opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED | GIT_DIFF_FIND_EXACT_MATCH_ONLY; cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, &diff_opts)); cl_git_pass(git_diff_find_similar(diff, &find_opts)); cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH)); cl_assert_equal_s(expected, diff_buf.ptr); git_buf_dispose(&diff_buf); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__rewrite_and_delete(void) { const char *old_sha = RENAME_MODIFICATION_COMMIT; const char *new_sha = REWRITE_DELETE_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT; git_buf diff_buf = GIT_BUF_INIT; const char *expected = "diff --git a/ikeepsix.txt b/ikeepsix.txt\n" "deleted file mode 100644\n" "index eaf4a3e..0000000\n" "--- a/ikeepsix.txt\n" "+++ /dev/null\n" "@@ -1,27 +0,0 @@\n" "-I Keep Six Honest Serving-Men\n" "-=============================\n" "-\n" "-She sends'em abroad on her own affairs,\n" "- From the second she opens her eyes—\n" "-One million Hows, two million Wheres,\n" "-And seven million Whys!\n" "-\n" "-I let them rest from nine till five,\n" "- For I am busy then,\n" "-As well as breakfast, lunch, and tea,\n" "- For they are hungry men.\n" "-But different folk have different views;\n" "-I know a person small—\n" "-She keeps ten million serving-men,\n" "-Who get no rest at all!\n" "-\n" "- -- Rudyard Kipling\n" "-\n" "-I KEEP six honest serving-men\n" "- (They taught me all I knew);\n" "-Their names are What and Why and When\n" "- And How and Where and Who.\n" "-I send them over land and sea,\n" "- I send them east and west;\n" "-But after they have worked for me,\n" "- I give them all a rest.\n" "diff --git a/songof7cities.txt b/songof7cities.txt\n" "index 4210ffd..95ceb12 100644\n" "--- a/songof7cities.txt\n" "+++ b/songof7cities.txt\n" "@@ -1,45 +1,45 @@\n" "-The Song of Seven Cities\n" "+THE SONG OF SEVEN CITIES\n" " ------------------------\n" " \n" "-I WAS Lord of Cities very sumptuously builded.\n" "-Seven roaring Cities paid me tribute from afar.\n" "-Ivory their outposts were--the guardrooms of them gilded,\n" "-And garrisoned with Amazons invincible in war.\n" "-\n" "-All the world went softly when it walked before my Cities--\n" "-Neither King nor Army vexed my peoples at their toil,\n" "-Never horse nor chariot irked or overbore my Cities,\n" "-Never Mob nor Ruler questioned whence they drew their spoil.\n" "-\n" "-Banded, mailed and arrogant from sunrise unto sunset;\n" "-Singing while they sacked it, they possessed the land at large.\n" "-Yet when men would rob them, they resisted, they made onset\n" "-And pierced the smoke of battle with a thousand-sabred charge.\n" "-\n" "-So they warred and trafficked only yesterday, my Cities.\n" "-To-day there is no mark or mound of where my Cities stood.\n" "-For the River rose at midnight and it washed away my Cities.\n" "-They are evened with Atlantis and the towns before the Flood.\n" "-\n" "-Rain on rain-gorged channels raised the water-levels round them,\n" "-Freshet backed on freshet swelled and swept their world from sight,\n" "-Till the emboldened floods linked arms and, flashing forward, drowned them--\n" "-Drowned my Seven Cities and their peoples in one night!\n" "-\n" "-Low among the alders lie their derelict foundations,\n" "-The beams wherein they trusted and the plinths whereon they built--\n" "-My rulers and their treasure and their unborn populations,\n" "-Dead, destroyed, aborted, and defiled with mud and silt!\n" "-\n" "-The Daughters of the Palace whom they cherished in my Cities,\n" "-My silver-tongued Princesses, and the promise of their May--\n" "-Their bridegrooms of the June-tide--all have perished in my Cities,\n" "-With the harsh envenomed virgins that can neither love nor play.\n" "-\n" "-I was Lord of Cities--I will build anew my Cities,\n" "-Seven, set on rocks, above the wrath of any flood.\n" "-Nor will I rest from search till I have filled anew my Cities\n" "-With peoples undefeated of the dark, enduring blood.\n" "+I WAS LORD OF CITIES VERY SUMPTUOUSLY BUILDED.\n" "+SEVEN ROARING CITIES PAID ME TRIBUTE FROM AFAR.\n" "+IVORY THEIR OUTPOSTS WERE--THE GUARDROOMS OF THEM GILDED,\n" "+AND GARRISONED WITH AMAZONS INVINCIBLE IN WAR.\n" "+\n" "+ALL THE WORLD WENT SOFTLY WHEN IT WALKED BEFORE MY CITIES--\n" "+NEITHER KING NOR ARMY VEXED MY PEOPLES AT THEIR TOIL,\n" "+NEVER HORSE NOR CHARIOT IRKED OR OVERBORE MY CITIES,\n" "+NEVER MOB NOR RULER QUESTIONED WHENCE THEY DREW THEIR SPOIL.\n" "+\n" "+BANDED, MAILED AND ARROGANT FROM SUNRISE UNTO SUNSET;\n" "+SINGING WHILE THEY SACKED IT, THEY POSSESSED THE LAND AT LARGE.\n" "+YET WHEN MEN WOULD ROB THEM, THEY RESISTED, THEY MADE ONSET\n" "+AND PIERCED THE SMOKE OF BATTLE WITH A THOUSAND-SABRED CHARGE.\n" "+\n" "+SO THEY WARRED AND TRAFFICKED ONLY YESTERDAY, MY CITIES.\n" "+TO-DAY THERE IS NO MARK OR MOUND OF WHERE MY CITIES STOOD.\n" "+FOR THE RIVER ROSE AT MIDNIGHT AND IT WASHED AWAY MY CITIES.\n" "+THEY ARE EVENED WITH ATLANTIS AND THE TOWNS BEFORE THE FLOOD.\n" "+\n" "+RAIN ON RAIN-GORGED CHANNELS RAISED THE WATER-LEVELS ROUND THEM,\n" "+FRESHET BACKED ON FRESHET SWELLED AND SWEPT THEIR WORLD FROM SIGHT,\n" "+TILL THE EMBOLDENED FLOODS LINKED ARMS AND, FLASHING FORWARD, DROWNED THEM--\n" "+DROWNED MY SEVEN CITIES AND THEIR PEOPLES IN ONE NIGHT!\n" "+\n" "+LOW AMONG THE ALDERS LIE THEIR DERELICT FOUNDATIONS,\n" "+THE BEAMS WHEREIN THEY TRUSTED AND THE PLINTHS WHEREON THEY BUILT--\n" "+MY RULERS AND THEIR TREASURE AND THEIR UNBORN POPULATIONS,\n" "+DEAD, DESTROYED, ABORTED, AND DEFILED WITH MUD AND SILT!\n" "+\n" "+THE DAUGHTERS OF THE PALACE WHOM THEY CHERISHED IN MY CITIES,\n" "+MY SILVER-TONGUED PRINCESSES, AND THE PROMISE OF THEIR MAY--\n" "+THEIR BRIDEGROOMS OF THE JUNE-TIDE--ALL HAVE PERISHED IN MY CITIES,\n" "+WITH THE HARSH ENVENOMED VIRGINS THAT CAN NEITHER LOVE NOR PLAY.\n" "+\n" "+I WAS LORD OF CITIES--I WILL BUILD ANEW MY CITIES,\n" "+SEVEN, SET ON ROCKS, ABOVE THE WRATH OF ANY FLOOD.\n" "+NOR WILL I REST FROM SEARCH TILL I HAVE FILLED ANEW MY CITIES\n" "+WITH PEOPLES UNDEFEATED OF THE DARK, ENDURING BLOOD.\n" " \n" " To the sound of trumpets shall their seed restore my Cities\n" " Wealthy and well-weaponed, that once more may I behold\n"; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); find_opts.flags = GIT_DIFF_FIND_RENAMES_FROM_REWRITES; cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL)); cl_git_pass(git_diff_find_similar(diff, &find_opts)); cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH)); cl_assert_equal_s(expected, diff_buf.ptr); git_buf_dispose(&diff_buf); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } void test_diff_rename__delete_and_rename(void) { const char *old_sha = RENAME_MODIFICATION_COMMIT; const char *new_sha = DELETE_RENAME_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT; git_buf diff_buf = GIT_BUF_INIT; const char *expected = "diff --git a/sixserving.txt b/sixserving.txt\n" "deleted file mode 100644\n" "index f90d4fc..0000000\n" "--- a/sixserving.txt\n" "+++ /dev/null\n" "@@ -1,25 +0,0 @@\n" "-I KEEP six honest serving-men\n" "- (They taught me all I knew);\n" "-Their names are What and Why and When\n" "- And How and Where and Who.\n" "-I send them over land and sea,\n" "- I send them east and west;\n" "-But after they have worked for me,\n" "- I give them all a rest.\n" "-\n" "-I let them rest from nine till five,\n" "- For I am busy then,\n" "-As well as breakfast, lunch, and tea,\n" "- For they are hungry men.\n" "-But different folk have different views;\n" "-I know a person small—\n" "-She keeps ten million serving-men,\n" "-Who get no rest at all!\n" "-\n" "-She sends'em abroad on her own affairs,\n" "- From the second she opens her eyes—\n" "-One million Hows, two million Wheres,\n" "-And seven million Whys!\n" "-\n" "- -- Rudyard Kipling\n" "-\n" "diff --git a/songof7cities.txt b/sixserving.txt\n" "similarity index 100%\n" "rename from songof7cities.txt\n" "rename to sixserving.txt\n"; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); find_opts.flags = GIT_DIFF_FIND_RENAMES_FROM_REWRITES; cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL)); cl_git_pass(git_diff_find_similar(diff, &find_opts)); cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH)); cl_assert_equal_s(expected, diff_buf.ptr); git_buf_dispose(&diff_buf); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } /* * The break_rewrite branch contains a testcase reduced from * a real-world scenario, rather than being "constructed" like * the above tests seem to be. There are two commits layered * on top of the repo's initial commit; the base commit which * clears out the files from the initial commit and installs * four files. And then there's the modification commit which * mutates the files in such a way as to trigger the bug in * libgit2. * commit db98035f715427eef1f5e17f03e1801c05301e9e * serving.txt (deleted) * sevencities.txt (deleted) * AAA (313 lines) * BBB (314 lines) * CCC (704 lines) * DDD (314 lines, identical to BBB) * commit 7e7bfb88ba9bc65fd700fee1819cf1c317aafa56 * This deletes CCC and makes slight modifications * to AAA, BBB, and DDD. The find_best_matches loop * for git_diff_find_similar computes the following: * CCC moved to AAA (similarity 91) * CCC copied to AAA (similarity 91) * DDD moved to BBB (similarity 52) * CCC copied to BBB (similarity 90) * BBB moved to DDD (similarity 52) * CCC copied to DDD (similarity 90) * The code to rewrite the diffs by resolving these * copies/renames would resolve the BBB <-> DDD moves * but then still leave BBB as a rename target for * the deleted file CCC. Since the split flag on BBB * was cleared, this would trigger an error. */ void test_diff_rename__break_rewrite(void) { const char *old_sha = BREAK_REWRITE_BASE_COMMIT; const char *new_sha = BREAK_REWRITE_COMMIT; git_tree *old_tree, *new_tree; git_diff *diff; git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT; old_tree = resolve_commit_oid_to_tree(g_repo, old_sha); new_tree = resolve_commit_oid_to_tree(g_repo, new_sha); find_opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES | GIT_DIFF_BREAK_REWRITES | GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY; cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL)); cl_git_pass(git_diff_find_similar(diff, &find_opts)); git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); }
libgit2-main
tests/libgit2/diff/rename.c
#include "clar_libgit2.h" #include "patch/patch_common.h" static void verify_patch_id(const char *diff_content, const char *expected_id) { git_oid expected_oid, actual_oid; git_diff *diff; cl_git_pass(git_oid__fromstr(&expected_oid, expected_id, GIT_OID_SHA1)); cl_git_pass(git_diff_from_buffer(&diff, diff_content, strlen(diff_content))); cl_git_pass(git_diff_patchid(&actual_oid, diff, NULL)); cl_assert_equal_oid(&expected_oid, &actual_oid); git_diff_free(diff); } void test_diff_patchid__simple_commit(void) { verify_patch_id(PATCH_SIMPLE_COMMIT, "06094b1948b878b7d9ff7560b4eae672a014b0ec"); } void test_diff_patchid__deleted_file(void) { verify_patch_id(PATCH_DELETE_ORIGINAL, "d18507fe189f49c028b32c8c34e1ad98dd6a1aad"); verify_patch_id(PATCH_DELETED_FILE_2_HUNKS, "f31412498a17e6c3fbc635f2c5f9aa3ef4c1a9b7"); } void test_diff_patchid__created_file(void) { verify_patch_id(PATCH_ADD_ORIGINAL, "a7d39379308021465ae2ce65e338c048a3110db6"); } void test_diff_patchid__binary_file(void) { verify_patch_id(PATCH_ADD_BINARY_NOT_PRINTED, "2b31236b485faa30cf4dd33e4d6539829996739f"); } void test_diff_patchid__renamed_file(void) { verify_patch_id(PATCH_RENAME_EXACT, "4666d50cea4976f6f727448046d43461912058fd"); verify_patch_id(PATCH_RENAME_SIMILAR, "a795087575fcb940227be524488bedd6b3d3f438"); } void test_diff_patchid__modechange(void) { verify_patch_id(PATCH_MODECHANGE_UNCHANGED, "dbf3423ee98375ef1c72a79fbd29a049a2bae771"); verify_patch_id(PATCH_MODECHANGE_MODIFIED, "93aba696e1bbd2bbb73e3e3e62ed71f232137657"); } void test_diff_patchid__shuffle_hunks(void) { verify_patch_id(PATCH_DELETED_FILE_2_HUNKS_SHUFFLED, "f31412498a17e6c3fbc635f2c5f9aa3ef4c1a9b7"); } void test_diff_patchid__filename_with_spaces(void) { verify_patch_id(PATCH_APPEND_NO_NL, "f0ba05413beaef743b630e796153839462ee477a"); } void test_diff_patchid__multiple_hunks(void) { verify_patch_id(PATCH_MULTIPLE_HUNKS, "81e26c34643d17f521e57c483a6a637e18ba1f57"); } void test_diff_patchid__multiple_files(void) { verify_patch_id(PATCH_MULTIPLE_FILES, "192d1f49d23f2004517963aecd3f8a6c467f50ff"); } void test_diff_patchid__same_diff_with_differing_whitespace_has_same_id(void) { const char *tabs = "diff --git a/file.txt b/file.txt\n" "index 8fecc09..1d43a92 100644\n" "--- a/file.txt\n" "+++ b/file.txt\n" "@@ -1 +1 @@\n" "-old text\n" "+ new text\n"; const char *spaces = "diff --git a/file.txt b/file.txt\n" "index 8fecc09..1d43a92 100644\n" "--- a/file.txt\n" "+++ b/file.txt\n" "@@ -1 +1 @@\n" "-old text\n" "+ new text\n"; const char *id = "11efdd13c30f7a1056eac2ae2fb952da475e2c23"; verify_patch_id(tabs, id); verify_patch_id(spaces, id); }
libgit2-main
tests/libgit2/diff/patchid.c
#include "clar_libgit2.h" #include "repository.h" #include "posix.h" #include "diff_helpers.h" #include "../submodule/submodule_helpers.h" static git_repository *g_repo = NULL; void test_diff_submodules__initialize(void) { } void test_diff_submodules__cleanup(void) { cl_git_sandbox_cleanup(); } #define get_buf_ptr(buf) ((buf)->size ? (buf)->ptr : NULL) static void check_diff_patches_at_line( git_diff *diff, const char **expected, const char *file, const char *func, int line) { const git_diff_delta *delta; git_patch *patch = NULL; size_t d, num_d = git_diff_num_deltas(diff); git_buf buf = GIT_BUF_INIT; for (d = 0; d < num_d; ++d, git_patch_free(patch)) { cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_assert((delta = git_patch_get_delta(patch)) != NULL); if (delta->status == GIT_DELTA_UNMODIFIED) { cl_assert_at_line(expected[d] == NULL, file, func, line); continue; } if (expected[d] && !strcmp(expected[d], "<SKIP>")) continue; if (expected[d] && !strcmp(expected[d], "<UNTRACKED>")) { cl_assert_at_line(delta->status == GIT_DELTA_UNTRACKED, file, func, line); continue; } if (expected[d] && !strcmp(expected[d], "<END>")) { cl_git_pass(git_patch_to_buf(&buf, patch)); cl_assert_at_line(!strcmp(expected[d], "<END>"), file, func, line); } cl_git_pass(git_patch_to_buf(&buf, patch)); clar__assert_equal( file, func, line, "expected diff did not match actual diff", 1, "%s", expected[d], get_buf_ptr(&buf)); git_buf_dispose(&buf); } cl_assert_at_line(expected[d] && !strcmp(expected[d], "<END>"), file, func, line); } #define check_diff_patches(diff, exp) \ check_diff_patches_at_line(diff, exp, __FILE__, __func__, __LINE__) void test_diff_submodules__unmodified_submodule(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; static const char *expected[] = { "<SKIP>", /* .gitmodules */ NULL, /* added */ NULL, /* ignored */ "diff --git a/modified b/modified\nindex 092bfb9..452216e 100644\n--- a/modified\n+++ b/modified\n@@ -1 +1,2 @@\n-yo\n+changed\n+\n", /* modified */ NULL, /* testrepo.git */ NULL, /* unmodified */ NULL, /* untracked */ "<END>" }; g_repo = setup_fixture_submodules(); opts.flags = GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_UNMODIFIED; opts.old_prefix = "a"; opts.new_prefix = "b"; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); git_diff_free(diff); } void test_diff_submodules__dirty_submodule(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; static const char *expected[] = { "<SKIP>", /* .gitmodules */ NULL, /* added */ NULL, /* ignored */ "diff --git a/modified b/modified\nindex 092bfb9..452216e 100644\n--- a/modified\n+++ b/modified\n@@ -1 +1,2 @@\n-yo\n+changed\n+\n", /* modified */ "diff --git a/testrepo b/testrepo\nindex a65fedf..a65fedf 160000\n--- a/testrepo\n+++ b/testrepo\n@@ -1 +1 @@\n-Subproject commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750\n+Subproject commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750-dirty\n", /* testrepo.git */ NULL, /* unmodified */ NULL, /* untracked */ "<END>" }; g_repo = setup_fixture_submodules(); cl_git_rewritefile("submodules/testrepo/README", "heyheyhey"); cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before"); opts.flags = GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_UNMODIFIED; opts.old_prefix = "a"; opts.new_prefix = "b"; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); git_diff_free(diff); } void test_diff_submodules__dirty_submodule_2(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL, *diff2 = NULL; char *smpath = "testrepo"; static const char *expected_none[] = { "<END>" }; static const char *expected_dirty[] = { "diff --git a/testrepo b/testrepo\nindex a65fedf..a65fedf 160000\n--- a/testrepo\n+++ b/testrepo\n@@ -1 +1 @@\n-Subproject commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750\n+Subproject commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750-dirty\n", /* testrepo.git */ "<END>" }; g_repo = setup_fixture_submodules(); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_SHOW_UNTRACKED_CONTENT | GIT_DIFF_RECURSE_UNTRACKED_DIRS | GIT_DIFF_DISABLE_PATHSPEC_MATCH; opts.old_prefix = "a"; opts.new_prefix = "b"; opts.pathspec.count = 1; opts.pathspec.strings = &smpath; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_none); git_diff_free(diff); cl_git_rewritefile("submodules/testrepo/README", "heyheyhey"); cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); { git_tree *head; cl_git_pass(git_repository_head_tree(&head, g_repo)); cl_git_pass(git_diff_tree_to_index(&diff2, g_repo, head, NULL, &opts)); cl_git_pass(git_diff_merge(diff, diff2)); git_diff_free(diff2); git_tree_free(head); check_diff_patches(diff, expected_dirty); } git_diff_free(diff); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); git_diff_free(diff); } void test_diff_submodules__submod2_index_to_wd(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; static const char *expected[] = { "<SKIP>", /* .gitmodules */ "<UNTRACKED>", /* not-submodule */ "<UNTRACKED>", /* not */ "diff --git a/sm_changed_file b/sm_changed_file\nindex 4800958..4800958 160000\n--- a/sm_changed_file\n+++ b/sm_changed_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_file */ "diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */ "<UNTRACKED>", /* sm_changed_head- */ "<UNTRACKED>", /* sm_changed_head_ */ "diff --git a/sm_changed_index b/sm_changed_index\nindex 4800958..4800958 160000\n--- a/sm_changed_index\n+++ b/sm_changed_index\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_index */ "diff --git a/sm_changed_untracked_file b/sm_changed_untracked_file\nindex 4800958..4800958 160000\n--- a/sm_changed_untracked_file\n+++ b/sm_changed_untracked_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_untracked_file */ "diff --git a/sm_missing_commits b/sm_missing_commits\nindex 4800958..5e49635 160000\n--- a/sm_missing_commits\n+++ b/sm_missing_commits\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 5e4963595a9774b90524d35a807169049de8ccad\n", /* sm_missing_commits */ "<END>" }; g_repo = setup_fixture_submod2(); /* bracket existing submodule with similarly named items */ cl_git_mkfile("submod2/sm_changed_head-", "hello"); cl_git_mkfile("submod2/sm_changed_head_", "hello"); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED; opts.old_prefix = "a"; opts.new_prefix = "b"; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); git_diff_free(diff); } void test_diff_submodules__submod2_head_to_index(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_tree *head; git_diff *diff = NULL; static const char *expected[] = { "<SKIP>", /* .gitmodules */ "diff --git a/sm_added_and_uncommited b/sm_added_and_uncommited\nnew file mode 160000\nindex 0000000..4800958\n--- /dev/null\n+++ b/sm_added_and_uncommited\n@@ -0,0 +1 @@\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n", /* sm_added_and_uncommited */ "<END>" }; g_repo = setup_fixture_submod2(); cl_git_pass(git_repository_head_tree(&head, g_repo)); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED; opts.old_prefix = "a"; opts.new_prefix = "b"; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, head, NULL, &opts)); check_diff_patches(diff, expected); git_diff_free(diff); git_tree_free(head); } void test_diff_submodules__invalid_cache(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; git_submodule *sm; char *smpath = "sm_changed_head"; git_repository *smrepo; git_index *smindex; static const char *expected_baseline[] = { "diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */ "<END>" }; static const char *expected_unchanged[] = { "<END>" }; static const char *expected_dirty[] = { "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247-dirty\n", "<END>" }; static const char *expected_moved[] = { "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae\n", "<END>" }; static const char *expected_moved_dirty[] = { "diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae-dirty\n", "<END>" }; g_repo = setup_fixture_submod2(); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED; opts.old_prefix = "a"; opts.new_prefix = "b"; opts.pathspec.count = 1; opts.pathspec.strings = &smpath; /* baseline */ cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_baseline); git_diff_free(diff); /* update index with new HEAD */ cl_git_pass(git_submodule_lookup(&sm, g_repo, smpath)); cl_git_pass(git_submodule_add_to_index(sm, 1)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); git_diff_free(diff); /* create untracked file in submodule working directory */ cl_git_mkfile("submod2/sm_changed_head/new_around_here", "hello"); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_NONE); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); git_diff_free(diff); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_UNTRACKED); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); git_diff_free(diff); /* modify tracked file in submodule working directory */ cl_git_append2file( "submod2/sm_changed_head/file_to_modify", "\nmore stuff\n"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); git_diff_free(diff); git_submodule_free(sm); cl_git_pass(git_submodule_lookup(&sm, g_repo, smpath)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); git_diff_free(diff); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_DIRTY); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); git_diff_free(diff); /* add file to index in submodule */ cl_git_pass(git_submodule_open(&smrepo, sm)); cl_git_pass(git_repository_index(&smindex, smrepo)); cl_git_pass(git_index_add_bypath(smindex, "file_to_modify")); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_UNTRACKED); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); git_diff_free(diff); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_DIRTY); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); git_diff_free(diff); /* commit changed index of submodule */ cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Move it"); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_DIRTY); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved); git_diff_free(diff); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_ALL); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); git_diff_free(diff); git_submodule_set_ignore(g_repo, git_submodule_name(sm), GIT_SUBMODULE_IGNORE_NONE); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved_dirty); git_diff_free(diff); p_unlink("submod2/sm_changed_head/new_around_here"); git_submodule_free(sm); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved); git_diff_free(diff); git_index_free(smindex); git_repository_free(smrepo); } void test_diff_submodules__diff_ignore_options(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; git_config *cfg; static const char *expected_normal[] = { "<SKIP>", /* .gitmodules */ "<UNTRACKED>", /* not-submodule */ "<UNTRACKED>", /* not */ "diff --git a/sm_changed_file b/sm_changed_file\nindex 4800958..4800958 160000\n--- a/sm_changed_file\n+++ b/sm_changed_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_file */ "diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */ "diff --git a/sm_changed_index b/sm_changed_index\nindex 4800958..4800958 160000\n--- a/sm_changed_index\n+++ b/sm_changed_index\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_index */ "diff --git a/sm_changed_untracked_file b/sm_changed_untracked_file\nindex 4800958..4800958 160000\n--- a/sm_changed_untracked_file\n+++ b/sm_changed_untracked_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_untracked_file */ "diff --git a/sm_missing_commits b/sm_missing_commits\nindex 4800958..5e49635 160000\n--- a/sm_missing_commits\n+++ b/sm_missing_commits\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 5e4963595a9774b90524d35a807169049de8ccad\n", /* sm_missing_commits */ "<END>" }; static const char *expected_ignore_all[] = { "<SKIP>", /* .gitmodules */ "<UNTRACKED>", /* not-submodule */ "<UNTRACKED>", /* not */ "<END>" }; static const char *expected_ignore_dirty[] = { "<SKIP>", /* .gitmodules */ "<UNTRACKED>", /* not-submodule */ "<UNTRACKED>", /* not */ "diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */ "diff --git a/sm_missing_commits b/sm_missing_commits\nindex 4800958..5e49635 160000\n--- a/sm_missing_commits\n+++ b/sm_missing_commits\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 5e4963595a9774b90524d35a807169049de8ccad\n", /* sm_missing_commits */ "<END>" }; g_repo = setup_fixture_submod2(); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED; opts.old_prefix = "a"; opts.new_prefix = "b"; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); git_diff_free(diff); opts.flags |= GIT_DIFF_IGNORE_SUBMODULES; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); git_diff_free(diff); opts.flags &= ~GIT_DIFF_IGNORE_SUBMODULES; opts.ignore_submodules = GIT_SUBMODULE_IGNORE_ALL; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); git_diff_free(diff); opts.ignore_submodules = GIT_SUBMODULE_IGNORE_DIRTY; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_dirty); git_diff_free(diff); opts.ignore_submodules = 0; cl_git_pass(git_repository_config(&cfg, g_repo)); cl_git_pass(git_config_set_bool(cfg, "diff.ignoreSubmodules", false)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); git_diff_free(diff); cl_git_pass(git_config_set_bool(cfg, "diff.ignoreSubmodules", true)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.ignoreSubmodules", "none")); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.ignoreSubmodules", "dirty")); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_dirty); git_diff_free(diff); git_config_free(cfg); } void test_diff_submodules__skips_empty_includes_used(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; diff_expects exp; /* A side effect of of Git's handling of untracked directories and * auto-ignoring of ".git" entries is that a newly initialized Git * repo inside another repo will be skipped by diff, but one that * actually has a commit it in will show as an untracked directory. * Let's make sure that works. */ g_repo = cl_git_sandbox_init("empty_standard_repo"); opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(0, exp.files); git_diff_free(diff); { git_repository *r2; cl_git_pass(git_repository_init(&r2, "empty_standard_repo/subrepo", 0)); git_repository_free(r2); } cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); git_diff_free(diff); cl_git_mkfile("empty_standard_repo/subrepo/README.txt", "hello\n"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); git_diff_free(diff); } static void ensure_submodules_found( git_repository *repo, const char **paths, size_t cnt) { git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const git_diff_delta *delta; size_t i, pathlen; opts.pathspec.strings = (char **)paths; opts.pathspec.count = cnt; git_diff_index_to_workdir(&diff, repo, NULL, &opts); cl_assert_equal_i(cnt, git_diff_num_deltas(diff)); for (i = 0; i < cnt; i++) { delta = git_diff_get_delta(diff, i); /* ensure that the given path is returned w/o trailing slashes. */ pathlen = strlen(opts.pathspec.strings[i]); while (pathlen && opts.pathspec.strings[i][pathlen - 1] == '/') pathlen--; cl_assert_equal_strn(opts.pathspec.strings[i], delta->new_file.path, pathlen); } git_diff_free(diff); } void test_diff_submodules__can_be_identified_by_trailing_slash_in_pathspec(void) { const char *one_path_without_slash[] = { "sm_changed_head" }; const char *one_path_with_slash[] = { "sm_changed_head/" }; const char *many_paths_without_slashes[] = { "sm_changed_head", "sm_changed_index" }; const char *many_paths_with_slashes[] = { "sm_changed_head/", "sm_changed_index/" }; g_repo = setup_fixture_submod2(); ensure_submodules_found(g_repo, one_path_without_slash, ARRAY_SIZE(one_path_without_slash)); ensure_submodules_found(g_repo, one_path_with_slash, ARRAY_SIZE(one_path_with_slash)); ensure_submodules_found(g_repo, many_paths_without_slashes, ARRAY_SIZE(many_paths_without_slashes)); ensure_submodules_found(g_repo, many_paths_with_slashes, ARRAY_SIZE(many_paths_with_slashes)); }
libgit2-main
tests/libgit2/diff/submodules.c
#include "clar_libgit2.h" #include "git2/sys/diff.h" #include "delta.h" #include "filebuf.h" #include "repository.h" static git_repository *repo; void test_diff_binary__initialize(void) { } void test_diff_binary__cleanup(void) { cl_git_sandbox_cleanup(); } static void test_patch( const char *one, const char *two, const git_diff_options *opts, const char *expected) { git_oid id_one, id_two; git_index *index = NULL; git_commit *commit_one, *commit_two = NULL; git_tree *tree_one, *tree_two; git_diff *diff; git_patch *patch; git_buf actual = GIT_BUF_INIT; cl_git_pass(git_oid__fromstr(&id_one, one, GIT_OID_SHA1)); cl_git_pass(git_commit_lookup(&commit_one, repo, &id_one)); cl_git_pass(git_commit_tree(&tree_one, commit_one)); if (two) { cl_git_pass(git_oid__fromstr(&id_two, two, GIT_OID_SHA1)); cl_git_pass(git_commit_lookup(&commit_two, repo, &id_two)); cl_git_pass(git_commit_tree(&tree_two, commit_two)); } else { cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_index_write_tree(&id_two, index)); cl_git_pass(git_tree_lookup(&tree_two, repo, &id_two)); } cl_git_pass(git_diff_tree_to_tree(&diff, repo, tree_one, tree_two, opts)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected, actual.ptr); git_buf_dispose(&actual); cl_git_pass(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, git_diff_print_callback__to_buf, &actual)); cl_assert_equal_s(expected, actual.ptr); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); git_tree_free(tree_one); git_tree_free(tree_two); git_commit_free(commit_one); git_commit_free(commit_two); git_index_free(index); } void test_diff_binary__add_normal(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "new file mode 100644\n" \ "index 0000000..bd474b2\n" \ "Binary files /dev/null and b/binary.bin differ\n"; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "873806f6f27e631eb0b23e4b56bea2bfac14a373", "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", &opts, expected); } void test_diff_binary__add(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "new file mode 100644\n" \ "index 0000000000000000000000000000000000000000..bd474b2519cc15eab801ff851cc7d50f0dee49a1\n" \ "GIT binary patch\n" \ "literal 3\n" \ "Kc${Nk-~s>u4FC%O\n" "\n" \ "literal 0\n" \ "Hc$@<O00001\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "873806f6f27e631eb0b23e4b56bea2bfac14a373", "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", &opts, expected); } void test_diff_binary__modify_normal(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "index bd474b2..9ac35ff 100644\n" \ "Binary files a/binary.bin and b/binary.bin differ\n"; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts, expected); } void test_diff_binary__modify(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \ "GIT binary patch\n" \ "literal 5\n" \ "Mc${NkU}WL~000&M4gdfE\n" \ "\n" \ "literal 3\n" \ "Kc${Nk-~s>u4FC%O\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts, expected); } void test_diff_binary__delete_normal(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "deleted file mode 100644\n" \ "index bd474b2..0000000\n" \ "Binary files a/binary.bin and /dev/null differ\n"; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", "873806f6f27e631eb0b23e4b56bea2bfac14a373", &opts, expected); } void test_diff_binary__delete(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/binary.bin b/binary.bin\n" \ "deleted file mode 100644\n" \ "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..0000000000000000000000000000000000000000\n" \ "GIT binary patch\n" \ "literal 0\n" \ "Hc$@<O00001\n" \ "\n" \ "literal 3\n" \ "Kc${Nk-~s>u4FC%O\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("diff_format_email"); test_patch( "897d3af16ca9e420cd071b1c4541bd2b91d04c8c", "873806f6f27e631eb0b23e4b56bea2bfac14a373", &opts, expected); } void test_diff_binary__delta(void) { git_index *index; git_str contents = GIT_STR_INIT; size_t i; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/songof7cities.txt b/songof7cities.txt\n" \ "index 4210ffd5c390b21dd5483375e75288dea9ede512..cc84ec183351c9944ed90a619ca08911924055b5 100644\n" \ "GIT binary patch\n" \ "delta 198\n" \ "zc$}LmI8{(0BqLQJI6p64AwNwaIJGP_Pa)Ye#M3o+qJ$<Jl;sX*mF<MGCYv&*L7AHu\n" \ "zGA1*^gt?gYVN82wTbPO_W)+x<&1+cP;HrPHR>PQ;Y(X&QMK*C5^Br3bjG4d=XI^5@\n" \ "JfH567LIG)KJdFSV\n" \ "\n" \ "delta 198\n" \ "zc$}LmI8{(0BqLQJI6p64AwNwaIJGP_Pr*5}Br~;mqJ$<Jl;sX*mF<MGCYv&*L7AHu\n" \ "zGA1*^gt?gYVN82wTbPO_W)+x<&1+cP;HrPHR>PQ;Y(X&QMK*C5^Br3bjG4d=XI^5@\n" \ "JfH567LIF3FM2!Fd\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_repository_index(&index, repo)); cl_git_pass(git_futils_readbuffer(&contents, "renames/songof7cities.txt")); for (i = 0; i < contents.size - 6; i++) { if (strncmp(&contents.ptr[i], "Cities", 6) == 0) memcpy(&contents.ptr[i], "cITIES", 6); } cl_git_rewritefile("renames/songof7cities.txt", contents.ptr); cl_git_pass(git_index_add_bypath(index, "songof7cities.txt")); cl_git_pass(git_index_write(index)); test_patch( "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13", NULL, &opts, expected); git_index_free(index); git_str_dispose(&contents); } void test_diff_binary__delta_append(void) { git_index *index; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/untimely.txt b/untimely.txt\n" \ "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \ "GIT binary patch\n" \ "delta 32\n" \ "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \ "\n" \ "delta 7\n" \ "Oc%18D`@*{63ljhg(E~C7\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_repository_index(&index, repo)); cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n"); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_index_write(index)); test_patch( "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13", NULL, &opts, expected); git_index_free(index); } void test_diff_binary__empty_for_no_diff(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_oid id; git_commit *commit; git_tree *tree; git_diff *diff; git_str actual = GIT_STR_INIT; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_oid__fromstr(&id, "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13", GIT_OID_SHA1)); cl_git_pass(git_commit_lookup(&commit, repo, &id)); cl_git_pass(git_commit_tree(&tree, commit)); cl_git_pass(git_diff_tree_to_tree(&diff, repo, tree, tree, &opts)); cl_git_pass(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, git_diff_print_callback__to_buf, &actual)); cl_assert_equal_s("", actual.ptr); git_str_dispose(&actual); git_diff_free(diff); git_commit_free(commit); git_tree_free(tree); } void test_diff_binary__index_to_workdir(void) { git_index *index; git_diff *diff; git_patch *patch; git_buf actual = GIT_BUF_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/untimely.txt b/untimely.txt\n" \ "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \ "GIT binary patch\n" \ "delta 32\n" \ "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \ "\n" \ "delta 7\n" \ "Oc%18D`@*{63ljhg(E~C7\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_repository_index(&index, repo)); cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n"); cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, &opts)); cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_buf(&actual, patch)); cl_assert_equal_s(expected, actual.ptr); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_index_write(index)); test_patch( "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13", NULL, &opts, expected); git_buf_dispose(&actual); git_patch_free(patch); git_diff_free(diff); git_index_free(index); } static int print_cb( const git_diff_delta *delta, const git_diff_hunk *hunk, const git_diff_line *line, void *payload) { git_str *buf = (git_str *)payload; GIT_UNUSED(delta); if (hunk) git_str_put(buf, hunk->header, hunk->header_len); if (line) git_str_put(buf, line->content, line->content_len); return git_str_oom(buf) ? -1 : 0; } void test_diff_binary__print_patch_from_diff(void) { git_index *index; git_diff *diff; git_str actual = GIT_STR_INIT; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const char *expected = "diff --git a/untimely.txt b/untimely.txt\n" \ "index 9a69d960ae94b060f56c2a8702545e2bb1abb935..1111d4f11f4b35bf6759e0fb714fe09731ef0840 100644\n" \ "GIT binary patch\n" \ "delta 32\n" \ "nc%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW\n" \ "\n" \ "delta 7\n" \ "Oc%18D`@*{63ljhg(E~C7\n" \ "\n"; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_repository_index(&index, repo)); cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n"); cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, &opts)); cl_git_pass(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, print_cb, &actual)); cl_assert_equal_s(expected, actual.ptr); git_str_dispose(&actual); git_diff_free(diff); git_index_free(index); } struct diff_data { char *old_path; git_oid old_id; git_str old_binary_base85; size_t old_binary_inflatedlen; git_diff_binary_t old_binary_type; char *new_path; git_oid new_id; git_str new_binary_base85; size_t new_binary_inflatedlen; git_diff_binary_t new_binary_type; }; static int file_cb( const git_diff_delta *delta, float progress, void *payload) { struct diff_data *diff_data = payload; GIT_UNUSED(progress); if (delta->old_file.path) diff_data->old_path = git__strdup(delta->old_file.path); if (delta->new_file.path) diff_data->new_path = git__strdup(delta->new_file.path); git_oid_cpy(&diff_data->old_id, &delta->old_file.id); git_oid_cpy(&diff_data->new_id, &delta->new_file.id); return 0; } static int binary_cb( const git_diff_delta *delta, const git_diff_binary *binary, void *payload) { struct diff_data *diff_data = payload; GIT_UNUSED(delta); git_str_encode_base85(&diff_data->old_binary_base85, binary->old_file.data, binary->old_file.datalen); diff_data->old_binary_inflatedlen = binary->old_file.inflatedlen; diff_data->old_binary_type = binary->old_file.type; git_str_encode_base85(&diff_data->new_binary_base85, binary->new_file.data, binary->new_file.datalen); diff_data->new_binary_inflatedlen = binary->new_file.inflatedlen; diff_data->new_binary_type = binary->new_file.type; return 0; } static int hunk_cb( const git_diff_delta *delta, const git_diff_hunk *hunk, void *payload) { GIT_UNUSED(delta); GIT_UNUSED(hunk); GIT_UNUSED(payload); cl_fail("did not expect hunk callback"); return 0; } static int line_cb( const git_diff_delta *delta, const git_diff_hunk *hunk, const git_diff_line *line, void *payload) { GIT_UNUSED(delta); GIT_UNUSED(hunk); GIT_UNUSED(line); GIT_UNUSED(payload); cl_fail("did not expect line callback"); return 0; } void test_diff_binary__blob_to_blob(void) { git_index *index; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_blob *old_blob, *new_blob; git_oid old_id, new_id; struct diff_data diff_data = {0}; opts.flags = GIT_DIFF_SHOW_BINARY | GIT_DIFF_FORCE_BINARY; opts.id_abbrev = GIT_OID_SHA1_HEXSIZE; repo = cl_git_sandbox_init("renames"); cl_git_pass(git_repository_index__weakptr(&index, repo)); cl_git_append2file("renames/untimely.txt", "Oh that crazy Kipling!\r\n"); cl_git_pass(git_index_add_bypath(index, "untimely.txt")); cl_git_pass(git_index_write(index)); git_oid__fromstr(&old_id, "9a69d960ae94b060f56c2a8702545e2bb1abb935", GIT_OID_SHA1); git_oid__fromstr(&new_id, "1111d4f11f4b35bf6759e0fb714fe09731ef0840", GIT_OID_SHA1); cl_git_pass(git_blob_lookup(&old_blob, repo, &old_id)); cl_git_pass(git_blob_lookup(&new_blob, repo, &new_id)); cl_git_pass(git_diff_blobs(old_blob, "untimely.txt", new_blob, "untimely.txt", &opts, file_cb, binary_cb, hunk_cb, line_cb, &diff_data)); cl_assert_equal_s("untimely.txt", diff_data.old_path); cl_assert_equal_oid(&old_id, &diff_data.old_id); cl_assert_equal_i(GIT_DIFF_BINARY_DELTA, diff_data.old_binary_type); cl_assert_equal_i(7, diff_data.old_binary_inflatedlen); cl_assert_equal_s("c%18D`@*{63ljhg(E~C7", diff_data.old_binary_base85.ptr); cl_assert_equal_s("untimely.txt", diff_data.new_path); cl_assert_equal_oid(&new_id, &diff_data.new_id); cl_assert_equal_i(GIT_DIFF_BINARY_DELTA, diff_data.new_binary_type); cl_assert_equal_i(32, diff_data.new_binary_inflatedlen); cl_assert_equal_s("c%1vf+QYWt3zLL@hC)e3Vu?a>QDRl4f_G*?PG(-ZA}<#J$+QbW", diff_data.new_binary_base85.ptr); git_blob_free(old_blob); git_blob_free(new_blob); git__free(diff_data.old_path); git__free(diff_data.new_path); git_str_dispose(&diff_data.old_binary_base85); git_str_dispose(&diff_data.new_binary_base85); }
libgit2-main
tests/libgit2/diff/binary.c
#include "clar.h" #include "clar_libgit2.h" #include "commit.h" #include "diff.h" #include "diff_generate.h" static git_repository *_repo; static git_diff_stats *_stats; void test_diff_stats__initialize(void) { _repo = cl_git_sandbox_init("diff_format_email"); } void test_diff_stats__cleanup(void) { git_diff_stats_free(_stats); _stats = NULL; cl_git_sandbox_cleanup(); } static void diff_stats_from_commit_oid( git_diff_stats **stats, const char *oidstr, bool rename) { git_oid oid; git_commit *commit; git_diff *diff; git_oid__fromstr(&oid, oidstr, GIT_OID_SHA1); cl_git_pass(git_commit_lookup(&commit, _repo, &oid)); cl_git_pass(git_diff__commit(&diff, _repo, commit, NULL)); if (rename) cl_git_pass(git_diff_find_similar(diff, NULL)); cl_git_pass(git_diff_get_stats(stats, diff)); git_diff_free(diff); git_commit_free(commit); } void test_diff_stats__stat(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file1.txt | 8 +++++---\n" \ " 1 file changed, 5 insertions(+), 3 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(5, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(3, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert(strcmp(buf.ptr, stat) == 0); git_buf_dispose(&buf); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 80)); cl_assert(strcmp(buf.ptr, stat) == 0); git_buf_dispose(&buf); } void test_diff_stats__multiple_hunks(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt | 5 +++--\n" \ " file3.txt | 6 ++++--\n" \ " 2 files changed, 7 insertions(+), 4 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false); cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(7, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(4, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__numstat(void) { git_buf buf = GIT_BUF_INIT; const char *stat = "3 2 file2.txt\n" "4 2 file3.txt\n"; diff_stats_from_commit_oid( &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__shortstat(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " 1 file changed, 5 insertions(+), 3 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(5, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(3, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__shortstat_noinsertions(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " 1 file changed, 2 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "06b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6", false); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(0, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(2, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__shortstat_nodeletions(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " 1 file changed, 3 insertions(+)\n"; diff_stats_from_commit_oid( &_stats, "5219b9784f9a92d7bd7cb567a6d6a21bfb86697e", false); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(3, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(0, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt => file2.txt.renamed | 1 +\n" " file3.txt => file3.txt.renamed | 4 +++-\n" " 2 files changed, 4 insertions(+), 1 deletion(-)\n"; diff_stats_from_commit_oid( &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true); cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(4, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(1, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_nochanges(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt.renamed => file2.txt.renamed2 | 0\n" " file3.txt.renamed => file3.txt.renamed2 | 0\n" " 2 files changed, 0 insertions(+), 0 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true); cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(0, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(0, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_and_modifiy(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt.renamed2 | 2 +-\n" " file3.txt.renamed2 => file3.txt.renamed | 0\n" " 2 files changed, 1 insertion(+), 1 deletion(-)\n"; diff_stats_from_commit_oid( &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true); cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(1, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(1, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_in_subdirectory(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " dir/{orig.txt => renamed.txt} | 0\n" " 1 file changed, 0 insertions(+), 0 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "0db2a262bc8c5c3cba55254730045a8258da7a37", true); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(0, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(0, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_no_find(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt | 5 -----\n" " file2.txt.renamed | 6 ++++++\n" " file3.txt | 5 -----\n" " file3.txt.renamed | 7 +++++++\n" " 4 files changed, 13 insertions(+), 10 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false); cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(13, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(10, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_nochanges_no_find(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt.renamed | 6 ------\n" " file2.txt.renamed2 | 6 ++++++\n" " file3.txt.renamed | 7 -------\n" " file3.txt.renamed2 | 7 +++++++\n" " 4 files changed, 13 insertions(+), 13 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false); cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(13, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(13, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__rename_and_modify_no_find(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file2.txt.renamed2 | 2 +-\n" " file3.txt.renamed | 7 +++++++\n" " file3.txt.renamed2 | 7 -------\n" " 3 files changed, 8 insertions(+), 8 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false); cl_assert_equal_sz(3, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(8, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(8, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__binary(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " binary.bin | Bin 3 -> 5 bytes\n" " 1 file changed, 0 insertions(+), 0 deletions(-)\n"; diff_stats_from_commit_oid( &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false); cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats)); cl_assert_equal_sz(0, git_diff_stats_insertions(_stats)); cl_assert_equal_sz(0, git_diff_stats_deletions(_stats)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__binary_numstat(void) { git_buf buf = GIT_BUF_INIT; const char *stat = "- - binary.bin\n"; diff_stats_from_commit_oid( &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__mode_change(void) { git_buf buf = GIT_BUF_INIT; const char *stat = " file1.txt.renamed | 0\n" \ " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \ " mode change 100644 => 100755 file1.txt.renamed\n"; diff_stats_from_commit_oid( &_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); } void test_diff_stats__new_file(void) { git_diff *diff; git_buf buf = GIT_BUF_INIT; const char *input = "---\n" " Gurjeet Singh | 1 +\n" " 1 file changed, 1 insertion(+)\n" " create mode 100644 Gurjeet Singh\n" "\n" "diff --git a/Gurjeet Singh b/Gurjeet Singh\n" "new file mode 100644\n" "index 0000000..6d0ecfd\n" "--- /dev/null\n" "+++ b/Gurjeet Singh \n" "@@ -0,0 +1 @@\n" "+I'm about to try git send-email\n" "-- \n" "2.21.0\n"; const char *stat = " Gurjeet Singh | 1 +\n" " 1 file changed, 1 insertion(+)\n" " create mode 100644 Gurjeet Singh\n"; cl_git_pass(git_diff_from_buffer(&diff, input, strlen(input))); cl_git_pass(git_diff_get_stats(&_stats, diff)); cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0)); cl_assert_equal_s(stat, buf.ptr); git_buf_dispose(&buf); git_diff_free(diff); }
libgit2-main
tests/libgit2/diff/stats.c
#include "clar_libgit2.h" #include "../checkout/checkout_helpers.h" #include "index.h" #include "repository.h" static git_repository *g_repo; void test_diff_externalmodifications__initialize(void) { g_repo = cl_git_sandbox_init("testrepo2"); } void test_diff_externalmodifications__cleanup(void) { cl_git_sandbox_cleanup(); g_repo = NULL; } void test_diff_externalmodifications__file_becomes_smaller(void) { git_index *index; git_diff *diff; git_patch* patch; git_str path = GIT_STR_INIT; char big_string[500001]; cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README")); /* Modify the file with a large string */ memset(big_string, '\n', sizeof(big_string) - 1); big_string[sizeof(big_string) - 1] = '\0'; cl_git_mkfile(path.ptr, big_string); /* Get a diff */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); cl_assert_equal_i(500000, git_diff_get_delta(diff, 0)->new_file.size); /* Simulate file modification after we've gotten the diff. * Write a shorter string to ensure that we don't mmap 500KB from * the previous revision, which would most likely crash. */ cl_git_mkfile(path.ptr, "hello"); /* Attempt to get a patch */ cl_git_fail(git_patch_from_diff(&patch, diff, 0)); git_index_free(index); git_diff_free(diff); git_str_dispose(&path); } void test_diff_externalmodifications__file_becomes_empty(void) { git_index *index; git_diff *diff; git_patch* patch; git_str path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README")); /* Modify the file */ cl_git_mkfile(path.ptr, "hello"); /* Get a diff */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); cl_assert_equal_i(5, git_diff_get_delta(diff, 0)->new_file.size); /* Empty out the file after we've gotten the diff */ cl_git_mkfile(path.ptr, ""); /* Attempt to get a patch */ cl_git_fail(git_patch_from_diff(&patch, diff, 0)); git_index_free(index); git_diff_free(diff); git_str_dispose(&path); } void test_diff_externalmodifications__file_deleted(void) { git_index *index; git_diff *diff; git_patch* patch; git_str path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README")); /* Get a diff */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(0, git_diff_num_deltas(diff)); /* Delete the file */ cl_git_rmfile(path.ptr); /* Attempt to get a patch */ cl_git_fail(git_patch_from_diff(&patch, diff, 0)); git_index_free(index); git_diff_free(diff); git_str_dispose(&path); } void test_diff_externalmodifications__empty_file_becomes_non_empty(void) { git_index *index; git_diff *diff; git_patch* patch; git_str path = GIT_STR_INIT; cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README")); /* Empty out the file */ cl_git_mkfile(path.ptr, ""); /* Get a diff */ cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); cl_assert_equal_i(0, git_diff_get_delta(diff, 0)->new_file.size); /* Simulate file modification after we've gotten the diff */ cl_git_mkfile(path.ptr, "hello"); cl_git_fail(git_patch_from_diff(&patch, diff, 0)); git_index_free(index); git_diff_free(diff); git_str_dispose(&path); }
libgit2-main
tests/libgit2/diff/externalmodifications.c
/* This test exercises the problem described in ** https://github.com/libgit2/libgit2/pull/3568 ** where deleting a directory during a diff/status ** operation can cause an access violation. ** ** The "test_diff_racediffiter__basic() test confirms ** the normal operation of diff on the given repo. ** ** The "test_diff_racediffiter__racy_rmdir() test ** uses the new diff progress callback to delete ** a directory (after the initial readdir() and ** before the directory itself is visited) causing ** the recursion and iteration to fail. */ #include "clar_libgit2.h" #include "diff_helpers.h" #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) void test_diff_racediffiter__initialize(void) { } void test_diff_racediffiter__cleanup(void) { cl_git_sandbox_cleanup(); } typedef struct { const char *path; git_delta_t t; } basic_payload; static int notify_cb__basic( const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload) { basic_payload *exp = (basic_payload *)payload; basic_payload *e; GIT_UNUSED(diff_so_far); GIT_UNUSED(matched_pathspec); for (e = exp; e->path; e++) { if (strcmp(e->path, delta_to_add->new_file.path) == 0) { cl_assert_equal_i(e->t, delta_to_add->status); return 0; } } cl_assert(0); return GIT_ENOTFOUND; } void test_diff_racediffiter__basic(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_repository *repo = cl_git_sandbox_init("diff"); git_diff *diff; basic_payload exp_a[] = { { "another.txt", GIT_DELTA_MODIFIED }, { "readme.txt", GIT_DELTA_MODIFIED }, { "zzzzz/", GIT_DELTA_IGNORED }, { NULL, 0 } }; cl_must_pass(p_mkdir("diff/zzzzz", 0777)); opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; opts.notify_cb = notify_cb__basic; opts.payload = exp_a; cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); git_diff_free(diff); } typedef struct { bool first_time; const char *dir; basic_payload *basic_payload; } racy_payload; static int notify_cb__racy_rmdir( const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload) { racy_payload *pay = (racy_payload *)payload; if (pay->first_time) { cl_must_pass(p_rmdir(pay->dir)); pay->first_time = false; } return notify_cb__basic(diff_so_far, delta_to_add, matched_pathspec, pay->basic_payload); } void test_diff_racediffiter__racy(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_repository *repo = cl_git_sandbox_init("diff"); git_diff *diff; basic_payload exp_a[] = { { "another.txt", GIT_DELTA_MODIFIED }, { "readme.txt", GIT_DELTA_MODIFIED }, { NULL, 0 } }; racy_payload pay = { true, "diff/zzzzz", exp_a }; cl_must_pass(p_mkdir("diff/zzzzz", 0777)); opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; opts.notify_cb = notify_cb__racy_rmdir; opts.payload = &pay; cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, &opts)); git_diff_free(diff); }
libgit2-main
tests/libgit2/diff/racediffiter.c
#include "clar_libgit2.h" #include "diff_helpers.h" static git_repository *g_repo = NULL; void test_diff_index__initialize(void) { g_repo = cl_git_sandbox_init("status"); } void test_diff_index__cleanup(void) { cl_git_sandbox_cleanup(); } void test_diff_index__0(void) { /* grabbed a couple of commit oids from the history of the attr repo */ const char *a_commit = "26a125ee1bf"; /* the current HEAD */ const char *b_commit = "0017bd4ab1ec3"; /* the start */ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; diff_expects exp; cl_assert(a); cl_assert(b); opts.context_lines = 1; opts.interhunk_lines = 1; memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); /* to generate these values: * - cd to tests/resources/status, * - mv .gitted .git * - git diff --name-status --cached 26a125ee1bf * - git diff -U1 --cached 26a125ee1bf * - mv .git .gitted */ cl_assert_equal_i(8, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(8, exp.hunks); cl_assert_equal_i(11, exp.lines); cl_assert_equal_i(3, exp.line_ctxt); cl_assert_equal_i(6, exp.line_adds); cl_assert_equal_i(2, exp.line_dels); git_diff_free(diff); diff = NULL; memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); /* to generate these values: * - cd to tests/resources/status, * - mv .gitted .git * - git diff --name-status --cached 0017bd4ab1ec3 * - git diff -U1 --cached 0017bd4ab1ec3 * - mv .git .gitted */ cl_assert_equal_i(12, exp.files); cl_assert_equal_i(7, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(12, exp.hunks); cl_assert_equal_i(16, exp.lines); cl_assert_equal_i(3, exp.line_ctxt); cl_assert_equal_i(11, exp.line_adds); cl_assert_equal_i(2, exp.line_dels); git_diff_free(diff); diff = NULL; git_tree_free(a); git_tree_free(b); } static int diff_stop_after_2_files( const git_diff_delta *delta, float progress, void *payload) { diff_expects *e = payload; GIT_UNUSED(progress); GIT_UNUSED(delta); e->files++; return (e->files == 2); } void test_diff_index__1(void) { /* grabbed a couple of commit oids from the history of the attr repo */ const char *a_commit = "26a125ee1bf"; /* the current HEAD */ const char *b_commit = "0017bd4ab1ec3"; /* the start */ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; diff_expects exp; cl_assert(a); cl_assert(b); opts.context_lines = 1; opts.interhunk_lines = 1; memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); cl_assert_equal_i(1, git_diff_foreach( diff, diff_stop_after_2_files, NULL, NULL, NULL, &exp) ); cl_assert_equal_i(2, exp.files); git_diff_free(diff); diff = NULL; git_tree_free(a); git_tree_free(b); } void test_diff_index__checks_options_version(void) { const char *a_commit = "26a125ee1bf"; git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff *diff = NULL; const git_error *err; opts.version = 0; cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); cl_assert_equal_p(diff, NULL); git_error_clear(); opts.version = 1024; cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); err = git_error_last(); cl_assert_equal_i(GIT_ERROR_INVALID, err->klass); cl_assert_equal_p(diff, NULL); git_tree_free(a); } static void do_conflicted_diff(diff_expects *exp, unsigned long flags) { const char *a_commit = "26a125ee1bf"; /* the current HEAD */ git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}}; git_diff *diff = NULL; git_index *index; cl_assert(a); opts.context_lines = 1; opts.interhunk_lines = 1; opts.flags |= flags; memset(exp, 0, sizeof(diff_expects)); cl_git_pass(git_repository_index(&index, g_repo)); ancestor.path = ours.path = theirs.path = "staged_changes"; ancestor.mode = ours.mode = theirs.mode = GIT_FILEMODE_BLOB; git_oid__fromstr(&ancestor.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1); git_oid__fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1); git_oid__fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1); cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, &opts)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, exp)); git_diff_free(diff); git_tree_free(a); git_index_free(index); } void test_diff_index__reports_conflicts(void) { diff_expects exp; do_conflicted_diff(&exp, 0); cl_assert_equal_i(8, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]); cl_assert_equal_i(7, exp.hunks); cl_assert_equal_i(9, exp.lines); cl_assert_equal_i(2, exp.line_ctxt); cl_assert_equal_i(5, exp.line_adds); cl_assert_equal_i(2, exp.line_dels); } void test_diff_index__reports_conflicts_when_reversed(void) { diff_expects exp; do_conflicted_diff(&exp, GIT_DIFF_REVERSE); cl_assert_equal_i(8, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]); cl_assert_equal_i(7, exp.hunks); cl_assert_equal_i(9, exp.lines); cl_assert_equal_i(2, exp.line_ctxt); cl_assert_equal_i(2, exp.line_adds); cl_assert_equal_i(5, exp.line_dels); } void test_diff_index__not_in_head_conflicted(void) { const char *a_commit = "26a125ee1bf"; /* the current HEAD */ git_index_entry theirs = {{0}}; git_index *index; git_diff *diff; const git_diff_delta *delta; git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_read_tree(index, a)); theirs.path = "file_not_in_head"; theirs.mode = GIT_FILEMODE_BLOB; git_oid__fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1); cl_git_pass(git_index_conflict_add(index, NULL, NULL, &theirs)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, NULL)); cl_assert_equal_i(git_diff_num_deltas(diff), 1); delta = git_diff_get_delta(diff, 0); cl_assert_equal_i(delta->status, GIT_DELTA_CONFLICTED); git_diff_free(diff); git_index_free(index); git_tree_free(a); } void test_diff_index__to_index(void) { const char *a_commit = "26a125ee1bf"; /* the current HEAD */ git_tree *old_tree; git_index *old_index; git_index *new_index; git_diff *diff; diff_expects exp; cl_git_pass(git_index_new(&old_index)); old_tree = resolve_commit_oid_to_tree(g_repo, a_commit); cl_git_pass(git_index_read_tree(old_index, old_tree)); cl_git_pass(git_repository_index(&new_index, g_repo)); cl_git_pass(git_diff_index_to_index(&diff, g_repo, old_index, new_index, NULL)); memset(&exp, 0, sizeof(diff_expects)); cl_git_pass(git_diff_foreach( diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp)); cl_assert_equal_i(8, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(0, exp.file_status[GIT_DELTA_CONFLICTED]); git_diff_free(diff); git_index_free(new_index); git_index_free(old_index); git_tree_free(old_tree); }
libgit2-main
tests/libgit2/diff/index.c