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);
185 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
188 name = strdup(abs_refname);
190 return got_error_from_errno();
192 *ref = calloc(1, sizeof(**ref));
194 return got_error_from_errno();
195 (*ref)->ref.ref.name = name;;
196 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
200 static const struct got_error *
201 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
202 int nsubdirs, const char *refname)
204 const struct got_error *err = NULL;
208 const char delim[3] = {'\0', '\0', '\0'};
209 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
212 abs_refname = (char *)refname;
214 line = fparseln(f, &len, NULL, delim, 0);
216 err = got_error(GOT_ERR_NOT_REF);
219 for (i = 0; i < nsubdirs; i++) {
220 if (!ref_is_absolute &&
221 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
223 return got_error_from_errno();
224 err = parse_packed_ref_line(ref, abs_refname, line);
225 if (!ref_is_absolute)
227 if (err || *ref != NULL)
233 } while (*ref == NULL);
238 static const struct got_error *
239 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
242 const struct got_error *err = NULL;
246 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
247 return got_error_from_errno();
249 normpath = got_path_normalize(path_ref);
250 if (normpath == NULL) {
251 err = got_error(GOT_ERR_NOT_REF);
255 err = parse_ref_file(ref, refname, normpath);
262 const struct got_error *
263 got_ref_open(struct got_reference **ref, struct got_repository *repo,
266 const struct got_error *err = NULL;
267 char *path_refs = NULL;
268 const char *subdirs[] = {
269 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
271 int i, well_known = is_well_known_ref(refname);
274 char *packed_refs_path;
277 packed_refs_path = got_repo_get_path_packed_refs(repo);
278 if (packed_refs_path == NULL)
279 return got_error_from_errno();
281 f = fopen(packed_refs_path, "rb");
282 free(packed_refs_path);
284 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
292 path_refs = get_refs_dir_path(repo, refname);
293 if (path_refs == NULL) {
294 err = got_error_from_errno();
299 for (i = 0; i < nitems(subdirs); i++) {
300 err = open_ref(ref, path_refs, subdirs[i], refname);
306 err = open_ref(ref, path_refs, "", refname);
313 got_ref_close(struct got_reference *ref)
315 if (ref->flags & GOT_REF_IS_SYMBOLIC)
316 free(ref->ref.symref.name);
318 free(ref->ref.ref.name);
322 struct got_reference *
323 got_ref_dup(struct got_reference *ref)
325 struct got_reference *ret;
327 ret = calloc(1, sizeof(*ret));
331 ret->flags = ref->flags;
332 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
333 ret->ref.symref.name = strdup(ref->ref.symref.name);
334 if (ret->ref.symref.name == NULL) {
338 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
339 if (ret->ref.symref.ref == NULL) {
340 free(ret->ref.symref.name);
345 ref->ref.ref.name = strdup(ref->ref.ref.name);
346 if (ref->ref.ref.name == NULL) {
350 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
357 static const struct got_error *
358 resolve_symbolic_ref(struct got_reference **resolved,
359 struct got_repository *repo, struct got_reference *ref)
361 struct got_reference *nextref;
362 const struct got_error *err;
364 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
368 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
369 err = resolve_symbolic_ref(resolved, repo, nextref);
371 *resolved = got_ref_dup(nextref);
373 got_ref_close(nextref);
377 const struct got_error *
378 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
379 struct got_reference *ref)
381 const struct got_error *err;
383 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
384 struct got_reference *resolved = NULL;
385 err = resolve_symbolic_ref(&resolved, repo, ref);
387 err = got_ref_resolve(id, repo, resolved);
392 *id = calloc(1, sizeof(**id));
394 return got_error_from_errno();
395 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
400 got_ref_to_str(struct got_reference *ref)
404 if (ref->flags & GOT_REF_IS_SYMBOLIC)
405 return strdup(ref->ref.symref.ref);
407 str = malloc(SHA1_DIGEST_STRING_LENGTH);
411 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
412 SHA1_DIGEST_STRING_LENGTH) == NULL) {
421 got_ref_get_name(struct got_reference *ref)
423 if (ref->flags & GOT_REF_IS_SYMBOLIC)
424 return ref->ref.symref.name;
426 return ref->ref.ref.name;