Blame


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