|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "repository.h" |
| 3 | + |
| 4 | +/* Fixture setup */ |
| 5 | +static git_repository *g_repo; |
| 6 | +static git_signature *g_author, *g_committer; |
| 7 | + |
| 8 | +void test_commit_create__initialize(void) |
| 9 | +{ |
| 10 | + g_repo = cl_git_sandbox_init("testrepo2"); |
| 11 | + cl_git_pass(git_signature_new(&g_author, "Edward Thomson", "ethomson@edwardthomson.com", 123456789, 60)); |
| 12 | + cl_git_pass(git_signature_new(&g_committer, "libgit2 user", "nobody@noreply.libgit2.org", 987654321, 90)); |
| 13 | +} |
| 14 | + |
| 15 | +void test_commit_create__cleanup(void) |
| 16 | +{ |
| 17 | + git_signature_free(g_committer); |
| 18 | + git_signature_free(g_author); |
| 19 | + cl_git_sandbox_cleanup(); |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +void test_commit_create__from_stage_simple(void) |
| 24 | +{ |
| 25 | + git_commit_create_options opts = GIT_COMMIT_CREATE_OPTIONS_INIT; |
| 26 | + git_index *index; |
| 27 | + git_oid commit_id; |
| 28 | + git_tree *tree; |
| 29 | + |
| 30 | + opts.author = g_author; |
| 31 | + opts.committer = g_committer; |
| 32 | + |
| 33 | + cl_git_rewritefile("testrepo2/newfile.txt", "This is a new file.\n"); |
| 34 | + cl_git_rewritefile("testrepo2/newfile2.txt", "This is a new file.\n"); |
| 35 | + cl_git_rewritefile("testrepo2/README", "hello, world.\n"); |
| 36 | + cl_git_rewritefile("testrepo2/new.txt", "hi there.\n"); |
| 37 | + |
| 38 | + cl_git_pass(git_repository_index(&index, g_repo)); |
| 39 | + cl_git_pass(git_index_add_bypath(index, "newfile2.txt")); |
| 40 | + cl_git_pass(git_index_add_bypath(index, "README")); |
| 41 | + cl_git_pass(git_index_write(index)); |
| 42 | + |
| 43 | + cl_git_pass(git_commit_create_from_stage(&commit_id, g_repo, "This is the message.", &opts)); |
| 44 | + |
| 45 | + cl_git_pass(git_repository_head_tree(&tree, g_repo)); |
| 46 | + |
| 47 | + cl_assert_equal_oidstr("241b5b04e847bc38dd7b4b9f49f21e55da40f3a6", &commit_id); |
| 48 | + cl_assert_equal_oidstr("b27210772d0633870b4f486d04ed3eb5ebbef5e7", git_tree_id(tree)); |
| 49 | + |
| 50 | + git_index_free(index); |
| 51 | + git_tree_free(tree); |
| 52 | +} |
| 53 | + |
| 54 | +void test_commit_create__from_stage_nochanges(void) |
| 55 | +{ |
| 56 | + git_commit_create_options opts = GIT_COMMIT_CREATE_OPTIONS_INIT; |
| 57 | + git_oid commit_id; |
| 58 | + git_tree *tree; |
| 59 | + |
| 60 | + opts.author = g_author; |
| 61 | + opts.committer = g_committer; |
| 62 | + |
| 63 | + cl_git_fail_with(GIT_EUNCHANGED, git_commit_create_from_stage(&commit_id, g_repo, "Message goes here.", &opts)); |
| 64 | + |
| 65 | + opts.allow_empty_commit = 1; |
| 66 | + |
| 67 | + cl_git_pass(git_commit_create_from_stage(&commit_id, g_repo, "Message goes here.", &opts)); |
| 68 | + |
| 69 | + cl_git_pass(git_repository_head_tree(&tree, g_repo)); |
| 70 | + |
| 71 | + cl_assert_equal_oidstr("f776dc4c7fd8164b7127dc8e4f9b44421cb01b56", &commit_id); |
| 72 | + cl_assert_equal_oidstr("c4dc1555e4d4fa0e0c9c3fc46734c7c35b3ce90b", git_tree_id(tree)); |
| 73 | + |
| 74 | + git_tree_free(tree); |
| 75 | +} |
| 76 | + |
| 77 | +void test_commit_create__from_stage_newrepo(void) |
| 78 | +{ |
| 79 | + git_commit_create_options opts = GIT_COMMIT_CREATE_OPTIONS_INIT; |
| 80 | + git_repository *newrepo; |
| 81 | + git_index *index; |
| 82 | + git_commit *commit; |
| 83 | + git_tree *tree; |
| 84 | + git_oid commit_id; |
| 85 | + |
| 86 | + opts.author = g_author; |
| 87 | + opts.committer = g_committer; |
| 88 | + |
| 89 | + git_repository_init(&newrepo, "newrepo", false); |
| 90 | + cl_git_pass(git_repository_index(&index, newrepo)); |
| 91 | + |
| 92 | + cl_git_rewritefile("newrepo/hello.txt", "hello, world.\n"); |
| 93 | + cl_git_rewritefile("newrepo/hi.txt", "hi there.\n"); |
| 94 | + cl_git_rewritefile("newrepo/foo.txt", "bar.\n"); |
| 95 | + |
| 96 | + cl_git_pass(git_index_add_bypath(index, "hello.txt")); |
| 97 | + cl_git_pass(git_index_add_bypath(index, "foo.txt")); |
| 98 | + cl_git_pass(git_index_write(index)); |
| 99 | + |
| 100 | + cl_git_pass(git_commit_create_from_stage(&commit_id, newrepo, "Initial commit.", &opts)); |
| 101 | + cl_git_pass(git_repository_head_commit(&commit, newrepo)); |
| 102 | + cl_git_pass(git_repository_head_tree(&tree, newrepo)); |
| 103 | + |
| 104 | + cl_assert_equal_oid(&commit_id, git_commit_id(commit)); |
| 105 | + cl_assert_equal_oidstr("b2fa96a4f191c76eb172437281c66aa29609dcaa", git_commit_tree_id(commit)); |
| 106 | + |
| 107 | + git_tree_free(tree); |
| 108 | + git_commit_free(commit); |
| 109 | + git_index_free(index); |
| 110 | + git_repository_free(newrepo); |
| 111 | + cl_fixture_cleanup("newrepo"); |
| 112 | +} |
0 commit comments