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"
36 static const struct got_error *
37 parse_symref(struct got_reference **ref, const char *name, const char *line)
39 struct got_symref *symref;
44 return got_error(GOT_ERR_NOT_REF);
46 symref_name = strdup(name);
47 if (symref_name == NULL)
48 return got_error(GOT_ERR_NO_MEM);
49 symref_ref = strdup(line);
50 if (symref_ref == NULL) {
52 return got_error(GOT_ERR_NO_MEM);
55 *ref = calloc(1, sizeof(**ref));
56 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
57 symref = &((*ref)->ref.symref);
58 symref->name = symref_name;
59 symref->ref = symref_ref;
63 static const struct got_error *
64 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
66 uint8_t digest[SHA1_DIGEST_LENGTH];
69 if (strncmp(line, "ref: ", 5) == 0) {
71 return parse_symref(ref, name, line);
74 ref_name = strdup(name);
76 return got_error(GOT_ERR_NO_MEM);
78 if (!got_parse_sha1_digest(digest, line))
79 return got_error(GOT_ERR_NOT_REF);
81 *ref = calloc(1, sizeof(**ref));
82 (*ref)->ref.ref.name = ref_name;
83 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
87 static const struct got_error *
88 parse_ref_file(struct got_reference **ref, const char *name,
91 const struct got_error *err = NULL;
92 FILE *f = fopen(abspath, "rb");
95 const char delim[3] = {'\0', '\0', '\0'};
98 return got_error(GOT_ERR_NOT_REF);
100 line = fparseln(f, &len, NULL, delim, 0);
102 err = got_error(GOT_ERR_NOT_REF);
106 err = parse_ref_line(ref, name, line);
114 get_refs_dir_path(struct got_repository *repo, const char *refname)
116 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
117 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
118 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
119 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
120 strncmp(refname, "refs/", 5) == 0)
121 return got_repo_get_path_git_dir(repo);
123 return got_repo_get_path_refs(repo);
126 const struct got_error *
127 got_ref_open(struct got_reference **ref, struct got_repository *repo,
130 const struct got_error *err = NULL;
131 char *path_ref = NULL;
132 char *normpath = NULL;
133 const char *parent_dir;
134 char *path_refs = get_refs_dir_path(repo, refname);
136 if (path_refs == NULL) {
137 err = got_error(GOT_ERR_NO_MEM);
141 /* XXX For now, this assumes that refs exist in the filesystem. */
143 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
144 err = got_error(GOT_ERR_NO_MEM);
148 normpath = got_path_normalize(path_ref);
149 if (normpath == NULL) {
150 err = got_error(GOT_ERR_NOT_REF);
154 err = parse_ref_file(ref, refname, normpath);
163 got_ref_close(struct got_reference *ref)
165 if (ref->flags & GOT_REF_IS_SYMBOLIC)
166 free(ref->ref.symref.name);
168 free(ref->ref.ref.name);
172 struct got_reference *
173 got_ref_dup(struct got_reference *ref)
175 struct got_reference *ret = calloc(1, sizeof(*ret));
182 ret->flags = ref->flags;
183 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
184 ret->ref.symref.name = strdup(ref->ref.symref.name);
185 if (ret->ref.symref.name == NULL) {
189 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
190 if (ret->ref.symref.ref == NULL) {
191 free(ret->ref.symref.name);
196 ref->ref.ref.name = strdup(ref->ref.ref.name);
197 if (ref->ref.ref.name == NULL) {
201 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
208 static const struct got_error *
209 resolve_symbolic_ref(struct got_reference **resolved,
210 struct got_repository *repo, struct got_reference *ref)
212 struct got_reference *nextref;
213 const struct got_error *err;
215 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
219 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
220 err = resolve_symbolic_ref(resolved, repo, nextref);
222 *resolved = got_ref_dup(nextref);
224 got_ref_close(nextref);
228 const struct got_error *
229 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
230 struct got_reference *ref)
232 const struct got_error *err;
234 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
235 struct got_reference *resolved = NULL;
236 err = resolve_symbolic_ref(&resolved, repo, ref);
238 err = got_ref_resolve(id, repo, resolved);
243 *id = calloc(1, sizeof(**id));
245 return got_error(GOT_ERR_NO_MEM);
246 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);