externalmodifications.c 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#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);
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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);
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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);
99

100 101 102 103 104 105 106
	/* 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);
}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

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);
}