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