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"
37 #include "refs.h"
39 static const struct got_error *
40 parse_symref(struct got_reference **ref, const char *name, const char *line)
41 {
42 struct got_symref *symref;
43 char *symref_name;
44 char *symref_ref;
46 if (line[0] == '\0')
47 return got_error(GOT_ERR_NOT_REF);
49 symref_name = strdup(name);
50 if (symref_name == NULL)
51 return got_error(GOT_ERR_NO_MEM);
52 symref_ref = strdup(line);
53 if (symref_ref == NULL) {
54 free(symref_name);
55 return got_error(GOT_ERR_NO_MEM);
56 }
58 *ref = calloc(1, sizeof(**ref));
59 if (*ref == NULL)
60 return got_error(GOT_ERR_NO_MEM);
61 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
62 symref = &((*ref)->ref.symref);
63 symref->name = symref_name;
64 symref->ref = symref_ref;
65 return NULL;
66 }
68 static const struct got_error *
69 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
70 {
71 uint8_t digest[SHA1_DIGEST_LENGTH];
72 char *ref_name;
74 if (strncmp(line, "ref: ", 5) == 0) {
75 line += 5;
76 return parse_symref(ref, name, line);
77 }
79 ref_name = strdup(name);
80 if (ref_name == NULL)
81 return got_error(GOT_ERR_NO_MEM);
83 if (!got_parse_sha1_digest(digest, line))
84 return got_error(GOT_ERR_NOT_REF);
86 *ref = calloc(1, sizeof(**ref));
87 if (*ref == NULL)
88 return got_error(GOT_ERR_NO_MEM);
89 (*ref)->ref.ref.name = ref_name;
90 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
91 return NULL;
92 }
94 static const struct got_error *
95 parse_ref_file(struct got_reference **ref, const char *name,
96 const char *abspath)
97 {
98 const struct got_error *err = NULL;
99 FILE *f = fopen(abspath, "rb");
100 char *line;
101 size_t len;
102 const char delim[3] = {'\0', '\0', '\0'};
104 if (f == NULL)
105 return got_error(GOT_ERR_NOT_REF);
107 line = fparseln(f, &len, NULL, delim, 0);
108 if (line == NULL) {
109 err = got_error(GOT_ERR_NOT_REF);
110 goto done;
113 err = parse_ref_line(ref, name, line);
114 done:
115 free(line);
116 fclose(f);
117 return err;
120 static char *
121 get_refs_dir_path(struct got_repository *repo, const char *refname)
123 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
124 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
125 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
126 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
127 strncmp(refname, "refs/", 5) == 0)
128 return got_repo_get_path_git_dir(repo);
130 return got_repo_get_path_refs(repo);
133 const struct got_error *
134 got_ref_open(struct got_reference **ref, struct got_repository *repo,
135 const char *refname)
137 const struct got_error *err = NULL;
138 char *path_ref = NULL;
139 char *normpath = NULL;
140 const char *parent_dir;
141 char *path_refs = get_refs_dir_path(repo, refname);
143 if (path_refs == NULL) {
144 err = got_error(GOT_ERR_NO_MEM);
145 goto done;
148 /* XXX For now, this assumes that refs exist in the filesystem. */
150 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
151 err = got_error(GOT_ERR_NO_MEM);
152 goto done;
155 normpath = got_path_normalize(path_ref);
156 if (normpath == NULL) {
157 err = got_error(GOT_ERR_NOT_REF);
158 goto done;
161 err = parse_ref_file(ref, refname, normpath);
162 done:
163 free(normpath);
164 free(path_ref);
165 free(path_refs);
166 return err;
169 void
170 got_ref_close(struct got_reference *ref)
172 if (ref->flags & GOT_REF_IS_SYMBOLIC)
173 free(ref->ref.symref.name);
174 else
175 free(ref->ref.ref.name);
176 free(ref);
179 struct got_reference *
180 got_ref_dup(struct got_reference *ref)
182 struct got_reference *ret;
183 char *name = NULL;
184 char *symref = NULL;
186 ret = calloc(1, sizeof(*ret));
187 if (ret == NULL)
188 return NULL;
190 ret->flags = ref->flags;
191 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
192 ret->ref.symref.name = strdup(ref->ref.symref.name);
193 if (ret->ref.symref.name == NULL) {
194 free(ret);
195 return NULL;
197 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
198 if (ret->ref.symref.ref == NULL) {
199 free(ret->ref.symref.name);
200 free(ret);
201 return NULL;
203 } else {
204 ref->ref.ref.name = strdup(ref->ref.ref.name);
205 if (ref->ref.ref.name == NULL) {
206 free(ret);
207 return NULL;
209 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
210 SHA1_DIGEST_LENGTH);
213 return ret;
216 static const struct got_error *
217 resolve_symbolic_ref(struct got_reference **resolved,
218 struct got_repository *repo, struct got_reference *ref)
220 struct got_reference *nextref;
221 const struct got_error *err;
223 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
224 if (err)
225 return err;
227 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
228 err = resolve_symbolic_ref(resolved, repo, nextref);
229 else
230 *resolved = got_ref_dup(nextref);
232 got_ref_close(nextref);
233 return err;
236 const struct got_error *
237 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
238 struct got_reference *ref)
240 const struct got_error *err;
242 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
243 struct got_reference *resolved = NULL;
244 err = resolve_symbolic_ref(&resolved, repo, ref);
245 if (err == NULL)
246 err = got_ref_resolve(id, repo, resolved);
247 free(resolved);
248 return err;
251 *id = calloc(1, sizeof(**id));
252 if (*id == NULL)
253 return got_error(GOT_ERR_NO_MEM);
254 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
255 return NULL;