Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sha1.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <util.h>
23 #include <limits.h>
24 #include <errno.h>
26 #include "got_error.h"
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_refs.h"
31 #include "path.h"
34 static const struct got_error *
35 parse_symref(struct got_reference **ref, const char *name, const char *line)
36 {
37 struct got_symref *symref;
38 char *symref_name;
39 char *symref_ref;
41 if (line[0] == '\0')
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) {
49 free(symref_name);
50 return got_error(GOT_ERR_NO_MEM);
51 }
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;
58 return NULL;
59 }
61 static int
62 parse_xdigit(uint8_t *val, const char *hex)
63 {
64 char *ep;
65 long lval;
67 errno = 0;
68 lval = strtol(hex, &ep, 16);
69 if (hex[0] == '\0' || *ep != '\0')
70 return 0;
71 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
72 return 0;
74 *val = (uint8_t)lval;
75 return 1;
76 }
78 static int
79 parse_sha1_digest(uint8_t *digest, const char *line)
80 {
81 uint8_t b = 0;
82 char hex[3] = {'\0', '\0', '\0'};
83 int i, j;
85 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
86 if (line[0] == '\0' || line[1] == '\0')
87 return 0;
88 for (j = 0; j < 2; j++) {
89 hex[j] = *line;
90 line++;
91 }
92 if (!parse_xdigit(&b, hex))
93 return 0;
94 digest[i] = b;
95 }
97 return 1;
98 }
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];
104 char *ref_name;
106 if (strncmp(line, "ref: ", 5) == 0) {
107 line += 5;
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);
121 return NULL;
124 static const struct got_error *
125 parse_ref_file(struct got_reference **ref, const char *name,
126 const char *abspath)
128 const struct got_error *err = NULL;
129 FILE *f = fopen(abspath, "rb");
130 char *line;
131 size_t len;
132 const char delim[3] = {'\0', '\0', '\0'};
134 if (f == NULL)
135 return got_error(GOT_ERR_NOT_REF);
137 line = fparseln(f, &len, NULL, delim, 0);
138 if (line == NULL) {
139 err = got_error(GOT_ERR_NOT_REF);
140 goto done;
143 err = parse_ref_line(ref, name, line);
144 done:
145 free(line);
146 fclose(f);
147 return err;
150 static char *
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,
165 const char *refname)
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);
175 goto done;
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);
182 goto done;
185 normpath = got_path_normalize(path_ref);
186 if (normpath == NULL) {
187 err = got_error(GOT_ERR_NOT_REF);
188 goto done;
191 err = parse_ref_file(ref, refname, normpath);
192 done:
193 free(normpath);
194 free(path_ref);
195 free(path_refs);
196 return err;
199 void
200 got_ref_close(struct got_reference *ref)
202 if (ref->flags & GOT_REF_IS_SYMBOLIC)
203 free(ref->ref.symref.name);
204 else
205 free(ref->ref.ref.name);
206 free(ref);
209 struct got_reference *
210 got_ref_dup(struct got_reference *ref)
212 struct got_reference *ret = calloc(1, sizeof(*ret));
213 char *name = NULL;
214 char *symref = NULL;
216 if (ret == NULL)
217 return NULL;
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) {
223 free(ret);
224 return NULL;
226 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
227 if (ret->ref.symref.ref == NULL) {
228 free(ret->ref.symref.name);
229 free(ret);
230 return NULL;
232 } else {
233 ref->ref.ref.name = strdup(ref->ref.ref.name);
234 if (ref->ref.ref.name == NULL) {
235 free(ret);
236 return NULL;
238 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
239 SHA1_DIGEST_LENGTH);
242 return ret;
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);
253 if (err)
254 return err;
256 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
257 err = resolve_symbolic_ref(resolved, repo, nextref);
258 else
259 *resolved = got_ref_dup(nextref);
261 got_ref_close(nextref);
262 return err;
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);
274 if (err == NULL)
275 err = got_ref_resolve(id, repo, resolved);
276 free(resolved);
277 return err;
280 *id = calloc(1, sizeof(**id));
281 if (*id == NULL)
282 return got_error(GOT_ERR_NO_MEM);
283 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
284 return NULL;