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>
19 9e672c74 2019-03-11 stsp #include <sys/stat.h>
21 0fd469ce 2019-03-11 stsp #include <errno.h>
22 f77a24b0 2019-03-11 stsp #include <ctype.h>
23 a04f49d2 2019-02-04 stsp #include <dirent.h>
24 5261c201 2018-04-01 stsp #include <sha1.h>
25 5261c201 2018-04-01 stsp #include <stdio.h>
26 5261c201 2018-04-01 stsp #include <stdlib.h>
27 5261c201 2018-04-01 stsp #include <string.h>
28 5261c201 2018-04-01 stsp #include <util.h>
29 5261c201 2018-04-01 stsp #include <zlib.h>
30 788c352e 2018-06-16 stsp #include <time.h>
31 0cd1c46a 2019-03-11 stsp #include <libgen.h>
33 5261c201 2018-04-01 stsp #include "got_error.h"
34 5261c201 2018-04-01 stsp #include "got_object.h"
35 5261c201 2018-04-01 stsp #include "got_repository.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
39 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
40 5261c201 2018-04-01 stsp #include "got_lib_path.h"
41 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 5261c201 2018-04-01 stsp #include "got_lib_object.h"
44 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
46 fb79db15 2019-02-01 stsp #ifndef nitems
47 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
51 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
52 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
54 5261c201 2018-04-01 stsp /* A symbolic reference. */
55 5261c201 2018-04-01 stsp struct got_symref {
60 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
61 5261c201 2018-04-01 stsp struct got_ref {
63 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
66 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
67 5261c201 2018-04-01 stsp struct got_reference {
68 5261c201 2018-04-01 stsp unsigned int flags;
69 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
70 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
73 5261c201 2018-04-01 stsp struct got_ref ref;
74 5261c201 2018-04-01 stsp struct got_symref symref;
78 5261c201 2018-04-01 stsp static const struct got_error *
79 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
80 f9267c9a 2019-03-15 stsp struct got_object_id *id, int flags)
82 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
84 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
85 f9267c9a 2019-03-15 stsp if (*ref == NULL)
86 5261c201 2018-04-01 stsp return got_error_from_errno();
88 f9267c9a 2019-03-15 stsp memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
89 c980e470 2019-03-15 stsp (*ref)->flags = flags;
90 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
91 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
92 f9267c9a 2019-03-15 stsp err = got_error_from_errno();
93 c980e470 2019-03-15 stsp got_ref_close(*ref);
94 f9267c9a 2019-03-15 stsp *ref = NULL;
99 f9267c9a 2019-03-15 stsp static const struct got_error *
100 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
101 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
103 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
105 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
106 5261c201 2018-04-01 stsp if (*ref == NULL)
107 5261c201 2018-04-01 stsp return got_error_from_errno();
109 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
110 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
111 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
112 f9267c9a 2019-03-15 stsp err = got_error_from_errno();
113 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
114 f9267c9a 2019-03-15 stsp *ref = NULL;
116 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
117 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
118 f9267c9a 2019-03-15 stsp err = got_error_from_errno();
119 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
120 f9267c9a 2019-03-15 stsp *ref = NULL;
122 f9267c9a 2019-03-15 stsp return err;
125 5261c201 2018-04-01 stsp static const struct got_error *
126 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
128 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
129 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
131 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
134 f9267c9a 2019-03-15 stsp static const struct got_error *
135 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
137 5892cdd6 2019-03-10 stsp struct got_object_id id;
139 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
141 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
144 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
145 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
147 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, 0);
150 5261c201 2018-04-01 stsp static const struct got_error *
151 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
152 5261c201 2018-04-01 stsp const char *abspath)
154 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
156 5261c201 2018-04-01 stsp char *line;
157 5261c201 2018-04-01 stsp size_t len;
158 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
160 c0a1c016 2019-03-15 stsp f = fopen(abspath, "rb");
161 5261c201 2018-04-01 stsp if (f == NULL)
162 1e37702e 2019-02-04 stsp return NULL;
164 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
165 5261c201 2018-04-01 stsp if (line == NULL) {
166 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
170 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
172 5261c201 2018-04-01 stsp free(line);
173 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
174 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
175 5261c201 2018-04-01 stsp return err;
179 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
181 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
182 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
183 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
184 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
187 c5f754cc 2019-02-01 stsp static char *
188 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
190 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
191 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
193 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
197 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
199 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
200 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
201 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
202 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
203 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
206 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
209 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
210 f77a24b0 2019-03-11 stsp if (slash == NULL)
215 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
217 f77a24b0 2019-03-11 stsp while (*s) {
218 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
219 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
222 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
223 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
224 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
227 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
229 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
231 f77a24b0 2019-03-11 stsp if (*s == '/') {
232 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
233 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
234 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
236 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
237 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
239 f77a24b0 2019-03-11 stsp seg = nextseg;
244 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
245 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
251 5892cdd6 2019-03-10 stsp const struct got_error *
252 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
253 5892cdd6 2019-03-10 stsp struct got_object_id *id)
255 f77a24b0 2019-03-11 stsp if (!is_valid_ref_name(name))
256 f77a24b0 2019-03-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
258 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, id, 0);
261 d1f2edc9 2018-06-13 stsp static const struct got_error *
262 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
263 fb79db15 2019-02-01 stsp const char *line)
265 5892cdd6 2019-03-10 stsp struct got_object_id id;
266 5892cdd6 2019-03-10 stsp const char *name;
268 fb79db15 2019-02-01 stsp *ref = NULL;
270 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
271 fb79db15 2019-02-01 stsp return NULL;
273 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
274 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
276 199a4027 2019-02-02 stsp if (abs_refname) {
277 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
278 199a4027 2019-02-02 stsp return NULL;
279 5892cdd6 2019-03-10 stsp name = abs_refname;
281 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
283 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
286 fb79db15 2019-02-01 stsp static const struct got_error *
287 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
288 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
290 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
291 fb79db15 2019-02-01 stsp char *abs_refname;
292 fb79db15 2019-02-01 stsp char *line;
293 fb79db15 2019-02-01 stsp size_t len;
294 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
295 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
297 1e37702e 2019-02-04 stsp *ref = NULL;
299 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
300 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
302 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
303 7ab0422a 2019-03-15 stsp if (line == NULL) {
304 7ab0422a 2019-03-15 stsp if (feof(f))
306 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
309 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
310 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
311 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
312 0dec1cc0 2019-02-01 stsp refname) == -1)
313 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
314 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
315 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
316 0dec1cc0 2019-02-01 stsp free(abs_refname);
317 532920c8 2019-02-01 stsp if (err || *ref != NULL)
320 fb79db15 2019-02-01 stsp free(line);
323 fb79db15 2019-02-01 stsp } while (*ref == NULL);
325 fb79db15 2019-02-01 stsp return err;
328 fb79db15 2019-02-01 stsp static const struct got_error *
329 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
330 a04f49d2 2019-02-04 stsp const char *name)
332 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
333 a04f49d2 2019-02-04 stsp char *path = NULL;
334 a04f49d2 2019-02-04 stsp char *normpath = NULL;
335 a04f49d2 2019-02-04 stsp char *absname = NULL;
336 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
337 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
339 1e37702e 2019-02-04 stsp *ref = NULL;
341 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
342 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
343 a04f49d2 2019-02-04 stsp return got_error_from_errno();
344 a04f49d2 2019-02-04 stsp absname = (char *)name;
346 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
347 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
348 a04f49d2 2019-02-04 stsp return got_error_from_errno();
350 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
351 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
352 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
357 a04f49d2 2019-02-04 stsp normpath = got_path_normalize(path);
358 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
359 30c0868d 2019-02-03 stsp err = got_error_from_errno();
363 a04f49d2 2019-02-04 stsp err = parse_ref_file(ref, absname, normpath);
365 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
366 a04f49d2 2019-02-04 stsp free(absname);
367 a04f49d2 2019-02-04 stsp free(path);
368 d1f2edc9 2018-06-13 stsp free(normpath);
369 d1f2edc9 2018-06-13 stsp return err;
372 5261c201 2018-04-01 stsp const struct got_error *
373 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
374 5261c201 2018-04-01 stsp const char *refname)
376 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
377 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
378 fb79db15 2019-02-01 stsp const char *subdirs[] = {
379 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
381 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
383 1e37702e 2019-02-04 stsp *ref = NULL;
385 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
386 e135804e 2019-02-10 stsp if (path_refs == NULL) {
387 e135804e 2019-02-10 stsp err = got_error_from_errno();
391 c5f754cc 2019-02-01 stsp if (!well_known) {
392 c5f754cc 2019-02-01 stsp char *packed_refs_path;
395 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
396 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
397 e135804e 2019-02-10 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
398 e135804e 2019-02-10 stsp if (err || *ref)
402 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
403 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
404 e135804e 2019-02-10 stsp err = got_error_from_errno();
408 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
409 c5f754cc 2019-02-01 stsp free(packed_refs_path);
410 c5f754cc 2019-02-01 stsp if (f != NULL) {
411 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
413 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
414 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
415 1e37702e 2019-02-04 stsp if (err || *ref)
420 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
424 1e37702e 2019-02-04 stsp if (*ref == NULL)
425 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
426 5261c201 2018-04-01 stsp free(path_refs);
427 5261c201 2018-04-01 stsp return err;
431 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
433 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
434 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
435 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
437 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
441 5261c201 2018-04-01 stsp struct got_reference *
442 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
444 5261c201 2018-04-01 stsp struct got_reference *ret;
446 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
447 5261c201 2018-04-01 stsp if (ret == NULL)
448 5261c201 2018-04-01 stsp return NULL;
450 5261c201 2018-04-01 stsp ret->flags = ref->flags;
451 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
452 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
453 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
455 5261c201 2018-04-01 stsp return NULL;
457 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
458 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
459 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
461 5261c201 2018-04-01 stsp return NULL;
464 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
465 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
467 5261c201 2018-04-01 stsp return NULL;
469 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
470 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
473 5261c201 2018-04-01 stsp return ret;
476 5261c201 2018-04-01 stsp static const struct got_error *
477 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
478 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
480 5261c201 2018-04-01 stsp struct got_reference *nextref;
481 5261c201 2018-04-01 stsp const struct got_error *err;
483 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
485 5261c201 2018-04-01 stsp return err;
487 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
488 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
490 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
492 5261c201 2018-04-01 stsp got_ref_close(nextref);
493 5261c201 2018-04-01 stsp return err;
496 5261c201 2018-04-01 stsp const struct got_error *
497 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
498 5261c201 2018-04-01 stsp struct got_reference *ref)
500 5261c201 2018-04-01 stsp const struct got_error *err;
502 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
503 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
504 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
505 5261c201 2018-04-01 stsp if (err == NULL)
506 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
507 c53671d0 2019-03-15 stsp got_ref_close(resolved);
508 5261c201 2018-04-01 stsp return err;
511 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
512 5261c201 2018-04-01 stsp if (*id == NULL)
513 5261c201 2018-04-01 stsp return got_error_from_errno();
514 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
515 5261c201 2018-04-01 stsp return NULL;
519 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
523 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
524 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
526 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
527 271d2a38 2018-12-25 stsp if (str == NULL)
528 271d2a38 2018-12-25 stsp return NULL;
530 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
531 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
533 271d2a38 2018-12-25 stsp return NULL;
536 5261c201 2018-04-01 stsp return str;
539 0bd18d37 2019-02-01 stsp const char *
540 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
542 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
543 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
545 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
548 199a4027 2019-02-02 stsp static const struct got_error *
549 505287be 2019-03-15 stsp insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
550 505287be 2019-03-15 stsp struct got_reference *ref, struct got_repository *repo)
552 199a4027 2019-02-02 stsp const struct got_error *err;
553 199a4027 2019-02-02 stsp struct got_object_id *id;
554 7a3c76f5 2019-02-05 stsp struct got_reflist_entry *new, *re, *prev;
557 505287be 2019-03-15 stsp *newp = NULL;
559 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
561 7a3c76f5 2019-02-05 stsp return err;
563 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
564 7a3c76f5 2019-02-05 stsp if (new == NULL) {
566 7a3c76f5 2019-02-05 stsp return got_error_from_errno();
568 7a3c76f5 2019-02-05 stsp new->ref = ref;
569 7a3c76f5 2019-02-05 stsp new->id = id;
570 505287be 2019-03-15 stsp *newp = new;
573 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
574 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
575 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
576 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
578 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
579 7a3c76f5 2019-02-05 stsp while (re) {
580 7a3c76f5 2019-02-05 stsp cmp = got_path_cmp(got_ref_get_name(re->ref),
581 27a1ed03 2019-03-15 stsp got_ref_get_name(new->ref));
582 e397b6db 2019-02-04 stsp if (cmp == 0) {
583 27a1ed03 2019-03-15 stsp /* duplicate */
584 27a1ed03 2019-03-15 stsp free(new->id);
586 505287be 2019-03-15 stsp *newp = NULL;
587 7a3c76f5 2019-02-05 stsp return NULL;
588 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
590 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
592 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
593 7a3c76f5 2019-02-05 stsp return NULL;
596 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
600 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
601 199a4027 2019-02-02 stsp return NULL;
604 a04f49d2 2019-02-04 stsp static const struct got_error *
605 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
606 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
608 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
609 a04f49d2 2019-02-04 stsp DIR *d = NULL;
610 a04f49d2 2019-02-04 stsp char *path_subdir;
612 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
613 a04f49d2 2019-02-04 stsp return got_error_from_errno();
615 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
616 a04f49d2 2019-02-04 stsp if (d == NULL)
619 a04f49d2 2019-02-04 stsp while (1) {
620 a04f49d2 2019-02-04 stsp struct dirent *dent;
621 a04f49d2 2019-02-04 stsp struct got_reference *ref;
622 a04f49d2 2019-02-04 stsp char *child;
624 a04f49d2 2019-02-04 stsp dent = readdir(d);
625 a04f49d2 2019-02-04 stsp if (dent == NULL)
628 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
629 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
632 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
633 a04f49d2 2019-02-04 stsp case DT_REG:
634 a04f49d2 2019-02-04 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name);
638 505287be 2019-03-15 stsp struct got_reflist_entry *new;
639 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
640 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
641 505287be 2019-03-15 stsp got_ref_close(ref);
646 a04f49d2 2019-02-04 stsp case DT_DIR:
647 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
648 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
649 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
652 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, child, repo);
653 a04f49d2 2019-02-04 stsp free(child);
661 a04f49d2 2019-02-04 stsp closedir(d);
662 a04f49d2 2019-02-04 stsp free(path_subdir);
663 a04f49d2 2019-02-04 stsp return err;
666 199a4027 2019-02-02 stsp const struct got_error *
667 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
669 199a4027 2019-02-02 stsp const struct got_error *err;
670 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
671 29b5c214 2019-02-04 stsp FILE *f = NULL;
672 199a4027 2019-02-02 stsp struct got_reference *ref;
673 505287be 2019-03-15 stsp struct got_reflist_entry *new;
675 29b5c214 2019-02-04 stsp /* HEAD ref should always exist. */
676 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
677 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
678 29b5c214 2019-02-04 stsp err = got_error_from_errno();
681 29b5c214 2019-02-04 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
684 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
685 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
686 505287be 2019-03-15 stsp got_ref_close(ref);
690 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
691 29b5c214 2019-02-04 stsp free(path_refs);
692 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
693 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
694 29b5c214 2019-02-04 stsp err = got_error_from_errno();
697 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, "", repo);
702 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
703 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
705 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
706 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
707 29b5c214 2019-02-04 stsp err = got_error_from_errno();
711 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
712 199a4027 2019-02-02 stsp free(packed_refs_path);
714 199a4027 2019-02-02 stsp char *line;
715 199a4027 2019-02-02 stsp size_t len;
716 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
717 199a4027 2019-02-02 stsp while (1) {
718 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
719 0bb4abae 2019-03-15 stsp if (line == NULL) {
720 0bb4abae 2019-03-15 stsp if (feof(f))
722 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
725 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
726 0bb4abae 2019-03-15 stsp free(line);
730 505287be 2019-03-15 stsp err = insert_ref(&new, refs, ref, repo);
731 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
732 505287be 2019-03-15 stsp got_ref_close(ref);
739 a04f49d2 2019-02-04 stsp free(path_refs);
740 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
741 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
742 199a4027 2019-02-02 stsp return err;
746 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
748 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
750 e2e879a0 2019-03-11 stsp while (!SIMPLEQ_EMPTY(refs)) {
751 e2e879a0 2019-03-11 stsp re = SIMPLEQ_FIRST(refs);
752 e2e879a0 2019-03-11 stsp SIMPLEQ_REMOVE_HEAD(refs, entry);
753 e2e879a0 2019-03-11 stsp got_ref_close(re->ref);
754 e2e879a0 2019-03-11 stsp free(re->id);
760 9e672c74 2019-03-11 stsp const struct got_error *
761 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
763 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
764 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
765 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
766 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
767 9e672c74 2019-03-11 stsp FILE *f = NULL;
769 9e672c74 2019-03-11 stsp struct stat sb;
771 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
772 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
773 9e672c74 2019-03-11 stsp err = got_error_from_errno();
777 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
778 9e672c74 2019-03-11 stsp err = got_error_from_errno();
782 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
784 d1667f0d 2019-03-11 stsp char *parent;
785 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
787 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
790 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
791 5e1c9f23 2019-03-11 stsp free(parent);
794 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
799 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
800 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
801 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
802 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
806 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
807 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
808 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
809 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
812 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
813 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
814 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
819 9e672c74 2019-03-11 stsp err = got_lockfile_lock(&lf, path);
823 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
825 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
826 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
827 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
830 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
833 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
834 9e672c74 2019-03-11 stsp err = got_error_from_errno();
837 9e672c74 2019-03-11 stsp free(tmppath);
838 9e672c74 2019-03-11 stsp tmppath = NULL;
840 9e672c74 2019-03-11 stsp if (chmod(path, sb.st_mode) != 0) {
841 9e672c74 2019-03-11 stsp err = got_error_from_errno();
846 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
848 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
849 9e672c74 2019-03-11 stsp err = got_error_from_errno();
851 9e672c74 2019-03-11 stsp free(path_refs);
852 9e672c74 2019-03-11 stsp free(path);
853 9e672c74 2019-03-11 stsp if (tmppath) {
854 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
855 9e672c74 2019-03-11 stsp err = got_error_from_errno();
856 9e672c74 2019-03-11 stsp free(tmppath);
858 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
861 2d2e1378 2019-03-11 stsp const struct got_error *
862 2d2e1378 2019-03-11 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
864 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
865 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
866 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
867 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
869 2d2e1378 2019-03-11 stsp /* TODO: handle packed refs ! */
871 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
872 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
873 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
877 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
878 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
882 2d2e1378 2019-03-11 stsp err = got_lockfile_lock(&lf, path);
886 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
888 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
889 2d2e1378 2019-03-11 stsp err = got_error_from_errno();
892 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
894 2d2e1378 2019-03-11 stsp free(path_refs);
895 2d2e1378 2019-03-11 stsp free(path);
896 9e672c74 2019-03-11 stsp return err ? err : unlock_err;