2 * Copyright (c) 2017 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>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
32 #include "got_sha1_priv.h"
33 #include "got_path_priv.h"
34 #include "got_delta_priv.h"
35 #include "got_zb_priv.h"
36 #include "got_object_priv.h"
38 /* A symbolic reference. */
44 /* A non-symbolic reference (there is no better designation). */
47 u_int8_t sha1[SHA1_DIGEST_LENGTH];
50 /* A reference which points to an arbitrary object. */
51 struct got_reference {
53 #define GOT_REF_IS_SYMBOLIC 0x01
57 struct got_symref symref;
61 static const struct got_error *
62 parse_symref(struct got_reference **ref, const char *name, const char *line)
64 struct got_symref *symref;
69 return got_error(GOT_ERR_NOT_REF);
71 symref_name = strdup(name);
72 if (symref_name == NULL)
73 return got_error(GOT_ERR_NO_MEM);
74 symref_ref = strdup(line);
75 if (symref_ref == NULL) {
77 return got_error(GOT_ERR_NO_MEM);
80 *ref = calloc(1, sizeof(**ref));
82 return got_error(GOT_ERR_NO_MEM);
83 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
84 symref = &((*ref)->ref.symref);
85 symref->name = symref_name;
86 symref->ref = symref_ref;
90 static const struct got_error *
91 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
93 uint8_t digest[SHA1_DIGEST_LENGTH];
96 if (strncmp(line, "ref: ", 5) == 0) {
98 return parse_symref(ref, name, line);
101 ref_name = strdup(name);
102 if (ref_name == NULL)
103 return got_error(GOT_ERR_NO_MEM);
105 if (!got_parse_sha1_digest(digest, line))
106 return got_error(GOT_ERR_NOT_REF);
108 *ref = calloc(1, sizeof(**ref));
110 return got_error(GOT_ERR_NO_MEM);
111 (*ref)->ref.ref.name = ref_name;
112 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
116 static const struct got_error *
117 parse_ref_file(struct got_reference **ref, const char *name,
120 const struct got_error *err = NULL;
121 FILE *f = fopen(abspath, "rb");
124 const char delim[3] = {'\0', '\0', '\0'};
127 return got_error(GOT_ERR_NOT_REF);
129 line = fparseln(f, &len, NULL, delim, 0);
131 err = got_error(GOT_ERR_NOT_REF);
135 err = parse_ref_line(ref, name, line);
143 get_refs_dir_path(struct got_repository *repo, const char *refname)
145 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
146 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
147 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
148 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
149 strncmp(refname, "refs/", 5) == 0)
150 return got_repo_get_path_git_dir(repo);
152 return got_repo_get_path_refs(repo);
155 const struct got_error *
156 got_ref_open(struct got_reference **ref, struct got_repository *repo,
159 const struct got_error *err = NULL;
160 char *path_ref = NULL;
161 char *normpath = NULL;
162 const char *parent_dir;
163 char *path_refs = get_refs_dir_path(repo, refname);
165 if (path_refs == NULL) {
166 err = got_error(GOT_ERR_NO_MEM);
170 /* XXX For now, this assumes that refs exist in the filesystem. */
172 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
173 err = got_error(GOT_ERR_NO_MEM);
177 normpath = got_path_normalize(path_ref);
178 if (normpath == NULL) {
179 err = got_error(GOT_ERR_NOT_REF);
183 err = parse_ref_file(ref, refname, normpath);
192 got_ref_close(struct got_reference *ref)
194 if (ref->flags & GOT_REF_IS_SYMBOLIC)
195 free(ref->ref.symref.name);
197 free(ref->ref.ref.name);
201 struct got_reference *
202 got_ref_dup(struct got_reference *ref)
204 struct got_reference *ret;
208 ret = calloc(1, sizeof(*ret));
212 ret->flags = ref->flags;
213 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
214 ret->ref.symref.name = strdup(ref->ref.symref.name);
215 if (ret->ref.symref.name == NULL) {
219 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
220 if (ret->ref.symref.ref == NULL) {
221 free(ret->ref.symref.name);
226 ref->ref.ref.name = strdup(ref->ref.ref.name);
227 if (ref->ref.ref.name == NULL) {
231 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
238 static const struct got_error *
239 resolve_symbolic_ref(struct got_reference **resolved,
240 struct got_repository *repo, struct got_reference *ref)
242 struct got_reference *nextref;
243 const struct got_error *err;
245 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
249 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
250 err = resolve_symbolic_ref(resolved, repo, nextref);
252 *resolved = got_ref_dup(nextref);
254 got_ref_close(nextref);
258 const struct got_error *
259 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
260 struct got_reference *ref)
262 const struct got_error *err;
264 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
265 struct got_reference *resolved = NULL;
266 err = resolve_symbolic_ref(&resolved, repo, ref);
268 err = got_ref_resolve(id, repo, resolved);
273 *id = calloc(1, sizeof(**id));
275 return got_error(GOT_ERR_NO_MEM);
276 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);