Blame


1 20662ea0 2021-04-10 stsp /*
2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 20662ea0 2021-04-10 stsp *
4 20662ea0 2021-04-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 20662ea0 2021-04-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 20662ea0 2021-04-10 stsp * copyright notice and this permission notice appear in all copies.
7 20662ea0 2021-04-10 stsp *
8 20662ea0 2021-04-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 20662ea0 2021-04-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 20662ea0 2021-04-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 20662ea0 2021-04-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 20662ea0 2021-04-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 20662ea0 2021-04-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 20662ea0 2021-04-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 20662ea0 2021-04-10 stsp */
16 20662ea0 2021-04-10 stsp
17 20662ea0 2021-04-10 stsp #include <sys/queue.h>
18 05118f5a 2021-06-22 stsp #include <sys/types.h>
19 20662ea0 2021-04-10 stsp
20 05118f5a 2021-06-22 stsp #include <ctype.h>
21 20662ea0 2021-04-10 stsp #include <getopt.h>
22 20662ea0 2021-04-10 stsp #include <err.h>
23 20662ea0 2021-04-10 stsp #include <errno.h>
24 20662ea0 2021-04-10 stsp #include <locale.h>
25 05118f5a 2021-06-22 stsp #include <inttypes.h>
26 05118f5a 2021-06-22 stsp #include <sha1.h>
27 20662ea0 2021-04-10 stsp #include <stdio.h>
28 20662ea0 2021-04-10 stsp #include <stdlib.h>
29 20662ea0 2021-04-10 stsp #include <signal.h>
30 20662ea0 2021-04-10 stsp #include <string.h>
31 20662ea0 2021-04-10 stsp #include <unistd.h>
32 20662ea0 2021-04-10 stsp #include <util.h>
33 20662ea0 2021-04-10 stsp
34 20662ea0 2021-04-10 stsp #include "got_version.h"
35 20662ea0 2021-04-10 stsp #include "got_error.h"
36 20662ea0 2021-04-10 stsp #include "got_object.h"
37 20662ea0 2021-04-10 stsp #include "got_reference.h"
38 05118f5a 2021-06-22 stsp #include "got_cancel.h"
39 20662ea0 2021-04-10 stsp #include "got_repository.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
42 20662ea0 2021-04-10 stsp #include "got_path.h"
43 20662ea0 2021-04-10 stsp #include "got_privsep.h"
44 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
45 20662ea0 2021-04-10 stsp
46 20662ea0 2021-04-10 stsp #ifndef nitems
47 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 20662ea0 2021-04-10 stsp #endif
49 20662ea0 2021-04-10 stsp
50 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
51 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
52 20662ea0 2021-04-10 stsp
53 20662ea0 2021-04-10 stsp static void
54 20662ea0 2021-04-10 stsp catch_sigint(int signo)
55 20662ea0 2021-04-10 stsp {
56 20662ea0 2021-04-10 stsp sigint_received = 1;
57 20662ea0 2021-04-10 stsp }
58 20662ea0 2021-04-10 stsp
59 20662ea0 2021-04-10 stsp static void
60 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
61 20662ea0 2021-04-10 stsp {
62 20662ea0 2021-04-10 stsp sigpipe_received = 1;
63 20662ea0 2021-04-10 stsp }
64 20662ea0 2021-04-10 stsp
65 05118f5a 2021-06-22 stsp static const struct got_error *
66 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
67 05118f5a 2021-06-22 stsp {
68 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
69 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
70 05118f5a 2021-06-22 stsp return NULL;
71 05118f5a 2021-06-22 stsp }
72 20662ea0 2021-04-10 stsp
73 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
74 20662ea0 2021-04-10 stsp const char *cmd_name;
75 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
77 20662ea0 2021-04-10 stsp const char *cmd_alias;
78 20662ea0 2021-04-10 stsp };
79 20662ea0 2021-04-10 stsp
80 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
81 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
82 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
83 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
84 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
85 20662ea0 2021-04-10 stsp
86 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
87 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
88 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
89 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
90 20662ea0 2021-04-10 stsp
91 20662ea0 2021-04-10 stsp static struct gotadmin_cmd gotadmin_commands[] = {
92 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
93 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
94 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
95 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
96 20662ea0 2021-04-10 stsp };
97 20662ea0 2021-04-10 stsp
98 20662ea0 2021-04-10 stsp static void
99 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
100 20662ea0 2021-04-10 stsp {
101 20662ea0 2021-04-10 stsp size_t i;
102 20662ea0 2021-04-10 stsp
103 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
104 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
105 20662ea0 2021-04-10 stsp struct gotadmin_cmd *cmd = &gotadmin_commands[i];
106 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
107 20662ea0 2021-04-10 stsp }
108 20662ea0 2021-04-10 stsp fputc('\n', fp);
109 20662ea0 2021-04-10 stsp }
110 20662ea0 2021-04-10 stsp
111 20662ea0 2021-04-10 stsp int
112 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
113 20662ea0 2021-04-10 stsp {
114 20662ea0 2021-04-10 stsp struct gotadmin_cmd *cmd;
115 20662ea0 2021-04-10 stsp size_t i;
116 20662ea0 2021-04-10 stsp int ch;
117 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
118 20662ea0 2021-04-10 stsp static struct option longopts[] = {
119 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
120 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
121 20662ea0 2021-04-10 stsp };
122 20662ea0 2021-04-10 stsp
123 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
124 20662ea0 2021-04-10 stsp
125 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
126 20662ea0 2021-04-10 stsp switch (ch) {
127 20662ea0 2021-04-10 stsp case 'h':
128 20662ea0 2021-04-10 stsp hflag = 1;
129 20662ea0 2021-04-10 stsp break;
130 20662ea0 2021-04-10 stsp case 'V':
131 20662ea0 2021-04-10 stsp Vflag = 1;
132 20662ea0 2021-04-10 stsp break;
133 20662ea0 2021-04-10 stsp default:
134 20662ea0 2021-04-10 stsp usage(hflag, 1);
135 20662ea0 2021-04-10 stsp /* NOTREACHED */
136 20662ea0 2021-04-10 stsp }
137 20662ea0 2021-04-10 stsp }
138 20662ea0 2021-04-10 stsp
139 20662ea0 2021-04-10 stsp argc -= optind;
140 20662ea0 2021-04-10 stsp argv += optind;
141 20662ea0 2021-04-10 stsp optind = 1;
142 20662ea0 2021-04-10 stsp optreset = 1;
143 20662ea0 2021-04-10 stsp
144 20662ea0 2021-04-10 stsp if (Vflag) {
145 20662ea0 2021-04-10 stsp got_version_print_str();
146 20662ea0 2021-04-10 stsp return 0;
147 20662ea0 2021-04-10 stsp }
148 20662ea0 2021-04-10 stsp
149 20662ea0 2021-04-10 stsp if (argc <= 0)
150 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
151 20662ea0 2021-04-10 stsp
152 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
153 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
154 20662ea0 2021-04-10 stsp
155 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
156 20662ea0 2021-04-10 stsp const struct got_error *error;
157 20662ea0 2021-04-10 stsp
158 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
159 20662ea0 2021-04-10 stsp
160 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
161 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
162 20662ea0 2021-04-10 stsp continue;
163 20662ea0 2021-04-10 stsp
164 20662ea0 2021-04-10 stsp if (hflag)
165 20662ea0 2021-04-10 stsp gotadmin_commands[i].cmd_usage();
166 20662ea0 2021-04-10 stsp
167 20662ea0 2021-04-10 stsp error = gotadmin_commands[i].cmd_main(argc, argv);
168 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
169 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
170 20662ea0 2021-04-10 stsp !(sigpipe_received &&
171 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
172 20662ea0 2021-04-10 stsp !(sigint_received &&
173 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
174 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
175 20662ea0 2021-04-10 stsp return 1;
176 20662ea0 2021-04-10 stsp }
177 20662ea0 2021-04-10 stsp
178 20662ea0 2021-04-10 stsp return 0;
179 20662ea0 2021-04-10 stsp }
180 20662ea0 2021-04-10 stsp
181 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 20662ea0 2021-04-10 stsp list_commands(stderr);
183 20662ea0 2021-04-10 stsp return 1;
184 20662ea0 2021-04-10 stsp }
185 20662ea0 2021-04-10 stsp
186 20662ea0 2021-04-10 stsp __dead static void
187 20662ea0 2021-04-10 stsp usage(int hflag, int status)
188 20662ea0 2021-04-10 stsp {
189 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
190 20662ea0 2021-04-10 stsp
191 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
192 20662ea0 2021-04-10 stsp getprogname());
193 20662ea0 2021-04-10 stsp if (hflag)
194 20662ea0 2021-04-10 stsp list_commands(fp);
195 20662ea0 2021-04-10 stsp exit(status);
196 20662ea0 2021-04-10 stsp }
197 20662ea0 2021-04-10 stsp
198 20662ea0 2021-04-10 stsp static const struct got_error *
199 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
200 20662ea0 2021-04-10 stsp {
201 20662ea0 2021-04-10 stsp const struct got_error *err;
202 20662ea0 2021-04-10 stsp
203 20662ea0 2021-04-10 stsp #ifdef PROFILE
204 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
205 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
206 20662ea0 2021-04-10 stsp #endif
207 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
208 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
209 20662ea0 2021-04-10 stsp
210 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
211 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
212 20662ea0 2021-04-10 stsp
213 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
214 20662ea0 2021-04-10 stsp if (err != NULL)
215 20662ea0 2021-04-10 stsp return err;
216 20662ea0 2021-04-10 stsp
217 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
218 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
219 20662ea0 2021-04-10 stsp
220 20662ea0 2021-04-10 stsp return NULL;
221 20662ea0 2021-04-10 stsp }
222 20662ea0 2021-04-10 stsp
223 20662ea0 2021-04-10 stsp __dead static void
224 20662ea0 2021-04-10 stsp usage_info(void)
225 20662ea0 2021-04-10 stsp {
226 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
227 20662ea0 2021-04-10 stsp getprogname());
228 20662ea0 2021-04-10 stsp exit(1);
229 20662ea0 2021-04-10 stsp }
230 20662ea0 2021-04-10 stsp
231 20662ea0 2021-04-10 stsp static const struct got_error *
232 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
233 20662ea0 2021-04-10 stsp {
234 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
235 20662ea0 2021-04-10 stsp char *cwd = NULL, *repo_path = NULL;
236 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
237 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
238 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
239 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
240 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
241 20662ea0 2021-04-10 stsp
242 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
243 20662ea0 2021-04-10 stsp switch (ch) {
244 20662ea0 2021-04-10 stsp case 'r':
245 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
246 20662ea0 2021-04-10 stsp if (repo_path == NULL)
247 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
248 20662ea0 2021-04-10 stsp optarg);
249 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
250 20662ea0 2021-04-10 stsp break;
251 20662ea0 2021-04-10 stsp default:
252 20662ea0 2021-04-10 stsp usage_info();
253 20662ea0 2021-04-10 stsp /* NOTREACHED */
254 20662ea0 2021-04-10 stsp }
255 20662ea0 2021-04-10 stsp }
256 20662ea0 2021-04-10 stsp
257 20662ea0 2021-04-10 stsp argc -= optind;
258 20662ea0 2021-04-10 stsp argv += optind;
259 20662ea0 2021-04-10 stsp
260 20662ea0 2021-04-10 stsp #ifndef PROFILE
261 20662ea0 2021-04-10 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
262 20662ea0 2021-04-10 stsp NULL) == -1)
263 20662ea0 2021-04-10 stsp err(1, "pledge");
264 20662ea0 2021-04-10 stsp #endif
265 20662ea0 2021-04-10 stsp cwd = getcwd(NULL, 0);
266 20662ea0 2021-04-10 stsp if (cwd == NULL) {
267 20662ea0 2021-04-10 stsp error = got_error_from_errno("getcwd");
268 20662ea0 2021-04-10 stsp goto done;
269 20662ea0 2021-04-10 stsp }
270 20662ea0 2021-04-10 stsp
271 20662ea0 2021-04-10 stsp error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
272 20662ea0 2021-04-10 stsp if (error)
273 20662ea0 2021-04-10 stsp goto done;
274 20662ea0 2021-04-10 stsp
275 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
276 20662ea0 2021-04-10 stsp if (error)
277 20662ea0 2021-04-10 stsp goto done;
278 20662ea0 2021-04-10 stsp
279 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
280 20662ea0 2021-04-10 stsp
281 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
282 20662ea0 2021-04-10 stsp if (gotconfig) {
283 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
284 20662ea0 2021-04-10 stsp int i, nremotes;
285 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
286 20662ea0 2021-04-10 stsp printf("default author: %s\n",
287 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
288 20662ea0 2021-04-10 stsp }
289 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
290 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
291 20662ea0 2021-04-10 stsp printf("remote \"%s\": %s\n", remotes[i].name,
292 20662ea0 2021-04-10 stsp remotes[i].url);
293 20662ea0 2021-04-10 stsp }
294 20662ea0 2021-04-10 stsp }
295 20662ea0 2021-04-10 stsp
296 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
297 20662ea0 2021-04-10 stsp &packsize, repo);
298 20662ea0 2021-04-10 stsp if (error)
299 20662ea0 2021-04-10 stsp goto done;
300 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
301 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
302 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
303 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
304 20662ea0 2021-04-10 stsp goto done;
305 20662ea0 2021-04-10 stsp }
306 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
307 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
308 20662ea0 2021-04-10 stsp }
309 20662ea0 2021-04-10 stsp
310 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
311 20662ea0 2021-04-10 stsp if (error)
312 20662ea0 2021-04-10 stsp goto done;
313 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
314 20662ea0 2021-04-10 stsp if (nobj > 0) {
315 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
316 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
317 20662ea0 2021-04-10 stsp goto done;
318 20662ea0 2021-04-10 stsp }
319 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
320 20662ea0 2021-04-10 stsp }
321 20662ea0 2021-04-10 stsp done:
322 20662ea0 2021-04-10 stsp if (repo)
323 20662ea0 2021-04-10 stsp got_repo_close(repo);
324 20662ea0 2021-04-10 stsp free(cwd);
325 20662ea0 2021-04-10 stsp return error;
326 05118f5a 2021-06-22 stsp }
327 05118f5a 2021-06-22 stsp
328 05118f5a 2021-06-22 stsp __dead static void
329 05118f5a 2021-06-22 stsp usage_pack(void)
330 05118f5a 2021-06-22 stsp {
331 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
332 05118f5a 2021-06-22 stsp "[-x reference] [reference ...]\n",
333 05118f5a 2021-06-22 stsp getprogname());
334 05118f5a 2021-06-22 stsp exit(1);
335 05118f5a 2021-06-22 stsp }
336 05118f5a 2021-06-22 stsp
337 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
338 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
339 05118f5a 2021-06-22 stsp int last_ncommits;
340 05118f5a 2021-06-22 stsp int last_nobj_total;
341 05118f5a 2021-06-22 stsp int last_p_deltify;
342 05118f5a 2021-06-22 stsp int last_p_written;
343 05118f5a 2021-06-22 stsp int last_p_indexed;
344 05118f5a 2021-06-22 stsp int last_p_resolved;
345 05118f5a 2021-06-22 stsp int verbosity;
346 05118f5a 2021-06-22 stsp int printed_something;
347 05118f5a 2021-06-22 stsp };
348 05118f5a 2021-06-22 stsp
349 05118f5a 2021-06-22 stsp static const struct got_error *
350 05118f5a 2021-06-22 stsp pack_progress(void *arg, off_t packfile_size, int ncommits,
351 05118f5a 2021-06-22 stsp int nobj_total, int nobj_deltify, int nobj_written)
352 05118f5a 2021-06-22 stsp {
353 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
354 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
355 05118f5a 2021-06-22 stsp int p_deltify, p_written;
356 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
357 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
358 05118f5a 2021-06-22 stsp
359 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
360 05118f5a 2021-06-22 stsp return NULL;
361 05118f5a 2021-06-22 stsp
362 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
363 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
364 05118f5a 2021-06-22 stsp
365 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
366 05118f5a 2021-06-22 stsp print_searching = 1;
367 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
368 05118f5a 2021-06-22 stsp }
369 05118f5a 2021-06-22 stsp
370 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
371 05118f5a 2021-06-22 stsp print_searching = 1;
372 05118f5a 2021-06-22 stsp print_total = 1;
373 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
374 05118f5a 2021-06-22 stsp }
375 05118f5a 2021-06-22 stsp
376 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
377 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
378 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
379 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
380 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
381 05118f5a 2021-06-22 stsp }
382 05118f5a 2021-06-22 stsp
383 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
384 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
385 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
386 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
387 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
388 05118f5a 2021-06-22 stsp print_searching = 1;
389 05118f5a 2021-06-22 stsp print_total = 1;
390 05118f5a 2021-06-22 stsp print_deltify = 1;
391 05118f5a 2021-06-22 stsp }
392 05118f5a 2021-06-22 stsp }
393 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
394 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
395 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
396 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
397 05118f5a 2021-06-22 stsp print_searching = 1;
398 05118f5a 2021-06-22 stsp print_total = 1;
399 05118f5a 2021-06-22 stsp print_deltify = 1;
400 05118f5a 2021-06-22 stsp print_written = 1;
401 05118f5a 2021-06-22 stsp }
402 05118f5a 2021-06-22 stsp }
403 05118f5a 2021-06-22 stsp }
404 05118f5a 2021-06-22 stsp
405 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
406 05118f5a 2021-06-22 stsp printf("\r");
407 05118f5a 2021-06-22 stsp if (print_searching)
408 05118f5a 2021-06-22 stsp printf("packing %d reference%s", ncommits,
409 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
410 05118f5a 2021-06-22 stsp if (print_total)
411 05118f5a 2021-06-22 stsp printf("; %d object%s", nobj_total,
412 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
413 05118f5a 2021-06-22 stsp if (print_deltify)
414 05118f5a 2021-06-22 stsp printf("; deltify: %d%%", p_deltify);
415 05118f5a 2021-06-22 stsp if (print_written)
416 05118f5a 2021-06-22 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
417 05118f5a 2021-06-22 stsp scaled_size, p_written);
418 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
419 05118f5a 2021-06-22 stsp print_written) {
420 05118f5a 2021-06-22 stsp a->printed_something = 1;
421 05118f5a 2021-06-22 stsp fflush(stdout);
422 05118f5a 2021-06-22 stsp }
423 05118f5a 2021-06-22 stsp return NULL;
424 05118f5a 2021-06-22 stsp }
425 05118f5a 2021-06-22 stsp
426 05118f5a 2021-06-22 stsp static const struct got_error *
427 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
428 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
429 05118f5a 2021-06-22 stsp {
430 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
431 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
432 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
433 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
434 05118f5a 2021-06-22 stsp
435 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
436 05118f5a 2021-06-22 stsp return NULL;
437 05118f5a 2021-06-22 stsp
438 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
439 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
440 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
441 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
442 05118f5a 2021-06-22 stsp print_size = 1;
443 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
444 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
445 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
446 05118f5a 2021-06-22 stsp }
447 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
448 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
449 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
450 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
451 05118f5a 2021-06-22 stsp print_indexed = 1;
452 05118f5a 2021-06-22 stsp print_size = 1;
453 05118f5a 2021-06-22 stsp }
454 05118f5a 2021-06-22 stsp }
455 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
456 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
457 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
458 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
459 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
460 05118f5a 2021-06-22 stsp print_resolved = 1;
461 05118f5a 2021-06-22 stsp print_indexed = 1;
462 05118f5a 2021-06-22 stsp print_size = 1;
463 05118f5a 2021-06-22 stsp }
464 05118f5a 2021-06-22 stsp }
465 05118f5a 2021-06-22 stsp
466 05118f5a 2021-06-22 stsp }
467 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
468 05118f5a 2021-06-22 stsp printf("\r");
469 05118f5a 2021-06-22 stsp if (print_size)
470 05118f5a 2021-06-22 stsp printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
471 05118f5a 2021-06-22 stsp if (print_indexed)
472 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
473 05118f5a 2021-06-22 stsp if (print_resolved)
474 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
475 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
476 05118f5a 2021-06-22 stsp fflush(stdout);
477 05118f5a 2021-06-22 stsp
478 05118f5a 2021-06-22 stsp return NULL;
479 20662ea0 2021-04-10 stsp }
480 05118f5a 2021-06-22 stsp
481 05118f5a 2021-06-22 stsp static const struct got_error *
482 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
483 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
484 05118f5a 2021-06-22 stsp {
485 05118f5a 2021-06-22 stsp const struct got_error *err;
486 05118f5a 2021-06-22 stsp struct got_reference *ref;
487 05118f5a 2021-06-22 stsp
488 05118f5a 2021-06-22 stsp *new = NULL;
489 05118f5a 2021-06-22 stsp
490 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
491 05118f5a 2021-06-22 stsp if (err) {
492 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
493 05118f5a 2021-06-22 stsp return err;
494 05118f5a 2021-06-22 stsp
495 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
496 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
497 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
498 05118f5a 2021-06-22 stsp } else {
499 05118f5a 2021-06-22 stsp err = got_reflist_insert(new, refs, ref, repo,
500 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
501 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
502 05118f5a 2021-06-22 stsp got_ref_close(ref);
503 05118f5a 2021-06-22 stsp }
504 05118f5a 2021-06-22 stsp
505 05118f5a 2021-06-22 stsp return err;
506 05118f5a 2021-06-22 stsp }
507 05118f5a 2021-06-22 stsp
508 05118f5a 2021-06-22 stsp static const struct got_error *
509 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
510 05118f5a 2021-06-22 stsp {
511 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
512 05118f5a 2021-06-22 stsp char *cwd = NULL, *repo_path = NULL;
513 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
514 05118f5a 2021-06-22 stsp int ch, i, loose_obj_only = 1;
515 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
516 05118f5a 2021-06-22 stsp char *id_str = NULL;
517 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
518 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
519 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
520 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
521 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
522 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
523 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
524 05118f5a 2021-06-22 stsp
525 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
526 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
527 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
528 05118f5a 2021-06-22 stsp
529 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
530 05118f5a 2021-06-22 stsp switch (ch) {
531 05118f5a 2021-06-22 stsp case 'a':
532 05118f5a 2021-06-22 stsp loose_obj_only = 0;
533 05118f5a 2021-06-22 stsp break;
534 05118f5a 2021-06-22 stsp case 'r':
535 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
536 05118f5a 2021-06-22 stsp if (repo_path == NULL)
537 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
538 05118f5a 2021-06-22 stsp optarg);
539 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
540 05118f5a 2021-06-22 stsp break;
541 05118f5a 2021-06-22 stsp case 'x':
542 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
543 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
544 05118f5a 2021-06-22 stsp optarg, NULL);
545 05118f5a 2021-06-22 stsp if (error)
546 05118f5a 2021-06-22 stsp return error;
547 05118f5a 2021-06-22 stsp break;
548 05118f5a 2021-06-22 stsp default:
549 05118f5a 2021-06-22 stsp usage_pack();
550 05118f5a 2021-06-22 stsp /* NOTREACHED */
551 05118f5a 2021-06-22 stsp }
552 05118f5a 2021-06-22 stsp }
553 05118f5a 2021-06-22 stsp
554 05118f5a 2021-06-22 stsp argc -= optind;
555 05118f5a 2021-06-22 stsp argv += optind;
556 05118f5a 2021-06-22 stsp
557 05118f5a 2021-06-22 stsp #ifndef PROFILE
558 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
559 05118f5a 2021-06-22 stsp NULL) == -1)
560 05118f5a 2021-06-22 stsp err(1, "pledge");
561 05118f5a 2021-06-22 stsp #endif
562 05118f5a 2021-06-22 stsp cwd = getcwd(NULL, 0);
563 05118f5a 2021-06-22 stsp if (cwd == NULL) {
564 05118f5a 2021-06-22 stsp error = got_error_from_errno("getcwd");
565 05118f5a 2021-06-22 stsp goto done;
566 05118f5a 2021-06-22 stsp }
567 05118f5a 2021-06-22 stsp
568 05118f5a 2021-06-22 stsp error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
569 05118f5a 2021-06-22 stsp if (error)
570 05118f5a 2021-06-22 stsp goto done;
571 05118f5a 2021-06-22 stsp
572 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
573 05118f5a 2021-06-22 stsp if (error)
574 05118f5a 2021-06-22 stsp goto done;
575 05118f5a 2021-06-22 stsp
576 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
577 05118f5a 2021-06-22 stsp const char *refname = pe->path;
578 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
579 05118f5a 2021-06-22 stsp if (error)
580 05118f5a 2021-06-22 stsp goto done;
581 05118f5a 2021-06-22 stsp
582 05118f5a 2021-06-22 stsp }
583 05118f5a 2021-06-22 stsp
584 05118f5a 2021-06-22 stsp if (argc == 0) {
585 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
586 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
587 05118f5a 2021-06-22 stsp if (error)
588 05118f5a 2021-06-22 stsp goto done;
589 05118f5a 2021-06-22 stsp } else {
590 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
591 05118f5a 2021-06-22 stsp const char *refname;
592 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
593 05118f5a 2021-06-22 stsp refname = argv[i];
594 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
595 05118f5a 2021-06-22 stsp if (error)
596 05118f5a 2021-06-22 stsp goto done;
597 05118f5a 2021-06-22 stsp }
598 05118f5a 2021-06-22 stsp }
599 05118f5a 2021-06-22 stsp
600 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
601 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
602 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
603 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
604 05118f5a 2021-06-22 stsp continue;
605 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
606 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
607 05118f5a 2021-06-22 stsp free(re);
608 05118f5a 2021-06-22 stsp }
609 05118f5a 2021-06-22 stsp
610 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
611 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
612 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
613 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
614 05118f5a 2021-06-22 stsp
615 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
616 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
617 05118f5a 2021-06-22 stsp pack_progress, &ppa, check_cancelled, NULL);
618 05118f5a 2021-06-22 stsp if (error) {
619 05118f5a 2021-06-22 stsp if (ppa.printed_something)
620 05118f5a 2021-06-22 stsp printf("\n");
621 05118f5a 2021-06-22 stsp goto done;
622 05118f5a 2021-06-22 stsp }
623 05118f5a 2021-06-22 stsp
624 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
625 05118f5a 2021-06-22 stsp if (error)
626 05118f5a 2021-06-22 stsp goto done;
627 05118f5a 2021-06-22 stsp printf("\nWrote %s.pack\n", id_str);
628 05118f5a 2021-06-22 stsp
629 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
630 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
631 05118f5a 2021-06-22 stsp if (error)
632 05118f5a 2021-06-22 stsp goto done;
633 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
634 05118f5a 2021-06-22 stsp done:
635 05118f5a 2021-06-22 stsp got_pathlist_free(&exclude_args);
636 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
637 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
638 05118f5a 2021-06-22 stsp free(id_str);
639 05118f5a 2021-06-22 stsp free(pack_hash);
640 05118f5a 2021-06-22 stsp free(cwd);
641 05118f5a 2021-06-22 stsp return error;
642 05118f5a 2021-06-22 stsp }
643 05118f5a 2021-06-22 stsp
644 05118f5a 2021-06-22 stsp __dead static void
645 05118f5a 2021-06-22 stsp usage_indexpack(void)
646 05118f5a 2021-06-22 stsp {
647 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
648 05118f5a 2021-06-22 stsp getprogname());
649 05118f5a 2021-06-22 stsp exit(1);
650 05118f5a 2021-06-22 stsp }
651 05118f5a 2021-06-22 stsp
652 05118f5a 2021-06-22 stsp static const struct got_error *
653 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
654 05118f5a 2021-06-22 stsp {
655 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
656 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
657 05118f5a 2021-06-22 stsp int ch;
658 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
659 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
660 05118f5a 2021-06-22 stsp char *id_str = NULL;
661 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
662 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
663 05118f5a 2021-06-22 stsp
664 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
665 05118f5a 2021-06-22 stsp switch (ch) {
666 05118f5a 2021-06-22 stsp default:
667 05118f5a 2021-06-22 stsp usage_indexpack();
668 05118f5a 2021-06-22 stsp /* NOTREACHED */
669 05118f5a 2021-06-22 stsp }
670 05118f5a 2021-06-22 stsp }
671 05118f5a 2021-06-22 stsp
672 05118f5a 2021-06-22 stsp argc -= optind;
673 05118f5a 2021-06-22 stsp argv += optind;
674 05118f5a 2021-06-22 stsp
675 05118f5a 2021-06-22 stsp if (argc != 1)
676 05118f5a 2021-06-22 stsp usage_indexpack();
677 05118f5a 2021-06-22 stsp
678 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
679 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
680 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
681 05118f5a 2021-06-22 stsp
682 05118f5a 2021-06-22 stsp #ifndef PROFILE
683 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
684 05118f5a 2021-06-22 stsp NULL) == -1)
685 05118f5a 2021-06-22 stsp err(1, "pledge");
686 05118f5a 2021-06-22 stsp #endif
687 05118f5a 2021-06-22 stsp
688 05118f5a 2021-06-22 stsp error = got_repo_open(&repo, packfile_path, NULL);
689 05118f5a 2021-06-22 stsp if (error)
690 05118f5a 2021-06-22 stsp goto done;
691 05118f5a 2021-06-22 stsp
692 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
693 05118f5a 2021-06-22 stsp if (error)
694 05118f5a 2021-06-22 stsp goto done;
695 05118f5a 2021-06-22 stsp
696 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
697 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
698 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
699 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
700 05118f5a 2021-06-22 stsp
701 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
702 05118f5a 2021-06-22 stsp packfile_path);
703 05118f5a 2021-06-22 stsp if (error)
704 05118f5a 2021-06-22 stsp goto done;
705 05118f5a 2021-06-22 stsp
706 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
707 05118f5a 2021-06-22 stsp if (error)
708 05118f5a 2021-06-22 stsp goto done;
709 05118f5a 2021-06-22 stsp
710 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
711 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
712 05118f5a 2021-06-22 stsp if (error)
713 05118f5a 2021-06-22 stsp goto done;
714 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
715 05118f5a 2021-06-22 stsp done:
716 05118f5a 2021-06-22 stsp free(id_str);
717 05118f5a 2021-06-22 stsp free(pack_hash);
718 05118f5a 2021-06-22 stsp return error;
719 05118f5a 2021-06-22 stsp }
720 05118f5a 2021-06-22 stsp
721 05118f5a 2021-06-22 stsp __dead static void
722 05118f5a 2021-06-22 stsp usage_listpack(void)
723 05118f5a 2021-06-22 stsp {
724 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
725 05118f5a 2021-06-22 stsp getprogname());
726 05118f5a 2021-06-22 stsp exit(1);
727 05118f5a 2021-06-22 stsp }
728 05118f5a 2021-06-22 stsp
729 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
730 05118f5a 2021-06-22 stsp int nblobs;
731 05118f5a 2021-06-22 stsp int ntrees;
732 05118f5a 2021-06-22 stsp int ncommits;
733 05118f5a 2021-06-22 stsp int ntags;
734 05118f5a 2021-06-22 stsp int noffdeltas;
735 05118f5a 2021-06-22 stsp int nrefdeltas;
736 05118f5a 2021-06-22 stsp int human_readable;
737 05118f5a 2021-06-22 stsp };
738 05118f5a 2021-06-22 stsp
739 05118f5a 2021-06-22 stsp static const struct got_error *
740 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
741 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
742 05118f5a 2021-06-22 stsp {
743 05118f5a 2021-06-22 stsp const struct got_error *err;
744 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
745 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
746 05118f5a 2021-06-22 stsp const char *type_str;
747 05118f5a 2021-06-22 stsp
748 05118f5a 2021-06-22 stsp err = got_object_id_str(&id_str, id);
749 05118f5a 2021-06-22 stsp if (err)
750 05118f5a 2021-06-22 stsp return err;
751 05118f5a 2021-06-22 stsp
752 05118f5a 2021-06-22 stsp switch (type) {
753 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
754 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
755 05118f5a 2021-06-22 stsp a->nblobs++;
756 05118f5a 2021-06-22 stsp break;
757 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
758 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
759 05118f5a 2021-06-22 stsp a->ntrees++;
760 05118f5a 2021-06-22 stsp break;
761 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
762 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
763 05118f5a 2021-06-22 stsp a->ncommits++;
764 05118f5a 2021-06-22 stsp break;
765 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
766 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
767 05118f5a 2021-06-22 stsp a->ntags++;
768 05118f5a 2021-06-22 stsp break;
769 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
770 05118f5a 2021-06-22 stsp type_str = "offset-delta";
771 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-offset %llu",
772 05118f5a 2021-06-22 stsp base_offset) == -1) {
773 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
774 05118f5a 2021-06-22 stsp goto done;
775 05118f5a 2021-06-22 stsp }
776 05118f5a 2021-06-22 stsp a->noffdeltas++;
777 05118f5a 2021-06-22 stsp break;
778 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
779 05118f5a 2021-06-22 stsp type_str = "ref-delta";
780 05118f5a 2021-06-22 stsp err = got_object_id_str(&base_id_str, base_id);
781 05118f5a 2021-06-22 stsp if (err)
782 05118f5a 2021-06-22 stsp goto done;
783 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
784 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
785 05118f5a 2021-06-22 stsp goto done;
786 05118f5a 2021-06-22 stsp }
787 05118f5a 2021-06-22 stsp a->nrefdeltas++;
788 05118f5a 2021-06-22 stsp break;
789 05118f5a 2021-06-22 stsp default:
790 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
791 05118f5a 2021-06-22 stsp goto done;
792 05118f5a 2021-06-22 stsp }
793 05118f5a 2021-06-22 stsp if (a->human_readable) {
794 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
795 05118f5a 2021-06-22 stsp char *s;;
796 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
797 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
798 05118f5a 2021-06-22 stsp goto done;
799 05118f5a 2021-06-22 stsp }
800 05118f5a 2021-06-22 stsp s = scaled;
801 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
802 05118f5a 2021-06-22 stsp s++;
803 05118f5a 2021-06-22 stsp printf("%s %s at %llu size %s%s\n", id_str, type_str, offset,
804 05118f5a 2021-06-22 stsp s, delta_str ? delta_str : "");
805 05118f5a 2021-06-22 stsp } else {
806 05118f5a 2021-06-22 stsp printf("%s %s at %llu size %llu%s\n", id_str, type_str, offset,
807 05118f5a 2021-06-22 stsp size, delta_str ? delta_str : "");
808 05118f5a 2021-06-22 stsp }
809 05118f5a 2021-06-22 stsp done:
810 05118f5a 2021-06-22 stsp free(id_str);
811 05118f5a 2021-06-22 stsp free(base_id_str);
812 05118f5a 2021-06-22 stsp free(delta_str);
813 05118f5a 2021-06-22 stsp return err;
814 05118f5a 2021-06-22 stsp }
815 05118f5a 2021-06-22 stsp
816 05118f5a 2021-06-22 stsp static const struct got_error *
817 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
818 05118f5a 2021-06-22 stsp {
819 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
820 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
821 05118f5a 2021-06-22 stsp int ch;
822 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
823 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
824 05118f5a 2021-06-22 stsp char *id_str = NULL;
825 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
826 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
827 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
828 05118f5a 2021-06-22 stsp
829 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
830 05118f5a 2021-06-22 stsp switch (ch) {
831 05118f5a 2021-06-22 stsp case 'h':
832 05118f5a 2021-06-22 stsp human_readable = 1;
833 05118f5a 2021-06-22 stsp break;
834 05118f5a 2021-06-22 stsp case 's':
835 05118f5a 2021-06-22 stsp show_stats = 1;
836 05118f5a 2021-06-22 stsp break;
837 05118f5a 2021-06-22 stsp default:
838 05118f5a 2021-06-22 stsp usage_listpack();
839 05118f5a 2021-06-22 stsp /* NOTREACHED */
840 05118f5a 2021-06-22 stsp }
841 05118f5a 2021-06-22 stsp }
842 05118f5a 2021-06-22 stsp
843 05118f5a 2021-06-22 stsp argc -= optind;
844 05118f5a 2021-06-22 stsp argv += optind;
845 05118f5a 2021-06-22 stsp
846 05118f5a 2021-06-22 stsp if (argc != 1)
847 05118f5a 2021-06-22 stsp usage_listpack();
848 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
849 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
850 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
851 05118f5a 2021-06-22 stsp
852 05118f5a 2021-06-22 stsp #ifndef PROFILE
853 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
854 05118f5a 2021-06-22 stsp NULL) == -1)
855 05118f5a 2021-06-22 stsp err(1, "pledge");
856 05118f5a 2021-06-22 stsp #endif
857 05118f5a 2021-06-22 stsp error = got_repo_open(&repo, packfile_path, NULL);
858 05118f5a 2021-06-22 stsp if (error)
859 05118f5a 2021-06-22 stsp goto done;
860 05118f5a 2021-06-22 stsp
861 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
862 05118f5a 2021-06-22 stsp if (error)
863 05118f5a 2021-06-22 stsp goto done;
864 05118f5a 2021-06-22 stsp
865 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
866 05118f5a 2021-06-22 stsp packfile_path);
867 05118f5a 2021-06-22 stsp if (error)
868 05118f5a 2021-06-22 stsp goto done;
869 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
870 05118f5a 2021-06-22 stsp if (error)
871 05118f5a 2021-06-22 stsp goto done;
872 05118f5a 2021-06-22 stsp
873 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
874 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
875 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
876 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
877 05118f5a 2021-06-22 stsp if (error)
878 05118f5a 2021-06-22 stsp goto done;
879 05118f5a 2021-06-22 stsp if (show_stats) {
880 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
881 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
882 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
883 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
884 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
885 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
886 05118f5a 2021-06-22 stsp }
887 05118f5a 2021-06-22 stsp done:
888 05118f5a 2021-06-22 stsp free(id_str);
889 05118f5a 2021-06-22 stsp free(pack_hash);
890 05118f5a 2021-06-22 stsp free(packfile_path);
891 05118f5a 2021-06-22 stsp return error;
892 05118f5a 2021-06-22 stsp }