Blob


1 /*
2 * Copyright (c) 2017 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"
31 #include "got_sha1.h"
33 #include "path.h"
36 static const struct got_error *
37 parse_symref(struct got_reference **ref, const char *name, const char *line)
38 {
39 struct got_symref *symref;
40 char *symref_name;
41 char *symref_ref;
43 if (line[0] == '\0')
44 return got_error(GOT_ERR_NOT_REF);
46 symref_name = strdup(name);
47 if (symref_name == NULL)
48 return got_error(GOT_ERR_NO_MEM);
49 symref_ref = strdup(line);
50 if (symref_ref == NULL) {
51 free(symref_name);
52 return got_error(GOT_ERR_NO_MEM);
53 }
55 *ref = calloc(1, sizeof(**ref));
56 if (*ref == NULL)
57 return got_error(GOT_ERR_NO_MEM);
58 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
59 symref = &((*ref)->ref.symref);
60 symref->name = symref_name;
61 symref->ref = symref_ref;
62 return NULL;
63 }
65 static const struct got_error *
66 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
67 {
68 uint8_t digest[SHA1_DIGEST_LENGTH];
69 char *ref_name;
71 if (strncmp(line, "ref: ", 5) == 0) {
72 line += 5;
73 return parse_symref(ref, name, line);
74 }
76 ref_name = strdup(name);
77 if (ref_name == NULL)
78 return got_error(GOT_ERR_NO_MEM);
80 if (!got_parse_sha1_digest(digest, line))
81 return got_error(GOT_ERR_NOT_REF);
83 *ref = calloc(1, sizeof(**ref));
84 if (*ref == NULL)
85 return got_error(GOT_ERR_NO_MEM);
86 (*ref)->ref.ref.name = ref_name;
87 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
88 return NULL;
89 }
91 static const struct got_error *
92 parse_ref_file(struct got_reference **ref, const char *name,
93 const char *abspath)
94 {
95 const struct got_error *err = NULL;
96 FILE *f = fopen(abspath, "rb");
97 char *line;
98 size_t len;
99 const char delim[3] = {'\0', '\0', '\0'};
101 if (f == NULL)
102 return got_error(GOT_ERR_NOT_REF);
104 line = fparseln(f, &len, NULL, delim, 0);
105 if (line == NULL) {
106 err = got_error(GOT_ERR_NOT_REF);
107 goto done;
110 err = parse_ref_line(ref, name, line);
111 done:
112 free(line);
113 fclose(f);
114 return err;
117 static char *
118 get_refs_dir_path(struct got_repository *repo, const char *refname)
120 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
121 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
122 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
123 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
124 strncmp(refname, "refs/", 5) == 0)
125 return got_repo_get_path_git_dir(repo);
127 return got_repo_get_path_refs(repo);
130 const struct got_error *
131 got_ref_open(struct got_reference **ref, struct got_repository *repo,
132 const char *refname)
134 const struct got_error *err = NULL;
135 char *path_ref = NULL;
136 char *normpath = NULL;
137 const char *parent_dir;
138 char *path_refs = get_refs_dir_path(repo, refname);
140 if (path_refs == NULL) {
141 err = got_error(GOT_ERR_NO_MEM);
142 goto done;
145 /* XXX For now, this assumes that refs exist in the filesystem. */
147 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
148 err = got_error(GOT_ERR_NO_MEM);
149 goto done;
152 normpath = got_path_normalize(path_ref);
153 if (normpath == NULL) {
154 err = got_error(GOT_ERR_NOT_REF);
155 goto done;
158 err = parse_ref_file(ref, refname, normpath);
159 done:
160 free(normpath);
161 free(path_ref);
162 free(path_refs);
163 return err;
166 void
167 got_ref_close(struct got_reference *ref)
169 if (ref->flags & GOT_REF_IS_SYMBOLIC)
170 free(ref->ref.symref.name);
171 else
172 free(ref->ref.ref.name);
173 free(ref);
176 struct got_reference *
177 got_ref_dup(struct got_reference *ref)
179 struct got_reference *ret;
180 char *name = NULL;
181 char *symref = NULL;
183 ret = calloc(1, sizeof(*ret));
184 if (ret == NULL)
185 return NULL;
187 ret->flags = ref->flags;
188 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
189 ret->ref.symref.name = strdup(ref->ref.symref.name);
190 if (ret->ref.symref.name == NULL) {
191 free(ret);
192 return NULL;
194 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
195 if (ret->ref.symref.ref == NULL) {
196 free(ret->ref.symref.name);
197 free(ret);
198 return NULL;
200 } else {
201 ref->ref.ref.name = strdup(ref->ref.ref.name);
202 if (ref->ref.ref.name == NULL) {
203 free(ret);
204 return NULL;
206 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
207 SHA1_DIGEST_LENGTH);
210 return ret;
213 static const struct got_error *
214 resolve_symbolic_ref(struct got_reference **resolved,
215 struct got_repository *repo, struct got_reference *ref)
217 struct got_reference *nextref;
218 const struct got_error *err;
220 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
221 if (err)
222 return err;
224 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
225 err = resolve_symbolic_ref(resolved, repo, nextref);
226 else
227 *resolved = got_ref_dup(nextref);
229 got_ref_close(nextref);
230 return err;
233 const struct got_error *
234 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
235 struct got_reference *ref)
237 const struct got_error *err;
239 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
240 struct got_reference *resolved = NULL;
241 err = resolve_symbolic_ref(&resolved, repo, ref);
242 if (err == NULL)
243 err = got_ref_resolve(id, repo, resolved);
244 free(resolved);
245 return err;
248 *id = calloc(1, sizeof(**id));
249 if (*id == NULL)
250 return got_error(GOT_ERR_NO_MEM);
251 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
252 return NULL;