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 9e672c74 2019-03-11 stsp #include <sys/stat.h>
20 0fd469ce 2019-03-11 stsp #include <errno.h>
21 f77a24b0 2019-03-11 stsp #include <ctype.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 <stdio.h>
25 5261c201 2018-04-01 stsp #include <stdlib.h>
26 5261c201 2018-04-01 stsp #include <string.h>
27 81a12da5 2020-09-09 naddy #include <unistd.h>
28 5261c201 2018-04-01 stsp #include <zlib.h>
29 788c352e 2018-06-16 stsp #include <time.h>
30 0cd1c46a 2019-03-11 stsp #include <libgen.h>
32 dd038bc6 2021-09-21 thomas.ad #include "got_compat.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 5261c201 2018-04-01 stsp #include "got_lib_sha1.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 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
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;
91 fd3a2fe6 2021-10-12 thomas /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
92 fd3a2fe6 2021-10-12 thomas time_t committer_time;
95 5261c201 2018-04-01 stsp static const struct got_error *
96 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
97 3f338f0a 2021-07-27 stsp struct got_object_id *id, int flags, time_t mtime)
99 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
101 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
102 f9267c9a 2019-03-15 stsp if (*ref == NULL)
103 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
105 80d5fc1f 2019-03-15 stsp memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
106 c980e470 2019-03-15 stsp (*ref)->flags = flags;
107 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
108 3f338f0a 2021-07-27 stsp (*ref)->mtime = mtime;
109 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
110 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
111 c980e470 2019-03-15 stsp got_ref_close(*ref);
112 f9267c9a 2019-03-15 stsp *ref = NULL;
114 f9267c9a 2019-03-15 stsp return err;
117 f9267c9a 2019-03-15 stsp static const struct got_error *
118 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
119 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
121 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
123 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
124 5261c201 2018-04-01 stsp if (*ref == NULL)
125 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
127 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
128 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
129 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
130 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
131 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
132 f9267c9a 2019-03-15 stsp *ref = NULL;
133 cdb8f1fa 2019-08-28 hiltjo return err;
135 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
136 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
137 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
138 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
139 f9267c9a 2019-03-15 stsp *ref = NULL;
141 f9267c9a 2019-03-15 stsp return err;
144 5261c201 2018-04-01 stsp static const struct got_error *
145 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
147 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
148 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
150 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
153 f9267c9a 2019-03-15 stsp static const struct got_error *
154 3f338f0a 2021-07-27 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line,
155 3f338f0a 2021-07-27 stsp time_t mtime)
157 5892cdd6 2019-03-10 stsp struct got_object_id id;
159 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
161 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
164 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
165 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
167 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, 0, mtime);
170 5261c201 2018-04-01 stsp static const struct got_error *
171 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
172 b2070a3f 2020-03-22 stsp const char *absname, const char *abspath, int lock)
174 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
176 c30018ad 2019-10-21 stsp char *line = NULL;
177 c30018ad 2019-10-21 stsp size_t linesize = 0;
178 c30018ad 2019-10-21 stsp ssize_t linelen;
179 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
180 3f338f0a 2021-07-27 stsp struct stat sb;
182 2f17228e 2019-05-12 stsp if (lock) {
183 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, abspath, -1);
185 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
186 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
187 9f142382 2020-03-21 stsp return err;
191 c56c5d8a 2021-12-31 thomas f = fopen(abspath, "rbe");
192 f5c58ad1 2019-05-12 stsp if (f == NULL) {
193 b2070a3f 2020-03-22 stsp if (errno != ENOTDIR && errno != ENOENT)
194 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("fopen", abspath);
196 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
198 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
199 b2070a3f 2020-03-22 stsp return err;
201 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
202 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", abspath);
206 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
207 c30018ad 2019-10-21 stsp if (linelen == -1) {
208 c30018ad 2019-10-21 stsp if (feof(f))
209 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
211 b2070a3f 2020-03-22 stsp if (errno == EISDIR)
212 b2070a3f 2020-03-22 stsp err = got_error(GOT_ERR_NOT_REF);
213 b2070a3f 2020-03-22 stsp else if (ferror(f))
214 b2070a3f 2020-03-22 stsp err = got_ferror(f, GOT_ERR_IO);
216 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
219 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
222 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
223 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
227 3f338f0a 2021-07-27 stsp err = parse_ref_line(ref, absname, line, sb.st_mtime);
228 f5c58ad1 2019-05-12 stsp if (lock) {
230 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
233 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
235 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
239 5261c201 2018-04-01 stsp free(line);
240 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL) {
241 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
242 2f17228e 2019-05-12 stsp if (*ref) {
244 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
245 2f17228e 2019-05-12 stsp got_ref_close(*ref);
246 2f17228e 2019-05-12 stsp *ref = NULL;
249 5261c201 2018-04-01 stsp return err;
253 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
255 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
256 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
257 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
258 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
261 c5f754cc 2019-02-01 stsp static char *
262 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
264 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
265 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
267 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
271 63e5aa5c 2021-08-23 stsp got_ref_name_is_valid(const char *name)
273 7fa81f88 2020-02-21 stsp const char *s, *seg;
274 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
275 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
276 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
277 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
280 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
285 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
287 f77a24b0 2019-03-11 stsp while (*s) {
288 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
289 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
292 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
293 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
294 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
297 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
299 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
301 f77a24b0 2019-03-11 stsp if (*s == '/') {
302 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
303 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
304 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
306 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
307 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
309 f77a24b0 2019-03-11 stsp seg = nextseg;
314 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
315 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
321 5892cdd6 2019-03-10 stsp const struct got_error *
322 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
323 5892cdd6 2019-03-10 stsp struct got_object_id *id)
325 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
326 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, id, 0, 0);
331 aaf88317 2019-07-10 stsp const struct got_error *
332 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
333 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
335 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
336 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
338 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
341 d1f2edc9 2018-06-13 stsp static const struct got_error *
342 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
343 3f338f0a 2021-07-27 stsp const char *line, time_t mtime)
345 5892cdd6 2019-03-10 stsp struct got_object_id id;
346 5892cdd6 2019-03-10 stsp const char *name;
348 fb79db15 2019-02-01 stsp *ref = NULL;
350 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
351 fb79db15 2019-02-01 stsp return NULL;
353 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
354 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
356 199a4027 2019-02-02 stsp if (abs_refname) {
357 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
358 199a4027 2019-02-02 stsp return NULL;
359 5892cdd6 2019-03-10 stsp name = abs_refname;
361 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
363 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
366 fb79db15 2019-02-01 stsp static const struct got_error *
367 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
368 3f338f0a 2021-07-27 stsp int nsubdirs, const char *refname, time_t mtime)
370 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
371 fb79db15 2019-02-01 stsp char *abs_refname;
372 9bdd68dd 2020-01-02 naddy char *line = NULL;
373 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
374 9bdd68dd 2020-01-02 naddy ssize_t linelen;
375 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
377 1e37702e 2019-02-04 stsp *ref = NULL;
379 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
380 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
382 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
383 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
384 7ab0422a 2019-03-15 stsp if (feof(f))
386 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
389 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
390 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
391 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
392 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
393 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
394 0dec1cc0 2019-02-01 stsp refname) == -1)
395 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
396 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(ref, abs_refname, line,
398 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
399 0dec1cc0 2019-02-01 stsp free(abs_refname);
400 532920c8 2019-02-01 stsp if (err || *ref != NULL)
405 fb79db15 2019-02-01 stsp } while (*ref == NULL);
406 9bdd68dd 2020-01-02 naddy free(line);
408 fb79db15 2019-02-01 stsp return err;
411 fb79db15 2019-02-01 stsp static const struct got_error *
412 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
413 2f17228e 2019-05-12 stsp const char *name, int lock)
415 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
416 a04f49d2 2019-02-04 stsp char *path = NULL;
417 a04f49d2 2019-02-04 stsp char *absname = NULL;
418 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
419 75236079 2020-03-25 stsp int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
421 1e37702e 2019-02-04 stsp *ref = NULL;
423 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
424 57c18198 2021-05-24 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
426 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
427 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
428 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
429 a04f49d2 2019-02-04 stsp absname = (char *)name;
431 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
432 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
433 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
435 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
436 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
437 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
442 b2070a3f 2020-03-22 stsp err = parse_ref_file(ref, name, absname, path, lock);
444 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
445 a04f49d2 2019-02-04 stsp free(absname);
446 a04f49d2 2019-02-04 stsp free(path);
447 d1f2edc9 2018-06-13 stsp return err;
450 5261c201 2018-04-01 stsp const struct got_error *
451 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
452 abc59930 2021-09-05 naddy const char *refname, int lock)
454 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
455 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
456 fb79db15 2019-02-01 stsp const char *subdirs[] = {
457 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
460 16aeacf7 2020-11-26 stsp int well_known = is_well_known_ref(refname);
461 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
463 1e37702e 2019-02-04 stsp *ref = NULL;
465 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
466 e135804e 2019-02-10 stsp if (path_refs == NULL) {
467 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
471 0885ce8f 2019-05-12 stsp if (well_known) {
472 0885ce8f 2019-05-12 stsp err = open_ref(ref, path_refs, "", refname, lock);
474 c5f754cc 2019-02-01 stsp char *packed_refs_path;
477 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
478 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
479 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
481 b2070a3f 2020-03-22 stsp if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
485 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
486 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
487 638f9024 2019-05-13 stsp err = got_error_from_errno(
488 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
492 2f17228e 2019-05-12 stsp if (lock) {
493 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
497 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "rbe");
498 c5f754cc 2019-02-01 stsp free(packed_refs_path);
499 c5f754cc 2019-02-01 stsp if (f != NULL) {
500 3f338f0a 2021-07-27 stsp struct stat sb;
501 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
502 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat",
503 3f338f0a 2021-07-27 stsp packed_refs_path);
506 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
507 3f338f0a 2021-07-27 stsp refname, sb.st_mtime);
508 a875589a 2019-05-12 stsp if (!err) {
509 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
510 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
511 a875589a 2019-05-12 stsp got_ref_close(*ref);
512 a875589a 2019-05-12 stsp *ref = NULL;
513 6e472abb 2019-05-13 stsp } else if (*ref)
514 a875589a 2019-05-12 stsp (*ref)->lf = lf;
519 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
520 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
521 a875589a 2019-05-12 stsp if (err && lf)
522 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
523 5261c201 2018-04-01 stsp free(path_refs);
524 5261c201 2018-04-01 stsp return err;
528 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
530 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
531 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
532 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
534 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
538 5261c201 2018-04-01 stsp struct got_reference *
539 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
541 5261c201 2018-04-01 stsp struct got_reference *ret;
543 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
544 5261c201 2018-04-01 stsp if (ret == NULL)
545 5261c201 2018-04-01 stsp return NULL;
547 5261c201 2018-04-01 stsp ret->flags = ref->flags;
548 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
549 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
550 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
552 5261c201 2018-04-01 stsp return NULL;
554 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
555 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
556 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
558 5261c201 2018-04-01 stsp return NULL;
561 4c4ce67b 2020-12-25 stsp ret->ref.ref.name = strdup(ref->ref.ref.name);
562 4c4ce67b 2020-12-25 stsp if (ret->ref.ref.name == NULL) {
564 5261c201 2018-04-01 stsp return NULL;
566 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
567 80d5fc1f 2019-03-15 stsp sizeof(ret->ref.ref.sha1));
570 5261c201 2018-04-01 stsp return ret;
573 b8bad2ba 2019-08-23 stsp const struct got_error *
574 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
575 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
577 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
578 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
580 b8bad2ba 2019-08-23 stsp *newp = NULL;
582 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
583 b8bad2ba 2019-08-23 stsp if (new == NULL)
584 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
586 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
587 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
588 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
590 b8bad2ba 2019-08-23 stsp return err;
593 b8bad2ba 2019-08-23 stsp *newp = new;
594 b8bad2ba 2019-08-23 stsp return NULL;
597 cce2f485 2021-08-22 stsp const struct got_error *
598 cce2f485 2021-08-22 stsp got_ref_resolve_symbolic(struct got_reference **resolved,
599 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
601 5261c201 2018-04-01 stsp struct got_reference *nextref;
602 5261c201 2018-04-01 stsp const struct got_error *err;
604 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
606 5261c201 2018-04-01 stsp return err;
608 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
609 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(resolved, repo, nextref);
611 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
613 5261c201 2018-04-01 stsp got_ref_close(nextref);
614 5261c201 2018-04-01 stsp return err;
617 29e86f7a 2019-08-12 stsp static const struct got_error *
618 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
621 5261c201 2018-04-01 stsp const struct got_error *err;
623 29e86f7a 2019-08-12 stsp if (recursion <= 0)
624 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
625 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
627 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
628 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
629 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(&resolved, repo, ref);
630 5261c201 2018-04-01 stsp if (err == NULL)
631 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
632 57b6f99a 2019-04-13 stsp if (resolved)
633 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
634 5261c201 2018-04-01 stsp return err;
637 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
638 5261c201 2018-04-01 stsp if (*id == NULL)
639 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
640 80d5fc1f 2019-03-15 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
641 5261c201 2018-04-01 stsp return NULL;
644 29e86f7a 2019-08-12 stsp const struct got_error *
645 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
646 29e86f7a 2019-08-12 stsp struct got_reference *ref)
648 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
652 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
656 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
657 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
659 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
660 271d2a38 2018-12-25 stsp if (str == NULL)
661 271d2a38 2018-12-25 stsp return NULL;
663 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
664 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
666 271d2a38 2018-12-25 stsp return NULL;
669 5261c201 2018-04-01 stsp return str;
672 0bd18d37 2019-02-01 stsp const char *
673 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
675 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
676 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
678 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
681 aaf88317 2019-07-10 stsp const char *
682 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
684 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
685 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
687 b8bad2ba 2019-08-23 stsp return NULL;
691 3f338f0a 2021-07-27 stsp got_ref_get_mtime(struct got_reference *ref)
693 3f338f0a 2021-07-27 stsp return ref->mtime;
696 b8bad2ba 2019-08-23 stsp const struct got_error *
697 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
698 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
700 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
701 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
703 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
704 aaf88317 2019-07-10 stsp return NULL;
707 d1f16636 2020-01-15 stsp const struct got_error *
708 d1f16636 2020-01-15 stsp got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
709 d1f16636 2020-01-15 stsp struct got_reference *ref2)
711 d1f16636 2020-01-15 stsp const struct got_error *err = NULL;
712 d1f16636 2020-01-15 stsp struct got_repository *repo = arg;
713 d1f16636 2020-01-15 stsp struct got_object_id *id1, *id2 = NULL;
714 d1f16636 2020-01-15 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
715 d1f16636 2020-01-15 stsp struct got_commit_object *commit1 = NULL, *commit2 = NULL;
716 d1f16636 2020-01-15 stsp time_t time1, time2;
720 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id1, repo, ref1);
722 d1f16636 2020-01-15 stsp return err;
723 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag1, repo, id1);
725 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
727 d1f16636 2020-01-15 stsp /* "lightweight" tag */
728 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit1, repo, id1);
731 d1f16636 2020-01-15 stsp time1 = got_object_commit_get_committer_time(commit1);
733 d1f16636 2020-01-15 stsp time1 = got_object_tag_get_tagger_time(tag1);
735 d1f16636 2020-01-15 stsp err = got_ref_resolve(&id2, repo, ref2);
738 d1f16636 2020-01-15 stsp err = got_object_open_as_tag(&tag2, repo, id2);
740 d1f16636 2020-01-15 stsp if (err->code != GOT_ERR_OBJ_TYPE)
742 d1f16636 2020-01-15 stsp /* "lightweight" tag */
743 d1f16636 2020-01-15 stsp err = got_object_open_as_commit(&commit2, repo, id2);
746 d1f16636 2020-01-15 stsp time2 = got_object_commit_get_committer_time(commit2);
748 d1f16636 2020-01-15 stsp time2 = got_object_tag_get_tagger_time(tag2);
750 d1f16636 2020-01-15 stsp /* Put latest tags first. */
751 d1f16636 2020-01-15 stsp if (time1 < time2)
753 d1f16636 2020-01-15 stsp else if (time1 > time2)
756 d1f16636 2020-01-15 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
761 d1f16636 2020-01-15 stsp got_object_tag_close(tag1);
763 d1f16636 2020-01-15 stsp got_object_tag_close(tag2);
764 e600f124 2021-03-21 stsp if (commit1)
765 e600f124 2021-03-21 stsp got_object_commit_close(commit1);
766 e600f124 2021-03-21 stsp if (commit2)
767 e600f124 2021-03-21 stsp got_object_commit_close(commit2);
768 3bfadbd4 2021-11-20 thomas return err;
771 3bfadbd4 2021-11-20 thomas static const struct got_error *
772 3bfadbd4 2021-11-20 thomas get_committer_time(struct got_reference *ref, struct got_repository *repo)
774 3bfadbd4 2021-11-20 thomas const struct got_error *err = NULL;
775 3bfadbd4 2021-11-20 thomas int obj_type;
776 3bfadbd4 2021-11-20 thomas struct got_commit_object *commit = NULL;
777 3bfadbd4 2021-11-20 thomas struct got_tag_object *tag = NULL;
778 3bfadbd4 2021-11-20 thomas struct got_object_id *id = NULL;
780 3bfadbd4 2021-11-20 thomas err = got_ref_resolve(&id, repo, ref);
782 3bfadbd4 2021-11-20 thomas return err;
784 3bfadbd4 2021-11-20 thomas err = got_object_get_type(&obj_type, repo, id);
786 3bfadbd4 2021-11-20 thomas goto done;
788 3bfadbd4 2021-11-20 thomas switch (obj_type) {
789 3bfadbd4 2021-11-20 thomas case GOT_OBJ_TYPE_COMMIT:
790 3bfadbd4 2021-11-20 thomas err = got_object_open_as_commit(&commit, repo, id);
792 3bfadbd4 2021-11-20 thomas goto done;
793 3bfadbd4 2021-11-20 thomas ref->committer_time =
794 3bfadbd4 2021-11-20 thomas got_object_commit_get_committer_time(commit);
796 3bfadbd4 2021-11-20 thomas case GOT_OBJ_TYPE_TAG:
797 3bfadbd4 2021-11-20 thomas err = got_object_open_as_tag(&tag, repo, id);
799 3bfadbd4 2021-11-20 thomas goto done;
800 3bfadbd4 2021-11-20 thomas ref->committer_time = got_object_tag_get_tagger_time(tag);
803 3bfadbd4 2021-11-20 thomas /* best effort for other object types */
804 3bfadbd4 2021-11-20 thomas ref->committer_time = got_ref_get_mtime(ref);
808 3bfadbd4 2021-11-20 thomas free(id);
809 3bfadbd4 2021-11-20 thomas if (commit)
810 3bfadbd4 2021-11-20 thomas got_object_commit_close(commit);
812 3bfadbd4 2021-11-20 thomas got_object_tag_close(tag);
813 e600f124 2021-03-21 stsp return err;
816 e600f124 2021-03-21 stsp const struct got_error *
817 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
818 e600f124 2021-03-21 stsp struct got_reference *ref1, struct got_reference *ref2)
820 e87a53ff 2021-11-20 thomas const struct got_error *err = NULL;
821 e600f124 2021-03-21 stsp struct got_repository *repo = arg;
825 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time == 0) {
826 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref1, repo);
828 fd3a2fe6 2021-10-12 thomas return err;
830 3bfadbd4 2021-11-20 thomas if (ref2->committer_time == 0) {
831 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref2, repo);
833 fd3a2fe6 2021-10-12 thomas return err;
836 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time < ref2->committer_time)
838 fd3a2fe6 2021-10-12 thomas else if (ref2->committer_time < ref1->committer_time)
841 a729e288 2021-11-20 thomas return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
843 d1f16636 2020-01-15 stsp return err;
846 779e1159 2021-06-18 stsp const struct got_error *
847 48b4f239 2021-12-31 thomas got_reflist_insert(struct got_reflist_entry **newp,
848 48b4f239 2021-12-31 thomas struct got_reflist_head *refs, struct got_reference *ref,
849 48b4f239 2021-12-31 thomas got_ref_cmp_cb cmp_cb, void *cmp_arg)
851 199a4027 2019-02-02 stsp const struct got_error *err;
852 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
855 505287be 2019-03-15 stsp *newp = NULL;
857 6fdbf7b0 2019-03-15 stsp new = malloc(sizeof(*new));
858 48cae60d 2020-09-22 stsp if (new == NULL)
859 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
860 7a3c76f5 2019-02-05 stsp new->ref = ref;
861 505287be 2019-03-15 stsp *newp = new;
864 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
865 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
866 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
867 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
869 d9dff0e5 2020-12-26 stsp * Many callers will provide paths in a somewhat sorted order.
870 d9dff0e5 2020-12-26 stsp * Iterating backwards from the tail of the list should be more
871 d9dff0e5 2020-12-26 stsp * efficient than traversing through the entire list each time
872 d9dff0e5 2020-12-26 stsp * an element is inserted.
874 d9dff0e5 2020-12-26 stsp re = TAILQ_LAST(refs, got_reflist_head);
875 7a3c76f5 2019-02-05 stsp while (re) {
876 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
878 b8bad2ba 2019-08-23 stsp return err;
879 e397b6db 2019-02-04 stsp if (cmp == 0) {
880 27a1ed03 2019-03-15 stsp /* duplicate */
882 505287be 2019-03-15 stsp *newp = NULL;
883 7a3c76f5 2019-02-05 stsp return NULL;
884 d9dff0e5 2020-12-26 stsp } else if (cmp < 0) {
885 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_AFTER(refs, re, new, entry);
886 7a3c76f5 2019-02-05 stsp return NULL;
888 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
891 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
892 199a4027 2019-02-02 stsp return NULL;
895 fb95e70d 2021-11-20 thomas const struct got_error *
896 fb95e70d 2021-11-20 thomas got_reflist_sort(struct got_reflist_head *refs,
897 fb95e70d 2021-11-20 thomas got_ref_cmp_cb cmp_cb, void *cmp_arg)
899 fb95e70d 2021-11-20 thomas const struct got_error *err = NULL;
900 fb95e70d 2021-11-20 thomas struct got_reflist_entry *re, *tmp, *new;
901 fb95e70d 2021-11-20 thomas struct got_reflist_head sorted;
903 fb95e70d 2021-11-20 thomas TAILQ_INIT(&sorted);
905 fb95e70d 2021-11-20 thomas TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
906 fb95e70d 2021-11-20 thomas struct got_reference *ref = re->ref;
907 fb95e70d 2021-11-20 thomas TAILQ_REMOVE(refs, re, entry);
908 fb95e70d 2021-11-20 thomas free(re);
909 fb95e70d 2021-11-20 thomas err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
910 fb95e70d 2021-11-20 thomas if (err || new == NULL /* duplicate */)
911 fb95e70d 2021-11-20 thomas got_ref_close(ref);
913 fb95e70d 2021-11-20 thomas return err;
916 fb95e70d 2021-11-20 thomas TAILQ_CONCAT(refs, &sorted, entry);
917 fb95e70d 2021-11-20 thomas return NULL;
920 a04f49d2 2019-02-04 stsp static const struct got_error *
921 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
922 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
923 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
925 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
926 a04f49d2 2019-02-04 stsp DIR *d = NULL;
927 a04f49d2 2019-02-04 stsp char *path_subdir;
929 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
932 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
933 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
935 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
936 a04f49d2 2019-02-04 stsp if (d == NULL)
940 a04f49d2 2019-02-04 stsp struct dirent *dent;
941 a04f49d2 2019-02-04 stsp struct got_reference *ref;
942 a04f49d2 2019-02-04 stsp char *child;
945 a04f49d2 2019-02-04 stsp dent = readdir(d);
946 a04f49d2 2019-02-04 stsp if (dent == NULL)
949 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
950 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
953 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
957 20ccae39 2020-07-21 stsp switch (type) {
958 a04f49d2 2019-02-04 stsp case DT_REG:
959 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
964 505287be 2019-03-15 stsp struct got_reflist_entry *new;
965 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
966 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
967 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
968 505287be 2019-03-15 stsp got_ref_close(ref);
973 a04f49d2 2019-02-04 stsp case DT_DIR:
974 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
975 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
976 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
979 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
980 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
981 a04f49d2 2019-02-04 stsp free(child);
989 a04f49d2 2019-02-04 stsp closedir(d);
990 a04f49d2 2019-02-04 stsp free(path_subdir);
991 a04f49d2 2019-02-04 stsp return err;
994 199a4027 2019-02-02 stsp const struct got_error *
995 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
996 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
998 199a4027 2019-02-02 stsp const struct got_error *err;
999 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
1000 0c6f49ba 2022-07-01 thomas char *abs_namespace = NULL, *buf = NULL;
1001 0c6f49ba 2022-07-01 thomas const char *ondisk_ref_namespace = NULL;
1002 9bdd68dd 2020-01-02 naddy char *line = NULL;
1003 29b5c214 2019-02-04 stsp FILE *f = NULL;
1004 199a4027 2019-02-02 stsp struct got_reference *ref;
1005 505287be 2019-03-15 stsp struct got_reflist_entry *new;
1007 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1008 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1009 29606af7 2019-08-23 stsp if (path_refs == NULL) {
1010 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
1011 29606af7 2019-08-23 stsp goto done;
1013 29606af7 2019-08-23 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1015 29606af7 2019-08-23 stsp goto done;
1016 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1017 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
1018 29606af7 2019-08-23 stsp got_ref_close(ref);
1019 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
1020 29606af7 2019-08-23 stsp goto done;
1022 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
1023 b2070a3f 2020-03-22 stsp const char *refname = ref_namespace;
1024 b2070a3f 2020-03-22 stsp path_refs = get_refs_dir_path(repo, refname);
1025 b2070a3f 2020-03-22 stsp if (path_refs == NULL) {
1026 b2070a3f 2020-03-22 stsp err = got_error_from_errno("get_refs_dir_path");
1027 b2070a3f 2020-03-22 stsp goto done;
1029 b2070a3f 2020-03-22 stsp err = open_ref(&ref, path_refs, "", refname, 0);
1030 b2070a3f 2020-03-22 stsp if (err) {
1031 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
1032 b2070a3f 2020-03-22 stsp goto done;
1033 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
1035 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1036 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
1037 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
1038 b2070a3f 2020-03-22 stsp got_ref_close(ref);
1039 b2070a3f 2020-03-22 stsp return err;
1043 b2070a3f 2020-03-22 stsp if (ref_namespace) {
1044 b2070a3f 2020-03-22 stsp size_t len;
1045 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
1046 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1047 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
1048 b2070a3f 2020-03-22 stsp goto done;
1050 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
1051 b2070a3f 2020-03-22 stsp buf = malloc(len);
1052 b2070a3f 2020-03-22 stsp if (buf == NULL) {
1053 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
1054 b2070a3f 2020-03-22 stsp goto done;
1056 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
1058 b2070a3f 2020-03-22 stsp goto done;
1059 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
1060 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
1061 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
1062 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1063 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
1064 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1065 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1068 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1069 29b5c214 2019-02-04 stsp free(path_refs);
1070 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1071 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1073 29b5c214 2019-02-04 stsp goto done;
1075 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1076 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1077 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1079 29b5c214 2019-02-04 stsp goto done;
1082 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1083 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1085 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1086 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1087 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1088 29b5c214 2019-02-04 stsp goto done;
1091 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1092 199a4027 2019-02-02 stsp free(packed_refs_path);
1094 7a90b680 2020-01-02 naddy size_t linesize = 0;
1095 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1096 3f338f0a 2021-07-27 stsp struct stat sb;
1098 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1099 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1100 3f338f0a 2021-07-27 stsp goto done;
1103 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1104 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1105 0bb4abae 2019-03-15 stsp if (feof(f))
1107 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1108 0bb4abae 2019-03-15 stsp goto done;
1110 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1111 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1112 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1113 3f338f0a 2021-07-27 stsp sb.st_mtime);
1115 199a4027 2019-02-02 stsp goto done;
1116 76b4ead2 2019-02-03 stsp if (ref) {
1117 29606af7 2019-08-23 stsp if (ref_namespace) {
1118 29606af7 2019-08-23 stsp const char *name;
1119 29606af7 2019-08-23 stsp name = got_ref_get_name(ref);
1120 b2070a3f 2020-03-22 stsp if (!got_path_is_child(name,
1121 b2070a3f 2020-03-22 stsp ref_namespace,
1122 b2070a3f 2020-03-22 stsp strlen(ref_namespace))) {
1123 29606af7 2019-08-23 stsp got_ref_close(ref);
1127 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1128 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1129 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1130 505287be 2019-03-15 stsp got_ref_close(ref);
1132 76b4ead2 2019-02-03 stsp goto done;
1137 b2070a3f 2020-03-22 stsp free(abs_namespace);
1138 b2070a3f 2020-03-22 stsp free(buf);
1139 9bdd68dd 2020-01-02 naddy free(line);
1140 a04f49d2 2019-02-04 stsp free(path_refs);
1141 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1142 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1143 199a4027 2019-02-02 stsp return err;
1147 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1149 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1151 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1152 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1153 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1160 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1162 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1165 b249b824 2019-05-09 stsp const struct got_error *
1166 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1168 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1169 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1171 b249b824 2019-05-09 stsp memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1172 b249b824 2019-05-09 stsp return NULL;
1175 9e672c74 2019-03-11 stsp const struct got_error *
1176 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1178 b249b824 2019-05-09 stsp char *new_name;
1180 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1181 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1183 b249b824 2019-05-09 stsp new_name = strdup(refname);
1184 b249b824 2019-05-09 stsp if (new_name == NULL)
1185 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1187 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1188 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1189 b249b824 2019-05-09 stsp return NULL;
1192 b249b824 2019-05-09 stsp const struct got_error *
1193 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1194 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1196 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1197 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1199 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1200 e8a967e0 2020-03-21 stsp memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1201 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1202 e8a967e0 2020-03-21 stsp return NULL;
1205 e8a967e0 2020-03-21 stsp const struct got_error *
1206 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1208 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1209 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1210 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1211 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1212 9e672c74 2019-03-11 stsp FILE *f = NULL;
1214 9e672c74 2019-03-11 stsp struct stat sb;
1216 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1217 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1218 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1219 9e672c74 2019-03-11 stsp goto done;
1222 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1223 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1224 9e672c74 2019-03-11 stsp goto done;
1227 9e672c74 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1228 49c7094f 2019-03-11 stsp if (err) {
1229 d1667f0d 2019-03-11 stsp char *parent;
1230 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1231 5e1c9f23 2019-03-11 stsp goto done;
1232 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1234 0cd1c46a 2019-03-11 stsp goto done;
1235 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1236 5e1c9f23 2019-03-11 stsp free(parent);
1238 0cd1c46a 2019-03-11 stsp goto done;
1239 0cd1c46a 2019-03-11 stsp err = got_opentemp_named(&tmppath, &f, path);
1241 0cd1c46a 2019-03-11 stsp goto done;
1244 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1245 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1246 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1247 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1248 9e672c74 2019-03-11 stsp goto done;
1251 9e672c74 2019-03-11 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1252 9e672c74 2019-03-11 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1253 9e672c74 2019-03-11 stsp sizeof(hex)) == NULL) {
1254 9e672c74 2019-03-11 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1255 9e672c74 2019-03-11 stsp goto done;
1257 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1258 8fa2f096 2019-03-11 stsp if (n != sizeof(hex)) {
1259 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1260 9e672c74 2019-03-11 stsp goto done;
1264 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1265 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1267 2f17228e 2019-05-12 stsp goto done;
1270 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1272 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1273 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1274 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1275 0cd1c46a 2019-03-11 stsp goto done;
1277 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1280 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1281 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1282 3818e3c4 2020-11-01 naddy goto done;
1285 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1286 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1287 9e672c74 2019-03-11 stsp goto done;
1289 9e672c74 2019-03-11 stsp free(tmppath);
1290 9e672c74 2019-03-11 stsp tmppath = NULL;
1292 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1293 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1294 3f338f0a 2021-07-27 stsp goto done;
1296 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1298 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1299 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1301 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1302 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1304 9e672c74 2019-03-11 stsp free(path_refs);
1305 9e672c74 2019-03-11 stsp free(path);
1306 9e672c74 2019-03-11 stsp if (tmppath) {
1307 9e672c74 2019-03-11 stsp if (unlink(tmppath) != 0 && err == NULL)
1308 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1309 9e672c74 2019-03-11 stsp free(tmppath);
1311 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1314 598a8b91 2019-03-15 stsp static const struct got_error *
1315 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1317 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1318 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1319 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1320 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1321 7a90b680 2020-01-02 naddy size_t linesize = 0;
1322 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1323 598a8b91 2019-03-15 stsp int found_delref = 0;
1325 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1326 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1327 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1329 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1331 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1332 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1333 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1335 598a8b91 2019-03-15 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1337 598a8b91 2019-03-15 stsp goto done;
1339 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1340 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1342 2f17228e 2019-05-12 stsp goto done;
1345 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1346 598a8b91 2019-03-15 stsp if (f == NULL) {
1347 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1348 598a8b91 2019-03-15 stsp goto done;
1351 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1352 598a8b91 2019-03-15 stsp struct got_reference *ref;
1353 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1355 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1356 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1357 598a8b91 2019-03-15 stsp if (feof(f))
1359 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1360 598a8b91 2019-03-15 stsp goto done;
1362 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1363 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1364 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line, 0);
1366 598a8b91 2019-03-15 stsp goto done;
1367 598a8b91 2019-03-15 stsp if (ref == NULL)
1370 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1371 598a8b91 2019-03-15 stsp memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1372 598a8b91 2019-03-15 stsp sizeof(delref->ref.ref.sha1)) == 0) {
1373 598a8b91 2019-03-15 stsp found_delref = 1;
1374 598a8b91 2019-03-15 stsp got_ref_close(ref);
1378 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1379 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1380 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1381 598a8b91 2019-03-15 stsp got_ref_close(ref);
1383 598a8b91 2019-03-15 stsp goto done;
1386 598a8b91 2019-03-15 stsp if (found_delref) {
1387 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1389 598a8b91 2019-03-15 stsp struct stat sb;
1391 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1392 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1393 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1394 598a8b91 2019-03-15 stsp goto done;
1397 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1398 b3dc731f 2021-09-28 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
1400 598a8b91 2019-03-15 stsp if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1401 598a8b91 2019-03-15 stsp sizeof(hex)) == NULL) {
1402 598a8b91 2019-03-15 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
1403 598a8b91 2019-03-15 stsp goto done;
1405 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1406 598a8b91 2019-03-15 stsp if (n != sizeof(hex)) {
1407 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1408 598a8b91 2019-03-15 stsp goto done;
1410 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1411 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1412 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1413 598a8b91 2019-03-15 stsp goto done;
1417 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1418 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1419 598a8b91 2019-03-15 stsp goto done;
1422 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1423 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1424 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1425 230a42bd 2019-05-11 jcs packed_refs_path);
1426 598a8b91 2019-03-15 stsp goto done;
1428 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1431 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1432 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1433 598a8b91 2019-03-15 stsp goto done;
1436 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1437 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1438 230a42bd 2019-05-11 jcs packed_refs_path);
1439 598a8b91 2019-03-15 stsp goto done;
1443 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1444 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1446 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1447 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1449 598a8b91 2019-03-15 stsp if (tmpf) {
1450 598a8b91 2019-03-15 stsp unlink(tmppath);
1451 56b63ca4 2021-01-22 stsp if (fclose(tmpf) == EOF && err == NULL)
1452 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1454 598a8b91 2019-03-15 stsp free(tmppath);
1455 598a8b91 2019-03-15 stsp free(packed_refs_path);
1456 9bdd68dd 2020-01-02 naddy free(line);
1457 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1458 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1461 2a104ff6 2020-09-21 stsp static const struct got_error *
1462 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1464 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1465 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1466 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1467 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1469 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1470 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1471 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1472 2d2e1378 2019-03-11 stsp goto done;
1475 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1476 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1477 2d2e1378 2019-03-11 stsp goto done;
1480 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1481 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1483 2f17228e 2019-05-12 stsp goto done;
1486 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1488 2d2e1378 2019-03-11 stsp if (unlink(path) != 0)
1489 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1491 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1492 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1494 2d2e1378 2019-03-11 stsp free(path_refs);
1495 2d2e1378 2019-03-11 stsp free(path);
1496 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1499 2a104ff6 2020-09-21 stsp const struct got_error *
1500 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1502 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1503 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1505 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1506 2a104ff6 2020-09-21 stsp err = delete_packed_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_loose_ref(ref2, repo);
1518 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1519 2a104ff6 2020-09-21 stsp return err;
1521 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1523 2a104ff6 2020-09-21 stsp return err;
1525 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1526 2a104ff6 2020-09-21 stsp if (err) {
1527 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1528 2a104ff6 2020-09-21 stsp return NULL;
1529 2a104ff6 2020-09-21 stsp return err;
1532 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1533 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1534 2a104ff6 2020-09-21 stsp return err;
1538 2f17228e 2019-05-12 stsp const struct got_error *
1539 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1541 2f17228e 2019-05-12 stsp const struct got_error *err;
1542 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1543 2f17228e 2019-05-12 stsp ref->lf = NULL;
1544 2f17228e 2019-05-12 stsp return err;
1547 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1548 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1551 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1552 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1555 24202e46 2020-12-26 stsp static const struct got_error *
1556 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1557 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1559 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1560 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1561 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1563 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1564 24202e46 2020-12-26 stsp if (ent == NULL) {
1565 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1566 24202e46 2020-12-26 stsp if (ent == NULL)
1567 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1569 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1570 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1571 ee7b409c 2022-04-16 thomas if (err) {
1572 ee7b409c 2022-04-16 thomas free(ent);
1573 24202e46 2020-12-26 stsp return err;
1577 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1579 24202e46 2020-12-26 stsp return err;
1581 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1582 24202e46 2020-12-26 stsp return NULL;
1585 7b5b670e 2020-12-25 stsp const struct got_error *
1586 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1587 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1589 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1590 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1591 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1592 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1594 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1595 7b5b670e 2020-12-25 stsp if (idset == NULL)
1596 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1598 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1599 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1600 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1601 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1603 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1605 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1606 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1608 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1610 7b5b670e 2020-12-25 stsp goto done;
1612 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1614 24202e46 2020-12-26 stsp goto done;
1616 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1618 24202e46 2020-12-26 stsp id = NULL;
1622 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1623 24202e46 2020-12-26 stsp if (err) {
1624 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1625 7b5b670e 2020-12-25 stsp goto done;
1626 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1627 24202e46 2020-12-26 stsp err = NULL;
1628 24202e46 2020-12-26 stsp tag = NULL;
1630 24202e46 2020-12-26 stsp id = NULL;
1634 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1635 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1636 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1638 7b5b670e 2020-12-25 stsp goto done;
1641 7b5b670e 2020-12-25 stsp id = NULL;
1645 7b5b670e 2020-12-25 stsp if (err) {
1646 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1647 7b5b670e 2020-12-25 stsp *map = NULL;
1649 a53af95f 2020-12-26 stsp return err;
1652 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1653 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1654 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1656 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1657 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1659 7b5b670e 2020-12-25 stsp return &ent->refs;
1660 7b5b670e 2020-12-25 stsp return NULL;
1663 7b5b670e 2020-12-25 stsp static const struct got_error *
1664 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1666 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1668 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1669 7b5b670e 2020-12-25 stsp free(ent);
1670 7b5b670e 2020-12-25 stsp return NULL;
1674 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1676 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1677 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1678 7b5b670e 2020-12-25 stsp free(map);