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;
153 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
155 c5f754cc 2019-02-01 stsp return (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 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
161 c5f754cc 2019-02-01 stsp static char *
162 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
164 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
165 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
167 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
170 d1f2edc9 2018-06-13 stsp static const struct got_error *
171 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
172 fb79db15 2019-02-01 stsp const char *line)
174 fb79db15 2019-02-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
175 fb79db15 2019-02-01 stsp char *name;
177 fb79db15 2019-02-01 stsp *ref = NULL;
179 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
180 fb79db15 2019-02-01 stsp return NULL;
182 fb79db15 2019-02-01 stsp if (!got_parse_sha1_digest(digest, line))
183 fb79db15 2019-02-01 stsp return got_error(GOT_ERR_NOT_REF);
185 fb79db15 2019-02-01 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
186 fb79db15 2019-02-01 stsp return NULL;
188 fb79db15 2019-02-01 stsp name = strdup(abs_refname);
189 fb79db15 2019-02-01 stsp if (name == NULL)
190 fb79db15 2019-02-01 stsp return got_error_from_errno();
192 fb79db15 2019-02-01 stsp *ref = calloc(1, sizeof(**ref));
193 fb79db15 2019-02-01 stsp if (*ref == NULL)
194 fb79db15 2019-02-01 stsp return got_error_from_errno();
195 fb79db15 2019-02-01 stsp (*ref)->ref.ref.name = name;;
196 fb79db15 2019-02-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
197 fb79db15 2019-02-01 stsp return NULL;
200 fb79db15 2019-02-01 stsp static const struct got_error *
201 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
202 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
204 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
205 fb79db15 2019-02-01 stsp char *abs_refname;
206 fb79db15 2019-02-01 stsp char *line;
207 fb79db15 2019-02-01 stsp size_t len;
208 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
209 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
211 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
212 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
214 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
215 fb79db15 2019-02-01 stsp if (line == NULL) {
216 fb79db15 2019-02-01 stsp err = got_error(GOT_ERR_NOT_REF);
219 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
220 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
221 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
222 0dec1cc0 2019-02-01 stsp refname) == -1)
223 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
224 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
225 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
226 0dec1cc0 2019-02-01 stsp free(abs_refname);
227 532920c8 2019-02-01 stsp if (err || *ref != NULL)
230 fb79db15 2019-02-01 stsp free(line);
233 fb79db15 2019-02-01 stsp } while (*ref == NULL);
235 fb79db15 2019-02-01 stsp return err;
238 fb79db15 2019-02-01 stsp static const struct got_error *
239 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
240 d1f2edc9 2018-06-13 stsp const char *refname)
242 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
243 d1f2edc9 2018-06-13 stsp char *path_ref;
244 d1f2edc9 2018-06-13 stsp char *normpath;
246 d1f2edc9 2018-06-13 stsp if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
247 d1f2edc9 2018-06-13 stsp return got_error_from_errno();
249 d1f2edc9 2018-06-13 stsp normpath = got_path_normalize(path_ref);
250 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
251 d1f2edc9 2018-06-13 stsp err = got_error(GOT_ERR_NOT_REF);
255 d1f2edc9 2018-06-13 stsp err = parse_ref_file(ref, refname, normpath);
257 d1f2edc9 2018-06-13 stsp free(path_ref);
258 d1f2edc9 2018-06-13 stsp free(normpath);
259 d1f2edc9 2018-06-13 stsp return err;
262 5261c201 2018-04-01 stsp const struct got_error *
263 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
264 5261c201 2018-04-01 stsp const char *refname)
266 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
267 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
268 fb79db15 2019-02-01 stsp const char *subdirs[] = {
269 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
271 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
273 c5f754cc 2019-02-01 stsp if (!well_known) {
274 c5f754cc 2019-02-01 stsp char *packed_refs_path;
277 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
278 c5f754cc 2019-02-01 stsp if (packed_refs_path == NULL)
279 c5f754cc 2019-02-01 stsp return got_error_from_errno();
281 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
282 c5f754cc 2019-02-01 stsp free(packed_refs_path);
283 c5f754cc 2019-02-01 stsp if (f != NULL) {
284 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
287 0dec1cc0 2019-02-01 stsp if (err == NULL)
292 fb79db15 2019-02-01 stsp path_refs = get_refs_dir_path(repo, refname);
293 5261c201 2018-04-01 stsp if (path_refs == NULL) {
294 5261c201 2018-04-01 stsp err = got_error_from_errno();
298 c5f754cc 2019-02-01 stsp if (!well_known) {
299 c5f754cc 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
300 c5f754cc 2019-02-01 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
301 c5f754cc 2019-02-01 stsp if (err == NULL)
306 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
308 5261c201 2018-04-01 stsp free(path_refs);
309 5261c201 2018-04-01 stsp return err;
313 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
315 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
316 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
318 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
322 5261c201 2018-04-01 stsp struct got_reference *
323 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
325 5261c201 2018-04-01 stsp struct got_reference *ret;
327 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
328 5261c201 2018-04-01 stsp if (ret == NULL)
329 5261c201 2018-04-01 stsp return NULL;
331 5261c201 2018-04-01 stsp ret->flags = ref->flags;
332 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
333 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
334 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
336 5261c201 2018-04-01 stsp return NULL;
338 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
339 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
340 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
342 5261c201 2018-04-01 stsp return NULL;
345 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
346 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
348 5261c201 2018-04-01 stsp return NULL;
350 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
351 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
354 5261c201 2018-04-01 stsp return ret;
357 5261c201 2018-04-01 stsp static const struct got_error *
358 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
359 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
361 5261c201 2018-04-01 stsp struct got_reference *nextref;
362 5261c201 2018-04-01 stsp const struct got_error *err;
364 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
366 5261c201 2018-04-01 stsp return err;
368 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
369 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
371 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
373 5261c201 2018-04-01 stsp got_ref_close(nextref);
374 5261c201 2018-04-01 stsp return err;
377 5261c201 2018-04-01 stsp const struct got_error *
378 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
379 5261c201 2018-04-01 stsp struct got_reference *ref)
381 5261c201 2018-04-01 stsp const struct got_error *err;
383 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
384 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
385 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
386 5261c201 2018-04-01 stsp if (err == NULL)
387 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
388 5261c201 2018-04-01 stsp free(resolved);
389 5261c201 2018-04-01 stsp return err;
392 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
393 5261c201 2018-04-01 stsp if (*id == NULL)
394 5261c201 2018-04-01 stsp return got_error_from_errno();
395 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
396 5261c201 2018-04-01 stsp return NULL;
400 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
404 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
405 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
407 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
408 271d2a38 2018-12-25 stsp if (str == NULL)
409 271d2a38 2018-12-25 stsp return NULL;
411 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
412 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
414 271d2a38 2018-12-25 stsp return NULL;
417 5261c201 2018-04-01 stsp return str;
420 0bd18d37 2019-02-01 stsp const char *
421 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
423 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
424 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
426 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;