2 * Copyright (c) 2018, 2019 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/types.h>
18 #include <sys/queue.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_reference.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #define GOT_REF_HEADS "heads"
44 #define GOT_REF_TAGS "tags"
45 #define GOT_REF_REMOTES "remotes"
47 /* A symbolic reference. */
53 /* A non-symbolic reference (there is no better designation). */
56 u_int8_t sha1[SHA1_DIGEST_LENGTH];
59 /* A reference which points to an arbitrary object. */
60 struct got_reference {
62 #define GOT_REF_IS_SYMBOLIC 0x01
66 struct got_symref symref;
70 static const struct got_error *
71 parse_symref(struct got_reference **ref, const char *name, const char *line)
73 struct got_symref *symref;
78 return got_error(GOT_ERR_NOT_REF);
80 symref_name = strdup(name);
81 if (symref_name == NULL)
82 return got_error_from_errno();
83 symref_ref = strdup(line);
84 if (symref_ref == NULL) {
85 const struct got_error *err = got_error_from_errno();
90 *ref = calloc(1, sizeof(**ref));
92 return got_error_from_errno();
93 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 symref = &((*ref)->ref.symref);
95 symref->name = symref_name;
96 symref->ref = symref_ref;
100 static const struct got_error *
101 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 uint8_t digest[SHA1_DIGEST_LENGTH];
106 if (strncmp(line, "ref: ", 5) == 0) {
108 return parse_symref(ref, name, line);
111 ref_name = strdup(name);
112 if (ref_name == NULL)
113 return got_error_from_errno();
115 if (!got_parse_sha1_digest(digest, line))
116 return got_error(GOT_ERR_NOT_REF);
118 *ref = calloc(1, sizeof(**ref));
120 return got_error_from_errno();
121 (*ref)->ref.ref.name = ref_name;
122 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
126 static const struct got_error *
127 parse_ref_file(struct got_reference **ref, const char *name,
130 const struct got_error *err = NULL;
131 FILE *f = fopen(abspath, "rb");
134 const char delim[3] = {'\0', '\0', '\0'};
137 return got_error(GOT_ERR_NOT_REF);
139 line = fparseln(f, &len, NULL, delim, 0);
141 err = got_error(GOT_ERR_NOT_REF);
145 err = parse_ref_line(ref, name, line);
153 is_well_known_ref(const char *refname)
155 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
162 get_refs_dir_path(struct got_repository *repo, const char *refname)
164 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
165 return strdup(got_repo_get_path_git_dir(repo));
167 return got_repo_get_path_refs(repo);
170 static const struct got_error *
171 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
174 uint8_t digest[SHA1_DIGEST_LENGTH];
179 if (line[0] == '#' || line[0] == '^')
182 if (!got_parse_sha1_digest(digest, line))
183 return got_error(GOT_ERR_NOT_REF);
186 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
189 name = strdup(abs_refname);
191 return got_error_from_errno();
193 name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
195 *ref = calloc(1, sizeof(**ref));
197 return got_error_from_errno();
198 (*ref)->ref.ref.name = name;;
199 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
203 static const struct got_error *
204 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
205 int nsubdirs, const char *refname)
207 const struct got_error *err = NULL;
211 const char delim[3] = {'\0', '\0', '\0'};
212 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
215 abs_refname = (char *)refname;
217 line = fparseln(f, &len, NULL, delim, 0);
219 err = got_error(GOT_ERR_NOT_REF);
222 for (i = 0; i < nsubdirs; i++) {
223 if (!ref_is_absolute &&
224 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
226 return got_error_from_errno();
227 err = parse_packed_ref_line(ref, abs_refname, line);
228 if (!ref_is_absolute)
230 if (err || *ref != NULL)
236 } while (*ref == NULL);
241 static const struct got_error *
242 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
245 const struct got_error *err = NULL;
249 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
250 return got_error_from_errno();
252 normpath = got_path_normalize(path_ref);
253 if (normpath == NULL) {
254 err = got_error(GOT_ERR_NOT_REF);
258 err = parse_ref_file(ref, refname, normpath);
265 const struct got_error *
266 got_ref_open(struct got_reference **ref, struct got_repository *repo,
269 const struct got_error *err = NULL;
270 char *path_refs = NULL;
271 const char *subdirs[] = {
272 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
274 int i, well_known = is_well_known_ref(refname);
277 char *packed_refs_path;
280 packed_refs_path = got_repo_get_path_packed_refs(repo);
281 if (packed_refs_path == NULL)
282 return got_error_from_errno();
284 f = fopen(packed_refs_path, "rb");
285 free(packed_refs_path);
287 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
295 path_refs = get_refs_dir_path(repo, refname);
296 if (path_refs == NULL) {
297 err = got_error_from_errno();
302 for (i = 0; i < nitems(subdirs); i++) {
303 err = open_ref(ref, path_refs, subdirs[i], refname);
309 err = open_ref(ref, path_refs, "", refname);
316 got_ref_close(struct got_reference *ref)
318 if (ref->flags & GOT_REF_IS_SYMBOLIC)
319 free(ref->ref.symref.name);
321 free(ref->ref.ref.name);
325 struct got_reference *
326 got_ref_dup(struct got_reference *ref)
328 struct got_reference *ret;
330 ret = calloc(1, sizeof(*ret));
334 ret->flags = ref->flags;
335 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
336 ret->ref.symref.name = strdup(ref->ref.symref.name);
337 if (ret->ref.symref.name == NULL) {
341 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
342 if (ret->ref.symref.ref == NULL) {
343 free(ret->ref.symref.name);
348 ref->ref.ref.name = strdup(ref->ref.ref.name);
349 if (ref->ref.ref.name == NULL) {
353 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
360 static const struct got_error *
361 resolve_symbolic_ref(struct got_reference **resolved,
362 struct got_repository *repo, struct got_reference *ref)
364 struct got_reference *nextref;
365 const struct got_error *err;
367 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
371 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
372 err = resolve_symbolic_ref(resolved, repo, nextref);
374 *resolved = got_ref_dup(nextref);
376 got_ref_close(nextref);
380 const struct got_error *
381 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
382 struct got_reference *ref)
384 const struct got_error *err;
386 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
387 struct got_reference *resolved = NULL;
388 err = resolve_symbolic_ref(&resolved, repo, ref);
390 err = got_ref_resolve(id, repo, resolved);
395 *id = calloc(1, sizeof(**id));
397 return got_error_from_errno();
398 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
403 got_ref_to_str(struct got_reference *ref)
407 if (ref->flags & GOT_REF_IS_SYMBOLIC)
408 return strdup(ref->ref.symref.ref);
410 str = malloc(SHA1_DIGEST_STRING_LENGTH);
414 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
415 SHA1_DIGEST_STRING_LENGTH) == NULL) {
424 got_ref_get_name(struct got_reference *ref)
426 if (ref->flags & GOT_REF_IS_SYMBOLIC)
427 return ref->ref.symref.name;
429 return ref->ref.ref.name;
432 static const struct got_error *
433 append_ref(struct got_reflist_head *refs, struct got_reference *ref,
434 struct got_repository *repo)
436 const struct got_error *err;
437 struct got_object_id *id;
438 struct got_reflist_entry *entry;
440 err = got_ref_resolve(&id, repo, ref);
443 entry = malloc(sizeof(*entry));
445 return got_error_from_errno();
448 SIMPLEQ_INSERT_TAIL(refs, entry, entry);
452 const struct got_error *
453 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
455 const struct got_error *err;
456 char *packed_refs_path, *path_refs;
458 struct got_reference *ref;
460 packed_refs_path = got_repo_get_path_packed_refs(repo);
461 if (packed_refs_path == NULL)
462 return got_error_from_errno();
464 f = fopen(packed_refs_path, "r");
465 free(packed_refs_path);
469 const char delim[3] = {'\0', '\0', '\0'};
471 line = fparseln(f, &len, NULL, delim, 0);
474 err = parse_packed_ref_line(&ref, NULL, line);
478 append_ref(refs, ref, repo);
482 /* HEAD ref should always exist. */
483 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
484 if (path_refs == NULL) {
485 err = got_error_from_errno();
488 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
492 append_ref(refs, ref, repo);