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