]> git.proxmox.com Git - libgit2.git/blame - src/repository.c
Redesigned the walking/object lookup interface
[libgit2.git] / src / repository.c
CommitLineData
3315782c
VM
1/*
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.
5 *
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.)
14 *
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.
19 *
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.
24 */
25
26#include "common.h"
27#include "repository.h"
28#include "commit.h"
29#include "tag.h"
30
31static const int default_table_size = 32;
32static const double max_load_factor = 0.65;
33
34uint32_t git_repository_object_hash(const void *key)
35{
36 uint32_t r;
37 git_oid *id;
38
39 id = (git_oid *)key;
40 memcpy(&r, id->id, sizeof(r));
41 return r;
42}
43
44int git_repository_object_haskey(void *object, const void *key)
45{
46 git_repository_object *obj;
47 git_oid *oid;
48
49 obj = (git_repository_object *)object;
50 oid = (git_oid *)key;
51
52 return (git_oid_cmp(oid, &obj->id) == 0);
53}
54
55git_repository *git_repository_alloc(git_odb *odb)
56{
57 git_repository *repo = git__malloc(sizeof(git_repository));
58 if (!repo)
59 return NULL;
60
61 memset(repo, 0x0, sizeof(git_repository));
62
63 repo->objects = git_hashtable_alloc(
64 default_table_size,
65 git_repository_object_hash,
66 git_repository_object_haskey);
67
68 if (repo->objects == NULL) {
69 free(repo);
70 return NULL;
71 }
72
73 repo->db = odb; /* TODO: create ODB manually! */
74
75 return repo;
76}
77
78void git_repository_free(git_repository *repo)
79{
80 git_hashtable_iterator it;
81 git_repository_object *object;
82
83 git_hashtable_iterator_init(repo->objects, &it);
84
85 while ((object = (git_repository_object *)
86 git_hashtable_iterator_next(&it)) != NULL) {
87
88 switch (object->type) {
89 case GIT_OBJ_COMMIT:
90 git_commit__free((git_commit *)object);
91 break;
92
93 case GIT_OBJ_TREE:
94 git_tree__free((git_tree *)object);
95 break;
96
97 case GIT_OBJ_TAG:
98 git_tag__free((git_tag *)object);
99 break;
100
101 default:
102 free(object);
103 break;
104 }
105 }
106
107 git_hashtable_free(repo->objects);
108 /* TODO: free odb */
109 free(repo);
110}
111
112git_repository_object *git_repository_lookup(git_repository *repo, const git_oid *id, git_otype type)
113{
114 git_repository_object *object = NULL;
115 git_obj obj_file;
116
117 assert(repo);
118
119 object = git_hashtable_lookup(repo->objects, id);
120 if (object != NULL)
121 return object;
122
123 if (git_odb_read(&obj_file, repo->db, id) < 0 ||
124 (type != GIT_OBJ_ANY && type != obj_file.type))
125 return NULL;
126
127 object = git__malloc(sizeof(git_commit));
128
129 if (object == NULL)
130 return NULL;
131
132 memset(object, 0x0, sizeof(git_commit));
133
134 /* Initialize parent object */
135 git_oid_cpy(&object->id, id);
136 object->repo = repo;
137 object->type = obj_file.type;
138
139 git_hashtable_insert(repo->objects, &object->id, object);
140 git_obj_close(&obj_file);
141
142 return object;
143}