Blame


1 5261c201 2018-04-01 stsp /*
2 5261c201 2018-04-01 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 5261c201 2018-04-01 stsp *
4 5261c201 2018-04-01 stsp * Permission to use, copy, modify, and distribute this software for any
5 5261c201 2018-04-01 stsp * purpose with or without fee is hereby granted, provided that the above
6 5261c201 2018-04-01 stsp * copyright notice and this permission notice appear in all copies.
7 5261c201 2018-04-01 stsp *
8 5261c201 2018-04-01 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 5261c201 2018-04-01 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 5261c201 2018-04-01 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 5261c201 2018-04-01 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 5261c201 2018-04-01 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 5261c201 2018-04-01 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 5261c201 2018-04-01 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 5261c201 2018-04-01 stsp */
16 5261c201 2018-04-01 stsp
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
19 5261c201 2018-04-01 stsp
20 5261c201 2018-04-01 stsp #include <sha1.h>
21 5261c201 2018-04-01 stsp #include <stdio.h>
22 5261c201 2018-04-01 stsp #include <stdlib.h>
23 5261c201 2018-04-01 stsp #include <string.h>
24 5261c201 2018-04-01 stsp #include <util.h>
25 5261c201 2018-04-01 stsp #include <zlib.h>
26 788c352e 2018-06-16 stsp #include <time.h>
27 5261c201 2018-04-01 stsp
28 5261c201 2018-04-01 stsp #include "got_error.h"
29 5261c201 2018-04-01 stsp #include "got_object.h"
30 5261c201 2018-04-01 stsp #include "got_repository.h"
31 5261c201 2018-04-01 stsp #include "got_reference.h"
32 5261c201 2018-04-01 stsp
33 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
34 5261c201 2018-04-01 stsp #include "got_lib_path.h"
35 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
36 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
37 5261c201 2018-04-01 stsp #include "got_lib_object.h"
38 5261c201 2018-04-01 stsp
39 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
40 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
41 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
42 d1f2edc9 2018-06-13 stsp
43 5261c201 2018-04-01 stsp /* A symbolic reference. */
44 5261c201 2018-04-01 stsp struct got_symref {
45 5261c201 2018-04-01 stsp char *name;
46 5261c201 2018-04-01 stsp char *ref;
47 5261c201 2018-04-01 stsp };
48 5261c201 2018-04-01 stsp
49 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
50 5261c201 2018-04-01 stsp struct got_ref {
51 5261c201 2018-04-01 stsp char *name;
52 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
53 5261c201 2018-04-01 stsp };
54 5261c201 2018-04-01 stsp
55 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
56 5261c201 2018-04-01 stsp struct got_reference {
57 5261c201 2018-04-01 stsp unsigned int flags;
58 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
59 5261c201 2018-04-01 stsp
60 5261c201 2018-04-01 stsp union {
61 5261c201 2018-04-01 stsp struct got_ref ref;
62 5261c201 2018-04-01 stsp struct got_symref symref;
63 5261c201 2018-04-01 stsp } ref;
64 5261c201 2018-04-01 stsp };
65 5261c201 2018-04-01 stsp
66 5261c201 2018-04-01 stsp static const struct got_error *
67 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
68 5261c201 2018-04-01 stsp {
69 5261c201 2018-04-01 stsp struct got_symref *symref;
70 5261c201 2018-04-01 stsp char *symref_name;
71 5261c201 2018-04-01 stsp char *symref_ref;
72 5261c201 2018-04-01 stsp
73 5261c201 2018-04-01 stsp if (line[0] == '\0')
74 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
75 5261c201 2018-04-01 stsp
76 5261c201 2018-04-01 stsp symref_name = strdup(name);
77 5261c201 2018-04-01 stsp if (symref_name == NULL)
78 5261c201 2018-04-01 stsp return got_error_from_errno();
79 5261c201 2018-04-01 stsp symref_ref = strdup(line);
80 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
81 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
82 5261c201 2018-04-01 stsp free(symref_name);
83 5261c201 2018-04-01 stsp return err;
84 5261c201 2018-04-01 stsp }
85 5261c201 2018-04-01 stsp
86 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
87 5261c201 2018-04-01 stsp if (*ref == NULL)
88 5261c201 2018-04-01 stsp return got_error_from_errno();
89 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
90 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
91 5261c201 2018-04-01 stsp symref->name = symref_name;
92 5261c201 2018-04-01 stsp symref->ref = symref_ref;
93 5261c201 2018-04-01 stsp return NULL;
94 5261c201 2018-04-01 stsp }
95 5261c201 2018-04-01 stsp
96 5261c201 2018-04-01 stsp static const struct got_error *
97 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
98 5261c201 2018-04-01 stsp {
99 5261c201 2018-04-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
100 5261c201 2018-04-01 stsp char *ref_name;
101 5261c201 2018-04-01 stsp
102 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
103 5261c201 2018-04-01 stsp line += 5;
104 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
105 5261c201 2018-04-01 stsp }
106 5261c201 2018-04-01 stsp
107 5261c201 2018-04-01 stsp ref_name = strdup(name);
108 5261c201 2018-04-01 stsp if (ref_name == NULL)
109 5261c201 2018-04-01 stsp return got_error_from_errno();
110 5261c201 2018-04-01 stsp
111 5261c201 2018-04-01 stsp if (!got_parse_sha1_digest(digest, line))
112 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
113 5261c201 2018-04-01 stsp
114 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
115 5261c201 2018-04-01 stsp if (*ref == NULL)
116 5261c201 2018-04-01 stsp return got_error_from_errno();
117 5261c201 2018-04-01 stsp (*ref)->ref.ref.name = ref_name;
118 5261c201 2018-04-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
119 5261c201 2018-04-01 stsp return NULL;
120 5261c201 2018-04-01 stsp }
121 5261c201 2018-04-01 stsp
122 5261c201 2018-04-01 stsp static const struct got_error *
123 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
124 5261c201 2018-04-01 stsp const char *abspath)
125 5261c201 2018-04-01 stsp {
126 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
127 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
128 5261c201 2018-04-01 stsp char *line;
129 5261c201 2018-04-01 stsp size_t len;
130 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
131 5261c201 2018-04-01 stsp
132 5261c201 2018-04-01 stsp if (f == NULL)
133 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
134 5261c201 2018-04-01 stsp
135 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
136 5261c201 2018-04-01 stsp if (line == NULL) {
137 5261c201 2018-04-01 stsp err = got_error(GOT_ERR_NOT_REF);
138 5261c201 2018-04-01 stsp goto done;
139 5261c201 2018-04-01 stsp }
140 5261c201 2018-04-01 stsp
141 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
142 5261c201 2018-04-01 stsp done:
143 5261c201 2018-04-01 stsp free(line);
144 5261c201 2018-04-01 stsp fclose(f);
145 5261c201 2018-04-01 stsp return err;
146 5261c201 2018-04-01 stsp }
147 5261c201 2018-04-01 stsp
148 5261c201 2018-04-01 stsp static char *
149 5261c201 2018-04-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
150 5261c201 2018-04-01 stsp {
151 5261c201 2018-04-01 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
155 5261c201 2018-04-01 stsp strncmp(refname, "refs/", 5) == 0)
156 5261c201 2018-04-01 stsp return got_repo_get_path_git_dir(repo);
157 5261c201 2018-04-01 stsp
158 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
159 5261c201 2018-04-01 stsp }
160 5261c201 2018-04-01 stsp
161 d1f2edc9 2018-06-13 stsp static const struct got_error *
162 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
163 d1f2edc9 2018-06-13 stsp const char *refname)
164 d1f2edc9 2018-06-13 stsp {
165 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
166 d1f2edc9 2018-06-13 stsp char *path_ref;
167 d1f2edc9 2018-06-13 stsp char *normpath;
168 d1f2edc9 2018-06-13 stsp
169 d1f2edc9 2018-06-13 stsp if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
170 d1f2edc9 2018-06-13 stsp return got_error_from_errno();
171 d1f2edc9 2018-06-13 stsp
172 d1f2edc9 2018-06-13 stsp normpath = got_path_normalize(path_ref);
173 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
174 d1f2edc9 2018-06-13 stsp err = got_error(GOT_ERR_NOT_REF);
175 d1f2edc9 2018-06-13 stsp goto done;
176 d1f2edc9 2018-06-13 stsp }
177 d1f2edc9 2018-06-13 stsp
178 d1f2edc9 2018-06-13 stsp err = parse_ref_file(ref, refname, normpath);
179 d1f2edc9 2018-06-13 stsp done:
180 d1f2edc9 2018-06-13 stsp free(path_ref);
181 d1f2edc9 2018-06-13 stsp free(normpath);
182 d1f2edc9 2018-06-13 stsp return err;
183 d1f2edc9 2018-06-13 stsp }
184 d1f2edc9 2018-06-13 stsp
185 5261c201 2018-04-01 stsp const struct got_error *
186 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
187 5261c201 2018-04-01 stsp const char *refname)
188 5261c201 2018-04-01 stsp {
189 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
190 5261c201 2018-04-01 stsp char *path_refs = get_refs_dir_path(repo, refname);
191 5261c201 2018-04-01 stsp
192 5261c201 2018-04-01 stsp if (path_refs == NULL) {
193 5261c201 2018-04-01 stsp err = got_error_from_errno();
194 5261c201 2018-04-01 stsp goto done;
195 5261c201 2018-04-01 stsp }
196 5261c201 2018-04-01 stsp
197 5261c201 2018-04-01 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
198 5261c201 2018-04-01 stsp
199 d1f2edc9 2018-06-13 stsp err = open_ref(ref, path_refs, GOT_REF_HEADS, refname);
200 d1f2edc9 2018-06-13 stsp if (err != NULL)
201 d1f2edc9 2018-06-13 stsp err = open_ref(ref, path_refs, GOT_REF_TAGS, refname);
202 d1f2edc9 2018-06-13 stsp if (err != NULL)
203 d1f2edc9 2018-06-13 stsp err = open_ref(ref, path_refs, GOT_REF_REMOTES, refname);
204 d1f2edc9 2018-06-13 stsp if (err != NULL)
205 d1f2edc9 2018-06-13 stsp err = open_ref(ref, path_refs, "", refname);
206 5261c201 2018-04-01 stsp done:
207 5261c201 2018-04-01 stsp free(path_refs);
208 5261c201 2018-04-01 stsp return err;
209 5261c201 2018-04-01 stsp }
210 5261c201 2018-04-01 stsp
211 5261c201 2018-04-01 stsp void
212 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
213 5261c201 2018-04-01 stsp {
214 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
215 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
216 5261c201 2018-04-01 stsp else
217 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
218 5261c201 2018-04-01 stsp free(ref);
219 5261c201 2018-04-01 stsp }
220 5261c201 2018-04-01 stsp
221 5261c201 2018-04-01 stsp struct got_reference *
222 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
223 5261c201 2018-04-01 stsp {
224 5261c201 2018-04-01 stsp struct got_reference *ret;
225 5261c201 2018-04-01 stsp
226 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
227 5261c201 2018-04-01 stsp if (ret == NULL)
228 5261c201 2018-04-01 stsp return NULL;
229 5261c201 2018-04-01 stsp
230 5261c201 2018-04-01 stsp ret->flags = ref->flags;
231 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
232 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
233 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
234 5261c201 2018-04-01 stsp free(ret);
235 5261c201 2018-04-01 stsp return NULL;
236 5261c201 2018-04-01 stsp }
237 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
238 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
239 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
240 5261c201 2018-04-01 stsp free(ret);
241 5261c201 2018-04-01 stsp return NULL;
242 5261c201 2018-04-01 stsp }
243 5261c201 2018-04-01 stsp } else {
244 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
245 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
246 5261c201 2018-04-01 stsp free(ret);
247 5261c201 2018-04-01 stsp return NULL;
248 5261c201 2018-04-01 stsp }
249 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
250 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
251 5261c201 2018-04-01 stsp }
252 5261c201 2018-04-01 stsp
253 5261c201 2018-04-01 stsp return ret;
254 5261c201 2018-04-01 stsp }
255 5261c201 2018-04-01 stsp
256 5261c201 2018-04-01 stsp static const struct got_error *
257 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
258 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
259 5261c201 2018-04-01 stsp {
260 5261c201 2018-04-01 stsp struct got_reference *nextref;
261 5261c201 2018-04-01 stsp const struct got_error *err;
262 5261c201 2018-04-01 stsp
263 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
264 5261c201 2018-04-01 stsp if (err)
265 5261c201 2018-04-01 stsp return err;
266 5261c201 2018-04-01 stsp
267 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
268 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
269 5261c201 2018-04-01 stsp else
270 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
271 5261c201 2018-04-01 stsp
272 5261c201 2018-04-01 stsp got_ref_close(nextref);
273 5261c201 2018-04-01 stsp return err;
274 5261c201 2018-04-01 stsp }
275 5261c201 2018-04-01 stsp
276 5261c201 2018-04-01 stsp const struct got_error *
277 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
278 5261c201 2018-04-01 stsp struct got_reference *ref)
279 5261c201 2018-04-01 stsp {
280 5261c201 2018-04-01 stsp const struct got_error *err;
281 5261c201 2018-04-01 stsp
282 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
283 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
284 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
285 5261c201 2018-04-01 stsp if (err == NULL)
286 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
287 5261c201 2018-04-01 stsp free(resolved);
288 5261c201 2018-04-01 stsp return err;
289 5261c201 2018-04-01 stsp }
290 5261c201 2018-04-01 stsp
291 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
292 5261c201 2018-04-01 stsp if (*id == NULL)
293 5261c201 2018-04-01 stsp return got_error_from_errno();
294 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
295 5261c201 2018-04-01 stsp return NULL;
296 5261c201 2018-04-01 stsp }
297 5261c201 2018-04-01 stsp
298 5261c201 2018-04-01 stsp char *
299 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
300 5261c201 2018-04-01 stsp {
301 5261c201 2018-04-01 stsp char *str;
302 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
303 5261c201 2018-04-01 stsp if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
304 5261c201 2018-04-01 stsp return NULL;
305 5261c201 2018-04-01 stsp } else {
306 5261c201 2018-04-01 stsp str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
307 5261c201 2018-04-01 stsp if (str == NULL)
308 5261c201 2018-04-01 stsp return NULL;
309 5261c201 2018-04-01 stsp str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
310 5261c201 2018-04-01 stsp SHA1_DIGEST_STRING_LENGTH);
311 5261c201 2018-04-01 stsp }
312 5261c201 2018-04-01 stsp
313 5261c201 2018-04-01 stsp return str;
314 5261c201 2018-04-01 stsp }