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