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"
41 #ifndef nitems
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #endif
45 #define GOT_GIT_DIR ".git"
47 /* Mandatory files and directories inside the git directory. */
48 #define GOT_OBJECTS_DIR "objects"
49 #define GOT_REFS_DIR "refs"
50 #define GOT_HEAD_FILE "HEAD"
52 /* Other files and directories inside the git directory. */
53 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
54 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
55 #define GOT_OBJECTS_PACK_DIR "objects/pack"
57 char *
58 got_repo_get_path(struct got_repository *repo)
59 {
60 return strdup(repo->path);
61 }
63 char *
64 got_repo_get_path_git_dir(struct got_repository *repo)
65 {
66 return strdup(repo->path_git_dir);
67 }
69 static char *
70 get_path_git_child(struct got_repository *repo, const char *basename)
71 {
72 char *path_child;
74 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
75 basename) == -1)
76 return NULL;
78 return path_child;
79 }
81 char *
82 got_repo_get_path_objects(struct got_repository *repo)
83 {
84 return get_path_git_child(repo, GOT_OBJECTS_DIR);
85 }
87 char *
88 got_repo_get_path_objects_pack(struct got_repository *repo)
89 {
90 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
91 }
93 char *
94 got_repo_get_path_refs(struct got_repository *repo)
95 {
96 return get_path_git_child(repo, GOT_REFS_DIR);
97 }
99 static char *
100 get_path_head(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_HEAD_FILE);
105 static int
106 is_git_repo(struct got_repository *repo)
108 char *path_git = got_repo_get_path_git_dir(repo);
109 char *path_objects = got_repo_get_path_objects(repo);
110 char *path_refs = got_repo_get_path_refs(repo);
111 char *path_head = get_path_head(repo);
112 int ret = 0;
113 struct stat sb;
114 struct got_reference *head_ref;
116 if (lstat(path_git, &sb) == -1)
117 goto done;
118 if (!S_ISDIR(sb.st_mode))
119 goto done;
121 if (lstat(path_objects, &sb) == -1)
122 goto done;
123 if (!S_ISDIR(sb.st_mode))
124 goto done;
126 if (lstat(path_refs, &sb) == -1)
127 goto done;
128 if (!S_ISDIR(sb.st_mode))
129 goto done;
131 if (lstat(path_head, &sb) == -1)
132 goto done;
133 if (!S_ISREG(sb.st_mode))
134 goto done;
136 /* Check if the HEAD reference can be opened. */
137 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
138 goto done;
139 got_ref_close(head_ref);
141 ret = 1;
142 done:
143 free(path_git);
144 free(path_objects);
145 free(path_refs);
146 free(path_head);
147 return ret;
151 const struct got_error*
152 got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
153 struct got_object *obj)
155 struct got_objcache_entry *ce;
157 if (repo->ncached >= GOT_OBJECT_CACHE_SIZE) {
158 ce = SIMPLEQ_FIRST(&repo->objcache);
159 SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
160 got_object_close(ce->obj);
161 free(ce);
162 repo->ncached--;
165 ce = calloc(1, sizeof(*ce));
166 if (ce == NULL)
167 return got_error_from_errno();
168 memcpy(&ce->id, id, sizeof(ce->id));
169 ce->obj = obj;
170 obj->refcnt++;
171 SIMPLEQ_INSERT_HEAD(&repo->objcache, ce, entry);
172 repo->ncached++;
173 return NULL;
176 struct got_object *
177 got_repo_get_cached_object(struct got_repository *repo,
178 struct got_object_id *id)
180 struct got_objcache_entry *ce;
182 SIMPLEQ_FOREACH(ce, &repo->objcache, entry) {
183 if (got_object_id_cmp(&ce->id, id) != 0)
184 continue;
185 repo->cache_hit++;
186 return ce->obj;
188 repo->cache_miss++;
189 return NULL;
192 const struct got_error *
193 got_repo_open(struct got_repository **ret, const char *path)
195 struct got_repository *repo = NULL;
196 const struct got_error *err = NULL;
197 char *abspath;
199 if (got_path_is_absolute(path))
200 abspath = strdup(path);
201 else
202 abspath = got_path_get_absolute(path);
203 if (abspath == NULL)
204 return got_error(GOT_ERR_BAD_PATH);
206 repo = calloc(1, sizeof(*repo));
207 if (repo == NULL) {
208 err = got_error_from_errno();
209 goto done;
212 repo->path = got_path_normalize(abspath);
213 if (repo->path == NULL) {
214 err = got_error(GOT_ERR_BAD_PATH);
215 goto done;
218 repo->path_git_dir = strdup(repo->path);
219 if (repo->path_git_dir == NULL) {
220 err = got_error_from_errno();
221 goto done;
223 if (!is_git_repo(repo)) {
224 free(repo->path_git_dir);
225 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
226 GOT_GIT_DIR) == -1) {
227 err = got_error_from_errno();
228 goto done;
230 if (!is_git_repo(repo)) {
231 struct got_worktree *worktree;
232 if (got_worktree_open(&worktree, repo->path) == NULL) {
233 free(repo->path_git_dir);
234 repo->path_git_dir =
235 strdup(worktree->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,
243 "%s/%s", worktree->repo_path,
244 GOT_GIT_DIR) == -1) {
245 err = got_error_from_errno();
246 goto done;
249 got_worktree_close(worktree);
252 if (!is_git_repo(repo)) {
253 err = got_error(GOT_ERR_NOT_GIT_REPO);
254 goto done;
258 *ret = repo;
259 done:
260 if (err)
261 got_repo_close(repo);
262 free(abspath);
263 return err;
266 void
267 got_repo_close(struct got_repository *repo)
269 int i;
271 for (i = 0; i < nitems(repo->packidx_cache); i++) {
272 if (repo->packidx_cache[i] == NULL)
273 break;
274 got_packidx_close(repo->packidx_cache[i]);
277 for (i = 0; i < nitems(repo->packs); i++) {
278 if (repo->packs[i].path_packfile == NULL)
279 break;
280 got_pack_close(&repo->packs[i]);
283 while (!SIMPLEQ_EMPTY(&repo->objcache)) {
284 struct got_objcache_entry *ce;
285 ce = SIMPLEQ_FIRST(&repo->objcache);
286 SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
287 got_object_close(ce->obj);
288 free(ce);
291 free(repo->path);
292 free(repo->path_git_dir);
293 free(repo);