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 56e0773d 2019-11-28 stsp #include <limits.h>
25 5261c201 2018-04-01 stsp #include <sha1.h>
26 5261c201 2018-04-01 stsp #include <stdio.h>
27 5261c201 2018-04-01 stsp #include <stdlib.h>
28 5261c201 2018-04-01 stsp #include <string.h>
29 81a12da5 2020-09-09 naddy #include <unistd.h>
30 5261c201 2018-04-01 stsp #include <util.h>
31 5261c201 2018-04-01 stsp #include <zlib.h>
32 788c352e 2018-06-16 stsp #include <time.h>
33 0cd1c46a 2019-03-11 stsp #include <libgen.h>
35 5261c201 2018-04-01 stsp #include "got_error.h"
36 5261c201 2018-04-01 stsp #include "got_object.h"
37 5261c201 2018-04-01 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
40 324d37e7 2019-05-11 stsp #include "got_path.h"
42 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
43 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 5261c201 2018-04-01 stsp #include "got_lib_object.h"
46 7b5b670e 2020-12-25 stsp #include "got_lib_object_idset.h"
47 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
49 fb79db15 2019-02-01 stsp #ifndef nitems
50 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
54 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
55 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
58 598a8b91 2019-03-15 stsp * We do not resolve tags yet, and don't yet care about sorting refs either,
59 598a8b91 2019-03-15 stsp * so packed-refs files we write contain a minimal header which disables all
60 598a8b91 2019-03-15 stsp * packed-refs "traits" supported by Git.
62 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 5261c201 2018-04-01 stsp /* A symbolic reference. */
65 5261c201 2018-04-01 stsp struct got_symref {
70 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
72 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
73 5261c201 2018-04-01 stsp struct got_ref {
75 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
79 5261c201 2018-04-01 stsp struct got_reference {
80 5261c201 2018-04-01 stsp unsigned int flags;
81 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
82 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
85 5261c201 2018-04-01 stsp struct got_ref ref;
86 5261c201 2018-04-01 stsp struct got_symref symref;
89 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
92 5261c201 2018-04-01 stsp static const struct got_error *
93 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
94 f9267c9a 2019-03-15 stsp struct got_object_id *id, int flags)
96 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
98 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
99 f9267c9a 2019-03-15 stsp if (*ref == NULL)
100 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
102 80d5fc1f 2019-03-15 stsp memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 c980e470 2019-03-15 stsp (*ref)->flags = flags;
104 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
105 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
106 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
107 c980e470 2019-03-15 stsp got_ref_close(*ref);
108 f9267c9a 2019-03-15 stsp *ref = NULL;
110 f9267c9a 2019-03-15 stsp return err;
113 f9267c9a 2019-03-15 stsp static const struct got_error *
114 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
115 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
117 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
119 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
120 5261c201 2018-04-01 stsp if (*ref == NULL)
121 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
123 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
124 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
125 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
127 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
128 f9267c9a 2019-03-15 stsp *ref = NULL;
129 cdb8f1fa 2019-08-28 hiltjo return err;
131 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
132 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
133 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
134 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
135 f9267c9a 2019-03-15 stsp *ref = NULL;
137 f9267c9a 2019-03-15 stsp return err;
140 5261c201 2018-04-01 stsp static const struct got_error *
141 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
143 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
144 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
146 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
149 f9267c9a 2019-03-15 stsp static const struct got_error *
150 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
152 5892cdd6 2019-03-10 stsp struct got_object_id id;
154 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
156 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
159 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
160 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
162 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, 0);
165 5261c201 2018-04-01 stsp static const struct got_error *
166 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
167 b2070a3f 2020-03-22 stsp const char *absname, const char *abspath, int lock)
169 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
171 c30018ad 2019-10-21 stsp char *line = NULL;
172 c30018ad 2019-10-21 stsp size_t linesize = 0;
173 c30018ad 2019-10-21 stsp ssize_t linelen;
174 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
176 2f17228e 2019-05-12 stsp if (lock) {
177 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, abspath);
179 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
180 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
181 9f142382 2020-03-21 stsp return err;
185 c0a1c016 2019-03-15 stsp f = fopen(abspath, "rb");
186 f5c58ad1 2019-05-12 stsp if (f == NULL) {
187 b2070a3f 2020-03-22 stsp if (errno != ENOTDIR && errno != ENOENT)
188 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("fopen", abspath);
190 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
192 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
193 b2070a3f 2020-03-22 stsp return err;
196 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
197 c30018ad 2019-10-21 stsp if (linelen == -1) {
198 c30018ad 2019-10-21 stsp if (feof(f))
199 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
201 b2070a3f 2020-03-22 stsp if (errno == EISDIR)
202 b2070a3f 2020-03-22 stsp err = got_error(GOT_ERR_NOT_REF);
203 b2070a3f 2020-03-22 stsp else if (ferror(f))
204 b2070a3f 2020-03-22 stsp err = got_ferror(f, GOT_ERR_IO);
206 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
209 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
212 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
213 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
217 b2070a3f 2020-03-22 stsp err = parse_ref_line(ref, absname, line);
218 f5c58ad1 2019-05-12 stsp if (lock) {
220 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
223 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
225 f5c58ad1 2019-05-12 stsp got_lockfile_unlock(lf);
229 5261c201 2018-04-01 stsp free(line);
230 2f17228e 2019-05-12 stsp if (fclose(f) != 0 && err == NULL) {
231 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
232 2f17228e 2019-05-12 stsp if (*ref) {
234 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
235 2f17228e 2019-05-12 stsp got_ref_close(*ref);
236 2f17228e 2019-05-12 stsp *ref = NULL;
239 5261c201 2018-04-01 stsp return err;
243 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
245 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
246 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
247 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
248 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
251 c5f754cc 2019-02-01 stsp static char *
252 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
254 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
255 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
257 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
261 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
263 7fa81f88 2020-02-21 stsp const char *s, *seg;
264 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
265 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
266 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
267 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
270 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
275 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
277 f77a24b0 2019-03-11 stsp while (*s) {
278 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
279 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
282 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
283 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
284 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
287 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
289 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
291 f77a24b0 2019-03-11 stsp if (*s == '/') {
292 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
293 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
294 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
296 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
297 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
299 f77a24b0 2019-03-11 stsp seg = nextseg;
304 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
305 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
311 5892cdd6 2019-03-10 stsp const struct got_error *
312 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
313 5892cdd6 2019-03-10 stsp struct got_object_id *id)
315 0f148cb7 2019-05-13 stsp if (!is_valid_ref_name(name))
316 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
318 0f148cb7 2019-05-13 stsp return alloc_ref(ref, name, id, 0);
321 aaf88317 2019-07-10 stsp const struct got_error *
322 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
323 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
325 aaf88317 2019-07-10 stsp if (!is_valid_ref_name(name))
326 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
331 d1f2edc9 2018-06-13 stsp static const struct got_error *
332 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
333 fb79db15 2019-02-01 stsp const char *line)
335 5892cdd6 2019-03-10 stsp struct got_object_id id;
336 5892cdd6 2019-03-10 stsp const char *name;
338 fb79db15 2019-02-01 stsp *ref = NULL;
340 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
341 fb79db15 2019-02-01 stsp return NULL;
343 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
344 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
346 199a4027 2019-02-02 stsp if (abs_refname) {
347 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
348 199a4027 2019-02-02 stsp return NULL;
349 5892cdd6 2019-03-10 stsp name = abs_refname;
351 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
353 f9267c9a 2019-03-15 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
356 fb79db15 2019-02-01 stsp static const struct got_error *
357 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
358 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
360 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
361 fb79db15 2019-02-01 stsp char *abs_refname;
362 fb79db15 2019-02-01 stsp char *line;
363 fb79db15 2019-02-01 stsp size_t len;
364 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
365 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
367 1e37702e 2019-02-04 stsp *ref = NULL;
369 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
370 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
372 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
373 7ab0422a 2019-03-15 stsp if (line == NULL) {
374 7ab0422a 2019-03-15 stsp if (feof(f))
376 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
379 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
380 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
381 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
382 0dec1cc0 2019-02-01 stsp refname) == -1)
383 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
384 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
385 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
386 0dec1cc0 2019-02-01 stsp free(abs_refname);
387 532920c8 2019-02-01 stsp if (err || *ref != NULL)
390 fb79db15 2019-02-01 stsp free(line);
393 fb79db15 2019-02-01 stsp } while (*ref == NULL);
395 fb79db15 2019-02-01 stsp return err;
398 fb79db15 2019-02-01 stsp static const struct got_error *
399 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
400 2f17228e 2019-05-12 stsp const char *name, int lock)
402 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
403 a04f49d2 2019-02-04 stsp char *path = NULL;
404 a04f49d2 2019-02-04 stsp char *absname = NULL;
405 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
406 75236079 2020-03-25 stsp int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
408 1e37702e 2019-02-04 stsp *ref = NULL;
410 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
411 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
412 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
413 a04f49d2 2019-02-04 stsp absname = (char *)name;
415 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
416 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
417 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
419 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
420 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
421 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
426 b2070a3f 2020-03-22 stsp err = parse_ref_file(ref, name, absname, path, lock);
428 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
429 a04f49d2 2019-02-04 stsp free(absname);
430 a04f49d2 2019-02-04 stsp free(path);
431 d1f2edc9 2018-06-13 stsp return err;
434 5261c201 2018-04-01 stsp const struct got_error *
435 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
436 2f17228e 2019-05-12 stsp const char *refname, int lock)
438 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
439 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
440 fb79db15 2019-02-01 stsp const char *subdirs[] = {
441 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
444 16aeacf7 2020-11-26 stsp int well_known = is_well_known_ref(refname);
445 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
447 1e37702e 2019-02-04 stsp *ref = NULL;
449 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
450 e135804e 2019-02-10 stsp if (path_refs == NULL) {
451 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
455 0885ce8f 2019-05-12 stsp if (well_known) {
456 0885ce8f 2019-05-12 stsp err = open_ref(ref, path_refs, "", refname, lock);
458 c5f754cc 2019-02-01 stsp char *packed_refs_path;
461 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
462 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
463 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
465 b2070a3f 2020-03-22 stsp if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
469 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
470 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
471 638f9024 2019-05-13 stsp err = got_error_from_errno(
472 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
476 2f17228e 2019-05-12 stsp if (lock) {
477 a875589a 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
481 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
482 c5f754cc 2019-02-01 stsp free(packed_refs_path);
483 c5f754cc 2019-02-01 stsp if (f != NULL) {
484 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
486 a875589a 2019-05-12 stsp if (!err) {
487 a875589a 2019-05-12 stsp if (fclose(f) != 0) {
488 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
489 a875589a 2019-05-12 stsp got_ref_close(*ref);
490 a875589a 2019-05-12 stsp *ref = NULL;
491 6e472abb 2019-05-13 stsp } else if (*ref)
492 a875589a 2019-05-12 stsp (*ref)->lf = lf;
497 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
498 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
499 a875589a 2019-05-12 stsp if (err && lf)
500 a875589a 2019-05-12 stsp got_lockfile_unlock(lf);
501 5261c201 2018-04-01 stsp free(path_refs);
502 5261c201 2018-04-01 stsp return err;
506 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
508 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
509 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
510 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
512 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
516 5261c201 2018-04-01 stsp struct got_reference *
517 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
519 5261c201 2018-04-01 stsp struct got_reference *ret;
521 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
522 5261c201 2018-04-01 stsp if (ret == NULL)
523 5261c201 2018-04-01 stsp return NULL;
525 5261c201 2018-04-01 stsp ret->flags = ref->flags;
526 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
527 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
528 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
530 5261c201 2018-04-01 stsp return NULL;
532 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
533 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
534 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
536 5261c201 2018-04-01 stsp return NULL;
539 4c4ce67b 2020-12-25 stsp ret->ref.ref.name = strdup(ref->ref.ref.name);
540 4c4ce67b 2020-12-25 stsp if (ret->ref.ref.name == NULL) {
542 5261c201 2018-04-01 stsp return NULL;
544 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
545 80d5fc1f 2019-03-15 stsp sizeof(ret->ref.ref.sha1));
548 5261c201 2018-04-01 stsp return ret;
551 b8bad2ba 2019-08-23 stsp const struct got_error *
552 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
553 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
555 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
556 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
558 b8bad2ba 2019-08-23 stsp *newp = NULL;
560 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
561 b8bad2ba 2019-08-23 stsp if (new == NULL)
562 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
564 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
565 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
566 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
568 b8bad2ba 2019-08-23 stsp return err;
571 b8bad2ba 2019-08-23 stsp *newp = new;
572 b8bad2ba 2019-08-23 stsp return NULL;
575 5261c201 2018-04-01 stsp static const struct got_error *
576 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
577 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
579 5261c201 2018-04-01 stsp struct got_reference *nextref;
580 5261c201 2018-04-01 stsp const struct got_error *err;
582 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
584 5261c201 2018-04-01 stsp return err;
586 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
587 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
589 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
591 5261c201 2018-04-01 stsp got_ref_close(nextref);
592 5261c201 2018-04-01 stsp return err;
595 29e86f7a 2019-08-12 stsp static const struct got_error *
596 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
597 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
599 5261c201 2018-04-01 stsp const struct got_error *err;
601 29e86f7a 2019-08-12 stsp if (recursion <= 0)
602 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
603 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
605 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
606 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
607 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
608 5261c201 2018-04-01 stsp if (err == NULL)
609 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
610 57b6f99a 2019-04-13 stsp if (resolved)
611 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
612 5261c201 2018-04-01 stsp return err;
615 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
616 5261c201 2018-04-01 stsp if (*id == NULL)
617 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
618 80d5fc1f 2019-03-15 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
619 5261c201 2018-04-01 stsp return NULL;
622 29e86f7a 2019-08-12 stsp const struct got_error *
623 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
624 29e86f7a 2019-08-12 stsp struct got_reference *ref)
626 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
630 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
634 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
635 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
637 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
638 271d2a38 2018-12-25 stsp if (str == NULL)
639 271d2a38 2018-12-25 stsp return NULL;
641 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
642 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
644 271d2a38 2018-12-25 stsp return NULL;
647 5261c201 2018-04-01 stsp return str;
650 0bd18d37 2019-02-01 stsp const char *
651 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
653 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
656 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
659 aaf88317 2019-07-10 stsp const char *
660 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
662 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
663 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
665 b8bad2ba 2019-08-23 stsp return NULL;
668 b8bad2ba 2019-08-23 stsp const struct got_error *
669 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
670 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
672 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
673 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
675 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
676 aaf88317 2019-07-10 stsp return NULL;
679 d1f16636 2020-01-15 stsp const struct got_error *
680 d1f16636 2020-01-15 stsp got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
681 d1f16636 2020-01-15 stsp struct got_reference *ref2)
683 d1f16636 2020-01-15 stsp const struct got_error *err = NULL;
684 d1f16636 2020-01-15 stsp struct got_repository *repo = arg;
685 d1f16636 2020-01-15 stsp struct got_object_id *id1, *id2 = NULL;
686 d1f16636 2020-01-15 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
687 d1f16636 2020-01-15 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
688 d1f16636 2020-01-15 stsp time_t time1, time2;
692 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id1, repo, ref1);
694 d1f16636 2020-01-15 stsp return err;
695 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag1, repo, id1);
697 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
699 d1f16636 2020-01-15 stsp /* "lightweight" tag */
700 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit1, repo, id1);
703 d1f16636 2020-01-15 stsp time1 = got_object_commit_get_committer_time(commit1);
705 d1f16636 2020-01-15 stsp time1 = got_object_tag_get_tagger_time(tag1);
707 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id2, repo, ref2);
710 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag2, repo, id2);
712 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
714 d1f16636 2020-01-15 stsp /* "lightweight" tag */
715 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit2, repo, id2);
718 d1f16636 2020-01-15 stsp time2 = got_object_commit_get_committer_time(commit2);
720 d1f16636 2020-01-15 stsp time2 = got_object_tag_get_tagger_time(tag2);
722 d1f16636 2020-01-15 stsp /* Put latest tags first. */
723 d1f16636 2020-01-15 stsp if (time1 < time2)
725 d1f16636 2020-01-15 stsp else if (time1 > time2)
728 d1f16636 2020-01-15 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
733 d1f16636 2020-01-15 stsp got_object_tag_close(tag1);
735 d1f16636 2020-01-15 stsp got_object_tag_close(tag2);
736 d1f16636 2020-01-15 stsp if (commit1)
737 d1f16636 2020-01-15 stsp got_object_commit_close(commit1);
738 d1f16636 2020-01-15 stsp if (commit2)
739 d1f16636 2020-01-15 stsp got_object_commit_close(commit2);
740 d1f16636 2020-01-15 stsp return err;
743 199a4027 2019-02-02 stsp static const struct got_error *
744 505287be 2019-03-15 stsp insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
745 b8bad2ba 2019-08-23 stsp struct got_reference *ref, struct got_repository *repo,
746 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
748 199a4027 2019-02-02 stsp const struct got_error *err;
749 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
752 505287be 2019-03-15 stsp *newp = NULL;
754 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
755 48cae60d 2020-09-22 stsp if (new == NULL)
756 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
757 7a3c76f5 2019-02-05 stsp new->ref = ref;
758 505287be 2019-03-15 stsp *newp = new;
761 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
762 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
763 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
764 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
766 d9dff0e5 2020-12-26 stsp * Many callers will provide paths in a somewhat sorted order.
767 d9dff0e5 2020-12-26 stsp * Iterating backwards from the tail of the list should be more
768 d9dff0e5 2020-12-26 stsp * efficient than traversing through the entire list each time
769 d9dff0e5 2020-12-26 stsp * an element is inserted.
771 d9dff0e5 2020-12-26 stsp re = TAILQ_LAST(refs, got_reflist_head);
772 7a3c76f5 2019-02-05 stsp while (re) {
773 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
775 b8bad2ba 2019-08-23 stsp return err;
776 e397b6db 2019-02-04 stsp if (cmp == 0) {
777 27a1ed03 2019-03-15 stsp /* duplicate */
779 505287be 2019-03-15 stsp *newp = NULL;
780 7a3c76f5 2019-02-05 stsp return NULL;
781 d9dff0e5 2020-12-26 stsp } else if (cmp < 0) {
782 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_AFTER(refs, re, new, entry);
783 7a3c76f5 2019-02-05 stsp return NULL;
785 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
788 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
789 199a4027 2019-02-02 stsp return NULL;
792 a04f49d2 2019-02-04 stsp static const struct got_error *
793 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
794 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
795 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
798 a04f49d2 2019-02-04 stsp DIR *d = NULL;
799 a04f49d2 2019-02-04 stsp char *path_subdir;
801 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
804 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
805 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
807 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
808 a04f49d2 2019-02-04 stsp if (d == NULL)
812 a04f49d2 2019-02-04 stsp struct dirent *dent;
813 a04f49d2 2019-02-04 stsp struct got_reference *ref;
814 a04f49d2 2019-02-04 stsp char *child;
817 a04f49d2 2019-02-04 stsp dent = readdir(d);
818 a04f49d2 2019-02-04 stsp if (dent == NULL)
821 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
822 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
825 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
829 20ccae39 2020-07-21 stsp switch (type) {
830 a04f49d2 2019-02-04 stsp case DT_REG:
831 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
836 505287be 2019-03-15 stsp struct got_reflist_entry *new;
837 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
838 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
839 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
840 505287be 2019-03-15 stsp got_ref_close(ref);
845 a04f49d2 2019-02-04 stsp case DT_DIR:
846 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
847 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
848 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
851 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
852 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
853 a04f49d2 2019-02-04 stsp free(child);
861 a04f49d2 2019-02-04 stsp closedir(d);
862 a04f49d2 2019-02-04 stsp free(path_subdir);
863 a04f49d2 2019-02-04 stsp return err;
866 199a4027 2019-02-02 stsp const struct got_error *
867 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
868 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
870 199a4027 2019-02-02 stsp const struct got_error *err;
871 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
872 b2070a3f 2020-03-22 stsp char *abs_namespace = NULL;
873 b2070a3f 2020-03-22 stsp char *buf = NULL, *ondisk_ref_namespace = NULL;
874 29b5c214 2019-02-04 stsp FILE *f = NULL;
875 199a4027 2019-02-02 stsp struct got_reference *ref;
876 505287be 2019-03-15 stsp struct got_reflist_entry *new;
878 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
879 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
880 29606af7 2019-08-23 stsp if (path_refs == NULL) {
881 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
884 29606af7 2019-08-23 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
887 f68a7890 2020-03-19 stsp err = insert_ref(&new, refs, ref, repo,
888 f68a7890 2020-03-19 stsp cmp_cb, cmp_arg);
889 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
890 29606af7 2019-08-23 stsp got_ref_close(ref);
891 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
894 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
895 b2070a3f 2020-03-22 stsp const char *refname = ref_namespace;
896 b2070a3f 2020-03-22 stsp path_refs = get_refs_dir_path(repo, refname);
897 b2070a3f 2020-03-22 stsp if (path_refs == NULL) {
898 b2070a3f 2020-03-22 stsp err = got_error_from_errno("get_refs_dir_path");
901 b2070a3f 2020-03-22 stsp err = open_ref(&ref, path_refs, "", refname, 0);
903 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
905 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
907 b2070a3f 2020-03-22 stsp err = insert_ref(&new, refs, ref, repo,
908 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
909 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
910 b2070a3f 2020-03-22 stsp got_ref_close(ref);
911 b2070a3f 2020-03-22 stsp return err;
915 b2070a3f 2020-03-22 stsp if (ref_namespace) {
916 b2070a3f 2020-03-22 stsp size_t len;
917 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
918 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
919 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
922 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
923 b2070a3f 2020-03-22 stsp buf = malloc(len);
924 b2070a3f 2020-03-22 stsp if (buf == NULL) {
925 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
928 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
931 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
932 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
933 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
934 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
935 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
936 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
937 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
940 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
941 29b5c214 2019-02-04 stsp free(path_refs);
942 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
943 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
944 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
947 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
948 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
949 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
954 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
955 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
957 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
958 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
963 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
964 199a4027 2019-02-02 stsp free(packed_refs_path);
966 199a4027 2019-02-02 stsp char *line;
967 199a4027 2019-02-02 stsp size_t len;
968 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
970 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
971 0bb4abae 2019-03-15 stsp if (line == NULL) {
972 0bb4abae 2019-03-15 stsp if (feof(f))
974 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
977 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
978 0bb4abae 2019-03-15 stsp free(line);
982 29606af7 2019-08-23 stsp if (ref_namespace) {
983 29606af7 2019-08-23 stsp const char *name;
984 29606af7 2019-08-23 stsp name = got_ref_get_name(ref);
985 b2070a3f 2020-03-22 stsp if (!got_path_is_child(name,
986 b2070a3f 2020-03-22 stsp ref_namespace,
987 b2070a3f 2020-03-22 stsp strlen(ref_namespace))) {
988 29606af7 2019-08-23 stsp got_ref_close(ref);
992 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, refs, ref, repo,
993 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
994 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
995 505287be 2019-03-15 stsp got_ref_close(ref);
1002 b2070a3f 2020-03-22 stsp free(abs_namespace);
1003 b2070a3f 2020-03-22 stsp free(buf);
1004 a04f49d2 2019-02-04 stsp free(path_refs);
1005 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
1006 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1007 199a4027 2019-02-02 stsp return err;
1011 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1013 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1015 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1016 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1023 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1025 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1028 b249b824 2019-05-09 stsp const struct got_error *
1029 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1031 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1032 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1034 b249b824 2019-05-09 stsp memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1035 b249b824 2019-05-09 stsp return NULL;
1038 9e672c74 2019-03-11 stsp const struct got_error *
1039 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1041 b249b824 2019-05-09 stsp char *new_name;
1043 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1044 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1046 b249b824 2019-05-09 stsp new_name = strdup(refname);
1047 b249b824 2019-05-09 stsp if (new_name == NULL)
1048 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1050 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1051 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1052 b249b824 2019-05-09 stsp return NULL;
1055 b249b824 2019-05-09 stsp const struct got_error *
1056 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1057 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1059 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1060 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1062 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1063 e8a967e0 2020-03-21 stsp memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1064 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1065 e8a967e0 2020-03-21 stsp return NULL;
1068 e8a967e0 2020-03-21 stsp const struct got_error *
1069 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1071 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1072 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1073 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1074 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1075 9e672c74 2019-03-11 stsp FILE *f = NULL;
1077 9e672c74 2019-03-11 stsp struct stat sb;
1079 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1080 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1081 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1082 9e672c74 2019-03-11 stsp goto done;
1085 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1086 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1087 9e672c74 2019-03-11 stsp goto done;
1090 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1091 49c7094f 2019-03-11 stsp if (err) {
1092 d1667f0d 2019-03-11 stsp char *parent;
1093 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 5e1c9f23 2019-03-11 stsp goto done;
1095 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1097 0cd1c46a 2019-03-11 stsp goto done;
1098 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1099 5e1c9f23 2019-03-11 stsp free(parent);
1101 0cd1c46a 2019-03-11 stsp goto done;
1102 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1104 0cd1c46a 2019-03-11 stsp goto done;
1107 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1108 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1109 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1110 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1111 9e672c74 2019-03-11 stsp goto done;
1114 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1115 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1116 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
1117 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1118 9e672c74 2019-03-11 stsp goto done;
1120 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1121 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
1122 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1123 9e672c74 2019-03-11 stsp goto done;
1127 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1128 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1130 2f17228e 2019-05-12 stsp goto done;
1133 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1135 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1136 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1137 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1138 0cd1c46a 2019-03-11 stsp goto done;
1140 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1143 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1144 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1145 3818e3c4 2020-11-01 naddy goto done;
1148 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1149 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1150 9e672c74 2019-03-11 stsp goto done;
1152 9e672c74 2019-03-11 stsp free(tmppath);
1153 9e672c74 2019-03-11 stsp tmppath = NULL;
1155 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1156 9e672c74 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1158 9e672c74 2019-03-11 stsp if (fclose(f) != 0 && err == NULL)
1159 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1161 9e672c74 2019-03-11 stsp free(path_refs);
1162 9e672c74 2019-03-11 stsp free(path);
1163 9e672c74 2019-03-11 stsp if (tmppath) {
1164 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
1165 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1166 9e672c74 2019-03-11 stsp free(tmppath);
1168 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1171 598a8b91 2019-03-15 stsp static const struct got_error *
1172 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1174 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1175 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1176 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1177 598a8b91 2019-03-15 stsp char *packed_refs_path, *tmppath = NULL;
1178 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1179 598a8b91 2019-03-15 stsp int found_delref = 0;
1181 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1182 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1183 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1185 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1187 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1188 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1189 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1191 598a8b91 2019-03-15 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1193 598a8b91 2019-03-15 stsp goto done;
1195 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1196 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, packed_refs_path);
1198 2f17228e 2019-05-12 stsp goto done;
1201 598a8b91 2019-03-15 stsp f = fopen(packed_refs_path, "r");
1202 598a8b91 2019-03-15 stsp if (f == NULL) {
1203 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1204 598a8b91 2019-03-15 stsp goto done;
1207 598a8b91 2019-03-15 stsp char *line;
1208 598a8b91 2019-03-15 stsp size_t len;
1209 598a8b91 2019-03-15 stsp const char delim[3] = {'\0', '\0', '\0'};
1210 598a8b91 2019-03-15 stsp struct got_reference *ref;
1211 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1213 598a8b91 2019-03-15 stsp line = fparseln(f, &len, NULL, delim, 0);
1214 598a8b91 2019-03-15 stsp if (line == NULL) {
1215 598a8b91 2019-03-15 stsp if (feof(f))
1217 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1218 598a8b91 2019-03-15 stsp goto done;
1220 598a8b91 2019-03-15 stsp err = parse_packed_ref_line(&ref, NULL, line);
1221 598a8b91 2019-03-15 stsp free(line);
1223 598a8b91 2019-03-15 stsp goto done;
1224 598a8b91 2019-03-15 stsp if (ref == NULL)
1227 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1228 598a8b91 2019-03-15 stsp memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1229 598a8b91 2019-03-15 stsp sizeof(delref->ref.ref.sha1)) == 0) {
1230 598a8b91 2019-03-15 stsp found_delref = 1;
1231 598a8b91 2019-03-15 stsp got_ref_close(ref);
1235 b8bad2ba 2019-08-23 stsp err = insert_ref(&new, &refs, ref, repo,
1236 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1237 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1238 598a8b91 2019-03-15 stsp got_ref_close(ref);
1240 598a8b91 2019-03-15 stsp goto done;
1243 598a8b91 2019-03-15 stsp if (found_delref) {
1244 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1246 598a8b91 2019-03-15 stsp struct stat sb;
1248 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1249 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1250 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1251 598a8b91 2019-03-15 stsp goto done;
1254 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1255 598a8b91 2019-03-15 stsp uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1257 598a8b91 2019-03-15 stsp if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1258 598a8b91 2019-03-15 stsp sizeof(hex)) == NULL) {
1259 598a8b91 2019-03-15 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1260 598a8b91 2019-03-15 stsp goto done;
1262 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1263 598a8b91 2019-03-15 stsp if (n != sizeof(hex)) {
1264 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1265 598a8b91 2019-03-15 stsp goto done;
1267 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1268 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1269 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1270 598a8b91 2019-03-15 stsp goto done;
1274 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1275 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1276 598a8b91 2019-03-15 stsp goto done;
1279 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1280 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1281 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1282 230a42bd 2019-05-11 jcs packed_refs_path);
1283 598a8b91 2019-03-15 stsp goto done;
1285 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1288 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1289 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1290 598a8b91 2019-03-15 stsp goto done;
1293 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1294 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1295 230a42bd 2019-05-11 jcs packed_refs_path);
1296 598a8b91 2019-03-15 stsp goto done;
1300 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1301 598a8b91 2019-03-15 stsp unlock_err = got_lockfile_unlock(lf);
1303 598a8b91 2019-03-15 stsp if (fclose(f) != 0 && err == NULL)
1304 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1306 598a8b91 2019-03-15 stsp if (tmpf) {
1307 598a8b91 2019-03-15 stsp unlink(tmppath);
1308 598a8b91 2019-03-15 stsp if (fclose(tmpf) != 0 && err == NULL)
1309 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1311 598a8b91 2019-03-15 stsp free(tmppath);
1312 598a8b91 2019-03-15 stsp free(packed_refs_path);
1313 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1314 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1317 2a104ff6 2020-09-21 stsp static const struct got_error *
1318 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1320 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1321 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1322 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1323 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1325 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1326 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1327 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1328 2d2e1378 2019-03-11 stsp goto done;
1331 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1332 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1333 2d2e1378 2019-03-11 stsp goto done;
1336 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1337 2f17228e 2019-05-12 stsp err = got_lockfile_lock(&lf, path);
1339 2f17228e 2019-05-12 stsp goto done;
1342 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1344 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
1345 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1347 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1348 2d2e1378 2019-03-11 stsp unlock_err = got_lockfile_unlock(lf);
1350 2d2e1378 2019-03-11 stsp free(path_refs);
1351 2d2e1378 2019-03-11 stsp free(path);
1352 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1355 2a104ff6 2020-09-21 stsp const struct got_error *
1356 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1358 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1359 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1361 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1362 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1364 2a104ff6 2020-09-21 stsp return err;
1366 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1367 2a104ff6 2020-09-21 stsp if (err) {
1368 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1369 2a104ff6 2020-09-21 stsp return NULL;
1370 2a104ff6 2020-09-21 stsp return err;
1373 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1374 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1375 2a104ff6 2020-09-21 stsp return err;
1377 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1379 2a104ff6 2020-09-21 stsp return err;
1381 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1382 2a104ff6 2020-09-21 stsp if (err) {
1383 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1384 2a104ff6 2020-09-21 stsp return NULL;
1385 2a104ff6 2020-09-21 stsp return err;
1388 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1389 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1390 2a104ff6 2020-09-21 stsp return err;
1394 2f17228e 2019-05-12 stsp const struct got_error *
1395 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1397 2f17228e 2019-05-12 stsp const struct got_error *err;
1398 2f17228e 2019-05-12 stsp err = got_lockfile_unlock(ref->lf);
1399 2f17228e 2019-05-12 stsp ref->lf = NULL;
1400 2f17228e 2019-05-12 stsp return err;
1403 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1404 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1407 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1408 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1411 24202e46 2020-12-26 stsp static const struct got_error *
1412 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1413 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1415 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1416 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1417 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1419 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1420 24202e46 2020-12-26 stsp if (ent == NULL) {
1421 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1422 24202e46 2020-12-26 stsp if (ent == NULL)
1423 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1425 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1426 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1428 24202e46 2020-12-26 stsp return err;
1431 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1433 24202e46 2020-12-26 stsp return err;
1435 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1436 24202e46 2020-12-26 stsp return NULL;
1439 7b5b670e 2020-12-25 stsp const struct got_error *
1440 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1441 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1443 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1444 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1445 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1446 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1448 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1449 7b5b670e 2020-12-25 stsp if (idset == NULL)
1450 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1452 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1453 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1454 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1455 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1457 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1459 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1460 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1462 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1464 7b5b670e 2020-12-25 stsp goto done;
1466 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1468 24202e46 2020-12-26 stsp goto done;
1470 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1472 24202e46 2020-12-26 stsp id = NULL;
1476 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1477 24202e46 2020-12-26 stsp if (err) {
1478 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1479 7b5b670e 2020-12-25 stsp goto done;
1480 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1481 24202e46 2020-12-26 stsp err = NULL;
1482 24202e46 2020-12-26 stsp tag = NULL;
1484 24202e46 2020-12-26 stsp id = NULL;
1488 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1489 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1491 7b5b670e 2020-12-25 stsp goto done;
1494 7b5b670e 2020-12-25 stsp id = NULL;
1498 7b5b670e 2020-12-25 stsp if (err) {
1499 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1500 7b5b670e 2020-12-25 stsp *map = NULL;
1502 a53af95f 2020-12-26 stsp return err;
1505 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1506 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1507 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1509 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1510 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1512 7b5b670e 2020-12-25 stsp return &ent->refs;
1513 7b5b670e 2020-12-25 stsp return NULL;
1516 7b5b670e 2020-12-25 stsp static const struct got_error *
1517 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1519 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1521 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1522 7b5b670e 2020-12-25 stsp free(ent);
1523 7b5b670e 2020-12-25 stsp return NULL;
1527 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1529 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1530 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1531 7b5b670e 2020-12-25 stsp free(map);