2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 #include "repository.h"
31 static const int default_table_size
= 32;
32 static const double max_load_factor
= 0.65;
34 uint32_t git_repository_object_hash(const void *key
)
40 memcpy(&r
, id
->id
, sizeof(r
));
44 int git_repository_object_haskey(void *object
, const void *key
)
46 git_repository_object
*obj
;
49 obj
= (git_repository_object
*)object
;
52 return (git_oid_cmp(oid
, &obj
->id
) == 0);
55 git_repository
*git_repository_alloc(git_odb
*odb
)
57 git_repository
*repo
= git__malloc(sizeof(git_repository
));
61 memset(repo
, 0x0, sizeof(git_repository
));
63 repo
->objects
= git_hashtable_alloc(
65 git_repository_object_hash
,
66 git_repository_object_haskey
);
68 if (repo
->objects
== NULL
) {
73 repo
->db
= odb
; /* TODO: create ODB manually! */
78 void git_repository_free(git_repository
*repo
)
80 git_hashtable_iterator it
;
81 git_repository_object
*object
;
83 git_hashtable_iterator_init(repo
->objects
, &it
);
85 while ((object
= (git_repository_object
*)
86 git_hashtable_iterator_next(&it
)) != NULL
) {
88 git_obj_close(&object
->dbo
);
90 switch (object
->dbo
.type
) {
92 git_commit__free((git_commit
*)object
);
96 git_tree__free((git_tree
*)object
);
100 git_tag__free((git_tag
*)object
);
109 git_hashtable_free(repo
->objects
);
114 int git_repository__open_dbo(git_repository_object
*object
)
118 if (object
->dbo_open
)
121 error
= git_odb_read(&object
->dbo
, object
->repo
->db
, &object
->id
);
125 object
->dbo_open
= 1;
129 void git_repository__close_dbo(git_repository_object
*object
)
131 if (!object
->dbo_open
) {
132 git_obj_close(&object
->dbo
);
133 object
->dbo_open
= 0;
137 git_repository_object
*git_repository_lookup(git_repository
*repo
, const git_oid
*id
, git_otype type
)
139 static const size_t object_sizes
[] = {
143 sizeof(git_repository_object
), /* TODO: sizeof(git_blob) */
147 git_repository_object
*object
= NULL
;
152 object
= git_hashtable_lookup(repo
->objects
, id
);
156 if (git_odb_read(&obj_file
, repo
->db
, id
) < 0)
159 if (type
!= GIT_OBJ_ANY
&& type
!= obj_file
.type
)
162 type
= obj_file
.type
;
164 object
= git__malloc(object_sizes
[type
]);
169 memset(object
, 0x0, object_sizes
[type
]);
171 /* Initialize parent object */
172 git_oid_cpy(&object
->id
, id
);
174 object
->dbo_open
= 1;
175 memcpy(&object
->dbo
, &obj_file
, sizeof(git_obj
));
180 if (git_commit__parse_basic((git_commit
*)object
) < 0) {
188 if (git_tree__parse((git_tree
*)object
) < 0) {
196 if (git_tag__parse((git_tag
*)object
) < 0) {
204 /* blobs get no parsing */
208 git_obj_close(&object
->dbo
);
209 object
->dbo_open
= 0;
211 git_hashtable_insert(repo
->objects
, &object
->id
, object
);