2 20662ea0 2021-04-10 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 20662ea0 2021-04-10 stsp #include <sys/queue.h>
18 05118f5a 2021-06-22 stsp #include <sys/types.h>
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 5822e79e 2023-02-23 op #include <sha2.h>
28 20662ea0 2021-04-10 stsp #include <stdio.h>
29 20662ea0 2021-04-10 stsp #include <stdlib.h>
30 20662ea0 2021-04-10 stsp #include <signal.h>
31 20662ea0 2021-04-10 stsp #include <string.h>
32 20662ea0 2021-04-10 stsp #include <unistd.h>
33 20662ea0 2021-04-10 stsp #include <util.h>
35 20662ea0 2021-04-10 stsp #include "got_version.h"
36 20662ea0 2021-04-10 stsp #include "got_error.h"
37 20662ea0 2021-04-10 stsp #include "got_object.h"
38 20662ea0 2021-04-10 stsp #include "got_reference.h"
39 05118f5a 2021-06-22 stsp #include "got_cancel.h"
40 20662ea0 2021-04-10 stsp #include "got_repository.h"
41 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
42 2df845d5 2023-07-07 op #include "got_repository_dump.h"
43 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
44 20662ea0 2021-04-10 stsp #include "got_path.h"
45 20662ea0 2021-04-10 stsp #include "got_privsep.h"
46 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
47 7d69d862 2021-11-15 stsp #include "got_worktree.h"
49 20662ea0 2021-04-10 stsp #ifndef nitems
50 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigint_received;
54 20662ea0 2021-04-10 stsp static volatile sig_atomic_t sigpipe_received;
57 20662ea0 2021-04-10 stsp catch_sigint(int signo)
59 20662ea0 2021-04-10 stsp sigint_received = 1;
63 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
65 20662ea0 2021-04-10 stsp sigpipe_received = 1;
68 05118f5a 2021-06-22 stsp static const struct got_error *
69 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
71 05118f5a 2021-06-22 stsp if (sigint_received || sigpipe_received)
72 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_CANCELLED);
73 05118f5a 2021-06-22 stsp return NULL;
76 20662ea0 2021-04-10 stsp struct gotadmin_cmd {
77 20662ea0 2021-04-10 stsp const char *cmd_name;
78 20662ea0 2021-04-10 stsp const struct got_error *(*cmd_main)(int, char *[]);
79 20662ea0 2021-04-10 stsp void (*cmd_usage)(void);
80 20662ea0 2021-04-10 stsp const char *cmd_alias;
83 20662ea0 2021-04-10 stsp __dead static void usage(int, int);
84 02a5c5d0 2022-07-04 stsp __dead static void usage_init(void);
85 20662ea0 2021-04-10 stsp __dead static void usage_info(void);
86 05118f5a 2021-06-22 stsp __dead static void usage_pack(void);
87 05118f5a 2021-06-22 stsp __dead static void usage_indexpack(void);
88 05118f5a 2021-06-22 stsp __dead static void usage_listpack(void);
89 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
90 2df845d5 2023-07-07 op __dead static void usage_dump(void);
92 02a5c5d0 2022-07-04 stsp static const struct got_error* cmd_init(int, char *[]);
93 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
94 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
95 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
96 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
97 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
98 2df845d5 2023-07-07 op static const struct got_error* cmd_dump(int, char *[]);
100 3e166534 2022-02-16 naddy static const struct gotadmin_cmd gotadmin_commands[] = {
101 02a5c5d0 2022-07-04 stsp { "init", cmd_init, usage_init, "" },
102 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
103 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
104 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
105 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
106 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
107 2df845d5 2023-07-07 op { "dump", cmd_dump, usage_dump, "" },
110 20662ea0 2021-04-10 stsp static void
111 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
115 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
116 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
117 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
118 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
120 20662ea0 2021-04-10 stsp fputc('\n', fp);
124 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
126 3e166534 2022-02-16 naddy const struct gotadmin_cmd *cmd;
129 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
130 3e166534 2022-02-16 naddy static const struct option longopts[] = {
131 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
132 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
135 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
137 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
138 20662ea0 2021-04-10 stsp switch (ch) {
146 20662ea0 2021-04-10 stsp usage(hflag, 1);
147 20662ea0 2021-04-10 stsp /* NOTREACHED */
151 20662ea0 2021-04-10 stsp argc -= optind;
152 20662ea0 2021-04-10 stsp argv += optind;
153 20662ea0 2021-04-10 stsp optind = 1;
154 20662ea0 2021-04-10 stsp optreset = 1;
156 20662ea0 2021-04-10 stsp if (Vflag) {
157 20662ea0 2021-04-10 stsp got_version_print_str();
161 20662ea0 2021-04-10 stsp if (argc <= 0)
162 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
164 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
165 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
167 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
168 20662ea0 2021-04-10 stsp const struct got_error *error;
170 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
172 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
173 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
177 3e166534 2022-02-16 naddy cmd->cmd_usage();
179 3e166534 2022-02-16 naddy error = cmd->cmd_main(argc, argv);
180 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
181 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
182 20662ea0 2021-04-10 stsp !(sigpipe_received &&
183 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
184 20662ea0 2021-04-10 stsp !(sigint_received &&
185 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
186 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
193 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
194 20662ea0 2021-04-10 stsp list_commands(stderr);
198 20662ea0 2021-04-10 stsp __dead static void
199 20662ea0 2021-04-10 stsp usage(int hflag, int status)
201 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
203 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
204 20662ea0 2021-04-10 stsp getprogname());
206 20662ea0 2021-04-10 stsp list_commands(fp);
207 20662ea0 2021-04-10 stsp exit(status);
210 20662ea0 2021-04-10 stsp static const struct got_error *
211 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
213 20662ea0 2021-04-10 stsp const struct got_error *err;
215 20662ea0 2021-04-10 stsp #ifdef PROFILE
216 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
217 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
219 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
220 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
222 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
223 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
225 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
226 20662ea0 2021-04-10 stsp if (err != NULL)
227 20662ea0 2021-04-10 stsp return err;
229 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
230 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
232 20662ea0 2021-04-10 stsp return NULL;
235 20662ea0 2021-04-10 stsp __dead static void
236 20662ea0 2021-04-10 stsp usage_info(void)
238 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
239 20662ea0 2021-04-10 stsp getprogname());
243 20662ea0 2021-04-10 stsp static const struct got_error *
244 7d69d862 2021-11-15 stsp get_repo_path(char **repo_path)
246 7d69d862 2021-11-15 stsp const struct got_error *err = NULL;
247 7d69d862 2021-11-15 stsp struct got_worktree *worktree = NULL;
250 7d69d862 2021-11-15 stsp *repo_path = NULL;
252 7d69d862 2021-11-15 stsp cwd = getcwd(NULL, 0);
253 7d69d862 2021-11-15 stsp if (cwd == NULL)
254 7d69d862 2021-11-15 stsp return got_error_from_errno("getcwd");
256 7d69d862 2021-11-15 stsp err = got_worktree_open(&worktree, cwd);
258 7d69d862 2021-11-15 stsp if (err->code != GOT_ERR_NOT_WORKTREE)
260 7d69d862 2021-11-15 stsp err = NULL;
263 7d69d862 2021-11-15 stsp if (worktree)
264 7d69d862 2021-11-15 stsp *repo_path = strdup(got_worktree_get_repo_path(worktree));
266 7d69d862 2021-11-15 stsp *repo_path = strdup(cwd);
267 7d69d862 2021-11-15 stsp if (*repo_path == NULL)
268 7d69d862 2021-11-15 stsp err = got_error_from_errno("strdup");
270 7d69d862 2021-11-15 stsp if (worktree)
271 7d69d862 2021-11-15 stsp got_worktree_close(worktree);
273 7d69d862 2021-11-15 stsp return err;
276 02a5c5d0 2022-07-04 stsp __dead static void
277 02a5c5d0 2022-07-04 stsp usage_init(void)
279 6f04a73d 2022-09-20 mark fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
280 6f04a73d 2022-09-20 mark getprogname());
284 02a5c5d0 2022-07-04 stsp static const struct got_error *
285 02a5c5d0 2022-07-04 stsp cmd_init(int argc, char *argv[])
287 02a5c5d0 2022-07-04 stsp const struct got_error *error = NULL;
288 6f04a73d 2022-09-20 mark const char *head_name = NULL;
289 02a5c5d0 2022-07-04 stsp char *repo_path = NULL;
292 1fa0d17d 2023-02-13 op #ifndef PROFILE
293 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
294 1fa0d17d 2023-02-13 op err(1, "pledge");
297 6f04a73d 2022-09-20 mark while ((ch = getopt(argc, argv, "b:")) != -1) {
298 02a5c5d0 2022-07-04 stsp switch (ch) {
300 6f04a73d 2022-09-20 mark head_name = optarg;
303 02a5c5d0 2022-07-04 stsp usage_init();
304 02a5c5d0 2022-07-04 stsp /* NOTREACHED */
308 02a5c5d0 2022-07-04 stsp argc -= optind;
309 02a5c5d0 2022-07-04 stsp argv += optind;
311 02a5c5d0 2022-07-04 stsp if (argc != 1)
312 02a5c5d0 2022-07-04 stsp usage_init();
314 02a5c5d0 2022-07-04 stsp repo_path = strdup(argv[0]);
315 02a5c5d0 2022-07-04 stsp if (repo_path == NULL)
316 02a5c5d0 2022-07-04 stsp return got_error_from_errno("strdup");
318 02a5c5d0 2022-07-04 stsp got_path_strip_trailing_slashes(repo_path);
320 02a5c5d0 2022-07-04 stsp error = got_path_mkdir(repo_path);
321 02a5c5d0 2022-07-04 stsp if (error &&
322 02a5c5d0 2022-07-04 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
325 02a5c5d0 2022-07-04 stsp error = apply_unveil(repo_path, 0);
329 6f04a73d 2022-09-20 mark error = got_repo_init(repo_path, head_name);
331 02a5c5d0 2022-07-04 stsp free(repo_path);
332 02a5c5d0 2022-07-04 stsp return error;
335 7d69d862 2021-11-15 stsp static const struct got_error *
336 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
338 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
339 7d69d862 2021-11-15 stsp char *repo_path = NULL;
340 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
341 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
342 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
343 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
344 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
345 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
347 1fa0d17d 2023-02-13 op #ifndef PROFILE
348 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
350 1fa0d17d 2023-02-13 op err(1, "pledge");
353 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
354 20662ea0 2021-04-10 stsp switch (ch) {
356 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
357 20662ea0 2021-04-10 stsp if (repo_path == NULL)
358 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
360 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
363 20662ea0 2021-04-10 stsp usage_info();
364 20662ea0 2021-04-10 stsp /* NOTREACHED */
368 20662ea0 2021-04-10 stsp argc -= optind;
369 20662ea0 2021-04-10 stsp argv += optind;
371 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
372 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
376 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
377 0ae84acc 2022-06-15 tracey if (error != NULL)
378 0ae84acc 2022-06-15 tracey goto done;
379 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
382 57160834 2022-05-31 stsp #ifndef PROFILE
383 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
384 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
385 57160834 2022-05-31 stsp NULL) == -1)
386 57160834 2022-05-31 stsp err(1, "pledge");
388 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
392 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
394 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
395 20662ea0 2021-04-10 stsp if (gotconfig) {
396 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
397 20662ea0 2021-04-10 stsp int i, nremotes;
398 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
399 20662ea0 2021-04-10 stsp printf("default author: %s\n",
400 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
402 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
403 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
404 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
405 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
406 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
407 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
408 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
410 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
411 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
412 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
413 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
418 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
419 20662ea0 2021-04-10 stsp &packsize, repo);
422 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
423 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
424 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
425 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
428 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
429 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
432 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
435 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
436 20662ea0 2021-04-10 stsp if (nobj > 0) {
437 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
438 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
441 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
445 20662ea0 2021-04-10 stsp got_repo_close(repo);
446 0ae84acc 2022-06-15 tracey if (pack_fds) {
447 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
448 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
449 0ae84acc 2022-06-15 tracey if (error == NULL)
450 0ae84acc 2022-06-15 tracey error = pack_err;
453 7d69d862 2021-11-15 stsp free(repo_path);
454 20662ea0 2021-04-10 stsp return error;
457 05118f5a 2021-06-22 stsp __dead static void
458 05118f5a 2021-06-22 stsp usage_pack(void)
460 c7a4fcc8 2023-02-17 op fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
461 827a167b 2022-08-16 stsp "[-x reference] [reference ...]\n", getprogname());
465 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
467 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
468 b8af7c06 2022-03-15 stsp int last_ncolored;
469 b8af7c06 2022-03-15 stsp int last_nfound;
470 b8af7c06 2022-03-15 stsp int last_ntrees;
471 b8af7c06 2022-03-15 stsp int loading_done;
472 05118f5a 2021-06-22 stsp int last_ncommits;
473 05118f5a 2021-06-22 stsp int last_nobj_total;
474 05118f5a 2021-06-22 stsp int last_p_deltify;
475 05118f5a 2021-06-22 stsp int last_p_written;
476 05118f5a 2021-06-22 stsp int last_p_indexed;
477 05118f5a 2021-06-22 stsp int last_p_resolved;
478 05118f5a 2021-06-22 stsp int verbosity;
479 05118f5a 2021-06-22 stsp int printed_something;
482 b8af7c06 2022-03-15 stsp static void
483 2df845d5 2023-07-07 op print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
484 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees)
486 b8af7c06 2022-03-15 stsp if (print_colored) {
487 2df845d5 2023-07-07 op fprintf(out, "%d commit%s colored", ncolored,
488 b8af7c06 2022-03-15 stsp ncolored == 1 ? "" : "s");
490 b8af7c06 2022-03-15 stsp if (print_found) {
491 2df845d5 2023-07-07 op fprintf(out, "%s%d object%s found",
492 b8af7c06 2022-03-15 stsp ncolored > 0 ? "; " : "",
493 b8af7c06 2022-03-15 stsp nfound, nfound == 1 ? "" : "s");
495 b8af7c06 2022-03-15 stsp if (print_trees) {
496 2df845d5 2023-07-07 op fprintf(out, "; %d tree%s scanned", ntrees,
497 b8af7c06 2022-03-15 stsp ntrees == 1 ? "" : "s");
501 05118f5a 2021-06-22 stsp static const struct got_error *
502 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
503 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
504 b8af7c06 2022-03-15 stsp int nobj_written)
506 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
507 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
508 05118f5a 2021-06-22 stsp int p_deltify, p_written;
509 b8af7c06 2022-03-15 stsp int print_colored = 0, print_found = 0, print_trees = 0;
510 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
511 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
513 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
514 b8af7c06 2022-03-15 stsp return NULL;
516 b8af7c06 2022-03-15 stsp if (a->last_ncolored != ncolored) {
517 b8af7c06 2022-03-15 stsp print_colored = 1;
518 b8af7c06 2022-03-15 stsp a->last_ncolored = ncolored;
521 b8af7c06 2022-03-15 stsp if (a->last_nfound != nfound) {
522 b8af7c06 2022-03-15 stsp print_colored = 1;
523 b8af7c06 2022-03-15 stsp print_found = 1;
524 b8af7c06 2022-03-15 stsp a->last_nfound = nfound;
527 b8af7c06 2022-03-15 stsp if (a->last_ntrees != ntrees) {
528 b8af7c06 2022-03-15 stsp print_colored = 1;
529 b8af7c06 2022-03-15 stsp print_found = 1;
530 b8af7c06 2022-03-15 stsp print_trees = 1;
531 b8af7c06 2022-03-15 stsp a->last_ntrees = ntrees;
534 b8af7c06 2022-03-15 stsp if ((print_colored || print_found || print_trees) &&
535 b8af7c06 2022-03-15 stsp !a->loading_done) {
536 2df845d5 2023-07-07 op fprintf(a->out, "\r");
537 2df845d5 2023-07-07 op print_load_info(a->out, print_colored, print_found,
538 2df845d5 2023-07-07 op print_trees, ncolored, nfound, ntrees);
539 b8af7c06 2022-03-15 stsp a->printed_something = 1;
540 2df845d5 2023-07-07 op fflush(a->out);
541 05118f5a 2021-06-22 stsp return NULL;
542 b8af7c06 2022-03-15 stsp } else if (!a->loading_done) {
543 2df845d5 2023-07-07 op fprintf(a->out, "\r");
544 2df845d5 2023-07-07 op print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
545 2df845d5 2023-07-07 op fprintf(a->out, "\n");
546 b8af7c06 2022-03-15 stsp a->loading_done = 1;
549 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
550 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
552 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
553 05118f5a 2021-06-22 stsp print_searching = 1;
554 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
557 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
558 05118f5a 2021-06-22 stsp print_searching = 1;
559 05118f5a 2021-06-22 stsp print_total = 1;
560 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
563 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
564 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
565 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
566 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
567 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
570 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
571 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
572 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
573 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
574 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
575 05118f5a 2021-06-22 stsp print_searching = 1;
576 05118f5a 2021-06-22 stsp print_total = 1;
577 05118f5a 2021-06-22 stsp print_deltify = 1;
580 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
581 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
582 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
583 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
584 05118f5a 2021-06-22 stsp print_searching = 1;
585 05118f5a 2021-06-22 stsp print_total = 1;
586 05118f5a 2021-06-22 stsp print_deltify = 1;
587 05118f5a 2021-06-22 stsp print_written = 1;
592 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
593 2df845d5 2023-07-07 op fprintf(a->out, "\r");
594 05118f5a 2021-06-22 stsp if (print_searching)
595 2df845d5 2023-07-07 op fprintf(a->out, "packing %d reference%s", ncommits,
596 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
597 05118f5a 2021-06-22 stsp if (print_total)
598 2df845d5 2023-07-07 op fprintf(a->out, "; %d object%s", nobj_total,
599 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
600 05118f5a 2021-06-22 stsp if (print_deltify)
601 2df845d5 2023-07-07 op fprintf(a->out, "; deltify: %d%%", p_deltify);
602 05118f5a 2021-06-22 stsp if (print_written)
603 2df845d5 2023-07-07 op fprintf(a->out, "; writing pack: %*s %d%%",
604 2df845d5 2023-07-07 op FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
605 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
606 05118f5a 2021-06-22 stsp print_written) {
607 05118f5a 2021-06-22 stsp a->printed_something = 1;
608 2df845d5 2023-07-07 op fflush(a->out);
610 05118f5a 2021-06-22 stsp return NULL;
613 05118f5a 2021-06-22 stsp static const struct got_error *
614 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
615 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
617 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
618 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
619 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
620 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
622 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
623 05118f5a 2021-06-22 stsp return NULL;
625 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
626 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
627 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
628 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
629 05118f5a 2021-06-22 stsp print_size = 1;
630 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
631 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
632 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
634 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
635 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
636 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
637 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
638 05118f5a 2021-06-22 stsp print_indexed = 1;
639 05118f5a 2021-06-22 stsp print_size = 1;
642 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
643 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
644 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
645 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
646 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
647 05118f5a 2021-06-22 stsp print_resolved = 1;
648 05118f5a 2021-06-22 stsp print_indexed = 1;
649 05118f5a 2021-06-22 stsp print_size = 1;
654 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
655 05118f5a 2021-06-22 stsp printf("\r");
656 05118f5a 2021-06-22 stsp if (print_size)
657 b5934965 2022-02-12 naddy printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
658 05118f5a 2021-06-22 stsp if (print_indexed)
659 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
660 05118f5a 2021-06-22 stsp if (print_resolved)
661 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
662 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
663 05118f5a 2021-06-22 stsp fflush(stdout);
665 05118f5a 2021-06-22 stsp return NULL;
668 05118f5a 2021-06-22 stsp static const struct got_error *
669 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
670 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
672 05118f5a 2021-06-22 stsp const struct got_error *err;
673 05118f5a 2021-06-22 stsp struct got_reference *ref;
675 05118f5a 2021-06-22 stsp *new = NULL;
677 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
679 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
680 05118f5a 2021-06-22 stsp return err;
682 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
683 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
684 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
686 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
687 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
688 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
689 05118f5a 2021-06-22 stsp got_ref_close(ref);
692 05118f5a 2021-06-22 stsp return err;
695 05118f5a 2021-06-22 stsp static const struct got_error *
696 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
698 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
699 7d69d862 2021-11-15 stsp char *repo_path = NULL;
700 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
701 c7a4fcc8 2023-02-17 op int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
702 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
703 05118f5a 2021-06-22 stsp char *id_str = NULL;
704 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
705 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
706 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
707 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
708 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
709 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
710 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
711 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
713 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
714 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
715 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
717 1fa0d17d 2023-02-13 op #ifndef PROFILE
718 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
720 1fa0d17d 2023-02-13 op err(1, "pledge");
723 c7a4fcc8 2023-02-17 op while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
724 05118f5a 2021-06-22 stsp switch (ch) {
726 05118f5a 2021-06-22 stsp loose_obj_only = 0;
729 c7a4fcc8 2023-02-17 op force_refdelta = 1;
732 6f319063 2022-10-27 stsp verbosity = -1;
735 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
736 05118f5a 2021-06-22 stsp if (repo_path == NULL)
737 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
739 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
742 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
743 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
744 05118f5a 2021-06-22 stsp optarg, NULL);
746 05118f5a 2021-06-22 stsp return error;
749 05118f5a 2021-06-22 stsp usage_pack();
750 05118f5a 2021-06-22 stsp /* NOTREACHED */
754 05118f5a 2021-06-22 stsp argc -= optind;
755 05118f5a 2021-06-22 stsp argv += optind;
757 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
758 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
762 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
763 0ae84acc 2022-06-15 tracey if (error != NULL)
764 0ae84acc 2022-06-15 tracey goto done;
765 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
769 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
773 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
774 05118f5a 2021-06-22 stsp const char *refname = pe->path;
775 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
780 05118f5a 2021-06-22 stsp if (argc == 0) {
781 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
782 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
786 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
787 05118f5a 2021-06-22 stsp const char *refname;
788 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
789 05118f5a 2021-06-22 stsp refname = argv[i];
790 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
796 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
797 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
798 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
799 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
801 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
802 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
806 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
807 2df845d5 2023-07-07 op ppa.out = stdout;
808 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
809 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
810 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
811 20e420c8 2022-04-11 stsp ppa.verbosity = verbosity;
813 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
814 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
815 c7a4fcc8 2023-02-17 op force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
816 05118f5a 2021-06-22 stsp if (error) {
817 05118f5a 2021-06-22 stsp if (ppa.printed_something)
818 05118f5a 2021-06-22 stsp printf("\n");
822 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
825 20e420c8 2022-04-11 stsp if (verbosity >= 0)
826 20e420c8 2022-04-11 stsp printf("\nWrote %s.pack\n", id_str);
828 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
829 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
832 20e420c8 2022-04-11 stsp if (verbosity >= 0)
833 20e420c8 2022-04-11 stsp printf("\nIndexed %s.pack\n", id_str);
836 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
837 0ae84acc 2022-06-15 tracey if (pack_fds) {
838 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
839 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
840 0ae84acc 2022-06-15 tracey if (error == NULL)
841 0ae84acc 2022-06-15 tracey error = pack_err;
843 d8bacb93 2023-01-10 mark got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
844 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
845 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
846 05118f5a 2021-06-22 stsp free(id_str);
847 05118f5a 2021-06-22 stsp free(pack_hash);
848 7d69d862 2021-11-15 stsp free(repo_path);
849 05118f5a 2021-06-22 stsp return error;
852 05118f5a 2021-06-22 stsp __dead static void
853 05118f5a 2021-06-22 stsp usage_indexpack(void)
855 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
856 05118f5a 2021-06-22 stsp getprogname());
860 05118f5a 2021-06-22 stsp static const struct got_error *
861 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
863 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
864 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
866 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
867 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
868 05118f5a 2021-06-22 stsp char *id_str = NULL;
869 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
870 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
871 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
873 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
874 05118f5a 2021-06-22 stsp switch (ch) {
876 05118f5a 2021-06-22 stsp usage_indexpack();
877 05118f5a 2021-06-22 stsp /* NOTREACHED */
881 05118f5a 2021-06-22 stsp argc -= optind;
882 05118f5a 2021-06-22 stsp argv += optind;
884 05118f5a 2021-06-22 stsp if (argc != 1)
885 05118f5a 2021-06-22 stsp usage_indexpack();
887 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
888 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
889 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
891 05118f5a 2021-06-22 stsp #ifndef PROFILE
892 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
893 05118f5a 2021-06-22 stsp NULL) == -1)
894 05118f5a 2021-06-22 stsp err(1, "pledge");
897 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
898 0ae84acc 2022-06-15 tracey if (error != NULL)
899 0ae84acc 2022-06-15 tracey goto done;
900 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
904 802c0f04 2021-10-15 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
908 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
909 2df845d5 2023-07-07 op ppa.out = stdout;
910 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
911 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
912 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
914 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
915 05118f5a 2021-06-22 stsp packfile_path);
919 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
923 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
924 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
927 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
930 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
931 0ae84acc 2022-06-15 tracey if (pack_fds) {
932 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
933 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
934 0ae84acc 2022-06-15 tracey if (error == NULL)
935 0ae84acc 2022-06-15 tracey error = pack_err;
937 05118f5a 2021-06-22 stsp free(id_str);
938 05118f5a 2021-06-22 stsp free(pack_hash);
939 05118f5a 2021-06-22 stsp return error;
942 05118f5a 2021-06-22 stsp __dead static void
943 05118f5a 2021-06-22 stsp usage_listpack(void)
945 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
946 05118f5a 2021-06-22 stsp getprogname());
950 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
951 05118f5a 2021-06-22 stsp int nblobs;
952 05118f5a 2021-06-22 stsp int ntrees;
953 05118f5a 2021-06-22 stsp int ncommits;
955 05118f5a 2021-06-22 stsp int noffdeltas;
956 05118f5a 2021-06-22 stsp int nrefdeltas;
957 05118f5a 2021-06-22 stsp int human_readable;
960 05118f5a 2021-06-22 stsp static const struct got_error *
961 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
962 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
964 05118f5a 2021-06-22 stsp const struct got_error *err;
965 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
966 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
967 05118f5a 2021-06-22 stsp const char *type_str;
969 0ae84acc 2022-06-15 tracey err = got_object_id_str(&id_str, id);
971 05118f5a 2021-06-22 stsp return err;
973 05118f5a 2021-06-22 stsp switch (type) {
974 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
975 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
976 05118f5a 2021-06-22 stsp a->nblobs++;
978 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
979 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
980 05118f5a 2021-06-22 stsp a->ntrees++;
982 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
983 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
984 05118f5a 2021-06-22 stsp a->ncommits++;
986 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
987 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
988 05118f5a 2021-06-22 stsp a->ntags++;
990 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
991 05118f5a 2021-06-22 stsp type_str = "offset-delta";
992 963ac08a 2021-09-25 naddy if (asprintf(&delta_str, " base-offset %lld",
993 963ac08a 2021-09-25 naddy (long long)base_offset) == -1) {
994 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
997 05118f5a 2021-06-22 stsp a->noffdeltas++;
999 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
1000 05118f5a 2021-06-22 stsp type_str = "ref-delta";
1001 5e91dae4 2022-08-30 stsp err = got_object_id_str(&base_id_str, base_id);
1003 05118f5a 2021-06-22 stsp goto done;
1004 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1005 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
1006 05118f5a 2021-06-22 stsp goto done;
1008 05118f5a 2021-06-22 stsp a->nrefdeltas++;
1011 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1012 05118f5a 2021-06-22 stsp goto done;
1014 05118f5a 2021-06-22 stsp if (a->human_readable) {
1015 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
1017 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
1018 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
1019 05118f5a 2021-06-22 stsp goto done;
1021 05118f5a 2021-06-22 stsp s = scaled;
1022 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
1024 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %s%s\n", id_str, type_str,
1025 963ac08a 2021-09-25 naddy (long long)offset, s, delta_str ? delta_str : "");
1027 963ac08a 2021-09-25 naddy printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1028 963ac08a 2021-09-25 naddy (long long)offset, (long long)size,
1029 963ac08a 2021-09-25 naddy delta_str ? delta_str : "");
1032 05118f5a 2021-06-22 stsp free(id_str);
1033 05118f5a 2021-06-22 stsp free(base_id_str);
1034 05118f5a 2021-06-22 stsp free(delta_str);
1035 05118f5a 2021-06-22 stsp return err;
1038 05118f5a 2021-06-22 stsp static const struct got_error *
1039 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
1041 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
1042 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
1044 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
1045 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
1046 05118f5a 2021-06-22 stsp char *id_str = NULL;
1047 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
1048 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
1049 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
1050 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1052 1fa0d17d 2023-02-13 op #ifndef PROFILE
1053 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1054 1fa0d17d 2023-02-13 op NULL) == -1)
1055 1fa0d17d 2023-02-13 op err(1, "pledge");
1058 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
1059 05118f5a 2021-06-22 stsp switch (ch) {
1061 05118f5a 2021-06-22 stsp human_readable = 1;
1064 05118f5a 2021-06-22 stsp show_stats = 1;
1067 05118f5a 2021-06-22 stsp usage_listpack();
1068 05118f5a 2021-06-22 stsp /* NOTREACHED */
1072 05118f5a 2021-06-22 stsp argc -= optind;
1073 05118f5a 2021-06-22 stsp argv += optind;
1075 05118f5a 2021-06-22 stsp if (argc != 1)
1076 05118f5a 2021-06-22 stsp usage_listpack();
1077 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
1078 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
1079 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1081 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1082 0ae84acc 2022-06-15 tracey if (error != NULL)
1083 0ae84acc 2022-06-15 tracey goto done;
1084 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1085 05118f5a 2021-06-22 stsp if (error)
1086 05118f5a 2021-06-22 stsp goto done;
1087 57160834 2022-05-31 stsp #ifndef PROFILE
1088 57160834 2022-05-31 stsp /* Remove "cpath" promise. */
1089 57160834 2022-05-31 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1090 57160834 2022-05-31 stsp NULL) == -1)
1091 57160834 2022-05-31 stsp err(1, "pledge");
1093 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1094 05118f5a 2021-06-22 stsp if (error)
1095 05118f5a 2021-06-22 stsp goto done;
1097 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1098 05118f5a 2021-06-22 stsp packfile_path);
1099 05118f5a 2021-06-22 stsp if (error)
1100 05118f5a 2021-06-22 stsp goto done;
1101 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1102 05118f5a 2021-06-22 stsp if (error)
1103 05118f5a 2021-06-22 stsp goto done;
1105 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1106 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1107 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1108 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1109 05118f5a 2021-06-22 stsp if (error)
1110 05118f5a 2021-06-22 stsp goto done;
1111 05118f5a 2021-06-22 stsp if (show_stats) {
1112 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1113 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1114 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1115 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1116 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1117 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1121 f8eebdd4 2021-10-15 stsp got_repo_close(repo);
1122 0ae84acc 2022-06-15 tracey if (pack_fds) {
1123 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1124 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1125 0ae84acc 2022-06-15 tracey if (error == NULL)
1126 0ae84acc 2022-06-15 tracey error = pack_err;
1128 05118f5a 2021-06-22 stsp free(id_str);
1129 05118f5a 2021-06-22 stsp free(pack_hash);
1130 05118f5a 2021-06-22 stsp free(packfile_path);
1131 05118f5a 2021-06-22 stsp return error;
1134 b3d68e7f 2021-07-03 stsp __dead static void
1135 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1137 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1138 827a167b 2022-08-16 stsp getprogname());
1142 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1143 b3d68e7f 2021-07-03 stsp int last_nloose;
1144 b3d68e7f 2021-07-03 stsp int last_ncommits;
1145 b3d68e7f 2021-07-03 stsp int last_npurged;
1146 9a7c12cf 2023-06-18 op int last_nredundant;
1147 b3d68e7f 2021-07-03 stsp int verbosity;
1148 b3d68e7f 2021-07-03 stsp int printed_something;
1149 b3d68e7f 2021-07-03 stsp int dry_run;
1152 b3d68e7f 2021-07-03 stsp static const struct got_error *
1153 9a7c12cf 2023-06-18 op cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1154 9a7c12cf 2023-06-18 op int nredundant)
1156 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1157 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1158 9a7c12cf 2023-06-18 op int print_redundant = 0;
1160 b3d68e7f 2021-07-03 stsp if (a->last_nloose != nloose) {
1161 b3d68e7f 2021-07-03 stsp print_loose = 1;
1162 b3d68e7f 2021-07-03 stsp a->last_nloose = nloose;
1164 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1165 b3d68e7f 2021-07-03 stsp print_loose = 1;
1166 b3d68e7f 2021-07-03 stsp print_commits = 1;
1167 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1169 b3d68e7f 2021-07-03 stsp if (a->last_npurged != npurged) {
1170 b3d68e7f 2021-07-03 stsp print_loose = 1;
1171 b3d68e7f 2021-07-03 stsp print_commits = 1;
1172 b3d68e7f 2021-07-03 stsp print_purged = 1;
1173 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1175 9a7c12cf 2023-06-18 op if (a->last_nredundant != nredundant) {
1176 900499fd 2023-06-23 stsp print_loose = 1;
1177 900499fd 2023-06-23 stsp print_commits = 1;
1178 900499fd 2023-06-23 stsp print_purged = 1;
1179 9a7c12cf 2023-06-18 op print_redundant = 1;
1180 9a7c12cf 2023-06-18 op a->last_nredundant = nredundant;
1183 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1184 b3d68e7f 2021-07-03 stsp return NULL;
1186 9a7c12cf 2023-06-18 op if (print_loose || print_commits || print_purged || print_redundant)
1187 b3d68e7f 2021-07-03 stsp printf("\r");
1188 b3d68e7f 2021-07-03 stsp if (print_loose)
1189 b3d68e7f 2021-07-03 stsp printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1190 b3d68e7f 2021-07-03 stsp if (print_commits)
1191 b3d68e7f 2021-07-03 stsp printf("; %d commit%s scanned", ncommits,
1192 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1193 900499fd 2023-06-23 stsp if (print_purged || print_redundant) {
1194 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1195 900499fd 2023-06-23 stsp printf("; could purge %d object%s", npurged,
1196 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1198 900499fd 2023-06-23 stsp printf("; purged %d object%s", npurged,
1199 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1202 9a7c12cf 2023-06-18 op if (print_redundant) {
1203 9a7c12cf 2023-06-18 op if (a->dry_run) {
1204 900499fd 2023-06-23 stsp printf(", %d pack file%s", nredundant,
1205 9a7c12cf 2023-06-18 op nredundant == 1 ? "" : "s");
1207 900499fd 2023-06-23 stsp printf(", %d pack file%s", nredundant,
1208 9a7c12cf 2023-06-18 op nredundant == 1 ? "" : "s");
1211 9a7c12cf 2023-06-18 op if (print_loose || print_commits || print_purged || print_redundant) {
1212 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1213 b3d68e7f 2021-07-03 stsp fflush(stdout);
1215 b3d68e7f 2021-07-03 stsp return NULL;
1218 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1219 1124fe40 2021-07-07 stsp int verbosity;
1220 1124fe40 2021-07-07 stsp int printed_something;
1221 1124fe40 2021-07-07 stsp int dry_run;
1224 b3d68e7f 2021-07-03 stsp static const struct got_error *
1225 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1227 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1229 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1230 1124fe40 2021-07-07 stsp return NULL;
1232 1124fe40 2021-07-07 stsp if (a->dry_run)
1233 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1235 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1237 1124fe40 2021-07-07 stsp a->printed_something = 1;
1238 1124fe40 2021-07-07 stsp return NULL;
1241 1124fe40 2021-07-07 stsp static const struct got_error *
1242 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1244 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1245 7d69d862 2021-11-15 stsp char *repo_path = NULL;
1246 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1247 900499fd 2023-06-23 stsp int ch, dry_run = 0, verbosity = 0;
1248 900499fd 2023-06-23 stsp int ncommits = 0, nloose = 0, npacked = 0;
1249 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1250 3bf0e21f 2023-06-19 op struct got_lockfile *lock = NULL;
1251 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1252 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1253 9a7c12cf 2023-06-18 op off_t loose_before, loose_after;
1254 9a7c12cf 2023-06-18 op off_t pack_before, pack_after;
1255 9a7c12cf 2023-06-18 op off_t total_size;
1256 9a7c12cf 2023-06-18 op char loose_before_scaled[FMT_SCALED_STRSIZE];
1257 9a7c12cf 2023-06-18 op char loose_after_scaled[FMT_SCALED_STRSIZE];
1258 9a7c12cf 2023-06-18 op char pack_before_scaled[FMT_SCALED_STRSIZE];
1259 9a7c12cf 2023-06-18 op char pack_after_scaled[FMT_SCALED_STRSIZE];
1260 9a7c12cf 2023-06-18 op char total_size_scaled[FMT_SCALED_STRSIZE];
1261 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
1263 1fa0d17d 2023-02-13 op #ifndef PROFILE
1264 1fa0d17d 2023-02-13 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1265 1fa0d17d 2023-02-13 op NULL) == -1)
1266 1fa0d17d 2023-02-13 op err(1, "pledge");
1269 6f319063 2022-10-27 stsp while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1270 b3d68e7f 2021-07-03 stsp switch (ch) {
1272 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1275 6f319063 2022-10-27 stsp dry_run = 1;
1278 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1281 6f319063 2022-10-27 stsp verbosity = -1;
1284 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1285 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1286 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1288 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1291 b3d68e7f 2021-07-03 stsp usage_cleanup();
1292 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1296 b3d68e7f 2021-07-03 stsp argc -= optind;
1297 b3d68e7f 2021-07-03 stsp argv += optind;
1299 7d69d862 2021-11-15 stsp if (repo_path == NULL) {
1300 7d69d862 2021-11-15 stsp error = get_repo_path(&repo_path);
1301 7d69d862 2021-11-15 stsp if (error)
1302 7d69d862 2021-11-15 stsp goto done;
1304 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
1305 0ae84acc 2022-06-15 tracey if (error != NULL)
1306 0ae84acc 2022-06-15 tracey goto done;
1307 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1308 b3d68e7f 2021-07-03 stsp if (error)
1309 b3d68e7f 2021-07-03 stsp goto done;
1311 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1312 b3d68e7f 2021-07-03 stsp if (error)
1313 b3d68e7f 2021-07-03 stsp goto done;
1315 798586ca 2023-02-05 op if (got_repo_has_extension(repo, "preciousObjects")) {
1316 798586ca 2023-02-05 op error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1317 798586ca 2023-02-05 op "the preciousObjects Git extension is enabled; "
1318 798586ca 2023-02-05 op "this implies that objects must not be deleted");
1322 3bf0e21f 2023-06-19 op error = got_repo_cleanup_prepare(repo, &lock);
1326 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1327 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1328 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1329 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1330 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1331 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1332 1124fe40 2021-07-07 stsp goto done;
1335 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1336 b3d68e7f 2021-07-03 stsp cpa.last_ncommits = -1;
1337 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1338 9a7c12cf 2023-06-18 op cpa.last_nredundant = -1;
1339 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1340 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1342 b3d68e7f 2021-07-03 stsp error = got_repo_purge_unreferenced_loose_objects(repo,
1343 900499fd 2023-06-23 stsp &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1344 900499fd 2023-06-23 stsp dry_run, ignore_mtime, cleanup_progress, &cpa,
1345 900499fd 2023-06-23 stsp check_cancelled, NULL);
1346 900499fd 2023-06-23 stsp if (error) {
1347 900499fd 2023-06-23 stsp if (cpa.printed_something)
1348 900499fd 2023-06-23 stsp printf("\n");
1349 900499fd 2023-06-23 stsp goto done;
1352 900499fd 2023-06-23 stsp error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1353 900499fd 2023-06-23 stsp &pack_after, dry_run, ncommits, nloose, npacked,
1354 abc59930 2021-09-05 naddy cleanup_progress, &cpa, check_cancelled, NULL);
1355 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1356 b3d68e7f 2021-07-03 stsp printf("\n");
1360 9a7c12cf 2023-06-18 op total_size = (loose_before - loose_after) + (pack_before - pack_after);
1362 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1363 9a7c12cf 2023-06-18 op if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1364 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1365 b3d68e7f 2021-07-03 stsp goto done;
1367 9a7c12cf 2023-06-18 op if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1368 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1369 b3d68e7f 2021-07-03 stsp goto done;
1371 9a7c12cf 2023-06-18 op if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1372 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1373 b3d68e7f 2021-07-03 stsp goto done;
1375 9a7c12cf 2023-06-18 op if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1376 9a7c12cf 2023-06-18 op error = got_error_from_errno("fmt_scaled");
1379 9a7c12cf 2023-06-18 op if (fmt_scaled(total_size, total_size_scaled) == -1) {
1380 9a7c12cf 2023-06-18 op error = got_error_from_errno("fmt_scaled");
1383 9a7c12cf 2023-06-18 op printf("loose total size before: %s\n", loose_before_scaled);
1384 9a7c12cf 2023-06-18 op printf("loose total size after: %s\n", loose_after_scaled);
1385 9a7c12cf 2023-06-18 op printf("pack files total size before: %s\n",
1386 9a7c12cf 2023-06-18 op pack_before_scaled);
1387 9a7c12cf 2023-06-18 op printf("pack files total size after: %s\n", pack_after_scaled);
1388 b3d68e7f 2021-07-03 stsp if (dry_run) {
1389 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1390 9a7c12cf 2023-06-18 op total_size_scaled);
1392 9a7c12cf 2023-06-18 op printf("disk space freed: %s\n", total_size_scaled);
1393 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1397 3bf0e21f 2023-06-19 op got_repo_cleanup_complete(repo, lock);
1399 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1400 0ae84acc 2022-06-15 tracey if (pack_fds) {
1401 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
1402 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
1403 2df845d5 2023-07-07 op if (error == NULL)
1404 2df845d5 2023-07-07 op error = pack_err;
1406 2df845d5 2023-07-07 op free(repo_path);
1407 2df845d5 2023-07-07 op return error;
1410 2df845d5 2023-07-07 op __dead static void
1411 2df845d5 2023-07-07 op usage_dump(void)
1413 2df845d5 2023-07-07 op fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1414 2df845d5 2023-07-07 op "[-x reference] [reference]...\n", getprogname());
1418 2df845d5 2023-07-07 op static const struct got_error *
1419 2df845d5 2023-07-07 op cmd_dump(int argc, char *argv[])
1421 2df845d5 2023-07-07 op const struct got_error *error = NULL;
1422 2df845d5 2023-07-07 op struct got_pack_progress_arg ppa;
1423 2df845d5 2023-07-07 op struct got_repository *repo = NULL;
1424 2df845d5 2023-07-07 op struct got_pathlist_head exclude_args;
1425 2df845d5 2023-07-07 op struct got_pathlist_entry *pe;
1426 2df845d5 2023-07-07 op struct got_reflist_head exclude_refs;
1427 2df845d5 2023-07-07 op struct got_reflist_head include_refs;
1428 2df845d5 2023-07-07 op struct got_reflist_entry *re, *new;
1429 2df845d5 2023-07-07 op const char *refname;
1430 2df845d5 2023-07-07 op char *repo_path = NULL;
1431 2df845d5 2023-07-07 op int *pack_fds = NULL;
1432 2df845d5 2023-07-07 op int verbosity = 0;
1435 2df845d5 2023-07-07 op TAILQ_INIT(&exclude_args);
1436 2df845d5 2023-07-07 op TAILQ_INIT(&exclude_refs);
1437 2df845d5 2023-07-07 op TAILQ_INIT(&include_refs);
1439 2df845d5 2023-07-07 op #ifndef PROFILE
1440 2df845d5 2023-07-07 op if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1441 2df845d5 2023-07-07 op NULL) == -1)
1442 2df845d5 2023-07-07 op err(1, "pledge");
1445 2df845d5 2023-07-07 op while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1446 2df845d5 2023-07-07 op switch (ch) {
1448 2df845d5 2023-07-07 op verbosity = -1;
1451 2df845d5 2023-07-07 op repo_path = realpath(optarg, NULL);
1452 2df845d5 2023-07-07 op if (repo_path == NULL)
1453 2df845d5 2023-07-07 op return got_error_from_errno2("realpath",
1455 2df845d5 2023-07-07 op got_path_strip_trailing_slashes(repo_path);
1458 2df845d5 2023-07-07 op error = got_pathlist_append(&exclude_args,
1459 2df845d5 2023-07-07 op optarg, NULL);
1461 2df845d5 2023-07-07 op return error;
1464 2df845d5 2023-07-07 op usage_dump();
1465 2df845d5 2023-07-07 op /* NOTREACHED */
1468 2df845d5 2023-07-07 op argc -= optind;
1469 2df845d5 2023-07-07 op argv += optind;
1471 2df845d5 2023-07-07 op if (repo_path == NULL) {
1472 2df845d5 2023-07-07 op error = get_repo_path(&repo_path);
1476 2df845d5 2023-07-07 op error = got_repo_pack_fds_open(&pack_fds);
1477 2df845d5 2023-07-07 op if (error != NULL)
1479 2df845d5 2023-07-07 op error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1483 2df845d5 2023-07-07 op error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1487 2df845d5 2023-07-07 op TAILQ_FOREACH(pe, &exclude_args, entry) {
1488 2df845d5 2023-07-07 op refname = pe->path;
1489 2df845d5 2023-07-07 op error = add_ref(&new, &exclude_refs, refname, repo);
1494 2df845d5 2023-07-07 op if (argc == 0) {
1495 2df845d5 2023-07-07 op error = got_ref_list(&include_refs, repo, "",
1496 2df845d5 2023-07-07 op got_ref_cmp_by_name, NULL);
1500 2df845d5 2023-07-07 op for (i = 0; i < argc; i++) {
1501 2df845d5 2023-07-07 op got_path_strip_trailing_slashes(argv[i]);
1502 2df845d5 2023-07-07 op refname = argv[i];
1503 2df845d5 2023-07-07 op error = add_ref(&new, &include_refs, refname, repo);
1509 2df845d5 2023-07-07 op /* Ignore references in the refs/got/ namespace. */
1510 2df845d5 2023-07-07 op TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1511 2df845d5 2023-07-07 op refname = got_ref_get_name(re->ref);
1512 2df845d5 2023-07-07 op if (strncmp("refs/got/", refname, 9) != 0)
1514 2df845d5 2023-07-07 op TAILQ_REMOVE(&include_refs, re, entry);
1515 2df845d5 2023-07-07 op got_ref_close(re->ref);
1519 2df845d5 2023-07-07 op memset(&ppa, 0, sizeof(ppa));
1520 2df845d5 2023-07-07 op ppa.out = stderr;
1521 2df845d5 2023-07-07 op ppa.verbosity = verbosity;
1523 2df845d5 2023-07-07 op error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1524 2df845d5 2023-07-07 op repo, pack_progress, &ppa, check_cancelled, NULL);
1525 2df845d5 2023-07-07 op if (ppa.printed_something)
1526 2df845d5 2023-07-07 op fprintf(stderr, "\n");
1529 2df845d5 2023-07-07 op got_repo_close(repo);
1531 2df845d5 2023-07-07 op if (pack_fds) {
1532 2df845d5 2023-07-07 op const struct got_error *pack_err;
1534 2df845d5 2023-07-07 op pack_err = got_repo_pack_fds_close(pack_fds);
1535 0ae84acc 2022-06-15 tracey if (error == NULL)
1536 0ae84acc 2022-06-15 tracey error = pack_err;
1539 2df845d5 2023-07-07 op got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1540 2df845d5 2023-07-07 op got_ref_list_free(&exclude_refs);
1541 2df845d5 2023-07-07 op got_ref_list_free(&include_refs);
1542 7d69d862 2021-11-15 stsp free(repo_path);
1544 b3d68e7f 2021-07-03 stsp return error;