Blob


1 /*
2 * Copyright (c) 2018, 2019 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/types.h>
18 #include <sys/queue.h>
20 #include <dirent.h>
21 #include <sha1.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <util.h>
26 #include <zlib.h>
27 #include <time.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_reference.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_REF_HEADS "heads"
45 #define GOT_REF_TAGS "tags"
46 #define GOT_REF_REMOTES "remotes"
48 /* A symbolic reference. */
49 struct got_symref {
50 char *name;
51 char *ref;
52 };
54 /* A non-symbolic reference (there is no better designation). */
55 struct got_ref {
56 char *name;
57 u_int8_t sha1[SHA1_DIGEST_LENGTH];
58 };
60 /* A reference which points to an arbitrary object. */
61 struct got_reference {
62 unsigned int flags;
63 #define GOT_REF_IS_SYMBOLIC 0x01
65 union {
66 struct got_ref ref;
67 struct got_symref symref;
68 } ref;
69 };
71 static const struct got_error *
72 parse_symref(struct got_reference **ref, const char *name, const char *line)
73 {
74 struct got_symref *symref;
75 char *symref_name;
76 char *symref_ref;
78 if (line[0] == '\0')
79 return got_error(GOT_ERR_BAD_REF_DATA);
81 symref_name = strdup(name);
82 if (symref_name == NULL)
83 return got_error_from_errno();
84 symref_ref = strdup(line);
85 if (symref_ref == NULL) {
86 const struct got_error *err = got_error_from_errno();
87 free(symref_name);
88 return err;
89 }
91 *ref = calloc(1, sizeof(**ref));
92 if (*ref == NULL)
93 return got_error_from_errno();
94 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
95 symref = &((*ref)->ref.symref);
96 symref->name = symref_name;
97 symref->ref = symref_ref;
98 return NULL;
99 }
101 static const struct got_error *
102 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
104 uint8_t digest[SHA1_DIGEST_LENGTH];
105 char *ref_name;
107 if (strncmp(line, "ref: ", 5) == 0) {
108 line += 5;
109 return parse_symref(ref, name, line);
112 ref_name = strdup(name);
113 if (ref_name == NULL)
114 return got_error_from_errno();
116 if (!got_parse_sha1_digest(digest, line))
117 return got_error(GOT_ERR_BAD_REF_DATA);
119 *ref = calloc(1, sizeof(**ref));
120 if (*ref == NULL)
121 return got_error_from_errno();
122 (*ref)->ref.ref.name = ref_name;
123 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
124 return NULL;
127 static const struct got_error *
128 parse_ref_file(struct got_reference **ref, const char *name,
129 const char *abspath)
131 const struct got_error *err = NULL;
132 FILE *f = fopen(abspath, "rb");
133 char *line;
134 size_t len;
135 const char delim[3] = {'\0', '\0', '\0'};
137 if (f == NULL)
138 return got_error_not_ref(name);
140 line = fparseln(f, &len, NULL, delim, 0);
141 if (line == NULL) {
142 err = got_error(GOT_ERR_BAD_REF_DATA);
143 goto done;
146 err = parse_ref_line(ref, name, line);
147 done:
148 free(line);
149 fclose(f);
150 return err;
153 static int
154 is_well_known_ref(const char *refname)
156 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
157 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
159 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
162 static char *
163 get_refs_dir_path(struct got_repository *repo, const char *refname)
165 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
166 return strdup(got_repo_get_path_git_dir(repo));
168 return got_repo_get_path_refs(repo);
171 static const struct got_error *
172 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
173 const char *line)
175 uint8_t digest[SHA1_DIGEST_LENGTH];
176 char *name;
178 *ref = NULL;
180 if (line[0] == '#' || line[0] == '^')
181 return NULL;
183 if (!got_parse_sha1_digest(digest, line))
184 return got_error(GOT_ERR_BAD_REF_DATA);
186 if (abs_refname) {
187 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
188 return NULL;
190 name = strdup(abs_refname);
191 if (name == NULL)
192 return got_error_from_errno();
193 } else
194 name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
196 *ref = calloc(1, sizeof(**ref));
197 if (*ref == NULL)
198 return got_error_from_errno();
199 (*ref)->ref.ref.name = name;;
200 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
201 return NULL;
204 static const struct got_error *
205 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
206 int nsubdirs, const char *refname)
208 const struct got_error *err = NULL;
209 char *abs_refname;
210 char *line;
211 size_t len;
212 const char delim[3] = {'\0', '\0', '\0'};
213 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
215 if (ref_is_absolute)
216 abs_refname = (char *)refname;
217 do {
218 line = fparseln(f, &len, NULL, delim, 0);
219 if (line == NULL) {
220 err = got_error_not_ref(refname);
221 break;
223 for (i = 0; i < nsubdirs; i++) {
224 if (!ref_is_absolute &&
225 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
226 refname) == -1)
227 return got_error_from_errno();
228 err = parse_packed_ref_line(ref, abs_refname, line);
229 if (!ref_is_absolute)
230 free(abs_refname);
231 if (err || *ref != NULL)
232 break;
234 free(line);
235 if (err)
236 break;
237 } while (*ref == NULL);
239 return err;
242 static const struct got_error *
243 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
244 const char *name)
246 const struct got_error *err = NULL;
247 char *path = NULL;
248 char *normpath = NULL;
249 char *absname = NULL;
250 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
251 int ref_is_well_known = is_well_known_ref(name);
253 if (ref_is_absolute || ref_is_well_known) {
254 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
255 return got_error_from_errno();
256 absname = (char *)name;
257 } else {
258 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
259 return got_error_from_errno();
261 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
262 err = got_error_from_errno();
263 goto done;
267 normpath = got_path_normalize(path);
268 if (normpath == NULL) {
269 err = got_error_from_errno();
270 goto done;
273 err = parse_ref_file(ref, absname, normpath);
274 done:
275 if (!ref_is_absolute && !ref_is_well_known)
276 free(absname);
277 free(path);
278 free(normpath);
279 return err;
282 const struct got_error *
283 got_ref_open(struct got_reference **ref, struct got_repository *repo,
284 const char *refname)
286 const struct got_error *err = NULL;
287 char *path_refs = NULL;
288 const char *subdirs[] = {
289 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
290 };
291 int i, well_known = is_well_known_ref(refname);
293 if (!well_known) {
294 char *packed_refs_path;
295 FILE *f;
297 packed_refs_path = got_repo_get_path_packed_refs(repo);
298 if (packed_refs_path == NULL)
299 return got_error_from_errno();
301 f = fopen(packed_refs_path, "rb");
302 free(packed_refs_path);
303 if (f != NULL) {
304 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
305 refname);
306 fclose(f);
307 if (err == NULL)
308 goto done;
312 path_refs = get_refs_dir_path(repo, refname);
313 if (path_refs == NULL) {
314 err = got_error_from_errno();
315 goto done;
318 if (!well_known) {
319 for (i = 0; i < nitems(subdirs); i++) {
320 err = open_ref(ref, path_refs, subdirs[i], refname);
321 if (err == NULL)
322 goto done;
326 err = open_ref(ref, path_refs, "", refname);
327 done:
328 free(path_refs);
329 return err;
332 void
333 got_ref_close(struct got_reference *ref)
335 if (ref->flags & GOT_REF_IS_SYMBOLIC)
336 free(ref->ref.symref.name);
337 else
338 free(ref->ref.ref.name);
339 free(ref);
342 struct got_reference *
343 got_ref_dup(struct got_reference *ref)
345 struct got_reference *ret;
347 ret = calloc(1, sizeof(*ret));
348 if (ret == NULL)
349 return NULL;
351 ret->flags = ref->flags;
352 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
353 ret->ref.symref.name = strdup(ref->ref.symref.name);
354 if (ret->ref.symref.name == NULL) {
355 free(ret);
356 return NULL;
358 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
359 if (ret->ref.symref.ref == NULL) {
360 free(ret->ref.symref.name);
361 free(ret);
362 return NULL;
364 } else {
365 ref->ref.ref.name = strdup(ref->ref.ref.name);
366 if (ref->ref.ref.name == NULL) {
367 free(ret);
368 return NULL;
370 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
371 SHA1_DIGEST_LENGTH);
374 return ret;
377 static const struct got_error *
378 resolve_symbolic_ref(struct got_reference **resolved,
379 struct got_repository *repo, struct got_reference *ref)
381 struct got_reference *nextref;
382 const struct got_error *err;
384 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
385 if (err)
386 return err;
388 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
389 err = resolve_symbolic_ref(resolved, repo, nextref);
390 else
391 *resolved = got_ref_dup(nextref);
393 got_ref_close(nextref);
394 return err;
397 const struct got_error *
398 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
399 struct got_reference *ref)
401 const struct got_error *err;
403 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
404 struct got_reference *resolved = NULL;
405 err = resolve_symbolic_ref(&resolved, repo, ref);
406 if (err == NULL)
407 err = got_ref_resolve(id, repo, resolved);
408 free(resolved);
409 return err;
412 *id = calloc(1, sizeof(**id));
413 if (*id == NULL)
414 return got_error_from_errno();
415 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
416 return NULL;
419 char *
420 got_ref_to_str(struct got_reference *ref)
422 char *str;
424 if (ref->flags & GOT_REF_IS_SYMBOLIC)
425 return strdup(ref->ref.symref.ref);
427 str = malloc(SHA1_DIGEST_STRING_LENGTH);
428 if (str == NULL)
429 return NULL;
431 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
432 SHA1_DIGEST_STRING_LENGTH) == NULL) {
433 free(str);
434 return NULL;
437 return str;
440 const char *
441 got_ref_get_name(struct got_reference *ref)
443 if (ref->flags & GOT_REF_IS_SYMBOLIC)
444 return ref->ref.symref.name;
446 return ref->ref.ref.name;
449 static const struct got_error *
450 append_ref(struct got_reflist_head *refs, struct got_reference *ref,
451 struct got_repository *repo)
453 const struct got_error *err;
454 struct got_object_id *id;
455 struct got_reflist_entry *entry;
457 err = got_ref_resolve(&id, repo, ref);
458 if (err)
459 return err;
460 entry = malloc(sizeof(*entry));
461 if (entry == NULL)
462 return got_error_from_errno();
463 entry->ref = ref;
464 entry->id = id;
465 SIMPLEQ_INSERT_TAIL(refs, entry, entry);
466 return NULL;
469 static const struct got_error *
470 gather_refs(struct got_reflist_head *refs, const char *path_refs,
471 const char *subdir, struct got_repository *repo)
473 const struct got_error *err = NULL;
474 DIR *d = NULL;
475 char *path_subdir;
477 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
478 return got_error_from_errno();
480 d = opendir(path_subdir);
481 if (d == NULL)
482 goto done;
484 while (1) {
485 struct dirent *dent;
486 struct got_reference *ref;
487 char *child;
489 dent = readdir(d);
490 if (dent == NULL)
491 break;
493 if (strcmp(dent->d_name, ".") == 0 ||
494 strcmp(dent->d_name, "..") == 0)
495 continue;
497 switch (dent->d_type) {
498 case DT_REG:
499 err = open_ref(&ref, path_refs, subdir, dent->d_name);
500 if (err && err->code != GOT_ERR_NOT_REF)
501 goto done;
502 if (ref) {
503 err = append_ref(refs, ref, repo);
504 if (err)
505 goto done;
507 break;
508 case DT_DIR:
509 if (asprintf(&child, "%s%s%s", subdir,
510 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
511 err = got_error_from_errno();
512 break;
514 err = gather_refs(refs, path_refs, child, repo);
515 free(child);
516 break;
517 default:
518 break;
521 done:
522 if (d)
523 closedir(d);
524 free(path_subdir);
525 return err;
528 const struct got_error *
529 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
531 const struct got_error *err;
532 char *packed_refs_path, *path_refs = NULL;
533 FILE *f;
534 struct got_reference *ref;
536 packed_refs_path = got_repo_get_path_packed_refs(repo);
537 if (packed_refs_path == NULL)
538 return got_error_from_errno();
540 f = fopen(packed_refs_path, "r");
541 free(packed_refs_path);
542 if (f) {
543 char *line;
544 size_t len;
545 const char delim[3] = {'\0', '\0', '\0'};
546 while (1) {
547 line = fparseln(f, &len, NULL, delim, 0);
548 if (line == NULL)
549 break;
550 err = parse_packed_ref_line(&ref, NULL, line);
551 if (err)
552 goto done;
553 if (ref) {
554 err = append_ref(refs, ref, repo);
555 if (err)
556 goto done;
561 /* HEAD ref should always exist. */
562 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
563 if (path_refs == NULL) {
564 err = got_error_from_errno();
565 goto done;
567 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
568 if (err)
569 goto done;
570 err = append_ref(refs, ref, repo);
571 if (err)
572 goto done;
574 free(path_refs);
575 path_refs = get_refs_dir_path(repo, "");
576 if (path_refs == NULL) {
577 err = got_error_from_errno();
578 goto done;
580 err = gather_refs(refs, path_refs, "", repo);
581 done:
582 free(path_refs);
583 if (f)
584 fclose(f);
585 return err;