commitstagedfile.c 6.22 KB
Newer Older
1
#include "clar_libgit2.h"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include "posix.h"

static git_repository *repo;

void test_object_commit_commitstagedfile__initialize(void)
{
	cl_fixture("treebuilder");
	cl_git_pass(git_repository_init(&repo, "treebuilder/", 0));
	cl_assert(repo != NULL);
}

void test_object_commit_commitstagedfile__cleanup(void)
{
	git_repository_free(repo);
	cl_fixture_cleanup("treebuilder");
}

void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
{
	git_index *index;
	git_index_entry *entry;
	git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
	git_signature *signature;
	git_tree *tree;
26
	char buffer[128];
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 54 55 56 57 58 59 60 61 62 63 64

	/*
	 * The test below replicates the following git scenario
	 *
	 * $ echo "test" > test.txt
	 * $ git hash-object test.txt
	 * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
	 *
	 * $ git add .
	 * $ git commit -m "Initial commit"
	 *
	 * $ git log
	 * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
	 * Author: nulltoken <emeric.fermas@gmail.com>
	 * Date:   Wed Dec 14 08:29:03 2011 +0100
	 *
	 *     Initial commit
	 *
	 * $ git show 1fe3 --format=raw
	 * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
	 * tree 2b297e643c551e76cfa1f93810c50811382f9117
	 * author nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
	 * committer nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
	 * 
	 *     Initial commit
	 * 
	 * diff --git a/test.txt b/test.txt
	 * new file mode 100644
	 * index 0000000..9daeafb
	 * --- /dev/null
	 * +++ b/test.txt
	 * @@ -0,0 +1 @@
	 * +test
	 *
	 * $ git ls-tree 2b297
	 * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4    test.txt
	 */

65
	cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d"));
66 67 68 69 70 71
	cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117"));
	cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4"));

	/*
	 * Add a new file to the index
	 */
72
	cl_git_mkfile("treebuilder/test.txt", "test\n");
73 74 75 76 77 78 79 80
	cl_git_pass(git_repository_index(&index, repo));
	cl_git_pass(git_index_add(index, "test.txt", 0));

	entry = git_index_get(index, 0);

	cl_assert(git_oid_cmp(&expected_blob_oid, &entry->oid) == 0);

	/*
81 82 83 84 85 86
	 * Information about index entry should match test file
	 */
	{
		struct stat st;
		cl_must_pass(p_lstat("treebuilder/test.txt", &st));
		cl_assert(entry->file_size == st.st_size);
87 88 89 90 91 92 93
#ifndef _WIN32
		/*
		 * Windows doesn't populate these fields, and the signage is
		 * wrong in the Windows version of the struct, so lets avoid
		 * the "comparing signed and unsigned" compilation warning in
		 * that case.
		 */
94 95
		cl_assert(entry->uid == st.st_uid);
		cl_assert(entry->gid == st.st_gid);
96
#endif
97 98 99
	}

	/*
100 101 102 103 104 105 106 107 108 109 110
	 * Build the tree from the index
	 */
	cl_git_pass(git_tree_create_fromindex(&tree_oid, index));

	cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0);

	/*
	 * Commit the staged file
	 */
	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
111

112
	cl_assert_equal_i(16, git_message_prettify(buffer, 128, "Initial commit", 0));
113

114 115 116 117 118 119 120
	cl_git_pass(git_commit_create_v(
		&commit_oid,
		repo,
		"HEAD",
		signature,
		signature,
		NULL,
121
		buffer,
122 123 124 125 126 127 128 129 130
		tree,
		0));

	cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);

	git_signature_free(signature);
	git_tree_free(tree);
	git_index_free(index);
}
131 132 133 134 135

void test_object_commit_commitstagedfile__message_prettify(void)
{
	char buffer[100];

136
	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1);
137
	cl_assert_equal_s(buffer, "");
138
	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1);
139 140
	cl_assert_equal_s(buffer, "");

141 142 143 144
	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0));
	cl_assert_equal_s("Short\n", buffer);
	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1));
	cl_assert_equal_s("Short\n", buffer);
145

146
	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0);
147
	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n");
148 149

	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0);
150 151 152
	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n");

	/* try out overflow */
153
	cl_assert(git_message_prettify(buffer, sizeof(buffer),
154 155
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "12345678",
156
		0) > 0);
157 158 159 160
	cl_assert_equal_s(buffer,
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");

161
	cl_assert(git_message_prettify(buffer, sizeof(buffer),
162 163
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n",
164
		0) > 0);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	cl_assert_equal_s(buffer,
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");

	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "123456789",
		0));
	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "123456789\n",
		0));
	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890",
		0));
	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x",
		0));

186
	cl_assert(git_message_prettify(buffer, sizeof(buffer),
187 188 189
		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
		"# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
		"1234567890",
190 191 192 193 194
		1) > 0);

	cl_assert(git_message_prettify(NULL, 0, "", 0) == 1);
	cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12);
	cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15);
195
}