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