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 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
31 4027f31a 2017-11-04 stsp
32 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
33 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
38 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
39 c3f94f68 2017-11-05 stsp
40 79b11c62 2018-03-09 stsp #ifndef nitems
41 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 79b11c62 2018-03-09 stsp #endif
43 3b339b2f 2018-02-12 stsp
44 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
45 4027f31a 2017-11-04 stsp
46 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
47 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
48 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
49 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
50 4027f31a 2017-11-04 stsp
51 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
52 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
53 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
54 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
55 4df642d9 2017-11-05 stsp
56 11995603 2017-11-05 stsp char *
57 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
58 86c3caaf 2018-03-09 stsp {
59 86c3caaf 2018-03-09 stsp return strdup(repo->path);
60 86c3caaf 2018-03-09 stsp }
61 86c3caaf 2018-03-09 stsp
62 86c3caaf 2018-03-09 stsp char *
63 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
64 4027f31a 2017-11-04 stsp {
65 4986b9d5 2018-03-12 stsp return strdup(repo->path_git_dir);
66 4027f31a 2017-11-04 stsp }
67 4027f31a 2017-11-04 stsp
68 4027f31a 2017-11-04 stsp static char *
69 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
70 4027f31a 2017-11-04 stsp {
71 4027f31a 2017-11-04 stsp char *path_child;
72 4027f31a 2017-11-04 stsp
73 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
74 4027f31a 2017-11-04 stsp basename) == -1)
75 4027f31a 2017-11-04 stsp return NULL;
76 4027f31a 2017-11-04 stsp
77 4027f31a 2017-11-04 stsp return path_child;
78 4027f31a 2017-11-04 stsp }
79 4027f31a 2017-11-04 stsp
80 11995603 2017-11-05 stsp char *
81 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
82 4027f31a 2017-11-04 stsp {
83 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
84 4027f31a 2017-11-04 stsp }
85 4027f31a 2017-11-04 stsp
86 11995603 2017-11-05 stsp char *
87 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
88 a1fd68d8 2018-01-12 stsp {
89 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
90 a1fd68d8 2018-01-12 stsp }
91 a1fd68d8 2018-01-12 stsp
92 a1fd68d8 2018-01-12 stsp char *
93 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
94 4027f31a 2017-11-04 stsp {
95 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
96 4027f31a 2017-11-04 stsp }
97 4027f31a 2017-11-04 stsp
98 4027f31a 2017-11-04 stsp static char *
99 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
100 4027f31a 2017-11-04 stsp {
101 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
102 4027f31a 2017-11-04 stsp }
103 4027f31a 2017-11-04 stsp
104 4027f31a 2017-11-04 stsp static int
105 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
106 4027f31a 2017-11-04 stsp {
107 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
108 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
109 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
110 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
111 deeca238 2018-03-12 stsp int ret = 0;
112 deeca238 2018-03-12 stsp struct stat sb;
113 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
114 4027f31a 2017-11-04 stsp
115 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
116 deeca238 2018-03-12 stsp goto done;
117 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
118 deeca238 2018-03-12 stsp goto done;
119 4027f31a 2017-11-04 stsp
120 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
121 deeca238 2018-03-12 stsp goto done;
122 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
123 deeca238 2018-03-12 stsp goto done;
124 deeca238 2018-03-12 stsp
125 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
126 deeca238 2018-03-12 stsp goto done;
127 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
128 deeca238 2018-03-12 stsp goto done;
129 deeca238 2018-03-12 stsp
130 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
131 deeca238 2018-03-12 stsp goto done;
132 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
133 deeca238 2018-03-12 stsp goto done;
134 4847cca1 2018-03-12 stsp
135 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
136 4847cca1 2018-03-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD) != NULL)
137 4847cca1 2018-03-12 stsp goto done;
138 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
139 4847cca1 2018-03-12 stsp
140 deeca238 2018-03-12 stsp ret = 1;
141 deeca238 2018-03-12 stsp done:
142 4027f31a 2017-11-04 stsp free(path_git);
143 4027f31a 2017-11-04 stsp free(path_objects);
144 4027f31a 2017-11-04 stsp free(path_refs);
145 4027f31a 2017-11-04 stsp free(path_head);
146 4027f31a 2017-11-04 stsp return ret;
147 4027f31a 2017-11-04 stsp
148 4027f31a 2017-11-04 stsp }
149 4027f31a 2017-11-04 stsp
150 4027f31a 2017-11-04 stsp const struct got_error *
151 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
152 4027f31a 2017-11-04 stsp {
153 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
154 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
155 2393f13b 2018-03-09 stsp char *abspath;
156 4027f31a 2017-11-04 stsp
157 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
158 2393f13b 2018-03-09 stsp abspath = strdup(path);
159 2393f13b 2018-03-09 stsp else
160 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
161 92af5469 2017-11-05 stsp if (abspath == NULL)
162 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
163 4027f31a 2017-11-04 stsp
164 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
165 92af5469 2017-11-05 stsp if (repo == NULL) {
166 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
167 92af5469 2017-11-05 stsp goto done;
168 92af5469 2017-11-05 stsp }
169 4027f31a 2017-11-04 stsp
170 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
171 92af5469 2017-11-05 stsp if (repo->path == NULL) {
172 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
173 92af5469 2017-11-05 stsp goto done;
174 92af5469 2017-11-05 stsp }
175 4027f31a 2017-11-04 stsp
176 4986b9d5 2018-03-12 stsp repo->path_git_dir = strdup(repo->path);
177 4986b9d5 2018-03-12 stsp if (repo->path_git_dir == NULL) {
178 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
179 92af5469 2017-11-05 stsp goto done;
180 92af5469 2017-11-05 stsp }
181 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
182 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
183 4986b9d5 2018-03-12 stsp if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
184 4986b9d5 2018-03-12 stsp GOT_GIT_DIR) == -1) {
185 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
186 4986b9d5 2018-03-12 stsp goto done;
187 4986b9d5 2018-03-12 stsp }
188 4986b9d5 2018-03-12 stsp if (!is_git_repo(repo)) {
189 442a3ddc 2018-04-23 stsp struct got_worktree *worktree;
190 442a3ddc 2018-04-23 stsp if (got_worktree_open(&worktree, repo->path) == NULL) {
191 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
192 442a3ddc 2018-04-23 stsp repo->path_git_dir =
193 442a3ddc 2018-04-23 stsp strdup(worktree->repo_path);
194 442a3ddc 2018-04-23 stsp if (repo->path_git_dir == NULL) {
195 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
196 442a3ddc 2018-04-23 stsp goto done;
197 442a3ddc 2018-04-23 stsp }
198 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
199 442a3ddc 2018-04-23 stsp free(repo->path_git_dir);
200 442a3ddc 2018-04-23 stsp if (asprintf(&repo->path_git_dir,
201 442a3ddc 2018-04-23 stsp "%s/%s", worktree->repo_path,
202 442a3ddc 2018-04-23 stsp GOT_GIT_DIR) == -1) {
203 442a3ddc 2018-04-23 stsp err = got_error_from_errno();
204 442a3ddc 2018-04-23 stsp goto done;
205 442a3ddc 2018-04-23 stsp }
206 442a3ddc 2018-04-23 stsp }
207 442a3ddc 2018-04-23 stsp got_worktree_close(worktree);
208 442a3ddc 2018-04-23 stsp }
209 442a3ddc 2018-04-23 stsp }
210 442a3ddc 2018-04-23 stsp if (!is_git_repo(repo)) {
211 4986b9d5 2018-03-12 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
212 4986b9d5 2018-03-12 stsp goto done;
213 4986b9d5 2018-03-12 stsp }
214 4986b9d5 2018-03-12 stsp }
215 4027f31a 2017-11-04 stsp
216 4027f31a 2017-11-04 stsp *ret = repo;
217 92af5469 2017-11-05 stsp done:
218 92af5469 2017-11-05 stsp if (err)
219 4986b9d5 2018-03-12 stsp got_repo_close(repo);
220 92af5469 2017-11-05 stsp free(abspath);
221 92af5469 2017-11-05 stsp return err;
222 4027f31a 2017-11-04 stsp }
223 4027f31a 2017-11-04 stsp
224 4027f31a 2017-11-04 stsp void
225 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
226 4027f31a 2017-11-04 stsp {
227 79b11c62 2018-03-09 stsp int i;
228 79b11c62 2018-03-09 stsp
229 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
230 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
231 79b11c62 2018-03-09 stsp break;
232 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
233 79b11c62 2018-03-09 stsp }
234 bd1223b9 2018-03-14 stsp
235 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
236 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
237 7e656b93 2018-03-17 stsp break;
238 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
239 7e656b93 2018-03-17 stsp }
240 7e656b93 2018-03-17 stsp
241 4027f31a 2017-11-04 stsp free(repo->path);
242 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
243 4027f31a 2017-11-04 stsp free(repo);
244 4027f31a 2017-11-04 stsp }