]>
git.proxmox.com Git - libgit2.git/blob - tests/commit/write.c
1 #include "clar_libgit2.h"
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
;
17 static git_repository
*g_repo
;
18 void test_commit_write__initialize(void)
20 g_repo
= cl_git_sandbox_init("testrepo");
23 void test_commit_write__cleanup(void)
25 git_reference_free(head
);
28 git_reference_free(branch
);
31 git_commit_free(commit
);
37 cl_git_sandbox_cleanup();
41 // write a new commit object from memory to disk
42 void test_commit_write__from_memory(void)
44 git_oid tree_id
, parent_id
, commit_id
;
45 git_signature
*author
, *committer
;
46 const git_signature
*author1
, *committer1
;
49 const char *commit_id_str
= "8496071c1b46c854b31185ea97743be6a8774479";
51 git_oid_fromstr(&tree_id
, tree_oid
);
52 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
54 git_oid_fromstr(&parent_id
, commit_id_str
);
55 cl_git_pass(git_commit_lookup(&parent
, g_repo
, &parent_id
));
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));
61 cl_git_pass(git_commit_create_v(
62 &commit_id
, /* out id */
64 NULL
, /* do not update the HEAD */
72 git_object_free((git_object
*)parent
);
73 git_object_free((git_object
*)tree
);
75 git_signature_free(committer
);
76 git_signature_free(author
);
78 cl_git_pass(git_commit_lookup(&commit
, g_repo
, &commit_id
));
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);
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);
95 cl_assert_equal_s(commit_message
, git_commit_message(commit
));
98 // create a root commit
99 void test_commit_write__root(void)
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";
107 const git_reflog_entry
*entry
;
109 git_oid_fromstr(&tree_id
, tree_oid
);
110 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &tree_id
));
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));
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
);
123 cl_git_pass(git_reference_symbolic_create(&head
, g_repo
, "HEAD", branch_name
, 1, NULL
));
125 cl_git_pass(git_commit_create_v(
126 &commit_id
, /* out id */
136 git_object_free((git_object
*)tree
);
137 git_signature_free(author
);
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.
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
));
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
));
158 git_signature_free(committer
);
159 git_reflog_free(log
);