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