Blame


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