]>
Commit | Line | Data |
---|---|---|
8fae76c5 | 1 | #include "clay_libgit2.h" |
2 | #include "posix.h" | |
3 | ||
4 | static git_repository *repo; | |
5 | ||
6 | static void file_create(const char *filename, const char *content) | |
7 | { | |
8 | int fd; | |
9 | ||
10 | fd = p_creat(filename, 0666); | |
11 | cl_assert(fd != 0); | |
12 | cl_git_pass(p_write(fd, content, strlen(content))); | |
13 | cl_git_pass(p_close(fd)); | |
14 | } | |
15 | ||
16 | void test_object_commit_commitstagedfile__initialize(void) | |
17 | { | |
18 | cl_fixture("treebuilder"); | |
19 | cl_git_pass(git_repository_init(&repo, "treebuilder/", 0)); | |
20 | cl_git_pass(git_repository_open(&repo, "treebuilder/.git")); | |
21 | cl_assert(repo != NULL); | |
22 | } | |
23 | ||
24 | void test_object_commit_commitstagedfile__cleanup(void) | |
25 | { | |
26 | git_repository_free(repo); | |
27 | cl_fixture_cleanup("treebuilder"); | |
28 | } | |
29 | ||
30 | void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) | |
31 | { | |
32 | git_index *index; | |
33 | git_index_entry *entry; | |
34 | git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid; | |
35 | git_signature *signature; | |
36 | git_tree *tree; | |
37 | ||
38 | /* | |
39 | * The test below replicates the following git scenario | |
40 | * | |
41 | * $ echo "test" > test.txt | |
42 | * $ git hash-object test.txt | |
43 | * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 | |
44 | * | |
45 | * $ git add . | |
46 | * $ git commit -m "Initial commit" | |
47 | * | |
48 | * $ git log | |
49 | * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d | |
50 | * Author: nulltoken <emeric.fermas@gmail.com> | |
51 | * Date: Wed Dec 14 08:29:03 2011 +0100 | |
52 | * | |
53 | * Initial commit | |
54 | * | |
55 | * $ git show 1fe3 --format=raw | |
56 | * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d | |
57 | * tree 2b297e643c551e76cfa1f93810c50811382f9117 | |
58 | * author nulltoken <emeric.fermas@gmail.com> 1323847743 +0100 | |
59 | * committer nulltoken <emeric.fermas@gmail.com> 1323847743 +0100 | |
60 | * | |
61 | * Initial commit | |
62 | * | |
63 | * diff --git a/test.txt b/test.txt | |
64 | * new file mode 100644 | |
65 | * index 0000000..9daeafb | |
66 | * --- /dev/null | |
67 | * +++ b/test.txt | |
68 | * @@ -0,0 +1 @@ | |
69 | * +test | |
70 | * | |
71 | * $ git ls-tree 2b297 | |
72 | * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt | |
73 | */ | |
74 | ||
75 | cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d")); | |
76 | cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117")); | |
77 | cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4")); | |
78 | ||
79 | /* | |
80 | * Add a new file to the index | |
81 | */ | |
82 | file_create("treebuilder/test.txt", "test\n"); | |
83 | cl_git_pass(git_repository_index(&index, repo)); | |
84 | cl_git_pass(git_index_add(index, "test.txt", 0)); | |
85 | ||
86 | entry = git_index_get(index, 0); | |
87 | ||
88 | cl_assert(git_oid_cmp(&expected_blob_oid, &entry->oid) == 0); | |
89 | ||
be00b00d RB |
90 | /* |
91 | * Information about index entry should match test file | |
92 | */ | |
93 | { | |
94 | struct stat st; | |
95 | cl_must_pass(p_lstat("treebuilder/test.txt", &st)); | |
96 | cl_assert(entry->file_size == st.st_size); | |
97 | cl_assert(entry->uid == st.st_uid); | |
98 | cl_assert(entry->gid == st.st_gid); | |
99 | } | |
100 | ||
8fae76c5 | 101 | /* |
102 | * Build the tree from the index | |
103 | */ | |
104 | cl_git_pass(git_tree_create_fromindex(&tree_oid, index)); | |
105 | ||
106 | cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0); | |
107 | ||
108 | /* | |
109 | * Commit the staged file | |
110 | */ | |
111 | cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); | |
112 | cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid)); | |
113 | cl_git_pass(git_commit_create_v( | |
114 | &commit_oid, | |
115 | repo, | |
116 | "HEAD", | |
117 | signature, | |
118 | signature, | |
119 | NULL, | |
120 | "Initial commit\n", // Note: the trailing linefeed is mandatory to replicate git behavior | |
121 | tree, | |
122 | 0)); | |
123 | ||
124 | cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0); | |
125 | ||
126 | git_signature_free(signature); | |
127 | git_tree_free(tree); | |
128 | git_index_free(index); | |
129 | } |