Blame


1 5261c201 2018-04-01 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 5261c201 2018-04-01 stsp *
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.
7 5261c201 2018-04-01 stsp *
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.
15 5261c201 2018-04-01 stsp */
16 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 5261c201 2018-04-01 stsp
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>
22 5261c201 2018-04-01 stsp
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>
33 5261c201 2018-04-01 stsp
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"
40 5261c201 2018-04-01 stsp
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"
47 5261c201 2018-04-01 stsp
48 fb79db15 2019-02-01 stsp #ifndef nitems
49 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 fb79db15 2019-02-01 stsp #endif
51 fb79db15 2019-02-01 stsp
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"
55 d1f2edc9 2018-06-13 stsp
56 598a8b91 2019-03-15 stsp /*
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.
60 598a8b91 2019-03-15 stsp */
61 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
62 598a8b91 2019-03-15 stsp
63 5261c201 2018-04-01 stsp /* A symbolic reference. */
64 5261c201 2018-04-01 stsp struct got_symref {
65 5261c201 2018-04-01 stsp char *name;
66 5261c201 2018-04-01 stsp char *ref;
67 5261c201 2018-04-01 stsp };
68 29e86f7a 2019-08-12 stsp
69 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
70 5261c201 2018-04-01 stsp
71 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
72 5261c201 2018-04-01 stsp struct got_ref {
73 5261c201 2018-04-01 stsp char *name;
74 66107227 2023-01-31 thomas struct got_object_id id;
75 5261c201 2018-04-01 stsp };
76 5261c201 2018-04-01 stsp
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
82 5261c201 2018-04-01 stsp
83 5261c201 2018-04-01 stsp union {
84 5261c201 2018-04-01 stsp struct got_ref ref;
85 5261c201 2018-04-01 stsp struct got_symref symref;
86 5261c201 2018-04-01 stsp } ref;
87 2f17228e 2019-05-12 stsp
88 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
89 3f338f0a 2021-07-27 stsp time_t mtime;
90 fd3a2fe6 2021-10-12 thomas
91 fd77650a 2024-03-19 thomas /*
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().
94 fd77650a 2024-03-19 thomas */
95 fd3a2fe6 2021-10-12 thomas time_t committer_time;
96 5261c201 2018-04-01 stsp };
97 5261c201 2018-04-01 stsp
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)
101 5261c201 2018-04-01 stsp {
102 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
103 5261c201 2018-04-01 stsp
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");
107 f9267c9a 2019-03-15 stsp
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;
116 5261c201 2018-04-01 stsp }
117 f9267c9a 2019-03-15 stsp return err;
118 f9267c9a 2019-03-15 stsp }
119 5261c201 2018-04-01 stsp
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)
123 f9267c9a 2019-03-15 stsp {
124 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
125 f9267c9a 2019-03-15 stsp
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");
129 f9267c9a 2019-03-15 stsp
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;
137 f9267c9a 2019-03-15 stsp }
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;
143 f9267c9a 2019-03-15 stsp }
144 f9267c9a 2019-03-15 stsp return err;
145 5261c201 2018-04-01 stsp }
146 5261c201 2018-04-01 stsp
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)
149 f9267c9a 2019-03-15 stsp {
150 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
151 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
152 f9267c9a 2019-03-15 stsp
153 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
154 f9267c9a 2019-03-15 stsp }
155 f9267c9a 2019-03-15 stsp
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)
159 5261c201 2018-04-01 stsp {
160 5892cdd6 2019-03-10 stsp struct got_object_id id;
161 5261c201 2018-04-01 stsp
162 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
163 5261c201 2018-04-01 stsp line += 5;
164 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
165 5261c201 2018-04-01 stsp }
166 5261c201 2018-04-01 stsp
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);
169 5261c201 2018-04-01 stsp
170 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, 0, mtime);
171 5261c201 2018-04-01 stsp }
172 5261c201 2018-04-01 stsp
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)
177 5261c201 2018-04-01 stsp {
178 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
179 c0a1c016 2019-03-15 stsp FILE *f;
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;
185 5261c201 2018-04-01 stsp
186 2f17228e 2019-05-12 stsp if (lock) {
187 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, abspath, -1);
188 9f142382 2020-03-21 stsp if (err) {
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;
192 9f142382 2020-03-21 stsp }
193 2f17228e 2019-05-12 stsp }
194 2f17228e 2019-05-12 stsp
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);
199 b2070a3f 2020-03-22 stsp else
200 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
201 f5c58ad1 2019-05-12 stsp if (lock)
202 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
203 b2070a3f 2020-03-22 stsp return err;
204 f5c58ad1 2019-05-12 stsp }
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);
207 3f338f0a 2021-07-27 stsp goto done;
208 3f338f0a 2021-07-27 stsp }
209 5261c201 2018-04-01 stsp
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) */
214 b2070a3f 2020-03-22 stsp else {
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);
219 b2070a3f 2020-03-22 stsp else
220 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
221 b2070a3f 2020-03-22 stsp }
222 f5c58ad1 2019-05-12 stsp if (lock)
223 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
224 5261c201 2018-04-01 stsp goto done;
225 5261c201 2018-04-01 stsp }
226 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
227 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
228 c30018ad 2019-10-21 stsp linelen--;
229 c30018ad 2019-10-21 stsp }
230 5261c201 2018-04-01 stsp
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) {
233 f5c58ad1 2019-05-12 stsp if (err)
234 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
235 f5c58ad1 2019-05-12 stsp else {
236 f5c58ad1 2019-05-12 stsp if (*ref)
237 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
238 f5c58ad1 2019-05-12 stsp else
239 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
240 f5c58ad1 2019-05-12 stsp }
241 f5c58ad1 2019-05-12 stsp }
242 5261c201 2018-04-01 stsp done:
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) {
247 f5c58ad1 2019-05-12 stsp if (lock)
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;
251 2f17228e 2019-05-12 stsp }
252 2f17228e 2019-05-12 stsp }
253 5261c201 2018-04-01 stsp return err;
254 5261c201 2018-04-01 stsp }
255 5261c201 2018-04-01 stsp
256 c5f754cc 2019-02-01 stsp static int
257 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
258 5261c201 2018-04-01 stsp {
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);
263 c5f754cc 2019-02-01 stsp }
264 c5f754cc 2019-02-01 stsp
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)
267 c5f754cc 2019-02-01 stsp {
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));
270 5261c201 2018-04-01 stsp
271 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
272 5892cdd6 2019-03-10 stsp }
273 5892cdd6 2019-03-10 stsp
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)
277 5892cdd6 2019-03-10 stsp {
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);
280 f77a24b0 2019-03-11 stsp
281 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, id, 0, 0);
282 aaf88317 2019-07-10 stsp }
283 aaf88317 2019-07-10 stsp
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)
287 aaf88317 2019-07-10 stsp {
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);
290 aaf88317 2019-07-10 stsp
291 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
292 5261c201 2018-04-01 stsp }
293 5261c201 2018-04-01 stsp
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)
297 fb79db15 2019-02-01 stsp {
298 5892cdd6 2019-03-10 stsp struct got_object_id id;
299 5892cdd6 2019-03-10 stsp const char *name;
300 fb79db15 2019-02-01 stsp
301 fb79db15 2019-02-01 stsp *ref = NULL;
302 fb79db15 2019-02-01 stsp
303 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
304 fb79db15 2019-02-01 stsp return NULL;
305 fb79db15 2019-02-01 stsp
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);
308 fb79db15 2019-02-01 stsp
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;
313 5892cdd6 2019-03-10 stsp } else
314 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
315 fb79db15 2019-02-01 stsp
316 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
317 fb79db15 2019-02-01 stsp }
318 fb79db15 2019-02-01 stsp
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)
323 fb79db15 2019-02-01 stsp {
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);
330 fb79db15 2019-02-01 stsp
331 1e37702e 2019-02-04 stsp *ref = NULL;
332 1e37702e 2019-02-04 stsp
333 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
334 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
335 fb79db15 2019-02-01 stsp do {
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))
339 7ab0422a 2019-03-15 stsp break;
340 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
341 fb79db15 2019-02-01 stsp break;
342 7ab0422a 2019-03-15 stsp }
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)
355 0dec1cc0 2019-02-01 stsp break;
356 0dec1cc0 2019-02-01 stsp }
357 fb79db15 2019-02-01 stsp if (err)
358 fb79db15 2019-02-01 stsp break;
359 fb79db15 2019-02-01 stsp } while (*ref == NULL);
360 9bdd68dd 2020-01-02 naddy free(line);
361 fb79db15 2019-02-01 stsp
362 fb79db15 2019-02-01 stsp return err;
363 fb79db15 2019-02-01 stsp }
364 fb79db15 2019-02-01 stsp
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)
368 d1f2edc9 2018-06-13 stsp {
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));
374 d1f2edc9 2018-06-13 stsp
375 1e37702e 2019-02-04 stsp *ref = NULL;
376 1e37702e 2019-02-04 stsp
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);
379 57c18198 2021-05-24 stsp
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;
384 a04f49d2 2019-02-04 stsp } else {
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");
388 d1f2edc9 2018-06-13 stsp
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");
392 a04f49d2 2019-02-04 stsp goto done;
393 a04f49d2 2019-02-04 stsp }
394 a04f49d2 2019-02-04 stsp }
395 a04f49d2 2019-02-04 stsp
396 f4204d57 2023-03-01 thomas err = parse_ref_file(ref, name, absname, path, lock, algo);
397 d1f2edc9 2018-06-13 stsp done:
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;
402 d1f2edc9 2018-06-13 stsp }
403 d1f2edc9 2018-06-13 stsp
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)
407 5261c201 2018-04-01 stsp {
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
412 fb79db15 2019-02-01 stsp };
413 16aeacf7 2020-11-26 stsp size_t i;
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;
416 1e37702e 2019-02-04 stsp
417 1e37702e 2019-02-04 stsp *ref = NULL;
418 e135804e 2019-02-10 stsp
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);
422 e135804e 2019-02-10 stsp goto done;
423 e135804e 2019-02-10 stsp }
424 5261c201 2018-04-01 stsp
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));
428 0885ce8f 2019-05-12 stsp } else {
429 c5f754cc 2019-02-01 stsp FILE *f;
430 c5f754cc 2019-02-01 stsp
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)
436 e135804e 2019-02-10 stsp goto done;
437 e135804e 2019-02-10 stsp }
438 e135804e 2019-02-10 stsp
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");
443 e135804e 2019-02-10 stsp goto done;
444 e135804e 2019-02-10 stsp }
445 c5f754cc 2019-02-01 stsp
446 2f17228e 2019-05-12 stsp if (lock) {
447 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
448 2f17228e 2019-05-12 stsp if (err)
449 2f17228e 2019-05-12 stsp goto done;
450 2f17228e 2019-05-12 stsp }
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);
457 3f338f0a 2021-07-27 stsp goto done;
458 3f338f0a 2021-07-27 stsp }
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;
469 a875589a 2019-05-12 stsp }
470 fb79db15 2019-02-01 stsp }
471 fb79db15 2019-02-01 stsp }
472 e135804e 2019-02-10 stsp done:
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;
480 5261c201 2018-04-01 stsp }
481 5261c201 2018-04-01 stsp
482 5261c201 2018-04-01 stsp void
483 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
484 5261c201 2018-04-01 stsp {
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);
488 e09d28b1 2019-03-15 stsp } else
489 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
490 5261c201 2018-04-01 stsp free(ref);
491 5261c201 2018-04-01 stsp }
492 5261c201 2018-04-01 stsp
493 5261c201 2018-04-01 stsp struct got_reference *
494 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
495 5261c201 2018-04-01 stsp {
496 5261c201 2018-04-01 stsp struct got_reference *ret;
497 5261c201 2018-04-01 stsp
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;
501 5261c201 2018-04-01 stsp
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) {
506 5261c201 2018-04-01 stsp free(ret);
507 5261c201 2018-04-01 stsp return NULL;
508 5261c201 2018-04-01 stsp }
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);
512 5261c201 2018-04-01 stsp free(ret);
513 5261c201 2018-04-01 stsp return NULL;
514 5261c201 2018-04-01 stsp }
515 5261c201 2018-04-01 stsp } else {
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) {
518 5261c201 2018-04-01 stsp free(ret);
519 5261c201 2018-04-01 stsp return NULL;
520 5261c201 2018-04-01 stsp }
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));
523 5261c201 2018-04-01 stsp }
524 5261c201 2018-04-01 stsp
525 5261c201 2018-04-01 stsp return ret;
526 5261c201 2018-04-01 stsp }
527 b8bad2ba 2019-08-23 stsp
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)
531 b8bad2ba 2019-08-23 stsp {
532 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
533 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
534 b8bad2ba 2019-08-23 stsp
535 b8bad2ba 2019-08-23 stsp *newp = NULL;
536 b8bad2ba 2019-08-23 stsp
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");
540 5261c201 2018-04-01 stsp
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");
544 b8bad2ba 2019-08-23 stsp free(new);
545 b8bad2ba 2019-08-23 stsp return err;
546 b8bad2ba 2019-08-23 stsp }
547 b8bad2ba 2019-08-23 stsp
548 b8bad2ba 2019-08-23 stsp *newp = new;
549 b8bad2ba 2019-08-23 stsp return NULL;
550 b8bad2ba 2019-08-23 stsp }
551 b8bad2ba 2019-08-23 stsp
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)
555 5261c201 2018-04-01 stsp {
556 5261c201 2018-04-01 stsp struct got_reference *nextref;
557 5261c201 2018-04-01 stsp const struct got_error *err;
558 5261c201 2018-04-01 stsp
559 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
560 5261c201 2018-04-01 stsp if (err)
561 5261c201 2018-04-01 stsp return err;
562 5261c201 2018-04-01 stsp
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);
565 5261c201 2018-04-01 stsp else
566 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
567 5261c201 2018-04-01 stsp
568 5261c201 2018-04-01 stsp got_ref_close(nextref);
569 5261c201 2018-04-01 stsp return err;
570 5261c201 2018-04-01 stsp }
571 5261c201 2018-04-01 stsp
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)
575 5261c201 2018-04-01 stsp {
576 5261c201 2018-04-01 stsp const struct got_error *err;
577 5261c201 2018-04-01 stsp
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");
581 29e86f7a 2019-08-12 stsp
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;
590 5261c201 2018-04-01 stsp }
591 5261c201 2018-04-01 stsp
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;
597 29e86f7a 2019-08-12 stsp }
598 29e86f7a 2019-08-12 stsp
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)
602 29e86f7a 2019-08-12 stsp {
603 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
604 5261c201 2018-04-01 stsp }
605 5261c201 2018-04-01 stsp
606 5261c201 2018-04-01 stsp char *
607 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
608 5261c201 2018-04-01 stsp {
609 5261c201 2018-04-01 stsp char *str;
610 271d2a38 2018-12-25 stsp
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);
613 271d2a38 2018-12-25 stsp
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;
616 5261c201 2018-04-01 stsp
617 5261c201 2018-04-01 stsp return str;
618 5261c201 2018-04-01 stsp }
619 0bd18d37 2019-02-01 stsp
620 0bd18d37 2019-02-01 stsp const char *
621 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
622 0bd18d37 2019-02-01 stsp {
623 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
625 0bd18d37 2019-02-01 stsp
626 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
627 199a4027 2019-02-02 stsp }
628 199a4027 2019-02-02 stsp
629 aaf88317 2019-07-10 stsp const char *
630 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
631 aaf88317 2019-07-10 stsp {
632 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
634 b8bad2ba 2019-08-23 stsp
635 b8bad2ba 2019-08-23 stsp return NULL;
636 3f338f0a 2021-07-27 stsp }
637 3f338f0a 2021-07-27 stsp
638 3f338f0a 2021-07-27 stsp time_t
639 3f338f0a 2021-07-27 stsp got_ref_get_mtime(struct got_reference *ref)
640 3f338f0a 2021-07-27 stsp {
641 3f338f0a 2021-07-27 stsp return ref->mtime;
642 b8bad2ba 2019-08-23 stsp }
643 b8bad2ba 2019-08-23 stsp
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)
647 b8bad2ba 2019-08-23 stsp {
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);
650 aaf88317 2019-07-10 stsp
651 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
652 aaf88317 2019-07-10 stsp return NULL;
653 3bfadbd4 2021-11-20 thomas }
654 3bfadbd4 2021-11-20 thomas
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)
657 3bfadbd4 2021-11-20 thomas {
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;
663 3bfadbd4 2021-11-20 thomas
664 3bfadbd4 2021-11-20 thomas err = got_ref_resolve(&id, repo, ref);
665 3bfadbd4 2021-11-20 thomas if (err)
666 3bfadbd4 2021-11-20 thomas return err;
667 3bfadbd4 2021-11-20 thomas
668 3bfadbd4 2021-11-20 thomas err = got_object_get_type(&obj_type, repo, id);
669 3bfadbd4 2021-11-20 thomas if (err)
670 3bfadbd4 2021-11-20 thomas goto done;
671 3bfadbd4 2021-11-20 thomas
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);
675 3bfadbd4 2021-11-20 thomas if (err)
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);
679 3bfadbd4 2021-11-20 thomas break;
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);
682 3bfadbd4 2021-11-20 thomas if (err)
683 3bfadbd4 2021-11-20 thomas goto done;
684 3bfadbd4 2021-11-20 thomas ref->committer_time = got_object_tag_get_tagger_time(tag);
685 3bfadbd4 2021-11-20 thomas break;
686 3bfadbd4 2021-11-20 thomas default:
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);
689 3bfadbd4 2021-11-20 thomas break;
690 3bfadbd4 2021-11-20 thomas }
691 3bfadbd4 2021-11-20 thomas done:
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);
695 3bfadbd4 2021-11-20 thomas if (tag)
696 3bfadbd4 2021-11-20 thomas got_object_tag_close(tag);
697 e600f124 2021-03-21 stsp return err;
698 e600f124 2021-03-21 stsp }
699 e600f124 2021-03-21 stsp
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)
703 fd77650a 2024-03-19 thomas {
704 fd77650a 2024-03-19 thomas const struct got_error *err = NULL;
705 fd77650a 2024-03-19 thomas struct got_repository *repo = arg;
706 fd77650a 2024-03-19 thomas
707 fd77650a 2024-03-19 thomas *cmp = 0;
708 fd77650a 2024-03-19 thomas
709 fd77650a 2024-03-19 thomas if (ref1->committer_time == 0) {
710 fd77650a 2024-03-19 thomas err = get_committer_time(ref1, repo);
711 fd77650a 2024-03-19 thomas if (err)
712 fd77650a 2024-03-19 thomas return err;
713 fd77650a 2024-03-19 thomas }
714 fd77650a 2024-03-19 thomas if (ref2->committer_time == 0) {
715 fd77650a 2024-03-19 thomas err = get_committer_time(ref2, repo);
716 fd77650a 2024-03-19 thomas if (err)
717 fd77650a 2024-03-19 thomas return err;
718 fd77650a 2024-03-19 thomas }
719 fd77650a 2024-03-19 thomas
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;
725 fd77650a 2024-03-19 thomas else
726 fd77650a 2024-03-19 thomas err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
727 fd77650a 2024-03-19 thomas
728 fd77650a 2024-03-19 thomas return err;
729 fd77650a 2024-03-19 thomas }
730 fd77650a 2024-03-19 thomas
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)
734 e600f124 2021-03-21 stsp {
735 e87a53ff 2021-11-20 thomas const struct got_error *err = NULL;
736 e600f124 2021-03-21 stsp struct got_repository *repo = arg;
737 e600f124 2021-03-21 stsp
738 e600f124 2021-03-21 stsp *cmp = 0;
739 e600f124 2021-03-21 stsp
740 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time == 0) {
741 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref1, repo);
742 fd3a2fe6 2021-10-12 thomas if (err)
743 fd3a2fe6 2021-10-12 thomas return err;
744 fd3a2fe6 2021-10-12 thomas }
745 3bfadbd4 2021-11-20 thomas if (ref2->committer_time == 0) {
746 3bfadbd4 2021-11-20 thomas err = get_committer_time(ref2, repo);
747 fd3a2fe6 2021-10-12 thomas if (err)
748 fd3a2fe6 2021-10-12 thomas return err;
749 fd3a2fe6 2021-10-12 thomas }
750 fd3a2fe6 2021-10-12 thomas
751 fd3a2fe6 2021-10-12 thomas if (ref1->committer_time < ref2->committer_time)
752 e600f124 2021-03-21 stsp *cmp = 1;
753 fd3a2fe6 2021-10-12 thomas else if (ref2->committer_time < ref1->committer_time)
754 e600f124 2021-03-21 stsp *cmp = -1;
755 a729e288 2021-11-20 thomas else
756 a729e288 2021-11-20 thomas return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
757 3bfadbd4 2021-11-20 thomas
758 d1f16636 2020-01-15 stsp return err;
759 aaf88317 2019-07-10 stsp }
760 aaf88317 2019-07-10 stsp
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)
765 199a4027 2019-02-02 stsp {
766 199a4027 2019-02-02 stsp const struct got_error *err;
767 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
768 e397b6db 2019-02-04 stsp int cmp;
769 7a3c76f5 2019-02-05 stsp
770 505287be 2019-03-15 stsp *newp = NULL;
771 7a3c76f5 2019-02-05 stsp
772 41861f2b 2022-09-05 thomas if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
773 21de8138 2022-09-05 thomas /*
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.
778 21de8138 2022-09-05 thomas */
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);
781 21de8138 2022-09-05 thomas if (err)
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;
785 21de8138 2022-09-05 thomas }
786 21de8138 2022-09-05 thomas }
787 21de8138 2022-09-05 thomas
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;
793 41861f2b 2022-09-05 thomas
794 29b5c214 2019-02-04 stsp /*
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.
799 d9dff0e5 2020-12-26 stsp *
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.
804 29b5c214 2019-02-04 stsp */
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);
808 b8bad2ba 2019-08-23 stsp if (err)
809 b8bad2ba 2019-08-23 stsp return err;
810 e397b6db 2019-02-04 stsp if (cmp == 0) {
811 27a1ed03 2019-03-15 stsp /* duplicate */
812 27a1ed03 2019-03-15 stsp free(new);
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;
818 7a3c76f5 2019-02-05 stsp }
819 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
820 29b5c214 2019-02-04 stsp }
821 199a4027 2019-02-02 stsp
822 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
823 199a4027 2019-02-02 stsp return NULL;
824 0bd18d37 2019-02-01 stsp }
825 fb95e70d 2021-11-20 thomas
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)
829 fb95e70d 2021-11-20 thomas {
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;
833 199a4027 2019-02-02 stsp
834 fb95e70d 2021-11-20 thomas TAILQ_INIT(&sorted);
835 fb95e70d 2021-11-20 thomas
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);
843 fb95e70d 2021-11-20 thomas if (err)
844 fb95e70d 2021-11-20 thomas return err;
845 fb95e70d 2021-11-20 thomas }
846 fb95e70d 2021-11-20 thomas
847 fb95e70d 2021-11-20 thomas TAILQ_CONCAT(refs, &sorted, entry);
848 fb95e70d 2021-11-20 thomas return NULL;
849 fb95e70d 2021-11-20 thomas }
850 fb95e70d 2021-11-20 thomas
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)
855 a04f49d2 2019-02-04 stsp {
856 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
857 a04f49d2 2019-02-04 stsp DIR *d = NULL;
858 a04f49d2 2019-02-04 stsp char *path_subdir;
859 a7259708 2024-03-30 thomas struct got_reference *ref;
860 a7259708 2024-03-30 thomas struct got_reflist_entry *new;
861 b2070a3f 2020-03-22 stsp
862 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
863 b2070a3f 2020-03-22 stsp subdir++;
864 a04f49d2 2019-02-04 stsp
865 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
867 a04f49d2 2019-02-04 stsp
868 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
869 a7259708 2024-03-30 thomas if (d == NULL) {
870 a7259708 2024-03-30 thomas char *refname;
871 a04f49d2 2019-02-04 stsp
872 a7259708 2024-03-30 thomas if (errno != ENOTDIR)
873 a7259708 2024-03-30 thomas goto done;
874 a7259708 2024-03-30 thomas
875 a7259708 2024-03-30 thomas /* This could be a regular on-disk reference file. */
876 a7259708 2024-03-30 thomas free(path_subdir);
877 a7259708 2024-03-30 thomas err = got_path_dirname(&path_subdir, subdir);
878 a7259708 2024-03-30 thomas if (err)
879 a7259708 2024-03-30 thomas return err;
880 a7259708 2024-03-30 thomas err = got_path_basename(&refname, subdir);
881 a7259708 2024-03-30 thomas if (err) {
882 a7259708 2024-03-30 thomas free(path_subdir);
883 a7259708 2024-03-30 thomas return err;
884 a7259708 2024-03-30 thomas }
885 a7259708 2024-03-30 thomas err = open_ref(&ref, path_refs, path_subdir, refname,
886 a7259708 2024-03-30 thomas 0, got_repo_get_object_format(repo));
887 a7259708 2024-03-30 thomas free(path_subdir);
888 a7259708 2024-03-30 thomas free(refname);
889 a7259708 2024-03-30 thomas if (err) {
890 a7259708 2024-03-30 thomas if (err->code == GOT_ERR_NOT_REF)
891 a7259708 2024-03-30 thomas return NULL;
892 a7259708 2024-03-30 thomas return err;
893 a7259708 2024-03-30 thomas }
894 a7259708 2024-03-30 thomas err = got_reflist_insert(&new, refs, ref,
895 a7259708 2024-03-30 thomas cmp_cb, cmp_arg);
896 a7259708 2024-03-30 thomas if (err || new == NULL /* duplicate */)
897 a7259708 2024-03-30 thomas got_ref_close(ref);
898 a7259708 2024-03-30 thomas return err;
899 a7259708 2024-03-30 thomas }
900 a7259708 2024-03-30 thomas
901 656b1f76 2019-05-11 jcs for (;;) {
902 a04f49d2 2019-02-04 stsp struct dirent *dent;
903 a04f49d2 2019-02-04 stsp char *child;
904 20ccae39 2020-07-21 stsp int type;
905 a04f49d2 2019-02-04 stsp
906 a04f49d2 2019-02-04 stsp dent = readdir(d);
907 a04f49d2 2019-02-04 stsp if (dent == NULL)
908 a04f49d2 2019-02-04 stsp break;
909 a04f49d2 2019-02-04 stsp
910 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
911 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
912 a04f49d2 2019-02-04 stsp continue;
913 a04f49d2 2019-02-04 stsp
914 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
915 20ccae39 2020-07-21 stsp if (err)
916 20ccae39 2020-07-21 stsp break;
917 20ccae39 2020-07-21 stsp
918 20ccae39 2020-07-21 stsp switch (type) {
919 a04f49d2 2019-02-04 stsp case DT_REG:
920 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
921 f4204d57 2023-03-01 thomas 0, got_repo_get_object_format(repo));
922 88f0d95e 2023-06-22 thomas if (err && err->code == GOT_ERR_BAD_REF_NAME)
923 88f0d95e 2023-06-22 thomas break;
924 1e37702e 2019-02-04 stsp if (err)
925 a04f49d2 2019-02-04 stsp goto done;
926 a04f49d2 2019-02-04 stsp if (ref) {
927 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
928 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
929 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
930 505287be 2019-03-15 stsp got_ref_close(ref);
931 a04f49d2 2019-02-04 stsp if (err)
932 a04f49d2 2019-02-04 stsp goto done;
933 a04f49d2 2019-02-04 stsp }
934 a04f49d2 2019-02-04 stsp break;
935 a04f49d2 2019-02-04 stsp case DT_DIR:
936 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
937 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
938 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
939 a04f49d2 2019-02-04 stsp break;
940 a04f49d2 2019-02-04 stsp }
941 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
942 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
943 a04f49d2 2019-02-04 stsp free(child);
944 a04f49d2 2019-02-04 stsp break;
945 a04f49d2 2019-02-04 stsp default:
946 a04f49d2 2019-02-04 stsp break;
947 a04f49d2 2019-02-04 stsp }
948 a04f49d2 2019-02-04 stsp }
949 a04f49d2 2019-02-04 stsp done:
950 a04f49d2 2019-02-04 stsp if (d)
951 a04f49d2 2019-02-04 stsp closedir(d);
952 a04f49d2 2019-02-04 stsp free(path_subdir);
953 a04f49d2 2019-02-04 stsp return err;
954 a7259708 2024-03-30 thomas }
955 a7259708 2024-03-30 thomas
956 a7259708 2024-03-30 thomas static int
957 a7259708 2024-03-30 thomas match_packed_ref(struct got_reference *ref, const char *ref_namespace)
958 a7259708 2024-03-30 thomas {
959 a7259708 2024-03-30 thomas const char *name = got_ref_get_name(ref);
960 a7259708 2024-03-30 thomas int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
961 a7259708 2024-03-30 thomas
962 a7259708 2024-03-30 thomas if (namespace_is_absolute) {
963 a7259708 2024-03-30 thomas return (strcmp(name, ref_namespace) == 0 ||
964 a7259708 2024-03-30 thomas got_path_is_child(name, ref_namespace,
965 a7259708 2024-03-30 thomas strlen(ref_namespace)));
966 a7259708 2024-03-30 thomas }
967 a7259708 2024-03-30 thomas
968 a7259708 2024-03-30 thomas /* Match all "subdirectories" as we do with on-disk refs. */
969 a7259708 2024-03-30 thomas while (*name != '\0') {
970 a7259708 2024-03-30 thomas while (*name == '/')
971 a7259708 2024-03-30 thomas name++;
972 a7259708 2024-03-30 thomas if (strcmp(name, ref_namespace) == 0 ||
973 a7259708 2024-03-30 thomas got_path_is_child(name, ref_namespace,
974 a7259708 2024-03-30 thomas strlen(ref_namespace)))
975 a7259708 2024-03-30 thomas return 1;
976 a7259708 2024-03-30 thomas while (*name != '\0' && *name != '/')
977 a7259708 2024-03-30 thomas name++;
978 a7259708 2024-03-30 thomas }
979 a7259708 2024-03-30 thomas
980 a7259708 2024-03-30 thomas return 0;
981 a04f49d2 2019-02-04 stsp }
982 a04f49d2 2019-02-04 stsp
983 199a4027 2019-02-02 stsp const struct got_error *
984 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
985 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
986 199a4027 2019-02-02 stsp {
987 199a4027 2019-02-02 stsp const struct got_error *err;
988 3bfa5a06 2022-07-21 thomas char *packed_refs_path = NULL, *path_refs = NULL;
989 0c6f49ba 2022-07-01 thomas char *abs_namespace = NULL, *buf = NULL;
990 0c6f49ba 2022-07-01 thomas const char *ondisk_ref_namespace = NULL;
991 9bdd68dd 2020-01-02 naddy char *line = NULL;
992 29b5c214 2019-02-04 stsp FILE *f = NULL;
993 199a4027 2019-02-02 stsp struct got_reference *ref;
994 505287be 2019-03-15 stsp struct got_reflist_entry *new;
995 199a4027 2019-02-02 stsp
996 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
997 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
998 29606af7 2019-08-23 stsp if (path_refs == NULL) {
999 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
1000 29606af7 2019-08-23 stsp goto done;
1001 29606af7 2019-08-23 stsp }
1002 f4204d57 2023-03-01 thomas err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1003 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
1004 29606af7 2019-08-23 stsp if (err)
1005 29606af7 2019-08-23 stsp goto done;
1006 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1007 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
1008 29606af7 2019-08-23 stsp got_ref_close(ref);
1009 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
1010 29606af7 2019-08-23 stsp goto done;
1011 b2070a3f 2020-03-22 stsp } else {
1012 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
1013 a7259708 2024-03-30 thomas err = got_ref_open(&ref, repo, ref_namespace, 0);
1014 b2070a3f 2020-03-22 stsp if (err) {
1015 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
1016 b2070a3f 2020-03-22 stsp goto done;
1017 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
1018 b2070a3f 2020-03-22 stsp } else {
1019 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1020 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
1021 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
1022 b2070a3f 2020-03-22 stsp got_ref_close(ref);
1023 b2070a3f 2020-03-22 stsp return err;
1024 b2070a3f 2020-03-22 stsp }
1025 29b5c214 2019-02-04 stsp }
1026 29b5c214 2019-02-04 stsp
1027 b2070a3f 2020-03-22 stsp if (ref_namespace) {
1028 b2070a3f 2020-03-22 stsp size_t len;
1029 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
1030 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1031 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
1032 b2070a3f 2020-03-22 stsp goto done;
1033 b2070a3f 2020-03-22 stsp }
1034 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
1035 b2070a3f 2020-03-22 stsp buf = malloc(len);
1036 b2070a3f 2020-03-22 stsp if (buf == NULL) {
1037 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
1038 b2070a3f 2020-03-22 stsp goto done;
1039 b2070a3f 2020-03-22 stsp }
1040 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
1041 b2070a3f 2020-03-22 stsp if (err)
1042 b2070a3f 2020-03-22 stsp goto done;
1043 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
1044 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
1045 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
1046 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1047 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
1048 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1049 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1050 b2070a3f 2020-03-22 stsp }
1051 29606af7 2019-08-23 stsp
1052 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1053 29b5c214 2019-02-04 stsp free(path_refs);
1054 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1055 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1056 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1057 29b5c214 2019-02-04 stsp goto done;
1058 29b5c214 2019-02-04 stsp }
1059 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1060 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1061 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1062 29b5c214 2019-02-04 stsp if (err)
1063 29b5c214 2019-02-04 stsp goto done;
1064 29b5c214 2019-02-04 stsp
1065 29b5c214 2019-02-04 stsp /*
1066 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1067 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1068 29b5c214 2019-02-04 stsp */
1069 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1070 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1071 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1072 29b5c214 2019-02-04 stsp goto done;
1073 29b5c214 2019-02-04 stsp }
1074 199a4027 2019-02-02 stsp
1075 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1076 199a4027 2019-02-02 stsp if (f) {
1077 7a90b680 2020-01-02 naddy size_t linesize = 0;
1078 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1079 3f338f0a 2021-07-27 stsp struct stat sb;
1080 3f338f0a 2021-07-27 stsp
1081 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1082 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1083 3f338f0a 2021-07-27 stsp goto done;
1084 3f338f0a 2021-07-27 stsp }
1085 656b1f76 2019-05-11 jcs for (;;) {
1086 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1087 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1088 0bb4abae 2019-03-15 stsp if (feof(f))
1089 0bb4abae 2019-03-15 stsp break;
1090 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1091 0bb4abae 2019-03-15 stsp goto done;
1092 0bb4abae 2019-03-15 stsp }
1093 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1094 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1095 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1096 f4204d57 2023-03-01 thomas sb.st_mtime, got_repo_get_object_format(repo));
1097 199a4027 2019-02-02 stsp if (err)
1098 199a4027 2019-02-02 stsp goto done;
1099 76b4ead2 2019-02-03 stsp if (ref) {
1100 a7259708 2024-03-30 thomas if (ref_namespace &&
1101 a7259708 2024-03-30 thomas !match_packed_ref(ref, ref_namespace)) {
1102 a7259708 2024-03-30 thomas got_ref_close(ref);
1103 a7259708 2024-03-30 thomas continue;
1104 29606af7 2019-08-23 stsp }
1105 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1106 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1107 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1108 505287be 2019-03-15 stsp got_ref_close(ref);
1109 76b4ead2 2019-02-03 stsp if (err)
1110 76b4ead2 2019-02-03 stsp goto done;
1111 76b4ead2 2019-02-03 stsp }
1112 199a4027 2019-02-02 stsp }
1113 199a4027 2019-02-02 stsp }
1114 199a4027 2019-02-02 stsp done:
1115 3bfa5a06 2022-07-21 thomas free(packed_refs_path);
1116 b2070a3f 2020-03-22 stsp free(abs_namespace);
1117 b2070a3f 2020-03-22 stsp free(buf);
1118 9bdd68dd 2020-01-02 naddy free(line);
1119 a04f49d2 2019-02-04 stsp free(path_refs);
1120 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1121 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1122 199a4027 2019-02-02 stsp return err;
1123 199a4027 2019-02-02 stsp }
1124 9e672c74 2019-03-11 stsp
1125 e2e879a0 2019-03-11 stsp void
1126 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1127 e2e879a0 2019-03-11 stsp {
1128 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1129 e2e879a0 2019-03-11 stsp
1130 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1131 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1132 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1133 e2e879a0 2019-03-11 stsp free(re);
1134 e2e879a0 2019-03-11 stsp }
1135 b249b824 2019-05-09 stsp }
1136 b249b824 2019-05-09 stsp
1137 b249b824 2019-05-09 stsp int
1138 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1139 b249b824 2019-05-09 stsp {
1140 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1141 b249b824 2019-05-09 stsp }
1142 b249b824 2019-05-09 stsp
1143 b249b824 2019-05-09 stsp const struct got_error *
1144 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1145 b249b824 2019-05-09 stsp {
1146 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1147 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1148 e2e879a0 2019-03-11 stsp
1149 66107227 2023-01-31 thomas memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1150 b249b824 2019-05-09 stsp return NULL;
1151 e2e879a0 2019-03-11 stsp }
1152 e2e879a0 2019-03-11 stsp
1153 9e672c74 2019-03-11 stsp const struct got_error *
1154 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1155 b249b824 2019-05-09 stsp {
1156 b249b824 2019-05-09 stsp char *new_name;
1157 b249b824 2019-05-09 stsp
1158 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1159 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1160 b249b824 2019-05-09 stsp
1161 b249b824 2019-05-09 stsp new_name = strdup(refname);
1162 b249b824 2019-05-09 stsp if (new_name == NULL)
1163 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1164 b249b824 2019-05-09 stsp
1165 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1166 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1167 b249b824 2019-05-09 stsp return NULL;
1168 b249b824 2019-05-09 stsp }
1169 b249b824 2019-05-09 stsp
1170 b249b824 2019-05-09 stsp const struct got_error *
1171 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1172 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1173 e8a967e0 2020-03-21 stsp {
1174 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1175 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1176 e8a967e0 2020-03-21 stsp
1177 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1178 66107227 2023-01-31 thomas memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1179 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1180 e8a967e0 2020-03-21 stsp return NULL;
1181 e8a967e0 2020-03-21 stsp }
1182 e8a967e0 2020-03-21 stsp
1183 e8a967e0 2020-03-21 stsp const struct got_error *
1184 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1185 9e672c74 2019-03-11 stsp {
1186 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1187 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1188 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1189 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1190 9e672c74 2019-03-11 stsp FILE *f = NULL;
1191 9e672c74 2019-03-11 stsp size_t n;
1192 9e672c74 2019-03-11 stsp struct stat sb;
1193 9e672c74 2019-03-11 stsp
1194 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1195 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1196 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1197 9e672c74 2019-03-11 stsp goto done;
1198 9e672c74 2019-03-11 stsp }
1199 9e672c74 2019-03-11 stsp
1200 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1202 9e672c74 2019-03-11 stsp goto done;
1203 9e672c74 2019-03-11 stsp }
1204 9e672c74 2019-03-11 stsp
1205 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1206 49c7094f 2019-03-11 stsp if (err) {
1207 d1667f0d 2019-03-11 stsp char *parent;
1208 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1209 5e1c9f23 2019-03-11 stsp goto done;
1210 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1211 d1667f0d 2019-03-11 stsp if (err)
1212 0cd1c46a 2019-03-11 stsp goto done;
1213 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1214 5e1c9f23 2019-03-11 stsp free(parent);
1215 0cd1c46a 2019-03-11 stsp if (err)
1216 0cd1c46a 2019-03-11 stsp goto done;
1217 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &f, path, "");
1218 49c7094f 2019-03-11 stsp if (err)
1219 0cd1c46a 2019-03-11 stsp goto done;
1220 9e672c74 2019-03-11 stsp }
1221 9e672c74 2019-03-11 stsp
1222 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1223 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1224 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1225 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1226 9e672c74 2019-03-11 stsp goto done;
1227 9e672c74 2019-03-11 stsp }
1228 9e672c74 2019-03-11 stsp } else {
1229 66107227 2023-01-31 thomas char *hex;
1230 66107227 2023-01-31 thomas size_t len;
1231 66107227 2023-01-31 thomas
1232 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &ref->ref.ref.id);
1233 66107227 2023-01-31 thomas if (err)
1234 66107227 2023-01-31 thomas goto done;
1235 66107227 2023-01-31 thomas len = strlen(hex);
1236 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1237 66107227 2023-01-31 thomas free(hex);
1238 66107227 2023-01-31 thomas if (n != len + 1) {
1239 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1240 9e672c74 2019-03-11 stsp goto done;
1241 9e672c74 2019-03-11 stsp }
1242 9e672c74 2019-03-11 stsp }
1243 9e672c74 2019-03-11 stsp
1244 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1245 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1246 2f17228e 2019-05-12 stsp if (err)
1247 2f17228e 2019-05-12 stsp goto done;
1248 2f17228e 2019-05-12 stsp }
1249 9e672c74 2019-03-11 stsp
1250 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1251 9e672c74 2019-03-11 stsp
1252 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1253 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1254 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1255 0cd1c46a 2019-03-11 stsp goto done;
1256 0cd1c46a 2019-03-11 stsp }
1257 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1258 3818e3c4 2020-11-01 naddy }
1259 3818e3c4 2020-11-01 naddy
1260 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1261 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1262 3818e3c4 2020-11-01 naddy goto done;
1263 9e672c74 2019-03-11 stsp }
1264 9e672c74 2019-03-11 stsp
1265 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1266 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1267 9e672c74 2019-03-11 stsp goto done;
1268 9e672c74 2019-03-11 stsp }
1269 9e672c74 2019-03-11 stsp free(tmppath);
1270 9e672c74 2019-03-11 stsp tmppath = NULL;
1271 3f338f0a 2021-07-27 stsp
1272 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1273 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1274 3f338f0a 2021-07-27 stsp goto done;
1275 3f338f0a 2021-07-27 stsp }
1276 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1277 9e672c74 2019-03-11 stsp done:
1278 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1279 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1280 9e672c74 2019-03-11 stsp if (f) {
1281 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1282 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1283 9e672c74 2019-03-11 stsp }
1284 9e672c74 2019-03-11 stsp free(path_refs);
1285 9e672c74 2019-03-11 stsp free(path);
1286 9e672c74 2019-03-11 stsp if (tmppath) {
1287 e6b88e16 2022-10-16 thomas if (unlink(tmppath) == -1 && err == NULL)
1288 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1289 9e672c74 2019-03-11 stsp free(tmppath);
1290 2d2e1378 2019-03-11 stsp }
1291 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1292 2d2e1378 2019-03-11 stsp }
1293 598a8b91 2019-03-15 stsp
1294 598a8b91 2019-03-15 stsp static const struct got_error *
1295 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1296 598a8b91 2019-03-15 stsp {
1297 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1298 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1299 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1300 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1301 7a90b680 2020-01-02 naddy size_t linesize = 0;
1302 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1303 598a8b91 2019-03-15 stsp int found_delref = 0;
1304 2d2e1378 2019-03-11 stsp
1305 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1306 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1307 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1308 598a8b91 2019-03-15 stsp
1309 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1310 598a8b91 2019-03-15 stsp
1311 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1312 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1313 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1314 598a8b91 2019-03-15 stsp
1315 fc2a50f2 2022-11-01 thomas err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1316 598a8b91 2019-03-15 stsp if (err)
1317 598a8b91 2019-03-15 stsp goto done;
1318 598a8b91 2019-03-15 stsp
1319 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1320 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1321 2f17228e 2019-05-12 stsp if (err)
1322 2f17228e 2019-05-12 stsp goto done;
1323 2f17228e 2019-05-12 stsp }
1324 598a8b91 2019-03-15 stsp
1325 c56c5d8a 2021-12-31 thomas f = fopen(packed_refs_path, "re");
1326 598a8b91 2019-03-15 stsp if (f == NULL) {
1327 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1328 598a8b91 2019-03-15 stsp goto done;
1329 598a8b91 2019-03-15 stsp }
1330 656b1f76 2019-05-11 jcs for (;;) {
1331 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1332 598a8b91 2019-03-15 stsp struct got_reference *ref;
1333 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1334 598a8b91 2019-03-15 stsp
1335 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1336 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1337 598a8b91 2019-03-15 stsp if (feof(f))
1338 598a8b91 2019-03-15 stsp break;
1339 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1340 598a8b91 2019-03-15 stsp goto done;
1341 598a8b91 2019-03-15 stsp }
1342 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1343 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1344 f4204d57 2023-03-01 thomas err = parse_packed_ref_line(&ref, NULL, line, 0,
1345 f4204d57 2023-03-01 thomas got_repo_get_object_format(repo));
1346 598a8b91 2019-03-15 stsp if (err)
1347 598a8b91 2019-03-15 stsp goto done;
1348 598a8b91 2019-03-15 stsp if (ref == NULL)
1349 598a8b91 2019-03-15 stsp continue;
1350 598a8b91 2019-03-15 stsp
1351 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1352 66107227 2023-01-31 thomas got_object_id_cmp(&ref->ref.ref.id,
1353 66107227 2023-01-31 thomas &delref->ref.ref.id) == 0) {
1354 598a8b91 2019-03-15 stsp found_delref = 1;
1355 598a8b91 2019-03-15 stsp got_ref_close(ref);
1356 598a8b91 2019-03-15 stsp continue;
1357 598a8b91 2019-03-15 stsp }
1358 598a8b91 2019-03-15 stsp
1359 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1360 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1361 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1362 598a8b91 2019-03-15 stsp got_ref_close(ref);
1363 598a8b91 2019-03-15 stsp if (err)
1364 598a8b91 2019-03-15 stsp goto done;
1365 598a8b91 2019-03-15 stsp }
1366 598a8b91 2019-03-15 stsp
1367 598a8b91 2019-03-15 stsp if (found_delref) {
1368 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1369 598a8b91 2019-03-15 stsp size_t n;
1370 598a8b91 2019-03-15 stsp struct stat sb;
1371 598a8b91 2019-03-15 stsp
1372 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1373 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1374 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1375 598a8b91 2019-03-15 stsp goto done;
1376 598a8b91 2019-03-15 stsp }
1377 598a8b91 2019-03-15 stsp
1378 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1379 66107227 2023-01-31 thomas char *hex;
1380 66107227 2023-01-31 thomas size_t len;
1381 598a8b91 2019-03-15 stsp
1382 66107227 2023-01-31 thomas err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1383 66107227 2023-01-31 thomas if (err)
1384 598a8b91 2019-03-15 stsp goto done;
1385 66107227 2023-01-31 thomas len = strlen(hex);
1386 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1387 66107227 2023-01-31 thomas free(hex);
1388 66107227 2023-01-31 thomas if (n != len + 1) {
1389 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1390 598a8b91 2019-03-15 stsp goto done;
1391 598a8b91 2019-03-15 stsp }
1392 66107227 2023-01-31 thomas
1393 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1394 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1395 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1396 598a8b91 2019-03-15 stsp goto done;
1397 598a8b91 2019-03-15 stsp }
1398 598a8b91 2019-03-15 stsp }
1399 598a8b91 2019-03-15 stsp
1400 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1401 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1402 598a8b91 2019-03-15 stsp goto done;
1403 598a8b91 2019-03-15 stsp }
1404 598a8b91 2019-03-15 stsp
1405 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1406 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1407 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1408 230a42bd 2019-05-11 jcs packed_refs_path);
1409 598a8b91 2019-03-15 stsp goto done;
1410 598a8b91 2019-03-15 stsp }
1411 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1412 598a8b91 2019-03-15 stsp }
1413 598a8b91 2019-03-15 stsp
1414 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1415 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1416 598a8b91 2019-03-15 stsp goto done;
1417 598a8b91 2019-03-15 stsp }
1418 598a8b91 2019-03-15 stsp
1419 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1420 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1421 230a42bd 2019-05-11 jcs packed_refs_path);
1422 598a8b91 2019-03-15 stsp goto done;
1423 598a8b91 2019-03-15 stsp }
1424 f96b13c8 2022-10-16 thomas free(tmppath);
1425 f96b13c8 2022-10-16 thomas tmppath = NULL;
1426 598a8b91 2019-03-15 stsp }
1427 598a8b91 2019-03-15 stsp done:
1428 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1429 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1430 598a8b91 2019-03-15 stsp if (f) {
1431 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1433 598a8b91 2019-03-15 stsp }
1434 f96b13c8 2022-10-16 thomas if (tmppath && unlink(tmppath) == -1 && err == NULL)
1435 f96b13c8 2022-10-16 thomas err = got_error_from_errno2("unlink", tmppath);
1436 f96b13c8 2022-10-16 thomas if (tmpf && fclose(tmpf) == EOF && err == NULL)
1437 f96b13c8 2022-10-16 thomas err = got_error_from_errno("fclose");
1438 598a8b91 2019-03-15 stsp free(tmppath);
1439 598a8b91 2019-03-15 stsp free(packed_refs_path);
1440 9bdd68dd 2020-01-02 naddy free(line);
1441 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1442 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1443 598a8b91 2019-03-15 stsp }
1444 598a8b91 2019-03-15 stsp
1445 2a104ff6 2020-09-21 stsp static const struct got_error *
1446 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1447 2d2e1378 2019-03-11 stsp {
1448 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1449 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1450 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1451 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1452 2d2e1378 2019-03-11 stsp
1453 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1454 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1455 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1456 2d2e1378 2019-03-11 stsp goto done;
1457 2d2e1378 2019-03-11 stsp }
1458 2d2e1378 2019-03-11 stsp
1459 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1460 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1461 2d2e1378 2019-03-11 stsp goto done;
1462 9e672c74 2019-03-11 stsp }
1463 2d2e1378 2019-03-11 stsp
1464 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1465 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1466 2f17228e 2019-05-12 stsp if (err)
1467 2f17228e 2019-05-12 stsp goto done;
1468 2f17228e 2019-05-12 stsp }
1469 2d2e1378 2019-03-11 stsp
1470 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1471 2d2e1378 2019-03-11 stsp
1472 e6b88e16 2022-10-16 thomas if (unlink(path) == -1)
1473 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1474 2d2e1378 2019-03-11 stsp done:
1475 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1476 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1477 2d2e1378 2019-03-11 stsp
1478 2d2e1378 2019-03-11 stsp free(path_refs);
1479 2d2e1378 2019-03-11 stsp free(path);
1480 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1481 2a104ff6 2020-09-21 stsp }
1482 2a104ff6 2020-09-21 stsp
1483 2a104ff6 2020-09-21 stsp const struct got_error *
1484 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1485 2a104ff6 2020-09-21 stsp {
1486 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1487 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1488 2a104ff6 2020-09-21 stsp
1489 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1490 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1491 2a104ff6 2020-09-21 stsp if (err)
1492 2a104ff6 2020-09-21 stsp return err;
1493 2a104ff6 2020-09-21 stsp
1494 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1495 2a104ff6 2020-09-21 stsp if (err) {
1496 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1497 2a104ff6 2020-09-21 stsp return NULL;
1498 2a104ff6 2020-09-21 stsp return err;
1499 2a104ff6 2020-09-21 stsp }
1500 2a104ff6 2020-09-21 stsp
1501 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1502 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1503 2a104ff6 2020-09-21 stsp return err;
1504 2a104ff6 2020-09-21 stsp } else {
1505 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1506 2a104ff6 2020-09-21 stsp if (err)
1507 2a104ff6 2020-09-21 stsp return err;
1508 2a104ff6 2020-09-21 stsp
1509 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1510 2a104ff6 2020-09-21 stsp if (err) {
1511 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1512 2a104ff6 2020-09-21 stsp return NULL;
1513 2a104ff6 2020-09-21 stsp return err;
1514 2a104ff6 2020-09-21 stsp }
1515 2a104ff6 2020-09-21 stsp
1516 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1517 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1518 2a104ff6 2020-09-21 stsp return err;
1519 2a104ff6 2020-09-21 stsp }
1520 9e672c74 2019-03-11 stsp }
1521 2f17228e 2019-05-12 stsp
1522 2f17228e 2019-05-12 stsp const struct got_error *
1523 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1524 2f17228e 2019-05-12 stsp {
1525 2f17228e 2019-05-12 stsp const struct got_error *err;
1526 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1527 2f17228e 2019-05-12 stsp ref->lf = NULL;
1528 2f17228e 2019-05-12 stsp return err;
1529 2f17228e 2019-05-12 stsp }
1530 7b5b670e 2020-12-25 stsp
1531 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1532 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1533 7b5b670e 2020-12-25 stsp };
1534 7b5b670e 2020-12-25 stsp
1535 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1536 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1537 7b5b670e 2020-12-25 stsp };
1538 24202e46 2020-12-26 stsp
1539 24202e46 2020-12-26 stsp static const struct got_error *
1540 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1541 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1542 24202e46 2020-12-26 stsp {
1543 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1544 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1545 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1546 24202e46 2020-12-26 stsp
1547 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1548 24202e46 2020-12-26 stsp if (ent == NULL) {
1549 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1550 24202e46 2020-12-26 stsp if (ent == NULL)
1551 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1552 7b5b670e 2020-12-25 stsp
1553 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1554 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1555 ee7b409c 2022-04-16 thomas if (err) {
1556 ee7b409c 2022-04-16 thomas free(ent);
1557 24202e46 2020-12-26 stsp return err;
1558 ee7b409c 2022-04-16 thomas }
1559 24202e46 2020-12-26 stsp }
1560 24202e46 2020-12-26 stsp
1561 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1562 24202e46 2020-12-26 stsp if (err)
1563 24202e46 2020-12-26 stsp return err;
1564 24202e46 2020-12-26 stsp
1565 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1566 24202e46 2020-12-26 stsp return NULL;
1567 24202e46 2020-12-26 stsp }
1568 24202e46 2020-12-26 stsp
1569 7b5b670e 2020-12-25 stsp const struct got_error *
1570 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1571 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1572 7b5b670e 2020-12-25 stsp {
1573 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1574 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1575 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1576 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1577 7b5b670e 2020-12-25 stsp
1578 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1579 7b5b670e 2020-12-25 stsp if (idset == NULL)
1580 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1581 7b5b670e 2020-12-25 stsp
1582 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1583 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1584 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1585 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1586 7b5b670e 2020-12-25 stsp }
1587 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1588 7b5b670e 2020-12-25 stsp
1589 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1590 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1591 7b5b670e 2020-12-25 stsp
1592 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1593 7b5b670e 2020-12-25 stsp if (err)
1594 7b5b670e 2020-12-25 stsp goto done;
1595 7b5b670e 2020-12-25 stsp
1596 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1597 24202e46 2020-12-26 stsp if (err)
1598 24202e46 2020-12-26 stsp goto done;
1599 24202e46 2020-12-26 stsp
1600 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1601 24202e46 2020-12-26 stsp free(id);
1602 24202e46 2020-12-26 stsp id = NULL;
1603 24202e46 2020-12-26 stsp continue;
1604 24202e46 2020-12-26 stsp }
1605 24202e46 2020-12-26 stsp
1606 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1607 24202e46 2020-12-26 stsp if (err) {
1608 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1609 7b5b670e 2020-12-25 stsp goto done;
1610 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1611 24202e46 2020-12-26 stsp err = NULL;
1612 24202e46 2020-12-26 stsp tag = NULL;
1613 24202e46 2020-12-26 stsp free(id);
1614 24202e46 2020-12-26 stsp id = NULL;
1615 24202e46 2020-12-26 stsp continue;
1616 7b5b670e 2020-12-25 stsp }
1617 7b5b670e 2020-12-25 stsp
1618 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1619 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1620 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1621 7b5b670e 2020-12-25 stsp if (err)
1622 7b5b670e 2020-12-25 stsp goto done;
1623 24202e46 2020-12-26 stsp
1624 7b5b670e 2020-12-25 stsp free(id);
1625 7b5b670e 2020-12-25 stsp id = NULL;
1626 7b5b670e 2020-12-25 stsp }
1627 7b5b670e 2020-12-25 stsp done:
1628 7b5b670e 2020-12-25 stsp free(id);
1629 7b5b670e 2020-12-25 stsp if (err) {
1630 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1631 7b5b670e 2020-12-25 stsp *map = NULL;
1632 7b5b670e 2020-12-25 stsp }
1633 a53af95f 2020-12-26 stsp return err;
1634 7b5b670e 2020-12-25 stsp }
1635 7b5b670e 2020-12-25 stsp
1636 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1637 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1638 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1639 7b5b670e 2020-12-25 stsp {
1640 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1641 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1642 7b5b670e 2020-12-25 stsp if (ent)
1643 7b5b670e 2020-12-25 stsp return &ent->refs;
1644 7b5b670e 2020-12-25 stsp return NULL;
1645 7b5b670e 2020-12-25 stsp }
1646 7b5b670e 2020-12-25 stsp
1647 7b5b670e 2020-12-25 stsp static const struct got_error *
1648 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1649 7b5b670e 2020-12-25 stsp {
1650 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1651 7b5b670e 2020-12-25 stsp
1652 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1653 7b5b670e 2020-12-25 stsp free(ent);
1654 7b5b670e 2020-12-25 stsp return NULL;
1655 7b5b670e 2020-12-25 stsp }
1656 7b5b670e 2020-12-25 stsp
1657 7b5b670e 2020-12-25 stsp void
1658 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1659 7b5b670e 2020-12-25 stsp {
1660 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1661 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1662 7b5b670e 2020-12-25 stsp free(map);
1663 7b5b670e 2020-12-25 stsp }