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 /* Some refs live in the .git directory. */
154 11995603 2017-11-05 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
155 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
156 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
157 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0)
158 11995603 2017-11-05 stsp return got_repo_get_path_git_dir(repo);
160 11995603 2017-11-05 stsp /* Is the ref name relative to the .git directory? */
161 11995603 2017-11-05 stsp if (strncmp(refname, "refs/", 5) == 0)
162 11995603 2017-11-05 stsp return got_repo_get_path_git_dir(repo);
164 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
167 4027f31a 2017-11-04 stsp const struct got_error *
168 11995603 2017-11-05 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
169 4027f31a 2017-11-04 stsp const char *refname)
171 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
172 4027f31a 2017-11-04 stsp char *path_ref = NULL;
173 4027f31a 2017-11-04 stsp char *normpath = NULL;
174 4027f31a 2017-11-04 stsp const char *parent_dir;
175 11995603 2017-11-05 stsp char *path_refs = get_refs_dir_path(repo, refname);
177 11995603 2017-11-05 stsp if (path_refs == NULL) {
178 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
182 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
184 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
185 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
189 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
190 4027f31a 2017-11-04 stsp if (normpath == NULL) {
191 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
195 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
197 4027f31a 2017-11-04 stsp free(normpath);
198 4027f31a 2017-11-04 stsp free(path_ref);
199 11995603 2017-11-05 stsp free(path_refs);
200 4027f31a 2017-11-04 stsp return err;
204 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
206 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
207 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
209 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
213 11995603 2017-11-05 stsp struct got_reference *
214 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
216 11995603 2017-11-05 stsp struct got_reference *ret = calloc(1, sizeof(*ret));
217 11995603 2017-11-05 stsp char *name = NULL;
218 11995603 2017-11-05 stsp char *symref = NULL;
220 11995603 2017-11-05 stsp if (ret == NULL)
221 11995603 2017-11-05 stsp return NULL;
223 11995603 2017-11-05 stsp ret->flags = ref->flags;
224 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
225 11995603 2017-11-05 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
226 11995603 2017-11-05 stsp if (ret->ref.symref.name == NULL) {
228 11995603 2017-11-05 stsp return NULL;
230 11995603 2017-11-05 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
231 11995603 2017-11-05 stsp if (ret->ref.symref.ref == NULL) {
232 11995603 2017-11-05 stsp free(ret->ref.symref.name);
234 11995603 2017-11-05 stsp return NULL;
237 11995603 2017-11-05 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
238 11995603 2017-11-05 stsp if (ref->ref.ref.name == NULL) {
240 11995603 2017-11-05 stsp return NULL;
242 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
243 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
246 11995603 2017-11-05 stsp return ret;
249 11995603 2017-11-05 stsp static const struct got_error *
250 11995603 2017-11-05 stsp resolve_symbolic_ref(struct got_reference **resolved,
251 11995603 2017-11-05 stsp struct got_repository *repo, struct got_reference *ref)
253 11995603 2017-11-05 stsp struct got_reference *nextref;
254 11995603 2017-11-05 stsp const struct got_error *err;
256 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
258 11995603 2017-11-05 stsp return err;
260 11995603 2017-11-05 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
261 11995603 2017-11-05 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
263 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
265 11995603 2017-11-05 stsp got_ref_close(nextref);
266 11995603 2017-11-05 stsp return err;
269 11995603 2017-11-05 stsp const struct got_error *
270 11995603 2017-11-05 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
271 11995603 2017-11-05 stsp struct got_reference *ref)
273 11995603 2017-11-05 stsp const struct got_error *err;
275 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
276 11995603 2017-11-05 stsp struct got_reference *resolved = NULL;
277 11995603 2017-11-05 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
278 11995603 2017-11-05 stsp if (err == NULL)
279 11995603 2017-11-05 stsp err = got_ref_resolve(id, repo, resolved);
280 11995603 2017-11-05 stsp free(resolved);
281 11995603 2017-11-05 stsp return err;
284 11995603 2017-11-05 stsp *id = calloc(1, sizeof(**id));
285 11995603 2017-11-05 stsp if (*id == NULL)
286 11995603 2017-11-05 stsp return got_error(GOT_ERR_NO_MEM);
287 11995603 2017-11-05 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
288 11995603 2017-11-05 stsp return NULL;