Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
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.
7 7b19e0f1 2017-11-05 stsp *
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.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
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>
25 4027f31a 2017-11-04 stsp
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"
30 4027f31a 2017-11-04 stsp
31 c3f94f68 2017-11-05 stsp #include "path.h"
32 c3f94f68 2017-11-05 stsp
33 11995603 2017-11-05 stsp
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)
36 4027f31a 2017-11-04 stsp {
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;
40 4027f31a 2017-11-04 stsp
41 4027f31a 2017-11-04 stsp if (line[0] == '\0')
42 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
43 4027f31a 2017-11-04 stsp
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);
51 4027f31a 2017-11-04 stsp }
52 4027f31a 2017-11-04 stsp
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;
59 4027f31a 2017-11-04 stsp }
60 4027f31a 2017-11-04 stsp
61 4027f31a 2017-11-04 stsp static int
62 11995603 2017-11-05 stsp parse_xdigit(uint8_t *val, const char *hex)
63 11995603 2017-11-05 stsp {
64 11995603 2017-11-05 stsp char *ep;
65 11995603 2017-11-05 stsp long lval;
66 11995603 2017-11-05 stsp
67 11995603 2017-11-05 stsp errno = 0;
68 11995603 2017-11-05 stsp lval = strtol(hex, &ep, 16);
69 11995603 2017-11-05 stsp if (hex[0] == '\0' || *ep != '\0')
70 11995603 2017-11-05 stsp return 0;
71 11995603 2017-11-05 stsp if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
72 11995603 2017-11-05 stsp return 0;
73 11995603 2017-11-05 stsp
74 11995603 2017-11-05 stsp *val = (uint8_t)lval;
75 11995603 2017-11-05 stsp return 1;
76 11995603 2017-11-05 stsp }
77 11995603 2017-11-05 stsp
78 11995603 2017-11-05 stsp static int
79 4027f31a 2017-11-04 stsp parse_sha1_digest(uint8_t *digest, const char *line)
80 4027f31a 2017-11-04 stsp {
81 11995603 2017-11-05 stsp uint8_t b = 0;
82 11995603 2017-11-05 stsp char hex[3] = {'\0', '\0', '\0'};
83 11995603 2017-11-05 stsp int i, j;
84 4027f31a 2017-11-04 stsp
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')
87 4027f31a 2017-11-04 stsp return 0;
88 11995603 2017-11-05 stsp for (j = 0; j < 2; j++) {
89 11995603 2017-11-05 stsp hex[j] = *line;
90 11995603 2017-11-05 stsp line++;
91 11995603 2017-11-05 stsp }
92 11995603 2017-11-05 stsp if (!parse_xdigit(&b, hex))
93 11995603 2017-11-05 stsp return 0;
94 11995603 2017-11-05 stsp digest[i] = b;
95 4027f31a 2017-11-04 stsp }
96 4027f31a 2017-11-04 stsp
97 4027f31a 2017-11-04 stsp return 1;
98 4027f31a 2017-11-04 stsp }
99 4027f31a 2017-11-04 stsp
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)
102 4027f31a 2017-11-04 stsp {
103 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
104 4027f31a 2017-11-04 stsp char *ref_name;
105 4027f31a 2017-11-04 stsp
106 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
107 4027f31a 2017-11-04 stsp line += 5;
108 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
109 4027f31a 2017-11-04 stsp }
110 4027f31a 2017-11-04 stsp
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);
114 4027f31a 2017-11-04 stsp
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);
117 4027f31a 2017-11-04 stsp
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;
122 4027f31a 2017-11-04 stsp }
123 4027f31a 2017-11-04 stsp
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)
127 4027f31a 2017-11-04 stsp {
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'};
133 4027f31a 2017-11-04 stsp
134 4027f31a 2017-11-04 stsp if (f == NULL)
135 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
136 4027f31a 2017-11-04 stsp
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);
140 4027f31a 2017-11-04 stsp goto done;
141 4027f31a 2017-11-04 stsp }
142 4027f31a 2017-11-04 stsp
143 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
144 4027f31a 2017-11-04 stsp done:
145 4027f31a 2017-11-04 stsp free(line);
146 4027f31a 2017-11-04 stsp fclose(f);
147 4027f31a 2017-11-04 stsp return err;
148 4027f31a 2017-11-04 stsp }
149 4027f31a 2017-11-04 stsp
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)
152 11995603 2017-11-05 stsp {
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);
159 11995603 2017-11-05 stsp
160 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
161 11995603 2017-11-05 stsp }
162 11995603 2017-11-05 stsp
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)
166 4027f31a 2017-11-04 stsp {
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);
172 11995603 2017-11-05 stsp
173 11995603 2017-11-05 stsp if (path_refs == NULL) {
174 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
175 11995603 2017-11-05 stsp goto done;
176 11995603 2017-11-05 stsp }
177 4027f31a 2017-11-04 stsp
178 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
179 4027f31a 2017-11-04 stsp
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);
182 4027f31a 2017-11-04 stsp goto done;
183 4027f31a 2017-11-04 stsp }
184 4027f31a 2017-11-04 stsp
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);
188 4027f31a 2017-11-04 stsp goto done;
189 4027f31a 2017-11-04 stsp }
190 4027f31a 2017-11-04 stsp
191 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
192 4027f31a 2017-11-04 stsp done:
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;
197 4027f31a 2017-11-04 stsp }
198 4027f31a 2017-11-04 stsp
199 4027f31a 2017-11-04 stsp void
200 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
201 4027f31a 2017-11-04 stsp {
202 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
203 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
204 4027f31a 2017-11-04 stsp else
205 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
206 4027f31a 2017-11-04 stsp free(ref);
207 4027f31a 2017-11-04 stsp }
208 11995603 2017-11-05 stsp
209 11995603 2017-11-05 stsp struct got_reference *
210 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
211 11995603 2017-11-05 stsp {
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;
215 11995603 2017-11-05 stsp
216 11995603 2017-11-05 stsp if (ret == NULL)
217 11995603 2017-11-05 stsp return NULL;
218 11995603 2017-11-05 stsp
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) {
223 11995603 2017-11-05 stsp free(ret);
224 11995603 2017-11-05 stsp return NULL;
225 11995603 2017-11-05 stsp }
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);
229 11995603 2017-11-05 stsp free(ret);
230 11995603 2017-11-05 stsp return NULL;
231 11995603 2017-11-05 stsp }
232 11995603 2017-11-05 stsp } else {
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) {
235 11995603 2017-11-05 stsp free(ret);
236 11995603 2017-11-05 stsp return NULL;
237 11995603 2017-11-05 stsp }
238 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
239 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
240 11995603 2017-11-05 stsp }
241 11995603 2017-11-05 stsp
242 11995603 2017-11-05 stsp return ret;
243 11995603 2017-11-05 stsp }
244 11995603 2017-11-05 stsp
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)
248 11995603 2017-11-05 stsp {
249 11995603 2017-11-05 stsp struct got_reference *nextref;
250 11995603 2017-11-05 stsp const struct got_error *err;
251 11995603 2017-11-05 stsp
252 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
253 11995603 2017-11-05 stsp if (err)
254 11995603 2017-11-05 stsp return err;
255 11995603 2017-11-05 stsp
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);
258 11995603 2017-11-05 stsp else
259 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
260 11995603 2017-11-05 stsp
261 11995603 2017-11-05 stsp got_ref_close(nextref);
262 11995603 2017-11-05 stsp return err;
263 11995603 2017-11-05 stsp }
264 11995603 2017-11-05 stsp
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)
268 11995603 2017-11-05 stsp {
269 11995603 2017-11-05 stsp const struct got_error *err;
270 11995603 2017-11-05 stsp
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;
278 11995603 2017-11-05 stsp }
279 11995603 2017-11-05 stsp
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;
285 11995603 2017-11-05 stsp }