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;
860 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
863 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
864 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
866 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
867 a04f49d2 2019-02-04 stsp if (d == NULL)
871 a04f49d2 2019-02-04 stsp struct dirent *dent;
872 a04f49d2 2019-02-04 stsp struct got_reference *ref;
873 a04f49d2 2019-02-04 stsp char *child;
876 a04f49d2 2019-02-04 stsp dent = readdir(d);
877 a04f49d2 2019-02-04 stsp if (dent == NULL)
880 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
881 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
884 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
888 20ccae39 2020-07-21 stsp switch (type) {
889 a04f49d2 2019-02-04 stsp case DT_REG:
890 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
891 f4204d57 2023-03-01 thomas 0, got_repo_get_object_format(repo));
892 88f0d95e 2023-06-22 thomas if (err && err->code == GOT_ERR_BAD_REF_NAME)
897 505287be 2019-03-15 stsp struct got_reflist_entry *new;
898 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
899 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
900 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
901 505287be 2019-03-15 stsp got_ref_close(ref);
906 a04f49d2 2019-02-04 stsp case DT_DIR:
907 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
908 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
909 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
912 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
913 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
914 a04f49d2 2019-02-04 stsp free(child);
922 a04f49d2 2019-02-04 stsp closedir(d);
923 a04f49d2 2019-02-04 stsp free(path_subdir);
924 a04f49d2 2019-02-04 stsp return err;
927 199a4027 2019-02-02 stsp const struct got_error *
928 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
929 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
931 199a4027 2019-02-02 stsp const struct got_error *err;
932 3bfa5a06 2022-07-21 thomas char *packed_refs_path = NULL, *path_refs = NULL;
933 0c6f49ba 2022-07-01 thomas char *abs_namespace = NULL, *buf = NULL;
934 0c6f49ba 2022-07-01 thomas const char *ondisk_ref_namespace = NULL;
935 9bdd68dd 2020-01-02 naddy char *line = NULL;
936 29b5c214 2019-02-04 stsp FILE *f = NULL;
937 199a4027 2019-02-02 stsp struct got_reference *ref;
938 505287be 2019-03-15 stsp struct got_reflist_entry *new;
940 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
941 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
942 29606af7 2019-08-23 stsp if (path_refs == NULL) {
943 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
946 f4204d57 2023-03-01 thomas err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
947 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
950 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
951 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
952 29606af7 2019-08-23 stsp got_ref_close(ref);
953 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
956 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
957 b2070a3f 2020-03-22 stsp const char *refname = ref_namespace;
958 b2070a3f 2020-03-22 stsp path_refs = get_refs_dir_path(repo, refname);
959 b2070a3f 2020-03-22 stsp if (path_refs == NULL) {
960 b2070a3f 2020-03-22 stsp err = got_error_from_errno("get_refs_dir_path");
963 f4204d57 2023-03-01 thomas err = open_ref(&ref, path_refs, "", refname, 0,
964 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
966 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
968 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
970 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
971 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
972 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
973 b2070a3f 2020-03-22 stsp got_ref_close(ref);
974 b2070a3f 2020-03-22 stsp return err;
978 b2070a3f 2020-03-22 stsp if (ref_namespace) {
979 b2070a3f 2020-03-22 stsp size_t len;
980 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
981 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
982 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
985 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
986 b2070a3f 2020-03-22 stsp buf = malloc(len);
987 b2070a3f 2020-03-22 stsp if (buf == NULL) {
988 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
991 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
994 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
995 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
996 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
997 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
998 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
999 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1000 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1003 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1004 29b5c214 2019-02-04 stsp free(path_refs);
1005 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1006 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1007 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1008 29b5c214 2019-02-04 stsp goto done;
1010 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1011 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1012 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1014 29b5c214 2019-02-04 stsp goto done;
1017 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1018 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1020 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1021 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1022 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1023 29b5c214 2019-02-04 stsp goto done;
1026 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1028 7a90b680 2020-01-02 naddy size_t linesize = 0;
1029 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1030 3f338f0a 2021-07-27 stsp struct stat sb;
1032 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1033 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1034 3f338f0a 2021-07-27 stsp goto done;
1037 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1038 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1039 0bb4abae 2019-03-15 stsp if (feof(f))
1041 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1042 0bb4abae 2019-03-15 stsp goto done;
1044 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1045 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1046 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1047 f4204d57 2023-03-01 thomas sb.st_mtime, got_repo_get_object_format(repo));
1049 199a4027 2019-02-02 stsp goto done;
1050 76b4ead2 2019-02-03 stsp if (ref) {
1051 29606af7 2019-08-23 stsp if (ref_namespace) {
1052 29606af7 2019-08-23 stsp const char *name;
1053 29606af7 2019-08-23 stsp name = got_ref_get_name(ref);
1054 b2070a3f 2020-03-22 stsp if (!got_path_is_child(name,
1055 b2070a3f 2020-03-22 stsp ref_namespace,
1056 b2070a3f 2020-03-22 stsp strlen(ref_namespace))) {
1057 29606af7 2019-08-23 stsp got_ref_close(ref);
1061 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1062 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1063 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1064 505287be 2019-03-15 stsp got_ref_close(ref);
1066 76b4ead2 2019-02-03 stsp goto done;
1071 3bfa5a06 2022-07-21 thomas free(packed_refs_path);
1072 b2070a3f 2020-03-22 stsp free(abs_namespace);
1073 b2070a3f 2020-03-22 stsp free(buf);
1074 9bdd68dd 2020-01-02 naddy free(line);
1075 a04f49d2 2019-02-04 stsp free(path_refs);
1076 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1077 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1078 199a4027 2019-02-02 stsp return err;
1082 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1084 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1086 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1087 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1088 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1094 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1096 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1099 b249b824 2019-05-09 stsp const struct got_error *
1100 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1102 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1103 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1105 66107227 2023-01-31 thomas memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1106 b249b824 2019-05-09 stsp return NULL;
1109 9e672c74 2019-03-11 stsp const struct got_error *
1110 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1112 b249b824 2019-05-09 stsp char *new_name;
1114 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1115 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1117 b249b824 2019-05-09 stsp new_name = strdup(refname);
1118 b249b824 2019-05-09 stsp if (new_name == NULL)
1119 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1121 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1122 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1123 b249b824 2019-05-09 stsp return NULL;
1126 b249b824 2019-05-09 stsp const struct got_error *
1127 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1128 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1130 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1131 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1133 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1134 66107227 2023-01-31 thomas memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1135 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1136 e8a967e0 2020-03-21 stsp return NULL;
1139 e8a967e0 2020-03-21 stsp const struct got_error *
1140 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1142 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1143 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1144 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1145 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1146 9e672c74 2019-03-11 stsp FILE *f = NULL;
1148 9e672c74 2019-03-11 stsp struct stat sb;
1150 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1151 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1152 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1153 9e672c74 2019-03-11 stsp goto done;
1156 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1157 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1158 9e672c74 2019-03-11 stsp goto done;
1161 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1162 49c7094f 2019-03-11 stsp if (err) {
1163 d1667f0d 2019-03-11 stsp char *parent;
1164 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1165 5e1c9f23 2019-03-11 stsp goto done;
1166 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1168 0cd1c46a 2019-03-11 stsp goto done;
1169 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1170 5e1c9f23 2019-03-11 stsp free(parent);
1172 0cd1c46a 2019-03-11 stsp goto done;
1173 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1175 0cd1c46a 2019-03-11 stsp goto done;
1178 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1179 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1180 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1181 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1182 9e672c74 2019-03-11 stsp goto done;
1185 66107227 2023-01-31 thomas char *hex;
1186 66107227 2023-01-31 thomas size_t len;
1188 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &ref->ref.ref.id);
1189 66107227 2023-01-31 thomas if (err)
1190 66107227 2023-01-31 thomas goto done;
1191 66107227 2023-01-31 thomas len = strlen(hex);
1192 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1193 66107227 2023-01-31 thomas free(hex);
1194 66107227 2023-01-31 thomas if (n != len + 1) {
1195 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1196 9e672c74 2019-03-11 stsp goto done;
1200 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1201 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1203 2f17228e 2019-05-12 stsp goto done;
1206 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1208 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1209 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1211 0cd1c46a 2019-03-11 stsp goto done;
1213 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1216 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1217 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1218 3818e3c4 2020-11-01 naddy goto done;
1221 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1222 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1223 9e672c74 2019-03-11 stsp goto done;
1225 9e672c74 2019-03-11 stsp free(tmppath);
1226 9e672c74 2019-03-11 stsp tmppath = NULL;
1228 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1229 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1230 3f338f0a 2021-07-27 stsp goto done;
1232 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1234 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1235 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1237 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1238 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1240 9e672c74 2019-03-11 stsp free(path_refs);
1241 9e672c74 2019-03-11 stsp free(path);
1242 9e672c74 2019-03-11 stsp if (tmppath) {
1243 e6b88e16 2022-10-16 thomas if (unlink(tmppath) == -1 && err == NULL)
1244 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1245 9e672c74 2019-03-11 stsp free(tmppath);
1247 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1250 598a8b91 2019-03-15 stsp static const struct got_error *
1251 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1253 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1254 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1255 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1256 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1257 7a90b680 2020-01-02 naddy size_t linesize = 0;
1258 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1259 598a8b91 2019-03-15 stsp int found_delref = 0;
1261 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1262 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1263 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1265 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1267 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1268 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1269 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1271 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1273 598a8b91 2019-03-15 stsp goto done;
1275 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1276 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1278 2f17228e 2019-05-12 stsp goto done;
1281 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1282 598a8b91 2019-03-15 stsp if (f == NULL) {
1283 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1284 598a8b91 2019-03-15 stsp goto done;
1287 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1288 598a8b91 2019-03-15 stsp struct got_reference *ref;
1289 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1291 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1292 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1293 598a8b91 2019-03-15 stsp if (feof(f))
1295 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1296 598a8b91 2019-03-15 stsp goto done;
1298 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1299 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1300 f4204d57 2023-03-01 thomas err = parse_packed_ref_line(&ref, NULL, line, 0,
1301 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
1303 598a8b91 2019-03-15 stsp goto done;
1304 598a8b91 2019-03-15 stsp if (ref == NULL)
1307 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1308 66107227 2023-01-31 thomas got_object_id_cmp(&ref->ref.ref.id,
1309 66107227 2023-01-31 thomas &delref->ref.ref.id) == 0) {
1310 598a8b91 2019-03-15 stsp found_delref = 1;
1311 598a8b91 2019-03-15 stsp got_ref_close(ref);
1315 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1316 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1317 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1318 598a8b91 2019-03-15 stsp got_ref_close(ref);
1320 598a8b91 2019-03-15 stsp goto done;
1323 598a8b91 2019-03-15 stsp if (found_delref) {
1324 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1326 598a8b91 2019-03-15 stsp struct stat sb;
1328 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1329 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1330 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1331 598a8b91 2019-03-15 stsp goto done;
1334 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1335 66107227 2023-01-31 thomas char *hex;
1336 66107227 2023-01-31 thomas size_t len;
1338 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1339 66107227 2023-01-31 thomas if (err)
1340 598a8b91 2019-03-15 stsp goto done;
1341 66107227 2023-01-31 thomas len = strlen(hex);
1342 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1343 66107227 2023-01-31 thomas free(hex);
1344 66107227 2023-01-31 thomas if (n != len + 1) {
1345 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1346 598a8b91 2019-03-15 stsp goto done;
1349 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1350 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1351 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1352 598a8b91 2019-03-15 stsp goto done;
1356 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1357 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1358 598a8b91 2019-03-15 stsp goto done;
1361 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1362 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1363 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1364 230a42bd 2019-05-11 jcs packed_refs_path);
1365 598a8b91 2019-03-15 stsp goto done;
1367 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1370 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1371 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1372 598a8b91 2019-03-15 stsp goto done;
1375 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1376 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1377 230a42bd 2019-05-11 jcs packed_refs_path);
1378 598a8b91 2019-03-15 stsp goto done;
1380 f96b13c8 2022-10-16 thomas free(tmppath);
1381 f96b13c8 2022-10-16 thomas tmppath = NULL;
1384 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1385 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1387 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1388 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1390 f96b13c8 2022-10-16 thomas if (tmppath && unlink(tmppath) == -1 && err == NULL)
1391 f96b13c8 2022-10-16 thomas err = got_error_from_errno2("unlink", tmppath);
1392 f96b13c8 2022-10-16 thomas if (tmpf && fclose(tmpf) == EOF && err == NULL)
1393 f96b13c8 2022-10-16 thomas err = got_error_from_errno("fclose");
1394 598a8b91 2019-03-15 stsp free(tmppath);
1395 598a8b91 2019-03-15 stsp free(packed_refs_path);
1396 9bdd68dd 2020-01-02 naddy free(line);
1397 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1398 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1401 2a104ff6 2020-09-21 stsp static const struct got_error *
1402 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1404 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1405 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1406 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1407 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1409 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1410 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1411 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1412 2d2e1378 2019-03-11 stsp goto done;
1415 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1416 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1417 2d2e1378 2019-03-11 stsp goto done;
1420 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1421 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1423 2f17228e 2019-05-12 stsp goto done;
1426 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1428 e6b88e16 2022-10-16 thomas if (unlink(path) == -1)
1429 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1431 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1432 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1434 2d2e1378 2019-03-11 stsp free(path_refs);
1435 2d2e1378 2019-03-11 stsp free(path);
1436 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1439 2a104ff6 2020-09-21 stsp const struct got_error *
1440 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1442 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1443 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1445 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1446 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1448 2a104ff6 2020-09-21 stsp return err;
1450 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1451 2a104ff6 2020-09-21 stsp if (err) {
1452 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1453 2a104ff6 2020-09-21 stsp return NULL;
1454 2a104ff6 2020-09-21 stsp return err;
1457 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1458 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1459 2a104ff6 2020-09-21 stsp return err;
1461 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1463 2a104ff6 2020-09-21 stsp return err;
1465 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1466 2a104ff6 2020-09-21 stsp if (err) {
1467 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1468 2a104ff6 2020-09-21 stsp return NULL;
1469 2a104ff6 2020-09-21 stsp return err;
1472 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1473 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1474 2a104ff6 2020-09-21 stsp return err;
1478 2f17228e 2019-05-12 stsp const struct got_error *
1479 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1481 2f17228e 2019-05-12 stsp const struct got_error *err;
1482 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1483 2f17228e 2019-05-12 stsp ref->lf = NULL;
1484 2f17228e 2019-05-12 stsp return err;
1487 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1488 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1491 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1492 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1495 24202e46 2020-12-26 stsp static const struct got_error *
1496 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1497 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1499 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1500 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1501 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1503 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1504 24202e46 2020-12-26 stsp if (ent == NULL) {
1505 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1506 24202e46 2020-12-26 stsp if (ent == NULL)
1507 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1509 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1510 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1511 ee7b409c 2022-04-16 thomas if (err) {
1512 ee7b409c 2022-04-16 thomas free(ent);
1513 24202e46 2020-12-26 stsp return err;
1517 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1519 24202e46 2020-12-26 stsp return err;
1521 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1522 24202e46 2020-12-26 stsp return NULL;
1525 7b5b670e 2020-12-25 stsp const struct got_error *
1526 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1527 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1529 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1530 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1531 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1532 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1534 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1535 7b5b670e 2020-12-25 stsp if (idset == NULL)
1536 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1538 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1539 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1540 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1541 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1543 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1545 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1546 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1548 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1550 7b5b670e 2020-12-25 stsp goto done;
1552 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1554 24202e46 2020-12-26 stsp goto done;
1556 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1558 24202e46 2020-12-26 stsp id = NULL;
1562 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1563 24202e46 2020-12-26 stsp if (err) {
1564 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1565 7b5b670e 2020-12-25 stsp goto done;
1566 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1567 24202e46 2020-12-26 stsp err = NULL;
1568 24202e46 2020-12-26 stsp tag = NULL;
1570 24202e46 2020-12-26 stsp id = NULL;
1574 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1575 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1576 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1578 7b5b670e 2020-12-25 stsp goto done;
1581 7b5b670e 2020-12-25 stsp id = NULL;
1585 7b5b670e 2020-12-25 stsp if (err) {
1586 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1587 7b5b670e 2020-12-25 stsp *map = NULL;
1589 a53af95f 2020-12-26 stsp return err;
1592 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1593 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1594 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1596 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1597 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1599 7b5b670e 2020-12-25 stsp return &ent->refs;
1600 7b5b670e 2020-12-25 stsp return NULL;
1603 7b5b670e 2020-12-25 stsp static const struct got_error *
1604 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1606 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1608 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1609 7b5b670e 2020-12-25 stsp free(ent);
1610 7b5b670e 2020-12-25 stsp return NULL;
1614 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1616 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1617 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1618 7b5b670e 2020-12-25 stsp free(map);