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_refs.h"
29 #include "got_repository.h"
31 #include "got_path_lib.h"
32 #include "got_repository_lib.h"
33 #include "got_zbuf_lib.h"
34 #include "got_delta_lib.h"
35 #include "got_object_lib.h"
36 #include "got_pack_lib.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 char *path_git;
65 if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
66 return NULL;
68 return path_git;
69 }
71 static char *
72 get_path_git_child(struct got_repository *repo, const char *basename)
73 {
74 char *path_child;
76 if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
77 basename) == -1)
78 return NULL;
80 return path_child;
81 }
83 char *
84 got_repo_get_path_objects(struct got_repository *repo)
85 {
86 return get_path_git_child(repo, GOT_OBJECTS_DIR);
87 }
89 char *
90 got_repo_get_path_objects_pack(struct got_repository *repo)
91 {
92 return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
93 }
95 char *
96 got_repo_get_path_refs(struct got_repository *repo)
97 {
98 return get_path_git_child(repo, GOT_REFS_DIR);
99 }
101 static char *
102 get_path_head(struct got_repository *repo)
104 return get_path_git_child(repo, GOT_HEAD_FILE);
107 static int
108 is_git_repo(struct got_repository *repo)
110 char *path_git = got_repo_get_path_git_dir(repo);
111 char *path_objects = got_repo_get_path_objects(repo);
112 char *path_refs = got_repo_get_path_refs(repo);
113 char *path_head = get_path_head(repo);
114 int ret = 0;
115 struct stat sb;
117 if (lstat(path_git, &sb) == -1)
118 goto done;
119 if (!S_ISDIR(sb.st_mode))
120 goto done;
122 if (lstat(path_objects, &sb) == -1)
123 goto done;
124 if (!S_ISDIR(sb.st_mode))
125 goto done;
127 if (lstat(path_refs, &sb) == -1)
128 goto done;
129 if (!S_ISDIR(sb.st_mode))
130 goto done;
132 if (lstat(path_head, &sb) == -1)
133 goto done;
134 if (!S_ISREG(sb.st_mode))
135 goto done;
136 ret = 1;
137 done:
138 free(path_git);
139 free(path_objects);
140 free(path_refs);
141 free(path_head);
142 return ret;
146 const struct got_error *
147 got_repo_open(struct got_repository **ret, const char *path)
149 struct got_repository *repo = NULL;
150 const struct got_error *err = NULL;
151 char *abspath;
153 if (got_path_is_absolute(path))
154 abspath = strdup(path);
155 else
156 abspath = got_path_get_absolute(path);
157 if (abspath == NULL)
158 return got_error(GOT_ERR_BAD_PATH);
160 repo = calloc(1, sizeof(*repo));
161 if (repo == NULL) {
162 err = got_error(GOT_ERR_NO_MEM);
163 goto done;
166 repo->path = got_path_normalize(abspath);
167 if (repo->path == NULL) {
168 err = got_error(GOT_ERR_BAD_PATH);
169 goto done;
172 if (!is_git_repo(repo)) {
173 err = got_error(GOT_ERR_NOT_GIT_REPO);
174 goto done;
177 *ret = repo;
178 done:
179 if (err)
180 free(repo);
181 free(abspath);
182 return err;
185 void
186 got_repo_close(struct got_repository *repo)
188 int i;
190 for (i = 0; i < nitems(repo->packidx_cache); i++) {
191 if (repo->packidx_cache[i] == NULL)
192 break;
193 got_packidx_close(repo->packidx_cache[i]);
195 free(repo->path);
196 free(repo);