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>
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_zbuf.h"
37 #include "got_lib_object.h"
39 #define GOT_REF_HEADS "heads"
40 #define GOT_REF_TAGS "tags"
41 #define GOT_REF_REMOTES "remotes"
43 /* A symbolic reference. */
49 /* A non-symbolic reference (there is no better designation). */
52 u_int8_t sha1[SHA1_DIGEST_LENGTH];
55 /* A reference which points to an arbitrary object. */
56 struct got_reference {
58 #define GOT_REF_IS_SYMBOLIC 0x01
62 struct got_symref symref;
66 static const struct got_error *
67 parse_symref(struct got_reference **ref, const char *name, const char *line)
69 struct got_symref *symref;
74 return got_error(GOT_ERR_NOT_REF);
76 symref_name = strdup(name);
77 if (symref_name == NULL)
78 return got_error_from_errno();
79 symref_ref = strdup(line);
80 if (symref_ref == NULL) {
81 const struct got_error *err = got_error_from_errno();
86 *ref = calloc(1, sizeof(**ref));
88 return got_error_from_errno();
89 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
90 symref = &((*ref)->ref.symref);
91 symref->name = symref_name;
92 symref->ref = symref_ref;
96 static const struct got_error *
97 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
99 uint8_t digest[SHA1_DIGEST_LENGTH];
102 if (strncmp(line, "ref: ", 5) == 0) {
104 return parse_symref(ref, name, line);
107 ref_name = strdup(name);
108 if (ref_name == NULL)
109 return got_error_from_errno();
111 if (!got_parse_sha1_digest(digest, line))
112 return got_error(GOT_ERR_NOT_REF);
114 *ref = calloc(1, sizeof(**ref));
116 return got_error_from_errno();
117 (*ref)->ref.ref.name = ref_name;
118 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
122 static const struct got_error *
123 parse_ref_file(struct got_reference **ref, const char *name,
126 const struct got_error *err = NULL;
127 FILE *f = fopen(abspath, "rb");
130 const char delim[3] = {'\0', '\0', '\0'};
133 return got_error(GOT_ERR_NOT_REF);
135 line = fparseln(f, &len, NULL, delim, 0);
137 err = got_error(GOT_ERR_NOT_REF);
141 err = parse_ref_line(ref, name, line);
149 get_refs_dir_path(struct got_repository *repo, const char *refname)
151 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
155 strncmp(refname, "refs/", 5) == 0)
156 return got_repo_get_path_git_dir(repo);
158 return got_repo_get_path_refs(repo);
161 static const struct got_error *
162 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
165 const struct got_error *err = NULL;
169 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
170 return got_error_from_errno();
172 normpath = got_path_normalize(path_ref);
173 if (normpath == NULL) {
174 err = got_error(GOT_ERR_NOT_REF);
178 err = parse_ref_file(ref, refname, normpath);
185 const struct got_error *
186 got_ref_open(struct got_reference **ref, struct got_repository *repo,
189 const struct got_error *err = NULL;
190 char *path_refs = get_refs_dir_path(repo, refname);
192 if (path_refs == NULL) {
193 err = got_error_from_errno();
197 /* XXX For now, this assumes that refs exist in the filesystem. */
199 err = open_ref(ref, path_refs, GOT_REF_HEADS, refname);
201 err = open_ref(ref, path_refs, GOT_REF_TAGS, refname);
203 err = open_ref(ref, path_refs, GOT_REF_REMOTES, refname);
205 err = open_ref(ref, path_refs, "", refname);
212 got_ref_close(struct got_reference *ref)
214 if (ref->flags & GOT_REF_IS_SYMBOLIC)
215 free(ref->ref.symref.name);
217 free(ref->ref.ref.name);
221 struct got_reference *
222 got_ref_dup(struct got_reference *ref)
224 struct got_reference *ret;
226 ret = calloc(1, sizeof(*ret));
230 ret->flags = ref->flags;
231 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
232 ret->ref.symref.name = strdup(ref->ref.symref.name);
233 if (ret->ref.symref.name == NULL) {
237 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
238 if (ret->ref.symref.ref == NULL) {
239 free(ret->ref.symref.name);
244 ref->ref.ref.name = strdup(ref->ref.ref.name);
245 if (ref->ref.ref.name == NULL) {
249 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
256 static const struct got_error *
257 resolve_symbolic_ref(struct got_reference **resolved,
258 struct got_repository *repo, struct got_reference *ref)
260 struct got_reference *nextref;
261 const struct got_error *err;
263 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
267 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
268 err = resolve_symbolic_ref(resolved, repo, nextref);
270 *resolved = got_ref_dup(nextref);
272 got_ref_close(nextref);
276 const struct got_error *
277 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
278 struct got_reference *ref)
280 const struct got_error *err;
282 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
283 struct got_reference *resolved = NULL;
284 err = resolve_symbolic_ref(&resolved, repo, ref);
286 err = got_ref_resolve(id, repo, resolved);
291 *id = calloc(1, sizeof(**id));
293 return got_error_from_errno();
294 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
299 got_ref_to_str(struct got_reference *ref)
302 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
303 if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
306 str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
309 str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
310 SHA1_DIGEST_STRING_LENGTH);