void test_config_write__preserves_whitespace_and_comments(void) { const char *file_name = "config-duplicate-header"; const char *n; git_config *cfg; git_buf newfile = GIT_BUF_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_buf_free(&newfile); git_config_free(cfg); }
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__automerge_from_index(void) { git_merge_file_result result = {0}; git_index_entry ancestor, ours, theirs; git_oid_fromstr(&ancestor.id, "6212c31dab5e482247d7977e4f0dd3601decf13b"); ancestor.path = "automergeable.txt"; ancestor.mode = 0100644; git_oid_fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); ours.path = "automergeable.txt"; ours.mode = 0100755; git_oid_fromstr(&theirs.id, "058541fc37114bfc1dddf6bd6bffc7fae5c2e6fe"); 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_checkout_tree__caches_attributes_during_checkout(void) { git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid oid; git_object *obj = NULL; git_buf ident1 = GIT_BUF_INIT, ident2 = GIT_BUF_INIT; char *ident_paths[] = { "ident1.txt", "ident2.txt" }; opts.progress_cb = update_attr_callback; assert_on_branch(g_repo, "master"); opts.checkout_strategy = GIT_CHECKOUT_FORCE; opts.paths.strings = ident_paths; opts.paths.count = 2; cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident")); cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY)); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt")); cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt")); cl_assert_equal_strn(ident1.ptr, "# $Id$", 6); cl_assert_equal_strn(ident2.ptr, "# $Id$", 6); cl_git_pass(git_checkout_tree(g_repo, obj, &opts)); cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt")); cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt")); cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7); cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7); git_buf_free(&ident1); git_buf_free(&ident2); git_object_free(obj); }
static void assert_name_is(const char *expected) { char *actual; size_t actual_len, expected_len, start; cl_assert(actual = get_filename(expected)); expected_len = strlen(expected); actual_len = strlen(actual); cl_assert(actual_len >= expected_len); start = actual_len - expected_len; cl_assert_equal_s(expected, actual + start); if (start) cl_assert_equal_strn("/", actual + (start - 1), 1); free(actual); }
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__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); }