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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 5261c201 2018-04-01 stsp #include <sys/types.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 9e672c74 2019-03-11 stsp #include <sys/stat.h>
23 0fd469ce 2019-03-11 stsp #include <errno.h>
24 a04f49d2 2019-02-04 stsp #include <dirent.h>
25 56e0773d 2019-11-28 stsp #include <limits.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 <zlib.h>
31 788c352e 2018-06-16 stsp #include <time.h>
32 0cd1c46a 2019-03-11 stsp #include <libgen.h>
34 5261c201 2018-04-01 stsp #include "got_error.h"
35 5261c201 2018-04-01 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
39 324d37e7 2019-05-11 stsp #include "got_path.h"
41 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
42 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 5261c201 2018-04-01 stsp #include "got_lib_object.h"
45 7b5b670e 2020-12-25 stsp #include "got_lib_object_idset.h"
46 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
48 fb79db15 2019-02-01 stsp #ifndef nitems
49 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
53 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
54 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
57 598a8b91 2019-03-15 stsp * We do not resolve tags yet, and don't yet care about sorting refs either,
58 598a8b91 2019-03-15 stsp * so packed-refs files we write contain a minimal header which disables all
59 598a8b91 2019-03-15 stsp * packed-refs "traits" supported by Git.
61 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 5261c201 2018-04-01 stsp /* A symbolic reference. */
64 5261c201 2018-04-01 stsp struct got_symref {
69 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
71 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
72 5261c201 2018-04-01 stsp struct got_ref {
74 66107227 2023-01-31 thomas struct got_object_id id;
77 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
78 5261c201 2018-04-01 stsp struct got_reference {
79 5261c201 2018-04-01 stsp unsigned int flags;
80 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
81 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
84 5261c201 2018-04-01 stsp struct got_ref ref;
85 5261c201 2018-04-01 stsp struct got_symref symref;
88 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
89 3f338f0a 2021-07-27 stsp time_t mtime;
92 fd77650a 2024-03-19 thomas * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
93 fd77650a 2024-03-19 thomas * and got_ref_cmp_tags().
95 fd3a2fe6 2021-10-12 thomas time_t committer_time;
98 5261c201 2018-04-01 stsp static const struct got_error *
99 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
100 3f338f0a 2021-07-27 stsp struct got_object_id *id, int flags, time_t mtime)
102 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
104 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
105 f9267c9a 2019-03-15 stsp if (*ref == NULL)
106 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
108 66107227 2023-01-31 thomas memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
109 c980e470 2019-03-15 stsp (*ref)->flags = flags;
110 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
111 3f338f0a 2021-07-27 stsp (*ref)->mtime = mtime;
112 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
114 c980e470 2019-03-15 stsp got_ref_close(*ref);
115 f9267c9a 2019-03-15 stsp *ref = NULL;
117 f9267c9a 2019-03-15 stsp return err;
120 f9267c9a 2019-03-15 stsp static const struct got_error *
121 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
122 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
124 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
126 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
127 5261c201 2018-04-01 stsp if (*ref == NULL)
128 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
130 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
131 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
132 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == 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;
136 cdb8f1fa 2019-08-28 hiltjo return err;
138 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
139 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
140 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
141 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
142 f9267c9a 2019-03-15 stsp *ref = NULL;
144 f9267c9a 2019-03-15 stsp return err;
147 5261c201 2018-04-01 stsp static const struct got_error *
148 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
150 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
151 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
153 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
156 f9267c9a 2019-03-15 stsp static const struct got_error *
157 3f338f0a 2021-07-27 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 f4204d57 2023-03-01 thomas time_t mtime, enum got_hash_algorithm algo)
160 5892cdd6 2019-03-10 stsp struct got_object_id id;
162 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
164 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
167 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id, line, algo))
168 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
170 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, 0, mtime);
173 5261c201 2018-04-01 stsp static const struct got_error *
174 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
175 f4204d57 2023-03-01 thomas const char *absname, const char *abspath, int lock,
176 f4204d57 2023-03-01 thomas enum got_hash_algorithm algo)
178 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
180 c30018ad 2019-10-21 stsp char *line = NULL;
181 c30018ad 2019-10-21 stsp size_t linesize = 0;
182 c30018ad 2019-10-21 stsp ssize_t linelen;
183 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
184 3f338f0a 2021-07-27 stsp struct stat sb;
186 2f17228e 2019-05-12 stsp if (lock) {
187 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, abspath, -1);
189 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
190 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
191 9f142382 2020-03-21 stsp return err;
195 c56c5d8a 2021-12-31 thomas f = fopen(abspath, "rbe");
196 f5c58ad1 2019-05-12 stsp if (f == NULL) {
197 b2070a3f 2020-03-22 stsp if (errno != ENOTDIR && errno != ENOENT)
198 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("fopen", abspath);
200 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
202 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
203 b2070a3f 2020-03-22 stsp return err;
205 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
206 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", abspath);
210 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
211 c30018ad 2019-10-21 stsp if (linelen == -1) {
212 c30018ad 2019-10-21 stsp if (feof(f))
213 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
215 b2070a3f 2020-03-22 stsp if (errno == EISDIR)
216 b2070a3f 2020-03-22 stsp err = got_error(GOT_ERR_NOT_REF);
217 b2070a3f 2020-03-22 stsp else if (ferror(f))
218 b2070a3f 2020-03-22 stsp err = got_ferror(f, GOT_ERR_IO);
220 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
223 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
226 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
227 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
231 f4204d57 2023-03-01 thomas err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
232 f5c58ad1 2019-05-12 stsp if (lock) {
234 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
237 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
239 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
243 5261c201 2018-04-01 stsp free(line);
244 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
246 2f17228e 2019-05-12 stsp if (*ref) {
248 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
249 2f17228e 2019-05-12 stsp got_ref_close(*ref);
250 2f17228e 2019-05-12 stsp *ref = NULL;
253 5261c201 2018-04-01 stsp return err;
257 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
259 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
260 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
261 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
262 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
265 c5f754cc 2019-02-01 stsp static char *
266 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
268 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
269 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
271 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
274 5892cdd6 2019-03-10 stsp const struct got_error *
275 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
276 5892cdd6 2019-03-10 stsp struct got_object_id *id)
278 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
279 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
281 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, id, 0, 0);
284 aaf88317 2019-07-10 stsp const struct got_error *
285 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
286 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
288 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
289 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
291 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
294 d1f2edc9 2018-06-13 stsp static const struct got_error *
295 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
296 f4204d57 2023-03-01 thomas const char *line, time_t mtime, enum got_hash_algorithm algo)
298 5892cdd6 2019-03-10 stsp struct got_object_id id;
299 5892cdd6 2019-03-10 stsp const char *name;
301 fb79db15 2019-02-01 stsp *ref = NULL;
303 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
304 fb79db15 2019-02-01 stsp return NULL;
306 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&id, line, algo))
307 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
309 199a4027 2019-02-02 stsp if (abs_refname) {
310 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
311 199a4027 2019-02-02 stsp return NULL;
312 5892cdd6 2019-03-10 stsp name = abs_refname;
314 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
316 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
319 fb79db15 2019-02-01 stsp static const struct got_error *
320 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
321 f4204d57 2023-03-01 thomas int nsubdirs, const char *refname, time_t mtime,
322 f4204d57 2023-03-01 thomas enum got_hash_algorithm algo)
324 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
325 fb79db15 2019-02-01 stsp char *abs_refname;
326 9bdd68dd 2020-01-02 naddy char *line = NULL;
327 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
328 9bdd68dd 2020-01-02 naddy ssize_t linelen;
329 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
331 1e37702e 2019-02-04 stsp *ref = NULL;
333 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
334 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
336 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
337 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
338 7ab0422a 2019-03-15 stsp if (feof(f))
340 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
343 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
344 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
345 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
346 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
347 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
348 0dec1cc0 2019-02-01 stsp refname) == -1)
349 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
350 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(ref, abs_refname, line,
351 f4204d57 2023-03-01 thomas mtime, algo);
352 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
353 0dec1cc0 2019-02-01 stsp free(abs_refname);
354 532920c8 2019-02-01 stsp if (err || *ref != NULL)
359 fb79db15 2019-02-01 stsp } while (*ref == NULL);
360 9bdd68dd 2020-01-02 naddy free(line);
362 fb79db15 2019-02-01 stsp return err;
365 fb79db15 2019-02-01 stsp static const struct got_error *
366 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
367 f4204d57 2023-03-01 thomas const char *name, int lock, enum got_hash_algorithm algo)
369 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
370 a04f49d2 2019-02-04 stsp char *path = NULL;
371 a04f49d2 2019-02-04 stsp char *absname = NULL;
372 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
373 75236079 2020-03-25 stsp int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
375 1e37702e 2019-02-04 stsp *ref = NULL;
377 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
378 57c18198 2021-05-24 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
380 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
381 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
382 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
383 a04f49d2 2019-02-04 stsp absname = (char *)name;
385 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
386 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
387 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
389 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
390 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
391 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
396 f4204d57 2023-03-01 thomas err = parse_ref_file(ref, name, absname, path, lock, algo);
398 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
399 a04f49d2 2019-02-04 stsp free(absname);
400 a04f49d2 2019-02-04 stsp free(path);
401 d1f2edc9 2018-06-13 stsp return err;
404 5261c201 2018-04-01 stsp const struct got_error *
405 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
406 abc59930 2021-09-05 naddy const char *refname, int lock)
408 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
409 3bfa5a06 2022-07-21 thomas char *packed_refs_path = NULL, *path_refs = NULL;
410 fb79db15 2019-02-01 stsp const char *subdirs[] = {
411 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
414 16aeacf7 2020-11-26 stsp int well_known = is_well_known_ref(refname);
415 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
417 1e37702e 2019-02-04 stsp *ref = NULL;
419 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
420 e135804e 2019-02-10 stsp if (path_refs == NULL) {
421 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
425 0885ce8f 2019-05-12 stsp if (well_known) {
426 f4204d57 2023-03-01 thomas err = open_ref(ref, path_refs, "", refname, lock,
427 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
431 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
432 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
433 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
434 f4204d57 2023-03-01 thomas lock, got_repo_get_object_format(repo));
435 b2070a3f 2020-03-22 stsp if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
439 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
440 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
441 638f9024 2019-05-13 stsp err = got_error_from_errno(
442 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
446 2f17228e 2019-05-12 stsp if (lock) {
447 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
451 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "rbe");
452 c5f754cc 2019-02-01 stsp if (f != NULL) {
453 3f338f0a 2021-07-27 stsp struct stat sb;
454 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
455 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat",
456 3f338f0a 2021-07-27 stsp packed_refs_path);
459 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
460 f4204d57 2023-03-01 thomas refname, sb.st_mtime,
461 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
462 a875589a 2019-05-12 stsp if (!err) {
463 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
464 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
465 a875589a 2019-05-12 stsp got_ref_close(*ref);
466 a875589a 2019-05-12 stsp *ref = NULL;
467 6e472abb 2019-05-13 stsp } else if (*ref)
468 a875589a 2019-05-12 stsp (*ref)->lf = lf;
473 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
474 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
475 a875589a 2019-05-12 stsp if (err && lf)
476 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
477 3bfa5a06 2022-07-21 thomas free(packed_refs_path);
478 5261c201 2018-04-01 stsp free(path_refs);
479 5261c201 2018-04-01 stsp return err;
483 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
485 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
487 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
489 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
493 5261c201 2018-04-01 stsp struct got_reference *
494 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
496 5261c201 2018-04-01 stsp struct got_reference *ret;
498 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
499 5261c201 2018-04-01 stsp if (ret == NULL)
500 5261c201 2018-04-01 stsp return NULL;
502 5261c201 2018-04-01 stsp ret->flags = ref->flags;
503 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
505 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
507 5261c201 2018-04-01 stsp return NULL;
509 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
511 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
513 5261c201 2018-04-01 stsp return NULL;
516 4c4ce67b 2020-12-25 stsp ret->ref.ref.name = strdup(ref->ref.ref.name);
517 4c4ce67b 2020-12-25 stsp if (ret->ref.ref.name == NULL) {
519 5261c201 2018-04-01 stsp return NULL;
521 66107227 2023-01-31 thomas memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
522 66107227 2023-01-31 thomas sizeof(ret->ref.ref.id));
525 5261c201 2018-04-01 stsp return ret;
528 b8bad2ba 2019-08-23 stsp const struct got_error *
529 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
530 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
532 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
533 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
535 b8bad2ba 2019-08-23 stsp *newp = NULL;
537 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
538 b8bad2ba 2019-08-23 stsp if (new == NULL)
539 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
541 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
542 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
543 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
545 b8bad2ba 2019-08-23 stsp return err;
548 b8bad2ba 2019-08-23 stsp *newp = new;
549 b8bad2ba 2019-08-23 stsp return NULL;
552 cce2f485 2021-08-22 stsp const struct got_error *
553 cce2f485 2021-08-22 stsp got_ref_resolve_symbolic(struct got_reference **resolved,
554 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
556 5261c201 2018-04-01 stsp struct got_reference *nextref;
557 5261c201 2018-04-01 stsp const struct got_error *err;
559 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
561 5261c201 2018-04-01 stsp return err;
563 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
564 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(resolved, repo, nextref);
566 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
568 5261c201 2018-04-01 stsp got_ref_close(nextref);
569 5261c201 2018-04-01 stsp return err;
572 29e86f7a 2019-08-12 stsp static const struct got_error *
573 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
574 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
576 5261c201 2018-04-01 stsp const struct got_error *err;
578 29e86f7a 2019-08-12 stsp if (recursion <= 0)
579 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
580 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
582 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
583 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
584 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(&resolved, repo, ref);
585 5261c201 2018-04-01 stsp if (err == NULL)
586 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
587 57b6f99a 2019-04-13 stsp if (resolved)
588 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
589 5261c201 2018-04-01 stsp return err;
592 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
593 5261c201 2018-04-01 stsp if (*id == NULL)
594 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
595 66107227 2023-01-31 thomas memcpy(*id, &ref->ref.ref.id, sizeof(**id));
596 5261c201 2018-04-01 stsp return NULL;
599 29e86f7a 2019-08-12 stsp const struct got_error *
600 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
601 29e86f7a 2019-08-12 stsp struct got_reference *ref)
603 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
607 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
611 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
612 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
614 66107227 2023-01-31 thomas if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
615 271d2a38 2018-12-25 stsp return NULL;
617 5261c201 2018-04-01 stsp return str;
620 0bd18d37 2019-02-01 stsp const char *
621 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
623 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
626 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
629 aaf88317 2019-07-10 stsp const char *
630 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
632 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
635 b8bad2ba 2019-08-23 stsp return NULL;
639 3f338f0a 2021-07-27 stsp got_ref_get_mtime(struct got_reference *ref)
641 3f338f0a 2021-07-27 stsp return ref->mtime;
644 b8bad2ba 2019-08-23 stsp const struct got_error *
645 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
646 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
648 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
649 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
651 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
652 aaf88317 2019-07-10 stsp return NULL;
655 3bfadbd4 2021-11-20 thomas static const struct got_error *
656 3bfadbd4 2021-11-20 thomas get_committer_time(struct got_reference *ref, struct got_repository *repo)
658 3bfadbd4 2021-11-20 thomas const struct got_error *err = NULL;
659 3bfadbd4 2021-11-20 thomas int obj_type;
660 3bfadbd4 2021-11-20 thomas struct got_commit_object *commit = NULL;
661 3bfadbd4 2021-11-20 thomas struct got_tag_object *tag = NULL;
662 3bfadbd4 2021-11-20 thomas struct got_object_id *id = NULL;
664 3bfadbd4 2021-11-20 thomas err = got_ref_resolve(&id, repo, ref);
666 3bfadbd4 2021-11-20 thomas return err;
668 3bfadbd4 2021-11-20 thomas err = got_object_get_type(&obj_type, repo, id);
670 3bfadbd4 2021-11-20 thomas goto done;
672 3bfadbd4 2021-11-20 thomas switch (obj_type) {
673 3bfadbd4 2021-11-20 thomas case GOT_OBJ_TYPE_COMMIT:
674 3bfadbd4 2021-11-20 thomas err = got_object_open_as_commit(&commit, repo, id);
676 3bfadbd4 2021-11-20 thomas goto done;
677 3bfadbd4 2021-11-20 thomas ref->committer_time =
678 3bfadbd4 2021-11-20 thomas got_object_commit_get_committer_time(commit);
680 3bfadbd4 2021-11-20 thomas case GOT_OBJ_TYPE_TAG:
681 3bfadbd4 2021-11-20 thomas err = got_object_open_as_tag(&tag, repo, id);
683 3bfadbd4 2021-11-20 thomas goto done;
684 3bfadbd4 2021-11-20 thomas ref->committer_time = got_object_tag_get_tagger_time(tag);
687 3bfadbd4 2021-11-20 thomas /* best effort for other object types */
688 3bfadbd4 2021-11-20 thomas ref->committer_time = got_ref_get_mtime(ref);
692 3bfadbd4 2021-11-20 thomas free(id);
693 3bfadbd4 2021-11-20 thomas if (commit)
694 3bfadbd4 2021-11-20 thomas got_object_commit_close(commit);
696 3bfadbd4 2021-11-20 thomas got_object_tag_close(tag);
697 e600f124 2021-03-21 stsp return err;
700 e600f124 2021-03-21 stsp const struct got_error *
701 fd77650a 2024-03-19 thomas got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
702 fd77650a 2024-03-19 thomas struct got_reference *ref2)
704 fd77650a 2024-03-19 thomas const struct got_error *err = NULL;
705 fd77650a 2024-03-19 thomas struct got_repository *repo = arg;
707 fd77650a 2024-03-19 thomas *cmp = 0;
709 fd77650a 2024-03-19 thomas if (ref1->committer_time == 0) {
710 fd77650a 2024-03-19 thomas err = get_committer_time(ref1, repo);
712 fd77650a 2024-03-19 thomas return err;
714 fd77650a 2024-03-19 thomas if (ref2->committer_time == 0) {
715 fd77650a 2024-03-19 thomas err = get_committer_time(ref2, repo);
717 fd77650a 2024-03-19 thomas return err;
720 fd77650a 2024-03-19 thomas /* Put latest tags first. */
721 fd77650a 2024-03-19 thomas if (ref1->committer_time < ref2->committer_time)
722 fd77650a 2024-03-19 thomas *cmp = 1;
723 fd77650a 2024-03-19 thomas else if (ref1->committer_time > ref2->committer_time)
724 fd77650a 2024-03-19 thomas *cmp = -1;
726 fd77650a 2024-03-19 thomas err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 fd77650a 2024-03-19 thomas return err;
731 fd77650a 2024-03-19 thomas const struct got_error *
732 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
733 e600f124 2021-03-21 stsp struct got_reference *ref1, struct got_reference *ref2)
735 e87a53ff 2021-11-20 thomas const struct got_error *err = NULL;
736 e600f124 2021-03-21 stsp struct got_repository *repo = arg;
740 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time == 0) {
741 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref1, repo);
743 fd3a2fe6 2021-10-12 thomas return err;
745 3bfadbd4 2021-11-20 thomas if (ref2->committer_time == 0) {
746 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref2, repo);
748 fd3a2fe6 2021-10-12 thomas return err;
751 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time < ref2->committer_time)
753 fd3a2fe6 2021-10-12 thomas else if (ref2->committer_time < ref1->committer_time)
756 a729e288 2021-11-20 thomas return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
758 d1f16636 2020-01-15 stsp return err;
761 779e1159 2021-06-18 stsp const struct got_error *
762 48b4f239 2021-12-31 thomas got_reflist_insert(struct got_reflist_entry **newp,
763 48b4f239 2021-12-31 thomas struct got_reflist_head *refs, struct got_reference *ref,
764 48b4f239 2021-12-31 thomas got_ref_cmp_cb cmp_cb, void *cmp_arg)
766 199a4027 2019-02-02 stsp const struct got_error *err;
767 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
770 505287be 2019-03-15 stsp *newp = NULL;
772 41861f2b 2022-09-05 thomas if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
774 21de8138 2022-09-05 thomas * If we are not sorting elements by name then we must still
775 21de8138 2022-09-05 thomas * detect collisions between a packed ref and an on-disk ref
776 21de8138 2022-09-05 thomas * using the same name. On-disk refs take precedence and are
777 21de8138 2022-09-05 thomas * already present on the list before packed refs get added.
779 21de8138 2022-09-05 thomas TAILQ_FOREACH(re, refs, entry) {
780 41861f2b 2022-09-05 thomas err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
782 21de8138 2022-09-05 thomas return err;
783 41861f2b 2022-09-05 thomas if (cmp == 0)
784 21de8138 2022-09-05 thomas return NULL;
788 41861f2b 2022-09-05 thomas new = malloc(sizeof(*new));
789 41861f2b 2022-09-05 thomas if (new == NULL)
790 41861f2b 2022-09-05 thomas return got_error_from_errno("malloc");
791 41861f2b 2022-09-05 thomas new->ref = ref;
792 41861f2b 2022-09-05 thomas *newp = new;
795 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
796 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
797 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
798 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
800 d9dff0e5 2020-12-26 stsp * Many callers will provide paths in a somewhat sorted order.
801 d9dff0e5 2020-12-26 stsp * Iterating backwards from the tail of the list should be more
802 d9dff0e5 2020-12-26 stsp * efficient than traversing through the entire list each time
803 d9dff0e5 2020-12-26 stsp * an element is inserted.
805 d9dff0e5 2020-12-26 stsp re = TAILQ_LAST(refs, got_reflist_head);
806 7a3c76f5 2019-02-05 stsp while (re) {
807 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
809 b8bad2ba 2019-08-23 stsp return err;
810 e397b6db 2019-02-04 stsp if (cmp == 0) {
811 27a1ed03 2019-03-15 stsp /* duplicate */
813 505287be 2019-03-15 stsp *newp = NULL;
814 7a3c76f5 2019-02-05 stsp return NULL;
815 d9dff0e5 2020-12-26 stsp } else if (cmp < 0) {
816 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_AFTER(refs, re, new, entry);
817 7a3c76f5 2019-02-05 stsp return NULL;
819 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
822 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
823 199a4027 2019-02-02 stsp return NULL;
826 fb95e70d 2021-11-20 thomas const struct got_error *
827 fb95e70d 2021-11-20 thomas got_reflist_sort(struct got_reflist_head *refs,
828 fb95e70d 2021-11-20 thomas got_ref_cmp_cb cmp_cb, void *cmp_arg)
830 fb95e70d 2021-11-20 thomas const struct got_error *err = NULL;
831 fb95e70d 2021-11-20 thomas struct got_reflist_entry *re, *tmp, *new;
832 fb95e70d 2021-11-20 thomas struct got_reflist_head sorted;
834 fb95e70d 2021-11-20 thomas TAILQ_INIT(&sorted);
836 fb95e70d 2021-11-20 thomas TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
837 fb95e70d 2021-11-20 thomas struct got_reference *ref = re->ref;
838 fb95e70d 2021-11-20 thomas TAILQ_REMOVE(refs, re, entry);
839 fb95e70d 2021-11-20 thomas free(re);
840 fb95e70d 2021-11-20 thomas err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
841 fb95e70d 2021-11-20 thomas if (err || new == NULL /* duplicate */)
842 fb95e70d 2021-11-20 thomas got_ref_close(ref);
844 fb95e70d 2021-11-20 thomas return err;
847 fb95e70d 2021-11-20 thomas TAILQ_CONCAT(refs, &sorted, entry);
848 fb95e70d 2021-11-20 thomas return NULL;
851 a04f49d2 2019-02-04 stsp static const struct got_error *
852 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
853 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
854 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
856 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
857 a04f49d2 2019-02-04 stsp DIR *d = NULL;
858 a04f49d2 2019-02-04 stsp char *path_subdir;
859 a7259708 2024-03-30 thomas struct got_reference *ref;
860 a7259708 2024-03-30 thomas struct got_reflist_entry *new;
862 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
865 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
868 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
869 a7259708 2024-03-30 thomas if (d == NULL) {
870 a7259708 2024-03-30 thomas char *refname;
872 a7259708 2024-03-30 thomas if (errno != ENOTDIR)
873 a7259708 2024-03-30 thomas goto done;
875 a7259708 2024-03-30 thomas /* This could be a regular on-disk reference file. */
876 a7259708 2024-03-30 thomas free(path_subdir);
877 a7259708 2024-03-30 thomas err = got_path_dirname(&path_subdir, subdir);
879 a7259708 2024-03-30 thomas return err;
880 a7259708 2024-03-30 thomas err = got_path_basename(&refname, subdir);
881 a7259708 2024-03-30 thomas if (err) {
882 a7259708 2024-03-30 thomas free(path_subdir);
883 a7259708 2024-03-30 thomas return err;
885 a7259708 2024-03-30 thomas err = open_ref(&ref, path_refs, path_subdir, refname,
886 a7259708 2024-03-30 thomas 0, got_repo_get_object_format(repo));
887 a7259708 2024-03-30 thomas free(path_subdir);
888 a7259708 2024-03-30 thomas free(refname);
889 a7259708 2024-03-30 thomas if (err) {
890 a7259708 2024-03-30 thomas if (err->code == GOT_ERR_NOT_REF)
891 a7259708 2024-03-30 thomas return NULL;
892 a7259708 2024-03-30 thomas return err;
894 a7259708 2024-03-30 thomas err = got_reflist_insert(&new, refs, ref,
895 a7259708 2024-03-30 thomas cmp_cb, cmp_arg);
896 a7259708 2024-03-30 thomas if (err || new == NULL /* duplicate */)
897 a7259708 2024-03-30 thomas got_ref_close(ref);
898 a7259708 2024-03-30 thomas return err;
902 a04f49d2 2019-02-04 stsp struct dirent *dent;
903 a04f49d2 2019-02-04 stsp char *child;
906 a04f49d2 2019-02-04 stsp dent = readdir(d);
907 a04f49d2 2019-02-04 stsp if (dent == NULL)
910 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
911 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
914 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
918 20ccae39 2020-07-21 stsp switch (type) {
919 a04f49d2 2019-02-04 stsp case DT_REG:
920 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
921 f4204d57 2023-03-01 thomas 0, got_repo_get_object_format(repo));
922 88f0d95e 2023-06-22 thomas if (err && err->code == GOT_ERR_BAD_REF_NAME)
927 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
928 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
929 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
930 505287be 2019-03-15 stsp got_ref_close(ref);
935 a04f49d2 2019-02-04 stsp case DT_DIR:
936 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
937 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
938 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
941 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
942 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
943 a04f49d2 2019-02-04 stsp free(child);
951 a04f49d2 2019-02-04 stsp closedir(d);
952 a04f49d2 2019-02-04 stsp free(path_subdir);
953 a04f49d2 2019-02-04 stsp return err;
956 a7259708 2024-03-30 thomas static int
957 a7259708 2024-03-30 thomas match_packed_ref(struct got_reference *ref, const char *ref_namespace)
959 a7259708 2024-03-30 thomas const char *name = got_ref_get_name(ref);
960 a7259708 2024-03-30 thomas int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
962 a7259708 2024-03-30 thomas if (namespace_is_absolute) {
963 a7259708 2024-03-30 thomas return (strcmp(name, ref_namespace) == 0 ||
964 a7259708 2024-03-30 thomas got_path_is_child(name, ref_namespace,
965 a7259708 2024-03-30 thomas strlen(ref_namespace)));
968 a7259708 2024-03-30 thomas /* Match all "subdirectories" as we do with on-disk refs. */
969 a7259708 2024-03-30 thomas while (*name != '\0') {
970 a7259708 2024-03-30 thomas while (*name == '/')
972 a7259708 2024-03-30 thomas if (strcmp(name, ref_namespace) == 0 ||
973 a7259708 2024-03-30 thomas got_path_is_child(name, ref_namespace,
974 a7259708 2024-03-30 thomas strlen(ref_namespace)))
975 a7259708 2024-03-30 thomas return 1;
976 a7259708 2024-03-30 thomas while (*name != '\0' && *name != '/')
980 a7259708 2024-03-30 thomas return 0;
983 199a4027 2019-02-02 stsp const struct got_error *
984 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
985 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
987 199a4027 2019-02-02 stsp const struct got_error *err;
988 3bfa5a06 2022-07-21 thomas char *packed_refs_path = NULL, *path_refs = NULL;
989 0c6f49ba 2022-07-01 thomas char *abs_namespace = NULL, *buf = NULL;
990 0c6f49ba 2022-07-01 thomas const char *ondisk_ref_namespace = NULL;
991 9bdd68dd 2020-01-02 naddy char *line = NULL;
992 29b5c214 2019-02-04 stsp FILE *f = NULL;
993 199a4027 2019-02-02 stsp struct got_reference *ref;
994 505287be 2019-03-15 stsp struct got_reflist_entry *new;
996 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
997 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
998 29606af7 2019-08-23 stsp if (path_refs == NULL) {
999 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
1000 29606af7 2019-08-23 stsp goto done;
1002 f4204d57 2023-03-01 thomas err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1003 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
1005 29606af7 2019-08-23 stsp goto done;
1006 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1007 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
1008 29606af7 2019-08-23 stsp got_ref_close(ref);
1009 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
1010 29606af7 2019-08-23 stsp goto done;
1012 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
1013 a7259708 2024-03-30 thomas err = got_ref_open(&ref, repo, ref_namespace, 0);
1014 b2070a3f 2020-03-22 stsp if (err) {
1015 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
1016 b2070a3f 2020-03-22 stsp goto done;
1017 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
1019 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1020 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
1021 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
1022 b2070a3f 2020-03-22 stsp got_ref_close(ref);
1023 b2070a3f 2020-03-22 stsp return err;
1027 b2070a3f 2020-03-22 stsp if (ref_namespace) {
1028 b2070a3f 2020-03-22 stsp size_t len;
1029 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
1030 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1031 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
1032 b2070a3f 2020-03-22 stsp goto done;
1034 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
1035 b2070a3f 2020-03-22 stsp buf = malloc(len);
1036 b2070a3f 2020-03-22 stsp if (buf == NULL) {
1037 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
1038 b2070a3f 2020-03-22 stsp goto done;
1040 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
1042 b2070a3f 2020-03-22 stsp goto done;
1043 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
1044 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
1045 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
1046 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1047 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
1048 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1049 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1052 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1053 29b5c214 2019-02-04 stsp free(path_refs);
1054 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1055 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1056 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1057 29b5c214 2019-02-04 stsp goto done;
1059 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1060 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1061 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1063 29b5c214 2019-02-04 stsp goto done;
1066 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1067 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1069 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1070 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1071 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1072 29b5c214 2019-02-04 stsp goto done;
1075 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1077 7a90b680 2020-01-02 naddy size_t linesize = 0;
1078 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1079 3f338f0a 2021-07-27 stsp struct stat sb;
1081 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1082 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1083 3f338f0a 2021-07-27 stsp goto done;
1086 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1087 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1088 0bb4abae 2019-03-15 stsp if (feof(f))
1090 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1091 0bb4abae 2019-03-15 stsp goto done;
1093 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1094 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1095 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1096 f4204d57 2023-03-01 thomas sb.st_mtime, got_repo_get_object_format(repo));
1098 199a4027 2019-02-02 stsp goto done;
1099 76b4ead2 2019-02-03 stsp if (ref) {
1100 a7259708 2024-03-30 thomas if (ref_namespace &&
1101 a7259708 2024-03-30 thomas !match_packed_ref(ref, ref_namespace)) {
1102 a7259708 2024-03-30 thomas got_ref_close(ref);
1103 a7259708 2024-03-30 thomas continue;
1105 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1106 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1107 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1108 505287be 2019-03-15 stsp got_ref_close(ref);
1110 76b4ead2 2019-02-03 stsp goto done;
1115 3bfa5a06 2022-07-21 thomas free(packed_refs_path);
1116 b2070a3f 2020-03-22 stsp free(abs_namespace);
1117 b2070a3f 2020-03-22 stsp free(buf);
1118 9bdd68dd 2020-01-02 naddy free(line);
1119 a04f49d2 2019-02-04 stsp free(path_refs);
1120 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1121 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1122 199a4027 2019-02-02 stsp return err;
1126 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1128 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1130 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1131 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1132 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1138 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1140 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1143 b249b824 2019-05-09 stsp const struct got_error *
1144 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1146 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1147 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1149 66107227 2023-01-31 thomas memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1150 b249b824 2019-05-09 stsp return NULL;
1153 9e672c74 2019-03-11 stsp const struct got_error *
1154 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1156 b249b824 2019-05-09 stsp char *new_name;
1158 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1159 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1161 b249b824 2019-05-09 stsp new_name = strdup(refname);
1162 b249b824 2019-05-09 stsp if (new_name == NULL)
1163 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1165 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1166 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1167 b249b824 2019-05-09 stsp return NULL;
1170 b249b824 2019-05-09 stsp const struct got_error *
1171 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1172 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1174 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1175 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1177 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1178 66107227 2023-01-31 thomas memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1179 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1180 e8a967e0 2020-03-21 stsp return NULL;
1183 e8a967e0 2020-03-21 stsp const struct got_error *
1184 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1186 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1187 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1188 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1189 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1190 9e672c74 2019-03-11 stsp FILE *f = NULL;
1192 9e672c74 2019-03-11 stsp struct stat sb;
1194 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1195 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1196 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1197 9e672c74 2019-03-11 stsp goto done;
1200 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1202 9e672c74 2019-03-11 stsp goto done;
1205 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1206 49c7094f 2019-03-11 stsp if (err) {
1207 d1667f0d 2019-03-11 stsp char *parent;
1208 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1209 5e1c9f23 2019-03-11 stsp goto done;
1210 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1212 0cd1c46a 2019-03-11 stsp goto done;
1213 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1214 5e1c9f23 2019-03-11 stsp free(parent);
1216 0cd1c46a 2019-03-11 stsp goto done;
1217 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1219 0cd1c46a 2019-03-11 stsp goto done;
1222 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1223 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1224 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1225 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1226 9e672c74 2019-03-11 stsp goto done;
1229 66107227 2023-01-31 thomas char *hex;
1230 66107227 2023-01-31 thomas size_t len;
1232 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &ref->ref.ref.id);
1233 66107227 2023-01-31 thomas if (err)
1234 66107227 2023-01-31 thomas goto done;
1235 66107227 2023-01-31 thomas len = strlen(hex);
1236 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1237 66107227 2023-01-31 thomas free(hex);
1238 66107227 2023-01-31 thomas if (n != len + 1) {
1239 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1240 9e672c74 2019-03-11 stsp goto done;
1244 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1245 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1247 2f17228e 2019-05-12 stsp goto done;
1250 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1252 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1253 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1254 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1255 0cd1c46a 2019-03-11 stsp goto done;
1257 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1260 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1261 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1262 3818e3c4 2020-11-01 naddy goto done;
1265 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1266 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1267 9e672c74 2019-03-11 stsp goto done;
1269 9e672c74 2019-03-11 stsp free(tmppath);
1270 9e672c74 2019-03-11 stsp tmppath = NULL;
1272 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1273 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1274 3f338f0a 2021-07-27 stsp goto done;
1276 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1278 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1279 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1281 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1282 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1284 9e672c74 2019-03-11 stsp free(path_refs);
1285 9e672c74 2019-03-11 stsp free(path);
1286 9e672c74 2019-03-11 stsp if (tmppath) {
1287 e6b88e16 2022-10-16 thomas if (unlink(tmppath) == -1 && err == NULL)
1288 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1289 9e672c74 2019-03-11 stsp free(tmppath);
1291 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1294 598a8b91 2019-03-15 stsp static const struct got_error *
1295 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1297 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1298 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1299 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1300 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1301 7a90b680 2020-01-02 naddy size_t linesize = 0;
1302 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1303 598a8b91 2019-03-15 stsp int found_delref = 0;
1305 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1306 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1307 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1309 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1311 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1312 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1315 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1317 598a8b91 2019-03-15 stsp goto done;
1319 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1320 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1322 2f17228e 2019-05-12 stsp goto done;
1325 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1326 598a8b91 2019-03-15 stsp if (f == NULL) {
1327 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1328 598a8b91 2019-03-15 stsp goto done;
1331 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1332 598a8b91 2019-03-15 stsp struct got_reference *ref;
1333 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1335 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1336 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1337 598a8b91 2019-03-15 stsp if (feof(f))
1339 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1340 598a8b91 2019-03-15 stsp goto done;
1342 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1343 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1344 f4204d57 2023-03-01 thomas err = parse_packed_ref_line(&ref, NULL, line, 0,
1345 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
1347 598a8b91 2019-03-15 stsp goto done;
1348 598a8b91 2019-03-15 stsp if (ref == NULL)
1351 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1352 66107227 2023-01-31 thomas got_object_id_cmp(&ref->ref.ref.id,
1353 66107227 2023-01-31 thomas &delref->ref.ref.id) == 0) {
1354 598a8b91 2019-03-15 stsp found_delref = 1;
1355 598a8b91 2019-03-15 stsp got_ref_close(ref);
1359 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1360 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1361 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1362 598a8b91 2019-03-15 stsp got_ref_close(ref);
1364 598a8b91 2019-03-15 stsp goto done;
1367 598a8b91 2019-03-15 stsp if (found_delref) {
1368 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1370 598a8b91 2019-03-15 stsp struct stat sb;
1372 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1373 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1374 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1375 598a8b91 2019-03-15 stsp goto done;
1378 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1379 66107227 2023-01-31 thomas char *hex;
1380 66107227 2023-01-31 thomas size_t len;
1382 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1383 66107227 2023-01-31 thomas if (err)
1384 598a8b91 2019-03-15 stsp goto done;
1385 66107227 2023-01-31 thomas len = strlen(hex);
1386 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1387 66107227 2023-01-31 thomas free(hex);
1388 66107227 2023-01-31 thomas if (n != len + 1) {
1389 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1390 598a8b91 2019-03-15 stsp goto done;
1393 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1394 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1395 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1396 598a8b91 2019-03-15 stsp goto done;
1400 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1401 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1402 598a8b91 2019-03-15 stsp goto done;
1405 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1406 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1407 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1408 230a42bd 2019-05-11 jcs packed_refs_path);
1409 598a8b91 2019-03-15 stsp goto done;
1411 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1414 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1415 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1416 598a8b91 2019-03-15 stsp goto done;
1419 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1420 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1421 230a42bd 2019-05-11 jcs packed_refs_path);
1422 598a8b91 2019-03-15 stsp goto done;
1424 f96b13c8 2022-10-16 thomas free(tmppath);
1425 f96b13c8 2022-10-16 thomas tmppath = NULL;
1428 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1429 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1431 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1434 f96b13c8 2022-10-16 thomas if (tmppath && unlink(tmppath) == -1 && err == NULL)
1435 f96b13c8 2022-10-16 thomas err = got_error_from_errno2("unlink", tmppath);
1436 f96b13c8 2022-10-16 thomas if (tmpf && fclose(tmpf) == EOF && err == NULL)
1437 f96b13c8 2022-10-16 thomas err = got_error_from_errno("fclose");
1438 598a8b91 2019-03-15 stsp free(tmppath);
1439 598a8b91 2019-03-15 stsp free(packed_refs_path);
1440 9bdd68dd 2020-01-02 naddy free(line);
1441 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1442 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1445 2a104ff6 2020-09-21 stsp static const struct got_error *
1446 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1448 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1449 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1450 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1451 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1453 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1454 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1455 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1456 2d2e1378 2019-03-11 stsp goto done;
1459 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1460 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1461 2d2e1378 2019-03-11 stsp goto done;
1464 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1465 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1467 2f17228e 2019-05-12 stsp goto done;
1470 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1472 e6b88e16 2022-10-16 thomas if (unlink(path) == -1)
1473 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1475 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1476 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1478 2d2e1378 2019-03-11 stsp free(path_refs);
1479 2d2e1378 2019-03-11 stsp free(path);
1480 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1483 2a104ff6 2020-09-21 stsp const struct got_error *
1484 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1486 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1487 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1489 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1490 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1492 2a104ff6 2020-09-21 stsp return err;
1494 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1495 2a104ff6 2020-09-21 stsp if (err) {
1496 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1497 2a104ff6 2020-09-21 stsp return NULL;
1498 2a104ff6 2020-09-21 stsp return err;
1501 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1502 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1503 2a104ff6 2020-09-21 stsp return err;
1505 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1507 2a104ff6 2020-09-21 stsp return err;
1509 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1510 2a104ff6 2020-09-21 stsp if (err) {
1511 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1512 2a104ff6 2020-09-21 stsp return NULL;
1513 2a104ff6 2020-09-21 stsp return err;
1516 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1517 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1518 2a104ff6 2020-09-21 stsp return err;
1522 2f17228e 2019-05-12 stsp const struct got_error *
1523 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1525 2f17228e 2019-05-12 stsp const struct got_error *err;
1526 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1527 2f17228e 2019-05-12 stsp ref->lf = NULL;
1528 2f17228e 2019-05-12 stsp return err;
1531 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1532 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1535 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1536 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1539 24202e46 2020-12-26 stsp static const struct got_error *
1540 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1541 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1543 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1544 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1545 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1547 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1548 24202e46 2020-12-26 stsp if (ent == NULL) {
1549 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1550 24202e46 2020-12-26 stsp if (ent == NULL)
1551 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1553 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1554 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1555 ee7b409c 2022-04-16 thomas if (err) {
1556 ee7b409c 2022-04-16 thomas free(ent);
1557 24202e46 2020-12-26 stsp return err;
1561 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1563 24202e46 2020-12-26 stsp return err;
1565 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1566 24202e46 2020-12-26 stsp return NULL;
1569 7b5b670e 2020-12-25 stsp const struct got_error *
1570 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1571 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1573 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1574 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1575 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1576 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1578 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1579 7b5b670e 2020-12-25 stsp if (idset == NULL)
1580 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1582 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1583 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1584 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1585 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1587 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1589 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1590 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1592 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1594 7b5b670e 2020-12-25 stsp goto done;
1596 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1598 24202e46 2020-12-26 stsp goto done;
1600 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1602 24202e46 2020-12-26 stsp id = NULL;
1606 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1607 24202e46 2020-12-26 stsp if (err) {
1608 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1609 7b5b670e 2020-12-25 stsp goto done;
1610 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1611 24202e46 2020-12-26 stsp err = NULL;
1612 24202e46 2020-12-26 stsp tag = NULL;
1614 24202e46 2020-12-26 stsp id = NULL;
1618 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1619 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1620 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1622 7b5b670e 2020-12-25 stsp goto done;
1625 7b5b670e 2020-12-25 stsp id = NULL;
1629 7b5b670e 2020-12-25 stsp if (err) {
1630 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1631 7b5b670e 2020-12-25 stsp *map = NULL;
1633 a53af95f 2020-12-26 stsp return err;
1636 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1637 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1638 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1640 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1641 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1643 7b5b670e 2020-12-25 stsp return &ent->refs;
1644 7b5b670e 2020-12-25 stsp return NULL;
1647 7b5b670e 2020-12-25 stsp static const struct got_error *
1648 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1650 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1652 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1653 7b5b670e 2020-12-25 stsp free(ent);
1654 7b5b670e 2020-12-25 stsp return NULL;
1658 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1660 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1661 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1662 7b5b670e 2020-12-25 stsp free(map);