Blob


1 /*
2 * Copyright (c) 2018 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/queue.h>
18 #include <sys/stat.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sha1.h>
24 #include <string.h>
25 #include <zlib.h>
27 #include "got_error.h"
28 #include "got_reference.h"
29 #include "got_repository.h"
31 #include "got_lib_path.h"
32 #include "got_lib_delta.h"
33 #include "got_lib_zbuf.h"
34 #include "got_lib_object.h"
35 #include "got_lib_pack.h"
36 #include "got_lib_repository.h"
38 #ifndef nitems
39 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 #endif
42 #define GOT_GIT_DIR ".git"
44 /* Mandatory files and directories inside the git directory. */
45 #define GOT_OBJECTS_DIR "objects"
46 #define GOT_REFS_DIR "refs"
47 #define GOT_HEAD_FILE "HEAD"
49 /* Other files and directories inside the git directory. */
50 #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
51 #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
52 #define GOT_OBJECTS_PACK_DIR "objects/pack"
54 char *
55 got_repo_get_path(struct got_repository *repo)
56 {
57 return strdup(repo->path);
58 }
60 char *
61 got_repo_get_path_git_dir(struct got_repository *repo)
62 {
63 return strdup(repo->path_git_dir);
64 }
66 static char *
67 get_path_git_child(struct got_repository *repo, const char *basename)
68 {
69 char *path_child;
71 if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
72 basename) == -1)
73 return NULL;
75 return path_child;
76 }
78 char *
79 got_repo_get_path_objects(struct got_repository *repo)
80 {
81 return get_path_git_child(repo, GOT_OBJECTS_DIR);
82 }
84 char *
85 got_repo_get_path_objects_pack(struct got_repository *repo)
86 {
87 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
88 }
90 char *
91 got_repo_get_path_refs(struct got_repository *repo)
92 {
93 return get_path_git_child(repo, GOT_REFS_DIR);
94 }
96 static char *
97 get_path_head(struct got_repository *repo)
98 {
99 return get_path_git_child(repo, GOT_HEAD_FILE);
102 static int
103 is_git_repo(struct got_repository *repo)
105 char *path_git = got_repo_get_path_git_dir(repo);
106 char *path_objects = got_repo_get_path_objects(repo);
107 char *path_refs = got_repo_get_path_refs(repo);
108 char *path_head = get_path_head(repo);
109 int ret = 0;
110 struct stat sb;
111 struct got_reference *head_ref;
113 if (lstat(path_git, &sb) == -1)
114 goto done;
115 if (!S_ISDIR(sb.st_mode))
116 goto done;
118 if (lstat(path_objects, &sb) == -1)
119 goto done;
120 if (!S_ISDIR(sb.st_mode))
121 goto done;
123 if (lstat(path_refs, &sb) == -1)
124 goto done;
125 if (!S_ISDIR(sb.st_mode))
126 goto done;
128 if (lstat(path_head, &sb) == -1)
129 goto done;
130 if (!S_ISREG(sb.st_mode))
131 goto done;
133 /* Check if the HEAD reference can be opened. */
134 if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
135 goto done;
136 got_ref_close(head_ref);
138 ret = 1;
139 done:
140 free(path_git);
141 free(path_objects);
142 free(path_refs);
143 free(path_head);
144 return ret;
148 const struct got_error *
149 got_repo_open(struct got_repository **ret, const char *path)
151 struct got_repository *repo = NULL;
152 const struct got_error *err = NULL;
153 char *abspath;
155 if (got_path_is_absolute(path))
156 abspath = strdup(path);
157 else
158 abspath = got_path_get_absolute(path);
159 if (abspath == NULL)
160 return got_error(GOT_ERR_BAD_PATH);
162 repo = calloc(1, sizeof(*repo));
163 if (repo == NULL) {
164 err = got_error_from_errno();
165 goto done;
168 repo->path = got_path_normalize(abspath);
169 if (repo->path == NULL) {
170 err = got_error(GOT_ERR_BAD_PATH);
171 goto done;
174 repo->path_git_dir = strdup(repo->path);
175 if (repo->path_git_dir == NULL) {
176 err = got_error_from_errno();
177 goto done;
179 if (!is_git_repo(repo)) {
180 free(repo->path_git_dir);
181 if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
182 GOT_GIT_DIR) == -1) {
183 err = got_error_from_errno();
184 goto done;
186 if (!is_git_repo(repo)) {
187 err = got_error(GOT_ERR_NOT_GIT_REPO);
188 goto done;
192 *ret = repo;
193 done:
194 if (err)
195 got_repo_close(repo);
196 free(abspath);
197 return err;
200 void
201 got_repo_close(struct got_repository *repo)
203 int i;
205 for (i = 0; i < nitems(repo->packidx_cache); i++) {
206 if (repo->packidx_cache[i] == NULL)
207 break;
208 got_packidx_close(repo->packidx_cache[i]);
211 for (i = 0; i < nitems(repo->packs); i++) {
212 if (repo->packs[i].path_packfile == NULL)
213 break;
214 got_pack_close(&repo->packs[i]);
217 free(repo->path);
218 free(repo->path_git_dir);
219 free(repo);