Blob


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