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 5261c201 2018-04-01 stsp
17 5261c201 2018-04-01 stsp #include <sys/types.h>
18 5261c201 2018-04-01 stsp #include <sys/queue.h>
19 9e672c74 2019-03-11 stsp #include <sys/stat.h>
20 5261c201 2018-04-01 stsp
21 0fd469ce 2019-03-11 stsp #include <errno.h>
22 a04f49d2 2019-02-04 stsp #include <dirent.h>
23 56e0773d 2019-11-28 stsp #include <limits.h>
24 5261c201 2018-04-01 stsp #include <sha1.h>
25 5822e79e 2023-02-23 op #include <sha2.h>
26 5261c201 2018-04-01 stsp #include <stdio.h>
27 5261c201 2018-04-01 stsp #include <stdlib.h>
28 5261c201 2018-04-01 stsp #include <string.h>
29 81a12da5 2020-09-09 naddy #include <unistd.h>
30 5261c201 2018-04-01 stsp #include <util.h>
31 5261c201 2018-04-01 stsp #include <zlib.h>
32 788c352e 2018-06-16 stsp #include <time.h>
33 0cd1c46a 2019-03-11 stsp #include <libgen.h>
34 5261c201 2018-04-01 stsp
35 5261c201 2018-04-01 stsp #include "got_error.h"
36 5261c201 2018-04-01 stsp #include "got_object.h"
37 5261c201 2018-04-01 stsp #include "got_repository.h"
38 5261c201 2018-04-01 stsp #include "got_reference.h"
39 9e672c74 2019-03-11 stsp #include "got_opentemp.h"
40 324d37e7 2019-05-11 stsp #include "got_path.h"
41 5261c201 2018-04-01 stsp
42 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
43 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 5261c201 2018-04-01 stsp #include "got_lib_object.h"
46 7b5b670e 2020-12-25 stsp #include "got_lib_object_idset.h"
47 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
48 5261c201 2018-04-01 stsp
49 fb79db15 2019-02-01 stsp #ifndef nitems
50 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 fb79db15 2019-02-01 stsp #endif
52 fb79db15 2019-02-01 stsp
53 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
54 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
55 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
56 d1f2edc9 2018-06-13 stsp
57 598a8b91 2019-03-15 stsp /*
58 598a8b91 2019-03-15 stsp * We do not resolve tags yet, and don't yet care about sorting refs either,
59 598a8b91 2019-03-15 stsp * so packed-refs files we write contain a minimal header which disables all
60 598a8b91 2019-03-15 stsp * packed-refs "traits" supported by Git.
61 598a8b91 2019-03-15 stsp */
62 598a8b91 2019-03-15 stsp #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 598a8b91 2019-03-15 stsp
64 5261c201 2018-04-01 stsp /* A symbolic reference. */
65 5261c201 2018-04-01 stsp struct got_symref {
66 5261c201 2018-04-01 stsp char *name;
67 5261c201 2018-04-01 stsp char *ref;
68 5261c201 2018-04-01 stsp };
69 29e86f7a 2019-08-12 stsp
70 29e86f7a 2019-08-12 stsp #define GOT_REF_RECURSE_MAX 20
71 5261c201 2018-04-01 stsp
72 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
73 5261c201 2018-04-01 stsp struct got_ref {
74 5261c201 2018-04-01 stsp char *name;
75 faf5b56f 2023-01-31 op struct got_object_id id;
76 5261c201 2018-04-01 stsp };
77 5261c201 2018-04-01 stsp
78 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
79 5261c201 2018-04-01 stsp struct got_reference {
80 5261c201 2018-04-01 stsp unsigned int flags;
81 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
82 f9267c9a 2019-03-15 stsp #define GOT_REF_IS_PACKED 0x02
83 5261c201 2018-04-01 stsp
84 5261c201 2018-04-01 stsp union {
85 5261c201 2018-04-01 stsp struct got_ref ref;
86 5261c201 2018-04-01 stsp struct got_symref symref;
87 5261c201 2018-04-01 stsp } ref;
88 2f17228e 2019-05-12 stsp
89 2f17228e 2019-05-12 stsp struct got_lockfile *lf;
90 3f338f0a 2021-07-27 stsp time_t mtime;
91 2c9e323b 2021-10-10 stsp
92 c5d61cbc 2024-03-16 stsp /*
93 c5d61cbc 2024-03-16 stsp * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 c5d61cbc 2024-03-16 stsp * and got_ref_cmp_tags().
95 c5d61cbc 2024-03-16 stsp */
96 2c9e323b 2021-10-10 stsp time_t committer_time;
97 5261c201 2018-04-01 stsp };
98 5261c201 2018-04-01 stsp
99 5261c201 2018-04-01 stsp static const struct got_error *
100 f9267c9a 2019-03-15 stsp alloc_ref(struct got_reference **ref, const char *name,
101 3f338f0a 2021-07-27 stsp struct got_object_id *id, int flags, time_t mtime)
102 5261c201 2018-04-01 stsp {
103 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
104 5261c201 2018-04-01 stsp
105 f9267c9a 2019-03-15 stsp *ref = calloc(1, sizeof(**ref));
106 f9267c9a 2019-03-15 stsp if (*ref == NULL)
107 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
108 f9267c9a 2019-03-15 stsp
109 faf5b56f 2023-01-31 op memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
110 c980e470 2019-03-15 stsp (*ref)->flags = flags;
111 f9267c9a 2019-03-15 stsp (*ref)->ref.ref.name = strdup(name);
112 3f338f0a 2021-07-27 stsp (*ref)->mtime = mtime;
113 f9267c9a 2019-03-15 stsp if ((*ref)->ref.ref.name == NULL) {
114 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
115 c980e470 2019-03-15 stsp got_ref_close(*ref);
116 f9267c9a 2019-03-15 stsp *ref = NULL;
117 5261c201 2018-04-01 stsp }
118 f9267c9a 2019-03-15 stsp return err;
119 f9267c9a 2019-03-15 stsp }
120 5261c201 2018-04-01 stsp
121 f9267c9a 2019-03-15 stsp static const struct got_error *
122 f9267c9a 2019-03-15 stsp alloc_symref(struct got_reference **ref, const char *name,
123 f9267c9a 2019-03-15 stsp const char *target_ref, int flags)
124 f9267c9a 2019-03-15 stsp {
125 f9267c9a 2019-03-15 stsp const struct got_error *err = NULL;
126 f9267c9a 2019-03-15 stsp
127 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
128 5261c201 2018-04-01 stsp if (*ref == NULL)
129 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
130 f9267c9a 2019-03-15 stsp
131 f9267c9a 2019-03-15 stsp (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
132 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.name = strdup(name);
133 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.name == NULL) {
134 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
135 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
136 f9267c9a 2019-03-15 stsp *ref = NULL;
137 cdb8f1fa 2019-08-28 hiltjo return err;
138 f9267c9a 2019-03-15 stsp }
139 f9267c9a 2019-03-15 stsp (*ref)->ref.symref.ref = strdup(target_ref);
140 f9267c9a 2019-03-15 stsp if ((*ref)->ref.symref.ref == NULL) {
141 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
142 f9267c9a 2019-03-15 stsp got_ref_close(*ref);
143 f9267c9a 2019-03-15 stsp *ref = NULL;
144 f9267c9a 2019-03-15 stsp }
145 f9267c9a 2019-03-15 stsp return err;
146 5261c201 2018-04-01 stsp }
147 5261c201 2018-04-01 stsp
148 5261c201 2018-04-01 stsp static const struct got_error *
149 f9267c9a 2019-03-15 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
150 f9267c9a 2019-03-15 stsp {
151 f9267c9a 2019-03-15 stsp if (line[0] == '\0')
152 f9267c9a 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
153 f9267c9a 2019-03-15 stsp
154 f9267c9a 2019-03-15 stsp return alloc_symref(ref, name, line, 0);
155 f9267c9a 2019-03-15 stsp }
156 f9267c9a 2019-03-15 stsp
157 f9267c9a 2019-03-15 stsp static const struct got_error *
158 3f338f0a 2021-07-27 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line,
159 b54930d5 2023-02-28 op time_t mtime, enum got_hash_algorithm algo)
160 5261c201 2018-04-01 stsp {
161 5892cdd6 2019-03-10 stsp struct got_object_id id;
162 5261c201 2018-04-01 stsp
163 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
164 5261c201 2018-04-01 stsp line += 5;
165 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
166 5261c201 2018-04-01 stsp }
167 5261c201 2018-04-01 stsp
168 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, line, algo))
169 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
170 5261c201 2018-04-01 stsp
171 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, 0, mtime);
172 5261c201 2018-04-01 stsp }
173 5261c201 2018-04-01 stsp
174 5261c201 2018-04-01 stsp static const struct got_error *
175 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
176 b54930d5 2023-02-28 op const char *absname, const char *abspath, int lock,
177 b54930d5 2023-02-28 op enum got_hash_algorithm algo)
178 5261c201 2018-04-01 stsp {
179 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
180 c0a1c016 2019-03-15 stsp FILE *f;
181 c30018ad 2019-10-21 stsp char *line = NULL;
182 c30018ad 2019-10-21 stsp size_t linesize = 0;
183 c30018ad 2019-10-21 stsp ssize_t linelen;
184 2f17228e 2019-05-12 stsp struct got_lockfile *lf = NULL;
185 3f338f0a 2021-07-27 stsp struct stat sb;
186 5261c201 2018-04-01 stsp
187 2f17228e 2019-05-12 stsp if (lock) {
188 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, abspath, -1);
189 9f142382 2020-03-21 stsp if (err) {
190 9f142382 2020-03-21 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
191 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
192 9f142382 2020-03-21 stsp return err;
193 9f142382 2020-03-21 stsp }
194 2f17228e 2019-05-12 stsp }
195 2f17228e 2019-05-12 stsp
196 00fe21f2 2021-12-31 stsp f = fopen(abspath, "rbe");
197 f5c58ad1 2019-05-12 stsp if (f == NULL) {
198 b2070a3f 2020-03-22 stsp if (errno != ENOTDIR && errno != ENOENT)
199 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("fopen", abspath);
200 b2070a3f 2020-03-22 stsp else
201 b2070a3f 2020-03-22 stsp err = got_error_not_ref(name);
202 f5c58ad1 2019-05-12 stsp if (lock)
203 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
204 b2070a3f 2020-03-22 stsp return err;
205 f5c58ad1 2019-05-12 stsp }
206 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
207 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", abspath);
208 3f338f0a 2021-07-27 stsp goto done;
209 3f338f0a 2021-07-27 stsp }
210 5261c201 2018-04-01 stsp
211 c30018ad 2019-10-21 stsp linelen = getline(&line, &linesize, f);
212 c30018ad 2019-10-21 stsp if (linelen == -1) {
213 c30018ad 2019-10-21 stsp if (feof(f))
214 c30018ad 2019-10-21 stsp err = NULL; /* ignore empty files (could be locks) */
215 b2070a3f 2020-03-22 stsp else {
216 b2070a3f 2020-03-22 stsp if (errno == EISDIR)
217 b2070a3f 2020-03-22 stsp err = got_error(GOT_ERR_NOT_REF);
218 b2070a3f 2020-03-22 stsp else if (ferror(f))
219 b2070a3f 2020-03-22 stsp err = got_ferror(f, GOT_ERR_IO);
220 b2070a3f 2020-03-22 stsp else
221 b2070a3f 2020-03-22 stsp err = got_error_from_errno2("getline", abspath);
222 b2070a3f 2020-03-22 stsp }
223 f5c58ad1 2019-05-12 stsp if (lock)
224 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
225 5261c201 2018-04-01 stsp goto done;
226 5261c201 2018-04-01 stsp }
227 c30018ad 2019-10-21 stsp while (linelen > 0 && line[linelen - 1] == '\n') {
228 c30018ad 2019-10-21 stsp line[linelen - 1] = '\0';
229 c30018ad 2019-10-21 stsp linelen--;
230 c30018ad 2019-10-21 stsp }
231 5261c201 2018-04-01 stsp
232 b54930d5 2023-02-28 op err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
233 f5c58ad1 2019-05-12 stsp if (lock) {
234 f5c58ad1 2019-05-12 stsp if (err)
235 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
236 f5c58ad1 2019-05-12 stsp else {
237 f5c58ad1 2019-05-12 stsp if (*ref)
238 f5c58ad1 2019-05-12 stsp (*ref)->lf = lf;
239 f5c58ad1 2019-05-12 stsp else
240 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
241 f5c58ad1 2019-05-12 stsp }
242 f5c58ad1 2019-05-12 stsp }
243 5261c201 2018-04-01 stsp done:
244 5261c201 2018-04-01 stsp free(line);
245 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
247 2f17228e 2019-05-12 stsp if (*ref) {
248 f5c58ad1 2019-05-12 stsp if (lock)
249 f5c58ad1 2019-05-12 stsp got_ref_unlock(*ref);
250 2f17228e 2019-05-12 stsp got_ref_close(*ref);
251 2f17228e 2019-05-12 stsp *ref = NULL;
252 2f17228e 2019-05-12 stsp }
253 2f17228e 2019-05-12 stsp }
254 5261c201 2018-04-01 stsp return err;
255 5261c201 2018-04-01 stsp }
256 5261c201 2018-04-01 stsp
257 c5f754cc 2019-02-01 stsp static int
258 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
259 5261c201 2018-04-01 stsp {
260 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
261 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
262 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
263 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
264 c5f754cc 2019-02-01 stsp }
265 c5f754cc 2019-02-01 stsp
266 c5f754cc 2019-02-01 stsp static char *
267 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
268 c5f754cc 2019-02-01 stsp {
269 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
270 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
271 5261c201 2018-04-01 stsp
272 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
273 5892cdd6 2019-03-10 stsp }
274 5892cdd6 2019-03-10 stsp
275 5892cdd6 2019-03-10 stsp const struct got_error *
276 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
277 5892cdd6 2019-03-10 stsp struct got_object_id *id)
278 5892cdd6 2019-03-10 stsp {
279 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
280 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
281 f77a24b0 2019-03-11 stsp
282 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, id, 0, 0);
283 aaf88317 2019-07-10 stsp }
284 aaf88317 2019-07-10 stsp
285 aaf88317 2019-07-10 stsp const struct got_error *
286 aaf88317 2019-07-10 stsp got_ref_alloc_symref(struct got_reference **ref, const char *name,
287 aaf88317 2019-07-10 stsp struct got_reference *target_ref)
288 aaf88317 2019-07-10 stsp {
289 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
290 24b5452a 2019-10-09 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
291 aaf88317 2019-07-10 stsp
292 aaf88317 2019-07-10 stsp return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
293 5261c201 2018-04-01 stsp }
294 5261c201 2018-04-01 stsp
295 d1f2edc9 2018-06-13 stsp static const struct got_error *
296 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
297 b54930d5 2023-02-28 op const char *line, time_t mtime, enum got_hash_algorithm algo)
298 fb79db15 2019-02-01 stsp {
299 5892cdd6 2019-03-10 stsp struct got_object_id id;
300 5892cdd6 2019-03-10 stsp const char *name;
301 fb79db15 2019-02-01 stsp
302 fb79db15 2019-02-01 stsp *ref = NULL;
303 fb79db15 2019-02-01 stsp
304 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
305 fb79db15 2019-02-01 stsp return NULL;
306 fb79db15 2019-02-01 stsp
307 87a3ab84 2023-02-23 op if (!got_parse_object_id(&id, line, algo))
308 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
309 fb79db15 2019-02-01 stsp
310 199a4027 2019-02-02 stsp if (abs_refname) {
311 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
312 199a4027 2019-02-02 stsp return NULL;
313 5892cdd6 2019-03-10 stsp name = abs_refname;
314 5892cdd6 2019-03-10 stsp } else
315 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
316 fb79db15 2019-02-01 stsp
317 3f338f0a 2021-07-27 stsp return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
318 fb79db15 2019-02-01 stsp }
319 fb79db15 2019-02-01 stsp
320 fb79db15 2019-02-01 stsp static const struct got_error *
321 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
322 b54930d5 2023-02-28 op int nsubdirs, const char *refname, time_t mtime,
323 b54930d5 2023-02-28 op enum got_hash_algorithm algo)
324 fb79db15 2019-02-01 stsp {
325 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
326 fb79db15 2019-02-01 stsp char *abs_refname;
327 9bdd68dd 2020-01-02 naddy char *line = NULL;
328 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
329 9bdd68dd 2020-01-02 naddy ssize_t linelen;
330 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
331 fb79db15 2019-02-01 stsp
332 1e37702e 2019-02-04 stsp *ref = NULL;
333 1e37702e 2019-02-04 stsp
334 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
335 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
336 fb79db15 2019-02-01 stsp do {
337 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
338 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
339 7ab0422a 2019-03-15 stsp if (feof(f))
340 7ab0422a 2019-03-15 stsp break;
341 7ab0422a 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
342 fb79db15 2019-02-01 stsp break;
343 7ab0422a 2019-03-15 stsp }
344 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
345 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
346 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
347 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
348 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
349 0dec1cc0 2019-02-01 stsp refname) == -1)
350 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
351 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(ref, abs_refname, line,
352 b54930d5 2023-02-28 op mtime, algo);
353 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
354 0dec1cc0 2019-02-01 stsp free(abs_refname);
355 532920c8 2019-02-01 stsp if (err || *ref != NULL)
356 0dec1cc0 2019-02-01 stsp break;
357 0dec1cc0 2019-02-01 stsp }
358 fb79db15 2019-02-01 stsp if (err)
359 fb79db15 2019-02-01 stsp break;
360 fb79db15 2019-02-01 stsp } while (*ref == NULL);
361 9bdd68dd 2020-01-02 naddy free(line);
362 fb79db15 2019-02-01 stsp
363 fb79db15 2019-02-01 stsp return err;
364 fb79db15 2019-02-01 stsp }
365 fb79db15 2019-02-01 stsp
366 fb79db15 2019-02-01 stsp static const struct got_error *
367 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 b54930d5 2023-02-28 op const char *name, int lock, enum got_hash_algorithm algo)
369 d1f2edc9 2018-06-13 stsp {
370 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
371 a04f49d2 2019-02-04 stsp char *path = NULL;
372 a04f49d2 2019-02-04 stsp char *absname = NULL;
373 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
374 75236079 2020-03-25 stsp int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
375 d1f2edc9 2018-06-13 stsp
376 1e37702e 2019-02-04 stsp *ref = NULL;
377 1e37702e 2019-02-04 stsp
378 63e5aa5c 2021-08-23 stsp if (!got_ref_name_is_valid(name))
379 57c18198 2021-05-24 stsp return got_error_path(name, GOT_ERR_BAD_REF_NAME);
380 57c18198 2021-05-24 stsp
381 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
382 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
383 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
384 a04f49d2 2019-02-04 stsp absname = (char *)name;
385 a04f49d2 2019-02-04 stsp } else {
386 58908ed0 2019-03-11 stsp if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
387 58908ed0 2019-03-11 stsp subdir[0] ? "/" : "", name) == -1)
388 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
389 d1f2edc9 2018-06-13 stsp
390 58908ed0 2019-03-11 stsp if (asprintf(&absname, "refs/%s%s%s",
391 58908ed0 2019-03-11 stsp subdir, subdir[0] ? "/" : "", name) == -1) {
392 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
393 a04f49d2 2019-02-04 stsp goto done;
394 a04f49d2 2019-02-04 stsp }
395 a04f49d2 2019-02-04 stsp }
396 a04f49d2 2019-02-04 stsp
397 b54930d5 2023-02-28 op err = parse_ref_file(ref, name, absname, path, lock, algo);
398 d1f2edc9 2018-06-13 stsp done:
399 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
400 a04f49d2 2019-02-04 stsp free(absname);
401 a04f49d2 2019-02-04 stsp free(path);
402 d1f2edc9 2018-06-13 stsp return err;
403 d1f2edc9 2018-06-13 stsp }
404 d1f2edc9 2018-06-13 stsp
405 5261c201 2018-04-01 stsp const struct got_error *
406 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
407 abc59930 2021-09-05 naddy const char *refname, int lock)
408 5261c201 2018-04-01 stsp {
409 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
410 c3821bef 2022-07-21 florian char *packed_refs_path = NULL, *path_refs = NULL;
411 fb79db15 2019-02-01 stsp const char *subdirs[] = {
412 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
413 fb79db15 2019-02-01 stsp };
414 16aeacf7 2020-11-26 stsp size_t i;
415 16aeacf7 2020-11-26 stsp int well_known = is_well_known_ref(refname);
416 a875589a 2019-05-12 stsp struct got_lockfile *lf = NULL;
417 1e37702e 2019-02-04 stsp
418 1e37702e 2019-02-04 stsp *ref = NULL;
419 e135804e 2019-02-10 stsp
420 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
421 e135804e 2019-02-10 stsp if (path_refs == NULL) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", refname);
423 e135804e 2019-02-10 stsp goto done;
424 e135804e 2019-02-10 stsp }
425 5261c201 2018-04-01 stsp
426 0885ce8f 2019-05-12 stsp if (well_known) {
427 b54930d5 2023-02-28 op err = open_ref(ref, path_refs, "", refname, lock,
428 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
429 0885ce8f 2019-05-12 stsp } else {
430 c5f754cc 2019-02-01 stsp FILE *f;
431 c5f754cc 2019-02-01 stsp
432 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
433 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
434 2f17228e 2019-05-12 stsp err = open_ref(ref, path_refs, subdirs[i], refname,
435 b54930d5 2023-02-28 op lock, got_repo_get_object_format(repo));
436 b2070a3f 2020-03-22 stsp if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
437 e135804e 2019-02-10 stsp goto done;
438 e135804e 2019-02-10 stsp }
439 e135804e 2019-02-10 stsp
440 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
441 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
442 638f9024 2019-05-13 stsp err = got_error_from_errno(
443 230a42bd 2019-05-11 jcs "got_repo_get_path_packed_refs");
444 e135804e 2019-02-10 stsp goto done;
445 e135804e 2019-02-10 stsp }
446 c5f754cc 2019-02-01 stsp
447 2f17228e 2019-05-12 stsp if (lock) {
448 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
449 2f17228e 2019-05-12 stsp if (err)
450 2f17228e 2019-05-12 stsp goto done;
451 2f17228e 2019-05-12 stsp }
452 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "rbe");
453 c5f754cc 2019-02-01 stsp if (f != NULL) {
454 3f338f0a 2021-07-27 stsp struct stat sb;
455 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
456 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat",
457 3f338f0a 2021-07-27 stsp packed_refs_path);
458 3f338f0a 2021-07-27 stsp goto done;
459 3f338f0a 2021-07-27 stsp }
460 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 b54930d5 2023-02-28 op refname, sb.st_mtime,
462 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
463 a875589a 2019-05-12 stsp if (!err) {
464 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
465 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
466 a875589a 2019-05-12 stsp got_ref_close(*ref);
467 a875589a 2019-05-12 stsp *ref = NULL;
468 6e472abb 2019-05-13 stsp } else if (*ref)
469 a875589a 2019-05-12 stsp (*ref)->lf = lf;
470 a875589a 2019-05-12 stsp }
471 fb79db15 2019-02-01 stsp }
472 fb79db15 2019-02-01 stsp }
473 e135804e 2019-02-10 stsp done:
474 5b575c25 2019-05-12 stsp if (!err && *ref == NULL)
475 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
476 a875589a 2019-05-12 stsp if (err && lf)
477 5345b4c7 2021-07-06 stsp got_lockfile_unlock(lf, -1);
478 c3821bef 2022-07-21 florian free(packed_refs_path);
479 5261c201 2018-04-01 stsp free(path_refs);
480 5261c201 2018-04-01 stsp return err;
481 5261c201 2018-04-01 stsp }
482 5261c201 2018-04-01 stsp
483 5261c201 2018-04-01 stsp void
484 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
485 5261c201 2018-04-01 stsp {
486 e09d28b1 2019-03-15 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
488 e09d28b1 2019-03-15 stsp free(ref->ref.symref.ref);
489 e09d28b1 2019-03-15 stsp } else
490 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
491 5261c201 2018-04-01 stsp free(ref);
492 5261c201 2018-04-01 stsp }
493 5261c201 2018-04-01 stsp
494 5261c201 2018-04-01 stsp struct got_reference *
495 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
496 5261c201 2018-04-01 stsp {
497 5261c201 2018-04-01 stsp struct got_reference *ret;
498 5261c201 2018-04-01 stsp
499 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
500 5261c201 2018-04-01 stsp if (ret == NULL)
501 5261c201 2018-04-01 stsp return NULL;
502 5261c201 2018-04-01 stsp
503 5261c201 2018-04-01 stsp ret->flags = ref->flags;
504 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
506 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
507 5261c201 2018-04-01 stsp free(ret);
508 5261c201 2018-04-01 stsp return NULL;
509 5261c201 2018-04-01 stsp }
510 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
512 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
513 5261c201 2018-04-01 stsp free(ret);
514 5261c201 2018-04-01 stsp return NULL;
515 5261c201 2018-04-01 stsp }
516 5261c201 2018-04-01 stsp } else {
517 4c4ce67b 2020-12-25 stsp ret->ref.ref.name = strdup(ref->ref.ref.name);
518 4c4ce67b 2020-12-25 stsp if (ret->ref.ref.name == NULL) {
519 5261c201 2018-04-01 stsp free(ret);
520 5261c201 2018-04-01 stsp return NULL;
521 5261c201 2018-04-01 stsp }
522 faf5b56f 2023-01-31 op memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
523 faf5b56f 2023-01-31 op sizeof(ret->ref.ref.id));
524 5261c201 2018-04-01 stsp }
525 5261c201 2018-04-01 stsp
526 5261c201 2018-04-01 stsp return ret;
527 5261c201 2018-04-01 stsp }
528 b8bad2ba 2019-08-23 stsp
529 b8bad2ba 2019-08-23 stsp const struct got_error *
530 b8bad2ba 2019-08-23 stsp got_reflist_entry_dup(struct got_reflist_entry **newp,
531 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re)
532 b8bad2ba 2019-08-23 stsp {
533 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
534 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *new;
535 b8bad2ba 2019-08-23 stsp
536 b8bad2ba 2019-08-23 stsp *newp = NULL;
537 b8bad2ba 2019-08-23 stsp
538 b8bad2ba 2019-08-23 stsp new = malloc(sizeof(*new));
539 b8bad2ba 2019-08-23 stsp if (new == NULL)
540 b8bad2ba 2019-08-23 stsp return got_error_from_errno("malloc");
541 5261c201 2018-04-01 stsp
542 b8bad2ba 2019-08-23 stsp new->ref = got_ref_dup(re->ref);
543 b8bad2ba 2019-08-23 stsp if (new->ref == NULL) {
544 b8bad2ba 2019-08-23 stsp err = got_error_from_errno("got_ref_dup");
545 b8bad2ba 2019-08-23 stsp free(new);
546 b8bad2ba 2019-08-23 stsp return err;
547 b8bad2ba 2019-08-23 stsp }
548 b8bad2ba 2019-08-23 stsp
549 b8bad2ba 2019-08-23 stsp *newp = new;
550 b8bad2ba 2019-08-23 stsp return NULL;
551 b8bad2ba 2019-08-23 stsp }
552 b8bad2ba 2019-08-23 stsp
553 cce2f485 2021-08-22 stsp const struct got_error *
554 cce2f485 2021-08-22 stsp got_ref_resolve_symbolic(struct got_reference **resolved,
555 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
556 5261c201 2018-04-01 stsp {
557 5261c201 2018-04-01 stsp struct got_reference *nextref;
558 5261c201 2018-04-01 stsp const struct got_error *err;
559 5261c201 2018-04-01 stsp
560 2f17228e 2019-05-12 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
561 5261c201 2018-04-01 stsp if (err)
562 5261c201 2018-04-01 stsp return err;
563 5261c201 2018-04-01 stsp
564 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
565 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(resolved, repo, nextref);
566 5261c201 2018-04-01 stsp else
567 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
568 5261c201 2018-04-01 stsp
569 5261c201 2018-04-01 stsp got_ref_close(nextref);
570 5261c201 2018-04-01 stsp return err;
571 5261c201 2018-04-01 stsp }
572 5261c201 2018-04-01 stsp
573 29e86f7a 2019-08-12 stsp static const struct got_error *
574 29e86f7a 2019-08-12 stsp ref_resolve(struct got_object_id **id, struct got_repository *repo,
575 29e86f7a 2019-08-12 stsp struct got_reference *ref, int recursion)
576 5261c201 2018-04-01 stsp {
577 5261c201 2018-04-01 stsp const struct got_error *err;
578 5261c201 2018-04-01 stsp
579 29e86f7a 2019-08-12 stsp if (recursion <= 0)
580 29e86f7a 2019-08-12 stsp return got_error_msg(GOT_ERR_RECURSION,
581 29e86f7a 2019-08-12 stsp "reference recursion limit reached");
582 29e86f7a 2019-08-12 stsp
583 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
584 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
585 cce2f485 2021-08-22 stsp err = got_ref_resolve_symbolic(&resolved, repo, ref);
586 5261c201 2018-04-01 stsp if (err == NULL)
587 29e86f7a 2019-08-12 stsp err = ref_resolve(id, repo, resolved, --recursion);
588 57b6f99a 2019-04-13 stsp if (resolved)
589 57b6f99a 2019-04-13 stsp got_ref_close(resolved);
590 5261c201 2018-04-01 stsp return err;
591 5261c201 2018-04-01 stsp }
592 5261c201 2018-04-01 stsp
593 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
594 5261c201 2018-04-01 stsp if (*id == NULL)
595 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
596 faf5b56f 2023-01-31 op memcpy(*id, &ref->ref.ref.id, sizeof(**id));
597 5261c201 2018-04-01 stsp return NULL;
598 29e86f7a 2019-08-12 stsp }
599 29e86f7a 2019-08-12 stsp
600 29e86f7a 2019-08-12 stsp const struct got_error *
601 29e86f7a 2019-08-12 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 29e86f7a 2019-08-12 stsp struct got_reference *ref)
603 29e86f7a 2019-08-12 stsp {
604 29e86f7a 2019-08-12 stsp return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
605 5261c201 2018-04-01 stsp }
606 5261c201 2018-04-01 stsp
607 5261c201 2018-04-01 stsp char *
608 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
609 5261c201 2018-04-01 stsp {
610 5261c201 2018-04-01 stsp char *str;
611 271d2a38 2018-12-25 stsp
612 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
613 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
614 271d2a38 2018-12-25 stsp
615 faf5b56f 2023-01-31 op if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
616 271d2a38 2018-12-25 stsp return NULL;
617 5261c201 2018-04-01 stsp
618 5261c201 2018-04-01 stsp return str;
619 5261c201 2018-04-01 stsp }
620 0bd18d37 2019-02-01 stsp
621 0bd18d37 2019-02-01 stsp const char *
622 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
623 0bd18d37 2019-02-01 stsp {
624 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
625 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
626 0bd18d37 2019-02-01 stsp
627 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
628 199a4027 2019-02-02 stsp }
629 199a4027 2019-02-02 stsp
630 aaf88317 2019-07-10 stsp const char *
631 aaf88317 2019-07-10 stsp got_ref_get_symref_target(struct got_reference *ref)
632 aaf88317 2019-07-10 stsp {
633 aaf88317 2019-07-10 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 aaf88317 2019-07-10 stsp return ref->ref.symref.ref;
635 b8bad2ba 2019-08-23 stsp
636 b8bad2ba 2019-08-23 stsp return NULL;
637 3f338f0a 2021-07-27 stsp }
638 3f338f0a 2021-07-27 stsp
639 3f338f0a 2021-07-27 stsp time_t
640 3f338f0a 2021-07-27 stsp got_ref_get_mtime(struct got_reference *ref)
641 3f338f0a 2021-07-27 stsp {
642 3f338f0a 2021-07-27 stsp return ref->mtime;
643 b8bad2ba 2019-08-23 stsp }
644 b8bad2ba 2019-08-23 stsp
645 b8bad2ba 2019-08-23 stsp const struct got_error *
646 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
647 b8bad2ba 2019-08-23 stsp struct got_reference* re2)
648 b8bad2ba 2019-08-23 stsp {
649 b8bad2ba 2019-08-23 stsp const char *name1 = got_ref_get_name(re1);
650 b8bad2ba 2019-08-23 stsp const char *name2 = got_ref_get_name(re2);
651 aaf88317 2019-07-10 stsp
652 b8bad2ba 2019-08-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
653 aaf88317 2019-07-10 stsp return NULL;
654 7f66531d 2021-11-16 stsp }
655 7f66531d 2021-11-16 stsp
656 7f66531d 2021-11-16 stsp static const struct got_error *
657 7f66531d 2021-11-16 stsp get_committer_time(struct got_reference *ref, struct got_repository *repo)
658 7f66531d 2021-11-16 stsp {
659 7f66531d 2021-11-16 stsp const struct got_error *err = NULL;
660 7f66531d 2021-11-16 stsp int obj_type;
661 7f66531d 2021-11-16 stsp struct got_commit_object *commit = NULL;
662 7f66531d 2021-11-16 stsp struct got_tag_object *tag = NULL;
663 7f66531d 2021-11-16 stsp struct got_object_id *id = NULL;
664 7f66531d 2021-11-16 stsp
665 7f66531d 2021-11-16 stsp err = got_ref_resolve(&id, repo, ref);
666 7f66531d 2021-11-16 stsp if (err)
667 7f66531d 2021-11-16 stsp return err;
668 7f66531d 2021-11-16 stsp
669 7f66531d 2021-11-16 stsp err = got_object_get_type(&obj_type, repo, id);
670 7f66531d 2021-11-16 stsp if (err)
671 7f66531d 2021-11-16 stsp goto done;
672 7f66531d 2021-11-16 stsp
673 7f66531d 2021-11-16 stsp switch (obj_type) {
674 7f66531d 2021-11-16 stsp case GOT_OBJ_TYPE_COMMIT:
675 7f66531d 2021-11-16 stsp err = got_object_open_as_commit(&commit, repo, id);
676 7f66531d 2021-11-16 stsp if (err)
677 7f66531d 2021-11-16 stsp goto done;
678 7f66531d 2021-11-16 stsp ref->committer_time =
679 7f66531d 2021-11-16 stsp got_object_commit_get_committer_time(commit);
680 7f66531d 2021-11-16 stsp break;
681 7f66531d 2021-11-16 stsp case GOT_OBJ_TYPE_TAG:
682 7f66531d 2021-11-16 stsp err = got_object_open_as_tag(&tag, repo, id);
683 7f66531d 2021-11-16 stsp if (err)
684 7f66531d 2021-11-16 stsp goto done;
685 7f66531d 2021-11-16 stsp ref->committer_time = got_object_tag_get_tagger_time(tag);
686 7f66531d 2021-11-16 stsp break;
687 7f66531d 2021-11-16 stsp default:
688 7f66531d 2021-11-16 stsp /* best effort for other object types */
689 7f66531d 2021-11-16 stsp ref->committer_time = got_ref_get_mtime(ref);
690 7f66531d 2021-11-16 stsp break;
691 7f66531d 2021-11-16 stsp }
692 7f66531d 2021-11-16 stsp done:
693 7f66531d 2021-11-16 stsp free(id);
694 7f66531d 2021-11-16 stsp if (commit)
695 7f66531d 2021-11-16 stsp got_object_commit_close(commit);
696 7f66531d 2021-11-16 stsp if (tag)
697 7f66531d 2021-11-16 stsp got_object_tag_close(tag);
698 e600f124 2021-03-21 stsp return err;
699 e600f124 2021-03-21 stsp }
700 e600f124 2021-03-21 stsp
701 e600f124 2021-03-21 stsp const struct got_error *
702 c5d61cbc 2024-03-16 stsp got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
703 c5d61cbc 2024-03-16 stsp struct got_reference *ref2)
704 c5d61cbc 2024-03-16 stsp {
705 c5d61cbc 2024-03-16 stsp const struct got_error *err = NULL;
706 c5d61cbc 2024-03-16 stsp struct got_repository *repo = arg;
707 c5d61cbc 2024-03-16 stsp
708 c5d61cbc 2024-03-16 stsp *cmp = 0;
709 c5d61cbc 2024-03-16 stsp
710 c5d61cbc 2024-03-16 stsp if (ref1->committer_time == 0) {
711 c5d61cbc 2024-03-16 stsp err = get_committer_time(ref1, repo);
712 c5d61cbc 2024-03-16 stsp if (err)
713 c5d61cbc 2024-03-16 stsp return err;
714 c5d61cbc 2024-03-16 stsp }
715 c5d61cbc 2024-03-16 stsp if (ref2->committer_time == 0) {
716 c5d61cbc 2024-03-16 stsp err = get_committer_time(ref2, repo);
717 c5d61cbc 2024-03-16 stsp if (err)
718 c5d61cbc 2024-03-16 stsp return err;
719 c5d61cbc 2024-03-16 stsp }
720 c5d61cbc 2024-03-16 stsp
721 c5d61cbc 2024-03-16 stsp /* Put latest tags first. */
722 c5d61cbc 2024-03-16 stsp if (ref1->committer_time < ref2->committer_time)
723 c5d61cbc 2024-03-16 stsp *cmp = 1;
724 c5d61cbc 2024-03-16 stsp else if (ref1->committer_time > ref2->committer_time)
725 c5d61cbc 2024-03-16 stsp *cmp = -1;
726 c5d61cbc 2024-03-16 stsp else
727 c5d61cbc 2024-03-16 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 c5d61cbc 2024-03-16 stsp
729 c5d61cbc 2024-03-16 stsp return err;
730 c5d61cbc 2024-03-16 stsp }
731 c5d61cbc 2024-03-16 stsp
732 c5d61cbc 2024-03-16 stsp const struct got_error *
733 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
734 e600f124 2021-03-21 stsp struct got_reference *ref1, struct got_reference *ref2)
735 e600f124 2021-03-21 stsp {
736 0309152a 2021-11-20 stsp const struct got_error *err = NULL;
737 e600f124 2021-03-21 stsp struct got_repository *repo = arg;
738 e600f124 2021-03-21 stsp
739 e600f124 2021-03-21 stsp *cmp = 0;
740 e600f124 2021-03-21 stsp
741 2c9e323b 2021-10-10 stsp if (ref1->committer_time == 0) {
742 7f66531d 2021-11-16 stsp err = get_committer_time(ref1, repo);
743 2c9e323b 2021-10-10 stsp if (err)
744 2c9e323b 2021-10-10 stsp return err;
745 2c9e323b 2021-10-10 stsp }
746 7f66531d 2021-11-16 stsp if (ref2->committer_time == 0) {
747 7f66531d 2021-11-16 stsp err = get_committer_time(ref2, repo);
748 2c9e323b 2021-10-10 stsp if (err)
749 2c9e323b 2021-10-10 stsp return err;
750 2c9e323b 2021-10-10 stsp }
751 2c9e323b 2021-10-10 stsp
752 2c9e323b 2021-10-10 stsp if (ref1->committer_time < ref2->committer_time)
753 e600f124 2021-03-21 stsp *cmp = 1;
754 2c9e323b 2021-10-10 stsp else if (ref2->committer_time < ref1->committer_time)
755 e600f124 2021-03-21 stsp *cmp = -1;
756 42864987 2021-11-20 stsp else
757 42864987 2021-11-20 stsp return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
758 7f66531d 2021-11-16 stsp
759 d1f16636 2020-01-15 stsp return err;
760 aaf88317 2019-07-10 stsp }
761 aaf88317 2019-07-10 stsp
762 779e1159 2021-06-18 stsp const struct got_error *
763 c0df5966 2021-12-31 stsp got_reflist_insert(struct got_reflist_entry **newp,
764 c0df5966 2021-12-31 stsp struct got_reflist_head *refs, struct got_reference *ref,
765 c0df5966 2021-12-31 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
766 199a4027 2019-02-02 stsp {
767 199a4027 2019-02-02 stsp const struct got_error *err;
768 d9dff0e5 2020-12-26 stsp struct got_reflist_entry *new, *re;
769 e397b6db 2019-02-04 stsp int cmp;
770 7a3c76f5 2019-02-05 stsp
771 505287be 2019-03-15 stsp *newp = NULL;
772 7a3c76f5 2019-02-05 stsp
773 3d8e0c5e 2022-09-05 stsp if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
774 7db2b0dd 2022-09-05 stsp /*
775 7db2b0dd 2022-09-05 stsp * If we are not sorting elements by name then we must still
776 7db2b0dd 2022-09-05 stsp * detect collisions between a packed ref and an on-disk ref
777 7db2b0dd 2022-09-05 stsp * using the same name. On-disk refs take precedence and are
778 7db2b0dd 2022-09-05 stsp * already present on the list before packed refs get added.
779 7db2b0dd 2022-09-05 stsp */
780 7db2b0dd 2022-09-05 stsp TAILQ_FOREACH(re, refs, entry) {
781 3d8e0c5e 2022-09-05 stsp err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
782 7db2b0dd 2022-09-05 stsp if (err)
783 7db2b0dd 2022-09-05 stsp return err;
784 3d8e0c5e 2022-09-05 stsp if (cmp == 0)
785 7db2b0dd 2022-09-05 stsp return NULL;
786 7db2b0dd 2022-09-05 stsp }
787 7db2b0dd 2022-09-05 stsp }
788 7db2b0dd 2022-09-05 stsp
789 3d8e0c5e 2022-09-05 stsp new = malloc(sizeof(*new));
790 3d8e0c5e 2022-09-05 stsp if (new == NULL)
791 3d8e0c5e 2022-09-05 stsp return got_error_from_errno("malloc");
792 3d8e0c5e 2022-09-05 stsp new->ref = ref;
793 3d8e0c5e 2022-09-05 stsp *newp = new;
794 3d8e0c5e 2022-09-05 stsp
795 29b5c214 2019-02-04 stsp /*
796 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
797 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
798 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
799 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
800 d9dff0e5 2020-12-26 stsp *
801 d9dff0e5 2020-12-26 stsp * Many callers will provide paths in a somewhat sorted order.
802 d9dff0e5 2020-12-26 stsp * Iterating backwards from the tail of the list should be more
803 d9dff0e5 2020-12-26 stsp * efficient than traversing through the entire list each time
804 d9dff0e5 2020-12-26 stsp * an element is inserted.
805 29b5c214 2019-02-04 stsp */
806 d9dff0e5 2020-12-26 stsp re = TAILQ_LAST(refs, got_reflist_head);
807 7a3c76f5 2019-02-05 stsp while (re) {
808 b8bad2ba 2019-08-23 stsp err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
809 b8bad2ba 2019-08-23 stsp if (err)
810 b8bad2ba 2019-08-23 stsp return err;
811 e397b6db 2019-02-04 stsp if (cmp == 0) {
812 27a1ed03 2019-03-15 stsp /* duplicate */
813 27a1ed03 2019-03-15 stsp free(new);
814 505287be 2019-03-15 stsp *newp = NULL;
815 7a3c76f5 2019-02-05 stsp return NULL;
816 d9dff0e5 2020-12-26 stsp } else if (cmp < 0) {
817 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_AFTER(refs, re, new, entry);
818 7a3c76f5 2019-02-05 stsp return NULL;
819 7a3c76f5 2019-02-05 stsp }
820 d9dff0e5 2020-12-26 stsp re = TAILQ_PREV(re, got_reflist_head, entry);
821 29b5c214 2019-02-04 stsp }
822 199a4027 2019-02-02 stsp
823 d9dff0e5 2020-12-26 stsp TAILQ_INSERT_HEAD(refs, new, entry);
824 199a4027 2019-02-02 stsp return NULL;
825 0bd18d37 2019-02-01 stsp }
826 2d497592 2021-11-20 stsp
827 2d497592 2021-11-20 stsp const struct got_error *
828 2d497592 2021-11-20 stsp got_reflist_sort(struct got_reflist_head *refs,
829 2d497592 2021-11-20 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
830 2d497592 2021-11-20 stsp {
831 2d497592 2021-11-20 stsp const struct got_error *err = NULL;
832 2d497592 2021-11-20 stsp struct got_reflist_entry *re, *tmp, *new;
833 2d497592 2021-11-20 stsp struct got_reflist_head sorted;
834 199a4027 2019-02-02 stsp
835 2d497592 2021-11-20 stsp TAILQ_INIT(&sorted);
836 2d497592 2021-11-20 stsp
837 2d497592 2021-11-20 stsp TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
838 2d497592 2021-11-20 stsp struct got_reference *ref = re->ref;
839 2d497592 2021-11-20 stsp TAILQ_REMOVE(refs, re, entry);
840 2d497592 2021-11-20 stsp free(re);
841 2d497592 2021-11-20 stsp err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
842 2d497592 2021-11-20 stsp if (err || new == NULL /* duplicate */)
843 2d497592 2021-11-20 stsp got_ref_close(ref);
844 2d497592 2021-11-20 stsp if (err)
845 2d497592 2021-11-20 stsp return err;
846 2d497592 2021-11-20 stsp }
847 2d497592 2021-11-20 stsp
848 2d497592 2021-11-20 stsp TAILQ_CONCAT(refs, &sorted, entry);
849 2d497592 2021-11-20 stsp return NULL;
850 2d497592 2021-11-20 stsp }
851 2d497592 2021-11-20 stsp
852 a04f49d2 2019-02-04 stsp static const struct got_error *
853 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
854 b8bad2ba 2019-08-23 stsp const char *subdir, struct got_repository *repo,
855 b8bad2ba 2019-08-23 stsp got_ref_cmp_cb cmp_cb, void *cmp_arg)
856 a04f49d2 2019-02-04 stsp {
857 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
858 a04f49d2 2019-02-04 stsp DIR *d = NULL;
859 a04f49d2 2019-02-04 stsp char *path_subdir;
860 0f8a0e25 2024-03-19 stsp struct got_reference *ref;
861 0f8a0e25 2024-03-19 stsp struct got_reflist_entry *new;
862 b2070a3f 2020-03-22 stsp
863 b2070a3f 2020-03-22 stsp while (subdir[0] == '/')
864 b2070a3f 2020-03-22 stsp subdir++;
865 a04f49d2 2019-02-04 stsp
866 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
867 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
868 a04f49d2 2019-02-04 stsp
869 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
870 0f8a0e25 2024-03-19 stsp if (d == NULL) {
871 0f8a0e25 2024-03-19 stsp char *refname;
872 a04f49d2 2019-02-04 stsp
873 0f8a0e25 2024-03-19 stsp if (errno != ENOTDIR)
874 0f8a0e25 2024-03-19 stsp goto done;
875 0f8a0e25 2024-03-19 stsp
876 0f8a0e25 2024-03-19 stsp /* This could be a regular on-disk reference file. */
877 0f8a0e25 2024-03-19 stsp free(path_subdir);
878 0f8a0e25 2024-03-19 stsp err = got_path_dirname(&path_subdir, subdir);
879 0f8a0e25 2024-03-19 stsp if (err)
880 0f8a0e25 2024-03-19 stsp return err;
881 0f8a0e25 2024-03-19 stsp err = got_path_basename(&refname, subdir);
882 0f8a0e25 2024-03-19 stsp if (err) {
883 0f8a0e25 2024-03-19 stsp free(path_subdir);
884 0f8a0e25 2024-03-19 stsp return err;
885 0f8a0e25 2024-03-19 stsp }
886 0f8a0e25 2024-03-19 stsp err = open_ref(&ref, path_refs, path_subdir, refname,
887 0f8a0e25 2024-03-19 stsp 0, got_repo_get_object_format(repo));
888 0f8a0e25 2024-03-19 stsp free(path_subdir);
889 0f8a0e25 2024-03-19 stsp free(refname);
890 0f8a0e25 2024-03-19 stsp if (err) {
891 0f8a0e25 2024-03-19 stsp if (err->code == GOT_ERR_NOT_REF)
892 0f8a0e25 2024-03-19 stsp return NULL;
893 0f8a0e25 2024-03-19 stsp return err;
894 0f8a0e25 2024-03-19 stsp }
895 0f8a0e25 2024-03-19 stsp err = got_reflist_insert(&new, refs, ref,
896 0f8a0e25 2024-03-19 stsp cmp_cb, cmp_arg);
897 0f8a0e25 2024-03-19 stsp if (err || new == NULL /* duplicate */)
898 0f8a0e25 2024-03-19 stsp got_ref_close(ref);
899 0f8a0e25 2024-03-19 stsp return err;
900 0f8a0e25 2024-03-19 stsp }
901 0f8a0e25 2024-03-19 stsp
902 656b1f76 2019-05-11 jcs for (;;) {
903 a04f49d2 2019-02-04 stsp struct dirent *dent;
904 a04f49d2 2019-02-04 stsp char *child;
905 20ccae39 2020-07-21 stsp int type;
906 a04f49d2 2019-02-04 stsp
907 a04f49d2 2019-02-04 stsp dent = readdir(d);
908 a04f49d2 2019-02-04 stsp if (dent == NULL)
909 a04f49d2 2019-02-04 stsp break;
910 a04f49d2 2019-02-04 stsp
911 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
912 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
913 a04f49d2 2019-02-04 stsp continue;
914 a04f49d2 2019-02-04 stsp
915 20ccae39 2020-07-21 stsp err = got_path_dirent_type(&type, path_subdir, dent);
916 20ccae39 2020-07-21 stsp if (err)
917 20ccae39 2020-07-21 stsp break;
918 20ccae39 2020-07-21 stsp
919 20ccae39 2020-07-21 stsp switch (type) {
920 a04f49d2 2019-02-04 stsp case DT_REG:
921 2f17228e 2019-05-12 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name,
922 b54930d5 2023-02-28 op 0, got_repo_get_object_format(repo));
923 2f3ccc5f 2023-06-16 op if (err && err->code == GOT_ERR_BAD_REF_NAME)
924 2f3ccc5f 2023-06-16 op break;
925 1e37702e 2019-02-04 stsp if (err)
926 a04f49d2 2019-02-04 stsp goto done;
927 a04f49d2 2019-02-04 stsp if (ref) {
928 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
929 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
930 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
931 505287be 2019-03-15 stsp got_ref_close(ref);
932 a04f49d2 2019-02-04 stsp if (err)
933 a04f49d2 2019-02-04 stsp goto done;
934 a04f49d2 2019-02-04 stsp }
935 a04f49d2 2019-02-04 stsp break;
936 a04f49d2 2019-02-04 stsp case DT_DIR:
937 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
938 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
940 a04f49d2 2019-02-04 stsp break;
941 a04f49d2 2019-02-04 stsp }
942 b8bad2ba 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs, child, repo,
943 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
944 a04f49d2 2019-02-04 stsp free(child);
945 a04f49d2 2019-02-04 stsp break;
946 a04f49d2 2019-02-04 stsp default:
947 a04f49d2 2019-02-04 stsp break;
948 a04f49d2 2019-02-04 stsp }
949 a04f49d2 2019-02-04 stsp }
950 a04f49d2 2019-02-04 stsp done:
951 a04f49d2 2019-02-04 stsp if (d)
952 a04f49d2 2019-02-04 stsp closedir(d);
953 a04f49d2 2019-02-04 stsp free(path_subdir);
954 a04f49d2 2019-02-04 stsp return err;
955 0f8a0e25 2024-03-19 stsp }
956 0f8a0e25 2024-03-19 stsp
957 0f8a0e25 2024-03-19 stsp static int
958 0f8a0e25 2024-03-19 stsp match_packed_ref(struct got_reference *ref, const char *ref_namespace)
959 0f8a0e25 2024-03-19 stsp {
960 0f8a0e25 2024-03-19 stsp const char *name = got_ref_get_name(ref);
961 0f8a0e25 2024-03-19 stsp int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
962 0f8a0e25 2024-03-19 stsp
963 0f8a0e25 2024-03-19 stsp if (namespace_is_absolute) {
964 0f8a0e25 2024-03-19 stsp return (strcmp(name, ref_namespace) == 0 ||
965 0f8a0e25 2024-03-19 stsp got_path_is_child(name, ref_namespace,
966 0f8a0e25 2024-03-19 stsp strlen(ref_namespace)));
967 0f8a0e25 2024-03-19 stsp }
968 0f8a0e25 2024-03-19 stsp
969 0f8a0e25 2024-03-19 stsp /* Match all "subdirectories" as we do with on-disk refs. */
970 0f8a0e25 2024-03-19 stsp while (*name != '\0') {
971 0f8a0e25 2024-03-19 stsp while (*name == '/')
972 0f8a0e25 2024-03-19 stsp name++;
973 0f8a0e25 2024-03-19 stsp if (strcmp(name, ref_namespace) == 0 ||
974 0f8a0e25 2024-03-19 stsp got_path_is_child(name, ref_namespace,
975 0f8a0e25 2024-03-19 stsp strlen(ref_namespace)))
976 0f8a0e25 2024-03-19 stsp return 1;
977 0f8a0e25 2024-03-19 stsp while (*name != '\0' && *name != '/')
978 0f8a0e25 2024-03-19 stsp name++;
979 0f8a0e25 2024-03-19 stsp }
980 0f8a0e25 2024-03-19 stsp
981 0f8a0e25 2024-03-19 stsp return 0;
982 a04f49d2 2019-02-04 stsp }
983 a04f49d2 2019-02-04 stsp
984 199a4027 2019-02-02 stsp const struct got_error *
985 29606af7 2019-08-23 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
986 b8bad2ba 2019-08-23 stsp const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
987 199a4027 2019-02-02 stsp {
988 199a4027 2019-02-02 stsp const struct got_error *err;
989 c3821bef 2022-07-21 florian char *packed_refs_path = NULL, *path_refs = NULL;
990 58e31a80 2022-06-27 op char *abs_namespace = NULL, *buf = NULL;
991 58e31a80 2022-06-27 op const char *ondisk_ref_namespace = NULL;
992 9bdd68dd 2020-01-02 naddy char *line = NULL;
993 29b5c214 2019-02-04 stsp FILE *f = NULL;
994 199a4027 2019-02-02 stsp struct got_reference *ref;
995 505287be 2019-03-15 stsp struct got_reflist_entry *new;
996 199a4027 2019-02-02 stsp
997 29606af7 2019-08-23 stsp if (ref_namespace == NULL || ref_namespace[0] == '\0') {
998 29606af7 2019-08-23 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
999 29606af7 2019-08-23 stsp if (path_refs == NULL) {
1000 29606af7 2019-08-23 stsp err = got_error_from_errno("get_refs_dir_path");
1001 29606af7 2019-08-23 stsp goto done;
1002 29606af7 2019-08-23 stsp }
1003 b54930d5 2023-02-28 op err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1004 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
1005 29606af7 2019-08-23 stsp if (err)
1006 29606af7 2019-08-23 stsp goto done;
1007 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1008 29606af7 2019-08-23 stsp if (err || new == NULL /* duplicate */)
1009 29606af7 2019-08-23 stsp got_ref_close(ref);
1010 f68a7890 2020-03-19 stsp if (err && err->code != GOT_ERR_NOT_REF)
1011 29606af7 2019-08-23 stsp goto done;
1012 b2070a3f 2020-03-22 stsp } else {
1013 b2070a3f 2020-03-22 stsp /* Try listing a single reference. */
1014 0f8a0e25 2024-03-19 stsp err = got_ref_open(&ref, repo, ref_namespace, 0);
1015 b2070a3f 2020-03-22 stsp if (err) {
1016 b2070a3f 2020-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
1017 b2070a3f 2020-03-22 stsp goto done;
1018 b2070a3f 2020-03-22 stsp /* Try to look up references in a given namespace. */
1019 b2070a3f 2020-03-22 stsp } else {
1020 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1021 b2070a3f 2020-03-22 stsp cmp_cb, cmp_arg);
1022 b2070a3f 2020-03-22 stsp if (err || new == NULL /* duplicate */)
1023 b2070a3f 2020-03-22 stsp got_ref_close(ref);
1024 b2070a3f 2020-03-22 stsp return err;
1025 b2070a3f 2020-03-22 stsp }
1026 29b5c214 2019-02-04 stsp }
1027 29b5c214 2019-02-04 stsp
1028 b2070a3f 2020-03-22 stsp if (ref_namespace) {
1029 b2070a3f 2020-03-22 stsp size_t len;
1030 b2070a3f 2020-03-22 stsp /* Canonicalize the path to eliminate double-slashes if any. */
1031 b2070a3f 2020-03-22 stsp if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1032 b2070a3f 2020-03-22 stsp err = got_error_from_errno("asprintf");
1033 b2070a3f 2020-03-22 stsp goto done;
1034 b2070a3f 2020-03-22 stsp }
1035 b2070a3f 2020-03-22 stsp len = strlen(abs_namespace) + 1;
1036 b2070a3f 2020-03-22 stsp buf = malloc(len);
1037 b2070a3f 2020-03-22 stsp if (buf == NULL) {
1038 b2070a3f 2020-03-22 stsp err = got_error_from_errno("malloc");
1039 b2070a3f 2020-03-22 stsp goto done;
1040 b2070a3f 2020-03-22 stsp }
1041 b2070a3f 2020-03-22 stsp err = got_canonpath(abs_namespace, buf, len);
1042 b2070a3f 2020-03-22 stsp if (err)
1043 b2070a3f 2020-03-22 stsp goto done;
1044 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = buf;
1045 b2070a3f 2020-03-22 stsp while (ondisk_ref_namespace[0] == '/')
1046 b2070a3f 2020-03-22 stsp ondisk_ref_namespace++;
1047 b2070a3f 2020-03-22 stsp if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1048 b2070a3f 2020-03-22 stsp ondisk_ref_namespace += 5;
1049 b2070a3f 2020-03-22 stsp else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1050 b2070a3f 2020-03-22 stsp ondisk_ref_namespace = "";
1051 b2070a3f 2020-03-22 stsp }
1052 29606af7 2019-08-23 stsp
1053 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
1054 29b5c214 2019-02-04 stsp free(path_refs);
1055 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
1056 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
1057 638f9024 2019-05-13 stsp err = got_error_from_errno("get_refs_dir_path");
1058 29b5c214 2019-02-04 stsp goto done;
1059 29b5c214 2019-02-04 stsp }
1060 29606af7 2019-08-23 stsp err = gather_on_disk_refs(refs, path_refs,
1061 6aeab596 2019-08-28 stsp ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1062 6aeab596 2019-08-28 stsp cmp_cb, cmp_arg);
1063 29b5c214 2019-02-04 stsp if (err)
1064 29b5c214 2019-02-04 stsp goto done;
1065 29b5c214 2019-02-04 stsp
1066 29b5c214 2019-02-04 stsp /*
1067 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
1068 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
1069 29b5c214 2019-02-04 stsp */
1070 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1071 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno("got_repo_get_path_packed_refs");
1073 29b5c214 2019-02-04 stsp goto done;
1074 29b5c214 2019-02-04 stsp }
1075 199a4027 2019-02-02 stsp
1076 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "re");
1077 199a4027 2019-02-02 stsp if (f) {
1078 7a90b680 2020-01-02 naddy size_t linesize = 0;
1079 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1080 3f338f0a 2021-07-27 stsp struct stat sb;
1081 3f338f0a 2021-07-27 stsp
1082 3f338f0a 2021-07-27 stsp if (fstat(fileno(f), &sb) == -1) {
1083 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("fstat", packed_refs_path);
1084 3f338f0a 2021-07-27 stsp goto done;
1085 3f338f0a 2021-07-27 stsp }
1086 656b1f76 2019-05-11 jcs for (;;) {
1087 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1088 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1089 0bb4abae 2019-03-15 stsp if (feof(f))
1090 0bb4abae 2019-03-15 stsp break;
1091 0bb4abae 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1092 0bb4abae 2019-03-15 stsp goto done;
1093 0bb4abae 2019-03-15 stsp }
1094 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1095 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1096 3f338f0a 2021-07-27 stsp err = parse_packed_ref_line(&ref, NULL, line,
1097 b54930d5 2023-02-28 op sb.st_mtime, got_repo_get_object_format(repo));
1098 199a4027 2019-02-02 stsp if (err)
1099 199a4027 2019-02-02 stsp goto done;
1100 76b4ead2 2019-02-03 stsp if (ref) {
1101 0f8a0e25 2024-03-19 stsp if (ref_namespace &&
1102 0f8a0e25 2024-03-19 stsp !match_packed_ref(ref, ref_namespace)) {
1103 0f8a0e25 2024-03-19 stsp got_ref_close(ref);
1104 0f8a0e25 2024-03-19 stsp continue;
1105 29606af7 2019-08-23 stsp }
1106 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, refs, ref,
1107 b8bad2ba 2019-08-23 stsp cmp_cb, cmp_arg);
1108 505287be 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1109 505287be 2019-03-15 stsp got_ref_close(ref);
1110 76b4ead2 2019-02-03 stsp if (err)
1111 76b4ead2 2019-02-03 stsp goto done;
1112 76b4ead2 2019-02-03 stsp }
1113 199a4027 2019-02-02 stsp }
1114 199a4027 2019-02-02 stsp }
1115 199a4027 2019-02-02 stsp done:
1116 c3821bef 2022-07-21 florian free(packed_refs_path);
1117 b2070a3f 2020-03-22 stsp free(abs_namespace);
1118 b2070a3f 2020-03-22 stsp free(buf);
1119 9bdd68dd 2020-01-02 naddy free(line);
1120 a04f49d2 2019-02-04 stsp free(path_refs);
1121 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1123 199a4027 2019-02-02 stsp return err;
1124 199a4027 2019-02-02 stsp }
1125 9e672c74 2019-03-11 stsp
1126 e2e879a0 2019-03-11 stsp void
1127 e2e879a0 2019-03-11 stsp got_ref_list_free(struct got_reflist_head *refs)
1128 e2e879a0 2019-03-11 stsp {
1129 e2e879a0 2019-03-11 stsp struct got_reflist_entry *re;
1130 e2e879a0 2019-03-11 stsp
1131 d9dff0e5 2020-12-26 stsp while ((re = TAILQ_FIRST(refs))) {
1132 d9dff0e5 2020-12-26 stsp TAILQ_REMOVE(refs, re, entry);
1133 af8a5c4a 2021-05-21 stsp got_ref_close(re->ref);
1134 e2e879a0 2019-03-11 stsp free(re);
1135 e2e879a0 2019-03-11 stsp }
1136 b249b824 2019-05-09 stsp }
1137 b249b824 2019-05-09 stsp
1138 b249b824 2019-05-09 stsp int
1139 b249b824 2019-05-09 stsp got_ref_is_symbolic(struct got_reference *ref)
1140 b249b824 2019-05-09 stsp {
1141 b249b824 2019-05-09 stsp return (ref->flags & GOT_REF_IS_SYMBOLIC);
1142 b249b824 2019-05-09 stsp }
1143 b249b824 2019-05-09 stsp
1144 b249b824 2019-05-09 stsp const struct got_error *
1145 b249b824 2019-05-09 stsp got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1146 b249b824 2019-05-09 stsp {
1147 b249b824 2019-05-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
1148 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1149 e2e879a0 2019-03-11 stsp
1150 faf5b56f 2023-01-31 op memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1151 b249b824 2019-05-09 stsp return NULL;
1152 e2e879a0 2019-03-11 stsp }
1153 e2e879a0 2019-03-11 stsp
1154 9e672c74 2019-03-11 stsp const struct got_error *
1155 d7b899ab 2020-03-25 stsp got_ref_change_symref(struct got_reference *ref, const char *refname)
1156 b249b824 2019-05-09 stsp {
1157 b249b824 2019-05-09 stsp char *new_name;
1158 b249b824 2019-05-09 stsp
1159 b249b824 2019-05-09 stsp if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 b249b824 2019-05-09 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1161 b249b824 2019-05-09 stsp
1162 b249b824 2019-05-09 stsp new_name = strdup(refname);
1163 b249b824 2019-05-09 stsp if (new_name == NULL)
1164 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1165 b249b824 2019-05-09 stsp
1166 d7b899ab 2020-03-25 stsp free(ref->ref.symref.ref);
1167 d7b899ab 2020-03-25 stsp ref->ref.symref.ref = new_name;
1168 b249b824 2019-05-09 stsp return NULL;
1169 b249b824 2019-05-09 stsp }
1170 b249b824 2019-05-09 stsp
1171 b249b824 2019-05-09 stsp const struct got_error *
1172 e8a967e0 2020-03-21 stsp got_ref_change_symref_to_ref(struct got_reference *symref,
1173 e8a967e0 2020-03-21 stsp struct got_object_id *id)
1174 e8a967e0 2020-03-21 stsp {
1175 e8a967e0 2020-03-21 stsp if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1176 e8a967e0 2020-03-21 stsp return got_error(GOT_ERR_BAD_REF_TYPE);
1177 e8a967e0 2020-03-21 stsp
1178 e8a967e0 2020-03-21 stsp symref->ref.ref.name = symref->ref.symref.name;
1179 faf5b56f 2023-01-31 op memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1180 e8a967e0 2020-03-21 stsp symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1181 e8a967e0 2020-03-21 stsp return NULL;
1182 e8a967e0 2020-03-21 stsp }
1183 e8a967e0 2020-03-21 stsp
1184 e8a967e0 2020-03-21 stsp const struct got_error *
1185 9e672c74 2019-03-11 stsp got_ref_write(struct got_reference *ref, struct got_repository *repo)
1186 9e672c74 2019-03-11 stsp {
1187 9e672c74 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1188 9e672c74 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1189 9e672c74 2019-03-11 stsp char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1190 9e672c74 2019-03-11 stsp struct got_lockfile *lf = NULL;
1191 9e672c74 2019-03-11 stsp FILE *f = NULL;
1192 9e672c74 2019-03-11 stsp size_t n;
1193 9e672c74 2019-03-11 stsp struct stat sb;
1194 9e672c74 2019-03-11 stsp
1195 9e672c74 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1196 9e672c74 2019-03-11 stsp if (path_refs == NULL) {
1197 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1198 9e672c74 2019-03-11 stsp goto done;
1199 9e672c74 2019-03-11 stsp }
1200 9e672c74 2019-03-11 stsp
1201 9e672c74 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1202 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1203 9e672c74 2019-03-11 stsp goto done;
1204 9e672c74 2019-03-11 stsp }
1205 9e672c74 2019-03-11 stsp
1206 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &f, path, "");
1207 49c7094f 2019-03-11 stsp if (err) {
1208 d1667f0d 2019-03-11 stsp char *parent;
1209 49c7094f 2019-03-11 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1210 5e1c9f23 2019-03-11 stsp goto done;
1211 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, path);
1212 d1667f0d 2019-03-11 stsp if (err)
1213 0cd1c46a 2019-03-11 stsp goto done;
1214 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(parent);
1215 5e1c9f23 2019-03-11 stsp free(parent);
1216 0cd1c46a 2019-03-11 stsp if (err)
1217 0cd1c46a 2019-03-11 stsp goto done;
1218 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &f, path, "");
1219 49c7094f 2019-03-11 stsp if (err)
1220 0cd1c46a 2019-03-11 stsp goto done;
1221 9e672c74 2019-03-11 stsp }
1222 9e672c74 2019-03-11 stsp
1223 9e672c74 2019-03-11 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1224 9e672c74 2019-03-11 stsp n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1225 9e672c74 2019-03-11 stsp if (n != strlen(ref->ref.symref.ref) + 6) {
1226 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1227 9e672c74 2019-03-11 stsp goto done;
1228 9e672c74 2019-03-11 stsp }
1229 9e672c74 2019-03-11 stsp } else {
1230 faf5b56f 2023-01-31 op char *hex;
1231 faf5b56f 2023-01-31 op size_t len;
1232 faf5b56f 2023-01-31 op
1233 faf5b56f 2023-01-31 op err = got_object_id_str(&hex, &ref->ref.ref.id);
1234 faf5b56f 2023-01-31 op if (err)
1235 faf5b56f 2023-01-31 op goto done;
1236 faf5b56f 2023-01-31 op len = strlen(hex);
1237 9e672c74 2019-03-11 stsp n = fprintf(f, "%s\n", hex);
1238 faf5b56f 2023-01-31 op free(hex);
1239 faf5b56f 2023-01-31 op if (n != len + 1) {
1240 9e672c74 2019-03-11 stsp err = got_ferror(f, GOT_ERR_IO);
1241 9e672c74 2019-03-11 stsp goto done;
1242 9e672c74 2019-03-11 stsp }
1243 9e672c74 2019-03-11 stsp }
1244 9e672c74 2019-03-11 stsp
1245 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1246 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1247 2f17228e 2019-05-12 stsp if (err)
1248 2f17228e 2019-05-12 stsp goto done;
1249 2f17228e 2019-05-12 stsp }
1250 9e672c74 2019-03-11 stsp
1251 9e672c74 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1252 9e672c74 2019-03-11 stsp
1253 0cd1c46a 2019-03-11 stsp if (stat(path, &sb) != 0) {
1254 0cd1c46a 2019-03-11 stsp if (errno != ENOENT) {
1255 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path);
1256 0cd1c46a 2019-03-11 stsp goto done;
1257 0cd1c46a 2019-03-11 stsp }
1258 0cd1c46a 2019-03-11 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1259 3818e3c4 2020-11-01 naddy }
1260 3818e3c4 2020-11-01 naddy
1261 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), sb.st_mode) != 0) {
1262 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1263 3818e3c4 2020-11-01 naddy goto done;
1264 9e672c74 2019-03-11 stsp }
1265 9e672c74 2019-03-11 stsp
1266 9e672c74 2019-03-11 stsp if (rename(tmppath, path) != 0) {
1267 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
1268 9e672c74 2019-03-11 stsp goto done;
1269 9e672c74 2019-03-11 stsp }
1270 9e672c74 2019-03-11 stsp free(tmppath);
1271 9e672c74 2019-03-11 stsp tmppath = NULL;
1272 3f338f0a 2021-07-27 stsp
1273 3f338f0a 2021-07-27 stsp if (stat(path, &sb) == -1) {
1274 3f338f0a 2021-07-27 stsp err = got_error_from_errno2("stat", path);
1275 3f338f0a 2021-07-27 stsp goto done;
1276 3f338f0a 2021-07-27 stsp }
1277 3f338f0a 2021-07-27 stsp ref->mtime = sb.st_mtime;
1278 9e672c74 2019-03-11 stsp done:
1279 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1280 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1281 9e672c74 2019-03-11 stsp if (f) {
1282 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1283 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1284 9e672c74 2019-03-11 stsp }
1285 9e672c74 2019-03-11 stsp free(path_refs);
1286 9e672c74 2019-03-11 stsp free(path);
1287 9e672c74 2019-03-11 stsp if (tmppath) {
1288 a06ca3f7 2022-10-15 stsp if (unlink(tmppath) == -1 && err == NULL)
1289 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
1290 9e672c74 2019-03-11 stsp free(tmppath);
1291 2d2e1378 2019-03-11 stsp }
1292 2d2e1378 2019-03-11 stsp return err ? err : unlock_err;
1293 2d2e1378 2019-03-11 stsp }
1294 598a8b91 2019-03-15 stsp
1295 598a8b91 2019-03-15 stsp static const struct got_error *
1296 598a8b91 2019-03-15 stsp delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1297 598a8b91 2019-03-15 stsp {
1298 598a8b91 2019-03-15 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1299 598a8b91 2019-03-15 stsp struct got_lockfile *lf = NULL;
1300 598a8b91 2019-03-15 stsp FILE *f = NULL, *tmpf = NULL;
1301 9bdd68dd 2020-01-02 naddy char *line = NULL, *packed_refs_path, *tmppath = NULL;
1302 7a90b680 2020-01-02 naddy size_t linesize = 0;
1303 598a8b91 2019-03-15 stsp struct got_reflist_head refs;
1304 598a8b91 2019-03-15 stsp int found_delref = 0;
1305 2d2e1378 2019-03-11 stsp
1306 598a8b91 2019-03-15 stsp /* The packed-refs file does not cotain symbolic references. */
1307 598a8b91 2019-03-15 stsp if (delref->flags & GOT_REF_IS_SYMBOLIC)
1308 598a8b91 2019-03-15 stsp return got_error(GOT_ERR_BAD_REF_DATA);
1309 598a8b91 2019-03-15 stsp
1310 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
1311 598a8b91 2019-03-15 stsp
1312 598a8b91 2019-03-15 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
1313 598a8b91 2019-03-15 stsp if (packed_refs_path == NULL)
1314 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_packed_refs");
1315 598a8b91 2019-03-15 stsp
1316 b90054ed 2022-11-01 stsp err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1317 598a8b91 2019-03-15 stsp if (err)
1318 598a8b91 2019-03-15 stsp goto done;
1319 598a8b91 2019-03-15 stsp
1320 2f17228e 2019-05-12 stsp if (delref->lf == NULL) {
1321 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, packed_refs_path, -1);
1322 2f17228e 2019-05-12 stsp if (err)
1323 2f17228e 2019-05-12 stsp goto done;
1324 2f17228e 2019-05-12 stsp }
1325 598a8b91 2019-03-15 stsp
1326 00fe21f2 2021-12-31 stsp f = fopen(packed_refs_path, "re");
1327 598a8b91 2019-03-15 stsp if (f == NULL) {
1328 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", packed_refs_path);
1329 598a8b91 2019-03-15 stsp goto done;
1330 598a8b91 2019-03-15 stsp }
1331 656b1f76 2019-05-11 jcs for (;;) {
1332 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1333 598a8b91 2019-03-15 stsp struct got_reference *ref;
1334 598a8b91 2019-03-15 stsp struct got_reflist_entry *new;
1335 598a8b91 2019-03-15 stsp
1336 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1337 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1338 598a8b91 2019-03-15 stsp if (feof(f))
1339 598a8b91 2019-03-15 stsp break;
1340 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1341 598a8b91 2019-03-15 stsp goto done;
1342 598a8b91 2019-03-15 stsp }
1343 9bdd68dd 2020-01-02 naddy if (linelen > 0 && line[linelen - 1] == '\n')
1344 9bdd68dd 2020-01-02 naddy line[linelen - 1] = '\0';
1345 b54930d5 2023-02-28 op err = parse_packed_ref_line(&ref, NULL, line, 0,
1346 b54930d5 2023-02-28 op got_repo_get_object_format(repo));
1347 598a8b91 2019-03-15 stsp if (err)
1348 598a8b91 2019-03-15 stsp goto done;
1349 598a8b91 2019-03-15 stsp if (ref == NULL)
1350 598a8b91 2019-03-15 stsp continue;
1351 598a8b91 2019-03-15 stsp
1352 598a8b91 2019-03-15 stsp if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1353 faf5b56f 2023-01-31 op got_object_id_cmp(&ref->ref.ref.id,
1354 faf5b56f 2023-01-31 op &delref->ref.ref.id) == 0) {
1355 598a8b91 2019-03-15 stsp found_delref = 1;
1356 598a8b91 2019-03-15 stsp got_ref_close(ref);
1357 598a8b91 2019-03-15 stsp continue;
1358 598a8b91 2019-03-15 stsp }
1359 598a8b91 2019-03-15 stsp
1360 72acb3d8 2021-08-06 stsp err = got_reflist_insert(&new, &refs, ref,
1361 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
1362 598a8b91 2019-03-15 stsp if (err || new == NULL /* duplicate */)
1363 598a8b91 2019-03-15 stsp got_ref_close(ref);
1364 598a8b91 2019-03-15 stsp if (err)
1365 598a8b91 2019-03-15 stsp goto done;
1366 598a8b91 2019-03-15 stsp }
1367 598a8b91 2019-03-15 stsp
1368 598a8b91 2019-03-15 stsp if (found_delref) {
1369 598a8b91 2019-03-15 stsp struct got_reflist_entry *re;
1370 598a8b91 2019-03-15 stsp size_t n;
1371 598a8b91 2019-03-15 stsp struct stat sb;
1372 598a8b91 2019-03-15 stsp
1373 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1374 598a8b91 2019-03-15 stsp if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1375 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1376 598a8b91 2019-03-15 stsp goto done;
1377 598a8b91 2019-03-15 stsp }
1378 598a8b91 2019-03-15 stsp
1379 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
1380 faf5b56f 2023-01-31 op char *hex;
1381 faf5b56f 2023-01-31 op size_t len;
1382 598a8b91 2019-03-15 stsp
1383 faf5b56f 2023-01-31 op err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1384 faf5b56f 2023-01-31 op if (err)
1385 598a8b91 2019-03-15 stsp goto done;
1386 faf5b56f 2023-01-31 op len = strlen(hex);
1387 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s ", hex);
1388 faf5b56f 2023-01-31 op free(hex);
1389 faf5b56f 2023-01-31 op if (n != len + 1) {
1390 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1391 598a8b91 2019-03-15 stsp goto done;
1392 598a8b91 2019-03-15 stsp }
1393 faf5b56f 2023-01-31 op
1394 598a8b91 2019-03-15 stsp n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1395 598a8b91 2019-03-15 stsp if (n != strlen(re->ref->ref.ref.name) + 1) {
1396 598a8b91 2019-03-15 stsp err = got_ferror(f, GOT_ERR_IO);
1397 598a8b91 2019-03-15 stsp goto done;
1398 598a8b91 2019-03-15 stsp }
1399 598a8b91 2019-03-15 stsp }
1400 598a8b91 2019-03-15 stsp
1401 598a8b91 2019-03-15 stsp if (fflush(tmpf) != 0) {
1402 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1403 598a8b91 2019-03-15 stsp goto done;
1404 598a8b91 2019-03-15 stsp }
1405 598a8b91 2019-03-15 stsp
1406 3818e3c4 2020-11-01 naddy if (fstat(fileno(f), &sb) != 0) {
1407 598a8b91 2019-03-15 stsp if (errno != ENOENT) {
1408 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fstat",
1409 230a42bd 2019-05-11 jcs packed_refs_path);
1410 598a8b91 2019-03-15 stsp goto done;
1411 598a8b91 2019-03-15 stsp }
1412 598a8b91 2019-03-15 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1413 598a8b91 2019-03-15 stsp }
1414 598a8b91 2019-03-15 stsp
1415 3818e3c4 2020-11-01 naddy if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1416 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", tmppath);
1417 598a8b91 2019-03-15 stsp goto done;
1418 598a8b91 2019-03-15 stsp }
1419 598a8b91 2019-03-15 stsp
1420 3818e3c4 2020-11-01 naddy if (rename(tmppath, packed_refs_path) != 0) {
1421 3818e3c4 2020-11-01 naddy err = got_error_from_errno3("rename", tmppath,
1422 230a42bd 2019-05-11 jcs packed_refs_path);
1423 598a8b91 2019-03-15 stsp goto done;
1424 598a8b91 2019-03-15 stsp }
1425 15c388a9 2022-10-15 stsp free(tmppath);
1426 15c388a9 2022-10-15 stsp tmppath = NULL;
1427 598a8b91 2019-03-15 stsp }
1428 598a8b91 2019-03-15 stsp done:
1429 2f17228e 2019-05-12 stsp if (delref->lf == NULL && lf)
1430 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1431 598a8b91 2019-03-15 stsp if (f) {
1432 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF && err == NULL)
1433 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1434 598a8b91 2019-03-15 stsp }
1435 15c388a9 2022-10-15 stsp if (tmppath && unlink(tmppath) == -1 && err == NULL)
1436 15c388a9 2022-10-15 stsp err = got_error_from_errno2("unlink", tmppath);
1437 15c388a9 2022-10-15 stsp if (tmpf && fclose(tmpf) == EOF && err == NULL)
1438 15c388a9 2022-10-15 stsp err = got_error_from_errno("fclose");
1439 598a8b91 2019-03-15 stsp free(tmppath);
1440 598a8b91 2019-03-15 stsp free(packed_refs_path);
1441 9bdd68dd 2020-01-02 naddy free(line);
1442 598a8b91 2019-03-15 stsp got_ref_list_free(&refs);
1443 598a8b91 2019-03-15 stsp return err ? err : unlock_err;
1444 598a8b91 2019-03-15 stsp }
1445 598a8b91 2019-03-15 stsp
1446 2a104ff6 2020-09-21 stsp static const struct got_error *
1447 2a104ff6 2020-09-21 stsp delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1448 2d2e1378 2019-03-11 stsp {
1449 2d2e1378 2019-03-11 stsp const struct got_error *err = NULL, *unlock_err = NULL;
1450 2d2e1378 2019-03-11 stsp const char *name = got_ref_get_name(ref);
1451 2d2e1378 2019-03-11 stsp char *path_refs = NULL, *path = NULL;
1452 2d2e1378 2019-03-11 stsp struct got_lockfile *lf = NULL;
1453 2d2e1378 2019-03-11 stsp
1454 2d2e1378 2019-03-11 stsp path_refs = get_refs_dir_path(repo, name);
1455 2d2e1378 2019-03-11 stsp if (path_refs == NULL) {
1456 638f9024 2019-05-13 stsp err = got_error_from_errno2("get_refs_dir_path", name);
1457 2d2e1378 2019-03-11 stsp goto done;
1458 2d2e1378 2019-03-11 stsp }
1459 2d2e1378 2019-03-11 stsp
1460 2d2e1378 2019-03-11 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1461 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1462 2d2e1378 2019-03-11 stsp goto done;
1463 9e672c74 2019-03-11 stsp }
1464 2d2e1378 2019-03-11 stsp
1465 2f17228e 2019-05-12 stsp if (ref->lf == NULL) {
1466 5345b4c7 2021-07-06 stsp err = got_lockfile_lock(&lf, path, -1);
1467 2f17228e 2019-05-12 stsp if (err)
1468 2f17228e 2019-05-12 stsp goto done;
1469 2f17228e 2019-05-12 stsp }
1470 2d2e1378 2019-03-11 stsp
1471 2d2e1378 2019-03-11 stsp /* XXX: check if old content matches our expectations? */
1472 2d2e1378 2019-03-11 stsp
1473 a06ca3f7 2022-10-15 stsp if (unlink(path) == -1)
1474 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", path);
1475 2d2e1378 2019-03-11 stsp done:
1476 2f17228e 2019-05-12 stsp if (ref->lf == NULL && lf)
1477 5345b4c7 2021-07-06 stsp unlock_err = got_lockfile_unlock(lf, -1);
1478 2d2e1378 2019-03-11 stsp
1479 2d2e1378 2019-03-11 stsp free(path_refs);
1480 2d2e1378 2019-03-11 stsp free(path);
1481 9e672c74 2019-03-11 stsp return err ? err : unlock_err;
1482 2a104ff6 2020-09-21 stsp }
1483 2a104ff6 2020-09-21 stsp
1484 2a104ff6 2020-09-21 stsp const struct got_error *
1485 2a104ff6 2020-09-21 stsp got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1486 2a104ff6 2020-09-21 stsp {
1487 2a104ff6 2020-09-21 stsp const struct got_error *err = NULL;
1488 2a104ff6 2020-09-21 stsp struct got_reference *ref2;
1489 2a104ff6 2020-09-21 stsp
1490 2a104ff6 2020-09-21 stsp if (ref->flags & GOT_REF_IS_PACKED) {
1491 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref, repo);
1492 2a104ff6 2020-09-21 stsp if (err)
1493 2a104ff6 2020-09-21 stsp return err;
1494 2a104ff6 2020-09-21 stsp
1495 a1c4175c 2020-09-22 stsp err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1496 2a104ff6 2020-09-21 stsp if (err) {
1497 2a104ff6 2020-09-21 stsp if (err->code == GOT_ERR_NOT_REF)
1498 2a104ff6 2020-09-21 stsp return NULL;
1499 2a104ff6 2020-09-21 stsp return err;
1500 2a104ff6 2020-09-21 stsp }
1501 2a104ff6 2020-09-21 stsp
1502 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref2, repo);
1503 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1504 2a104ff6 2020-09-21 stsp return err;
1505 2a104ff6 2020-09-21 stsp } else {
1506 2a104ff6 2020-09-21 stsp err = delete_loose_ref(ref, repo);
1507 2a104ff6 2020-09-21 stsp if (err)
1508 2a104ff6 2020-09-21 stsp return err;
1509 2a104ff6 2020-09-21 stsp
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;
1515 2a104ff6 2020-09-21 stsp }
1516 2a104ff6 2020-09-21 stsp
1517 2a104ff6 2020-09-21 stsp err = delete_packed_ref(ref2, repo);
1518 2a104ff6 2020-09-21 stsp got_ref_close(ref2);
1519 2a104ff6 2020-09-21 stsp return err;
1520 2a104ff6 2020-09-21 stsp }
1521 9e672c74 2019-03-11 stsp }
1522 2f17228e 2019-05-12 stsp
1523 2f17228e 2019-05-12 stsp const struct got_error *
1524 2f17228e 2019-05-12 stsp got_ref_unlock(struct got_reference *ref)
1525 2f17228e 2019-05-12 stsp {
1526 2f17228e 2019-05-12 stsp const struct got_error *err;
1527 5345b4c7 2021-07-06 stsp err = got_lockfile_unlock(ref->lf, -1);
1528 2f17228e 2019-05-12 stsp ref->lf = NULL;
1529 2f17228e 2019-05-12 stsp return err;
1530 2f17228e 2019-05-12 stsp }
1531 7b5b670e 2020-12-25 stsp
1532 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map {
1533 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1534 7b5b670e 2020-12-25 stsp };
1535 7b5b670e 2020-12-25 stsp
1536 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry {
1537 7b5b670e 2020-12-25 stsp struct got_reflist_head refs;
1538 7b5b670e 2020-12-25 stsp };
1539 24202e46 2020-12-26 stsp
1540 24202e46 2020-12-26 stsp static const struct got_error *
1541 24202e46 2020-12-26 stsp add_object_id_map_entry(struct got_object_idset *idset,
1542 24202e46 2020-12-26 stsp struct got_object_id *id, struct got_reflist_entry *re)
1543 24202e46 2020-12-26 stsp {
1544 24202e46 2020-12-26 stsp const struct got_error *err = NULL;
1545 24202e46 2020-12-26 stsp struct got_reflist_object_id_map_entry *ent;
1546 24202e46 2020-12-26 stsp struct got_reflist_entry *new;
1547 24202e46 2020-12-26 stsp
1548 24202e46 2020-12-26 stsp ent = got_object_idset_get(idset, id);
1549 24202e46 2020-12-26 stsp if (ent == NULL) {
1550 24202e46 2020-12-26 stsp ent = malloc(sizeof(*ent));
1551 24202e46 2020-12-26 stsp if (ent == NULL)
1552 24202e46 2020-12-26 stsp return got_error_from_errno("malloc");
1553 7b5b670e 2020-12-25 stsp
1554 24202e46 2020-12-26 stsp TAILQ_INIT(&ent->refs);
1555 24202e46 2020-12-26 stsp err = got_object_idset_add(idset, id, ent);
1556 bb6672b6 2022-04-14 tb if (err) {
1557 bb6672b6 2022-04-14 tb free(ent);
1558 24202e46 2020-12-26 stsp return err;
1559 bb6672b6 2022-04-14 tb }
1560 24202e46 2020-12-26 stsp }
1561 24202e46 2020-12-26 stsp
1562 24202e46 2020-12-26 stsp err = got_reflist_entry_dup(&new, re);
1563 24202e46 2020-12-26 stsp if (err)
1564 24202e46 2020-12-26 stsp return err;
1565 24202e46 2020-12-26 stsp
1566 24202e46 2020-12-26 stsp TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1567 24202e46 2020-12-26 stsp return NULL;
1568 24202e46 2020-12-26 stsp }
1569 24202e46 2020-12-26 stsp
1570 7b5b670e 2020-12-25 stsp const struct got_error *
1571 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1572 7b5b670e 2020-12-25 stsp struct got_reflist_head *refs, struct got_repository *repo)
1573 7b5b670e 2020-12-25 stsp {
1574 7b5b670e 2020-12-25 stsp const struct got_error *err = NULL;
1575 7b5b670e 2020-12-25 stsp struct got_object_idset *idset;
1576 7b5b670e 2020-12-25 stsp struct got_object_id *id = NULL;
1577 7b5b670e 2020-12-25 stsp struct got_reflist_entry *re;
1578 7b5b670e 2020-12-25 stsp
1579 7b5b670e 2020-12-25 stsp idset = got_object_idset_alloc();
1580 7b5b670e 2020-12-25 stsp if (idset == NULL)
1581 7b5b670e 2020-12-25 stsp return got_error_from_errno("got_object_idset_alloc");
1582 7b5b670e 2020-12-25 stsp
1583 7b5b670e 2020-12-25 stsp *map = malloc(sizeof(**map));
1584 7b5b670e 2020-12-25 stsp if (*map == NULL) {
1585 7b5b670e 2020-12-25 stsp got_object_idset_free(idset);
1586 7b5b670e 2020-12-25 stsp return got_error_from_errno("malloc");
1587 7b5b670e 2020-12-25 stsp }
1588 7b5b670e 2020-12-25 stsp (*map)->idset = idset;
1589 7b5b670e 2020-12-25 stsp
1590 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1591 24202e46 2020-12-26 stsp struct got_tag_object *tag = NULL;
1592 7b5b670e 2020-12-25 stsp
1593 7b5b670e 2020-12-25 stsp err = got_ref_resolve(&id, repo, re->ref);
1594 7b5b670e 2020-12-25 stsp if (err)
1595 7b5b670e 2020-12-25 stsp goto done;
1596 7b5b670e 2020-12-25 stsp
1597 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset, id, re);
1598 24202e46 2020-12-26 stsp if (err)
1599 24202e46 2020-12-26 stsp goto done;
1600 24202e46 2020-12-26 stsp
1601 24202e46 2020-12-26 stsp if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1602 24202e46 2020-12-26 stsp free(id);
1603 24202e46 2020-12-26 stsp id = NULL;
1604 24202e46 2020-12-26 stsp continue;
1605 24202e46 2020-12-26 stsp }
1606 24202e46 2020-12-26 stsp
1607 24202e46 2020-12-26 stsp err = got_object_open_as_tag(&tag, repo, id);
1608 24202e46 2020-12-26 stsp if (err) {
1609 24202e46 2020-12-26 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1610 7b5b670e 2020-12-25 stsp goto done;
1611 24202e46 2020-12-26 stsp /* Ref points at something other than a tag. */
1612 24202e46 2020-12-26 stsp err = NULL;
1613 24202e46 2020-12-26 stsp tag = NULL;
1614 24202e46 2020-12-26 stsp free(id);
1615 24202e46 2020-12-26 stsp id = NULL;
1616 24202e46 2020-12-26 stsp continue;
1617 7b5b670e 2020-12-25 stsp }
1618 7b5b670e 2020-12-25 stsp
1619 24202e46 2020-12-26 stsp err = add_object_id_map_entry(idset,
1620 24202e46 2020-12-26 stsp got_object_tag_get_object_id(tag), re);
1621 f0ff8d4c 2020-12-26 stsp got_object_tag_close(tag);
1622 7b5b670e 2020-12-25 stsp if (err)
1623 7b5b670e 2020-12-25 stsp goto done;
1624 24202e46 2020-12-26 stsp
1625 7b5b670e 2020-12-25 stsp free(id);
1626 7b5b670e 2020-12-25 stsp id = NULL;
1627 7b5b670e 2020-12-25 stsp }
1628 7b5b670e 2020-12-25 stsp done:
1629 7b5b670e 2020-12-25 stsp free(id);
1630 7b5b670e 2020-12-25 stsp if (err) {
1631 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(*map);
1632 7b5b670e 2020-12-25 stsp *map = NULL;
1633 7b5b670e 2020-12-25 stsp }
1634 a53af95f 2020-12-26 stsp return err;
1635 7b5b670e 2020-12-25 stsp }
1636 7b5b670e 2020-12-25 stsp
1637 7b5b670e 2020-12-25 stsp struct got_reflist_head *
1638 7b5b670e 2020-12-25 stsp got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1639 7b5b670e 2020-12-25 stsp struct got_object_id *id)
1640 7b5b670e 2020-12-25 stsp {
1641 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent;
1642 7b5b670e 2020-12-25 stsp ent = got_object_idset_get(map->idset, id);
1643 7b5b670e 2020-12-25 stsp if (ent)
1644 7b5b670e 2020-12-25 stsp return &ent->refs;
1645 7b5b670e 2020-12-25 stsp return NULL;
1646 7b5b670e 2020-12-25 stsp }
1647 7b5b670e 2020-12-25 stsp
1648 7b5b670e 2020-12-25 stsp static const struct got_error *
1649 7b5b670e 2020-12-25 stsp free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1650 7b5b670e 2020-12-25 stsp {
1651 7b5b670e 2020-12-25 stsp struct got_reflist_object_id_map_entry *ent = data;
1652 7b5b670e 2020-12-25 stsp
1653 7b5b670e 2020-12-25 stsp got_ref_list_free(&ent->refs);
1654 7b5b670e 2020-12-25 stsp free(ent);
1655 7b5b670e 2020-12-25 stsp return NULL;
1656 7b5b670e 2020-12-25 stsp }
1657 7b5b670e 2020-12-25 stsp
1658 7b5b670e 2020-12-25 stsp void
1659 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1660 7b5b670e 2020-12-25 stsp {
1661 7b5b670e 2020-12-25 stsp got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1662 7b5b670e 2020-12-25 stsp got_object_idset_free(map->idset);
1663 7b5b670e 2020-12-25 stsp free(map);
1664 7b5b670e 2020-12-25 stsp }