2 * Copyright (c) 2018 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"
30 #include "got_reference.h"
32 #include "got_lib_sha1.h"
33 #include "got_lib_path.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_zbuf.h"
36 #include "got_lib_object.h"
38 #define GOT_REF_HEADS "heads"
39 #define GOT_REF_TAGS "tags"
40 #define GOT_REF_REMOTES "remotes"
42 /* A symbolic reference. */
48 /* A non-symbolic reference (there is no better designation). */
51 u_int8_t sha1[SHA1_DIGEST_LENGTH];
54 /* A reference which points to an arbitrary object. */
55 struct got_reference {
57 #define GOT_REF_IS_SYMBOLIC 0x01
61 struct got_symref symref;
65 static const struct got_error *
66 parse_symref(struct got_reference **ref, const char *name, const char *line)
68 struct got_symref *symref;
73 return got_error(GOT_ERR_NOT_REF);
75 symref_name = strdup(name);
76 if (symref_name == NULL)
77 return got_error_from_errno();
78 symref_ref = strdup(line);
79 if (symref_ref == NULL) {
80 const struct got_error *err = got_error_from_errno();
85 *ref = calloc(1, sizeof(**ref));
87 return got_error_from_errno();
88 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
89 symref = &((*ref)->ref.symref);
90 symref->name = symref_name;
91 symref->ref = symref_ref;
95 static const struct got_error *
96 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
98 uint8_t digest[SHA1_DIGEST_LENGTH];
101 if (strncmp(line, "ref: ", 5) == 0) {
103 return parse_symref(ref, name, line);
106 ref_name = strdup(name);
107 if (ref_name == NULL)
108 return got_error_from_errno();
110 if (!got_parse_sha1_digest(digest, line))
111 return got_error(GOT_ERR_NOT_REF);
113 *ref = calloc(1, sizeof(**ref));
115 return got_error_from_errno();
116 (*ref)->ref.ref.name = ref_name;
117 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
121 static const struct got_error *
122 parse_ref_file(struct got_reference **ref, const char *name,
125 const struct got_error *err = NULL;
126 FILE *f = fopen(abspath, "rb");
129 const char delim[3] = {'\0', '\0', '\0'};
132 return got_error(GOT_ERR_NOT_REF);
134 line = fparseln(f, &len, NULL, delim, 0);
136 err = got_error(GOT_ERR_NOT_REF);
140 err = parse_ref_line(ref, name, line);
148 get_refs_dir_path(struct got_repository *repo, const char *refname)
150 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
151 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
154 strncmp(refname, "refs/", 5) == 0)
155 return got_repo_get_path_git_dir(repo);
157 return got_repo_get_path_refs(repo);
160 static const struct got_error *
161 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
164 const struct got_error *err = NULL;
168 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
169 return got_error_from_errno();
171 normpath = got_path_normalize(path_ref);
172 if (normpath == NULL) {
173 err = got_error(GOT_ERR_NOT_REF);
177 err = parse_ref_file(ref, refname, normpath);
184 const struct got_error *
185 got_ref_open(struct got_reference **ref, struct got_repository *repo,
188 const struct got_error *err = NULL;
189 char *path_refs = get_refs_dir_path(repo, refname);
191 if (path_refs == NULL) {
192 err = got_error_from_errno();
196 /* XXX For now, this assumes that refs exist in the filesystem. */
198 err = open_ref(ref, path_refs, GOT_REF_HEADS, refname);
200 err = open_ref(ref, path_refs, GOT_REF_TAGS, refname);
202 err = open_ref(ref, path_refs, GOT_REF_REMOTES, refname);
204 err = open_ref(ref, path_refs, "", refname);
211 got_ref_close(struct got_reference *ref)
213 if (ref->flags & GOT_REF_IS_SYMBOLIC)
214 free(ref->ref.symref.name);
216 free(ref->ref.ref.name);
220 struct got_reference *
221 got_ref_dup(struct got_reference *ref)
223 struct got_reference *ret;
225 ret = calloc(1, sizeof(*ret));
229 ret->flags = ref->flags;
230 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
231 ret->ref.symref.name = strdup(ref->ref.symref.name);
232 if (ret->ref.symref.name == NULL) {
236 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
237 if (ret->ref.symref.ref == NULL) {
238 free(ret->ref.symref.name);
243 ref->ref.ref.name = strdup(ref->ref.ref.name);
244 if (ref->ref.ref.name == NULL) {
248 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
255 static const struct got_error *
256 resolve_symbolic_ref(struct got_reference **resolved,
257 struct got_repository *repo, struct got_reference *ref)
259 struct got_reference *nextref;
260 const struct got_error *err;
262 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
266 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
267 err = resolve_symbolic_ref(resolved, repo, nextref);
269 *resolved = got_ref_dup(nextref);
271 got_ref_close(nextref);
275 const struct got_error *
276 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
277 struct got_reference *ref)
279 const struct got_error *err;
281 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
282 struct got_reference *resolved = NULL;
283 err = resolve_symbolic_ref(&resolved, repo, ref);
285 err = got_ref_resolve(id, repo, resolved);
290 *id = calloc(1, sizeof(**id));
292 return got_error_from_errno();
293 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
298 got_ref_to_str(struct got_reference *ref)
301 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
302 if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
305 str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
308 str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
309 SHA1_DIGEST_STRING_LENGTH);