2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
38 static const struct got_error *
39 parse_symref(struct got_reference **ref, const char *name, const char *line)
41 struct got_symref *symref;
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) {
54 return got_error(GOT_ERR_NO_MEM);
57 *ref = calloc(1, sizeof(**ref));
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;
67 static const struct got_error *
68 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
70 uint8_t digest[SHA1_DIGEST_LENGTH];
73 if (strncmp(line, "ref: ", 5) == 0) {
75 return parse_symref(ref, name, line);
78 ref_name = strdup(name);
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));
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);
93 static const struct got_error *
94 parse_ref_file(struct got_reference **ref, const char *name,
97 const struct got_error *err = NULL;
98 FILE *f = fopen(abspath, "rb");
101 const char delim[3] = {'\0', '\0', '\0'};
104 return got_error(GOT_ERR_NOT_REF);
106 line = fparseln(f, &len, NULL, delim, 0);
108 err = got_error(GOT_ERR_NOT_REF);
112 err = parse_ref_line(ref, name, line);
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,
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);
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);
154 normpath = got_path_normalize(path_ref);
155 if (normpath == NULL) {
156 err = got_error(GOT_ERR_NOT_REF);
160 err = parse_ref_file(ref, refname, normpath);
169 got_ref_close(struct got_reference *ref)
171 if (ref->flags & GOT_REF_IS_SYMBOLIC)
172 free(ref->ref.symref.name);
174 free(ref->ref.ref.name);
178 struct got_reference *
179 got_ref_dup(struct got_reference *ref)
181 struct got_reference *ret;
185 ret = calloc(1, sizeof(*ret));
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) {
196 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
197 if (ret->ref.symref.ref == NULL) {
198 free(ret->ref.symref.name);
203 ref->ref.ref.name = strdup(ref->ref.ref.name);
204 if (ref->ref.ref.name == NULL) {
208 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
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);
226 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
227 err = resolve_symbolic_ref(resolved, repo, nextref);
229 *resolved = got_ref_dup(nextref);
231 got_ref_close(nextref);
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);
245 err = got_ref_resolve(id, repo, resolved);
250 *id = calloc(1, sizeof(**id));
252 return got_error(GOT_ERR_NO_MEM);
253 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);