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 5261c201 2018-04-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
105 5261c201 2018-04-01 stsp char *ref_name;
106 5261c201 2018-04-01 stsp
107 5261c201 2018-04-01 stsp if (strncmp(line, "ref: ", 5) == 0) {
108 5261c201 2018-04-01 stsp line += 5;
109 5261c201 2018-04-01 stsp return parse_symref(ref, name, line);
110 5261c201 2018-04-01 stsp }
111 5261c201 2018-04-01 stsp
112 5261c201 2018-04-01 stsp ref_name = strdup(name);
113 5261c201 2018-04-01 stsp if (ref_name == NULL)
114 5261c201 2018-04-01 stsp return got_error_from_errno();
115 5261c201 2018-04-01 stsp
116 5261c201 2018-04-01 stsp if (!got_parse_sha1_digest(digest, line))
117 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
118 5261c201 2018-04-01 stsp
119 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
120 5261c201 2018-04-01 stsp if (*ref == NULL)
121 5261c201 2018-04-01 stsp return got_error_from_errno();
122 5261c201 2018-04-01 stsp (*ref)->ref.ref.name = ref_name;
123 5261c201 2018-04-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
124 5261c201 2018-04-01 stsp return NULL;
125 5261c201 2018-04-01 stsp }
126 5261c201 2018-04-01 stsp
127 5261c201 2018-04-01 stsp static const struct got_error *
128 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
129 5261c201 2018-04-01 stsp const char *abspath)
130 5261c201 2018-04-01 stsp {
131 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
132 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
133 5261c201 2018-04-01 stsp char *line;
134 5261c201 2018-04-01 stsp size_t len;
135 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
136 5261c201 2018-04-01 stsp
137 5261c201 2018-04-01 stsp if (f == NULL)
138 30c0868d 2019-02-03 stsp return got_error_not_ref(name);
139 5261c201 2018-04-01 stsp
140 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
141 5261c201 2018-04-01 stsp if (line == NULL) {
142 30c0868d 2019-02-03 stsp err = got_error(GOT_ERR_BAD_REF_DATA);
143 5261c201 2018-04-01 stsp goto done;
144 5261c201 2018-04-01 stsp }
145 5261c201 2018-04-01 stsp
146 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
147 5261c201 2018-04-01 stsp done:
148 5261c201 2018-04-01 stsp free(line);
149 5261c201 2018-04-01 stsp fclose(f);
150 5261c201 2018-04-01 stsp return err;
151 5261c201 2018-04-01 stsp }
152 5261c201 2018-04-01 stsp
153 c5f754cc 2019-02-01 stsp static int
154 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
155 5261c201 2018-04-01 stsp {
156 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
157 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
158 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
159 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
160 c5f754cc 2019-02-01 stsp }
161 c5f754cc 2019-02-01 stsp
162 c5f754cc 2019-02-01 stsp static char *
163 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
164 c5f754cc 2019-02-01 stsp {
165 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
166 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
167 5261c201 2018-04-01 stsp
168 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
169 5261c201 2018-04-01 stsp }
170 5261c201 2018-04-01 stsp
171 d1f2edc9 2018-06-13 stsp static const struct got_error *
172 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
173 fb79db15 2019-02-01 stsp const char *line)
174 fb79db15 2019-02-01 stsp {
175 fb79db15 2019-02-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
176 fb79db15 2019-02-01 stsp char *name;
177 fb79db15 2019-02-01 stsp
178 fb79db15 2019-02-01 stsp *ref = NULL;
179 fb79db15 2019-02-01 stsp
180 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
181 fb79db15 2019-02-01 stsp return NULL;
182 fb79db15 2019-02-01 stsp
183 fb79db15 2019-02-01 stsp if (!got_parse_sha1_digest(digest, line))
184 30c0868d 2019-02-03 stsp return got_error(GOT_ERR_BAD_REF_DATA);
185 fb79db15 2019-02-01 stsp
186 199a4027 2019-02-02 stsp if (abs_refname) {
187 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
188 199a4027 2019-02-02 stsp return NULL;
189 fb79db15 2019-02-01 stsp
190 199a4027 2019-02-02 stsp name = strdup(abs_refname);
191 199a4027 2019-02-02 stsp if (name == NULL)
192 199a4027 2019-02-02 stsp return got_error_from_errno();
193 199a4027 2019-02-02 stsp } else
194 199a4027 2019-02-02 stsp name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
195 fb79db15 2019-02-01 stsp
196 fb79db15 2019-02-01 stsp *ref = calloc(1, sizeof(**ref));
197 fb79db15 2019-02-01 stsp if (*ref == NULL)
198 fb79db15 2019-02-01 stsp return got_error_from_errno();
199 fb79db15 2019-02-01 stsp (*ref)->ref.ref.name = name;;
200 fb79db15 2019-02-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
201 fb79db15 2019-02-01 stsp return NULL;
202 fb79db15 2019-02-01 stsp }
203 fb79db15 2019-02-01 stsp
204 fb79db15 2019-02-01 stsp static const struct got_error *
205 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
206 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
207 fb79db15 2019-02-01 stsp {
208 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
209 fb79db15 2019-02-01 stsp char *abs_refname;
210 fb79db15 2019-02-01 stsp char *line;
211 fb79db15 2019-02-01 stsp size_t len;
212 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
213 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
214 fb79db15 2019-02-01 stsp
215 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
216 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
217 fb79db15 2019-02-01 stsp do {
218 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
219 fb79db15 2019-02-01 stsp if (line == NULL) {
220 30c0868d 2019-02-03 stsp err = got_error_not_ref(refname);
221 fb79db15 2019-02-01 stsp break;
222 fb79db15 2019-02-01 stsp }
223 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
224 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
225 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
226 0dec1cc0 2019-02-01 stsp refname) == -1)
227 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
228 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
229 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
230 0dec1cc0 2019-02-01 stsp free(abs_refname);
231 532920c8 2019-02-01 stsp if (err || *ref != NULL)
232 0dec1cc0 2019-02-01 stsp break;
233 0dec1cc0 2019-02-01 stsp }
234 fb79db15 2019-02-01 stsp free(line);
235 fb79db15 2019-02-01 stsp if (err)
236 fb79db15 2019-02-01 stsp break;
237 fb79db15 2019-02-01 stsp } while (*ref == NULL);
238 fb79db15 2019-02-01 stsp
239 fb79db15 2019-02-01 stsp return err;
240 fb79db15 2019-02-01 stsp }
241 fb79db15 2019-02-01 stsp
242 fb79db15 2019-02-01 stsp static const struct got_error *
243 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
244 a04f49d2 2019-02-04 stsp const char *name)
245 d1f2edc9 2018-06-13 stsp {
246 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
247 a04f49d2 2019-02-04 stsp char *path = NULL;
248 a04f49d2 2019-02-04 stsp char *normpath = NULL;
249 a04f49d2 2019-02-04 stsp char *absname = NULL;
250 a04f49d2 2019-02-04 stsp int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
251 a04f49d2 2019-02-04 stsp int ref_is_well_known = is_well_known_ref(name);
252 d1f2edc9 2018-06-13 stsp
253 a04f49d2 2019-02-04 stsp if (ref_is_absolute || ref_is_well_known) {
254 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s", path_refs, name) == -1)
255 a04f49d2 2019-02-04 stsp return got_error_from_errno();
256 a04f49d2 2019-02-04 stsp absname = (char *)name;
257 a04f49d2 2019-02-04 stsp } else {
258 a04f49d2 2019-02-04 stsp if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
259 a04f49d2 2019-02-04 stsp return got_error_from_errno();
260 d1f2edc9 2018-06-13 stsp
261 a04f49d2 2019-02-04 stsp if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
262 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
263 a04f49d2 2019-02-04 stsp goto done;
264 a04f49d2 2019-02-04 stsp }
265 a04f49d2 2019-02-04 stsp }
266 a04f49d2 2019-02-04 stsp
267 a04f49d2 2019-02-04 stsp normpath = got_path_normalize(path);
268 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
269 30c0868d 2019-02-03 stsp err = got_error_from_errno();
270 d1f2edc9 2018-06-13 stsp goto done;
271 d1f2edc9 2018-06-13 stsp }
272 d1f2edc9 2018-06-13 stsp
273 a04f49d2 2019-02-04 stsp err = parse_ref_file(ref, absname, normpath);
274 d1f2edc9 2018-06-13 stsp done:
275 a04f49d2 2019-02-04 stsp if (!ref_is_absolute && !ref_is_well_known)
276 a04f49d2 2019-02-04 stsp free(absname);
277 a04f49d2 2019-02-04 stsp free(path);
278 d1f2edc9 2018-06-13 stsp free(normpath);
279 d1f2edc9 2018-06-13 stsp return err;
280 d1f2edc9 2018-06-13 stsp }
281 d1f2edc9 2018-06-13 stsp
282 5261c201 2018-04-01 stsp const struct got_error *
283 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
284 5261c201 2018-04-01 stsp const char *refname)
285 5261c201 2018-04-01 stsp {
286 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
287 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
288 fb79db15 2019-02-01 stsp const char *subdirs[] = {
289 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
290 fb79db15 2019-02-01 stsp };
291 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
292 5261c201 2018-04-01 stsp
293 c5f754cc 2019-02-01 stsp if (!well_known) {
294 c5f754cc 2019-02-01 stsp char *packed_refs_path;
295 c5f754cc 2019-02-01 stsp FILE *f;
296 c5f754cc 2019-02-01 stsp
297 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
298 c5f754cc 2019-02-01 stsp if (packed_refs_path == NULL)
299 c5f754cc 2019-02-01 stsp return got_error_from_errno();
300 c5f754cc 2019-02-01 stsp
301 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
302 c5f754cc 2019-02-01 stsp free(packed_refs_path);
303 c5f754cc 2019-02-01 stsp if (f != NULL) {
304 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
305 0dec1cc0 2019-02-01 stsp refname);
306 c5f754cc 2019-02-01 stsp fclose(f);
307 0dec1cc0 2019-02-01 stsp if (err == NULL)
308 0dec1cc0 2019-02-01 stsp goto done;
309 fb79db15 2019-02-01 stsp }
310 fb79db15 2019-02-01 stsp }
311 fb79db15 2019-02-01 stsp
312 fb79db15 2019-02-01 stsp path_refs = get_refs_dir_path(repo, refname);
313 5261c201 2018-04-01 stsp if (path_refs == NULL) {
314 5261c201 2018-04-01 stsp err = got_error_from_errno();
315 5261c201 2018-04-01 stsp goto done;
316 5261c201 2018-04-01 stsp }
317 c5f754cc 2019-02-01 stsp
318 c5f754cc 2019-02-01 stsp if (!well_known) {
319 c5f754cc 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
320 c5f754cc 2019-02-01 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
321 c5f754cc 2019-02-01 stsp if (err == NULL)
322 c5f754cc 2019-02-01 stsp goto done;
323 c5f754cc 2019-02-01 stsp }
324 fb79db15 2019-02-01 stsp }
325 c5f754cc 2019-02-01 stsp
326 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
327 5261c201 2018-04-01 stsp done:
328 5261c201 2018-04-01 stsp free(path_refs);
329 5261c201 2018-04-01 stsp return err;
330 5261c201 2018-04-01 stsp }
331 5261c201 2018-04-01 stsp
332 5261c201 2018-04-01 stsp void
333 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
334 5261c201 2018-04-01 stsp {
335 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
336 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
337 5261c201 2018-04-01 stsp else
338 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
339 5261c201 2018-04-01 stsp free(ref);
340 5261c201 2018-04-01 stsp }
341 5261c201 2018-04-01 stsp
342 5261c201 2018-04-01 stsp struct got_reference *
343 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
344 5261c201 2018-04-01 stsp {
345 5261c201 2018-04-01 stsp struct got_reference *ret;
346 5261c201 2018-04-01 stsp
347 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
348 5261c201 2018-04-01 stsp if (ret == NULL)
349 5261c201 2018-04-01 stsp return NULL;
350 5261c201 2018-04-01 stsp
351 5261c201 2018-04-01 stsp ret->flags = ref->flags;
352 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
353 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
354 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
355 5261c201 2018-04-01 stsp free(ret);
356 5261c201 2018-04-01 stsp return NULL;
357 5261c201 2018-04-01 stsp }
358 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
359 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
360 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
361 5261c201 2018-04-01 stsp free(ret);
362 5261c201 2018-04-01 stsp return NULL;
363 5261c201 2018-04-01 stsp }
364 5261c201 2018-04-01 stsp } else {
365 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
366 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
367 5261c201 2018-04-01 stsp free(ret);
368 5261c201 2018-04-01 stsp return NULL;
369 5261c201 2018-04-01 stsp }
370 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
371 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
372 5261c201 2018-04-01 stsp }
373 5261c201 2018-04-01 stsp
374 5261c201 2018-04-01 stsp return ret;
375 5261c201 2018-04-01 stsp }
376 5261c201 2018-04-01 stsp
377 5261c201 2018-04-01 stsp static const struct got_error *
378 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
379 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
380 5261c201 2018-04-01 stsp {
381 5261c201 2018-04-01 stsp struct got_reference *nextref;
382 5261c201 2018-04-01 stsp const struct got_error *err;
383 5261c201 2018-04-01 stsp
384 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
385 5261c201 2018-04-01 stsp if (err)
386 5261c201 2018-04-01 stsp return err;
387 5261c201 2018-04-01 stsp
388 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
389 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
390 5261c201 2018-04-01 stsp else
391 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
392 5261c201 2018-04-01 stsp
393 5261c201 2018-04-01 stsp got_ref_close(nextref);
394 5261c201 2018-04-01 stsp return err;
395 5261c201 2018-04-01 stsp }
396 5261c201 2018-04-01 stsp
397 5261c201 2018-04-01 stsp const struct got_error *
398 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
399 5261c201 2018-04-01 stsp struct got_reference *ref)
400 5261c201 2018-04-01 stsp {
401 5261c201 2018-04-01 stsp const struct got_error *err;
402 5261c201 2018-04-01 stsp
403 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
404 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
405 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
406 5261c201 2018-04-01 stsp if (err == NULL)
407 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
408 5261c201 2018-04-01 stsp free(resolved);
409 5261c201 2018-04-01 stsp return err;
410 5261c201 2018-04-01 stsp }
411 5261c201 2018-04-01 stsp
412 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
413 5261c201 2018-04-01 stsp if (*id == NULL)
414 5261c201 2018-04-01 stsp return got_error_from_errno();
415 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
416 5261c201 2018-04-01 stsp return NULL;
417 5261c201 2018-04-01 stsp }
418 5261c201 2018-04-01 stsp
419 5261c201 2018-04-01 stsp char *
420 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
421 5261c201 2018-04-01 stsp {
422 5261c201 2018-04-01 stsp char *str;
423 271d2a38 2018-12-25 stsp
424 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
425 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
426 271d2a38 2018-12-25 stsp
427 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
428 271d2a38 2018-12-25 stsp if (str == NULL)
429 271d2a38 2018-12-25 stsp return NULL;
430 271d2a38 2018-12-25 stsp
431 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
432 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
433 271d2a38 2018-12-25 stsp free(str);
434 271d2a38 2018-12-25 stsp return NULL;
435 5261c201 2018-04-01 stsp }
436 5261c201 2018-04-01 stsp
437 5261c201 2018-04-01 stsp return str;
438 5261c201 2018-04-01 stsp }
439 0bd18d37 2019-02-01 stsp
440 0bd18d37 2019-02-01 stsp const char *
441 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
442 0bd18d37 2019-02-01 stsp {
443 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
444 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
445 0bd18d37 2019-02-01 stsp
446 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
447 199a4027 2019-02-02 stsp }
448 199a4027 2019-02-02 stsp
449 199a4027 2019-02-02 stsp static const struct got_error *
450 199a4027 2019-02-02 stsp append_ref(struct got_reflist_head *refs, struct got_reference *ref,
451 199a4027 2019-02-02 stsp struct got_repository *repo)
452 199a4027 2019-02-02 stsp {
453 199a4027 2019-02-02 stsp const struct got_error *err;
454 199a4027 2019-02-02 stsp struct got_object_id *id;
455 199a4027 2019-02-02 stsp struct got_reflist_entry *entry;
456 199a4027 2019-02-02 stsp
457 199a4027 2019-02-02 stsp err = got_ref_resolve(&id, repo, ref);
458 199a4027 2019-02-02 stsp if (err)
459 199a4027 2019-02-02 stsp return err;
460 199a4027 2019-02-02 stsp entry = malloc(sizeof(*entry));
461 199a4027 2019-02-02 stsp if (entry == NULL)
462 199a4027 2019-02-02 stsp return got_error_from_errno();
463 199a4027 2019-02-02 stsp entry->ref = ref;
464 199a4027 2019-02-02 stsp entry->id = id;
465 199a4027 2019-02-02 stsp SIMPLEQ_INSERT_TAIL(refs, entry, entry);
466 199a4027 2019-02-02 stsp return NULL;
467 0bd18d37 2019-02-01 stsp }
468 199a4027 2019-02-02 stsp
469 a04f49d2 2019-02-04 stsp static const struct got_error *
470 a04f49d2 2019-02-04 stsp gather_refs(struct got_reflist_head *refs, const char *path_refs,
471 a04f49d2 2019-02-04 stsp const char *subdir, struct got_repository *repo)
472 a04f49d2 2019-02-04 stsp {
473 a04f49d2 2019-02-04 stsp const struct got_error *err = NULL;
474 a04f49d2 2019-02-04 stsp DIR *d = NULL;
475 a04f49d2 2019-02-04 stsp char *path_subdir;
476 a04f49d2 2019-02-04 stsp
477 a04f49d2 2019-02-04 stsp if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
478 a04f49d2 2019-02-04 stsp return got_error_from_errno();
479 a04f49d2 2019-02-04 stsp
480 a04f49d2 2019-02-04 stsp d = opendir(path_subdir);
481 a04f49d2 2019-02-04 stsp if (d == NULL)
482 a04f49d2 2019-02-04 stsp goto done;
483 a04f49d2 2019-02-04 stsp
484 a04f49d2 2019-02-04 stsp while (1) {
485 a04f49d2 2019-02-04 stsp struct dirent *dent;
486 a04f49d2 2019-02-04 stsp struct got_reference *ref;
487 a04f49d2 2019-02-04 stsp char *child;
488 a04f49d2 2019-02-04 stsp
489 a04f49d2 2019-02-04 stsp dent = readdir(d);
490 a04f49d2 2019-02-04 stsp if (dent == NULL)
491 a04f49d2 2019-02-04 stsp break;
492 a04f49d2 2019-02-04 stsp
493 a04f49d2 2019-02-04 stsp if (strcmp(dent->d_name, ".") == 0 ||
494 a04f49d2 2019-02-04 stsp strcmp(dent->d_name, "..") == 0)
495 a04f49d2 2019-02-04 stsp continue;
496 a04f49d2 2019-02-04 stsp
497 a04f49d2 2019-02-04 stsp switch (dent->d_type) {
498 a04f49d2 2019-02-04 stsp case DT_REG:
499 a04f49d2 2019-02-04 stsp err = open_ref(&ref, path_refs, subdir, dent->d_name);
500 a04f49d2 2019-02-04 stsp if (err && err->code != GOT_ERR_NOT_REF)
501 a04f49d2 2019-02-04 stsp goto done;
502 a04f49d2 2019-02-04 stsp if (ref) {
503 a04f49d2 2019-02-04 stsp err = append_ref(refs, ref, repo);
504 a04f49d2 2019-02-04 stsp if (err)
505 a04f49d2 2019-02-04 stsp goto done;
506 a04f49d2 2019-02-04 stsp }
507 a04f49d2 2019-02-04 stsp break;
508 a04f49d2 2019-02-04 stsp case DT_DIR:
509 a04f49d2 2019-02-04 stsp if (asprintf(&child, "%s%s%s", subdir,
510 a04f49d2 2019-02-04 stsp subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
511 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
512 a04f49d2 2019-02-04 stsp break;
513 a04f49d2 2019-02-04 stsp }
514 a04f49d2 2019-02-04 stsp err = gather_refs(refs, path_refs, child, repo);
515 a04f49d2 2019-02-04 stsp free(child);
516 a04f49d2 2019-02-04 stsp break;
517 a04f49d2 2019-02-04 stsp default:
518 a04f49d2 2019-02-04 stsp break;
519 a04f49d2 2019-02-04 stsp }
520 a04f49d2 2019-02-04 stsp }
521 a04f49d2 2019-02-04 stsp done:
522 a04f49d2 2019-02-04 stsp if (d)
523 a04f49d2 2019-02-04 stsp closedir(d);
524 a04f49d2 2019-02-04 stsp free(path_subdir);
525 a04f49d2 2019-02-04 stsp return err;
526 a04f49d2 2019-02-04 stsp }
527 a04f49d2 2019-02-04 stsp
528 199a4027 2019-02-02 stsp const struct got_error *
529 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
530 199a4027 2019-02-02 stsp {
531 199a4027 2019-02-02 stsp const struct got_error *err;
532 a04f49d2 2019-02-04 stsp char *packed_refs_path, *path_refs = NULL;
533 199a4027 2019-02-02 stsp FILE *f;
534 199a4027 2019-02-02 stsp struct got_reference *ref;
535 199a4027 2019-02-02 stsp
536 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
537 199a4027 2019-02-02 stsp if (packed_refs_path == NULL)
538 199a4027 2019-02-02 stsp return got_error_from_errno();
539 199a4027 2019-02-02 stsp
540 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
541 199a4027 2019-02-02 stsp free(packed_refs_path);
542 199a4027 2019-02-02 stsp if (f) {
543 199a4027 2019-02-02 stsp char *line;
544 199a4027 2019-02-02 stsp size_t len;
545 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
546 199a4027 2019-02-02 stsp while (1) {
547 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
548 199a4027 2019-02-02 stsp if (line == NULL)
549 199a4027 2019-02-02 stsp break;
550 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
551 199a4027 2019-02-02 stsp if (err)
552 199a4027 2019-02-02 stsp goto done;
553 76b4ead2 2019-02-03 stsp if (ref) {
554 76b4ead2 2019-02-03 stsp err = append_ref(refs, ref, repo);
555 76b4ead2 2019-02-03 stsp if (err)
556 76b4ead2 2019-02-03 stsp goto done;
557 76b4ead2 2019-02-03 stsp }
558 199a4027 2019-02-02 stsp }
559 199a4027 2019-02-02 stsp }
560 199a4027 2019-02-02 stsp
561 199a4027 2019-02-02 stsp /* HEAD ref should always exist. */
562 199a4027 2019-02-02 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
563 199a4027 2019-02-02 stsp if (path_refs == NULL) {
564 199a4027 2019-02-02 stsp err = got_error_from_errno();
565 199a4027 2019-02-02 stsp goto done;
566 199a4027 2019-02-02 stsp }
567 199a4027 2019-02-02 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
568 199a4027 2019-02-02 stsp if (err)
569 199a4027 2019-02-02 stsp goto done;
570 76b4ead2 2019-02-03 stsp err = append_ref(refs, ref, repo);
571 76b4ead2 2019-02-03 stsp if (err)
572 76b4ead2 2019-02-03 stsp goto done;
573 199a4027 2019-02-02 stsp
574 a04f49d2 2019-02-04 stsp free(path_refs);
575 a04f49d2 2019-02-04 stsp path_refs = get_refs_dir_path(repo, "");
576 a04f49d2 2019-02-04 stsp if (path_refs == NULL) {
577 a04f49d2 2019-02-04 stsp err = got_error_from_errno();
578 a04f49d2 2019-02-04 stsp goto done;
579 a04f49d2 2019-02-04 stsp }
580 a04f49d2 2019-02-04 stsp err = gather_refs(refs, path_refs, "", repo);
581 199a4027 2019-02-02 stsp done:
582 a04f49d2 2019-02-04 stsp free(path_refs);
583 199a4027 2019-02-02 stsp if (f)
584 199a4027 2019-02-02 stsp fclose(f);
585 199a4027 2019-02-02 stsp return err;
586 199a4027 2019-02-02 stsp }