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