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>
26 #include "got_error.h"
27 #include "got_object.h"
28 #include "got_repository.h"
34 static const struct got_error *
35 parse_symref(struct got_reference **ref, const char *name, const char *line)
37 struct got_symref *symref;
42 return got_error(GOT_ERR_NOT_REF);
44 symref_name = strdup(name);
45 if (symref_name == NULL)
46 return got_error(GOT_ERR_NO_MEM);
47 symref_ref = strdup(line);
48 if (symref_ref == NULL) {
50 return got_error(GOT_ERR_NO_MEM);
53 *ref = calloc(1, sizeof(**ref));
54 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
55 symref = &((*ref)->ref.symref);
56 symref->name = symref_name;
57 symref->ref = symref_ref;
62 parse_xdigit(uint8_t *val, const char *hex)
68 lval = strtol(hex, &ep, 16);
69 if (hex[0] == '\0' || *ep != '\0')
71 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
79 parse_sha1_digest(uint8_t *digest, const char *line)
82 char hex[3] = {'\0', '\0', '\0'};
85 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
86 if (line[0] == '\0' || line[1] == '\0')
88 for (j = 0; j < 2; j++) {
92 if (!parse_xdigit(&b, hex))
100 static const struct got_error *
101 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 uint8_t digest[SHA1_DIGEST_LENGTH];
106 if (strncmp(line, "ref: ", 5) == 0) {
108 return parse_symref(ref, name, line);
111 ref_name = strdup(name);
112 if (ref_name == NULL)
113 return got_error(GOT_ERR_NO_MEM);
115 if (!parse_sha1_digest(digest, line))
116 return got_error(GOT_ERR_NOT_REF);
118 *ref = calloc(1, sizeof(**ref));
119 (*ref)->ref.ref.name = ref_name;
120 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
124 static const struct got_error *
125 parse_ref_file(struct got_reference **ref, const char *name,
128 const struct got_error *err = NULL;
129 FILE *f = fopen(abspath, "rb");
132 const char delim[3] = {'\0', '\0', '\0'};
135 return got_error(GOT_ERR_NOT_REF);
137 line = fparseln(f, &len, NULL, delim, 0);
139 err = got_error(GOT_ERR_NOT_REF);
143 err = parse_ref_line(ref, name, line);
151 get_refs_dir_path(struct got_repository *repo, const char *refname)
153 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
155 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
157 strncmp(refname, "refs/", 5) == 0)
158 return got_repo_get_path_git_dir(repo);
160 return got_repo_get_path_refs(repo);
163 const struct got_error *
164 got_ref_open(struct got_reference **ref, struct got_repository *repo,
167 const struct got_error *err = NULL;
168 char *path_ref = NULL;
169 char *normpath = NULL;
170 const char *parent_dir;
171 char *path_refs = get_refs_dir_path(repo, refname);
173 if (path_refs == NULL) {
174 err = got_error(GOT_ERR_NO_MEM);
178 /* XXX For now, this assumes that refs exist in the filesystem. */
180 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
181 err = got_error(GOT_ERR_NO_MEM);
185 normpath = got_path_normalize(path_ref);
186 if (normpath == NULL) {
187 err = got_error(GOT_ERR_NOT_REF);
191 err = parse_ref_file(ref, refname, normpath);
200 got_ref_close(struct got_reference *ref)
202 if (ref->flags & GOT_REF_IS_SYMBOLIC)
203 free(ref->ref.symref.name);
205 free(ref->ref.ref.name);
209 struct got_reference *
210 got_ref_dup(struct got_reference *ref)
212 struct got_reference *ret = calloc(1, sizeof(*ret));
219 ret->flags = ref->flags;
220 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
221 ret->ref.symref.name = strdup(ref->ref.symref.name);
222 if (ret->ref.symref.name == NULL) {
226 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
227 if (ret->ref.symref.ref == NULL) {
228 free(ret->ref.symref.name);
233 ref->ref.ref.name = strdup(ref->ref.ref.name);
234 if (ref->ref.ref.name == NULL) {
238 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
245 static const struct got_error *
246 resolve_symbolic_ref(struct got_reference **resolved,
247 struct got_repository *repo, struct got_reference *ref)
249 struct got_reference *nextref;
250 const struct got_error *err;
252 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
256 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
257 err = resolve_symbolic_ref(resolved, repo, nextref);
259 *resolved = got_ref_dup(nextref);
261 got_ref_close(nextref);
265 const struct got_error *
266 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
267 struct got_reference *ref)
269 const struct got_error *err;
271 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
272 struct got_reference *resolved = NULL;
273 err = resolve_symbolic_ref(&resolved, repo, ref);
275 err = got_ref_resolve(id, repo, resolved);
280 *id = calloc(1, sizeof(**id));
282 return got_error(GOT_ERR_NO_MEM);
283 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);