Blame


1 7b19e0f1 2017-11-05 stsp /*
2 3b339b2f 2018-02-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 79b11c62 2018-03-09 stsp #include <sys/queue.h>
18 deeca238 2018-03-12 stsp #include <sys/stat.h>
19 79b11c62 2018-03-09 stsp
20 4027f31a 2017-11-04 stsp #include <limits.h>
21 4027f31a 2017-11-04 stsp #include <stdlib.h>
22 4027f31a 2017-11-04 stsp #include <stdio.h>
23 4027f31a 2017-11-04 stsp #include <sha1.h>
24 4027f31a 2017-11-04 stsp #include <string.h>
25 79b11c62 2018-03-09 stsp #include <zlib.h>
26 4027f31a 2017-11-04 stsp
27 4027f31a 2017-11-04 stsp #include "got_error.h"
28 5261c201 2018-04-01 stsp #include "got_reference.h"
29 4027f31a 2017-11-04 stsp #include "got_repository.h"
30 4027f31a 2017-11-04 stsp
31 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
32 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
37 c3f94f68 2017-11-05 stsp
38 79b11c62 2018-03-09 stsp #ifndef nitems
39 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 79b11c62 2018-03-09 stsp #endif
41 3b339b2f 2018-02-12 stsp
42 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
43 4027f31a 2017-11-04 stsp
44 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
45 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
46 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
47 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
48 4027f31a 2017-11-04 stsp
49 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
50 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
51 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
52 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
53 4df642d9 2017-11-05 stsp
54 11995603 2017-11-05 stsp char *
55 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
56 86c3caaf 2018-03-09 stsp {
57 86c3caaf 2018-03-09 stsp return strdup(repo->path);
58 86c3caaf 2018-03-09 stsp }
59 86c3caaf 2018-03-09 stsp
60 86c3caaf 2018-03-09 stsp char *
61 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
62 4027f31a 2017-11-04 stsp {
63 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
64 4027f31a 2017-11-04 stsp }
65 4027f31a 2017-11-04 stsp
66 4027f31a 2017-11-04 stsp static char *
67 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
68 4027f31a 2017-11-04 stsp {
69 4027f31a 2017-11-04 stsp char *path_child;
70 4027f31a 2017-11-04 stsp
71 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
72 4027f31a 2017-11-04 stsp basename) == -1)
73 4027f31a 2017-11-04 stsp return NULL;
74 4027f31a 2017-11-04 stsp
75 4027f31a 2017-11-04 stsp return path_child;
76 4027f31a 2017-11-04 stsp }
77 4027f31a 2017-11-04 stsp
78 11995603 2017-11-05 stsp char *
79 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
80 4027f31a 2017-11-04 stsp {
81 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
82 4027f31a 2017-11-04 stsp }
83 4027f31a 2017-11-04 stsp
84 11995603 2017-11-05 stsp char *
85 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
86 a1fd68d8 2018-01-12 stsp {
87 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
88 a1fd68d8 2018-01-12 stsp }
89 a1fd68d8 2018-01-12 stsp
90 a1fd68d8 2018-01-12 stsp char *
91 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
92 4027f31a 2017-11-04 stsp {
93 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
94 4027f31a 2017-11-04 stsp }
95 4027f31a 2017-11-04 stsp
96 4027f31a 2017-11-04 stsp static char *
97 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
98 4027f31a 2017-11-04 stsp {
99 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
100 4027f31a 2017-11-04 stsp }
101 4027f31a 2017-11-04 stsp
102 4027f31a 2017-11-04 stsp static int
103 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
104 4027f31a 2017-11-04 stsp {
105 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
106 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
107 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
108 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
109 deeca238 2018-03-12 stsp int ret = 0;
110 deeca238 2018-03-12 stsp struct stat sb;
111 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
112 4027f31a 2017-11-04 stsp
113 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
114 deeca238 2018-03-12 stsp goto done;
115 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
116 deeca238 2018-03-12 stsp goto done;
117 4027f31a 2017-11-04 stsp
118 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
119 deeca238 2018-03-12 stsp goto done;
120 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
121 deeca238 2018-03-12 stsp goto done;
122 deeca238 2018-03-12 stsp
123 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
124 deeca238 2018-03-12 stsp goto done;
125 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
126 deeca238 2018-03-12 stsp goto done;
127 deeca238 2018-03-12 stsp
128 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
129 deeca238 2018-03-12 stsp goto done;
130 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
131 deeca238 2018-03-12 stsp goto done;
132 4847cca1 2018-03-12 stsp
133 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
134 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
135 4847cca1 2018-03-12 stsp goto done;
136 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
137 4847cca1 2018-03-12 stsp
138 deeca238 2018-03-12 stsp ret = 1;
139 deeca238 2018-03-12 stsp done:
140 4027f31a 2017-11-04 stsp free(path_git);
141 4027f31a 2017-11-04 stsp free(path_objects);
142 4027f31a 2017-11-04 stsp free(path_refs);
143 4027f31a 2017-11-04 stsp free(path_head);
144 4027f31a 2017-11-04 stsp return ret;
145 4027f31a 2017-11-04 stsp
146 4027f31a 2017-11-04 stsp }
147 4027f31a 2017-11-04 stsp
148 4027f31a 2017-11-04 stsp const struct got_error *
149 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
150 4027f31a 2017-11-04 stsp {
151 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
152 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
153 2393f13b 2018-03-09 stsp char *abspath;
154 4027f31a 2017-11-04 stsp
155 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
156 2393f13b 2018-03-09 stsp abspath = strdup(path);
157 2393f13b 2018-03-09 stsp else
158 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
159 92af5469 2017-11-05 stsp if (abspath == NULL)
160 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
161 4027f31a 2017-11-04 stsp
162 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
163 92af5469 2017-11-05 stsp if (repo == NULL) {
164 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
165 92af5469 2017-11-05 stsp goto done;
166 92af5469 2017-11-05 stsp }
167 4027f31a 2017-11-04 stsp
168 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
169 92af5469 2017-11-05 stsp if (repo->path == NULL) {
170 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
171 92af5469 2017-11-05 stsp goto done;
172 92af5469 2017-11-05 stsp }
173 4027f31a 2017-11-04 stsp
174 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
175 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
176 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
177 92af5469 2017-11-05 stsp goto done;
178 92af5469 2017-11-05 stsp }
179 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
180 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
181 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
182 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
183 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
184 4986b9d5 2018-03-12 stsp goto done;
185 4986b9d5 2018-03-12 stsp }
186 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
187 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
188 4986b9d5 2018-03-12 stsp goto done;
189 4986b9d5 2018-03-12 stsp }
190 4986b9d5 2018-03-12 stsp }
191 4027f31a 2017-11-04 stsp
192 4027f31a 2017-11-04 stsp *ret = repo;
193 92af5469 2017-11-05 stsp done:
194 92af5469 2017-11-05 stsp if (err)
195 4986b9d5 2018-03-12 stsp got_repo_close(repo);
196 92af5469 2017-11-05 stsp free(abspath);
197 92af5469 2017-11-05 stsp return err;
198 4027f31a 2017-11-04 stsp }
199 4027f31a 2017-11-04 stsp
200 4027f31a 2017-11-04 stsp void
201 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
202 4027f31a 2017-11-04 stsp {
203 79b11c62 2018-03-09 stsp int i;
204 79b11c62 2018-03-09 stsp
205 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
206 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
207 79b11c62 2018-03-09 stsp break;
208 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
209 79b11c62 2018-03-09 stsp }
210 bd1223b9 2018-03-14 stsp
211 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
212 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
213 7e656b93 2018-03-17 stsp break;
214 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
215 7e656b93 2018-03-17 stsp }
216 7e656b93 2018-03-17 stsp
217 4027f31a 2017-11-04 stsp free(repo->path);
218 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
219 4027f31a 2017-11-04 stsp free(repo);
220 4027f31a 2017-11-04 stsp }