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>
26 #include "got_error.h"
27 #include "got_object.h"
28 #include "got_repository.h"
29 #include "got_refs.h"
30 #include "got_sha1.h"
32 #include "path.h"
35 static const struct got_error *
36 parse_symref(struct got_reference **ref, const char *name, const char *line)
37 {
38 struct got_symref *symref;
39 char *symref_name;
40 char *symref_ref;
42 if (line[0] == '\0')
43 return got_error(GOT_ERR_NOT_REF);
45 symref_name = strdup(name);
46 if (symref_name == NULL)
47 return got_error(GOT_ERR_NO_MEM);
48 symref_ref = strdup(line);
49 if (symref_ref == NULL) {
50 free(symref_name);
51 return got_error(GOT_ERR_NO_MEM);
52 }
54 *ref = calloc(1, sizeof(**ref));
55 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
56 symref = &((*ref)->ref.symref);
57 symref->name = symref_name;
58 symref->ref = symref_ref;
59 return NULL;
60 }
62 static const struct got_error *
63 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
64 {
65 uint8_t digest[SHA1_DIGEST_LENGTH];
66 char *ref_name;
68 if (strncmp(line, "ref: ", 5) == 0) {
69 line += 5;
70 return parse_symref(ref, name, line);
71 }
73 ref_name = strdup(name);
74 if (ref_name == NULL)
75 return got_error(GOT_ERR_NO_MEM);
77 if (!got_parse_sha1_digest(digest, line))
78 return got_error(GOT_ERR_NOT_REF);
80 *ref = calloc(1, sizeof(**ref));
81 (*ref)->ref.ref.name = ref_name;
82 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
83 return NULL;
84 }
86 static const struct got_error *
87 parse_ref_file(struct got_reference **ref, const char *name,
88 const char *abspath)
89 {
90 const struct got_error *err = NULL;
91 FILE *f = fopen(abspath, "rb");
92 char *line;
93 size_t len;
94 const char delim[3] = {'\0', '\0', '\0'};
96 if (f == NULL)
97 return got_error(GOT_ERR_NOT_REF);
99 line = fparseln(f, &len, NULL, delim, 0);
100 if (line == NULL) {
101 err = got_error(GOT_ERR_NOT_REF);
102 goto done;
105 err = parse_ref_line(ref, name, line);
106 done:
107 free(line);
108 fclose(f);
109 return err;
112 static char *
113 get_refs_dir_path(struct got_repository *repo, const char *refname)
115 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
116 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
117 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
118 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
119 strncmp(refname, "refs/", 5) == 0)
120 return got_repo_get_path_git_dir(repo);
122 return got_repo_get_path_refs(repo);
125 const struct got_error *
126 got_ref_open(struct got_reference **ref, struct got_repository *repo,
127 const char *refname)
129 const struct got_error *err = NULL;
130 char *path_ref = NULL;
131 char *normpath = NULL;
132 const char *parent_dir;
133 char *path_refs = get_refs_dir_path(repo, refname);
135 if (path_refs == NULL) {
136 err = got_error(GOT_ERR_NO_MEM);
137 goto done;
140 /* XXX For now, this assumes that refs exist in the filesystem. */
142 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
143 err = got_error(GOT_ERR_NO_MEM);
144 goto done;
147 normpath = got_path_normalize(path_ref);
148 if (normpath == NULL) {
149 err = got_error(GOT_ERR_NOT_REF);
150 goto done;
153 err = parse_ref_file(ref, refname, normpath);
154 done:
155 free(normpath);
156 free(path_ref);
157 free(path_refs);
158 return err;
161 void
162 got_ref_close(struct got_reference *ref)
164 if (ref->flags & GOT_REF_IS_SYMBOLIC)
165 free(ref->ref.symref.name);
166 else
167 free(ref->ref.ref.name);
168 free(ref);
171 struct got_reference *
172 got_ref_dup(struct got_reference *ref)
174 struct got_reference *ret = calloc(1, sizeof(*ret));
175 char *name = NULL;
176 char *symref = NULL;
178 if (ret == NULL)
179 return NULL;
181 ret->flags = ref->flags;
182 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
183 ret->ref.symref.name = strdup(ref->ref.symref.name);
184 if (ret->ref.symref.name == NULL) {
185 free(ret);
186 return NULL;
188 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
189 if (ret->ref.symref.ref == NULL) {
190 free(ret->ref.symref.name);
191 free(ret);
192 return NULL;
194 } else {
195 ref->ref.ref.name = strdup(ref->ref.ref.name);
196 if (ref->ref.ref.name == NULL) {
197 free(ret);
198 return NULL;
200 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
201 SHA1_DIGEST_LENGTH);
204 return ret;
207 static const struct got_error *
208 resolve_symbolic_ref(struct got_reference **resolved,
209 struct got_repository *repo, struct got_reference *ref)
211 struct got_reference *nextref;
212 const struct got_error *err;
214 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
215 if (err)
216 return err;
218 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
219 err = resolve_symbolic_ref(resolved, repo, nextref);
220 else
221 *resolved = got_ref_dup(nextref);
223 got_ref_close(nextref);
224 return err;
227 const struct got_error *
228 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
229 struct got_reference *ref)
231 const struct got_error *err;
233 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
234 struct got_reference *resolved = NULL;
235 err = resolve_symbolic_ref(&resolved, repo, ref);
236 if (err == NULL)
237 err = got_ref_resolve(id, repo, resolved);
238 free(resolved);
239 return err;
242 *id = calloc(1, sizeof(**id));
243 if (*id == NULL)
244 return got_error(GOT_ERR_NO_MEM);
245 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
246 return NULL;