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 NULL;
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 if (fclose(f) != 0 && err == NULL)
150 err = got_error_from_errno();
151 return err;
154 static int
155 is_well_known_ref(const char *refname)
157 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
159 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
160 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
163 static char *
164 get_refs_dir_path(struct got_repository *repo, const char *refname)
166 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
167 return strdup(got_repo_get_path_git_dir(repo));
169 return got_repo_get_path_refs(repo);
172 static const struct got_error *
173 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
174 const char *line)
176 uint8_t digest[SHA1_DIGEST_LENGTH];
177 char *name;
179 *ref = NULL;
181 if (line[0] == '#' || line[0] == '^')
182 return NULL;
184 if (!got_parse_sha1_digest(digest, line))
185 return got_error(GOT_ERR_BAD_REF_DATA);
187 if (abs_refname) {
188 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
189 return NULL;
191 name = strdup(abs_refname);
192 if (name == NULL)
193 return got_error_from_errno();
194 } else
195 name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
197 *ref = calloc(1, sizeof(**ref));
198 if (*ref == NULL)
199 return got_error_from_errno();
200 (*ref)->ref.ref.name = name;;
201 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
202 return NULL;
205 static const struct got_error *
206 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
207 int nsubdirs, const char *refname)
209 const struct got_error *err = NULL;
210 char *abs_refname;
211 char *line;
212 size_t len;
213 const char delim[3] = {'\0', '\0', '\0'};
214 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
216 *ref = NULL;
218 if (ref_is_absolute)
219 abs_refname = (char *)refname;
220 do {
221 line = fparseln(f, &len, NULL, delim, 0);
222 if (line == NULL)
223 break;
224 for (i = 0; i < nsubdirs; i++) {
225 if (!ref_is_absolute &&
226 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
227 refname) == -1)
228 return got_error_from_errno();
229 err = parse_packed_ref_line(ref, abs_refname, line);
230 if (!ref_is_absolute)
231 free(abs_refname);
232 if (err || *ref != NULL)
233 break;
235 free(line);
236 if (err)
237 break;
238 } while (*ref == NULL);
240 return err;
243 static const struct got_error *
244 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
245 const char *name)
247 const struct got_error *err = NULL;
248 char *path = NULL;
249 char *normpath = NULL;
250 char *absname = NULL;
251 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
252 int ref_is_well_known = is_well_known_ref(name);
254 *ref = NULL;
256 if (ref_is_absolute || ref_is_well_known) {
257 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
258 return got_error_from_errno();
259 absname = (char *)name;
260 } else {
261 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
262 return got_error_from_errno();
264 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
265 err = got_error_from_errno();
266 goto done;
270 normpath = got_path_normalize(path);
271 if (normpath == NULL) {
272 err = got_error_from_errno();
273 goto done;
276 err = parse_ref_file(ref, absname, normpath);
277 done:
278 if (!ref_is_absolute && !ref_is_well_known)
279 free(absname);
280 free(path);
281 free(normpath);
282 return err;
285 const struct got_error *
286 got_ref_open(struct got_reference **ref, struct got_repository *repo,
287 const char *refname)
289 const struct got_error *err = NULL;
290 char *path_refs = NULL;
291 const char *subdirs[] = {
292 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
293 };
294 int i, well_known = is_well_known_ref(refname);
296 *ref = NULL;
298 path_refs = get_refs_dir_path(repo, refname);
299 if (path_refs == NULL) {
300 err = got_error_from_errno();
301 goto done;
304 if (!well_known) {
305 char *packed_refs_path;
306 FILE *f;
308 /* Search on-disk refs before packed refs! */
309 for (i = 0; i < nitems(subdirs); i++) {
310 err = open_ref(ref, path_refs, subdirs[i], refname);
311 if (err || *ref)
312 goto done;
315 packed_refs_path = got_repo_get_path_packed_refs(repo);
316 if (packed_refs_path == NULL) {
317 err = got_error_from_errno();
318 goto done;
321 f = fopen(packed_refs_path, "rb");
322 free(packed_refs_path);
323 if (f != NULL) {
324 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
325 refname);
326 if (fclose(f) != 0 && err == NULL)
327 err = got_error_from_errno();
328 if (err || *ref)
329 goto done;
333 err = open_ref(ref, path_refs, "", refname);
334 if (err)
335 goto done;
336 done:
337 if (*ref == NULL)
338 err = got_error_not_ref(refname);
339 free(path_refs);
340 return err;
343 void
344 got_ref_close(struct got_reference *ref)
346 if (ref->flags & GOT_REF_IS_SYMBOLIC)
347 free(ref->ref.symref.name);
348 else
349 free(ref->ref.ref.name);
350 free(ref);
353 struct got_reference *
354 got_ref_dup(struct got_reference *ref)
356 struct got_reference *ret;
358 ret = calloc(1, sizeof(*ret));
359 if (ret == NULL)
360 return NULL;
362 ret->flags = ref->flags;
363 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
364 ret->ref.symref.name = strdup(ref->ref.symref.name);
365 if (ret->ref.symref.name == NULL) {
366 free(ret);
367 return NULL;
369 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
370 if (ret->ref.symref.ref == NULL) {
371 free(ret->ref.symref.name);
372 free(ret);
373 return NULL;
375 } else {
376 ref->ref.ref.name = strdup(ref->ref.ref.name);
377 if (ref->ref.ref.name == NULL) {
378 free(ret);
379 return NULL;
381 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
382 SHA1_DIGEST_LENGTH);
385 return ret;
388 static const struct got_error *
389 resolve_symbolic_ref(struct got_reference **resolved,
390 struct got_repository *repo, struct got_reference *ref)
392 struct got_reference *nextref;
393 const struct got_error *err;
395 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
396 if (err)
397 return err;
399 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
400 err = resolve_symbolic_ref(resolved, repo, nextref);
401 else
402 *resolved = got_ref_dup(nextref);
404 got_ref_close(nextref);
405 return err;
408 const struct got_error *
409 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
410 struct got_reference *ref)
412 const struct got_error *err;
414 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
415 struct got_reference *resolved = NULL;
416 err = resolve_symbolic_ref(&resolved, repo, ref);
417 if (err == NULL)
418 err = got_ref_resolve(id, repo, resolved);
419 free(resolved);
420 return err;
423 *id = calloc(1, sizeof(**id));
424 if (*id == NULL)
425 return got_error_from_errno();
426 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
427 return NULL;
430 char *
431 got_ref_to_str(struct got_reference *ref)
433 char *str;
435 if (ref->flags & GOT_REF_IS_SYMBOLIC)
436 return strdup(ref->ref.symref.ref);
438 str = malloc(SHA1_DIGEST_STRING_LENGTH);
439 if (str == NULL)
440 return NULL;
442 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
443 SHA1_DIGEST_STRING_LENGTH) == NULL) {
444 free(str);
445 return NULL;
448 return str;
451 const char *
452 got_ref_get_name(struct got_reference *ref)
454 if (ref->flags & GOT_REF_IS_SYMBOLIC)
455 return ref->ref.symref.name;
457 return ref->ref.ref.name;
460 static const struct got_error *
461 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
462 struct got_repository *repo)
464 const struct got_error *err;
465 struct got_object_id *id;
466 struct got_reflist_entry *new, *re, *prev;
467 int cmp;
469 err = got_ref_resolve(&id, repo, ref);
470 if (err)
471 return err;
473 new = malloc(sizeof(*re));
474 if (new == NULL) {
475 free(id);
476 return got_error_from_errno();
478 new->ref = ref;
479 new->id = id;
481 /*
482 * We must de-duplicate entries on insert because packed-refs may
483 * contain redundant entries. On-disk refs take precedence.
484 * This code assumes that on-disk revs are read before packed-refs.
485 * We're iterating the list anyway, so insert elements sorted by name.
486 */
487 re = SIMPLEQ_FIRST(refs);
488 while (re) {
489 cmp = got_path_cmp(got_ref_get_name(re->ref),
490 got_ref_get_name(ref));
491 if (cmp == 0) {
492 free(ref); /* duplicate */
493 return NULL;
494 } else if (cmp > 0) {
495 if (prev)
496 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
497 else
498 SIMPLEQ_INSERT_HEAD(refs, new, entry);
499 return NULL;
500 } else {
501 prev = re;
502 re = SIMPLEQ_NEXT(re, entry);
506 SIMPLEQ_INSERT_TAIL(refs, new, entry);
507 return NULL;
510 static const struct got_error *
511 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
512 const char *subdir, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 DIR *d = NULL;
516 char *path_subdir;
518 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
519 return got_error_from_errno();
521 d = opendir(path_subdir);
522 if (d == NULL)
523 goto done;
525 while (1) {
526 struct dirent *dent;
527 struct got_reference *ref;
528 char *child;
530 dent = readdir(d);
531 if (dent == NULL)
532 break;
534 if (strcmp(dent->d_name, ".") == 0 ||
535 strcmp(dent->d_name, "..") == 0)
536 continue;
538 switch (dent->d_type) {
539 case DT_REG:
540 err = open_ref(&ref, path_refs, subdir, dent->d_name);
541 if (err)
542 goto done;
543 if (ref) {
544 err = insert_ref(refs, ref, repo);
545 if (err)
546 goto done;
548 break;
549 case DT_DIR:
550 if (asprintf(&child, "%s%s%s", subdir,
551 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
552 err = got_error_from_errno();
553 break;
555 err = gather_on_disk_refs(refs, path_refs, child, repo);
556 free(child);
557 break;
558 default:
559 break;
562 done:
563 if (d)
564 closedir(d);
565 free(path_subdir);
566 return err;
569 const struct got_error *
570 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
572 const struct got_error *err;
573 char *packed_refs_path, *path_refs = NULL;
574 FILE *f = NULL;
575 struct got_reference *ref;
577 /* HEAD ref should always exist. */
578 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
579 if (path_refs == NULL) {
580 err = got_error_from_errno();
581 goto done;
583 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
584 if (err)
585 goto done;
586 err = insert_ref(refs, ref, repo);
587 if (err)
588 goto done;
590 /* Gather on-disk refs before parsing packed-refs. */
591 free(path_refs);
592 path_refs = get_refs_dir_path(repo, "");
593 if (path_refs == NULL) {
594 err = got_error_from_errno();
595 goto done;
597 err = gather_on_disk_refs(refs, path_refs, "", repo);
598 if (err)
599 goto done;
601 /*
602 * The packed-refs file may contain redundant entries, in which
603 * case on-disk refs take precedence.
604 */
605 packed_refs_path = got_repo_get_path_packed_refs(repo);
606 if (packed_refs_path == NULL) {
607 err = got_error_from_errno();
608 goto done;
611 f = fopen(packed_refs_path, "r");
612 free(packed_refs_path);
613 if (f) {
614 char *line;
615 size_t len;
616 const char delim[3] = {'\0', '\0', '\0'};
617 while (1) {
618 line = fparseln(f, &len, NULL, delim, 0);
619 if (line == NULL)
620 break;
621 err = parse_packed_ref_line(&ref, NULL, line);
622 if (err)
623 goto done;
624 if (ref) {
625 err = insert_ref(refs, ref, repo);
626 if (err)
627 goto done;
631 done:
632 free(path_refs);
633 if (f && fclose(f) != 0 && err == NULL)
634 err = got_error_from_errno();
635 return err;