]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/index/collision.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / index / collision.c
CommitLineData
95fbedcd
RB
1#include "clar_libgit2.h"
2#include "git2/repository.h"
3#include "git2/index.h"
4
9bc8c80f
ET
5static git_repository *g_repo;
6static git_odb *g_odb;
7static git_index *g_index;
8static git_oid g_empty_id;
9
10void test_index_collision__initialize(void)
11{
12 g_repo = cl_git_sandbox_init("empty_standard_repo");
13 cl_git_pass(git_repository_odb(&g_odb, g_repo));
14 cl_git_pass(git_repository_index(&g_index, g_repo));
15
ac3d33df 16 cl_git_pass(git_odb_write(&g_empty_id, g_odb, "", 0, GIT_OBJECT_BLOB));
9bc8c80f 17}
95fbedcd
RB
18
19void test_index_collision__cleanup(void)
20{
9bc8c80f
ET
21 git_index_free(g_index);
22 git_odb_free(g_odb);
95fbedcd 23 cl_git_sandbox_cleanup();
95fbedcd
RB
24}
25
ac3d33df 26void test_index_collision__add_blob_with_conflicting_file(void)
95fbedcd 27{
95fbedcd 28 git_index_entry entry;
ac3d33df 29 git_tree_entry *tentry;
95fbedcd
RB
30 git_oid tree_id;
31 git_tree *tree;
32
95fbedcd
RB
33 memset(&entry, 0, sizeof(entry));
34 entry.ctime.seconds = 12346789;
35 entry.mtime.seconds = 12346789;
36 entry.mode = 0100644;
37 entry.file_size = 0;
9bc8c80f 38 git_oid_cpy(&entry.id, &g_empty_id);
95fbedcd
RB
39
40 entry.path = "a/b";
9bc8c80f 41 cl_git_pass(git_index_add(g_index, &entry));
95fbedcd 42
ac3d33df
JK
43 /* Check a/b exists here */
44 cl_git_pass(git_index_write_tree(&tree_id, g_index));
45 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
46 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
47 git_tree_entry_free(tentry);
48 git_tree_free(tree);
49
95fbedcd
RB
50 /* create a tree/blob collision */
51 entry.path = "a/b/c";
ac3d33df 52 cl_git_pass(git_index_add(g_index, &entry));
95fbedcd 53
ac3d33df 54 /* a/b should now be a tree and a/b/c a blob */
9bc8c80f
ET
55 cl_git_pass(git_index_write_tree(&tree_id, g_index));
56 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
ac3d33df
JK
57 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
58 git_tree_entry_free(tentry);
59 git_tree_free(tree);
60}
61
62void test_index_collision__add_blob_with_conflicting_dir(void)
63{
64 git_index_entry entry;
65 git_tree_entry *tentry;
66 git_oid tree_id;
67 git_tree *tree;
95fbedcd 68
ac3d33df
JK
69 memset(&entry, 0, sizeof(entry));
70 entry.ctime.seconds = 12346789;
71 entry.mtime.seconds = 12346789;
72 entry.mode = 0100644;
73 entry.file_size = 0;
74 git_oid_cpy(&entry.id, &g_empty_id);
75
76 entry.path = "a/b/c";
77 cl_git_pass(git_index_add(g_index, &entry));
78
79 /* Check a/b/c exists here */
80 cl_git_pass(git_index_write_tree(&tree_id, g_index));
81 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
82 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
83 git_tree_entry_free(tentry);
84 git_tree_free(tree);
85
86 /* create a blob/tree collision */
87 entry.path = "a/b";
88 cl_git_pass(git_index_add(g_index, &entry));
89
90 /* a/b should now be a tree and a/b/c a blob */
91 cl_git_pass(git_index_write_tree(&tree_id, g_index));
92 cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
93 cl_git_pass(git_tree_entry_bypath(&tentry, tree, "a/b"));
94 cl_git_fail(git_tree_entry_bypath(&tentry, tree, "a/b/c"));
95 git_tree_entry_free(tentry);
95fbedcd 96 git_tree_free(tree);
95fbedcd 97}
bae8bea0
ET
98
99void test_index_collision__add_with_highstage_1(void)
100{
bae8bea0
ET
101 git_index_entry entry;
102
bae8bea0
ET
103 memset(&entry, 0, sizeof(entry));
104 entry.ctime.seconds = 12346789;
105 entry.mtime.seconds = 12346789;
106 entry.mode = 0100644;
107 entry.file_size = 0;
9bc8c80f 108 git_oid_cpy(&entry.id, &g_empty_id);
bae8bea0
ET
109
110 entry.path = "a/b";
ac3d33df 111 GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
9bc8c80f 112 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0
ET
113
114 /* create a blob beneath the previous tree entry */
115 entry.path = "a/b/c";
116 entry.flags = 0;
9bc8c80f 117 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0
ET
118
119 /* create another tree entry above the blob */
120 entry.path = "a/b";
ac3d33df 121 GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
9bc8c80f 122 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0
ET
123}
124
125void test_index_collision__add_with_highstage_2(void)
126{
bae8bea0
ET
127 git_index_entry entry;
128
bae8bea0
ET
129 memset(&entry, 0, sizeof(entry));
130 entry.ctime.seconds = 12346789;
131 entry.mtime.seconds = 12346789;
132 entry.mode = 0100644;
133 entry.file_size = 0;
9bc8c80f 134 git_oid_cpy(&entry.id, &g_empty_id);
bae8bea0
ET
135
136 entry.path = "a/b/c";
ac3d33df 137 GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
9bc8c80f 138 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0
ET
139
140 /* create a blob beneath the previous tree entry */
141 entry.path = "a/b/c";
ac3d33df 142 GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
9bc8c80f 143 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0
ET
144
145 /* create another tree entry above the blob */
146 entry.path = "a/b";
ac3d33df 147 GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
9bc8c80f 148 cl_git_pass(git_index_add(g_index, &entry));
bae8bea0 149}