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"
32 #include "got_lib_path.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_zbuf.h"
35 #include "got_lib_object.h"
36 #include "got_lib_pack.h"
37 #include "got_lib_repository.h"
38 #include "got_lib_worktree.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_GIT_DIR ".git"
46 /* Mandatory files and directories inside the git directory. */
47 #define GOT_OBJECTS_DIR "objects"
48 #define GOT_REFS_DIR "refs"
49 #define GOT_HEAD_FILE "HEAD"
51 /* Other files and directories inside the git directory. */
52 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
53 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
54 #define GOT_OBJECTS_PACK_DIR "objects/pack"
56 char *
57 got_repo_get_path(struct got_repository *repo)
58 {
59 return strdup(repo->path);
60 }
62 char *
63 got_repo_get_path_git_dir(struct got_repository *repo)
64 {
65 return strdup(repo->path_git_dir);
66 }
68 static char *
69 get_path_git_child(struct got_repository *repo, const char *basename)
70 {
71 char *path_child;
73 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
74 basename) == -1)
75 return NULL;
77 return path_child;
78 }
80 char *
81 got_repo_get_path_objects(struct got_repository *repo)
82 {
83 return get_path_git_child(repo, GOT_OBJECTS_DIR);
84 }
86 char *
87 got_repo_get_path_objects_pack(struct got_repository *repo)
88 {
89 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
90 }
92 char *
93 got_repo_get_path_refs(struct got_repository *repo)
94 {
95 return get_path_git_child(repo, GOT_REFS_DIR);
96 }
98 static char *
99 get_path_head(struct got_repository *repo)
101 return get_path_git_child(repo, GOT_HEAD_FILE);
104 static int
105 is_git_repo(struct got_repository *repo)
107 char *path_git = got_repo_get_path_git_dir(repo);
108 char *path_objects = got_repo_get_path_objects(repo);
109 char *path_refs = got_repo_get_path_refs(repo);
110 char *path_head = get_path_head(repo);
111 int ret = 0;
112 struct stat sb;
113 struct got_reference *head_ref;
115 if (lstat(path_git, &sb) == -1)
116 goto done;
117 if (!S_ISDIR(sb.st_mode))
118 goto done;
120 if (lstat(path_objects, &sb) == -1)
121 goto done;
122 if (!S_ISDIR(sb.st_mode))
123 goto done;
125 if (lstat(path_refs, &sb) == -1)
126 goto done;
127 if (!S_ISDIR(sb.st_mode))
128 goto done;
130 if (lstat(path_head, &sb) == -1)
131 goto done;
132 if (!S_ISREG(sb.st_mode))
133 goto done;
135 /* Check if the HEAD reference can be opened. */
136 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
137 goto done;
138 got_ref_close(head_ref);
140 ret = 1;
141 done:
142 free(path_git);
143 free(path_objects);
144 free(path_refs);
145 free(path_head);
146 return ret;
150 const struct got_error *
151 got_repo_open(struct got_repository **ret, const char *path)
153 struct got_repository *repo = NULL;
154 const struct got_error *err = NULL;
155 char *abspath;
157 if (got_path_is_absolute(path))
158 abspath = strdup(path);
159 else
160 abspath = got_path_get_absolute(path);
161 if (abspath == NULL)
162 return got_error(GOT_ERR_BAD_PATH);
164 repo = calloc(1, sizeof(*repo));
165 if (repo == NULL) {
166 err = got_error_from_errno();
167 goto done;
170 repo->path = got_path_normalize(abspath);
171 if (repo->path == NULL) {
172 err = got_error(GOT_ERR_BAD_PATH);
173 goto done;
176 repo->path_git_dir = strdup(repo->path);
177 if (repo->path_git_dir == NULL) {
178 err = got_error_from_errno();
179 goto done;
181 if (!is_git_repo(repo)) {
182 free(repo->path_git_dir);
183 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
184 GOT_GIT_DIR) == -1) {
185 err = got_error_from_errno();
186 goto done;
188 if (!is_git_repo(repo)) {
189 struct got_worktree *worktree;
190 if (got_worktree_open(&worktree, repo->path) == NULL) {
191 free(repo->path_git_dir);
192 repo->path_git_dir =
193 strdup(worktree->repo_path);
194 if (repo->path_git_dir == NULL) {
195 err = got_error_from_errno();
196 goto done;
198 if (!is_git_repo(repo)) {
199 free(repo->path_git_dir);
200 if (asprintf(&repo->path_git_dir,
201 "%s/%s", worktree->repo_path,
202 GOT_GIT_DIR) == -1) {
203 err = got_error_from_errno();
204 goto done;
207 got_worktree_close(worktree);
210 if (!is_git_repo(repo)) {
211 err = got_error(GOT_ERR_NOT_GIT_REPO);
212 goto done;
216 *ret = repo;
217 done:
218 if (err)
219 got_repo_close(repo);
220 free(abspath);
221 return err;
224 void
225 got_repo_close(struct got_repository *repo)
227 int i;
229 for (i = 0; i < nitems(repo->packidx_cache); i++) {
230 if (repo->packidx_cache[i] == NULL)
231 break;
232 got_packidx_close(repo->packidx_cache[i]);
235 for (i = 0; i < nitems(repo->packs); i++) {
236 if (repo->packs[i].path_packfile == NULL)
237 break;
238 got_pack_close(&repo->packs[i]);
241 free(repo->path);
242 free(repo->path_git_dir);
243 free(repo);