Blame


1 7b19e0f1 2017-11-05 stsp /*
2 7b19e0f1 2017-11-05 stsp * Copyright (c) 2017 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 d1cda826 2017-11-06 stsp #include "got_sha1.h"
32 4027f31a 2017-11-04 stsp
33 c3f94f68 2017-11-05 stsp #include "path.h"
34 59ece79d 2018-02-12 stsp #include "delta.h"
35 59ece79d 2018-02-12 stsp #include "zb.h"
36 59ece79d 2018-02-12 stsp #include "object.h"
37 c3f94f68 2017-11-05 stsp
38 4027f31a 2017-11-04 stsp static const struct got_error *
39 4027f31a 2017-11-04 stsp parse_symref(struct got_reference **ref, const char *name, const char *line)
40 4027f31a 2017-11-04 stsp {
41 4027f31a 2017-11-04 stsp struct got_symref *symref;
42 4027f31a 2017-11-04 stsp char *symref_name;
43 4027f31a 2017-11-04 stsp char *symref_ref;
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp if (line[0] == '\0')
46 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
47 4027f31a 2017-11-04 stsp
48 4027f31a 2017-11-04 stsp symref_name = strdup(name);
49 4027f31a 2017-11-04 stsp if (symref_name == NULL)
50 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
51 4027f31a 2017-11-04 stsp symref_ref = strdup(line);
52 4027f31a 2017-11-04 stsp if (symref_ref == NULL) {
53 4027f31a 2017-11-04 stsp free(symref_name);
54 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
55 4027f31a 2017-11-04 stsp }
56 4027f31a 2017-11-04 stsp
57 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
58 1db76ab5 2018-01-26 mpi if (*ref == NULL)
59 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
60 4027f31a 2017-11-04 stsp (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
61 4027f31a 2017-11-04 stsp symref = &((*ref)->ref.symref);
62 4027f31a 2017-11-04 stsp symref->name = symref_name;
63 4027f31a 2017-11-04 stsp symref->ref = symref_ref;
64 4027f31a 2017-11-04 stsp return NULL;
65 4027f31a 2017-11-04 stsp }
66 4027f31a 2017-11-04 stsp
67 4027f31a 2017-11-04 stsp static const struct got_error *
68 4027f31a 2017-11-04 stsp parse_ref_line(struct got_reference **ref, const char *name, const char *line)
69 4027f31a 2017-11-04 stsp {
70 4027f31a 2017-11-04 stsp uint8_t digest[SHA1_DIGEST_LENGTH];
71 4027f31a 2017-11-04 stsp char *ref_name;
72 4027f31a 2017-11-04 stsp
73 4027f31a 2017-11-04 stsp if (strncmp(line, "ref: ", 5) == 0) {
74 4027f31a 2017-11-04 stsp line += 5;
75 4027f31a 2017-11-04 stsp return parse_symref(ref, name, line);
76 4027f31a 2017-11-04 stsp }
77 4027f31a 2017-11-04 stsp
78 4027f31a 2017-11-04 stsp ref_name = strdup(name);
79 4027f31a 2017-11-04 stsp if (ref_name == NULL)
80 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NO_MEM);
81 4027f31a 2017-11-04 stsp
82 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(digest, line))
83 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
84 4027f31a 2017-11-04 stsp
85 4027f31a 2017-11-04 stsp *ref = calloc(1, sizeof(**ref));
86 1db76ab5 2018-01-26 mpi if (*ref == NULL)
87 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
88 4027f31a 2017-11-04 stsp (*ref)->ref.ref.name = ref_name;
89 4027f31a 2017-11-04 stsp memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
90 4027f31a 2017-11-04 stsp return NULL;
91 4027f31a 2017-11-04 stsp }
92 4027f31a 2017-11-04 stsp
93 4027f31a 2017-11-04 stsp static const struct got_error *
94 4027f31a 2017-11-04 stsp parse_ref_file(struct got_reference **ref, const char *name,
95 4027f31a 2017-11-04 stsp const char *abspath)
96 4027f31a 2017-11-04 stsp {
97 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
98 4027f31a 2017-11-04 stsp FILE *f = fopen(abspath, "rb");
99 4027f31a 2017-11-04 stsp char *line;
100 4027f31a 2017-11-04 stsp size_t len;
101 4027f31a 2017-11-04 stsp const char delim[3] = {'\0', '\0', '\0'};
102 4027f31a 2017-11-04 stsp
103 4027f31a 2017-11-04 stsp if (f == NULL)
104 4027f31a 2017-11-04 stsp return got_error(GOT_ERR_NOT_REF);
105 4027f31a 2017-11-04 stsp
106 4027f31a 2017-11-04 stsp line = fparseln(f, &len, NULL, delim, 0);
107 4027f31a 2017-11-04 stsp if (line == NULL) {
108 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
109 4027f31a 2017-11-04 stsp goto done;
110 4027f31a 2017-11-04 stsp }
111 4027f31a 2017-11-04 stsp
112 4027f31a 2017-11-04 stsp err = parse_ref_line(ref, name, line);
113 4027f31a 2017-11-04 stsp done:
114 4027f31a 2017-11-04 stsp free(line);
115 4027f31a 2017-11-04 stsp fclose(f);
116 4027f31a 2017-11-04 stsp return err;
117 4027f31a 2017-11-04 stsp }
118 4027f31a 2017-11-04 stsp
119 11995603 2017-11-05 stsp static char *
120 11995603 2017-11-05 stsp get_refs_dir_path(struct got_repository *repo, const char *refname)
121 11995603 2017-11-05 stsp {
122 11995603 2017-11-05 stsp if (strcmp(refname, GOT_REF_HEAD) == 0 ||
123 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
124 11995603 2017-11-05 stsp strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
125 d4a5a885 2017-11-05 stsp strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
126 d4a5a885 2017-11-05 stsp strncmp(refname, "refs/", 5) == 0)
127 11995603 2017-11-05 stsp return got_repo_get_path_git_dir(repo);
128 11995603 2017-11-05 stsp
129 11995603 2017-11-05 stsp return got_repo_get_path_refs(repo);
130 11995603 2017-11-05 stsp }
131 11995603 2017-11-05 stsp
132 4027f31a 2017-11-04 stsp const struct got_error *
133 11995603 2017-11-05 stsp got_ref_open(struct got_reference **ref, struct got_repository *repo,
134 4027f31a 2017-11-04 stsp const char *refname)
135 4027f31a 2017-11-04 stsp {
136 4027f31a 2017-11-04 stsp const struct got_error *err = NULL;
137 4027f31a 2017-11-04 stsp char *path_ref = NULL;
138 4027f31a 2017-11-04 stsp char *normpath = NULL;
139 4027f31a 2017-11-04 stsp const char *parent_dir;
140 11995603 2017-11-05 stsp char *path_refs = get_refs_dir_path(repo, refname);
141 11995603 2017-11-05 stsp
142 11995603 2017-11-05 stsp if (path_refs == NULL) {
143 11995603 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
144 11995603 2017-11-05 stsp goto done;
145 11995603 2017-11-05 stsp }
146 4027f31a 2017-11-04 stsp
147 4027f31a 2017-11-04 stsp /* XXX For now, this assumes that refs exist in the filesystem. */
148 4027f31a 2017-11-04 stsp
149 4027f31a 2017-11-04 stsp if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
150 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NO_MEM);
151 4027f31a 2017-11-04 stsp goto done;
152 4027f31a 2017-11-04 stsp }
153 4027f31a 2017-11-04 stsp
154 4027f31a 2017-11-04 stsp normpath = got_path_normalize(path_ref);
155 4027f31a 2017-11-04 stsp if (normpath == NULL) {
156 4027f31a 2017-11-04 stsp err = got_error(GOT_ERR_NOT_REF);
157 4027f31a 2017-11-04 stsp goto done;
158 4027f31a 2017-11-04 stsp }
159 4027f31a 2017-11-04 stsp
160 11995603 2017-11-05 stsp err = parse_ref_file(ref, refname, normpath);
161 4027f31a 2017-11-04 stsp done:
162 4027f31a 2017-11-04 stsp free(normpath);
163 4027f31a 2017-11-04 stsp free(path_ref);
164 11995603 2017-11-05 stsp free(path_refs);
165 4027f31a 2017-11-04 stsp return err;
166 4027f31a 2017-11-04 stsp }
167 4027f31a 2017-11-04 stsp
168 4027f31a 2017-11-04 stsp void
169 4027f31a 2017-11-04 stsp got_ref_close(struct got_reference *ref)
170 4027f31a 2017-11-04 stsp {
171 4027f31a 2017-11-04 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC)
172 4027f31a 2017-11-04 stsp free(ref->ref.symref.name);
173 4027f31a 2017-11-04 stsp else
174 4027f31a 2017-11-04 stsp free(ref->ref.ref.name);
175 4027f31a 2017-11-04 stsp free(ref);
176 4027f31a 2017-11-04 stsp }
177 11995603 2017-11-05 stsp
178 11995603 2017-11-05 stsp struct got_reference *
179 11995603 2017-11-05 stsp got_ref_dup(struct got_reference *ref)
180 11995603 2017-11-05 stsp {
181 1db76ab5 2018-01-26 mpi struct got_reference *ret;
182 11995603 2017-11-05 stsp char *name = NULL;
183 11995603 2017-11-05 stsp char *symref = NULL;
184 11995603 2017-11-05 stsp
185 1db76ab5 2018-01-26 mpi ret = calloc(1, sizeof(*ret));
186 11995603 2017-11-05 stsp if (ret == NULL)
187 11995603 2017-11-05 stsp return NULL;
188 11995603 2017-11-05 stsp
189 11995603 2017-11-05 stsp ret->flags = ref->flags;
190 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
191 11995603 2017-11-05 stsp ret->ref.symref.name = strdup(ref->ref.symref.name);
192 11995603 2017-11-05 stsp if (ret->ref.symref.name == NULL) {
193 11995603 2017-11-05 stsp free(ret);
194 11995603 2017-11-05 stsp return NULL;
195 11995603 2017-11-05 stsp }
196 11995603 2017-11-05 stsp ret->ref.symref.ref = strdup(ref->ref.symref.ref);
197 11995603 2017-11-05 stsp if (ret->ref.symref.ref == NULL) {
198 11995603 2017-11-05 stsp free(ret->ref.symref.name);
199 11995603 2017-11-05 stsp free(ret);
200 11995603 2017-11-05 stsp return NULL;
201 11995603 2017-11-05 stsp }
202 11995603 2017-11-05 stsp } else {
203 11995603 2017-11-05 stsp ref->ref.ref.name = strdup(ref->ref.ref.name);
204 11995603 2017-11-05 stsp if (ref->ref.ref.name == NULL) {
205 11995603 2017-11-05 stsp free(ret);
206 11995603 2017-11-05 stsp return NULL;
207 11995603 2017-11-05 stsp }
208 11995603 2017-11-05 stsp memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
209 11995603 2017-11-05 stsp SHA1_DIGEST_LENGTH);
210 11995603 2017-11-05 stsp }
211 11995603 2017-11-05 stsp
212 11995603 2017-11-05 stsp return ret;
213 11995603 2017-11-05 stsp }
214 11995603 2017-11-05 stsp
215 11995603 2017-11-05 stsp static const struct got_error *
216 11995603 2017-11-05 stsp resolve_symbolic_ref(struct got_reference **resolved,
217 11995603 2017-11-05 stsp struct got_repository *repo, struct got_reference *ref)
218 11995603 2017-11-05 stsp {
219 11995603 2017-11-05 stsp struct got_reference *nextref;
220 11995603 2017-11-05 stsp const struct got_error *err;
221 11995603 2017-11-05 stsp
222 11995603 2017-11-05 stsp err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
223 11995603 2017-11-05 stsp if (err)
224 11995603 2017-11-05 stsp return err;
225 11995603 2017-11-05 stsp
226 11995603 2017-11-05 stsp if (nextref->flags & GOT_REF_IS_SYMBOLIC)
227 11995603 2017-11-05 stsp err = resolve_symbolic_ref(resolved, repo, nextref);
228 11995603 2017-11-05 stsp else
229 11995603 2017-11-05 stsp *resolved = got_ref_dup(nextref);
230 11995603 2017-11-05 stsp
231 11995603 2017-11-05 stsp got_ref_close(nextref);
232 11995603 2017-11-05 stsp return err;
233 11995603 2017-11-05 stsp }
234 11995603 2017-11-05 stsp
235 11995603 2017-11-05 stsp const struct got_error *
236 11995603 2017-11-05 stsp got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
237 11995603 2017-11-05 stsp struct got_reference *ref)
238 11995603 2017-11-05 stsp {
239 11995603 2017-11-05 stsp const struct got_error *err;
240 11995603 2017-11-05 stsp
241 11995603 2017-11-05 stsp if (ref->flags & GOT_REF_IS_SYMBOLIC) {
242 11995603 2017-11-05 stsp struct got_reference *resolved = NULL;
243 11995603 2017-11-05 stsp err = resolve_symbolic_ref(&resolved, repo, ref);
244 11995603 2017-11-05 stsp if (err == NULL)
245 11995603 2017-11-05 stsp err = got_ref_resolve(id, repo, resolved);
246 11995603 2017-11-05 stsp free(resolved);
247 11995603 2017-11-05 stsp return err;
248 11995603 2017-11-05 stsp }
249 11995603 2017-11-05 stsp
250 11995603 2017-11-05 stsp *id = calloc(1, sizeof(**id));
251 11995603 2017-11-05 stsp if (*id == NULL)
252 11995603 2017-11-05 stsp return got_error(GOT_ERR_NO_MEM);
253 11995603 2017-11-05 stsp memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
254 11995603 2017-11-05 stsp return NULL;
255 11995603 2017-11-05 stsp }