Commit 155fa2342d838bdb2aa873c95a42e091351bb69a

Russell Belfer 2013-09-05T15:06:42

Add clar helper to create new commit from index There were a lot of places in the test code base that were creating a commit from the index on the current branch. This just adds a helper to handle that case pretty easily. There was only one test where this change ended up tweaking the test data, so pretty easy and mostly just a cleanup.

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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 522f736..4cf6824 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -337,6 +337,65 @@ int cl_git_remove_placeholders(const char *directory_path, const char *filename)
 	return error;
 }
 
+#define CL_COMMIT_NAME "Libgit2 Tester"
+#define CL_COMMIT_EMAIL "libgit2-test@github.com"
+#define CL_COMMIT_MSG "Test commit of tree "
+
+void cl_repo_commit_from_index(
+	git_oid *out,
+	git_repository *repo,
+	git_signature *sig,
+	git_time_t time,
+	const char *msg)
+{
+	git_index *index;
+	git_oid commit_id, tree_id;
+	git_object *parent = NULL;
+	git_reference *ref = NULL;
+	git_tree *tree = NULL;
+	char buf[128];
+	int free_sig = (sig == NULL);
+
+	/* it is fine if looking up HEAD fails - we make this the first commit */
+	git_revparse_ext(&parent, &ref, repo, "HEAD");
+
+	/* write the index content as a tree */
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_index_write_tree(&tree_id, index));
+	cl_git_pass(git_index_write(index));
+	git_index_free(index);
+
+	cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
+
+	if (sig)
+		cl_assert(sig->name && sig->email);
+	else if (!time)
+		cl_git_pass(git_signature_now(&sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL));
+	else
+		cl_git_pass(git_signature_new(
+			&sig, CL_COMMIT_NAME, CL_COMMIT_EMAIL, time, 0));
+
+	if (!msg) {
+		strcpy(buf, CL_COMMIT_MSG);
+		git_oid_tostr(buf + strlen(CL_COMMIT_MSG),
+			sizeof(buf) - strlen(CL_COMMIT_MSG), &tree_id);
+		msg = buf;
+	}
+
+	cl_git_pass(git_commit_create_v(
+		&commit_id, repo, ref ? git_reference_name(ref) : "HEAD",
+		sig, sig, NULL, msg, tree, parent ? 1 : 0, parent));
+
+	if (out)
+		git_oid_cpy(out, &commit_id);
+
+	git_object_free(parent);
+	git_reference_free(ref);
+	if (free_sig)
+		git_signature_free(sig);
+	git_tree_free(tree);
+}
+
 void cl_repo_set_bool(git_repository *repo, const char *cfg, int value)
 {
 	git_config *config;
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index 76299e4..f2d9c4d 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -98,6 +98,14 @@ const char* cl_git_path_url(const char *path);
 /* Test repository cleaner */
 int cl_git_remove_placeholders(const char *directory_path, const char *filename);
 
+/* commit creation helpers */
+void cl_repo_commit_from_index(
+	git_oid *out,
+	git_repository *repo,
+	git_signature *sig,
+	git_time_t time,
+	const char *msg);
+
 /* config setting helpers */
 void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
 int cl_repo_get_bool(git_repository *repo, const char *cfg);
diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c
index 167dedf..036ff09 100644
--- a/tests-clar/diff/submodules.c
+++ b/tests-clar/diff/submodules.c
@@ -228,11 +228,11 @@ void test_diff_submodules__invalid_cache(void)
 		"<END>"
 	};
 	static const char *expected_moved[] = {
-		"diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..0910a13 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 0910a13dfa2210496f6c590d75bc360dd11b2a1b\n",
+		"diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae\n",
 		"<END>"
 	};
 	static const char *expected_moved_dirty[] = {
-		"diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..0910a13 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 0910a13dfa2210496f6c590d75bc360dd11b2a1b-dirty\n",
+		"diff --git a/sm_changed_head b/sm_changed_head\nindex 3d9386c..7002348 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n+Subproject commit 700234833f6ccc20d744b238612646be071acaae-dirty\n",
 		"<END>"
 	};
 
@@ -309,26 +309,7 @@ void test_diff_submodules__invalid_cache(void)
 	git_diff_list_free(diff);
 
 	/* commit changed index of submodule */
-	{
-		git_object *parent;
-		git_oid tree_id, commit_id;
-		git_tree *tree;
-		git_signature *sig;
-		git_reference *ref;
-
-		cl_git_pass(git_revparse_ext(&parent, &ref, smrepo, "HEAD"));
-		cl_git_pass(git_index_write_tree(&tree_id, smindex));
-		cl_git_pass(git_index_write(smindex));
-		cl_git_pass(git_tree_lookup(&tree, smrepo, &tree_id));
-		cl_git_pass(git_signature_new(&sig, "Sm Test", "sm@tester.test", 1372350000, 480));
-		cl_git_pass(git_commit_create_v(
-			&commit_id, smrepo, git_reference_name(ref), sig, sig,
-			NULL, "Move it", tree, 1, parent));
-		git_object_free(parent);
-		git_tree_free(tree);
-		git_reference_free(ref);
-		git_signature_free(sig);
-	}
+	cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Move it");
 
 	git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY);
 
diff --git a/tests-clar/index/addall.c b/tests-clar/index/addall.c
index 00388ee..f46a1e1 100644
--- a/tests-clar/index/addall.c
+++ b/tests-clar/index/addall.c
@@ -120,37 +120,6 @@ static void check_stat_data(git_index *index, const char *path, bool match)
 	}
 }
 
-static void commit_index_to_head(
-	git_repository *repo,
-	const char *commit_message)
-{
-	git_index *index;
-	git_oid tree_id, commit_id;
-	git_tree *tree;
-	git_signature *sig;
-	git_commit *parent = NULL;
-
-	git_revparse_single((git_object **)&parent, repo, "HEAD");
-	/* it is okay if looking up the HEAD fails */
-
-	cl_git_pass(git_repository_index(&index, repo));
-	cl_git_pass(git_index_write_tree(&tree_id, index));
-	cl_git_pass(git_index_write(index)); /* not needed, but might as well */
-	git_index_free(index);
-
-	cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
-
-	cl_git_pass(git_signature_now(&sig, "Testy McTester", "tt@tester.test"));
-
-	cl_git_pass(git_commit_create_v(
-		&commit_id, repo, "HEAD", sig, sig,
-		NULL, commit_message, tree, parent ? 1 : 0, parent));
-
-	git_commit_free(parent);
-	git_tree_free(tree);
-	git_signature_free(sig);
-}
-
 void test_index_addall__repo_lifecycle(void)
 {
 	int error;
@@ -197,7 +166,7 @@ void test_index_addall__repo_lifecycle(void)
 	check_stat_data(index, "addall/file.zzz", true);
 	check_status(g_repo, 2, 0, 0, 3, 0, 0, 1);
 
-	commit_index_to_head(g_repo, "first commit");
+	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "first commit");
 	check_status(g_repo, 0, 0, 0, 3, 0, 0, 1);
 
 	/* attempt to add an ignored file - does nothing */
@@ -244,7 +213,7 @@ void test_index_addall__repo_lifecycle(void)
 	cl_git_pass(git_index_add_bypath(index, "file.zzz"));
 	check_status(g_repo, 1, 0, 1, 3, 0, 0, 0);
 
-	commit_index_to_head(g_repo, "second commit");
+	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "second commit");
 	check_status(g_repo, 0, 0, 0, 3, 0, 0, 0);
 
 	cl_must_pass(p_unlink("addall/file.zzz"));
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index e3fc112..caa211e 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -565,6 +565,11 @@ void test_repo_init__init_with_initial_commit(void)
 	cl_git_pass(git_index_add_bypath(index, "file.txt"));
 	cl_git_pass(git_index_write(index));
 
+	/* Intentionally not using cl_repo_commit_from_index here so this code
+	 * can be used as an example of how an initial commit is typically
+	 * made to a repository...
+	 */
+
 	/* Make sure we're ready to use git_signature_default :-) */
 	{
 		git_config *cfg, *local;
diff --git a/tests-clar/stash/drop.c b/tests-clar/stash/drop.c
index 60b3c72..59413f0 100644
--- a/tests-clar/stash/drop.c
+++ b/tests-clar/stash/drop.c
@@ -36,25 +36,27 @@ static void push_three_states(void)
 	cl_git_mkfile("stash/zero.txt", "content\n");
 	cl_git_pass(git_repository_index(&index, repo));
 	cl_git_pass(git_index_add_bypath(index, "zero.txt"));
-	commit_staged_files(&oid, index, signature);
+	cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
 	cl_assert(git_path_exists("stash/zero.txt"));
+	git_index_free(index);
 
 	cl_git_mkfile("stash/one.txt", "content\n");
-	cl_git_pass(git_stash_save(&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
+	cl_git_pass(git_stash_save(
+		&oid, repo, signature, "First", GIT_STASH_INCLUDE_UNTRACKED));
 	cl_assert(!git_path_exists("stash/one.txt"));
 	cl_assert(git_path_exists("stash/zero.txt"));
 
 	cl_git_mkfile("stash/two.txt", "content\n");
-	cl_git_pass(git_stash_save(&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
+	cl_git_pass(git_stash_save(
+		&oid, repo, signature, "Second", GIT_STASH_INCLUDE_UNTRACKED));
 	cl_assert(!git_path_exists("stash/two.txt"));
 	cl_assert(git_path_exists("stash/zero.txt"));
 
 	cl_git_mkfile("stash/three.txt", "content\n");
-	cl_git_pass(git_stash_save(&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
+	cl_git_pass(git_stash_save(
+		&oid, repo, signature, "Third", GIT_STASH_INCLUDE_UNTRACKED));
 	cl_assert(!git_path_exists("stash/three.txt"));
 	cl_assert(git_path_exists("stash/zero.txt"));
-
-	git_index_free(index);
 }
 
 void test_stash_drop__cannot_drop_a_non_existing_stashed_state(void)
@@ -160,7 +162,7 @@ void test_stash_drop__dropping_the_top_stash_updates_the_stash_reference(void)
 	retrieve_top_stash_id(&oid);
 
 	cl_git_pass(git_revparse_single(&next_top_stash, repo, "stash@{1}"));
-	cl_assert_equal_i(false, git_oid_cmp(&oid, git_object_id(next_top_stash)) == 0);
+	cl_assert(git_oid_cmp(&oid, git_object_id(next_top_stash)) != 0);
 
 	cl_git_pass(git_stash_drop(repo, 0));
 
diff --git a/tests-clar/stash/save.c b/tests-clar/stash/save.c
index bb35a3d..035b622 100644
--- a/tests-clar/stash/save.c
+++ b/tests-clar/stash/save.c
@@ -241,7 +241,7 @@ void test_stash_save__stashing_updates_the_reflog(void)
 void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
 {
 	git_index *index;
-	git_oid commit_oid, stash_tip_oid;
+	git_oid stash_tip_oid;
 
 	cl_git_pass(git_repository_index(&index, repo));
 
@@ -251,8 +251,7 @@ void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
 	 */
 	cl_git_pass(git_index_add_bypath(index, "what"));
 	cl_git_pass(git_index_add_bypath(index, "who"));
-	cl_git_pass(git_index_write(index));
-	commit_staged_files(&commit_oid, index, signature);
+	cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
 	git_index_free(index);
 
 	cl_assert_equal_i(GIT_ENOTFOUND,
diff --git a/tests-clar/stash/stash_helpers.c b/tests-clar/stash/stash_helpers.c
index f462a13..06b63f1 100644
--- a/tests-clar/stash/stash_helpers.c
+++ b/tests-clar/stash/stash_helpers.c
@@ -2,38 +2,8 @@
 #include "fileops.h"
 #include "stash_helpers.h"
 
-void commit_staged_files(
-	git_oid *commit_oid,
-	git_index *index,
-	git_signature *signature)
-{
-	git_tree *tree;
-	git_oid tree_oid;
-	git_repository *repo;
-
-	repo = git_index_owner(index);
-
-	cl_git_pass(git_index_write_tree(&tree_oid, index));
-
-	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
-
-	cl_git_pass(git_commit_create_v(
-		commit_oid,
-		repo,
-		"HEAD",
-		signature,
-		signature,
-		NULL,
-		"Initial commit",
-		tree,
-		0));
-
-	git_tree_free(tree);
-}
-
 void setup_stash(git_repository *repo, git_signature *signature)
 {
-	git_oid commit_oid;
 	git_index *index;
 
 	cl_git_pass(git_repository_index(&index, repo));
@@ -50,9 +20,8 @@ void setup_stash(git_repository *repo, git_signature *signature)
 	cl_git_pass(git_index_add_bypath(index, "how"));
 	cl_git_pass(git_index_add_bypath(index, "who"));
 	cl_git_pass(git_index_add_bypath(index, ".gitignore"));
-	cl_git_pass(git_index_write(index));
 
-	commit_staged_files(&commit_oid, index, signature);
+	cl_repo_commit_from_index(NULL, repo, signature, 0, "Initial commit");
 
 	cl_git_rewritefile("stash/what", "goodbye\n");			/* dd7e1c6f0fefe118f0b63d9f10908c460aa317a6 */
 	cl_git_rewritefile("stash/how", "not so small and\n");	/* e6d64adb2c7f3eb8feb493b556cc8070dca379a3 */
diff --git a/tests-clar/stash/stash_helpers.h b/tests-clar/stash/stash_helpers.h
index bb7fec4..7c3e13d 100644
--- a/tests-clar/stash/stash_helpers.h
+++ b/tests-clar/stash/stash_helpers.h
@@ -1,8 +1,3 @@
 void setup_stash(
 	git_repository *repo,
 	git_signature *signature);
-
-void commit_staged_files(
-	git_oid *commit_oid,
-	git_index *index,
-	git_signature *signature);
\ No newline at end of file
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index be7398c..135a958 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -632,35 +632,12 @@ void test_status_worktree__conflicted_item(void)
 
 static void stage_and_commit(git_repository *repo, const char *path)
 {
-	git_oid tree_oid, commit_oid;
-	git_tree *tree;
-	git_signature *signature;
 	git_index *index;
 
 	cl_git_pass(git_repository_index(&index, repo));
 	cl_git_pass(git_index_add_bypath(index, path));
-	cl_git_pass(git_index_write(index));
-
-	cl_git_pass(git_index_write_tree(&tree_oid, index));
+	cl_repo_commit_from_index(NULL, repo, NULL, 1323847743, "Initial commit\n");
 	git_index_free(index);
-
-	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
-
-	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
-
-	cl_git_pass(git_commit_create_v(
-		&commit_oid,
-		repo,
-		"HEAD",
-		signature,
-		signature,
-		NULL,
-		"Initial commit\n\0",
-		tree,
-		0));
-
-	git_tree_free(tree);
-	git_signature_free(signature);
 }
 
 static void assert_ignore_case(
diff --git a/tests-clar/stress/diff.c b/tests-clar/stress/diff.c
index 0524aa1..1d31973 100644
--- a/tests-clar/stress/diff.c
+++ b/tests-clar/stress/diff.c
@@ -54,27 +54,9 @@ static void test_with_many(int expected_new)
 
 	git_diff_list_free(diff);
 
-	{
-		git_object *parent;
-		git_signature *sig;
-		git_oid tree_id, commit_id;
-		git_reference *ref;
-
-		cl_git_pass(git_index_write_tree(&tree_id, index));
-		cl_git_pass(git_tree_lookup(&new_tree, g_repo, &tree_id));
-
-		cl_git_pass(git_revparse_ext(&parent, &ref, g_repo, "HEAD"));
-		cl_git_pass(git_signature_new(
-			&sig, "Sm Test", "sm@tester.test", 1372350000, 480));
-
-		cl_git_pass(git_commit_create_v(
-			&commit_id, g_repo, git_reference_name(ref), sig, sig,
-			NULL, "yoyoyo", new_tree, 1, parent));
-
-		git_object_free(parent);
-		git_reference_free(ref);
-		git_signature_free(sig);
-	}
+	cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo");
+	cl_git_pass(git_revparse_single(
+		(git_object **)&new_tree, g_repo, "HEAD^{tree}"));
 
 	cl_git_pass(git_diff_tree_to_tree(
 		&diff, g_repo, tree, new_tree, &diffopts));