Skip to content

Commit 8137026

Browse files
authored
Merge pull request libgit2#5374 from pks-t/pks/diff-with-empty-subtree
tests: diff: verify that we are able to diff with empty subtrees
2 parents ff3297d + 17670ef commit 8137026

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

tests/diff/tree.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,52 @@ void test_diff_tree__diff_configs(void)
524524
cl_assert_equal_i(7, expect.line_adds);
525525
cl_assert_equal_i(15, expect.line_dels);
526526
}
527+
528+
void test_diff_tree__diff_tree_with_empty_dir_entry_succeeds(void)
529+
{
530+
const char *content = "This is a blob\n";
531+
const git_diff_delta *delta;
532+
git_oid empty_tree, invalid_tree, blob;
533+
git_buf patch = GIT_BUF_INIT;
534+
git_treebuilder *builder;
535+
536+
g_repo = cl_git_sandbox_init("empty_standard_repo");
537+
538+
cl_git_pass(git_blob_create_from_buffer(&blob, g_repo, content, strlen(content)));
539+
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
540+
cl_git_pass(git_treebuilder_write(&empty_tree, builder));
541+
cl_git_pass(git_treebuilder_insert(NULL, builder, "empty_tree", &empty_tree, GIT_FILEMODE_TREE));
542+
cl_git_pass(git_treebuilder_insert(NULL, builder, "blob", &blob, GIT_FILEMODE_BLOB));
543+
cl_git_pass(git_treebuilder_write(&invalid_tree, builder));
544+
545+
cl_git_pass(git_tree_lookup(&a, g_repo, &empty_tree));
546+
cl_git_pass(git_tree_lookup(&b, g_repo, &invalid_tree));
547+
cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));
548+
549+
cl_git_pass(git_diff_foreach(diff,
550+
diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
551+
cl_assert_equal_i(1, expect.files);
552+
cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
553+
cl_assert_equal_i(1, expect.hunks);
554+
cl_assert_equal_i(1, expect.lines);
555+
cl_assert_equal_i(0, expect.line_ctxt);
556+
cl_assert_equal_i(1, expect.line_adds);
557+
cl_assert_equal_i(0, expect.line_dels);
558+
559+
cl_git_pass(git_diff_to_buf(&patch, diff, GIT_DIFF_FORMAT_PATCH));
560+
cl_assert_equal_s(patch.ptr,
561+
"diff --git a/blob b/blob\n"
562+
"new file mode 100644\n"
563+
"index 0000000..bbf2e80\n"
564+
"--- /dev/null\n"
565+
"+++ b/blob\n"
566+
"@@ -0,0 +1 @@\n"
567+
"+This is a blob\n");
568+
569+
cl_assert_equal_i(git_diff_num_deltas(diff), 1);
570+
delta = git_diff_get_delta(diff, 0);
571+
cl_assert_equal_s(delta->new_file.path, "blob");
572+
573+
git_treebuilder_free(builder);
574+
git_buf_dispose(&patch);
575+
}

tests/diff/workdir.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,3 +2160,46 @@ void test_diff_workdir__symlink_changed_on_non_symlink_platform(void)
21602160
git_tree_free(tree);
21612161
git_vector_free(&pathlist);
21622162
}
2163+
2164+
void test_diff_workdir__order(void)
2165+
{
2166+
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
2167+
git_buf patch = GIT_BUF_INIT;
2168+
git_oid tree_oid, blob_oid;
2169+
git_treebuilder *builder;
2170+
git_tree *tree;
2171+
git_diff *diff;
2172+
2173+
g_repo = cl_git_sandbox_init("empty_standard_repo");
2174+
2175+
/* Build tree with a single file "abc.txt" */
2176+
cl_git_pass(git_blob_create_from_buffer(&blob_oid, g_repo, "foo\n", 4));
2177+
cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
2178+
cl_git_pass(git_treebuilder_insert(NULL, builder, "abc.txt", &blob_oid, GIT_FILEMODE_BLOB));
2179+
cl_git_pass(git_treebuilder_write(&tree_oid, builder));
2180+
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
2181+
2182+
/* Create a directory that sorts before and one that sorts after "abc.txt" */
2183+
cl_git_mkfile("empty_standard_repo/abc.txt", "bar\n");
2184+
cl_must_pass(p_mkdir("empty_standard_repo/abb", 0777));
2185+
cl_must_pass(p_mkdir("empty_standard_repo/abd", 0777));
2186+
2187+
opts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
2188+
cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts));
2189+
2190+
cl_assert_equal_i(1, git_diff_num_deltas(diff));
2191+
cl_git_pass(git_diff_to_buf(&patch, diff, GIT_DIFF_FORMAT_PATCH));
2192+
cl_assert_equal_s(patch.ptr,
2193+
"diff --git a/abc.txt b/abc.txt\n"
2194+
"index 257cc56..5716ca5 100644\n"
2195+
"--- a/abc.txt\n"
2196+
"+++ b/abc.txt\n"
2197+
"@@ -1 +1 @@\n"
2198+
"-foo\n"
2199+
"+bar\n");
2200+
2201+
git_treebuilder_free(builder);
2202+
git_buf_dispose(&patch);
2203+
git_diff_free(diff);
2204+
git_tree_free(tree);
2205+
}

0 commit comments

Comments
 (0)