Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sha1.h>
24 #include <string.h>
25 #include <zlib.h>
27 #include "got_error.h"
28 #include "got_reference.h"
29 #include "got_repository.h"
30 #include "got_worktree.h"
31 #include "got_object.h"
33 #include "got_lib_path.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_zbuf.h"
36 #include "got_lib_object.h"
37 #include "got_lib_pack.h"
38 #include "got_lib_repository.h"
39 #include "got_lib_worktree.h"
40 #include "got_lib_object_idset.h"
42 #ifndef nitems
43 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #endif
46 #define GOT_GIT_DIR ".git"
48 /* Mandatory files and directories inside the git directory. */
49 #define GOT_OBJECTS_DIR "objects"
50 #define GOT_REFS_DIR "refs"
51 #define GOT_HEAD_FILE "HEAD"
53 /* Other files and directories inside the git directory. */
54 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
55 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
56 #define GOT_OBJECTS_PACK_DIR "objects/pack"
58 char *
59 got_repo_get_path(struct got_repository *repo)
60 {
61 return strdup(repo->path);
62 }
64 char *
65 got_repo_get_path_git_dir(struct got_repository *repo)
66 {
67 return strdup(repo->path_git_dir);
68 }
70 static char *
71 get_path_git_child(struct got_repository *repo, const char *basename)
72 {
73 char *path_child;
75 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
76 basename) == -1)
77 return NULL;
79 return path_child;
80 }
82 char *
83 got_repo_get_path_objects(struct got_repository *repo)
84 {
85 return get_path_git_child(repo, GOT_OBJECTS_DIR);
86 }
88 char *
89 got_repo_get_path_objects_pack(struct got_repository *repo)
90 {
91 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
92 }
94 char *
95 got_repo_get_path_refs(struct got_repository *repo)
96 {
97 return get_path_git_child(repo, GOT_REFS_DIR);
98 }
100 static char *
101 get_path_head(struct got_repository *repo)
103 return get_path_git_child(repo, GOT_HEAD_FILE);
106 static int
107 is_git_repo(struct got_repository *repo)
109 char *path_git = got_repo_get_path_git_dir(repo);
110 char *path_objects = got_repo_get_path_objects(repo);
111 char *path_refs = got_repo_get_path_refs(repo);
112 char *path_head = get_path_head(repo);
113 int ret = 0;
114 struct stat sb;
115 struct got_reference *head_ref;
117 if (lstat(path_git, &sb) == -1)
118 goto done;
119 if (!S_ISDIR(sb.st_mode))
120 goto done;
122 if (lstat(path_objects, &sb) == -1)
123 goto done;
124 if (!S_ISDIR(sb.st_mode))
125 goto done;
127 if (lstat(path_refs, &sb) == -1)
128 goto done;
129 if (!S_ISDIR(sb.st_mode))
130 goto done;
132 if (lstat(path_head, &sb) == -1)
133 goto done;
134 if (!S_ISREG(sb.st_mode))
135 goto done;
137 /* Check if the HEAD reference can be opened. */
138 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
139 goto done;
140 got_ref_close(head_ref);
142 ret = 1;
143 done:
144 free(path_git);
145 free(path_objects);
146 free(path_refs);
147 free(path_head);
148 return ret;
152 const struct got_error *
153 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
154 struct got_object *obj)
156 const struct got_error *err = NULL;
157 struct got_objcache_entry *ce;
159 if (repo->ncached >= GOT_OBJECT_CACHE_SIZE) {
160 err = got_object_idset_remove_random((void **)&ce,
161 repo->objcache);
162 if (err)
163 return err;
164 got_object_close(ce->obj);
165 free(ce);
166 repo->ncached--;
169 ce = calloc(1, sizeof(*ce));
170 if (ce == NULL)
171 return got_error_from_errno();
172 memcpy(&ce->id, id, sizeof(ce->id));
173 ce->obj = obj;
174 err = got_object_idset_add(NULL, repo->objcache, id, ce);
175 if (err) {
176 if (err->code == GOT_ERR_OBJ_EXISTS) {
177 free(ce);
178 err = NULL;
180 } else {
181 obj->refcnt++;
182 repo->ncached++;
185 return err;
188 struct got_object *
189 got_repo_get_cached_object(struct got_repository *repo,
190 struct got_object_id *id)
192 struct got_objcache_entry *ce;
194 ce = got_object_idset_get(repo->objcache, id);
195 if (ce) {
196 repo->cache_hit++;
197 return ce->obj;
199 repo->cache_miss++;
200 return NULL;
203 const struct got_error *
204 got_repo_open(struct got_repository **ret, const char *path)
206 struct got_repository *repo = NULL;
207 const struct got_error *err = NULL;
208 char *abspath;
210 if (got_path_is_absolute(path))
211 abspath = strdup(path);
212 else
213 abspath = got_path_get_absolute(path);
214 if (abspath == NULL)
215 return got_error(GOT_ERR_BAD_PATH);
217 repo = calloc(1, sizeof(*repo));
218 if (repo == NULL) {
219 err = got_error_from_errno();
220 goto done;
223 repo->objcache = got_object_idset_alloc();
224 if (repo->objcache == NULL) {
225 err = got_error_from_errno();
226 goto done;
229 repo->path = got_path_normalize(abspath);
230 if (repo->path == NULL) {
231 err = got_error(GOT_ERR_BAD_PATH);
232 goto done;
235 repo->path_git_dir = strdup(repo->path);
236 if (repo->path_git_dir == NULL) {
237 err = got_error_from_errno();
238 goto done;
240 if (!is_git_repo(repo)) {
241 free(repo->path_git_dir);
242 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
243 GOT_GIT_DIR) == -1) {
244 err = got_error_from_errno();
245 goto done;
247 if (!is_git_repo(repo)) {
248 struct got_worktree *worktree;
249 if (got_worktree_open(&worktree, repo->path) == NULL) {
250 free(repo->path_git_dir);
251 repo->path_git_dir =
252 strdup(worktree->repo_path);
253 if (repo->path_git_dir == NULL) {
254 err = got_error_from_errno();
255 goto done;
257 if (!is_git_repo(repo)) {
258 free(repo->path_git_dir);
259 if (asprintf(&repo->path_git_dir,
260 "%s/%s", worktree->repo_path,
261 GOT_GIT_DIR) == -1) {
262 err = got_error_from_errno();
263 goto done;
266 got_worktree_close(worktree);
269 if (!is_git_repo(repo)) {
270 err = got_error(GOT_ERR_NOT_GIT_REPO);
271 goto done;
275 *ret = repo;
276 done:
277 if (err)
278 got_repo_close(repo);
279 free(abspath);
280 return err;
283 void
284 got_repo_close(struct got_repository *repo)
286 int i;
288 for (i = 0; i < nitems(repo->packidx_cache); i++) {
289 if (repo->packidx_cache[i] == NULL)
290 break;
291 got_packidx_close(repo->packidx_cache[i]);
294 for (i = 0; i < nitems(repo->packs); i++) {
295 if (repo->packs[i].path_packfile == NULL)
296 break;
297 got_pack_close(&repo->packs[i]);
300 free(repo->path);
301 free(repo->path_git_dir);
302 if (repo->objcache)
303 got_object_idset_free(repo->objcache);
304 free(repo);