]> git.proxmox.com Git - libgit2.git/blob - tests/commit/write.c
transports: smart: fix memory leak on OOM path
[libgit2.git] / tests / commit / write.c
1 #include "clar_libgit2.h"
2
3 static const char *committer_name = "Vicent Marti";
4 static const char *committer_email = "vicent@github.com";
5 static const char *commit_message = "This commit has been created in memory\n\
6 This is a commit created in memory and it will be written back to disk\n";
7 static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
8 static const char *root_commit_message = "This is a root commit\n\
9 This is a root commit and should be the only one in this branch\n";
10 static const char *root_reflog_message = "commit (initial): This is a root commit \
11 This is a root commit and should be the only one in this branch";
12 static char *head_old;
13 static git_reference *head, *branch;
14 static git_commit *commit;
15
16 // Fixture setup
17 static git_repository *g_repo;
18 void test_commit_write__initialize(void)
19 {
20 g_repo = cl_git_sandbox_init("testrepo");
21 }
22
23 void test_commit_write__cleanup(void)
24 {
25 git_reference_free(head);
26 head = NULL;
27
28 git_reference_free(branch);
29 branch = NULL;
30
31 git_commit_free(commit);
32 commit = NULL;
33
34 git__free(head_old);
35 head_old = NULL;
36
37 cl_git_sandbox_cleanup();
38 }
39
40
41 // write a new commit object from memory to disk
42 void test_commit_write__from_memory(void)
43 {
44 git_oid tree_id, parent_id, commit_id;
45 git_signature *author, *committer;
46 const git_signature *author1, *committer1;
47 git_commit *parent;
48 git_tree *tree;
49 const char *commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479";
50
51 git_oid_fromstr(&tree_id, tree_oid);
52 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
53
54 git_oid_fromstr(&parent_id, commit_id_str);
55 cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id));
56
57 /* create signatures */
58 cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
59 cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
60
61 cl_git_pass(git_commit_create_v(
62 &commit_id, /* out id */
63 g_repo,
64 NULL, /* do not update the HEAD */
65 author,
66 committer,
67 NULL,
68 commit_message,
69 tree,
70 1, parent));
71
72 git_object_free((git_object *)parent);
73 git_object_free((git_object *)tree);
74
75 git_signature_free(committer);
76 git_signature_free(author);
77
78 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
79
80 /* Check attributes were set correctly */
81 author1 = git_commit_author(commit);
82 cl_assert(author1 != NULL);
83 cl_assert_equal_s(committer_name, author1->name);
84 cl_assert_equal_s(committer_email, author1->email);
85 cl_assert(author1->when.time == 987654321);
86 cl_assert(author1->when.offset == 90);
87
88 committer1 = git_commit_committer(commit);
89 cl_assert(committer1 != NULL);
90 cl_assert_equal_s(committer_name, committer1->name);
91 cl_assert_equal_s(committer_email, committer1->email);
92 cl_assert(committer1->when.time == 123456789);
93 cl_assert(committer1->when.offset == 60);
94
95 cl_assert_equal_s(commit_message, git_commit_message(commit));
96 }
97
98 // create a root commit
99 void test_commit_write__root(void)
100 {
101 git_oid tree_id, commit_id;
102 const git_oid *branch_oid;
103 git_signature *author, *committer;
104 const char *branch_name = "refs/heads/root-commit-branch";
105 git_tree *tree;
106 git_reflog *log;
107 const git_reflog_entry *entry;
108
109 git_oid_fromstr(&tree_id, tree_oid);
110 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
111
112 /* create signatures */
113 cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60));
114 cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90));
115
116 /* First we need to update HEAD so it points to our non-existant branch */
117 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
118 cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC);
119 head_old = git__strdup(git_reference_symbolic_target(head));
120 cl_assert(head_old != NULL);
121 git_reference_free(head);
122
123 cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1, NULL));
124
125 cl_git_pass(git_commit_create_v(
126 &commit_id, /* out id */
127 g_repo,
128 "HEAD",
129 author,
130 committer,
131 NULL,
132 root_commit_message,
133 tree,
134 0));
135
136 git_object_free((git_object *)tree);
137 git_signature_free(author);
138
139 /*
140 * The fact that creating a commit works has already been
141 * tested. Here we just make sure it's our commit and that it was
142 * written as a root commit.
143 */
144 cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
145 cl_assert(git_commit_parentcount(commit) == 0);
146 cl_git_pass(git_reference_lookup(&branch, g_repo, branch_name));
147 branch_oid = git_reference_target(branch);
148 cl_assert_equal_oid(branch_oid, &commit_id);
149 cl_assert_equal_s(root_commit_message, git_commit_message(commit));
150
151 cl_git_pass(git_reflog_read(&log, g_repo, branch_name));
152 cl_assert_equal_i(1, git_reflog_entrycount(log));
153 entry = git_reflog_entry_byindex(log, 0);
154 cl_assert_equal_s(committer->email, git_reflog_entry_committer(entry)->email);
155 cl_assert_equal_s(committer->name, git_reflog_entry_committer(entry)->name);
156 cl_assert_equal_s(root_reflog_message, git_reflog_entry_message(entry));
157
158 git_signature_free(committer);
159 git_reflog_free(log);
160 }