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