Blame


1 7b19e0f1 2017-11-05 stsp /*
2 3b339b2f 2018-02-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 79b11c62 2018-03-09 stsp #include <sys/queue.h>
18 deeca238 2018-03-12 stsp #include <sys/stat.h>
19 79b11c62 2018-03-09 stsp
20 4027f31a 2017-11-04 stsp #include <limits.h>
21 4027f31a 2017-11-04 stsp #include <stdlib.h>
22 4027f31a 2017-11-04 stsp #include <stdio.h>
23 4027f31a 2017-11-04 stsp #include <sha1.h>
24 4027f31a 2017-11-04 stsp #include <string.h>
25 79b11c62 2018-03-09 stsp #include <zlib.h>
26 4027f31a 2017-11-04 stsp
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 5261c201 2018-04-01 stsp #include "got_reference.h"
29 4027f31a 2017-11-04 stsp #include "got_repository.h"
30 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
31 7bb0daa1 2018-06-21 stsp #include "got_object.h"
32 4027f31a 2017-11-04 stsp
33 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
39 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
40 c3f94f68 2017-11-05 stsp
41 79b11c62 2018-03-09 stsp #ifndef nitems
42 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 79b11c62 2018-03-09 stsp #endif
44 3b339b2f 2018-02-12 stsp
45 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
46 4027f31a 2017-11-04 stsp
47 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
48 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
49 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
50 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
51 4027f31a 2017-11-04 stsp
52 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
53 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
54 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
55 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
56 4df642d9 2017-11-05 stsp
57 11995603 2017-11-05 stsp char *
58 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
59 86c3caaf 2018-03-09 stsp {
60 86c3caaf 2018-03-09 stsp return strdup(repo->path);
61 86c3caaf 2018-03-09 stsp }
62 86c3caaf 2018-03-09 stsp
63 86c3caaf 2018-03-09 stsp char *
64 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
65 4027f31a 2017-11-04 stsp {
66 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
67 4027f31a 2017-11-04 stsp }
68 4027f31a 2017-11-04 stsp
69 4027f31a 2017-11-04 stsp static char *
70 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
71 4027f31a 2017-11-04 stsp {
72 4027f31a 2017-11-04 stsp char *path_child;
73 4027f31a 2017-11-04 stsp
74 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
75 4027f31a 2017-11-04 stsp basename) == -1)
76 4027f31a 2017-11-04 stsp return NULL;
77 4027f31a 2017-11-04 stsp
78 4027f31a 2017-11-04 stsp return path_child;
79 4027f31a 2017-11-04 stsp }
80 4027f31a 2017-11-04 stsp
81 11995603 2017-11-05 stsp char *
82 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
83 4027f31a 2017-11-04 stsp {
84 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
85 4027f31a 2017-11-04 stsp }
86 4027f31a 2017-11-04 stsp
87 11995603 2017-11-05 stsp char *
88 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
89 a1fd68d8 2018-01-12 stsp {
90 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
91 a1fd68d8 2018-01-12 stsp }
92 a1fd68d8 2018-01-12 stsp
93 a1fd68d8 2018-01-12 stsp char *
94 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
95 4027f31a 2017-11-04 stsp {
96 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
97 4027f31a 2017-11-04 stsp }
98 4027f31a 2017-11-04 stsp
99 4027f31a 2017-11-04 stsp static char *
100 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
101 4027f31a 2017-11-04 stsp {
102 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
103 4027f31a 2017-11-04 stsp }
104 4027f31a 2017-11-04 stsp
105 4027f31a 2017-11-04 stsp static int
106 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
107 4027f31a 2017-11-04 stsp {
108 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
109 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
110 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
111 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
112 deeca238 2018-03-12 stsp int ret = 0;
113 deeca238 2018-03-12 stsp struct stat sb;
114 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
115 4027f31a 2017-11-04 stsp
116 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
117 deeca238 2018-03-12 stsp goto done;
118 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
119 deeca238 2018-03-12 stsp goto done;
120 4027f31a 2017-11-04 stsp
121 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
122 deeca238 2018-03-12 stsp goto done;
123 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
124 deeca238 2018-03-12 stsp goto done;
125 deeca238 2018-03-12 stsp
126 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
127 deeca238 2018-03-12 stsp goto done;
128 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
129 deeca238 2018-03-12 stsp goto done;
130 deeca238 2018-03-12 stsp
131 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
132 deeca238 2018-03-12 stsp goto done;
133 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
134 deeca238 2018-03-12 stsp goto done;
135 4847cca1 2018-03-12 stsp
136 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
137 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
138 4847cca1 2018-03-12 stsp goto done;
139 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
140 4847cca1 2018-03-12 stsp
141 deeca238 2018-03-12 stsp ret = 1;
142 deeca238 2018-03-12 stsp done:
143 4027f31a 2017-11-04 stsp free(path_git);
144 4027f31a 2017-11-04 stsp free(path_objects);
145 4027f31a 2017-11-04 stsp free(path_refs);
146 4027f31a 2017-11-04 stsp free(path_head);
147 4027f31a 2017-11-04 stsp return ret;
148 4027f31a 2017-11-04 stsp
149 4027f31a 2017-11-04 stsp }
150 4027f31a 2017-11-04 stsp
151 7bb0daa1 2018-06-21 stsp const struct got_error*
152 7bb0daa1 2018-06-21 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
153 7bb0daa1 2018-06-21 stsp struct got_object *obj)
154 7bb0daa1 2018-06-21 stsp {
155 7bb0daa1 2018-06-21 stsp struct got_objcache_entry *ce;
156 7bb0daa1 2018-06-21 stsp
157 7bb0daa1 2018-06-21 stsp if (repo->ncached >= GOT_OBJECT_CACHE_SIZE) {
158 7bb0daa1 2018-06-21 stsp ce = SIMPLEQ_FIRST(&repo->objcache);
159 7bb0daa1 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
160 7bb0daa1 2018-06-21 stsp got_object_close(ce->obj);
161 7bb0daa1 2018-06-21 stsp free(ce);
162 7bb0daa1 2018-06-21 stsp repo->ncached--;
163 7bb0daa1 2018-06-21 stsp }
164 7bb0daa1 2018-06-21 stsp
165 7bb0daa1 2018-06-21 stsp ce = calloc(1, sizeof(*ce));
166 7bb0daa1 2018-06-21 stsp if (ce == NULL)
167 7bb0daa1 2018-06-21 stsp return got_error_from_errno();
168 7bb0daa1 2018-06-21 stsp memcpy(&ce->id, id, sizeof(ce->id));
169 7bb0daa1 2018-06-21 stsp ce->obj = obj;
170 7bb0daa1 2018-06-21 stsp obj->refcnt++;
171 7bb0daa1 2018-06-21 stsp SIMPLEQ_INSERT_HEAD(&repo->objcache, ce, entry);
172 7bb0daa1 2018-06-21 stsp repo->ncached++;
173 7bb0daa1 2018-06-21 stsp return NULL;
174 7bb0daa1 2018-06-21 stsp }
175 7bb0daa1 2018-06-21 stsp
176 7bb0daa1 2018-06-21 stsp struct got_object *
177 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
178 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
179 7bb0daa1 2018-06-21 stsp {
180 7bb0daa1 2018-06-21 stsp struct got_objcache_entry *ce;
181 7bb0daa1 2018-06-21 stsp
182 7bb0daa1 2018-06-21 stsp SIMPLEQ_FOREACH(ce, &repo->objcache, entry) {
183 7bb0daa1 2018-06-21 stsp if (got_object_id_cmp(&ce->id, id) != 0)
184 7bb0daa1 2018-06-21 stsp continue;
185 7bb0daa1 2018-06-21 stsp repo->cache_hit++;
186 7bb0daa1 2018-06-21 stsp return ce->obj;
187 7bb0daa1 2018-06-21 stsp }
188 7bb0daa1 2018-06-21 stsp repo->cache_miss++;
189 7bb0daa1 2018-06-21 stsp return NULL;
190 7bb0daa1 2018-06-21 stsp }
191 7bb0daa1 2018-06-21 stsp
192 4027f31a 2017-11-04 stsp const struct got_error *
193 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
194 4027f31a 2017-11-04 stsp {
195 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
196 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
197 2393f13b 2018-03-09 stsp char *abspath;
198 4027f31a 2017-11-04 stsp
199 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
200 2393f13b 2018-03-09 stsp abspath = strdup(path);
201 2393f13b 2018-03-09 stsp else
202 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
203 92af5469 2017-11-05 stsp if (abspath == NULL)
204 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
205 4027f31a 2017-11-04 stsp
206 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
207 92af5469 2017-11-05 stsp if (repo == NULL) {
208 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
209 92af5469 2017-11-05 stsp goto done;
210 92af5469 2017-11-05 stsp }
211 4027f31a 2017-11-04 stsp
212 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
213 92af5469 2017-11-05 stsp if (repo->path == NULL) {
214 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
215 92af5469 2017-11-05 stsp goto done;
216 92af5469 2017-11-05 stsp }
217 4027f31a 2017-11-04 stsp
218 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
219 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
220 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
221 92af5469 2017-11-05 stsp goto done;
222 92af5469 2017-11-05 stsp }
223 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
224 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
225 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
226 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
227 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
228 4986b9d5 2018-03-12 stsp goto done;
229 4986b9d5 2018-03-12 stsp }
230 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
231 442a3ddc 2018-04-23 stsp struct got_worktree *worktree;
232 442a3ddc 2018-04-23 stsp if (got_worktree_open(&worktree, repo->path) == NULL) {
233 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
234 442a3ddc 2018-04-23 stsp repo->path_git_dir =
235 442a3ddc 2018-04-23 stsp strdup(worktree->repo_path);
236 442a3ddc 2018-04-23 stsp if (repo->path_git_dir == NULL) {
237 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
238 442a3ddc 2018-04-23 stsp goto done;
239 442a3ddc 2018-04-23 stsp }
240 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
241 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
242 442a3ddc 2018-04-23 stsp if (asprintf(&repo->path_git_dir,
243 442a3ddc 2018-04-23 stsp "%s/%s", worktree->repo_path,
244 442a3ddc 2018-04-23 stsp GOT_GIT_DIR) == -1) {
245 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
246 442a3ddc 2018-04-23 stsp goto done;
247 442a3ddc 2018-04-23 stsp }
248 442a3ddc 2018-04-23 stsp }
249 442a3ddc 2018-04-23 stsp got_worktree_close(worktree);
250 442a3ddc 2018-04-23 stsp }
251 442a3ddc 2018-04-23 stsp }
252 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
253 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
254 4986b9d5 2018-03-12 stsp goto done;
255 4986b9d5 2018-03-12 stsp }
256 4986b9d5 2018-03-12 stsp }
257 4027f31a 2017-11-04 stsp
258 4027f31a 2017-11-04 stsp *ret = repo;
259 92af5469 2017-11-05 stsp done:
260 92af5469 2017-11-05 stsp if (err)
261 4986b9d5 2018-03-12 stsp got_repo_close(repo);
262 92af5469 2017-11-05 stsp free(abspath);
263 92af5469 2017-11-05 stsp return err;
264 4027f31a 2017-11-04 stsp }
265 4027f31a 2017-11-04 stsp
266 4027f31a 2017-11-04 stsp void
267 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
268 4027f31a 2017-11-04 stsp {
269 79b11c62 2018-03-09 stsp int i;
270 79b11c62 2018-03-09 stsp
271 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
272 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
273 79b11c62 2018-03-09 stsp break;
274 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
275 79b11c62 2018-03-09 stsp }
276 bd1223b9 2018-03-14 stsp
277 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
278 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
279 7e656b93 2018-03-17 stsp break;
280 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
281 7e656b93 2018-03-17 stsp }
282 7e656b93 2018-03-17 stsp
283 7bb0daa1 2018-06-21 stsp while (!SIMPLEQ_EMPTY(&repo->objcache)) {
284 7bb0daa1 2018-06-21 stsp struct got_objcache_entry *ce;
285 7bb0daa1 2018-06-21 stsp ce = SIMPLEQ_FIRST(&repo->objcache);
286 7bb0daa1 2018-06-21 stsp SIMPLEQ_REMOVE_HEAD(&repo->objcache, entry);
287 7bb0daa1 2018-06-21 stsp got_object_close(ce->obj);
288 7bb0daa1 2018-06-21 stsp free(ce);
289 7bb0daa1 2018-06-21 stsp }
290 7bb0daa1 2018-06-21 stsp
291 4027f31a 2017-11-04 stsp free(repo->path);
292 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
293 4027f31a 2017-11-04 stsp free(repo);
294 4027f31a 2017-11-04 stsp }