]> git.proxmox.com Git - libgit2.git/blob - tests/index/read_index.c
Merge pull request #3808 from ethomson/read_index_fixes
[libgit2.git] / tests / index / read_index.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "index.h"
4
5 static git_repository *_repo;
6 static git_index *_index;
7
8 void test_index_read_index__initialize(void)
9 {
10 git_object *head;
11 git_reference *head_ref;
12
13 _repo = cl_git_sandbox_init("testrepo");
14 cl_git_pass(git_revparse_ext(&head, &head_ref, _repo, "HEAD"));
15 cl_git_pass(git_reset(_repo, head, GIT_RESET_HARD, NULL));
16 cl_git_pass(git_repository_index(&_index, _repo));
17
18 git_reference_free(head_ref);
19 git_object_free(head);
20 }
21
22 void test_index_read_index__cleanup(void)
23 {
24 git_index_free(_index);
25 cl_git_sandbox_cleanup();
26 }
27
28 void test_index_read_index__maintains_stat_cache(void)
29 {
30 git_index *new_index;
31 git_oid index_id;
32 git_index_entry new_entry;
33 const git_index_entry *e;
34 git_tree *tree;
35 size_t i;
36
37 cl_assert_equal_i(4, git_index_entrycount(_index));
38
39 /* write-tree */
40 cl_git_pass(git_index_write_tree(&index_id, _index));
41
42 /* read-tree, then read index */
43 git_tree_lookup(&tree, _repo, &index_id);
44 cl_git_pass(git_index_new(&new_index));
45 cl_git_pass(git_index_read_tree(new_index, tree));
46 git_tree_free(tree);
47
48 /* add a new entry that will not have stat data */
49 memset(&new_entry, 0, sizeof(git_index_entry));
50 new_entry.path = "Hello";
51 git_oid_fromstr(&new_entry.id, "0123456789012345678901234567890123456789");
52 new_entry.file_size = 1234;
53 new_entry.mode = 0100644;
54 cl_git_pass(git_index_add(new_index, &new_entry));
55 cl_assert_equal_i(5, git_index_entrycount(new_index));
56
57 cl_git_pass(git_index_read_index(_index, new_index));
58 git_index_free(new_index);
59
60 cl_assert_equal_i(5, git_index_entrycount(_index));
61
62 for (i = 0; i < git_index_entrycount(_index); i++) {
63 e = git_index_get_byindex(_index, i);
64
65 if (strcmp(e->path, "Hello") == 0) {
66 cl_assert_equal_i(0, e->ctime.seconds);
67 cl_assert_equal_i(0, e->mtime.seconds);
68 } else {
69 cl_assert(0 != e->ctime.seconds);
70 cl_assert(0 != e->mtime.seconds);
71 }
72 }
73 }
74
75 static bool roundtrip_with_read_index(const char *tree_idstr)
76 {
77 git_oid tree_id, new_tree_id;
78 git_tree *tree;
79 git_index *tree_index;
80
81 cl_git_pass(git_oid_fromstr(&tree_id, tree_idstr));
82 cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
83 cl_git_pass(git_index_new(&tree_index));
84 cl_git_pass(git_index_read_tree(tree_index, tree));
85 cl_git_pass(git_index_read_index(_index, tree_index));
86 cl_git_pass(git_index_write_tree(&new_tree_id, _index));
87
88 git_tree_free(tree);
89 git_index_free(tree_index);
90
91 return git_oid_equal(&tree_id, &new_tree_id);
92 }
93
94 void test_index_read_index__produces_treesame_indexes(void)
95 {
96 roundtrip_with_read_index("53fc32d17276939fc79ed05badaef2db09990016");
97 roundtrip_with_read_index("944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
98 roundtrip_with_read_index("1810dff58d8a660512d4832e740f692884338ccd");
99 roundtrip_with_read_index("d52a8fe84ceedf260afe4f0287bbfca04a117e83");
100 roundtrip_with_read_index("c36d8ea75da8cb510fcb0c408c1d7e53f9a99dbe");
101 roundtrip_with_read_index("7b2417a23b63e1fdde88c80e14b33247c6e5785a");
102 roundtrip_with_read_index("f82a8eb4cb20e88d1030fd10d89286215a715396");
103 roundtrip_with_read_index("fd093bff70906175335656e6ce6ae05783708765");
104 roundtrip_with_read_index("ae90f12eea699729ed24555e40b9fd669da12a12");
105 }
106
107 void test_index_read_index__read_and_writes(void)
108 {
109 git_oid tree_id, new_tree_id;
110 git_tree *tree;
111 git_index *tree_index, *new_index;
112
113 cl_git_pass(git_oid_fromstr(&tree_id, "ae90f12eea699729ed24555e40b9fd669da12a12"));
114 cl_git_pass(git_tree_lookup(&tree, _repo, &tree_id));
115 cl_git_pass(git_index_new(&tree_index));
116 cl_git_pass(git_index_read_tree(tree_index, tree));
117 cl_git_pass(git_index_read_index(_index, tree_index));
118 cl_git_pass(git_index_write(_index));
119
120 cl_git_pass(git_index_open(&new_index, git_index_path(_index)));
121 cl_git_pass(git_index_write_tree_to(&new_tree_id, new_index, _repo));
122
123 cl_assert_equal_oid(&tree_id, &new_tree_id);
124
125 git_tree_free(tree);
126 git_index_free(tree_index);
127 git_index_free(new_index);
128 }