Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/diff/drivers.c')
-rw-r--r--tests/diff/drivers.c141
1 files changed, 115 insertions, 26 deletions
diff --git a/tests/diff/drivers.c b/tests/diff/drivers.c
index fbd1dff81..8b12368ea 100644
--- a/tests/diff/drivers.c
+++ b/tests/diff/drivers.c
@@ -15,6 +15,23 @@ void test_diff_drivers__cleanup(void)
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;
@@ -22,7 +39,7 @@ void test_diff_drivers__patterns(void)
git_tree *one;
git_diff *diff;
git_patch *patch;
- char *text;
+ 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";
@@ -45,10 +62,10 @@ void test_diff_drivers__patterns(void)
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_str(&text, patch));
- cl_assert_equal_s(expected0, text);
+ cl_git_pass(git_patch_to_buf(&actual, patch));
+ cl_assert_equal_s(expected0, actual.ptr);
- git__free(text);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
@@ -60,10 +77,10 @@ void test_diff_drivers__patterns(void)
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_str(&text, patch));
- cl_assert_equal_s(expected1, text);
+ cl_git_pass(git_patch_to_buf(&actual, patch));
+ cl_assert_equal_s(expected1, actual.ptr);
- git__free(text);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
@@ -75,10 +92,10 @@ void test_diff_drivers__patterns(void)
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_str(&text, patch));
- cl_assert_equal_s(expected0, text);
+ cl_git_pass(git_patch_to_buf(&actual, patch));
+ cl_assert_equal_s(expected0, actual.ptr);
- git__free(text);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
@@ -92,10 +109,10 @@ void test_diff_drivers__patterns(void)
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_str(&text, patch));
- cl_assert_equal_s(expected1, text);
+ cl_git_pass(git_patch_to_buf(&actual, patch));
+ cl_assert_equal_s(expected1, actual.ptr);
- git__free(text);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
@@ -106,17 +123,17 @@ void test_diff_drivers__patterns(void)
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"));
+ 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_str(&text, patch));
- cl_assert_equal_s(expected2, text);
+ cl_git_pass(git_patch_to_buf(&actual, patch));
+ cl_assert_equal_s(expected2, actual.ptr);
- git__free(text);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
@@ -129,7 +146,7 @@ void test_diff_drivers__long_lines(void)
git_index *idx;
git_diff *diff;
git_patch *patch;
- char *actual;
+ 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");
@@ -145,19 +162,91 @@ void test_diff_drivers__long_lines(void)
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_str(&actual, patch));
+ cl_git_pass(git_patch_to_buf(&actual, patch));
/* if chmod not supported, overwrite mode bits since anything is possible */
- if (!cl_is_chmod_supported()) {
- size_t actual_len = strlen(actual);
- if (actual_len > 72 && memcmp(&actual[66], "100644", 6) != 0)
- memcpy(&actual[66], "100644", 6);
- }
+ overwrite_filemode(expected, &actual);
- cl_assert_equal_s(expected, actual);
+ cl_assert_equal_s(expected, actual.ptr);
- free(actual);
+ git_buf_free(&actual);
git_patch_free(patch);
git_diff_free(diff);
}
+void test_diff_drivers__builtins(void)
+{
+ git_diff *diff;
+ git_patch *patch;
+ git_buf file = GIT_BUF_INIT, actual = GIT_BUF_INIT, expected = 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_path_dirload("userdiff/files", 9, 0, 0, &files));
+
+ 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_buf_sets(&expected, "userdiff/expected/nodriver/diff.");
+ git_buf_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_clear(&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_buf_sets(&expected, "userdiff/expected/driver/diff.");
+ git_buf_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_clear(&actual);
+ git_patch_free(patch);
+ git_diff_free(diff);
+
+ git__free(path);
+ }
+
+ git_buf_free(&file);
+ git_buf_free(&actual);
+ git_buf_free(&expected);
+ git_vector_free(&files);
+}