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 a04f49d2 2019-02-04 stsp #include <dirent.h>
23 56e0773d 2019-11-28 stsp #include <limits.h>
24 5261c201 2018-04-01 stsp #include <sha1.h>
25 5822e79e 2023-02-23 op #include <sha2.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 53bf0b54 2023-02-23 op #include "got_lib_hash.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 faf5b56f 2023-01-31 op struct got_object_id id;
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;
90 3f338f0a 2021-07-27 stsp time_t mtime;
93 c5d61cbc 2024-03-16 stsp * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 c5d61cbc 2024-03-16 stsp * and got_ref_cmp_tags().
96 2c9e323b 2021-10-10 stsp time_t committer_time;
99 5261c201 2018-04-01 stsp static const struct got_error *
100 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
101 3f338f0a 2021-07-27 stsp struct got_object_id *id, int flags, time_t mtime)
103 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
105 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
106 f9267c9a 2019-03-15 stsp if (*ref == NULL)
107 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
109 faf5b56f 2023-01-31 op memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
110 c980e470 2019-03-15 stsp (*ref)->flags = flags;
111 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
112 3f338f0a 2021-07-27 stsp (*ref)->mtime = mtime;
113 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
114 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
115 c980e470 2019-03-15 stsp got_ref_close(*ref);
116 f9267c9a 2019-03-15 stsp *ref = NULL;
118 f9267c9a 2019-03-15 stsp return err;
121 f9267c9a 2019-03-15 stsp static const struct got_error *
122 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
123 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
125 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
127 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
128 5261c201 2018-04-01 stsp if (*ref == NULL)
129 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
131 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
132 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
133 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
134 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
135 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
136 f9267c9a 2019-03-15 stsp *ref = NULL;
137 cdb8f1fa 2019-08-28 hiltjo return err;
139 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
140 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
141 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
142 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
143 f9267c9a 2019-03-15 stsp *ref = NULL;
145 f9267c9a 2019-03-15 stsp return err;
148 5261c201 2018-04-01 stsp static const struct got_error *
149 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
151 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
152 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
154 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
157 f9267c9a 2019-03-15 stsp static const struct got_error *
158 3f338f0a 2021-07-27 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line,
159 b54930d5 2023-02-28 op time_t mtime, enum got_hash_algorithm algo)
161 5892cdd6 2019-03-10 stsp struct got_object_id id;
163 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
165 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
168 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, line, algo))
169 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
171 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, 0, mtime);
174 5261c201 2018-04-01 stsp static const struct got_error *
175 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
176 b54930d5 2023-02-28 op const char *absname, const char *abspath, int lock,
177 b54930d5 2023-02-28 op enum got_hash_algorithm algo)
179 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
181 c30018ad 2019-10-21 stsp char *line = NULL;
182 c30018ad 2019-10-21 stsp size_t linesize = 0;
183 c30018ad 2019-10-21 stsp ssize_t linelen;
184 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
185 3f338f0a 2021-07-27 stsp struct stat sb;
187 2f17228e 2019-05-12 stsp if (lock) {
188 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, abspath, -1);
190 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
191 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
192 9f142382 2020-03-21 stsp return err;
196 00fe21f2 2021-12-31 stsp f = fopen(abspath, "rbe");
197 f5c58ad1 2019-05-12 stsp if (f == NULL) {
198 b2070a3f 2020-03-22 stsp if (errno != ENOTDIR && errno != ENOENT)
199 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("fopen", abspath);
201 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
203 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
204 b2070a3f 2020-03-22 stsp return err;
206 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
207 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", abspath);
211 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
212 c30018ad 2019-10-21 stsp if (linelen == -1) {
213 c30018ad 2019-10-21 stsp if (feof(f))
214 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
216 b2070a3f 2020-03-22 stsp if (errno == EISDIR)
217 b2070a3f 2020-03-22 stsp err = got_error(GOT_ERR_NOT_REF);
218 b2070a3f 2020-03-22 stsp else if (ferror(f))
219 b2070a3f 2020-03-22 stsp err = got_ferror(f, GOT_ERR_IO);
221 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
224 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
227 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
228 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
232 b54930d5 2023-02-28 op err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
233 f5c58ad1 2019-05-12 stsp if (lock) {
235 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
238 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
240 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
244 5261c201 2018-04-01 stsp free(line);
245 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
247 2f17228e 2019-05-12 stsp if (*ref) {
249 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
250 2f17228e 2019-05-12 stsp got_ref_close(*ref);
251 2f17228e 2019-05-12 stsp *ref = NULL;
254 5261c201 2018-04-01 stsp return err;
258 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
260 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
261 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
262 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
263 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
266 c5f754cc 2019-02-01 stsp static char *
267 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
269 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
270 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
272 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
275 5892cdd6 2019-03-10 stsp const struct got_error *
276 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
277 5892cdd6 2019-03-10 stsp struct got_object_id *id)
279 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
280 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
282 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, id, 0, 0);
285 aaf88317 2019-07-10 stsp const struct got_error *
286 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
287 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
289 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
290 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
292 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
295 d1f2edc9 2018-06-13 stsp static const struct got_error *
296 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
297 b54930d5 2023-02-28 op const char *line, time_t mtime, enum got_hash_algorithm algo)
299 5892cdd6 2019-03-10 stsp struct got_object_id id;
300 5892cdd6 2019-03-10 stsp const char *name;
302 fb79db15 2019-02-01 stsp *ref = NULL;
304 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
305 fb79db15 2019-02-01 stsp return NULL;
307 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, line, algo))
308 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
310 199a4027 2019-02-02 stsp if (abs_refname) {
311 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
312 199a4027 2019-02-02 stsp return NULL;
313 5892cdd6 2019-03-10 stsp name = abs_refname;
315 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
317 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
320 fb79db15 2019-02-01 stsp static const struct got_error *
321 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
322 b54930d5 2023-02-28 op int nsubdirs, const char *refname, time_t mtime,
323 b54930d5 2023-02-28 op enum got_hash_algorithm algo)
325 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
326 fb79db15 2019-02-01 stsp char *abs_refname;
327 9bdd68dd 2020-01-02 naddy char *line = NULL;
328 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
329 9bdd68dd 2020-01-02 naddy ssize_t linelen;
330 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
332 1e37702e 2019-02-04 stsp *ref = NULL;
334 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
335 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
337 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
338 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
339 7ab0422a 2019-03-15 stsp if (feof(f))
341 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
344 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
345 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
346 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
347 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
348 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
349 0dec1cc0 2019-02-01 stsp refname) == -1)
350 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
351 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(ref, abs_refname, line,
352 b54930d5 2023-02-28 op mtime, algo);
353 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
354 0dec1cc0 2019-02-01 stsp free(abs_refname);
355 532920c8 2019-02-01 stsp if (err || *ref != NULL)
360 fb79db15 2019-02-01 stsp } while (*ref == NULL);
361 9bdd68dd 2020-01-02 naddy free(line);
363 fb79db15 2019-02-01 stsp return err;
366 fb79db15 2019-02-01 stsp static const struct got_error *
367 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 b54930d5 2023-02-28 op const char *name, int lock, enum got_hash_algorithm algo)
370 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
371 a04f49d2 2019-02-04 stsp char *path = NULL;
372 a04f49d2 2019-02-04 stsp char *absname = NULL;
373 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
374 75236079 2020-03-25 stsp int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
376 1e37702e 2019-02-04 stsp *ref = NULL;
378 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
379 57c18198 2021-05-24 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
381 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
382 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
383 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
384 a04f49d2 2019-02-04 stsp absname = (char *)name;
386 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
387 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
390 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
391 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
392 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
397 b54930d5 2023-02-28 op err = parse_ref_file(ref, name, absname, path, lock, algo);
399 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
400 a04f49d2 2019-02-04 stsp free(absname);
401 a04f49d2 2019-02-04 stsp free(path);
402 d1f2edc9 2018-06-13 stsp return err;
405 5261c201 2018-04-01 stsp const struct got_error *
406 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
407 abc59930 2021-09-05 naddy const char *refname, int lock)
409 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
410 c3821bef 2022-07-21 florian char *packed_refs_path = NULL, *path_refs = NULL;
411 fb79db15 2019-02-01 stsp const char *subdirs[] = {
412 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
415 16aeacf7 2020-11-26 stsp int well_known = is_well_known_ref(refname);
416 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
418 1e37702e 2019-02-04 stsp *ref = NULL;
420 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
421 e135804e 2019-02-10 stsp if (path_refs == NULL) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
426 0885ce8f 2019-05-12 stsp if (well_known) {
427 b54930d5 2023-02-28 op err = open_ref(ref, path_refs, "", refname, lock,
428 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
432 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
433 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
434 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
435 b54930d5 2023-02-28 op lock, got_repo_get_object_format(repo));
436 b2070a3f 2020-03-22 stsp if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
440 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
441 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
442 638f9024 2019-05-13 stsp err = got_error_from_errno(
443 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
447 2f17228e 2019-05-12 stsp if (lock) {
448 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
452 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "rbe");
453 c5f754cc 2019-02-01 stsp if (f != NULL) {
454 3f338f0a 2021-07-27 stsp struct stat sb;
455 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
456 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat",
457 3f338f0a 2021-07-27 stsp packed_refs_path);
460 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 b54930d5 2023-02-28 op refname, sb.st_mtime,
462 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
463 a875589a 2019-05-12 stsp if (!err) {
464 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
466 a875589a 2019-05-12 stsp got_ref_close(*ref);
467 a875589a 2019-05-12 stsp *ref = NULL;
468 6e472abb 2019-05-13 stsp } else if (*ref)
469 a875589a 2019-05-12 stsp (*ref)->lf = lf;
474 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
475 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
476 a875589a 2019-05-12 stsp if (err && lf)
477 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
478 c3821bef 2022-07-21 florian free(packed_refs_path);
479 5261c201 2018-04-01 stsp free(path_refs);
480 5261c201 2018-04-01 stsp return err;
484 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
486 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
488 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
490 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
494 5261c201 2018-04-01 stsp struct got_reference *
495 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
497 5261c201 2018-04-01 stsp struct got_reference *ret;
499 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
500 5261c201 2018-04-01 stsp if (ret == NULL)
501 5261c201 2018-04-01 stsp return NULL;
503 5261c201 2018-04-01 stsp ret->flags = ref->flags;
504 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
506 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
508 5261c201 2018-04-01 stsp return NULL;
510 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
512 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
514 5261c201 2018-04-01 stsp return NULL;
517 4c4ce67b 2020-12-25 stsp ret->ref.ref.name = strdup(ref->ref.ref.name);
518 4c4ce67b 2020-12-25 stsp if (ret->ref.ref.name == NULL) {
520 5261c201 2018-04-01 stsp return NULL;
522 faf5b56f 2023-01-31 op memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
523 faf5b56f 2023-01-31 op sizeof(ret->ref.ref.id));
526 5261c201 2018-04-01 stsp return ret;
529 b8bad2ba 2019-08-23 stsp const struct got_error *
530 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
531 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
533 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
534 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
536 b8bad2ba 2019-08-23 stsp *newp = NULL;
538 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
539 b8bad2ba 2019-08-23 stsp if (new == NULL)
540 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
542 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
543 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
544 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
546 b8bad2ba 2019-08-23 stsp return err;
549 b8bad2ba 2019-08-23 stsp *newp = new;
550 b8bad2ba 2019-08-23 stsp return NULL;
553 cce2f485 2021-08-22 stsp const struct got_error *
554 cce2f485 2021-08-22 stsp got_ref_resolve_symbolic(struct got_reference **resolved,
555 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
557 5261c201 2018-04-01 stsp struct got_reference *nextref;
558 5261c201 2018-04-01 stsp const struct got_error *err;
560 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
562 5261c201 2018-04-01 stsp return err;
564 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
565 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(resolved, repo, nextref);
567 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
569 5261c201 2018-04-01 stsp got_ref_close(nextref);
570 5261c201 2018-04-01 stsp return err;
573 29e86f7a 2019-08-12 stsp static const struct got_error *
574 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
575 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
577 5261c201 2018-04-01 stsp const struct got_error *err;
579 29e86f7a 2019-08-12 stsp if (recursion <= 0)
580 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
581 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
583 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
584 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
585 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(&resolved, repo, ref);
586 5261c201 2018-04-01 stsp if (err == NULL)
587 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
588 57b6f99a 2019-04-13 stsp if (resolved)
589 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
590 5261c201 2018-04-01 stsp return err;
593 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
594 5261c201 2018-04-01 stsp if (*id == NULL)
595 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
596 faf5b56f 2023-01-31 op memcpy(*id, &ref->ref.ref.id, sizeof(**id));
597 5261c201 2018-04-01 stsp return NULL;
600 29e86f7a 2019-08-12 stsp const struct got_error *
601 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 29e86f7a 2019-08-12 stsp struct got_reference *ref)
604 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
608 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
612 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
613 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
615 faf5b56f 2023-01-31 op if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
616 271d2a38 2018-12-25 stsp return NULL;
618 5261c201 2018-04-01 stsp return str;
621 0bd18d37 2019-02-01 stsp const char *
622 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
624 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
625 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
627 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
630 aaf88317 2019-07-10 stsp const char *
631 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
633 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
636 b8bad2ba 2019-08-23 stsp return NULL;
640 3f338f0a 2021-07-27 stsp got_ref_get_mtime(struct got_reference *ref)
642 3f338f0a 2021-07-27 stsp return ref->mtime;
645 b8bad2ba 2019-08-23 stsp const struct got_error *
646 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
647 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
649 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
650 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
652 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
653 aaf88317 2019-07-10 stsp return NULL;
656 7f66531d 2021-11-16 stsp static const struct got_error *
657 7f66531d 2021-11-16 stsp get_committer_time(struct got_reference *ref, struct got_repository *repo)
659 7f66531d 2021-11-16 stsp const struct got_error *err = NULL;
660 7f66531d 2021-11-16 stsp int obj_type;
661 7f66531d 2021-11-16 stsp struct got_commit_object *commit = NULL;
662 7f66531d 2021-11-16 stsp struct got_tag_object *tag = NULL;
663 7f66531d 2021-11-16 stsp struct got_object_id *id = NULL;
665 7f66531d 2021-11-16 stsp err = got_ref_resolve(&id, repo, ref);
667 7f66531d 2021-11-16 stsp return err;
669 7f66531d 2021-11-16 stsp err = got_object_get_type(&obj_type, repo, id);
673 7f66531d 2021-11-16 stsp switch (obj_type) {
674 7f66531d 2021-11-16 stsp case GOT_OBJ_TYPE_COMMIT:
675 7f66531d 2021-11-16 stsp err = got_object_open_as_commit(&commit, repo, id);
678 7f66531d 2021-11-16 stsp ref->committer_time =
679 7f66531d 2021-11-16 stsp got_object_commit_get_committer_time(commit);
681 7f66531d 2021-11-16 stsp case GOT_OBJ_TYPE_TAG:
682 7f66531d 2021-11-16 stsp err = got_object_open_as_tag(&tag, repo, id);
685 7f66531d 2021-11-16 stsp ref->committer_time = got_object_tag_get_tagger_time(tag);
688 7f66531d 2021-11-16 stsp /* best effort for other object types */
689 7f66531d 2021-11-16 stsp ref->committer_time = got_ref_get_mtime(ref);
694 7f66531d 2021-11-16 stsp if (commit)
695 7f66531d 2021-11-16 stsp got_object_commit_close(commit);
697 7f66531d 2021-11-16 stsp got_object_tag_close(tag);
698 e600f124 2021-03-21 stsp return err;
701 e600f124 2021-03-21 stsp const struct got_error *
702 c5d61cbc 2024-03-16 stsp got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
703 c5d61cbc 2024-03-16 stsp struct got_reference *ref2)
705 c5d61cbc 2024-03-16 stsp const struct got_error *err = NULL;
706 c5d61cbc 2024-03-16 stsp struct got_repository *repo = arg;
710 c5d61cbc 2024-03-16 stsp if (ref1->committer_time == 0) {
711 c5d61cbc 2024-03-16 stsp err = get_committer_time(ref1, repo);
713 c5d61cbc 2024-03-16 stsp return err;
715 c5d61cbc 2024-03-16 stsp if (ref2->committer_time == 0) {
716 c5d61cbc 2024-03-16 stsp err = get_committer_time(ref2, repo);
718 c5d61cbc 2024-03-16 stsp return err;
721 c5d61cbc 2024-03-16 stsp /* Put latest tags first. */
722 c5d61cbc 2024-03-16 stsp if (ref1->committer_time < ref2->committer_time)
724 c5d61cbc 2024-03-16 stsp else if (ref1->committer_time > ref2->committer_time)
727 c5d61cbc 2024-03-16 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
729 c5d61cbc 2024-03-16 stsp return err;
732 c5d61cbc 2024-03-16 stsp const struct got_error *
733 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
734 e600f124 2021-03-21 stsp struct got_reference *ref1, struct got_reference *ref2)
736 0309152a 2021-11-20 stsp const struct got_error *err = NULL;
737 e600f124 2021-03-21 stsp struct got_repository *repo = arg;
741 2c9e323b 2021-10-10 stsp if (ref1->committer_time == 0) {
742 7f66531d 2021-11-16 stsp err = get_committer_time(ref1, repo);
744 2c9e323b 2021-10-10 stsp return err;
746 7f66531d 2021-11-16 stsp if (ref2->committer_time == 0) {
747 7f66531d 2021-11-16 stsp err = get_committer_time(ref2, repo);
749 2c9e323b 2021-10-10 stsp return err;
752 2c9e323b 2021-10-10 stsp if (ref1->committer_time < ref2->committer_time)
754 2c9e323b 2021-10-10 stsp else if (ref2->committer_time < ref1->committer_time)
757 42864987 2021-11-20 stsp return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
759 d1f16636 2020-01-15 stsp return err;
762 779e1159 2021-06-18 stsp const struct got_error *
763 c0df5966 2021-12-31 stsp got_reflist_insert(struct got_reflist_entry **newp,
764 c0df5966 2021-12-31 stsp struct got_reflist_head *refs, struct got_reference *ref,
765 c0df5966 2021-12-31 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
767 199a4027 2019-02-02 stsp const struct got_error *err;
768 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
771 505287be 2019-03-15 stsp *newp = NULL;
773 3d8e0c5e 2022-09-05 stsp if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
775 7db2b0dd 2022-09-05 stsp * If we are not sorting elements by name then we must still
776 7db2b0dd 2022-09-05 stsp * detect collisions between a packed ref and an on-disk ref
777 7db2b0dd 2022-09-05 stsp * using the same name. On-disk refs take precedence and are
778 7db2b0dd 2022-09-05 stsp * already present on the list before packed refs get added.
780 7db2b0dd 2022-09-05 stsp TAILQ_FOREACH(re, refs, entry) {
781 3d8e0c5e 2022-09-05 stsp err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
783 7db2b0dd 2022-09-05 stsp return err;
784 3d8e0c5e 2022-09-05 stsp if (cmp == 0)
785 7db2b0dd 2022-09-05 stsp return NULL;
789 3d8e0c5e 2022-09-05 stsp new = malloc(sizeof(*new));
790 3d8e0c5e 2022-09-05 stsp if (new == NULL)
791 3d8e0c5e 2022-09-05 stsp return got_error_from_errno("malloc");
792 3d8e0c5e 2022-09-05 stsp new->ref = ref;
793 3d8e0c5e 2022-09-05 stsp *newp = new;
796 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
797 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
798 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
799 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
801 d9dff0e5 2020-12-26 stsp * Many callers will provide paths in a somewhat sorted order.
802 d9dff0e5 2020-12-26 stsp * Iterating backwards from the tail of the list should be more
803 d9dff0e5 2020-12-26 stsp * efficient than traversing through the entire list each time
804 d9dff0e5 2020-12-26 stsp * an element is inserted.
806 d9dff0e5 2020-12-26 stsp re = TAILQ_LAST(refs, got_reflist_head);
807 7a3c76f5 2019-02-05 stsp while (re) {
808 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
810 b8bad2ba 2019-08-23 stsp return err;
811 e397b6db 2019-02-04 stsp if (cmp == 0) {
812 27a1ed03 2019-03-15 stsp /* duplicate */
814 505287be 2019-03-15 stsp *newp = NULL;
815 7a3c76f5 2019-02-05 stsp return NULL;
816 d9dff0e5 2020-12-26 stsp } else if (cmp < 0) {
817 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_AFTER(refs, re, new, entry);
818 7a3c76f5 2019-02-05 stsp return NULL;
820 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
823 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
824 199a4027 2019-02-02 stsp return NULL;
827 2d497592 2021-11-20 stsp const struct got_error *
828 2d497592 2021-11-20 stsp got_reflist_sort(struct got_reflist_head *refs,
829 2d497592 2021-11-20 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
831 2d497592 2021-11-20 stsp const struct got_error *err = NULL;
832 2d497592 2021-11-20 stsp struct got_reflist_entry *re, *tmp, *new;
833 2d497592 2021-11-20 stsp struct got_reflist_head sorted;
835 2d497592 2021-11-20 stsp TAILQ_INIT(&sorted);
837 2d497592 2021-11-20 stsp TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
838 2d497592 2021-11-20 stsp struct got_reference *ref = re->ref;
839 2d497592 2021-11-20 stsp TAILQ_REMOVE(refs, re, entry);
841 2d497592 2021-11-20 stsp err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
842 2d497592 2021-11-20 stsp if (err || new == NULL /* duplicate */)
843 2d497592 2021-11-20 stsp got_ref_close(ref);
845 2d497592 2021-11-20 stsp return err;
848 2d497592 2021-11-20 stsp TAILQ_CONCAT(refs, &sorted, entry);
849 2d497592 2021-11-20 stsp return NULL;
852 a04f49d2 2019-02-04 stsp static const struct got_error *
853 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
854 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
855 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
857 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
858 a04f49d2 2019-02-04 stsp DIR *d = NULL;
859 a04f49d2 2019-02-04 stsp char *path_subdir;
860 0f8a0e25 2024-03-19 stsp struct got_reference *ref;
861 0f8a0e25 2024-03-19 stsp struct got_reflist_entry *new;
863 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
866 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
867 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
869 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
870 0f8a0e25 2024-03-19 stsp if (d == NULL) {
871 0f8a0e25 2024-03-19 stsp char *refname;
873 0f8a0e25 2024-03-19 stsp if (errno != ENOTDIR)
876 0f8a0e25 2024-03-19 stsp /* This could be a regular on-disk reference file. */
877 0f8a0e25 2024-03-19 stsp free(path_subdir);
878 0f8a0e25 2024-03-19 stsp err = got_path_dirname(&path_subdir, subdir);
880 0f8a0e25 2024-03-19 stsp return err;
881 0f8a0e25 2024-03-19 stsp err = got_path_basename(&refname, subdir);
883 0f8a0e25 2024-03-19 stsp free(path_subdir);
884 0f8a0e25 2024-03-19 stsp return err;
886 0f8a0e25 2024-03-19 stsp err = open_ref(&ref, path_refs, path_subdir, refname,
887 0f8a0e25 2024-03-19 stsp 0, got_repo_get_object_format(repo));
888 0f8a0e25 2024-03-19 stsp free(path_subdir);
889 0f8a0e25 2024-03-19 stsp free(refname);
891 0f8a0e25 2024-03-19 stsp if (err->code == GOT_ERR_NOT_REF)
892 0f8a0e25 2024-03-19 stsp return NULL;
893 0f8a0e25 2024-03-19 stsp return err;
895 0f8a0e25 2024-03-19 stsp err = got_reflist_insert(&new, refs, ref,
896 0f8a0e25 2024-03-19 stsp cmp_cb, cmp_arg);
897 0f8a0e25 2024-03-19 stsp if (err || new == NULL /* duplicate */)
898 0f8a0e25 2024-03-19 stsp got_ref_close(ref);
899 0f8a0e25 2024-03-19 stsp return err;
903 a04f49d2 2019-02-04 stsp struct dirent *dent;
904 a04f49d2 2019-02-04 stsp char *child;
907 a04f49d2 2019-02-04 stsp dent = readdir(d);
908 a04f49d2 2019-02-04 stsp if (dent == NULL)
911 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
912 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
915 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
919 20ccae39 2020-07-21 stsp switch (type) {
920 a04f49d2 2019-02-04 stsp case DT_REG:
921 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
922 b54930d5 2023-02-28 op 0, got_repo_get_object_format(repo));
923 2f3ccc5f 2023-06-16 op if (err && err->code == GOT_ERR_BAD_REF_NAME)
928 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
929 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
930 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
931 505287be 2019-03-15 stsp got_ref_close(ref);
936 a04f49d2 2019-02-04 stsp case DT_DIR:
937 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
938 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
942 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
943 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
944 a04f49d2 2019-02-04 stsp free(child);
952 a04f49d2 2019-02-04 stsp closedir(d);
953 a04f49d2 2019-02-04 stsp free(path_subdir);
954 a04f49d2 2019-02-04 stsp return err;
958 0f8a0e25 2024-03-19 stsp match_packed_ref(struct got_reference *ref, const char *ref_namespace)
960 0f8a0e25 2024-03-19 stsp const char *name = got_ref_get_name(ref);
961 0f8a0e25 2024-03-19 stsp int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
963 0f8a0e25 2024-03-19 stsp if (namespace_is_absolute) {
964 0f8a0e25 2024-03-19 stsp return (strcmp(name, ref_namespace) == 0 ||
965 0f8a0e25 2024-03-19 stsp got_path_is_child(name, ref_namespace,
966 0f8a0e25 2024-03-19 stsp strlen(ref_namespace)));
969 0f8a0e25 2024-03-19 stsp /* Match all "subdirectories" as we do with on-disk refs. */
970 0f8a0e25 2024-03-19 stsp while (*name != '\0') {
971 0f8a0e25 2024-03-19 stsp while (*name == '/')
973 0f8a0e25 2024-03-19 stsp if (strcmp(name, ref_namespace) == 0 ||
974 0f8a0e25 2024-03-19 stsp got_path_is_child(name, ref_namespace,
975 0f8a0e25 2024-03-19 stsp strlen(ref_namespace)))
977 0f8a0e25 2024-03-19 stsp while (*name != '\0' && *name != '/')
984 199a4027 2019-02-02 stsp const struct got_error *
985 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
986 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
988 199a4027 2019-02-02 stsp const struct got_error *err;
989 c3821bef 2022-07-21 florian char *packed_refs_path = NULL, *path_refs = NULL;
990 58e31a80 2022-06-27 op char *abs_namespace = NULL, *buf = NULL;
991 58e31a80 2022-06-27 op const char *ondisk_ref_namespace = NULL;
992 9bdd68dd 2020-01-02 naddy char *line = NULL;
993 29b5c214 2019-02-04 stsp FILE *f = NULL;
994 199a4027 2019-02-02 stsp struct got_reference *ref;
995 505287be 2019-03-15 stsp struct got_reflist_entry *new;
997 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
998 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
999 29606af7 2019-08-23 stsp if (path_refs == NULL) {
1000 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
1001 29606af7 2019-08-23 stsp goto done;
1003 b54930d5 2023-02-28 op err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1004 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
1006 29606af7 2019-08-23 stsp goto done;
1007 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1008 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
1009 29606af7 2019-08-23 stsp got_ref_close(ref);
1010 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
1011 29606af7 2019-08-23 stsp goto done;
1013 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
1014 0f8a0e25 2024-03-19 stsp err = got_ref_open(&ref, repo, ref_namespace, 0);
1015 b2070a3f 2020-03-22 stsp if (err) {
1016 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
1017 b2070a3f 2020-03-22 stsp goto done;
1018 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
1020 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1021 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
1022 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
1023 b2070a3f 2020-03-22 stsp got_ref_close(ref);
1024 b2070a3f 2020-03-22 stsp return err;
1028 b2070a3f 2020-03-22 stsp if (ref_namespace) {
1029 b2070a3f 2020-03-22 stsp size_t len;
1030 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
1031 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1032 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
1033 b2070a3f 2020-03-22 stsp goto done;
1035 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
1036 b2070a3f 2020-03-22 stsp buf = malloc(len);
1037 b2070a3f 2020-03-22 stsp if (buf == NULL) {
1038 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
1039 b2070a3f 2020-03-22 stsp goto done;
1041 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
1043 b2070a3f 2020-03-22 stsp goto done;
1044 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
1045 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
1046 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
1047 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1048 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
1049 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1050 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1053 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1054 29b5c214 2019-02-04 stsp free(path_refs);
1055 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1056 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1057 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1058 29b5c214 2019-02-04 stsp goto done;
1060 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1061 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1062 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1064 29b5c214 2019-02-04 stsp goto done;
1067 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1068 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1070 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1071 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1073 29b5c214 2019-02-04 stsp goto done;
1076 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "re");
1078 7a90b680 2020-01-02 naddy size_t linesize = 0;
1079 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1080 3f338f0a 2021-07-27 stsp struct stat sb;
1082 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1083 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1084 3f338f0a 2021-07-27 stsp goto done;
1087 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1088 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1089 0bb4abae 2019-03-15 stsp if (feof(f))
1091 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1092 0bb4abae 2019-03-15 stsp goto done;
1094 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1095 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1096 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1097 b54930d5 2023-02-28 op sb.st_mtime, got_repo_get_object_format(repo));
1099 199a4027 2019-02-02 stsp goto done;
1100 76b4ead2 2019-02-03 stsp if (ref) {
1101 0f8a0e25 2024-03-19 stsp if (ref_namespace &&
1102 0f8a0e25 2024-03-19 stsp !match_packed_ref(ref, ref_namespace)) {
1103 0f8a0e25 2024-03-19 stsp got_ref_close(ref);
1106 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1107 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1108 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1109 505287be 2019-03-15 stsp got_ref_close(ref);
1111 76b4ead2 2019-02-03 stsp goto done;
1116 c3821bef 2022-07-21 florian free(packed_refs_path);
1117 b2070a3f 2020-03-22 stsp free(abs_namespace);
1118 b2070a3f 2020-03-22 stsp free(buf);
1119 9bdd68dd 2020-01-02 naddy free(line);
1120 a04f49d2 2019-02-04 stsp free(path_refs);
1121 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1123 199a4027 2019-02-02 stsp return err;
1127 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1129 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1131 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1132 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1133 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1139 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1141 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1144 b249b824 2019-05-09 stsp const struct got_error *
1145 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1147 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1148 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1150 faf5b56f 2023-01-31 op memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1151 b249b824 2019-05-09 stsp return NULL;
1154 9e672c74 2019-03-11 stsp const struct got_error *
1155 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1157 b249b824 2019-05-09 stsp char *new_name;
1159 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1162 b249b824 2019-05-09 stsp new_name = strdup(refname);
1163 b249b824 2019-05-09 stsp if (new_name == NULL)
1164 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1166 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1167 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1168 b249b824 2019-05-09 stsp return NULL;
1171 b249b824 2019-05-09 stsp const struct got_error *
1172 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1173 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1175 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1176 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1178 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1179 faf5b56f 2023-01-31 op memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1180 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1181 e8a967e0 2020-03-21 stsp return NULL;
1184 e8a967e0 2020-03-21 stsp const struct got_error *
1185 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1187 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1188 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1189 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1190 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1191 9e672c74 2019-03-11 stsp FILE *f = NULL;
1193 9e672c74 2019-03-11 stsp struct stat sb;
1195 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1196 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1197 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1198 9e672c74 2019-03-11 stsp goto done;
1201 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1202 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1203 9e672c74 2019-03-11 stsp goto done;
1206 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &f, path, "");
1207 49c7094f 2019-03-11 stsp if (err) {
1208 d1667f0d 2019-03-11 stsp char *parent;
1209 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1210 5e1c9f23 2019-03-11 stsp goto done;
1211 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1213 0cd1c46a 2019-03-11 stsp goto done;
1214 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1215 5e1c9f23 2019-03-11 stsp free(parent);
1217 0cd1c46a 2019-03-11 stsp goto done;
1218 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &f, path, "");
1220 0cd1c46a 2019-03-11 stsp goto done;
1223 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1224 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1225 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1226 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1227 9e672c74 2019-03-11 stsp goto done;
1233 faf5b56f 2023-01-31 op err = got_object_id_str(&hex, &ref->ref.ref.id);
1236 faf5b56f 2023-01-31 op len = strlen(hex);
1237 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1239 faf5b56f 2023-01-31 op if (n != len + 1) {
1240 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1241 9e672c74 2019-03-11 stsp goto done;
1245 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1246 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1248 2f17228e 2019-05-12 stsp goto done;
1251 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1253 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1254 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1255 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1256 0cd1c46a 2019-03-11 stsp goto done;
1258 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1261 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1262 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1263 3818e3c4 2020-11-01 naddy goto done;
1266 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1267 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1268 9e672c74 2019-03-11 stsp goto done;
1270 9e672c74 2019-03-11 stsp free(tmppath);
1271 9e672c74 2019-03-11 stsp tmppath = NULL;
1273 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1274 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1275 3f338f0a 2021-07-27 stsp goto done;
1277 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1279 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1280 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1282 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1283 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1285 9e672c74 2019-03-11 stsp free(path_refs);
1286 9e672c74 2019-03-11 stsp free(path);
1287 9e672c74 2019-03-11 stsp if (tmppath) {
1288 a06ca3f7 2022-10-15 stsp if (unlink(tmppath) == -1 && err == NULL)
1289 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1290 9e672c74 2019-03-11 stsp free(tmppath);
1292 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1295 598a8b91 2019-03-15 stsp static const struct got_error *
1296 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1298 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1299 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1300 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1301 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1302 7a90b680 2020-01-02 naddy size_t linesize = 0;
1303 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1304 598a8b91 2019-03-15 stsp int found_delref = 0;
1306 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1307 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1308 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1310 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1312 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1313 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1314 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1316 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1318 598a8b91 2019-03-15 stsp goto done;
1320 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1321 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1323 2f17228e 2019-05-12 stsp goto done;
1326 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "re");
1327 598a8b91 2019-03-15 stsp if (f == NULL) {
1328 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1329 598a8b91 2019-03-15 stsp goto done;
1332 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1333 598a8b91 2019-03-15 stsp struct got_reference *ref;
1334 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1336 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1337 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1338 598a8b91 2019-03-15 stsp if (feof(f))
1340 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1341 598a8b91 2019-03-15 stsp goto done;
1343 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1344 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1345 b54930d5 2023-02-28 op err = parse_packed_ref_line(&ref, NULL, line, 0,
1346 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
1348 598a8b91 2019-03-15 stsp goto done;
1349 598a8b91 2019-03-15 stsp if (ref == NULL)
1352 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1353 faf5b56f 2023-01-31 op got_object_id_cmp(&ref->ref.ref.id,
1354 faf5b56f 2023-01-31 op &delref->ref.ref.id) == 0) {
1355 598a8b91 2019-03-15 stsp found_delref = 1;
1356 598a8b91 2019-03-15 stsp got_ref_close(ref);
1360 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1361 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1362 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1363 598a8b91 2019-03-15 stsp got_ref_close(ref);
1365 598a8b91 2019-03-15 stsp goto done;
1368 598a8b91 2019-03-15 stsp if (found_delref) {
1369 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1371 598a8b91 2019-03-15 stsp struct stat sb;
1373 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1374 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1375 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1376 598a8b91 2019-03-15 stsp goto done;
1379 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1383 faf5b56f 2023-01-31 op err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1385 598a8b91 2019-03-15 stsp goto done;
1386 faf5b56f 2023-01-31 op len = strlen(hex);
1387 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1389 faf5b56f 2023-01-31 op if (n != len + 1) {
1390 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1391 598a8b91 2019-03-15 stsp goto done;
1394 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1395 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1396 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1397 598a8b91 2019-03-15 stsp goto done;
1401 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1402 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1403 598a8b91 2019-03-15 stsp goto done;
1406 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1407 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1408 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1409 230a42bd 2019-05-11 jcs packed_refs_path);
1410 598a8b91 2019-03-15 stsp goto done;
1412 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1415 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1416 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1417 598a8b91 2019-03-15 stsp goto done;
1420 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1421 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1422 230a42bd 2019-05-11 jcs packed_refs_path);
1423 598a8b91 2019-03-15 stsp goto done;
1425 15c388a9 2022-10-15 stsp free(tmppath);
1426 15c388a9 2022-10-15 stsp tmppath = NULL;
1429 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1430 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1432 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1433 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1435 15c388a9 2022-10-15 stsp if (tmppath && unlink(tmppath) == -1 && err == NULL)
1436 15c388a9 2022-10-15 stsp err = got_error_from_errno2("unlink", tmppath);
1437 15c388a9 2022-10-15 stsp if (tmpf && fclose(tmpf) == EOF && err == NULL)
1438 15c388a9 2022-10-15 stsp err = got_error_from_errno("fclose");
1439 598a8b91 2019-03-15 stsp free(tmppath);
1440 598a8b91 2019-03-15 stsp free(packed_refs_path);
1441 9bdd68dd 2020-01-02 naddy free(line);
1442 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1443 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1446 2a104ff6 2020-09-21 stsp static const struct got_error *
1447 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1449 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1450 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1451 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1452 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1454 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1455 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1456 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1457 2d2e1378 2019-03-11 stsp goto done;
1460 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1462 2d2e1378 2019-03-11 stsp goto done;
1465 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1466 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1468 2f17228e 2019-05-12 stsp goto done;
1471 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1473 a06ca3f7 2022-10-15 stsp if (unlink(path) == -1)
1474 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1476 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1477 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1479 2d2e1378 2019-03-11 stsp free(path_refs);
1480 2d2e1378 2019-03-11 stsp free(path);
1481 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1484 2a104ff6 2020-09-21 stsp const struct got_error *
1485 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1487 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1488 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1490 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1491 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1493 2a104ff6 2020-09-21 stsp return err;
1495 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1496 2a104ff6 2020-09-21 stsp if (err) {
1497 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1498 2a104ff6 2020-09-21 stsp return NULL;
1499 2a104ff6 2020-09-21 stsp return err;
1502 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1503 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1504 2a104ff6 2020-09-21 stsp return err;
1506 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1508 2a104ff6 2020-09-21 stsp return err;
1510 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1511 2a104ff6 2020-09-21 stsp if (err) {
1512 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1513 2a104ff6 2020-09-21 stsp return NULL;
1514 2a104ff6 2020-09-21 stsp return err;
1517 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1518 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1519 2a104ff6 2020-09-21 stsp return err;
1523 2f17228e 2019-05-12 stsp const struct got_error *
1524 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1526 2f17228e 2019-05-12 stsp const struct got_error *err;
1527 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1528 2f17228e 2019-05-12 stsp ref->lf = NULL;
1529 2f17228e 2019-05-12 stsp return err;
1532 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1533 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1536 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1537 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1540 24202e46 2020-12-26 stsp static const struct got_error *
1541 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1542 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1544 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1545 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1546 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1548 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1549 24202e46 2020-12-26 stsp if (ent == NULL) {
1550 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1551 24202e46 2020-12-26 stsp if (ent == NULL)
1552 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1554 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1555 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1558 24202e46 2020-12-26 stsp return err;
1562 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1564 24202e46 2020-12-26 stsp return err;
1566 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1567 24202e46 2020-12-26 stsp return NULL;
1570 7b5b670e 2020-12-25 stsp const struct got_error *
1571 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1572 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1574 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1575 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1576 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1577 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1579 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1580 7b5b670e 2020-12-25 stsp if (idset == NULL)
1581 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1583 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1584 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1585 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1586 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1588 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1590 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1591 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1593 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1595 7b5b670e 2020-12-25 stsp goto done;
1597 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1599 24202e46 2020-12-26 stsp goto done;
1601 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1603 24202e46 2020-12-26 stsp id = NULL;
1607 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1608 24202e46 2020-12-26 stsp if (err) {
1609 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1610 7b5b670e 2020-12-25 stsp goto done;
1611 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1612 24202e46 2020-12-26 stsp err = NULL;
1613 24202e46 2020-12-26 stsp tag = NULL;
1615 24202e46 2020-12-26 stsp id = NULL;
1619 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1620 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1621 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1623 7b5b670e 2020-12-25 stsp goto done;
1626 7b5b670e 2020-12-25 stsp id = NULL;
1630 7b5b670e 2020-12-25 stsp if (err) {
1631 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1632 7b5b670e 2020-12-25 stsp *map = NULL;
1634 a53af95f 2020-12-26 stsp return err;
1637 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1638 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1639 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1641 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1642 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1644 7b5b670e 2020-12-25 stsp return &ent->refs;
1645 7b5b670e 2020-12-25 stsp return NULL;
1648 7b5b670e 2020-12-25 stsp static const struct got_error *
1649 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1651 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1653 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1654 7b5b670e 2020-12-25 stsp free(ent);
1655 7b5b670e 2020-12-25 stsp return NULL;
1659 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1661 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1662 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1663 7b5b670e 2020-12-25 stsp free(map);