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 5261c201 2018-04-01 stsp
20 f77a24b0 2019-03-11 stsp #include <ctype.h>
21 a04f49d2 2019-02-04 stsp #include <dirent.h>
22 5261c201 2018-04-01 stsp #include <sha1.h>
23 5261c201 2018-04-01 stsp #include <stdio.h>
24 5261c201 2018-04-01 stsp #include <stdlib.h>
25 5261c201 2018-04-01 stsp #include <string.h>
26 5261c201 2018-04-01 stsp #include <util.h>
27 5261c201 2018-04-01 stsp #include <zlib.h>
28 788c352e 2018-06-16 stsp #include <time.h>
29 5261c201 2018-04-01 stsp
30 5261c201 2018-04-01 stsp #include "got_error.h"
31 5261c201 2018-04-01 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_repository.h"
33 5261c201 2018-04-01 stsp #include "got_reference.h"
34 5261c201 2018-04-01 stsp
35 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
36 5261c201 2018-04-01 stsp #include "got_lib_path.h"
37 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
38 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
39 5261c201 2018-04-01 stsp #include "got_lib_object.h"
40 f77a24b0 2019-03-11 stsp #include "got_lib_lockfile.h"
41 5261c201 2018-04-01 stsp
42 fb79db15 2019-02-01 stsp #ifndef nitems
43 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 fb79db15 2019-02-01 stsp #endif
45 fb79db15 2019-02-01 stsp
46 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
47 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
48 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
49 d1f2edc9 2018-06-13 stsp
50 5261c201 2018-04-01 stsp /* A symbolic reference. */
51 5261c201 2018-04-01 stsp struct got_symref {
52 5261c201 2018-04-01 stsp char *name;
53 5261c201 2018-04-01 stsp char *ref;
54 5261c201 2018-04-01 stsp };
55 5261c201 2018-04-01 stsp
56 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
57 5261c201 2018-04-01 stsp struct got_ref {
58 5261c201 2018-04-01 stsp char *name;
59 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
60 5261c201 2018-04-01 stsp };
61 5261c201 2018-04-01 stsp
62 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
63 5261c201 2018-04-01 stsp struct got_reference {
64 5261c201 2018-04-01 stsp unsigned int flags;
65 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
66 5261c201 2018-04-01 stsp
67 5261c201 2018-04-01 stsp union {
68 5261c201 2018-04-01 stsp struct got_ref ref;
69 5261c201 2018-04-01 stsp struct got_symref symref;
70 5261c201 2018-04-01 stsp } ref;
71 5261c201 2018-04-01 stsp };
72 5261c201 2018-04-01 stsp
73 5261c201 2018-04-01 stsp static const struct got_error *
74 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
75 5261c201 2018-04-01 stsp {
76 5261c201 2018-04-01 stsp struct got_symref *symref;
77 5261c201 2018-04-01 stsp char *symref_name;
78 5261c201 2018-04-01 stsp char *symref_ref;
79 5261c201 2018-04-01 stsp
80 5261c201 2018-04-01 stsp if (line[0] == '\0')
81 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
82 5261c201 2018-04-01 stsp
83 5261c201 2018-04-01 stsp symref_name = strdup(name);
84 5261c201 2018-04-01 stsp if (symref_name == NULL)
85 5261c201 2018-04-01 stsp return got_error_from_errno();
86 5261c201 2018-04-01 stsp symref_ref = strdup(line);
87 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
88 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
89 5261c201 2018-04-01 stsp free(symref_name);
90 5261c201 2018-04-01 stsp return err;
91 5261c201 2018-04-01 stsp }
92 5261c201 2018-04-01 stsp
93 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
94 5261c201 2018-04-01 stsp if (*ref == NULL)
95 5261c201 2018-04-01 stsp return got_error_from_errno();
96 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
97 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
98 5261c201 2018-04-01 stsp symref->name = symref_name;
99 5261c201 2018-04-01 stsp symref->ref = symref_ref;
100 5261c201 2018-04-01 stsp return NULL;
101 5261c201 2018-04-01 stsp }
102 5261c201 2018-04-01 stsp
103 5261c201 2018-04-01 stsp static const struct got_error *
104 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
105 5261c201 2018-04-01 stsp {
106 5892cdd6 2019-03-10 stsp struct got_object_id id;
107 5261c201 2018-04-01 stsp
108 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
109 5261c201 2018-04-01 stsp line += 5;
110 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
111 5261c201 2018-04-01 stsp }
112 5261c201 2018-04-01 stsp
113 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
114 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
115 5261c201 2018-04-01 stsp
116 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
117 5261c201 2018-04-01 stsp }
118 5261c201 2018-04-01 stsp
119 5261c201 2018-04-01 stsp static const struct got_error *
120 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
121 5261c201 2018-04-01 stsp const char *abspath)
122 5261c201 2018-04-01 stsp {
123 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
124 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
125 5261c201 2018-04-01 stsp char *line;
126 5261c201 2018-04-01 stsp size_t len;
127 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
128 5261c201 2018-04-01 stsp
129 5261c201 2018-04-01 stsp if (f == NULL)
130 1e37702e 2019-02-04 stsp return NULL;
131 5261c201 2018-04-01 stsp
132 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
133 5261c201 2018-04-01 stsp if (line == NULL) {
134 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
135 5261c201 2018-04-01 stsp goto done;
136 5261c201 2018-04-01 stsp }
137 5261c201 2018-04-01 stsp
138 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
139 5261c201 2018-04-01 stsp done:
140 5261c201 2018-04-01 stsp free(line);
141 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
142 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
143 5261c201 2018-04-01 stsp return err;
144 5261c201 2018-04-01 stsp }
145 5261c201 2018-04-01 stsp
146 c5f754cc 2019-02-01 stsp static int
147 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
148 5261c201 2018-04-01 stsp {
149 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
150 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
151 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
152 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
153 c5f754cc 2019-02-01 stsp }
154 c5f754cc 2019-02-01 stsp
155 c5f754cc 2019-02-01 stsp static char *
156 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
157 c5f754cc 2019-02-01 stsp {
158 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
159 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
160 5261c201 2018-04-01 stsp
161 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
162 f77a24b0 2019-03-11 stsp }
163 f77a24b0 2019-03-11 stsp
164 f77a24b0 2019-03-11 stsp static int
165 f77a24b0 2019-03-11 stsp is_valid_ref_name(const char *name)
166 f77a24b0 2019-03-11 stsp {
167 f77a24b0 2019-03-11 stsp const char *s, *slash, *seg;
168 f77a24b0 2019-03-11 stsp const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
169 f77a24b0 2019-03-11 stsp const char *forbidden_seq[] = { "//", "..", "@{" };
170 f77a24b0 2019-03-11 stsp const char *lfs = GOT_LOCKFILE_SUFFIX;
171 f77a24b0 2019-03-11 stsp const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
172 f77a24b0 2019-03-11 stsp int i;
173 f77a24b0 2019-03-11 stsp
174 f77a24b0 2019-03-11 stsp if (name[0] == '@' && name[1] == '\0')
175 f77a24b0 2019-03-11 stsp return 0;
176 f77a24b0 2019-03-11 stsp
177 f77a24b0 2019-03-11 stsp slash = strchr(name, '/');
178 f77a24b0 2019-03-11 stsp if (slash == NULL)
179 f77a24b0 2019-03-11 stsp return 0;
180 f77a24b0 2019-03-11 stsp
181 f77a24b0 2019-03-11 stsp s = name;
182 f77a24b0 2019-03-11 stsp seg = s;
183 f77a24b0 2019-03-11 stsp if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
184 f77a24b0 2019-03-11 stsp return 0;
185 f77a24b0 2019-03-11 stsp while (*s) {
186 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden); i++) {
187 f77a24b0 2019-03-11 stsp if (*s == forbidden[i])
188 f77a24b0 2019-03-11 stsp return 0;
189 f77a24b0 2019-03-11 stsp }
190 f77a24b0 2019-03-11 stsp for (i = 0; i < nitems(forbidden_seq); i++) {
191 f77a24b0 2019-03-11 stsp if (s[0] == forbidden_seq[i][0] &&
192 f77a24b0 2019-03-11 stsp s[1] == forbidden_seq[i][1])
193 f77a24b0 2019-03-11 stsp return 0;
194 f77a24b0 2019-03-11 stsp }
195 f77a24b0 2019-03-11 stsp if (iscntrl((unsigned char)s[0]))
196 f77a24b0 2019-03-11 stsp return 0;
197 f77a24b0 2019-03-11 stsp if (s[0] == '.' && s[1] == '\0')
198 f77a24b0 2019-03-11 stsp return 0;
199 f77a24b0 2019-03-11 stsp if (*s == '/') {
200 f77a24b0 2019-03-11 stsp const char *nextseg = s + 1;
201 f77a24b0 2019-03-11 stsp if (nextseg[0] == '\0' || nextseg[0] == '.' ||
202 f77a24b0 2019-03-11 stsp nextseg[0] == '/')
203 f77a24b0 2019-03-11 stsp return 0;
204 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
205 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
206 f77a24b0 2019-03-11 stsp return 0;
207 f77a24b0 2019-03-11 stsp seg = nextseg;
208 f77a24b0 2019-03-11 stsp }
209 f77a24b0 2019-03-11 stsp s++;
210 f77a24b0 2019-03-11 stsp }
211 f77a24b0 2019-03-11 stsp
212 f77a24b0 2019-03-11 stsp if (seg <= s - lfs_len &&
213 f77a24b0 2019-03-11 stsp strncmp(s - lfs_len, lfs, lfs_len) == 0)
214 f77a24b0 2019-03-11 stsp return 0;
215 f77a24b0 2019-03-11 stsp
216 f77a24b0 2019-03-11 stsp return 1;
217 5892cdd6 2019-03-10 stsp }
218 5892cdd6 2019-03-10 stsp
219 5892cdd6 2019-03-10 stsp const struct got_error *
220 5892cdd6 2019-03-10 stsp got_ref_alloc(struct got_reference **ref, const char *name,
221 5892cdd6 2019-03-10 stsp struct got_object_id *id)
222 5892cdd6 2019-03-10 stsp {
223 5892cdd6 2019-03-10 stsp const struct got_error *err = NULL;
224 5892cdd6 2019-03-10 stsp
225 f77a24b0 2019-03-11 stsp if (!is_valid_ref_name(name))
226 f77a24b0 2019-03-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
227 f77a24b0 2019-03-11 stsp
228 5892cdd6 2019-03-10 stsp *ref = calloc(1, sizeof(**ref));
229 5892cdd6 2019-03-10 stsp if (*ref == NULL)
230 5892cdd6 2019-03-10 stsp return got_error_from_errno();
231 5892cdd6 2019-03-10 stsp
232 5892cdd6 2019-03-10 stsp memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
233 5892cdd6 2019-03-10 stsp (*ref)->ref.ref.name = strdup(name);
234 5892cdd6 2019-03-10 stsp if ((*ref)->ref.ref.name == NULL) {
235 5892cdd6 2019-03-10 stsp err = got_error_from_errno();
236 5892cdd6 2019-03-10 stsp free(*ref);
237 5892cdd6 2019-03-10 stsp *ref = NULL;
238 5892cdd6 2019-03-10 stsp }
239 5892cdd6 2019-03-10 stsp return err;
240 5261c201 2018-04-01 stsp }
241 5261c201 2018-04-01 stsp
242 d1f2edc9 2018-06-13 stsp static const struct got_error *
243 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
244 fb79db15 2019-02-01 stsp const char *line)
245 fb79db15 2019-02-01 stsp {
246 5892cdd6 2019-03-10 stsp struct got_object_id id;
247 5892cdd6 2019-03-10 stsp const char *name;
248 fb79db15 2019-02-01 stsp
249 fb79db15 2019-02-01 stsp *ref = NULL;
250 fb79db15 2019-02-01 stsp
251 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
252 fb79db15 2019-02-01 stsp return NULL;
253 fb79db15 2019-02-01 stsp
254 5892cdd6 2019-03-10 stsp if (!got_parse_sha1_digest(id.sha1, line))
255 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
256 fb79db15 2019-02-01 stsp
257 199a4027 2019-02-02 stsp if (abs_refname) {
258 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
259 199a4027 2019-02-02 stsp return NULL;
260 5892cdd6 2019-03-10 stsp name = abs_refname;
261 5892cdd6 2019-03-10 stsp } else
262 5892cdd6 2019-03-10 stsp name = line + SHA1_DIGEST_STRING_LENGTH;
263 fb79db15 2019-02-01 stsp
264 5892cdd6 2019-03-10 stsp return got_ref_alloc(ref, name, &id);
265 fb79db15 2019-02-01 stsp }
266 fb79db15 2019-02-01 stsp
267 fb79db15 2019-02-01 stsp static const struct got_error *
268 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
269 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
270 fb79db15 2019-02-01 stsp {
271 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
272 fb79db15 2019-02-01 stsp char *abs_refname;
273 fb79db15 2019-02-01 stsp char *line;
274 fb79db15 2019-02-01 stsp size_t len;
275 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
276 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
277 fb79db15 2019-02-01 stsp
278 1e37702e 2019-02-04 stsp *ref = NULL;
279 1e37702e 2019-02-04 stsp
280 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
281 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
282 fb79db15 2019-02-01 stsp do {
283 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
284 1e37702e 2019-02-04 stsp if (line == NULL)
285 fb79db15 2019-02-01 stsp break;
286 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
287 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
288 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
289 0dec1cc0 2019-02-01 stsp refname) == -1)
290 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
291 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
292 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
293 0dec1cc0 2019-02-01 stsp free(abs_refname);
294 532920c8 2019-02-01 stsp if (err || *ref != NULL)
295 0dec1cc0 2019-02-01 stsp break;
296 0dec1cc0 2019-02-01 stsp }
297 fb79db15 2019-02-01 stsp free(line);
298 fb79db15 2019-02-01 stsp if (err)
299 fb79db15 2019-02-01 stsp break;
300 fb79db15 2019-02-01 stsp } while (*ref == NULL);
301 fb79db15 2019-02-01 stsp
302 fb79db15 2019-02-01 stsp return err;
303 fb79db15 2019-02-01 stsp }
304 fb79db15 2019-02-01 stsp
305 fb79db15 2019-02-01 stsp static const struct got_error *
306 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
307 a04f49d2 2019-02-04 stsp const char *name)
308 d1f2edc9 2018-06-13 stsp {
309 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
310 a04f49d2 2019-02-04 stsp char *path = NULL;
311 a04f49d2 2019-02-04 stsp char *normpath = NULL;
312 a04f49d2 2019-02-04 stsp char *absname = NULL;
313 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
314 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
315 d1f2edc9 2018-06-13 stsp
316 1e37702e 2019-02-04 stsp *ref = NULL;
317 1e37702e 2019-02-04 stsp
318 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
319 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
320 a04f49d2 2019-02-04 stsp return got_error_from_errno();
321 a04f49d2 2019-02-04 stsp absname = (char *)name;
322 a04f49d2 2019-02-04 stsp } else {
323 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
324 a04f49d2 2019-02-04 stsp return got_error_from_errno();
325 d1f2edc9 2018-06-13 stsp
326 a04f49d2 2019-02-04 stsp if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
327 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
328 a04f49d2 2019-02-04 stsp goto done;
329 a04f49d2 2019-02-04 stsp }
330 a04f49d2 2019-02-04 stsp }
331 a04f49d2 2019-02-04 stsp
332 a04f49d2 2019-02-04 stsp normpath = got_path_normalize(path);
333 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
334 30c0868d 2019-02-03 stsp err = got_error_from_errno();
335 d1f2edc9 2018-06-13 stsp goto done;
336 d1f2edc9 2018-06-13 stsp }
337 d1f2edc9 2018-06-13 stsp
338 a04f49d2 2019-02-04 stsp err = parse_ref_file(ref, absname, normpath);
339 d1f2edc9 2018-06-13 stsp done:
340 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
341 a04f49d2 2019-02-04 stsp free(absname);
342 a04f49d2 2019-02-04 stsp free(path);
343 d1f2edc9 2018-06-13 stsp free(normpath);
344 d1f2edc9 2018-06-13 stsp return err;
345 d1f2edc9 2018-06-13 stsp }
346 d1f2edc9 2018-06-13 stsp
347 5261c201 2018-04-01 stsp const struct got_error *
348 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
349 5261c201 2018-04-01 stsp const char *refname)
350 5261c201 2018-04-01 stsp {
351 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
352 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
353 fb79db15 2019-02-01 stsp const char *subdirs[] = {
354 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
355 fb79db15 2019-02-01 stsp };
356 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
357 1e37702e 2019-02-04 stsp
358 1e37702e 2019-02-04 stsp *ref = NULL;
359 e135804e 2019-02-10 stsp
360 e135804e 2019-02-10 stsp path_refs = get_refs_dir_path(repo, refname);
361 e135804e 2019-02-10 stsp if (path_refs == NULL) {
362 e135804e 2019-02-10 stsp err = got_error_from_errno();
363 e135804e 2019-02-10 stsp goto done;
364 e135804e 2019-02-10 stsp }
365 5261c201 2018-04-01 stsp
366 c5f754cc 2019-02-01 stsp if (!well_known) {
367 c5f754cc 2019-02-01 stsp char *packed_refs_path;
368 c5f754cc 2019-02-01 stsp FILE *f;
369 c5f754cc 2019-02-01 stsp
370 e135804e 2019-02-10 stsp /* Search on-disk refs before packed refs! */
371 e135804e 2019-02-10 stsp for (i = 0; i < nitems(subdirs); i++) {
372 e135804e 2019-02-10 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
373 e135804e 2019-02-10 stsp if (err || *ref)
374 e135804e 2019-02-10 stsp goto done;
375 e135804e 2019-02-10 stsp }
376 e135804e 2019-02-10 stsp
377 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
378 e135804e 2019-02-10 stsp if (packed_refs_path == NULL) {
379 e135804e 2019-02-10 stsp err = got_error_from_errno();
380 e135804e 2019-02-10 stsp goto done;
381 e135804e 2019-02-10 stsp }
382 c5f754cc 2019-02-01 stsp
383 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
384 c5f754cc 2019-02-01 stsp free(packed_refs_path);
385 c5f754cc 2019-02-01 stsp if (f != NULL) {
386 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
387 0dec1cc0 2019-02-01 stsp refname);
388 fb43ecf1 2019-02-11 stsp if (fclose(f) != 0 && err == NULL)
389 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
390 1e37702e 2019-02-04 stsp if (err || *ref)
391 0dec1cc0 2019-02-01 stsp goto done;
392 fb79db15 2019-02-01 stsp }
393 fb79db15 2019-02-01 stsp }
394 fb79db15 2019-02-01 stsp
395 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
396 1e37702e 2019-02-04 stsp if (err)
397 1e37702e 2019-02-04 stsp goto done;
398 e135804e 2019-02-10 stsp done:
399 1e37702e 2019-02-04 stsp if (*ref == NULL)
400 1e37702e 2019-02-04 stsp err = got_error_not_ref(refname);
401 5261c201 2018-04-01 stsp free(path_refs);
402 5261c201 2018-04-01 stsp return err;
403 5261c201 2018-04-01 stsp }
404 5261c201 2018-04-01 stsp
405 5261c201 2018-04-01 stsp void
406 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
407 5261c201 2018-04-01 stsp {
408 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
409 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
410 5261c201 2018-04-01 stsp else
411 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
412 5261c201 2018-04-01 stsp free(ref);
413 5261c201 2018-04-01 stsp }
414 5261c201 2018-04-01 stsp
415 5261c201 2018-04-01 stsp struct got_reference *
416 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
417 5261c201 2018-04-01 stsp {
418 5261c201 2018-04-01 stsp struct got_reference *ret;
419 5261c201 2018-04-01 stsp
420 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
421 5261c201 2018-04-01 stsp if (ret == NULL)
422 5261c201 2018-04-01 stsp return NULL;
423 5261c201 2018-04-01 stsp
424 5261c201 2018-04-01 stsp ret->flags = ref->flags;
425 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
426 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
427 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
428 5261c201 2018-04-01 stsp free(ret);
429 5261c201 2018-04-01 stsp return NULL;
430 5261c201 2018-04-01 stsp }
431 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
432 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
433 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
434 5261c201 2018-04-01 stsp free(ret);
435 5261c201 2018-04-01 stsp return NULL;
436 5261c201 2018-04-01 stsp }
437 5261c201 2018-04-01 stsp } else {
438 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
439 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
440 5261c201 2018-04-01 stsp free(ret);
441 5261c201 2018-04-01 stsp return NULL;
442 5261c201 2018-04-01 stsp }
443 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
444 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
445 5261c201 2018-04-01 stsp }
446 5261c201 2018-04-01 stsp
447 5261c201 2018-04-01 stsp return ret;
448 5261c201 2018-04-01 stsp }
449 5261c201 2018-04-01 stsp
450 5261c201 2018-04-01 stsp static const struct got_error *
451 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
452 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
453 5261c201 2018-04-01 stsp {
454 5261c201 2018-04-01 stsp struct got_reference *nextref;
455 5261c201 2018-04-01 stsp const struct got_error *err;
456 5261c201 2018-04-01 stsp
457 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
458 5261c201 2018-04-01 stsp if (err)
459 5261c201 2018-04-01 stsp return err;
460 5261c201 2018-04-01 stsp
461 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
462 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
463 5261c201 2018-04-01 stsp else
464 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
465 5261c201 2018-04-01 stsp
466 5261c201 2018-04-01 stsp got_ref_close(nextref);
467 5261c201 2018-04-01 stsp return err;
468 5261c201 2018-04-01 stsp }
469 5261c201 2018-04-01 stsp
470 5261c201 2018-04-01 stsp const struct got_error *
471 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
472 5261c201 2018-04-01 stsp struct got_reference *ref)
473 5261c201 2018-04-01 stsp {
474 5261c201 2018-04-01 stsp const struct got_error *err;
475 5261c201 2018-04-01 stsp
476 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
477 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
478 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
479 5261c201 2018-04-01 stsp if (err == NULL)
480 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
481 5261c201 2018-04-01 stsp free(resolved);
482 5261c201 2018-04-01 stsp return err;
483 5261c201 2018-04-01 stsp }
484 5261c201 2018-04-01 stsp
485 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
486 5261c201 2018-04-01 stsp if (*id == NULL)
487 5261c201 2018-04-01 stsp return got_error_from_errno();
488 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
489 5261c201 2018-04-01 stsp return NULL;
490 5261c201 2018-04-01 stsp }
491 5261c201 2018-04-01 stsp
492 5261c201 2018-04-01 stsp char *
493 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
494 5261c201 2018-04-01 stsp {
495 5261c201 2018-04-01 stsp char *str;
496 271d2a38 2018-12-25 stsp
497 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
498 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
499 271d2a38 2018-12-25 stsp
500 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
501 271d2a38 2018-12-25 stsp if (str == NULL)
502 271d2a38 2018-12-25 stsp return NULL;
503 271d2a38 2018-12-25 stsp
504 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
505 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
506 271d2a38 2018-12-25 stsp free(str);
507 271d2a38 2018-12-25 stsp return NULL;
508 5261c201 2018-04-01 stsp }
509 5261c201 2018-04-01 stsp
510 5261c201 2018-04-01 stsp return str;
511 5261c201 2018-04-01 stsp }
512 0bd18d37 2019-02-01 stsp
513 0bd18d37 2019-02-01 stsp const char *
514 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
515 0bd18d37 2019-02-01 stsp {
516 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
517 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
518 0bd18d37 2019-02-01 stsp
519 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
520 199a4027 2019-02-02 stsp }
521 199a4027 2019-02-02 stsp
522 199a4027 2019-02-02 stsp static const struct got_error *
523 29b5c214 2019-02-04 stsp insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
524 199a4027 2019-02-02 stsp struct got_repository *repo)
525 199a4027 2019-02-02 stsp {
526 199a4027 2019-02-02 stsp const struct got_error *err;
527 199a4027 2019-02-02 stsp struct got_object_id *id;
528 7a3c76f5 2019-02-05 stsp struct got_reflist_entry *new, *re, *prev;
529 e397b6db 2019-02-04 stsp int cmp;
530 7a3c76f5 2019-02-05 stsp
531 7a3c76f5 2019-02-05 stsp err = got_ref_resolve(&id, repo, ref);
532 7a3c76f5 2019-02-05 stsp if (err)
533 7a3c76f5 2019-02-05 stsp return err;
534 29b5c214 2019-02-04 stsp
535 7a3c76f5 2019-02-05 stsp new = malloc(sizeof(*re));
536 7a3c76f5 2019-02-05 stsp if (new == NULL) {
537 7a3c76f5 2019-02-05 stsp free(id);
538 7a3c76f5 2019-02-05 stsp return got_error_from_errno();
539 7a3c76f5 2019-02-05 stsp }
540 7a3c76f5 2019-02-05 stsp new->ref = ref;
541 7a3c76f5 2019-02-05 stsp new->id = id;
542 7a3c76f5 2019-02-05 stsp
543 29b5c214 2019-02-04 stsp /*
544 29b5c214 2019-02-04 stsp * We must de-duplicate entries on insert because packed-refs may
545 29b5c214 2019-02-04 stsp * contain redundant entries. On-disk refs take precedence.
546 29b5c214 2019-02-04 stsp * This code assumes that on-disk revs are read before packed-refs.
547 e397b6db 2019-02-04 stsp * We're iterating the list anyway, so insert elements sorted by name.
548 29b5c214 2019-02-04 stsp */
549 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_FIRST(refs);
550 7a3c76f5 2019-02-05 stsp while (re) {
551 7a3c76f5 2019-02-05 stsp cmp = got_path_cmp(got_ref_get_name(re->ref),
552 7a3c76f5 2019-02-05 stsp got_ref_get_name(ref));
553 e397b6db 2019-02-04 stsp if (cmp == 0) {
554 7a3c76f5 2019-02-05 stsp free(ref); /* duplicate */
555 7a3c76f5 2019-02-05 stsp return NULL;
556 7a3c76f5 2019-02-05 stsp } else if (cmp > 0) {
557 7a3c76f5 2019-02-05 stsp if (prev)
558 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
559 7a3c76f5 2019-02-05 stsp else
560 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_HEAD(refs, new, entry);
561 7a3c76f5 2019-02-05 stsp return NULL;
562 7a3c76f5 2019-02-05 stsp } else {
563 e397b6db 2019-02-04 stsp prev = re;
564 7a3c76f5 2019-02-05 stsp re = SIMPLEQ_NEXT(re, entry);
565 7a3c76f5 2019-02-05 stsp }
566 29b5c214 2019-02-04 stsp }
567 199a4027 2019-02-02 stsp
568 7a3c76f5 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(refs, new, entry);
569 199a4027 2019-02-02 stsp return NULL;
570 0bd18d37 2019-02-01 stsp }
571 199a4027 2019-02-02 stsp
572 a04f49d2 2019-02-04 stsp static const struct got_error *
573 29b5c214 2019-02-04 stsp gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
574 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
575 a04f49d2 2019-02-04 stsp {
576 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
577 a04f49d2 2019-02-04 stsp DIR *d = NULL;
578 a04f49d2 2019-02-04 stsp char *path_subdir;
579 a04f49d2 2019-02-04 stsp
580 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
581 a04f49d2 2019-02-04 stsp return got_error_from_errno();
582 a04f49d2 2019-02-04 stsp
583 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
584 a04f49d2 2019-02-04 stsp if (d == NULL)
585 a04f49d2 2019-02-04 stsp goto done;
586 a04f49d2 2019-02-04 stsp
587 a04f49d2 2019-02-04 stsp while (1) {
588 a04f49d2 2019-02-04 stsp struct dirent *dent;
589 a04f49d2 2019-02-04 stsp struct got_reference *ref;
590 a04f49d2 2019-02-04 stsp char *child;
591 a04f49d2 2019-02-04 stsp
592 a04f49d2 2019-02-04 stsp dent = readdir(d);
593 a04f49d2 2019-02-04 stsp if (dent == NULL)
594 a04f49d2 2019-02-04 stsp break;
595 a04f49d2 2019-02-04 stsp
596 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
597 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
598 a04f49d2 2019-02-04 stsp continue;
599 a04f49d2 2019-02-04 stsp
600 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
601 a04f49d2 2019-02-04 stsp case DT_REG:
602 a04f49d2 2019-02-04 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name);
603 1e37702e 2019-02-04 stsp if (err)
604 a04f49d2 2019-02-04 stsp goto done;
605 a04f49d2 2019-02-04 stsp if (ref) {
606 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
607 a04f49d2 2019-02-04 stsp if (err)
608 a04f49d2 2019-02-04 stsp goto done;
609 a04f49d2 2019-02-04 stsp }
610 a04f49d2 2019-02-04 stsp break;
611 a04f49d2 2019-02-04 stsp case DT_DIR:
612 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
613 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
614 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
615 a04f49d2 2019-02-04 stsp break;
616 a04f49d2 2019-02-04 stsp }
617 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, child, repo);
618 a04f49d2 2019-02-04 stsp free(child);
619 a04f49d2 2019-02-04 stsp break;
620 a04f49d2 2019-02-04 stsp default:
621 a04f49d2 2019-02-04 stsp break;
622 a04f49d2 2019-02-04 stsp }
623 a04f49d2 2019-02-04 stsp }
624 a04f49d2 2019-02-04 stsp done:
625 a04f49d2 2019-02-04 stsp if (d)
626 a04f49d2 2019-02-04 stsp closedir(d);
627 a04f49d2 2019-02-04 stsp free(path_subdir);
628 a04f49d2 2019-02-04 stsp return err;
629 a04f49d2 2019-02-04 stsp }
630 a04f49d2 2019-02-04 stsp
631 199a4027 2019-02-02 stsp const struct got_error *
632 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
633 199a4027 2019-02-02 stsp {
634 199a4027 2019-02-02 stsp const struct got_error *err;
635 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
636 29b5c214 2019-02-04 stsp FILE *f = NULL;
637 199a4027 2019-02-02 stsp struct got_reference *ref;
638 199a4027 2019-02-02 stsp
639 29b5c214 2019-02-04 stsp /* HEAD ref should always exist. */
640 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
641 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
642 29b5c214 2019-02-04 stsp err = got_error_from_errno();
643 29b5c214 2019-02-04 stsp goto done;
644 29b5c214 2019-02-04 stsp }
645 29b5c214 2019-02-04 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
646 29b5c214 2019-02-04 stsp if (err)
647 29b5c214 2019-02-04 stsp goto done;
648 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
649 29b5c214 2019-02-04 stsp if (err)
650 29b5c214 2019-02-04 stsp goto done;
651 29b5c214 2019-02-04 stsp
652 29b5c214 2019-02-04 stsp /* Gather on-disk refs before parsing packed-refs. */
653 29b5c214 2019-02-04 stsp free(path_refs);
654 29b5c214 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
655 29b5c214 2019-02-04 stsp if (path_refs == NULL) {
656 29b5c214 2019-02-04 stsp err = got_error_from_errno();
657 29b5c214 2019-02-04 stsp goto done;
658 29b5c214 2019-02-04 stsp }
659 29b5c214 2019-02-04 stsp err = gather_on_disk_refs(refs, path_refs, "", repo);
660 29b5c214 2019-02-04 stsp if (err)
661 29b5c214 2019-02-04 stsp goto done;
662 29b5c214 2019-02-04 stsp
663 29b5c214 2019-02-04 stsp /*
664 29b5c214 2019-02-04 stsp * The packed-refs file may contain redundant entries, in which
665 29b5c214 2019-02-04 stsp * case on-disk refs take precedence.
666 29b5c214 2019-02-04 stsp */
667 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
668 29b5c214 2019-02-04 stsp if (packed_refs_path == NULL) {
669 29b5c214 2019-02-04 stsp err = got_error_from_errno();
670 29b5c214 2019-02-04 stsp goto done;
671 29b5c214 2019-02-04 stsp }
672 199a4027 2019-02-02 stsp
673 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
674 199a4027 2019-02-02 stsp free(packed_refs_path);
675 199a4027 2019-02-02 stsp if (f) {
676 199a4027 2019-02-02 stsp char *line;
677 199a4027 2019-02-02 stsp size_t len;
678 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
679 199a4027 2019-02-02 stsp while (1) {
680 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
681 199a4027 2019-02-02 stsp if (line == NULL)
682 199a4027 2019-02-02 stsp break;
683 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
684 199a4027 2019-02-02 stsp if (err)
685 199a4027 2019-02-02 stsp goto done;
686 76b4ead2 2019-02-03 stsp if (ref) {
687 29b5c214 2019-02-04 stsp err = insert_ref(refs, ref, repo);
688 76b4ead2 2019-02-03 stsp if (err)
689 76b4ead2 2019-02-03 stsp goto done;
690 76b4ead2 2019-02-03 stsp }
691 199a4027 2019-02-02 stsp }
692 199a4027 2019-02-02 stsp }
693 199a4027 2019-02-02 stsp done:
694 a04f49d2 2019-02-04 stsp free(path_refs);
695 fb43ecf1 2019-02-11 stsp if (f && fclose(f) != 0 && err == NULL)
696 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
697 199a4027 2019-02-02 stsp return err;
698 199a4027 2019-02-02 stsp }