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>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_reference.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 #define GOT_REF_HEADS "heads"
40 #define GOT_REF_TAGS "tags"
41 #define GOT_REF_REMOTES "remotes"
43 /* A symbolic reference. */
44 struct got_symref {
45 char *name;
46 char *ref;
47 };
49 /* A non-symbolic reference (there is no better designation). */
50 struct got_ref {
51 char *name;
52 u_int8_t sha1[SHA1_DIGEST_LENGTH];
53 };
55 /* A reference which points to an arbitrary object. */
56 struct got_reference {
57 unsigned int flags;
58 #define GOT_REF_IS_SYMBOLIC 0x01
60 union {
61 struct got_ref ref;
62 struct got_symref symref;
63 } ref;
64 };
66 static const struct got_error *
67 parse_symref(struct got_reference **ref, const char *name, const char *line)
68 {
69 struct got_symref *symref;
70 char *symref_name;
71 char *symref_ref;
73 if (line[0] == '\0')
74 return got_error(GOT_ERR_NOT_REF);
76 symref_name = strdup(name);
77 if (symref_name == NULL)
78 return got_error_from_errno();
79 symref_ref = strdup(line);
80 if (symref_ref == NULL) {
81 const struct got_error *err = got_error_from_errno();
82 free(symref_name);
83 return err;
84 }
86 *ref = calloc(1, sizeof(**ref));
87 if (*ref == NULL)
88 return got_error_from_errno();
89 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
90 symref = &((*ref)->ref.symref);
91 symref->name = symref_name;
92 symref->ref = symref_ref;
93 return NULL;
94 }
96 static const struct got_error *
97 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
98 {
99 uint8_t digest[SHA1_DIGEST_LENGTH];
100 char *ref_name;
102 if (strncmp(line, "ref: ", 5) == 0) {
103 line += 5;
104 return parse_symref(ref, name, line);
107 ref_name = strdup(name);
108 if (ref_name == NULL)
109 return got_error_from_errno();
111 if (!got_parse_sha1_digest(digest, line))
112 return got_error(GOT_ERR_NOT_REF);
114 *ref = calloc(1, sizeof(**ref));
115 if (*ref == NULL)
116 return got_error_from_errno();
117 (*ref)->ref.ref.name = ref_name;
118 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
119 return NULL;
122 static const struct got_error *
123 parse_ref_file(struct got_reference **ref, const char *name,
124 const char *abspath)
126 const struct got_error *err = NULL;
127 FILE *f = fopen(abspath, "rb");
128 char *line;
129 size_t len;
130 const char delim[3] = {'\0', '\0', '\0'};
132 if (f == NULL)
133 return got_error(GOT_ERR_NOT_REF);
135 line = fparseln(f, &len, NULL, delim, 0);
136 if (line == NULL) {
137 err = got_error(GOT_ERR_NOT_REF);
138 goto done;
141 err = parse_ref_line(ref, name, line);
142 done:
143 free(line);
144 fclose(f);
145 return err;
148 static char *
149 get_refs_dir_path(struct got_repository *repo, const char *refname)
151 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
155 strncmp(refname, "refs/", 5) == 0)
156 return got_repo_get_path_git_dir(repo);
158 return got_repo_get_path_refs(repo);
161 static const struct got_error *
162 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
163 const char *refname)
165 const struct got_error *err = NULL;
166 char *path_ref;
167 char *normpath;
169 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
170 return got_error_from_errno();
172 normpath = got_path_normalize(path_ref);
173 if (normpath == NULL) {
174 err = got_error(GOT_ERR_NOT_REF);
175 goto done;
178 err = parse_ref_file(ref, refname, normpath);
179 done:
180 free(path_ref);
181 free(normpath);
182 return err;
185 const struct got_error *
186 got_ref_open(struct got_reference **ref, struct got_repository *repo,
187 const char *refname)
189 const struct got_error *err = NULL;
190 char *path_refs = get_refs_dir_path(repo, refname);
192 if (path_refs == NULL) {
193 err = got_error_from_errno();
194 goto done;
197 /* XXX For now, this assumes that refs exist in the filesystem. */
199 err = open_ref(ref, path_refs, GOT_REF_HEADS, refname);
200 if (err != NULL)
201 err = open_ref(ref, path_refs, GOT_REF_TAGS, refname);
202 if (err != NULL)
203 err = open_ref(ref, path_refs, GOT_REF_REMOTES, refname);
204 if (err != NULL)
205 err = open_ref(ref, path_refs, "", refname);
206 done:
207 free(path_refs);
208 return err;
211 void
212 got_ref_close(struct got_reference *ref)
214 if (ref->flags & GOT_REF_IS_SYMBOLIC)
215 free(ref->ref.symref.name);
216 else
217 free(ref->ref.ref.name);
218 free(ref);
221 struct got_reference *
222 got_ref_dup(struct got_reference *ref)
224 struct got_reference *ret;
226 ret = calloc(1, sizeof(*ret));
227 if (ret == NULL)
228 return NULL;
230 ret->flags = ref->flags;
231 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
232 ret->ref.symref.name = strdup(ref->ref.symref.name);
233 if (ret->ref.symref.name == NULL) {
234 free(ret);
235 return NULL;
237 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
238 if (ret->ref.symref.ref == NULL) {
239 free(ret->ref.symref.name);
240 free(ret);
241 return NULL;
243 } else {
244 ref->ref.ref.name = strdup(ref->ref.ref.name);
245 if (ref->ref.ref.name == NULL) {
246 free(ret);
247 return NULL;
249 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
250 SHA1_DIGEST_LENGTH);
253 return ret;
256 static const struct got_error *
257 resolve_symbolic_ref(struct got_reference **resolved,
258 struct got_repository *repo, struct got_reference *ref)
260 struct got_reference *nextref;
261 const struct got_error *err;
263 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
264 if (err)
265 return err;
267 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
268 err = resolve_symbolic_ref(resolved, repo, nextref);
269 else
270 *resolved = got_ref_dup(nextref);
272 got_ref_close(nextref);
273 return err;
276 const struct got_error *
277 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
278 struct got_reference *ref)
280 const struct got_error *err;
282 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
283 struct got_reference *resolved = NULL;
284 err = resolve_symbolic_ref(&resolved, repo, ref);
285 if (err == NULL)
286 err = got_ref_resolve(id, repo, resolved);
287 free(resolved);
288 return err;
291 *id = calloc(1, sizeof(**id));
292 if (*id == NULL)
293 return got_error_from_errno();
294 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
295 return NULL;
298 char *
299 got_ref_to_str(struct got_reference *ref)
301 char *str;
302 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
303 if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
304 return NULL;
305 } else {
306 str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
307 if (str == NULL)
308 return NULL;
309 str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
310 SHA1_DIGEST_STRING_LENGTH);
313 return str;