]> git.proxmox.com Git - libgit2.git/blame - tests/object/tree/duplicateentries.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / object / tree / duplicateentries.c
CommitLineData
29cc374d 1#include "clar_libgit2.h"
2#include "tree.h"
3
4static git_repository *_repo;
5
6void test_object_tree_duplicateentries__initialize(void) {
7 _repo = cl_git_sandbox_init("testrepo");
8}
9
10void test_object_tree_duplicateentries__cleanup(void) {
11 cl_git_sandbox_cleanup();
12}
13
14/*
15 * $ git show --format=raw refs/heads/dir
16 * commit 144344043ba4d4a405da03de3844aa829ae8be0e
17 * tree d52a8fe84ceedf260afe4f0287bbfca04a117e83
18 * parent cf80f8de9f1185bf3a05f993f6121880dd0cfbc9
19 * author Ben Straub <bstraub@github.com> 1343755506 -0700
20 * committer Ben Straub <bstraub@github.com> 1343755506 -0700
ac3d33df 21 *
29cc374d 22 * Change a file mode
ac3d33df 23 *
29cc374d 24 * diff --git a/a/b.txt b/a/b.txt
25 * old mode 100644
26 * new mode 100755
27 *
28 * $ git ls-tree d52a8fe84ceedf260afe4f0287bbfca04a117e83
29 * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README
30 * 040000 tree 4e0883eeeeebc1fb1735161cea82f7cb5fab7e63 a
31 * 100644 blob 45b983be36b73c0788dc9cbcb76cbb80fc7bb057 branch_file.txt
32 * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt
33 */
34
35static void tree_checker(
36 git_oid *tid,
37 const char *expected_sha,
38 git_filemode_t expected_filemode)
39{
40 git_tree *tree;
41 const git_tree_entry *entry;
42 git_oid oid;
43
44 cl_git_pass(git_tree_lookup(&tree, _repo, tid));
a8122b5d 45 cl_assert_equal_i(1, (int)git_tree_entrycount(tree));
29cc374d 46 entry = git_tree_entry_byindex(tree, 0);
47
48 cl_git_pass(git_oid_fromstr(&oid, expected_sha));
49
50 cl_assert_equal_i(0, git_oid_cmp(&oid, git_tree_entry_id(entry)));
51 cl_assert_equal_i(expected_filemode, git_tree_entry_filemode(entry));
52
53 git_tree_free(tree);
54}
55
56static void tree_creator(git_oid *out, void (*fn)(git_treebuilder *))
57{
58 git_treebuilder *builder;
59
208a2c8a 60 cl_git_pass(git_treebuilder_new(&builder, _repo, NULL));
29cc374d 61
62 fn(builder);
63
dce7b1a4 64 cl_git_pass(git_treebuilder_write(out, builder));
29cc374d 65 git_treebuilder_free(builder);
66}
67
68static void two_blobs(git_treebuilder *bld)
69{
70 git_oid oid;
71 const git_tree_entry *entry;
ac3d33df 72
29cc374d 73 cl_git_pass(git_oid_fromstr(&oid,
74 "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); /* blob oid (README) */
75
76 cl_git_pass(git_treebuilder_insert(
77 &entry, bld, "duplicate", &oid,
78 GIT_FILEMODE_BLOB));
79
80 cl_git_pass(git_oid_fromstr(&oid,
81 "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd")); /* blob oid (new.txt) */
82
83 cl_git_pass(git_treebuilder_insert(
84 &entry, bld, "duplicate", &oid,
85 GIT_FILEMODE_BLOB));
86}
87
88static void one_blob_and_one_tree(git_treebuilder *bld)
89{
90 git_oid oid;
91 const git_tree_entry *entry;
ac3d33df 92
29cc374d 93 cl_git_pass(git_oid_fromstr(&oid,
94 "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); /* blob oid (README) */
95
96 cl_git_pass(git_treebuilder_insert(
97 &entry, bld, "duplicate", &oid,
98 GIT_FILEMODE_BLOB));
99
100 cl_git_pass(git_oid_fromstr(&oid,
101 "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63")); /* tree oid (a) */
102
103 cl_git_pass(git_treebuilder_insert(
104 &entry, bld, "duplicate", &oid,
105 GIT_FILEMODE_TREE));
106}
107
108void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_through_the_treebuilder(void)
109{
110 git_oid tid;
111
112 tree_creator(&tid, two_blobs);
113 tree_checker(&tid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", GIT_FILEMODE_BLOB);
ac3d33df 114
29cc374d 115 tree_creator(&tid, one_blob_and_one_tree);
116 tree_checker(&tid, "4e0883eeeeebc1fb1735161cea82f7cb5fab7e63", GIT_FILEMODE_TREE);
117}
f92bcaea 118
ef887151 119static void add_fake_conflicts(git_index *index)
f92bcaea 120{
121 git_index_entry ancestor_entry, our_entry, their_entry;
122
123 memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
124 memset(&our_entry, 0x0, sizeof(git_index_entry));
125 memset(&their_entry, 0x0, sizeof(git_index_entry));
126
127 ancestor_entry.path = "duplicate";
128 ancestor_entry.mode = GIT_FILEMODE_BLOB;
ac3d33df 129 GIT_INDEX_ENTRY_STAGE_SET(&ancestor_entry, 1);
d541170c 130 git_oid_fromstr(&ancestor_entry.id, "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
f92bcaea 131
132 our_entry.path = "duplicate";
133 our_entry.mode = GIT_FILEMODE_BLOB;
ac3d33df 134 GIT_INDEX_ENTRY_STAGE_SET(&our_entry, 2);
d541170c 135 git_oid_fromstr(&our_entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
f92bcaea 136
137 their_entry.path = "duplicate";
138 their_entry.mode = GIT_FILEMODE_BLOB;
ac3d33df 139 GIT_INDEX_ENTRY_STAGE_SET(&their_entry, 3);
d541170c 140 git_oid_fromstr(&their_entry.id, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
f92bcaea 141
142 cl_git_pass(git_index_conflict_add(index, &ancestor_entry, &our_entry, &their_entry));
143}
144
145void test_object_tree_duplicateentries__cannot_create_a_duplicate_entry_building_a_tree_from_a_index_with_conflicts(void)
146{
147 git_index *index;
148 git_oid tid;
149
150 cl_git_pass(git_repository_index(&index, _repo));
151
ac3d33df 152 add_fake_conflicts(index);
f92bcaea 153
154 cl_assert_equal_i(GIT_EUNMERGED, git_index_write_tree(&tid, index));
155
156 git_index_free(index);
157}