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