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