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 4027f31a 2017-11-04 stsp #include "got_refs.h"
29 4027f31a 2017-11-04 stsp #include "got_repository.h"
30 4027f31a 2017-11-04 stsp
31 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
32 32cb896c 2018-03-11 stsp #include "got_repository_lib.h"
33 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
34 32cb896c 2018-03-11 stsp #include "got_delta_lib.h"
35 32cb896c 2018-03-11 stsp #include "got_object_lib.h"
36 32cb896c 2018-03-11 stsp #include "got_pack_lib.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 4027f31a 2017-11-04 stsp char *path_git;
64 4027f31a 2017-11-04 stsp
65 4027f31a 2017-11-04 stsp if (asprintf(&path_git, "%s/%s", repo->path, GOT_GIT_DIR) == -1)
66 4027f31a 2017-11-04 stsp return NULL;
67 4027f31a 2017-11-04 stsp
68 4027f31a 2017-11-04 stsp return path_git;
69 4027f31a 2017-11-04 stsp }
70 4027f31a 2017-11-04 stsp
71 4027f31a 2017-11-04 stsp static char *
72 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
73 4027f31a 2017-11-04 stsp {
74 4027f31a 2017-11-04 stsp char *path_child;
75 4027f31a 2017-11-04 stsp
76 4027f31a 2017-11-04 stsp if (asprintf(&path_child, "%s/%s/%s", repo->path, GOT_GIT_DIR,
77 4027f31a 2017-11-04 stsp basename) == -1)
78 4027f31a 2017-11-04 stsp return NULL;
79 4027f31a 2017-11-04 stsp
80 4027f31a 2017-11-04 stsp return path_child;
81 4027f31a 2017-11-04 stsp }
82 4027f31a 2017-11-04 stsp
83 11995603 2017-11-05 stsp char *
84 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
85 4027f31a 2017-11-04 stsp {
86 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
87 4027f31a 2017-11-04 stsp }
88 4027f31a 2017-11-04 stsp
89 11995603 2017-11-05 stsp char *
90 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
91 a1fd68d8 2018-01-12 stsp {
92 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
93 a1fd68d8 2018-01-12 stsp }
94 a1fd68d8 2018-01-12 stsp
95 a1fd68d8 2018-01-12 stsp char *
96 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
97 4027f31a 2017-11-04 stsp {
98 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
99 4027f31a 2017-11-04 stsp }
100 4027f31a 2017-11-04 stsp
101 4027f31a 2017-11-04 stsp static char *
102 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
103 4027f31a 2017-11-04 stsp {
104 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
105 4027f31a 2017-11-04 stsp }
106 4027f31a 2017-11-04 stsp
107 4027f31a 2017-11-04 stsp static int
108 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
109 4027f31a 2017-11-04 stsp {
110 11995603 2017-11-05 stsp char *path_git = got_repo_get_path_git_dir(repo);
111 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
112 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
113 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
114 deeca238 2018-03-12 stsp int ret = 0;
115 deeca238 2018-03-12 stsp struct stat sb;
116 4027f31a 2017-11-04 stsp
117 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
118 deeca238 2018-03-12 stsp goto done;
119 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
120 deeca238 2018-03-12 stsp goto done;
121 4027f31a 2017-11-04 stsp
122 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
123 deeca238 2018-03-12 stsp goto done;
124 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
125 deeca238 2018-03-12 stsp goto done;
126 deeca238 2018-03-12 stsp
127 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
128 deeca238 2018-03-12 stsp goto done;
129 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
130 deeca238 2018-03-12 stsp goto done;
131 deeca238 2018-03-12 stsp
132 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
133 deeca238 2018-03-12 stsp goto done;
134 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
135 deeca238 2018-03-12 stsp goto done;
136 deeca238 2018-03-12 stsp ret = 1;
137 deeca238 2018-03-12 stsp done:
138 4027f31a 2017-11-04 stsp free(path_git);
139 4027f31a 2017-11-04 stsp free(path_objects);
140 4027f31a 2017-11-04 stsp free(path_refs);
141 4027f31a 2017-11-04 stsp free(path_head);
142 4027f31a 2017-11-04 stsp return ret;
143 4027f31a 2017-11-04 stsp
144 4027f31a 2017-11-04 stsp }
145 4027f31a 2017-11-04 stsp
146 4027f31a 2017-11-04 stsp const struct got_error *
147 92af5469 2017-11-05 stsp got_repo_open(struct got_repository **ret, const char *path)
148 4027f31a 2017-11-04 stsp {
149 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
150 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
151 2393f13b 2018-03-09 stsp char *abspath;
152 4027f31a 2017-11-04 stsp
153 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
154 2393f13b 2018-03-09 stsp abspath = strdup(path);
155 2393f13b 2018-03-09 stsp else
156 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
157 92af5469 2017-11-05 stsp if (abspath == NULL)
158 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
159 4027f31a 2017-11-04 stsp
160 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
161 92af5469 2017-11-05 stsp if (repo == NULL) {
162 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NO_MEM);
163 92af5469 2017-11-05 stsp goto done;
164 92af5469 2017-11-05 stsp }
165 4027f31a 2017-11-04 stsp
166 4027f31a 2017-11-04 stsp repo->path = got_path_normalize(abspath);
167 92af5469 2017-11-05 stsp if (repo->path == NULL) {
168 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
169 92af5469 2017-11-05 stsp goto done;
170 92af5469 2017-11-05 stsp }
171 4027f31a 2017-11-04 stsp
172 92af5469 2017-11-05 stsp if (!is_git_repo(repo)) {
173 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
174 92af5469 2017-11-05 stsp goto done;
175 92af5469 2017-11-05 stsp }
176 4027f31a 2017-11-04 stsp
177 4027f31a 2017-11-04 stsp *ret = repo;
178 92af5469 2017-11-05 stsp done:
179 92af5469 2017-11-05 stsp if (err)
180 92af5469 2017-11-05 stsp free(repo);
181 92af5469 2017-11-05 stsp free(abspath);
182 92af5469 2017-11-05 stsp return err;
183 4027f31a 2017-11-04 stsp }
184 4027f31a 2017-11-04 stsp
185 4027f31a 2017-11-04 stsp void
186 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
187 4027f31a 2017-11-04 stsp {
188 79b11c62 2018-03-09 stsp int i;
189 79b11c62 2018-03-09 stsp
190 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
191 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
192 79b11c62 2018-03-09 stsp break;
193 79b11c62 2018-03-09 stsp got_packidx_close(repo->packidx_cache[i]);
194 79b11c62 2018-03-09 stsp }
195 4027f31a 2017-11-04 stsp free(repo->path);
196 4027f31a 2017-11-04 stsp free(repo);
197 4027f31a 2017-11-04 stsp }