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"
36 static const struct got_error *
37 parse_symref(struct got_reference **ref, const char *name, const char *line)
39 struct got_symref *symref;
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) {
52 return got_error(GOT_ERR_NO_MEM);
55 *ref = calloc(1, sizeof(**ref));
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;
65 static const struct got_error *
66 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
68 uint8_t digest[SHA1_DIGEST_LENGTH];
71 if (strncmp(line, "ref: ", 5) == 0) {
73 return parse_symref(ref, name, line);
76 ref_name = strdup(name);
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));
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);
91 static const struct got_error *
92 parse_ref_file(struct got_reference **ref, const char *name,
95 const struct got_error *err = NULL;
96 FILE *f = fopen(abspath, "rb");
99 const char delim[3] = {'\0', '\0', '\0'};
102 return got_error(GOT_ERR_NOT_REF);
104 line = fparseln(f, &len, NULL, delim, 0);
106 err = got_error(GOT_ERR_NOT_REF);
110 err = parse_ref_line(ref, name, line);
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,
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);
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);
152 normpath = got_path_normalize(path_ref);
153 if (normpath == NULL) {
154 err = got_error(GOT_ERR_NOT_REF);
158 err = parse_ref_file(ref, refname, normpath);
167 got_ref_close(struct got_reference *ref)
169 if (ref->flags & GOT_REF_IS_SYMBOLIC)
170 free(ref->ref.symref.name);
172 free(ref->ref.ref.name);
176 struct got_reference *
177 got_ref_dup(struct got_reference *ref)
179 struct got_reference *ret;
183 ret = calloc(1, sizeof(*ret));
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) {
194 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
195 if (ret->ref.symref.ref == NULL) {
196 free(ret->ref.symref.name);
201 ref->ref.ref.name = strdup(ref->ref.ref.name);
202 if (ref->ref.ref.name == NULL) {
206 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
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);
224 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
225 err = resolve_symbolic_ref(resolved, repo, nextref);
227 *resolved = got_ref_dup(nextref);
229 got_ref_close(nextref);
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);
243 err = got_ref_resolve(id, repo, resolved);
248 *id = calloc(1, sizeof(**id));
250 return got_error(GOT_ERR_NO_MEM);
251 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);