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 <assert.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <sha1.h>
22 #include <string.h>
24 #include "got_path.h"
25 #include "got_error.h"
26 #include "got_refs.h"
27 #include "got_repository.h"
29 #define GOT_GIT_DIR ".git"
31 /* Mandatory files and directories inside the git directory. */
32 #define GOT_OBJECTS_DIR "objects"
33 #define GOT_REFS_DIR "refs"
34 #define GOT_HEAD_FILE "HEAD"
36 static char *
37 get_path_git_dir(struct got_repository *repo)
38 {
39 char *path_git;
41 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
42 return NULL;
44 return path_git;
45 }
47 static char *
48 get_path_git_child(struct got_repository *repo, const char *basename)
49 {
50 char *path_child;
52 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
53 basename) == -1)
54 return NULL;
56 return path_child;
57 }
59 static char *
60 get_path_objects(struct got_repository *repo)
61 {
62 return get_path_git_child(repo, GOT_OBJECTS_DIR);
63 }
65 static char *
66 get_path_refs(struct got_repository *repo)
67 {
68 return get_path_git_child(repo, GOT_REFS_DIR);
69 }
71 static char *
72 get_path_head(struct got_repository *repo)
73 {
74 return get_path_git_child(repo, GOT_HEAD_FILE);
75 }
77 static int
78 is_git_repo(struct got_repository *repo)
79 {
80 char *path_git = get_path_git_dir(repo);
81 char *path_objects = get_path_objects(repo);
82 char *path_refs = get_path_refs(repo);
83 char *path_head = get_path_head(repo);
84 int ret;
86 ret = (path_git != NULL) && (path_objects != NULL) &&
87 (path_refs != NULL) && (path_head != NULL);
89 free(path_git);
90 free(path_objects);
91 free(path_refs);
92 free(path_head);
93 return ret;
95 }
97 const struct got_error *
98 got_repo_open(struct got_repository **ret, const char *abspath)
99 {
100 struct got_repository *repo;
102 if (!got_path_is_absolute(abspath))
103 return got_error(GOT_ERR_NOT_ABSPATH);
105 repo = calloc(1, sizeof(*repo));
106 if (repo == NULL)
107 return got_error(GOT_ERR_NO_MEM);
109 repo->path = got_path_normalize(abspath);
110 if (repo->path == NULL)
111 return got_error(GOT_ERR_BAD_PATH);
113 if (!is_git_repo(repo))
114 return got_error(GOT_ERR_NOT_GIT_REPO);
116 *ret = repo;
117 return NULL;
120 void
121 got_repo_close(struct got_repository *repo)
123 free(repo->path);
124 free(repo);
127 const char *
128 got_repo_get_path(struct got_repository *repo)
130 return repo->path;
133 const struct got_error *
134 got_repo_get_reference(struct got_reference **ref,
135 struct got_repository *repo, const char *refname)
137 const struct got_error *err = NULL;
138 char *path_refs;
140 /* Some refs live in the .git directory. */
141 if (strcmp(refname, GOT_REF_HEAD) == 0)
142 path_refs = get_path_git_dir(repo);
143 else
144 path_refs = get_path_refs(repo);
146 err = got_ref_open(ref, path_refs, refname);
147 free(path_refs);
148 return err;