Blame


1 7b19e0f1 2017-11-05 stsp /*
2 8eea5a5c 2018-03-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 4027f31a 2017-11-04 stsp #include <sys/types.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
19 d1cda826 2017-11-06 stsp
20 4027f31a 2017-11-04 stsp #include <sha1.h>
21 4027f31a 2017-11-04 stsp #include <stdio.h>
22 4027f31a 2017-11-04 stsp #include <stdlib.h>
23 4027f31a 2017-11-04 stsp #include <string.h>
24 4027f31a 2017-11-04 stsp #include <util.h>
25 68482ea3 2017-11-27 stsp #include <zlib.h>
26 4027f31a 2017-11-04 stsp
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 11995603 2017-11-05 stsp #include "got_object.h"
29 11995603 2017-11-05 stsp #include "got_repository.h"
30 4027f31a 2017-11-04 stsp #include "got_refs.h"
31 4027f31a 2017-11-04 stsp
32 32cb896c 2018-03-11 stsp #include "got_sha1_lib.h"
33 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
34 32cb896c 2018-03-11 stsp #include "got_delta_lib.h"
35 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
36 32cb896c 2018-03-11 stsp #include "got_object_lib.h"
37 38d42f42 2018-02-12 stsp
38 38d42f42 2018-02-12 stsp /* A symbolic reference. */
39 38d42f42 2018-02-12 stsp struct got_symref {
40 38d42f42 2018-02-12 stsp char *name;
41 38d42f42 2018-02-12 stsp char *ref;
42 38d42f42 2018-02-12 stsp };
43 38d42f42 2018-02-12 stsp
44 38d42f42 2018-02-12 stsp /* A non-symbolic reference (there is no better designation). */
45 38d42f42 2018-02-12 stsp struct got_ref {
46 38d42f42 2018-02-12 stsp char *name;
47 38d42f42 2018-02-12 stsp u_int8_t sha1[SHA1_DIGEST_LENGTH];
48 38d42f42 2018-02-12 stsp };
49 c3f94f68 2017-11-05 stsp
50 38d42f42 2018-02-12 stsp /* A reference which points to an arbitrary object. */
51 38d42f42 2018-02-12 stsp struct got_reference {
52 38d42f42 2018-02-12 stsp unsigned int flags;
53 38d42f42 2018-02-12 stsp #define GOT_REF_IS_SYMBOLIC 0x01
54 38d42f42 2018-02-12 stsp
55 38d42f42 2018-02-12 stsp union {
56 38d42f42 2018-02-12 stsp struct got_ref ref;
57 38d42f42 2018-02-12 stsp struct got_symref symref;
58 38d42f42 2018-02-12 stsp } ref;
59 38d42f42 2018-02-12 stsp };
60 38d42f42 2018-02-12 stsp
61 4027f31a 2017-11-04 stsp static const struct got_error *
62 4027f31a 2017-11-04 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
63 4027f31a 2017-11-04 stsp {
64 4027f31a 2017-11-04 stsp struct got_symref *symref;
65 4027f31a 2017-11-04 stsp char *symref_name;
66 4027f31a 2017-11-04 stsp char *symref_ref;
67 4027f31a 2017-11-04 stsp
68 4027f31a 2017-11-04 stsp if (line[0] == '\0')
69 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
70 4027f31a 2017-11-04 stsp
71 4027f31a 2017-11-04 stsp symref_name = strdup(name);
72 4027f31a 2017-11-04 stsp if (symref_name == NULL)
73 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
74 4027f31a 2017-11-04 stsp symref_ref = strdup(line);
75 4027f31a 2017-11-04 stsp if (symref_ref == NULL) {
76 4027f31a 2017-11-04 stsp free(symref_name);
77 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
78 4027f31a 2017-11-04 stsp }
79 4027f31a 2017-11-04 stsp
80 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
81 1db76ab5 2018-01-26 mpi if (*ref == NULL)
82 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
83 4027f31a 2017-11-04 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
84 4027f31a 2017-11-04 stsp symref = &((*ref)->ref.symref);
85 4027f31a 2017-11-04 stsp symref->name = symref_name;
86 4027f31a 2017-11-04 stsp symref->ref = symref_ref;
87 4027f31a 2017-11-04 stsp return NULL;
88 4027f31a 2017-11-04 stsp }
89 4027f31a 2017-11-04 stsp
90 4027f31a 2017-11-04 stsp static const struct got_error *
91 4027f31a 2017-11-04 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
92 4027f31a 2017-11-04 stsp {
93 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
94 4027f31a 2017-11-04 stsp char *ref_name;
95 4027f31a 2017-11-04 stsp
96 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
97 4027f31a 2017-11-04 stsp line += 5;
98 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
99 4027f31a 2017-11-04 stsp }
100 4027f31a 2017-11-04 stsp
101 4027f31a 2017-11-04 stsp ref_name = strdup(name);
102 4027f31a 2017-11-04 stsp if (ref_name == NULL)
103 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
104 4027f31a 2017-11-04 stsp
105 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(digest, line))
106 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
107 4027f31a 2017-11-04 stsp
108 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
109 1db76ab5 2018-01-26 mpi if (*ref == NULL)
110 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
111 4027f31a 2017-11-04 stsp (*ref)->ref.ref.name = ref_name;
112 4027f31a 2017-11-04 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
113 4027f31a 2017-11-04 stsp return NULL;
114 4027f31a 2017-11-04 stsp }
115 4027f31a 2017-11-04 stsp
116 4027f31a 2017-11-04 stsp static const struct got_error *
117 4027f31a 2017-11-04 stsp parse_ref_file(struct got_reference **ref, const char *name,
118 4027f31a 2017-11-04 stsp const char *abspath)
119 4027f31a 2017-11-04 stsp {
120 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
121 4027f31a 2017-11-04 stsp FILE *f = fopen(abspath, "rb");
122 4027f31a 2017-11-04 stsp char *line;
123 4027f31a 2017-11-04 stsp size_t len;
124 4027f31a 2017-11-04 stsp const char delim[3] = {'\0', '\0', '\0'};
125 4027f31a 2017-11-04 stsp
126 4027f31a 2017-11-04 stsp if (f == NULL)
127 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
128 4027f31a 2017-11-04 stsp
129 4027f31a 2017-11-04 stsp line = fparseln(f, &len, NULL, delim, 0);
130 4027f31a 2017-11-04 stsp if (line == NULL) {
131 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
132 4027f31a 2017-11-04 stsp goto done;
133 4027f31a 2017-11-04 stsp }
134 4027f31a 2017-11-04 stsp
135 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
136 4027f31a 2017-11-04 stsp done:
137 4027f31a 2017-11-04 stsp free(line);
138 4027f31a 2017-11-04 stsp fclose(f);
139 4027f31a 2017-11-04 stsp return err;
140 4027f31a 2017-11-04 stsp }
141 4027f31a 2017-11-04 stsp
142 11995603 2017-11-05 stsp static char *
143 11995603 2017-11-05 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
144 11995603 2017-11-05 stsp {
145 11995603 2017-11-05 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
146 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
147 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
148 d4a5a885 2017-11-05 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
149 d4a5a885 2017-11-05 stsp strncmp(refname, "refs/", 5) == 0)
150 11995603 2017-11-05 stsp return got_repo_get_path_git_dir(repo);
151 11995603 2017-11-05 stsp
152 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
153 11995603 2017-11-05 stsp }
154 11995603 2017-11-05 stsp
155 4027f31a 2017-11-04 stsp const struct got_error *
156 11995603 2017-11-05 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
157 4027f31a 2017-11-04 stsp const char *refname)
158 4027f31a 2017-11-04 stsp {
159 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
160 4027f31a 2017-11-04 stsp char *path_ref = NULL;
161 4027f31a 2017-11-04 stsp char *normpath = NULL;
162 4027f31a 2017-11-04 stsp const char *parent_dir;
163 11995603 2017-11-05 stsp char *path_refs = get_refs_dir_path(repo, refname);
164 11995603 2017-11-05 stsp
165 11995603 2017-11-05 stsp if (path_refs == NULL) {
166 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
167 11995603 2017-11-05 stsp goto done;
168 11995603 2017-11-05 stsp }
169 4027f31a 2017-11-04 stsp
170 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
171 4027f31a 2017-11-04 stsp
172 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
173 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
174 4027f31a 2017-11-04 stsp goto done;
175 4027f31a 2017-11-04 stsp }
176 4027f31a 2017-11-04 stsp
177 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
178 4027f31a 2017-11-04 stsp if (normpath == NULL) {
179 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
180 4027f31a 2017-11-04 stsp goto done;
181 4027f31a 2017-11-04 stsp }
182 4027f31a 2017-11-04 stsp
183 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
184 4027f31a 2017-11-04 stsp done:
185 4027f31a 2017-11-04 stsp free(normpath);
186 4027f31a 2017-11-04 stsp free(path_ref);
187 11995603 2017-11-05 stsp free(path_refs);
188 4027f31a 2017-11-04 stsp return err;
189 4027f31a 2017-11-04 stsp }
190 4027f31a 2017-11-04 stsp
191 4027f31a 2017-11-04 stsp void
192 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
193 4027f31a 2017-11-04 stsp {
194 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
195 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
196 4027f31a 2017-11-04 stsp else
197 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
198 4027f31a 2017-11-04 stsp free(ref);
199 4027f31a 2017-11-04 stsp }
200 11995603 2017-11-05 stsp
201 11995603 2017-11-05 stsp struct got_reference *
202 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
203 11995603 2017-11-05 stsp {
204 1db76ab5 2018-01-26 mpi struct got_reference *ret;
205 11995603 2017-11-05 stsp char *name = NULL;
206 11995603 2017-11-05 stsp char *symref = NULL;
207 11995603 2017-11-05 stsp
208 1db76ab5 2018-01-26 mpi ret = calloc(1, sizeof(*ret));
209 11995603 2017-11-05 stsp if (ret == NULL)
210 11995603 2017-11-05 stsp return NULL;
211 11995603 2017-11-05 stsp
212 11995603 2017-11-05 stsp ret->flags = ref->flags;
213 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
214 11995603 2017-11-05 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
215 11995603 2017-11-05 stsp if (ret->ref.symref.name == NULL) {
216 11995603 2017-11-05 stsp free(ret);
217 11995603 2017-11-05 stsp return NULL;
218 11995603 2017-11-05 stsp }
219 11995603 2017-11-05 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
220 11995603 2017-11-05 stsp if (ret->ref.symref.ref == NULL) {
221 11995603 2017-11-05 stsp free(ret->ref.symref.name);
222 11995603 2017-11-05 stsp free(ret);
223 11995603 2017-11-05 stsp return NULL;
224 11995603 2017-11-05 stsp }
225 11995603 2017-11-05 stsp } else {
226 11995603 2017-11-05 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
227 11995603 2017-11-05 stsp if (ref->ref.ref.name == NULL) {
228 11995603 2017-11-05 stsp free(ret);
229 11995603 2017-11-05 stsp return NULL;
230 11995603 2017-11-05 stsp }
231 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
232 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
233 11995603 2017-11-05 stsp }
234 11995603 2017-11-05 stsp
235 11995603 2017-11-05 stsp return ret;
236 11995603 2017-11-05 stsp }
237 11995603 2017-11-05 stsp
238 11995603 2017-11-05 stsp static const struct got_error *
239 11995603 2017-11-05 stsp resolve_symbolic_ref(struct got_reference **resolved,
240 11995603 2017-11-05 stsp struct got_repository *repo, struct got_reference *ref)
241 11995603 2017-11-05 stsp {
242 11995603 2017-11-05 stsp struct got_reference *nextref;
243 11995603 2017-11-05 stsp const struct got_error *err;
244 11995603 2017-11-05 stsp
245 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
246 11995603 2017-11-05 stsp if (err)
247 11995603 2017-11-05 stsp return err;
248 11995603 2017-11-05 stsp
249 11995603 2017-11-05 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
250 11995603 2017-11-05 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
251 11995603 2017-11-05 stsp else
252 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
253 11995603 2017-11-05 stsp
254 11995603 2017-11-05 stsp got_ref_close(nextref);
255 11995603 2017-11-05 stsp return err;
256 11995603 2017-11-05 stsp }
257 11995603 2017-11-05 stsp
258 11995603 2017-11-05 stsp const struct got_error *
259 11995603 2017-11-05 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
260 11995603 2017-11-05 stsp struct got_reference *ref)
261 11995603 2017-11-05 stsp {
262 11995603 2017-11-05 stsp const struct got_error *err;
263 11995603 2017-11-05 stsp
264 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
265 11995603 2017-11-05 stsp struct got_reference *resolved = NULL;
266 11995603 2017-11-05 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
267 11995603 2017-11-05 stsp if (err == NULL)
268 11995603 2017-11-05 stsp err = got_ref_resolve(id, repo, resolved);
269 11995603 2017-11-05 stsp free(resolved);
270 11995603 2017-11-05 stsp return err;
271 11995603 2017-11-05 stsp }
272 11995603 2017-11-05 stsp
273 11995603 2017-11-05 stsp *id = calloc(1, sizeof(**id));
274 11995603 2017-11-05 stsp if (*id == NULL)
275 11995603 2017-11-05 stsp return got_error(GOT_ERR_NO_MEM);
276 11995603 2017-11-05 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
277 11995603 2017-11-05 stsp return NULL;
278 86c3caaf 2018-03-09 stsp }
279 86c3caaf 2018-03-09 stsp
280 86c3caaf 2018-03-09 stsp char *
281 86c3caaf 2018-03-09 stsp got_ref_to_str(struct got_reference *ref)
282 86c3caaf 2018-03-09 stsp {
283 86c3caaf 2018-03-09 stsp char *str;
284 86c3caaf 2018-03-09 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
285 86c3caaf 2018-03-09 stsp if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
286 86c3caaf 2018-03-09 stsp return NULL;
287 86c3caaf 2018-03-09 stsp } else {
288 86c3caaf 2018-03-09 stsp str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
289 86c3caaf 2018-03-09 stsp if (str == NULL)
290 86c3caaf 2018-03-09 stsp return NULL;
291 86c3caaf 2018-03-09 stsp str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
292 86c3caaf 2018-03-09 stsp SHA1_DIGEST_STRING_LENGTH);
293 86c3caaf 2018-03-09 stsp }
294 86c3caaf 2018-03-09 stsp
295 86c3caaf 2018-03-09 stsp return str;
296 11995603 2017-11-05 stsp }