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"
39 static const struct got_error *
40 parse_symref(struct got_reference **ref, const char *name, const char *line)
42 struct got_symref *symref;
47 return got_error(GOT_ERR_NOT_REF);
49 symref_name = strdup(name);
50 if (symref_name == NULL)
51 return got_error(GOT_ERR_NO_MEM);
52 symref_ref = strdup(line);
53 if (symref_ref == NULL) {
55 return got_error(GOT_ERR_NO_MEM);
58 *ref = calloc(1, sizeof(**ref));
60 return got_error(GOT_ERR_NO_MEM);
61 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
62 symref = &((*ref)->ref.symref);
63 symref->name = symref_name;
64 symref->ref = symref_ref;
68 static const struct got_error *
69 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
71 uint8_t digest[SHA1_DIGEST_LENGTH];
74 if (strncmp(line, "ref: ", 5) == 0) {
76 return parse_symref(ref, name, line);
79 ref_name = strdup(name);
81 return got_error(GOT_ERR_NO_MEM);
83 if (!got_parse_sha1_digest(digest, line))
84 return got_error(GOT_ERR_NOT_REF);
86 *ref = calloc(1, sizeof(**ref));
88 return got_error(GOT_ERR_NO_MEM);
89 (*ref)->ref.ref.name = ref_name;
90 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
94 static const struct got_error *
95 parse_ref_file(struct got_reference **ref, const char *name,
98 const struct got_error *err = NULL;
99 FILE *f = fopen(abspath, "rb");
102 const char delim[3] = {'\0', '\0', '\0'};
105 return got_error(GOT_ERR_NOT_REF);
107 line = fparseln(f, &len, NULL, delim, 0);
109 err = got_error(GOT_ERR_NOT_REF);
113 err = parse_ref_line(ref, name, line);
121 get_refs_dir_path(struct got_repository *repo, const char *refname)
123 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
124 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
125 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
126 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
127 strncmp(refname, "refs/", 5) == 0)
128 return got_repo_get_path_git_dir(repo);
130 return got_repo_get_path_refs(repo);
133 const struct got_error *
134 got_ref_open(struct got_reference **ref, struct got_repository *repo,
137 const struct got_error *err = NULL;
138 char *path_ref = NULL;
139 char *normpath = NULL;
140 const char *parent_dir;
141 char *path_refs = get_refs_dir_path(repo, refname);
143 if (path_refs == NULL) {
144 err = got_error(GOT_ERR_NO_MEM);
148 /* XXX For now, this assumes that refs exist in the filesystem. */
150 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
151 err = got_error(GOT_ERR_NO_MEM);
155 normpath = got_path_normalize(path_ref);
156 if (normpath == NULL) {
157 err = got_error(GOT_ERR_NOT_REF);
161 err = parse_ref_file(ref, refname, normpath);
170 got_ref_close(struct got_reference *ref)
172 if (ref->flags & GOT_REF_IS_SYMBOLIC)
173 free(ref->ref.symref.name);
175 free(ref->ref.ref.name);
179 struct got_reference *
180 got_ref_dup(struct got_reference *ref)
182 struct got_reference *ret;
186 ret = calloc(1, sizeof(*ret));
190 ret->flags = ref->flags;
191 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
192 ret->ref.symref.name = strdup(ref->ref.symref.name);
193 if (ret->ref.symref.name == NULL) {
197 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
198 if (ret->ref.symref.ref == NULL) {
199 free(ret->ref.symref.name);
204 ref->ref.ref.name = strdup(ref->ref.ref.name);
205 if (ref->ref.ref.name == NULL) {
209 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
216 static const struct got_error *
217 resolve_symbolic_ref(struct got_reference **resolved,
218 struct got_repository *repo, struct got_reference *ref)
220 struct got_reference *nextref;
221 const struct got_error *err;
223 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
227 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
228 err = resolve_symbolic_ref(resolved, repo, nextref);
230 *resolved = got_ref_dup(nextref);
232 got_ref_close(nextref);
236 const struct got_error *
237 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
238 struct got_reference *ref)
240 const struct got_error *err;
242 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
243 struct got_reference *resolved = NULL;
244 err = resolve_symbolic_ref(&resolved, repo, ref);
246 err = got_ref_resolve(id, repo, resolved);
251 *id = calloc(1, sizeof(**id));
253 return got_error(GOT_ERR_NO_MEM);
254 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);