2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
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>
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"
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"
39 fb79db15 2019-02-01 stsp #ifndef nitems
40 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
44 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
45 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
47 5261c201 2018-04-01 stsp /* A symbolic reference. */
48 5261c201 2018-04-01 stsp struct got_symref {
53 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
54 5261c201 2018-04-01 stsp struct got_ref {
56 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
59 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
60 5261c201 2018-04-01 stsp struct got_reference {
61 5261c201 2018-04-01 stsp unsigned int flags;
62 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
65 5261c201 2018-04-01 stsp struct got_ref ref;
66 5261c201 2018-04-01 stsp struct got_symref symref;
70 5261c201 2018-04-01 stsp static const struct got_error *
71 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
73 5261c201 2018-04-01 stsp struct got_symref *symref;
74 5261c201 2018-04-01 stsp char *symref_name;
75 5261c201 2018-04-01 stsp char *symref_ref;
77 5261c201 2018-04-01 stsp if (line[0] == '\0')
78 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
80 5261c201 2018-04-01 stsp symref_name = strdup(name);
81 5261c201 2018-04-01 stsp if (symref_name == NULL)
82 5261c201 2018-04-01 stsp return got_error_from_errno();
83 5261c201 2018-04-01 stsp symref_ref = strdup(line);
84 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
85 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
86 5261c201 2018-04-01 stsp free(symref_name);
90 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
91 5261c201 2018-04-01 stsp if (*ref == NULL)
92 5261c201 2018-04-01 stsp return got_error_from_errno();
93 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
95 5261c201 2018-04-01 stsp symref->name = symref_name;
96 5261c201 2018-04-01 stsp symref->ref = symref_ref;
97 5261c201 2018-04-01 stsp return NULL;
100 5261c201 2018-04-01 stsp static const struct got_error *
101 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 5261c201 2018-04-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
104 5261c201 2018-04-01 stsp char *ref_name;
106 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
108 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
111 5261c201 2018-04-01 stsp ref_name = strdup(name);
112 5261c201 2018-04-01 stsp if (ref_name == NULL)
113 5261c201 2018-04-01 stsp return got_error_from_errno();
115 5261c201 2018-04-01 stsp if (!got_parse_sha1_digest(digest, line))
116 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
118 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
119 5261c201 2018-04-01 stsp if (*ref == NULL)
120 5261c201 2018-04-01 stsp return got_error_from_errno();
121 5261c201 2018-04-01 stsp (*ref)->ref.ref.name = ref_name;
122 5261c201 2018-04-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
123 5261c201 2018-04-01 stsp return NULL;
126 5261c201 2018-04-01 stsp static const struct got_error *
127 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
128 5261c201 2018-04-01 stsp const char *abspath)
130 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
131 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
132 5261c201 2018-04-01 stsp char *line;
133 5261c201 2018-04-01 stsp size_t len;
134 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
136 5261c201 2018-04-01 stsp if (f == NULL)
137 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
139 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
140 5261c201 2018-04-01 stsp if (line == NULL) {
141 5261c201 2018-04-01 stsp err = got_error(GOT_ERR_NOT_REF);
145 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
147 5261c201 2018-04-01 stsp free(line);
149 5261c201 2018-04-01 stsp return err;
152 5261c201 2018-04-01 stsp static char *
153 5261c201 2018-04-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
155 5261c201 2018-04-01 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
159 5261c201 2018-04-01 stsp strncmp(refname, "refs/", 5) == 0)
160 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
162 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
165 d1f2edc9 2018-06-13 stsp static const struct got_error *
166 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
167 fb79db15 2019-02-01 stsp const char *line)
169 fb79db15 2019-02-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
170 fb79db15 2019-02-01 stsp char *name;
172 fb79db15 2019-02-01 stsp *ref = NULL;
174 fb79db15 2019-02-01 stsp if (line[0] == '#')
175 fb79db15 2019-02-01 stsp return NULL;
177 fb79db15 2019-02-01 stsp if (!got_parse_sha1_digest(digest, line))
178 fb79db15 2019-02-01 stsp return got_error(GOT_ERR_NOT_REF);
180 fb79db15 2019-02-01 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
181 fb79db15 2019-02-01 stsp return NULL;
183 fb79db15 2019-02-01 stsp name = strdup(abs_refname);
184 fb79db15 2019-02-01 stsp if (name == NULL)
185 fb79db15 2019-02-01 stsp return got_error_from_errno();
187 fb79db15 2019-02-01 stsp *ref = calloc(1, sizeof(**ref));
188 fb79db15 2019-02-01 stsp if (*ref == NULL)
189 fb79db15 2019-02-01 stsp return got_error_from_errno();
190 fb79db15 2019-02-01 stsp (*ref)->ref.ref.name = name;;
191 fb79db15 2019-02-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
192 fb79db15 2019-02-01 stsp return NULL;
195 fb79db15 2019-02-01 stsp static const struct got_error *
196 fb79db15 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char *subdir,
197 fb79db15 2019-02-01 stsp const char *refname)
199 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
200 fb79db15 2019-02-01 stsp char *abs_refname;
201 fb79db15 2019-02-01 stsp char *line;
202 fb79db15 2019-02-01 stsp size_t len;
203 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
205 fb79db15 2019-02-01 stsp if (asprintf(&abs_refname, "refs/%s/%s", subdir, refname) == -1)
206 fb79db15 2019-02-01 stsp return got_error_from_errno();
209 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
210 fb79db15 2019-02-01 stsp if (line == NULL) {
211 fb79db15 2019-02-01 stsp err = got_error(GOT_ERR_NOT_REF);
215 fb79db15 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
216 fb79db15 2019-02-01 stsp free(line);
219 fb79db15 2019-02-01 stsp } while (*ref == NULL);
221 fb79db15 2019-02-01 stsp free(abs_refname);
222 fb79db15 2019-02-01 stsp return err;
225 fb79db15 2019-02-01 stsp static const struct got_error *
226 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
227 d1f2edc9 2018-06-13 stsp const char *refname)
229 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
230 d1f2edc9 2018-06-13 stsp char *path_ref;
231 d1f2edc9 2018-06-13 stsp char *normpath;
233 d1f2edc9 2018-06-13 stsp if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
234 d1f2edc9 2018-06-13 stsp return got_error_from_errno();
236 d1f2edc9 2018-06-13 stsp normpath = got_path_normalize(path_ref);
237 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
238 d1f2edc9 2018-06-13 stsp err = got_error(GOT_ERR_NOT_REF);
242 d1f2edc9 2018-06-13 stsp err = parse_ref_file(ref, refname, normpath);
244 d1f2edc9 2018-06-13 stsp free(path_ref);
245 d1f2edc9 2018-06-13 stsp free(normpath);
246 d1f2edc9 2018-06-13 stsp return err;
249 5261c201 2018-04-01 stsp const struct got_error *
250 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
251 5261c201 2018-04-01 stsp const char *refname)
253 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
254 fb79db15 2019-02-01 stsp char *path_refs;
255 fb79db15 2019-02-01 stsp const char *subdirs[] = {
256 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
258 fb79db15 2019-02-01 stsp char *packed_refs_path = got_repo_get_path_packed_refs(repo);
259 fb79db15 2019-02-01 stsp FILE *f = fopen(packed_refs_path, "rb");
263 fb79db15 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
264 fb79db15 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs[i], refname);
265 fb79db15 2019-02-01 stsp if (err == NULL)
266 fb79db15 2019-02-01 stsp return NULL;
273 fb79db15 2019-02-01 stsp path_refs = get_refs_dir_path(repo, refname);
274 5261c201 2018-04-01 stsp if (path_refs == NULL) {
275 5261c201 2018-04-01 stsp err = got_error_from_errno();
279 fb79db15 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
280 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
281 fb79db15 2019-02-01 stsp if (err == NULL)
284 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
286 5261c201 2018-04-01 stsp free(path_refs);
287 5261c201 2018-04-01 stsp return err;
291 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
293 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
294 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
296 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
300 5261c201 2018-04-01 stsp struct got_reference *
301 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
303 5261c201 2018-04-01 stsp struct got_reference *ret;
305 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
306 5261c201 2018-04-01 stsp if (ret == NULL)
307 5261c201 2018-04-01 stsp return NULL;
309 5261c201 2018-04-01 stsp ret->flags = ref->flags;
310 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
311 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
312 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
314 5261c201 2018-04-01 stsp return NULL;
316 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
317 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
318 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
320 5261c201 2018-04-01 stsp return NULL;
323 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
324 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
326 5261c201 2018-04-01 stsp return NULL;
328 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
329 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
332 5261c201 2018-04-01 stsp return ret;
335 5261c201 2018-04-01 stsp static const struct got_error *
336 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
337 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
339 5261c201 2018-04-01 stsp struct got_reference *nextref;
340 5261c201 2018-04-01 stsp const struct got_error *err;
342 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
344 5261c201 2018-04-01 stsp return err;
346 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
347 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
349 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
351 5261c201 2018-04-01 stsp got_ref_close(nextref);
352 5261c201 2018-04-01 stsp return err;
355 5261c201 2018-04-01 stsp const struct got_error *
356 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
357 5261c201 2018-04-01 stsp struct got_reference *ref)
359 5261c201 2018-04-01 stsp const struct got_error *err;
361 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
362 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
363 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
364 5261c201 2018-04-01 stsp if (err == NULL)
365 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
366 5261c201 2018-04-01 stsp free(resolved);
367 5261c201 2018-04-01 stsp return err;
370 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
371 5261c201 2018-04-01 stsp if (*id == NULL)
372 5261c201 2018-04-01 stsp return got_error_from_errno();
373 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
374 5261c201 2018-04-01 stsp return NULL;
378 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
382 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
383 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
385 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
386 271d2a38 2018-12-25 stsp if (str == NULL)
387 271d2a38 2018-12-25 stsp return NULL;
389 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
390 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
392 271d2a38 2018-12-25 stsp return NULL;
395 5261c201 2018-04-01 stsp return str;