2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.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"
42 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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"
58 got_repo_get_path(struct got_repository *repo)
60 return strdup(repo->path);
64 got_repo_get_path_git_dir(struct got_repository *repo)
66 return strdup(repo->path_git_dir);
70 get_path_git_child(struct got_repository *repo, const char *basename)
74 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
82 got_repo_get_path_objects(struct got_repository *repo)
84 return get_path_git_child(repo, GOT_OBJECTS_DIR);
88 got_repo_get_path_objects_pack(struct got_repository *repo)
90 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
94 got_repo_get_path_refs(struct got_repository *repo)
96 return get_path_git_child(repo, GOT_REFS_DIR);
100 get_path_head(struct got_repository *repo)
102 return get_path_git_child(repo, GOT_HEAD_FILE);
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);
114 struct got_reference *head_ref;
116 if (lstat(path_git, &sb) == -1)
118 if (!S_ISDIR(sb.st_mode))
121 if (lstat(path_objects, &sb) == -1)
123 if (!S_ISDIR(sb.st_mode))
126 if (lstat(path_refs, &sb) == -1)
128 if (!S_ISDIR(sb.st_mode))
131 if (lstat(path_head, &sb) == -1)
133 if (!S_ISREG(sb.st_mode))
136 /* Check if the HEAD reference can be opened. */
137 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
139 got_ref_close(head_ref);
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);
165 ce = calloc(1, sizeof(*ce));
167 return got_error_from_errno();
168 memcpy(&ce->id, id, sizeof(ce->id));
171 SIMPLEQ_INSERT_HEAD(&repo->objcache, ce, entry);
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)
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;
199 if (got_path_is_absolute(path))
200 abspath = strdup(path);
202 abspath = got_path_get_absolute(path);
204 return got_error(GOT_ERR_BAD_PATH);
206 repo = calloc(1, sizeof(*repo));
208 err = got_error_from_errno();
212 repo->path = got_path_normalize(abspath);
213 if (repo->path == NULL) {
214 err = got_error(GOT_ERR_BAD_PATH);
218 repo->path_git_dir = strdup(repo->path);
219 if (repo->path_git_dir == NULL) {
220 err = got_error_from_errno();
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();
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);
235 strdup(worktree->repo_path);
236 if (repo->path_git_dir == NULL) {
237 err = got_error_from_errno();
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();
249 got_worktree_close(worktree);
252 if (!is_git_repo(repo)) {
253 err = got_error(GOT_ERR_NOT_GIT_REPO);
261 got_repo_close(repo);
267 got_repo_close(struct got_repository *repo)
271 for (i = 0; i < nitems(repo->packidx_cache); i++) {
272 if (repo->packidx_cache[i] == NULL)
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)
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);
292 free(repo->path_git_dir);