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_refs.h"
32 #include "got_sha1_lib.h"
33 #include "got_path_lib.h"
34 #include "got_delta_lib.h"
35 #include "got_zbuf_lib.h"
36 #include "got_object_lib.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(GOT_ERR_NO_MEM);
74 symref_ref = strdup(line);
75 if (symref_ref == NULL) {
76 free(symref_name);
77 return got_error(GOT_ERR_NO_MEM);
78 }
80 *ref = calloc(1, sizeof(**ref));
81 if (*ref == NULL)
82 return got_error(GOT_ERR_NO_MEM);
83 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
84 symref = &((*ref)->ref.symref);
85 symref->name = symref_name;
86 symref->ref = symref_ref;
87 return NULL;
88 }
90 static const struct got_error *
91 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
92 {
93 uint8_t digest[SHA1_DIGEST_LENGTH];
94 char *ref_name;
96 if (strncmp(line, "ref: ", 5) == 0) {
97 line += 5;
98 return parse_symref(ref, name, line);
99 }
101 ref_name = strdup(name);
102 if (ref_name == NULL)
103 return got_error(GOT_ERR_NO_MEM);
105 if (!got_parse_sha1_digest(digest, line))
106 return got_error(GOT_ERR_NOT_REF);
108 *ref = calloc(1, sizeof(**ref));
109 if (*ref == NULL)
110 return got_error(GOT_ERR_NO_MEM);
111 (*ref)->ref.ref.name = ref_name;
112 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
113 return NULL;
116 static const struct got_error *
117 parse_ref_file(struct got_reference **ref, const char *name,
118 const char *abspath)
120 const struct got_error *err = NULL;
121 FILE *f = fopen(abspath, "rb");
122 char *line;
123 size_t len;
124 const char delim[3] = {'\0', '\0', '\0'};
126 if (f == NULL)
127 return got_error(GOT_ERR_NOT_REF);
129 line = fparseln(f, &len, NULL, delim, 0);
130 if (line == NULL) {
131 err = got_error(GOT_ERR_NOT_REF);
132 goto done;
135 err = parse_ref_line(ref, name, line);
136 done:
137 free(line);
138 fclose(f);
139 return err;
142 static char *
143 get_refs_dir_path(struct got_repository *repo, const char *refname)
145 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
146 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
147 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
148 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
149 strncmp(refname, "refs/", 5) == 0)
150 return got_repo_get_path_git_dir(repo);
152 return got_repo_get_path_refs(repo);
155 const struct got_error *
156 got_ref_open(struct got_reference **ref, struct got_repository *repo,
157 const char *refname)
159 const struct got_error *err = NULL;
160 char *path_ref = NULL;
161 char *normpath = NULL;
162 char *path_refs = get_refs_dir_path(repo, refname);
164 if (path_refs == NULL) {
165 err = got_error(GOT_ERR_NO_MEM);
166 goto done;
169 /* XXX For now, this assumes that refs exist in the filesystem. */
171 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
172 err = got_error(GOT_ERR_NO_MEM);
173 goto done;
176 normpath = got_path_normalize(path_ref);
177 if (normpath == NULL) {
178 err = got_error(GOT_ERR_NOT_REF);
179 goto done;
182 err = parse_ref_file(ref, refname, normpath);
183 done:
184 free(normpath);
185 free(path_ref);
186 free(path_refs);
187 return err;
190 void
191 got_ref_close(struct got_reference *ref)
193 if (ref->flags & GOT_REF_IS_SYMBOLIC)
194 free(ref->ref.symref.name);
195 else
196 free(ref->ref.ref.name);
197 free(ref);
200 struct got_reference *
201 got_ref_dup(struct got_reference *ref)
203 struct got_reference *ret;
205 ret = calloc(1, sizeof(*ret));
206 if (ret == NULL)
207 return NULL;
209 ret->flags = ref->flags;
210 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
211 ret->ref.symref.name = strdup(ref->ref.symref.name);
212 if (ret->ref.symref.name == NULL) {
213 free(ret);
214 return NULL;
216 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
217 if (ret->ref.symref.ref == NULL) {
218 free(ret->ref.symref.name);
219 free(ret);
220 return NULL;
222 } else {
223 ref->ref.ref.name = strdup(ref->ref.ref.name);
224 if (ref->ref.ref.name == NULL) {
225 free(ret);
226 return NULL;
228 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
229 SHA1_DIGEST_LENGTH);
232 return ret;
235 static const struct got_error *
236 resolve_symbolic_ref(struct got_reference **resolved,
237 struct got_repository *repo, struct got_reference *ref)
239 struct got_reference *nextref;
240 const struct got_error *err;
242 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
243 if (err)
244 return err;
246 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
247 err = resolve_symbolic_ref(resolved, repo, nextref);
248 else
249 *resolved = got_ref_dup(nextref);
251 got_ref_close(nextref);
252 return err;
255 const struct got_error *
256 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
257 struct got_reference *ref)
259 const struct got_error *err;
261 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
262 struct got_reference *resolved = NULL;
263 err = resolve_symbolic_ref(&resolved, repo, ref);
264 if (err == NULL)
265 err = got_ref_resolve(id, repo, resolved);
266 free(resolved);
267 return err;
270 *id = calloc(1, sizeof(**id));
271 if (*id == NULL)
272 return got_error(GOT_ERR_NO_MEM);
273 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
274 return NULL;
277 char *
278 got_ref_to_str(struct got_reference *ref)
280 char *str;
281 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
282 if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
283 return NULL;
284 } else {
285 str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
286 if (str == NULL)
287 return NULL;
288 str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
289 SHA1_DIGEST_STRING_LENGTH);
292 return str;