Blame


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