Blame


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