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 /* 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);
159 11995603 2017-11-05 stsp
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);
163 11995603 2017-11-05 stsp
164 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
165 11995603 2017-11-05 stsp }
166 11995603 2017-11-05 stsp
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)
170 4027f31a 2017-11-04 stsp {
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);
176 11995603 2017-11-05 stsp
177 11995603 2017-11-05 stsp if (path_refs == NULL) {
178 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
179 11995603 2017-11-05 stsp goto done;
180 11995603 2017-11-05 stsp }
181 4027f31a 2017-11-04 stsp
182 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
183 4027f31a 2017-11-04 stsp
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);
186 4027f31a 2017-11-04 stsp goto done;
187 4027f31a 2017-11-04 stsp }
188 4027f31a 2017-11-04 stsp
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);
192 4027f31a 2017-11-04 stsp goto done;
193 4027f31a 2017-11-04 stsp }
194 4027f31a 2017-11-04 stsp
195 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
196 4027f31a 2017-11-04 stsp done:
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;
201 4027f31a 2017-11-04 stsp }
202 4027f31a 2017-11-04 stsp
203 4027f31a 2017-11-04 stsp void
204 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
205 4027f31a 2017-11-04 stsp {
206 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
207 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
208 4027f31a 2017-11-04 stsp else
209 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
210 4027f31a 2017-11-04 stsp free(ref);
211 4027f31a 2017-11-04 stsp }
212 11995603 2017-11-05 stsp
213 11995603 2017-11-05 stsp struct got_reference *
214 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
215 11995603 2017-11-05 stsp {
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;
219 11995603 2017-11-05 stsp
220 11995603 2017-11-05 stsp if (ret == NULL)
221 11995603 2017-11-05 stsp return NULL;
222 11995603 2017-11-05 stsp
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) {
227 11995603 2017-11-05 stsp free(ret);
228 11995603 2017-11-05 stsp return NULL;
229 11995603 2017-11-05 stsp }
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);
233 11995603 2017-11-05 stsp free(ret);
234 11995603 2017-11-05 stsp return NULL;
235 11995603 2017-11-05 stsp }
236 11995603 2017-11-05 stsp } else {
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) {
239 11995603 2017-11-05 stsp free(ret);
240 11995603 2017-11-05 stsp return NULL;
241 11995603 2017-11-05 stsp }
242 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
243 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
244 11995603 2017-11-05 stsp }
245 11995603 2017-11-05 stsp
246 11995603 2017-11-05 stsp return ret;
247 11995603 2017-11-05 stsp }
248 11995603 2017-11-05 stsp
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)
252 11995603 2017-11-05 stsp {
253 11995603 2017-11-05 stsp struct got_reference *nextref;
254 11995603 2017-11-05 stsp const struct got_error *err;
255 11995603 2017-11-05 stsp
256 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
257 11995603 2017-11-05 stsp if (err)
258 11995603 2017-11-05 stsp return err;
259 11995603 2017-11-05 stsp
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);
262 11995603 2017-11-05 stsp else
263 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
264 11995603 2017-11-05 stsp
265 11995603 2017-11-05 stsp got_ref_close(nextref);
266 11995603 2017-11-05 stsp return err;
267 11995603 2017-11-05 stsp }
268 11995603 2017-11-05 stsp
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)
272 11995603 2017-11-05 stsp {
273 11995603 2017-11-05 stsp const struct got_error *err;
274 11995603 2017-11-05 stsp
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;
282 11995603 2017-11-05 stsp }
283 11995603 2017-11-05 stsp
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;
289 11995603 2017-11-05 stsp }