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"
34 #include "delta.h"
35 #include "zb.h"
36 #include "object.h"
38 static const struct got_error *
39 parse_symref(struct got_reference **ref, const char *name, const char *line)
40 {
41 struct got_symref *symref;
42 char *symref_name;
43 char *symref_ref;
45 if (line[0] == '\0')
46 return got_error(GOT_ERR_NOT_REF);
48 symref_name = strdup(name);
49 if (symref_name == NULL)
50 return got_error(GOT_ERR_NO_MEM);
51 symref_ref = strdup(line);
52 if (symref_ref == NULL) {
53 free(symref_name);
54 return got_error(GOT_ERR_NO_MEM);
55 }
57 *ref = calloc(1, sizeof(**ref));
58 if (*ref == NULL)
59 return got_error(GOT_ERR_NO_MEM);
60 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
61 symref = &((*ref)->ref.symref);
62 symref->name = symref_name;
63 symref->ref = symref_ref;
64 return NULL;
65 }
67 static const struct got_error *
68 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
69 {
70 uint8_t digest[SHA1_DIGEST_LENGTH];
71 char *ref_name;
73 if (strncmp(line, "ref: ", 5) == 0) {
74 line += 5;
75 return parse_symref(ref, name, line);
76 }
78 ref_name = strdup(name);
79 if (ref_name == NULL)
80 return got_error(GOT_ERR_NO_MEM);
82 if (!got_parse_sha1_digest(digest, line))
83 return got_error(GOT_ERR_NOT_REF);
85 *ref = calloc(1, sizeof(**ref));
86 if (*ref == NULL)
87 return got_error(GOT_ERR_NO_MEM);
88 (*ref)->ref.ref.name = ref_name;
89 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
90 return NULL;
91 }
93 static const struct got_error *
94 parse_ref_file(struct got_reference **ref, const char *name,
95 const char *abspath)
96 {
97 const struct got_error *err = NULL;
98 FILE *f = fopen(abspath, "rb");
99 char *line;
100 size_t len;
101 const char delim[3] = {'\0', '\0', '\0'};
103 if (f == NULL)
104 return got_error(GOT_ERR_NOT_REF);
106 line = fparseln(f, &len, NULL, delim, 0);
107 if (line == NULL) {
108 err = got_error(GOT_ERR_NOT_REF);
109 goto done;
112 err = parse_ref_line(ref, name, line);
113 done:
114 free(line);
115 fclose(f);
116 return err;
119 static char *
120 get_refs_dir_path(struct got_repository *repo, const char *refname)
122 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
123 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
124 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
125 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
126 strncmp(refname, "refs/", 5) == 0)
127 return got_repo_get_path_git_dir(repo);
129 return got_repo_get_path_refs(repo);
132 const struct got_error *
133 got_ref_open(struct got_reference **ref, struct got_repository *repo,
134 const char *refname)
136 const struct got_error *err = NULL;
137 char *path_ref = NULL;
138 char *normpath = NULL;
139 const char *parent_dir;
140 char *path_refs = get_refs_dir_path(repo, refname);
142 if (path_refs == NULL) {
143 err = got_error(GOT_ERR_NO_MEM);
144 goto done;
147 /* XXX For now, this assumes that refs exist in the filesystem. */
149 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
150 err = got_error(GOT_ERR_NO_MEM);
151 goto done;
154 normpath = got_path_normalize(path_ref);
155 if (normpath == NULL) {
156 err = got_error(GOT_ERR_NOT_REF);
157 goto done;
160 err = parse_ref_file(ref, refname, normpath);
161 done:
162 free(normpath);
163 free(path_ref);
164 free(path_refs);
165 return err;
168 void
169 got_ref_close(struct got_reference *ref)
171 if (ref->flags & GOT_REF_IS_SYMBOLIC)
172 free(ref->ref.symref.name);
173 else
174 free(ref->ref.ref.name);
175 free(ref);
178 struct got_reference *
179 got_ref_dup(struct got_reference *ref)
181 struct got_reference *ret;
182 char *name = NULL;
183 char *symref = NULL;
185 ret = calloc(1, sizeof(*ret));
186 if (ret == NULL)
187 return NULL;
189 ret->flags = ref->flags;
190 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
191 ret->ref.symref.name = strdup(ref->ref.symref.name);
192 if (ret->ref.symref.name == NULL) {
193 free(ret);
194 return NULL;
196 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
197 if (ret->ref.symref.ref == NULL) {
198 free(ret->ref.symref.name);
199 free(ret);
200 return NULL;
202 } else {
203 ref->ref.ref.name = strdup(ref->ref.ref.name);
204 if (ref->ref.ref.name == NULL) {
205 free(ret);
206 return NULL;
208 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
209 SHA1_DIGEST_LENGTH);
212 return ret;
215 static const struct got_error *
216 resolve_symbolic_ref(struct got_reference **resolved,
217 struct got_repository *repo, struct got_reference *ref)
219 struct got_reference *nextref;
220 const struct got_error *err;
222 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
223 if (err)
224 return err;
226 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
227 err = resolve_symbolic_ref(resolved, repo, nextref);
228 else
229 *resolved = got_ref_dup(nextref);
231 got_ref_close(nextref);
232 return err;
235 const struct got_error *
236 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
237 struct got_reference *ref)
239 const struct got_error *err;
241 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
242 struct got_reference *resolved = NULL;
243 err = resolve_symbolic_ref(&resolved, repo, ref);
244 if (err == NULL)
245 err = got_ref_resolve(id, repo, resolved);
246 free(resolved);
247 return err;
250 *id = calloc(1, sizeof(**id));
251 if (*id == NULL)
252 return got_error(GOT_ERR_NO_MEM);
253 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
254 return NULL;