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 5261c201 2018-04-01 stsp #include <sha1.h>
21 5261c201 2018-04-01 stsp #include <stdio.h>
22 5261c201 2018-04-01 stsp #include <stdlib.h>
23 5261c201 2018-04-01 stsp #include <string.h>
24 5261c201 2018-04-01 stsp #include <util.h>
25 5261c201 2018-04-01 stsp #include <zlib.h>
26 788c352e 2018-06-16 stsp #include <time.h>
27 5261c201 2018-04-01 stsp
28 5261c201 2018-04-01 stsp #include "got_error.h"
29 5261c201 2018-04-01 stsp #include "got_object.h"
30 5261c201 2018-04-01 stsp #include "got_repository.h"
31 5261c201 2018-04-01 stsp #include "got_reference.h"
32 5261c201 2018-04-01 stsp
33 5261c201 2018-04-01 stsp #include "got_lib_sha1.h"
34 5261c201 2018-04-01 stsp #include "got_lib_path.h"
35 5261c201 2018-04-01 stsp #include "got_lib_delta.h"
36 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
37 5261c201 2018-04-01 stsp #include "got_lib_object.h"
38 5261c201 2018-04-01 stsp
39 fb79db15 2019-02-01 stsp #ifndef nitems
40 fb79db15 2019-02-01 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 fb79db15 2019-02-01 stsp #endif
42 fb79db15 2019-02-01 stsp
43 d1f2edc9 2018-06-13 stsp #define GOT_REF_HEADS "heads"
44 d1f2edc9 2018-06-13 stsp #define GOT_REF_TAGS "tags"
45 d1f2edc9 2018-06-13 stsp #define GOT_REF_REMOTES "remotes"
46 d1f2edc9 2018-06-13 stsp
47 5261c201 2018-04-01 stsp /* A symbolic reference. */
48 5261c201 2018-04-01 stsp struct got_symref {
49 5261c201 2018-04-01 stsp char *name;
50 5261c201 2018-04-01 stsp char *ref;
51 5261c201 2018-04-01 stsp };
52 5261c201 2018-04-01 stsp
53 5261c201 2018-04-01 stsp /* A non-symbolic reference (there is no better designation). */
54 5261c201 2018-04-01 stsp struct got_ref {
55 5261c201 2018-04-01 stsp char *name;
56 5261c201 2018-04-01 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
57 5261c201 2018-04-01 stsp };
58 5261c201 2018-04-01 stsp
59 5261c201 2018-04-01 stsp /* A reference which points to an arbitrary object. */
60 5261c201 2018-04-01 stsp struct got_reference {
61 5261c201 2018-04-01 stsp unsigned int flags;
62 5261c201 2018-04-01 stsp #define GOT_REF_IS_SYMBOLIC 0x01
63 5261c201 2018-04-01 stsp
64 5261c201 2018-04-01 stsp union {
65 5261c201 2018-04-01 stsp struct got_ref ref;
66 5261c201 2018-04-01 stsp struct got_symref symref;
67 5261c201 2018-04-01 stsp } ref;
68 5261c201 2018-04-01 stsp };
69 5261c201 2018-04-01 stsp
70 5261c201 2018-04-01 stsp static const struct got_error *
71 5261c201 2018-04-01 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
72 5261c201 2018-04-01 stsp {
73 5261c201 2018-04-01 stsp struct got_symref *symref;
74 5261c201 2018-04-01 stsp char *symref_name;
75 5261c201 2018-04-01 stsp char *symref_ref;
76 5261c201 2018-04-01 stsp
77 5261c201 2018-04-01 stsp if (line[0] == '\0')
78 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
79 5261c201 2018-04-01 stsp
80 5261c201 2018-04-01 stsp symref_name = strdup(name);
81 5261c201 2018-04-01 stsp if (symref_name == NULL)
82 5261c201 2018-04-01 stsp return got_error_from_errno();
83 5261c201 2018-04-01 stsp symref_ref = strdup(line);
84 5261c201 2018-04-01 stsp if (symref_ref == NULL) {
85 5261c201 2018-04-01 stsp const struct got_error *err = got_error_from_errno();
86 5261c201 2018-04-01 stsp free(symref_name);
87 5261c201 2018-04-01 stsp return err;
88 5261c201 2018-04-01 stsp }
89 5261c201 2018-04-01 stsp
90 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
91 5261c201 2018-04-01 stsp if (*ref == NULL)
92 5261c201 2018-04-01 stsp return got_error_from_errno();
93 5261c201 2018-04-01 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 5261c201 2018-04-01 stsp symref = &((*ref)->ref.symref);
95 5261c201 2018-04-01 stsp symref->name = symref_name;
96 5261c201 2018-04-01 stsp symref->ref = symref_ref;
97 5261c201 2018-04-01 stsp return NULL;
98 5261c201 2018-04-01 stsp }
99 5261c201 2018-04-01 stsp
100 5261c201 2018-04-01 stsp static const struct got_error *
101 5261c201 2018-04-01 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
102 5261c201 2018-04-01 stsp {
103 5261c201 2018-04-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
104 5261c201 2018-04-01 stsp char *ref_name;
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 5261c201 2018-04-01 stsp ref_name = strdup(name);
112 5261c201 2018-04-01 stsp if (ref_name == NULL)
113 5261c201 2018-04-01 stsp return got_error_from_errno();
114 5261c201 2018-04-01 stsp
115 5261c201 2018-04-01 stsp if (!got_parse_sha1_digest(digest, line))
116 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
117 5261c201 2018-04-01 stsp
118 5261c201 2018-04-01 stsp *ref = calloc(1, sizeof(**ref));
119 5261c201 2018-04-01 stsp if (*ref == NULL)
120 5261c201 2018-04-01 stsp return got_error_from_errno();
121 5261c201 2018-04-01 stsp (*ref)->ref.ref.name = ref_name;
122 5261c201 2018-04-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
123 5261c201 2018-04-01 stsp return NULL;
124 5261c201 2018-04-01 stsp }
125 5261c201 2018-04-01 stsp
126 5261c201 2018-04-01 stsp static const struct got_error *
127 5261c201 2018-04-01 stsp parse_ref_file(struct got_reference **ref, const char *name,
128 5261c201 2018-04-01 stsp const char *abspath)
129 5261c201 2018-04-01 stsp {
130 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
131 5261c201 2018-04-01 stsp FILE *f = fopen(abspath, "rb");
132 5261c201 2018-04-01 stsp char *line;
133 5261c201 2018-04-01 stsp size_t len;
134 5261c201 2018-04-01 stsp const char delim[3] = {'\0', '\0', '\0'};
135 5261c201 2018-04-01 stsp
136 5261c201 2018-04-01 stsp if (f == NULL)
137 5261c201 2018-04-01 stsp return got_error(GOT_ERR_NOT_REF);
138 5261c201 2018-04-01 stsp
139 5261c201 2018-04-01 stsp line = fparseln(f, &len, NULL, delim, 0);
140 5261c201 2018-04-01 stsp if (line == NULL) {
141 5261c201 2018-04-01 stsp err = got_error(GOT_ERR_NOT_REF);
142 5261c201 2018-04-01 stsp goto done;
143 5261c201 2018-04-01 stsp }
144 5261c201 2018-04-01 stsp
145 5261c201 2018-04-01 stsp err = parse_ref_line(ref, name, line);
146 5261c201 2018-04-01 stsp done:
147 5261c201 2018-04-01 stsp free(line);
148 5261c201 2018-04-01 stsp fclose(f);
149 5261c201 2018-04-01 stsp return err;
150 5261c201 2018-04-01 stsp }
151 5261c201 2018-04-01 stsp
152 c5f754cc 2019-02-01 stsp static int
153 c5f754cc 2019-02-01 stsp is_well_known_ref(const char *refname)
154 5261c201 2018-04-01 stsp {
155 c5f754cc 2019-02-01 stsp return (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 5261c201 2018-04-01 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 c5f754cc 2019-02-01 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
159 c5f754cc 2019-02-01 stsp }
160 c5f754cc 2019-02-01 stsp
161 c5f754cc 2019-02-01 stsp static char *
162 c5f754cc 2019-02-01 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
163 c5f754cc 2019-02-01 stsp {
164 c5f754cc 2019-02-01 stsp if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
165 6e9da951 2019-01-06 stsp return strdup(got_repo_get_path_git_dir(repo));
166 5261c201 2018-04-01 stsp
167 5261c201 2018-04-01 stsp return got_repo_get_path_refs(repo);
168 5261c201 2018-04-01 stsp }
169 5261c201 2018-04-01 stsp
170 d1f2edc9 2018-06-13 stsp static const struct got_error *
171 fb79db15 2019-02-01 stsp parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
172 fb79db15 2019-02-01 stsp const char *line)
173 fb79db15 2019-02-01 stsp {
174 fb79db15 2019-02-01 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
175 fb79db15 2019-02-01 stsp char *name;
176 fb79db15 2019-02-01 stsp
177 fb79db15 2019-02-01 stsp *ref = NULL;
178 fb79db15 2019-02-01 stsp
179 532920c8 2019-02-01 stsp if (line[0] == '#' || line[0] == '^')
180 fb79db15 2019-02-01 stsp return NULL;
181 fb79db15 2019-02-01 stsp
182 fb79db15 2019-02-01 stsp if (!got_parse_sha1_digest(digest, line))
183 fb79db15 2019-02-01 stsp return got_error(GOT_ERR_NOT_REF);
184 fb79db15 2019-02-01 stsp
185 199a4027 2019-02-02 stsp if (abs_refname) {
186 199a4027 2019-02-02 stsp if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
187 199a4027 2019-02-02 stsp return NULL;
188 fb79db15 2019-02-01 stsp
189 199a4027 2019-02-02 stsp name = strdup(abs_refname);
190 199a4027 2019-02-02 stsp if (name == NULL)
191 199a4027 2019-02-02 stsp return got_error_from_errno();
192 199a4027 2019-02-02 stsp } else
193 199a4027 2019-02-02 stsp name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
194 fb79db15 2019-02-01 stsp
195 fb79db15 2019-02-01 stsp *ref = calloc(1, sizeof(**ref));
196 fb79db15 2019-02-01 stsp if (*ref == NULL)
197 fb79db15 2019-02-01 stsp return got_error_from_errno();
198 fb79db15 2019-02-01 stsp (*ref)->ref.ref.name = name;;
199 fb79db15 2019-02-01 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
200 fb79db15 2019-02-01 stsp return NULL;
201 fb79db15 2019-02-01 stsp }
202 fb79db15 2019-02-01 stsp
203 fb79db15 2019-02-01 stsp static const struct got_error *
204 0dec1cc0 2019-02-01 stsp open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
205 0dec1cc0 2019-02-01 stsp int nsubdirs, const char *refname)
206 fb79db15 2019-02-01 stsp {
207 fb79db15 2019-02-01 stsp const struct got_error *err = NULL;
208 fb79db15 2019-02-01 stsp char *abs_refname;
209 fb79db15 2019-02-01 stsp char *line;
210 fb79db15 2019-02-01 stsp size_t len;
211 fb79db15 2019-02-01 stsp const char delim[3] = {'\0', '\0', '\0'};
212 0dec1cc0 2019-02-01 stsp int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
213 fb79db15 2019-02-01 stsp
214 0dec1cc0 2019-02-01 stsp if (ref_is_absolute)
215 0dec1cc0 2019-02-01 stsp abs_refname = (char *)refname;
216 fb79db15 2019-02-01 stsp do {
217 fb79db15 2019-02-01 stsp line = fparseln(f, &len, NULL, delim, 0);
218 fb79db15 2019-02-01 stsp if (line == NULL) {
219 fb79db15 2019-02-01 stsp err = got_error(GOT_ERR_NOT_REF);
220 fb79db15 2019-02-01 stsp break;
221 fb79db15 2019-02-01 stsp }
222 0dec1cc0 2019-02-01 stsp for (i = 0; i < nsubdirs; i++) {
223 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute &&
224 0dec1cc0 2019-02-01 stsp asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
225 0dec1cc0 2019-02-01 stsp refname) == -1)
226 0dec1cc0 2019-02-01 stsp return got_error_from_errno();
227 0dec1cc0 2019-02-01 stsp err = parse_packed_ref_line(ref, abs_refname, line);
228 0dec1cc0 2019-02-01 stsp if (!ref_is_absolute)
229 0dec1cc0 2019-02-01 stsp free(abs_refname);
230 532920c8 2019-02-01 stsp if (err || *ref != NULL)
231 0dec1cc0 2019-02-01 stsp break;
232 0dec1cc0 2019-02-01 stsp }
233 fb79db15 2019-02-01 stsp free(line);
234 fb79db15 2019-02-01 stsp if (err)
235 fb79db15 2019-02-01 stsp break;
236 fb79db15 2019-02-01 stsp } while (*ref == NULL);
237 fb79db15 2019-02-01 stsp
238 fb79db15 2019-02-01 stsp return err;
239 fb79db15 2019-02-01 stsp }
240 fb79db15 2019-02-01 stsp
241 fb79db15 2019-02-01 stsp static const struct got_error *
242 d1f2edc9 2018-06-13 stsp open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
243 d1f2edc9 2018-06-13 stsp const char *refname)
244 d1f2edc9 2018-06-13 stsp {
245 d1f2edc9 2018-06-13 stsp const struct got_error *err = NULL;
246 d1f2edc9 2018-06-13 stsp char *path_ref;
247 d1f2edc9 2018-06-13 stsp char *normpath;
248 d1f2edc9 2018-06-13 stsp
249 d1f2edc9 2018-06-13 stsp if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
250 d1f2edc9 2018-06-13 stsp return got_error_from_errno();
251 d1f2edc9 2018-06-13 stsp
252 d1f2edc9 2018-06-13 stsp normpath = got_path_normalize(path_ref);
253 d1f2edc9 2018-06-13 stsp if (normpath == NULL) {
254 d1f2edc9 2018-06-13 stsp err = got_error(GOT_ERR_NOT_REF);
255 d1f2edc9 2018-06-13 stsp goto done;
256 d1f2edc9 2018-06-13 stsp }
257 d1f2edc9 2018-06-13 stsp
258 d1f2edc9 2018-06-13 stsp err = parse_ref_file(ref, refname, normpath);
259 d1f2edc9 2018-06-13 stsp done:
260 d1f2edc9 2018-06-13 stsp free(path_ref);
261 d1f2edc9 2018-06-13 stsp free(normpath);
262 d1f2edc9 2018-06-13 stsp return err;
263 d1f2edc9 2018-06-13 stsp }
264 d1f2edc9 2018-06-13 stsp
265 5261c201 2018-04-01 stsp const struct got_error *
266 5261c201 2018-04-01 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
267 5261c201 2018-04-01 stsp const char *refname)
268 5261c201 2018-04-01 stsp {
269 5261c201 2018-04-01 stsp const struct got_error *err = NULL;
270 c5f754cc 2019-02-01 stsp char *path_refs = NULL;
271 fb79db15 2019-02-01 stsp const char *subdirs[] = {
272 fb79db15 2019-02-01 stsp GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
273 fb79db15 2019-02-01 stsp };
274 c5f754cc 2019-02-01 stsp int i, well_known = is_well_known_ref(refname);
275 5261c201 2018-04-01 stsp
276 c5f754cc 2019-02-01 stsp if (!well_known) {
277 c5f754cc 2019-02-01 stsp char *packed_refs_path;
278 c5f754cc 2019-02-01 stsp FILE *f;
279 c5f754cc 2019-02-01 stsp
280 c5f754cc 2019-02-01 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
281 c5f754cc 2019-02-01 stsp if (packed_refs_path == NULL)
282 c5f754cc 2019-02-01 stsp return got_error_from_errno();
283 c5f754cc 2019-02-01 stsp
284 c5f754cc 2019-02-01 stsp f = fopen(packed_refs_path, "rb");
285 c5f754cc 2019-02-01 stsp free(packed_refs_path);
286 c5f754cc 2019-02-01 stsp if (f != NULL) {
287 0dec1cc0 2019-02-01 stsp err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
288 0dec1cc0 2019-02-01 stsp refname);
289 c5f754cc 2019-02-01 stsp fclose(f);
290 0dec1cc0 2019-02-01 stsp if (err == NULL)
291 0dec1cc0 2019-02-01 stsp goto done;
292 fb79db15 2019-02-01 stsp }
293 fb79db15 2019-02-01 stsp }
294 fb79db15 2019-02-01 stsp
295 fb79db15 2019-02-01 stsp path_refs = get_refs_dir_path(repo, refname);
296 5261c201 2018-04-01 stsp if (path_refs == NULL) {
297 5261c201 2018-04-01 stsp err = got_error_from_errno();
298 5261c201 2018-04-01 stsp goto done;
299 5261c201 2018-04-01 stsp }
300 c5f754cc 2019-02-01 stsp
301 c5f754cc 2019-02-01 stsp if (!well_known) {
302 c5f754cc 2019-02-01 stsp for (i = 0; i < nitems(subdirs); i++) {
303 c5f754cc 2019-02-01 stsp err = open_ref(ref, path_refs, subdirs[i], refname);
304 c5f754cc 2019-02-01 stsp if (err == NULL)
305 c5f754cc 2019-02-01 stsp goto done;
306 c5f754cc 2019-02-01 stsp }
307 fb79db15 2019-02-01 stsp }
308 c5f754cc 2019-02-01 stsp
309 fb79db15 2019-02-01 stsp err = open_ref(ref, path_refs, "", refname);
310 5261c201 2018-04-01 stsp done:
311 5261c201 2018-04-01 stsp free(path_refs);
312 5261c201 2018-04-01 stsp return err;
313 5261c201 2018-04-01 stsp }
314 5261c201 2018-04-01 stsp
315 5261c201 2018-04-01 stsp void
316 5261c201 2018-04-01 stsp got_ref_close(struct got_reference *ref)
317 5261c201 2018-04-01 stsp {
318 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
319 5261c201 2018-04-01 stsp free(ref->ref.symref.name);
320 5261c201 2018-04-01 stsp else
321 5261c201 2018-04-01 stsp free(ref->ref.ref.name);
322 5261c201 2018-04-01 stsp free(ref);
323 5261c201 2018-04-01 stsp }
324 5261c201 2018-04-01 stsp
325 5261c201 2018-04-01 stsp struct got_reference *
326 5261c201 2018-04-01 stsp got_ref_dup(struct got_reference *ref)
327 5261c201 2018-04-01 stsp {
328 5261c201 2018-04-01 stsp struct got_reference *ret;
329 5261c201 2018-04-01 stsp
330 5261c201 2018-04-01 stsp ret = calloc(1, sizeof(*ret));
331 5261c201 2018-04-01 stsp if (ret == NULL)
332 5261c201 2018-04-01 stsp return NULL;
333 5261c201 2018-04-01 stsp
334 5261c201 2018-04-01 stsp ret->flags = ref->flags;
335 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
336 5261c201 2018-04-01 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
337 5261c201 2018-04-01 stsp if (ret->ref.symref.name == NULL) {
338 5261c201 2018-04-01 stsp free(ret);
339 5261c201 2018-04-01 stsp return NULL;
340 5261c201 2018-04-01 stsp }
341 5261c201 2018-04-01 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
342 5261c201 2018-04-01 stsp if (ret->ref.symref.ref == NULL) {
343 5261c201 2018-04-01 stsp free(ret->ref.symref.name);
344 5261c201 2018-04-01 stsp free(ret);
345 5261c201 2018-04-01 stsp return NULL;
346 5261c201 2018-04-01 stsp }
347 5261c201 2018-04-01 stsp } else {
348 5261c201 2018-04-01 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
349 5261c201 2018-04-01 stsp if (ref->ref.ref.name == NULL) {
350 5261c201 2018-04-01 stsp free(ret);
351 5261c201 2018-04-01 stsp return NULL;
352 5261c201 2018-04-01 stsp }
353 5261c201 2018-04-01 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
354 5261c201 2018-04-01 stsp SHA1_DIGEST_LENGTH);
355 5261c201 2018-04-01 stsp }
356 5261c201 2018-04-01 stsp
357 5261c201 2018-04-01 stsp return ret;
358 5261c201 2018-04-01 stsp }
359 5261c201 2018-04-01 stsp
360 5261c201 2018-04-01 stsp static const struct got_error *
361 5261c201 2018-04-01 stsp resolve_symbolic_ref(struct got_reference **resolved,
362 5261c201 2018-04-01 stsp struct got_repository *repo, struct got_reference *ref)
363 5261c201 2018-04-01 stsp {
364 5261c201 2018-04-01 stsp struct got_reference *nextref;
365 5261c201 2018-04-01 stsp const struct got_error *err;
366 5261c201 2018-04-01 stsp
367 5261c201 2018-04-01 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
368 5261c201 2018-04-01 stsp if (err)
369 5261c201 2018-04-01 stsp return err;
370 5261c201 2018-04-01 stsp
371 5261c201 2018-04-01 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
372 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
373 5261c201 2018-04-01 stsp else
374 5261c201 2018-04-01 stsp *resolved = got_ref_dup(nextref);
375 5261c201 2018-04-01 stsp
376 5261c201 2018-04-01 stsp got_ref_close(nextref);
377 5261c201 2018-04-01 stsp return err;
378 5261c201 2018-04-01 stsp }
379 5261c201 2018-04-01 stsp
380 5261c201 2018-04-01 stsp const struct got_error *
381 5261c201 2018-04-01 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
382 5261c201 2018-04-01 stsp struct got_reference *ref)
383 5261c201 2018-04-01 stsp {
384 5261c201 2018-04-01 stsp const struct got_error *err;
385 5261c201 2018-04-01 stsp
386 5261c201 2018-04-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
387 5261c201 2018-04-01 stsp struct got_reference *resolved = NULL;
388 5261c201 2018-04-01 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
389 5261c201 2018-04-01 stsp if (err == NULL)
390 5261c201 2018-04-01 stsp err = got_ref_resolve(id, repo, resolved);
391 5261c201 2018-04-01 stsp free(resolved);
392 5261c201 2018-04-01 stsp return err;
393 5261c201 2018-04-01 stsp }
394 5261c201 2018-04-01 stsp
395 5261c201 2018-04-01 stsp *id = calloc(1, sizeof(**id));
396 5261c201 2018-04-01 stsp if (*id == NULL)
397 5261c201 2018-04-01 stsp return got_error_from_errno();
398 5261c201 2018-04-01 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
399 5261c201 2018-04-01 stsp return NULL;
400 5261c201 2018-04-01 stsp }
401 5261c201 2018-04-01 stsp
402 5261c201 2018-04-01 stsp char *
403 5261c201 2018-04-01 stsp got_ref_to_str(struct got_reference *ref)
404 5261c201 2018-04-01 stsp {
405 5261c201 2018-04-01 stsp char *str;
406 271d2a38 2018-12-25 stsp
407 271d2a38 2018-12-25 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
408 271d2a38 2018-12-25 stsp return strdup(ref->ref.symref.ref);
409 271d2a38 2018-12-25 stsp
410 271d2a38 2018-12-25 stsp str = malloc(SHA1_DIGEST_STRING_LENGTH);
411 271d2a38 2018-12-25 stsp if (str == NULL)
412 271d2a38 2018-12-25 stsp return NULL;
413 271d2a38 2018-12-25 stsp
414 271d2a38 2018-12-25 stsp if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
415 271d2a38 2018-12-25 stsp SHA1_DIGEST_STRING_LENGTH) == NULL) {
416 271d2a38 2018-12-25 stsp free(str);
417 271d2a38 2018-12-25 stsp return NULL;
418 5261c201 2018-04-01 stsp }
419 5261c201 2018-04-01 stsp
420 5261c201 2018-04-01 stsp return str;
421 5261c201 2018-04-01 stsp }
422 0bd18d37 2019-02-01 stsp
423 0bd18d37 2019-02-01 stsp const char *
424 0bd18d37 2019-02-01 stsp got_ref_get_name(struct got_reference *ref)
425 0bd18d37 2019-02-01 stsp {
426 0bd18d37 2019-02-01 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
427 0bd18d37 2019-02-01 stsp return ref->ref.symref.name;
428 0bd18d37 2019-02-01 stsp
429 0bd18d37 2019-02-01 stsp return ref->ref.ref.name;
430 199a4027 2019-02-02 stsp }
431 199a4027 2019-02-02 stsp
432 199a4027 2019-02-02 stsp static const struct got_error *
433 199a4027 2019-02-02 stsp append_ref(struct got_reflist_head *refs, struct got_reference *ref,
434 199a4027 2019-02-02 stsp struct got_repository *repo)
435 199a4027 2019-02-02 stsp {
436 199a4027 2019-02-02 stsp const struct got_error *err;
437 199a4027 2019-02-02 stsp struct got_object_id *id;
438 199a4027 2019-02-02 stsp struct got_reflist_entry *entry;
439 199a4027 2019-02-02 stsp
440 199a4027 2019-02-02 stsp err = got_ref_resolve(&id, repo, ref);
441 199a4027 2019-02-02 stsp if (err)
442 199a4027 2019-02-02 stsp return err;
443 199a4027 2019-02-02 stsp entry = malloc(sizeof(*entry));
444 199a4027 2019-02-02 stsp if (entry == NULL)
445 199a4027 2019-02-02 stsp return got_error_from_errno();
446 199a4027 2019-02-02 stsp entry->ref = ref;
447 199a4027 2019-02-02 stsp entry->id = id;
448 199a4027 2019-02-02 stsp SIMPLEQ_INSERT_TAIL(refs, entry, entry);
449 199a4027 2019-02-02 stsp return NULL;
450 0bd18d37 2019-02-01 stsp }
451 199a4027 2019-02-02 stsp
452 199a4027 2019-02-02 stsp const struct got_error *
453 199a4027 2019-02-02 stsp got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
454 199a4027 2019-02-02 stsp {
455 199a4027 2019-02-02 stsp const struct got_error *err;
456 199a4027 2019-02-02 stsp char *packed_refs_path, *path_refs;
457 199a4027 2019-02-02 stsp FILE *f;
458 199a4027 2019-02-02 stsp struct got_reference *ref;
459 199a4027 2019-02-02 stsp
460 199a4027 2019-02-02 stsp packed_refs_path = got_repo_get_path_packed_refs(repo);
461 199a4027 2019-02-02 stsp if (packed_refs_path == NULL)
462 199a4027 2019-02-02 stsp return got_error_from_errno();
463 199a4027 2019-02-02 stsp
464 199a4027 2019-02-02 stsp f = fopen(packed_refs_path, "r");
465 199a4027 2019-02-02 stsp free(packed_refs_path);
466 199a4027 2019-02-02 stsp if (f) {
467 199a4027 2019-02-02 stsp char *line;
468 199a4027 2019-02-02 stsp size_t len;
469 199a4027 2019-02-02 stsp const char delim[3] = {'\0', '\0', '\0'};
470 199a4027 2019-02-02 stsp while (1) {
471 199a4027 2019-02-02 stsp line = fparseln(f, &len, NULL, delim, 0);
472 199a4027 2019-02-02 stsp if (line == NULL)
473 199a4027 2019-02-02 stsp break;
474 199a4027 2019-02-02 stsp err = parse_packed_ref_line(&ref, NULL, line);
475 199a4027 2019-02-02 stsp if (err)
476 199a4027 2019-02-02 stsp goto done;
477 199a4027 2019-02-02 stsp if (ref)
478 199a4027 2019-02-02 stsp append_ref(refs, ref, repo);
479 199a4027 2019-02-02 stsp }
480 199a4027 2019-02-02 stsp }
481 199a4027 2019-02-02 stsp
482 199a4027 2019-02-02 stsp /* HEAD ref should always exist. */
483 199a4027 2019-02-02 stsp path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
484 199a4027 2019-02-02 stsp if (path_refs == NULL) {
485 199a4027 2019-02-02 stsp err = got_error_from_errno();
486 199a4027 2019-02-02 stsp goto done;
487 199a4027 2019-02-02 stsp }
488 199a4027 2019-02-02 stsp err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
489 199a4027 2019-02-02 stsp free(path_refs);
490 199a4027 2019-02-02 stsp if (err)
491 199a4027 2019-02-02 stsp goto done;
492 199a4027 2019-02-02 stsp append_ref(refs, ref, repo);
493 199a4027 2019-02-02 stsp
494 199a4027 2019-02-02 stsp done:
495 199a4027 2019-02-02 stsp if (f)
496 199a4027 2019-02-02 stsp fclose(f);
497 199a4027 2019-02-02 stsp return err;
498 199a4027 2019-02-02 stsp }