2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4027f31a 2017-11-04 stsp #include <sys/types.h>
18 4027f31a 2017-11-04 stsp #include <sha1.h>
19 4027f31a 2017-11-04 stsp #include <stdio.h>
20 4027f31a 2017-11-04 stsp #include <stdlib.h>
21 4027f31a 2017-11-04 stsp #include <string.h>
22 4027f31a 2017-11-04 stsp #include <util.h>
23 11995603 2017-11-05 stsp #include <limits.h>
24 11995603 2017-11-05 stsp #include <errno.h>
26 4027f31a 2017-11-04 stsp #include "got_error.h"
27 11995603 2017-11-05 stsp #include "got_object.h"
28 11995603 2017-11-05 stsp #include "got_repository.h"
29 4027f31a 2017-11-04 stsp #include "got_refs.h"
31 c3f94f68 2017-11-05 stsp #include "path.h"
34 4027f31a 2017-11-04 stsp static const struct got_error *
35 4027f31a 2017-11-04 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
37 4027f31a 2017-11-04 stsp struct got_symref *symref;
38 4027f31a 2017-11-04 stsp char *symref_name;
39 4027f31a 2017-11-04 stsp char *symref_ref;
41 4027f31a 2017-11-04 stsp if (line[0] == '\0')
42 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
44 4027f31a 2017-11-04 stsp symref_name = strdup(name);
45 4027f31a 2017-11-04 stsp if (symref_name == NULL)
46 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
47 4027f31a 2017-11-04 stsp symref_ref = strdup(line);
48 4027f31a 2017-11-04 stsp if (symref_ref == NULL) {
49 4027f31a 2017-11-04 stsp free(symref_name);
50 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
53 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
54 4027f31a 2017-11-04 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
55 4027f31a 2017-11-04 stsp symref = &((*ref)->ref.symref);
56 4027f31a 2017-11-04 stsp symref->name = symref_name;
57 4027f31a 2017-11-04 stsp symref->ref = symref_ref;
58 4027f31a 2017-11-04 stsp return NULL;
62 11995603 2017-11-05 stsp parse_xdigit(uint8_t *val, const char *hex)
68 11995603 2017-11-05 stsp lval = strtol(hex, &ep, 16);
69 11995603 2017-11-05 stsp if (hex[0] == '\0' || *ep != '\0')
71 11995603 2017-11-05 stsp if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
74 11995603 2017-11-05 stsp *val = (uint8_t)lval;
79 4027f31a 2017-11-04 stsp parse_sha1_digest(uint8_t *digest, const char *line)
81 11995603 2017-11-05 stsp uint8_t b = 0;
82 11995603 2017-11-05 stsp char hex[3] = {'\0', '\0', '\0'};
85 4027f31a 2017-11-04 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
86 11995603 2017-11-05 stsp if (line[0] == '\0' || line[1] == '\0')
88 11995603 2017-11-05 stsp for (j = 0; j < 2; j++) {
89 11995603 2017-11-05 stsp hex[j] = *line;
92 11995603 2017-11-05 stsp if (!parse_xdigit(&b, hex))
94 11995603 2017-11-05 stsp digest[i] = b;
100 4027f31a 2017-11-04 stsp static const struct got_error *
101 4027f31a 2017-11-04 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
104 4027f31a 2017-11-04 stsp char *ref_name;
106 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
108 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
111 4027f31a 2017-11-04 stsp ref_name = strdup(name);
112 4027f31a 2017-11-04 stsp if (ref_name == NULL)
113 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
115 4027f31a 2017-11-04 stsp if (!parse_sha1_digest(digest, line))
116 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
118 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
119 4027f31a 2017-11-04 stsp (*ref)->ref.ref.name = ref_name;
120 4027f31a 2017-11-04 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
121 4027f31a 2017-11-04 stsp return NULL;
124 4027f31a 2017-11-04 stsp static const struct got_error *
125 4027f31a 2017-11-04 stsp parse_ref_file(struct got_reference **ref, const char *name,
126 4027f31a 2017-11-04 stsp const char *abspath)
128 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
129 4027f31a 2017-11-04 stsp FILE *f = fopen(abspath, "rb");
130 4027f31a 2017-11-04 stsp char *line;
131 4027f31a 2017-11-04 stsp size_t len;
132 4027f31a 2017-11-04 stsp const char delim[3] = {'\0', '\0', '\0'};
134 4027f31a 2017-11-04 stsp if (f == NULL)
135 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
137 4027f31a 2017-11-04 stsp line = fparseln(f, &len, NULL, delim, 0);
138 4027f31a 2017-11-04 stsp if (line == NULL) {
139 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
143 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
145 4027f31a 2017-11-04 stsp free(line);
147 4027f31a 2017-11-04 stsp return err;
150 11995603 2017-11-05 stsp static char *
151 11995603 2017-11-05 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
153 11995603 2017-11-05 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
154 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
155 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
156 d4a5a885 2017-11-05 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
157 d4a5a885 2017-11-05 stsp strncmp(refname, "refs/", 5) == 0)
158 11995603 2017-11-05 stsp return got_repo_get_path_git_dir(repo);
160 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
163 4027f31a 2017-11-04 stsp const struct got_error *
164 11995603 2017-11-05 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
165 4027f31a 2017-11-04 stsp const char *refname)
167 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
168 4027f31a 2017-11-04 stsp char *path_ref = NULL;
169 4027f31a 2017-11-04 stsp char *normpath = NULL;
170 4027f31a 2017-11-04 stsp const char *parent_dir;
171 11995603 2017-11-05 stsp char *path_refs = get_refs_dir_path(repo, refname);
173 11995603 2017-11-05 stsp if (path_refs == NULL) {
174 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
178 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
180 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
181 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
185 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
186 4027f31a 2017-11-04 stsp if (normpath == NULL) {
187 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
191 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
193 4027f31a 2017-11-04 stsp free(normpath);
194 4027f31a 2017-11-04 stsp free(path_ref);
195 11995603 2017-11-05 stsp free(path_refs);
196 4027f31a 2017-11-04 stsp return err;
200 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
202 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
203 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
205 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
209 11995603 2017-11-05 stsp struct got_reference *
210 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
212 11995603 2017-11-05 stsp struct got_reference *ret = calloc(1, sizeof(*ret));
213 11995603 2017-11-05 stsp char *name = NULL;
214 11995603 2017-11-05 stsp char *symref = NULL;
216 11995603 2017-11-05 stsp if (ret == NULL)
217 11995603 2017-11-05 stsp return NULL;
219 11995603 2017-11-05 stsp ret->flags = ref->flags;
220 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
221 11995603 2017-11-05 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
222 11995603 2017-11-05 stsp if (ret->ref.symref.name == NULL) {
224 11995603 2017-11-05 stsp return NULL;
226 11995603 2017-11-05 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
227 11995603 2017-11-05 stsp if (ret->ref.symref.ref == NULL) {
228 11995603 2017-11-05 stsp free(ret->ref.symref.name);
230 11995603 2017-11-05 stsp return NULL;
233 11995603 2017-11-05 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
234 11995603 2017-11-05 stsp if (ref->ref.ref.name == NULL) {
236 11995603 2017-11-05 stsp return NULL;
238 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
239 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
242 11995603 2017-11-05 stsp return ret;
245 11995603 2017-11-05 stsp static const struct got_error *
246 11995603 2017-11-05 stsp resolve_symbolic_ref(struct got_reference **resolved,
247 11995603 2017-11-05 stsp struct got_repository *repo, struct got_reference *ref)
249 11995603 2017-11-05 stsp struct got_reference *nextref;
250 11995603 2017-11-05 stsp const struct got_error *err;
252 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
254 11995603 2017-11-05 stsp return err;
256 11995603 2017-11-05 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
257 11995603 2017-11-05 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
259 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
261 11995603 2017-11-05 stsp got_ref_close(nextref);
262 11995603 2017-11-05 stsp return err;
265 11995603 2017-11-05 stsp const struct got_error *
266 11995603 2017-11-05 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
267 11995603 2017-11-05 stsp struct got_reference *ref)
269 11995603 2017-11-05 stsp const struct got_error *err;
271 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
272 11995603 2017-11-05 stsp struct got_reference *resolved = NULL;
273 11995603 2017-11-05 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
274 11995603 2017-11-05 stsp if (err == NULL)
275 11995603 2017-11-05 stsp err = got_ref_resolve(id, repo, resolved);
276 11995603 2017-11-05 stsp free(resolved);
277 11995603 2017-11-05 stsp return err;
280 11995603 2017-11-05 stsp *id = calloc(1, sizeof(**id));
281 11995603 2017-11-05 stsp if (*id == NULL)
282 11995603 2017-11-05 stsp return got_error(GOT_ERR_NO_MEM);
283 11995603 2017-11-05 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
284 11995603 2017-11-05 stsp return NULL;