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 05118f5a 2021-06-22 stsp #include <sys/types.h>
19 05118f5a 2021-06-22 stsp #include <ctype.h>
20 20662ea0 2021-04-10 stsp #include <getopt.h>
21 20662ea0 2021-04-10 stsp #include <err.h>
22 20662ea0 2021-04-10 stsp #include <errno.h>
23 20662ea0 2021-04-10 stsp #include <locale.h>
24 05118f5a 2021-06-22 stsp #include <inttypes.h>
25 20662ea0 2021-04-10 stsp #include <stdio.h>
26 20662ea0 2021-04-10 stsp #include <stdlib.h>
27 20662ea0 2021-04-10 stsp #include <signal.h>
28 20662ea0 2021-04-10 stsp #include <string.h>
29 20662ea0 2021-04-10 stsp #include <unistd.h>
31 2b0eee35 2021-09-21 thomas.ad #include "got_compat.h"
33 20662ea0 2021-04-10 stsp #include "got_version.h"
34 20662ea0 2021-04-10 stsp #include "got_error.h"
35 20662ea0 2021-04-10 stsp #include "got_object.h"
36 20662ea0 2021-04-10 stsp #include "got_reference.h"
37 05118f5a 2021-06-22 stsp #include "got_cancel.h"
38 20662ea0 2021-04-10 stsp #include "got_repository.h"
39 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
40 20662ea0 2021-04-10 stsp #include "got_gotconfig.h"
41 20662ea0 2021-04-10 stsp #include "got_path.h"
42 20662ea0 2021-04-10 stsp #include "got_privsep.h"
43 20662ea0 2021-04-10 stsp #include "got_opentemp.h"
44 1ea7ccc6 2021-11-15 thomas #include "got_worktree.h"
46 20662ea0 2021-04-10 stsp #ifndef nitems
47 20662ea0 2021-04-10 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
54 20662ea0 2021-04-10 stsp catch_sigint(int signo)
56 20662ea0 2021-04-10 stsp sigint_received = 1;
60 20662ea0 2021-04-10 stsp catch_sigpipe(int signo)
62 20662ea0 2021-04-10 stsp sigpipe_received = 1;
65 05118f5a 2021-06-22 stsp static const struct got_error *
66 05118f5a 2021-06-22 stsp check_cancelled(void *arg)
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;
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;
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 b3d68e7f 2021-07-03 stsp __dead static void usage_cleanup(void);
87 20662ea0 2021-04-10 stsp static const struct got_error* cmd_info(int, char *[]);
88 05118f5a 2021-06-22 stsp static const struct got_error* cmd_pack(int, char *[]);
89 05118f5a 2021-06-22 stsp static const struct got_error* cmd_indexpack(int, char *[]);
90 05118f5a 2021-06-22 stsp static const struct got_error* cmd_listpack(int, char *[]);
91 b3d68e7f 2021-07-03 stsp static const struct got_error* cmd_cleanup(int, char *[]);
93 641a8ee6 2022-02-16 thomas static const struct gotadmin_cmd gotadmin_commands[] = {
94 20662ea0 2021-04-10 stsp { "info", cmd_info, usage_info, "" },
95 05118f5a 2021-06-22 stsp { "pack", cmd_pack, usage_pack, "" },
96 05118f5a 2021-06-22 stsp { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
97 05118f5a 2021-06-22 stsp { "listpack", cmd_listpack, usage_listpack, "ls" },
98 b3d68e7f 2021-07-03 stsp { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
101 20662ea0 2021-04-10 stsp static void
102 20662ea0 2021-04-10 stsp list_commands(FILE *fp)
106 20662ea0 2021-04-10 stsp fprintf(fp, "commands:");
107 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
108 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
109 20662ea0 2021-04-10 stsp fprintf(fp, " %s", cmd->cmd_name);
111 20662ea0 2021-04-10 stsp fputc('\n', fp);
115 20662ea0 2021-04-10 stsp main(int argc, char *argv[])
117 641a8ee6 2022-02-16 thomas const struct gotadmin_cmd *cmd;
120 20662ea0 2021-04-10 stsp int hflag = 0, Vflag = 0;
121 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
122 20662ea0 2021-04-10 stsp { "version", no_argument, NULL, 'V' },
123 20662ea0 2021-04-10 stsp { NULL, 0, NULL, 0 }
126 20662ea0 2021-04-10 stsp setlocale(LC_CTYPE, "");
128 20662ea0 2021-04-10 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
129 20662ea0 2021-04-10 stsp switch (ch) {
137 20662ea0 2021-04-10 stsp usage(hflag, 1);
138 20662ea0 2021-04-10 stsp /* NOTREACHED */
142 20662ea0 2021-04-10 stsp argc -= optind;
143 20662ea0 2021-04-10 stsp argv += optind;
144 20662ea0 2021-04-10 stsp optind = 1;
145 20662ea0 2021-04-10 stsp optreset = 1;
147 20662ea0 2021-04-10 stsp if (Vflag) {
148 20662ea0 2021-04-10 stsp got_version_print_str();
152 20662ea0 2021-04-10 stsp if (argc <= 0)
153 20662ea0 2021-04-10 stsp usage(hflag, hflag ? 0 : 1);
155 20662ea0 2021-04-10 stsp signal(SIGINT, catch_sigint);
156 20662ea0 2021-04-10 stsp signal(SIGPIPE, catch_sigpipe);
158 20662ea0 2021-04-10 stsp for (i = 0; i < nitems(gotadmin_commands); i++) {
159 20662ea0 2021-04-10 stsp const struct got_error *error;
161 20662ea0 2021-04-10 stsp cmd = &gotadmin_commands[i];
163 20662ea0 2021-04-10 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
164 20662ea0 2021-04-10 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
168 641a8ee6 2022-02-16 thomas cmd->cmd_usage();
170 641a8ee6 2022-02-16 thomas error = cmd->cmd_main(argc, argv);
171 20662ea0 2021-04-10 stsp if (error && error->code != GOT_ERR_CANCELLED &&
172 20662ea0 2021-04-10 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
173 20662ea0 2021-04-10 stsp !(sigpipe_received &&
174 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
175 20662ea0 2021-04-10 stsp !(sigint_received &&
176 20662ea0 2021-04-10 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
177 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
184 20662ea0 2021-04-10 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 20662ea0 2021-04-10 stsp list_commands(stderr);
189 20662ea0 2021-04-10 stsp __dead static void
190 20662ea0 2021-04-10 stsp usage(int hflag, int status)
192 20662ea0 2021-04-10 stsp FILE *fp = (status == 0) ? stdout : stderr;
194 20662ea0 2021-04-10 stsp fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
195 20662ea0 2021-04-10 stsp getprogname());
197 20662ea0 2021-04-10 stsp list_commands(fp);
198 20662ea0 2021-04-10 stsp exit(status);
201 20662ea0 2021-04-10 stsp static const struct got_error *
202 20662ea0 2021-04-10 stsp apply_unveil(const char *repo_path, int repo_read_only)
204 20662ea0 2021-04-10 stsp const struct got_error *err;
206 20662ea0 2021-04-10 stsp #ifdef PROFILE
207 20662ea0 2021-04-10 stsp if (unveil("gmon.out", "rwc") != 0)
208 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", "gmon.out");
210 20662ea0 2021-04-10 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
211 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", repo_path);
213 20662ea0 2021-04-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
214 20662ea0 2021-04-10 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
216 20662ea0 2021-04-10 stsp err = got_privsep_unveil_exec_helpers();
217 20662ea0 2021-04-10 stsp if (err != NULL)
218 20662ea0 2021-04-10 stsp return err;
220 20662ea0 2021-04-10 stsp if (unveil(NULL, NULL) != 0)
221 20662ea0 2021-04-10 stsp return got_error_from_errno("unveil");
223 20662ea0 2021-04-10 stsp return NULL;
226 20662ea0 2021-04-10 stsp __dead static void
227 20662ea0 2021-04-10 stsp usage_info(void)
229 20662ea0 2021-04-10 stsp fprintf(stderr, "usage: %s info [-r repository-path]\n",
230 20662ea0 2021-04-10 stsp getprogname());
234 20662ea0 2021-04-10 stsp static const struct got_error *
235 1ea7ccc6 2021-11-15 thomas get_repo_path(char **repo_path)
237 1ea7ccc6 2021-11-15 thomas const struct got_error *err = NULL;
238 1ea7ccc6 2021-11-15 thomas struct got_worktree *worktree = NULL;
239 1ea7ccc6 2021-11-15 thomas char *cwd;
241 1ea7ccc6 2021-11-15 thomas *repo_path = NULL;
243 1ea7ccc6 2021-11-15 thomas cwd = getcwd(NULL, 0);
244 1ea7ccc6 2021-11-15 thomas if (cwd == NULL)
245 1ea7ccc6 2021-11-15 thomas return got_error_from_errno("getcwd");
247 1ea7ccc6 2021-11-15 thomas err = got_worktree_open(&worktree, cwd);
248 1ea7ccc6 2021-11-15 thomas if (err) {
249 1ea7ccc6 2021-11-15 thomas if (err->code != GOT_ERR_NOT_WORKTREE)
250 1ea7ccc6 2021-11-15 thomas goto done;
251 1ea7ccc6 2021-11-15 thomas err = NULL;
254 1ea7ccc6 2021-11-15 thomas if (worktree)
255 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 1ea7ccc6 2021-11-15 thomas *repo_path = strdup(cwd);
258 1ea7ccc6 2021-11-15 thomas if (*repo_path == NULL)
259 1ea7ccc6 2021-11-15 thomas err = got_error_from_errno("strdup");
261 1ea7ccc6 2021-11-15 thomas if (worktree)
262 1ea7ccc6 2021-11-15 thomas got_worktree_close(worktree);
263 1ea7ccc6 2021-11-15 thomas free(cwd);
264 1ea7ccc6 2021-11-15 thomas return err;
267 1ea7ccc6 2021-11-15 thomas static const struct got_error *
268 20662ea0 2021-04-10 stsp cmd_info(int argc, char *argv[])
270 20662ea0 2021-04-10 stsp const struct got_error *error = NULL;
271 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
272 20662ea0 2021-04-10 stsp struct got_repository *repo = NULL;
273 20662ea0 2021-04-10 stsp const struct got_gotconfig *gotconfig = NULL;
274 20662ea0 2021-04-10 stsp int ch, npackfiles, npackedobj, nobj;
275 20662ea0 2021-04-10 stsp off_t packsize, loose_size;
276 20662ea0 2021-04-10 stsp char scaled[FMT_SCALED_STRSIZE];
277 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
279 20662ea0 2021-04-10 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
280 20662ea0 2021-04-10 stsp switch (ch) {
282 20662ea0 2021-04-10 stsp repo_path = realpath(optarg, NULL);
283 20662ea0 2021-04-10 stsp if (repo_path == NULL)
284 20662ea0 2021-04-10 stsp return got_error_from_errno2("realpath",
286 20662ea0 2021-04-10 stsp got_path_strip_trailing_slashes(repo_path);
289 20662ea0 2021-04-10 stsp usage_info();
290 20662ea0 2021-04-10 stsp /* NOTREACHED */
294 20662ea0 2021-04-10 stsp argc -= optind;
295 20662ea0 2021-04-10 stsp argv += optind;
297 20662ea0 2021-04-10 stsp #ifndef PROFILE
298 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
299 20662ea0 2021-04-10 stsp NULL) == -1)
300 20662ea0 2021-04-10 stsp err(1, "pledge");
302 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
303 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
304 1ea7ccc6 2021-11-15 thomas if (error)
305 1ea7ccc6 2021-11-15 thomas goto done;
307 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
308 7cd52833 2022-06-23 thomas if (error != NULL)
309 7cd52833 2022-06-23 thomas goto done;
310 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
313 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
314 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
315 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
316 bfb5ee0b 2022-05-31 thomas NULL) == -1)
317 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
319 20662ea0 2021-04-10 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
323 20662ea0 2021-04-10 stsp printf("repository: %s\n", got_repo_get_path_git_dir(repo));
325 20662ea0 2021-04-10 stsp gotconfig = got_repo_get_gotconfig(repo);
326 20662ea0 2021-04-10 stsp if (gotconfig) {
327 20662ea0 2021-04-10 stsp const struct got_remote_repo *remotes;
328 20662ea0 2021-04-10 stsp int i, nremotes;
329 20662ea0 2021-04-10 stsp if (got_gotconfig_get_author(gotconfig)) {
330 20662ea0 2021-04-10 stsp printf("default author: %s\n",
331 20662ea0 2021-04-10 stsp got_gotconfig_get_author(gotconfig));
333 20662ea0 2021-04-10 stsp got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
334 20662ea0 2021-04-10 stsp for (i = 0; i < nremotes; i++) {
335 13b2084e 2021-09-06 stsp const char *fetch_url = remotes[i].fetch_url;
336 13b2084e 2021-09-06 stsp const char *send_url = remotes[i].send_url;
337 13b2084e 2021-09-06 stsp if (strcmp(fetch_url, send_url) == 0) {
338 13b2084e 2021-09-06 stsp printf("remote \"%s\": %s\n", remotes[i].name,
339 13b2084e 2021-09-06 stsp remotes[i].fetch_url);
341 13b2084e 2021-09-06 stsp printf("remote \"%s\" (fetch): %s\n",
342 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].fetch_url);
343 13b2084e 2021-09-06 stsp printf("remote \"%s\" (send): %s\n",
344 13b2084e 2021-09-06 stsp remotes[i].name, remotes[i].send_url);
349 20662ea0 2021-04-10 stsp error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
350 20662ea0 2021-04-10 stsp &packsize, repo);
353 20662ea0 2021-04-10 stsp printf("pack files: %d\n", npackfiles);
354 20662ea0 2021-04-10 stsp if (npackfiles > 0) {
355 20662ea0 2021-04-10 stsp if (fmt_scaled(packsize, scaled) == -1) {
356 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
359 20662ea0 2021-04-10 stsp printf("packed objects: %d\n", npackedobj);
360 20662ea0 2021-04-10 stsp printf("packed total size: %s\n", scaled);
363 20662ea0 2021-04-10 stsp error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
366 20662ea0 2021-04-10 stsp printf("loose objects: %d\n", nobj);
367 20662ea0 2021-04-10 stsp if (nobj > 0) {
368 20662ea0 2021-04-10 stsp if (fmt_scaled(loose_size, scaled) == -1) {
369 20662ea0 2021-04-10 stsp error = got_error_from_errno("fmt_scaled");
372 20662ea0 2021-04-10 stsp printf("loose total size: %s\n", scaled);
376 20662ea0 2021-04-10 stsp got_repo_close(repo);
377 7cd52833 2022-06-23 thomas if (pack_fds) {
378 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
379 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
380 7cd52833 2022-06-23 thomas if (error == NULL)
381 7cd52833 2022-06-23 thomas error = pack_err;
384 1ea7ccc6 2021-11-15 thomas free(repo_path);
385 20662ea0 2021-04-10 stsp return error;
388 05118f5a 2021-06-22 stsp __dead static void
389 05118f5a 2021-06-22 stsp usage_pack(void)
391 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
392 2cdcfdc6 2022-04-23 thomas "[-x reference] [-q] [reference ...]\n",
393 05118f5a 2021-06-22 stsp getprogname());
397 05118f5a 2021-06-22 stsp struct got_pack_progress_arg {
398 05118f5a 2021-06-22 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
399 85220b0e 2022-03-22 thomas int last_ncolored;
400 85220b0e 2022-03-22 thomas int last_nfound;
401 85220b0e 2022-03-22 thomas int last_ntrees;
402 85220b0e 2022-03-22 thomas int loading_done;
403 05118f5a 2021-06-22 stsp int last_ncommits;
404 05118f5a 2021-06-22 stsp int last_nobj_total;
405 05118f5a 2021-06-22 stsp int last_p_deltify;
406 05118f5a 2021-06-22 stsp int last_p_written;
407 05118f5a 2021-06-22 stsp int last_p_indexed;
408 05118f5a 2021-06-22 stsp int last_p_resolved;
409 05118f5a 2021-06-22 stsp int verbosity;
410 05118f5a 2021-06-22 stsp int printed_something;
413 85220b0e 2022-03-22 thomas static void
414 85220b0e 2022-03-22 thomas print_load_info(int print_colored, int print_found, int print_trees,
415 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees)
417 85220b0e 2022-03-22 thomas if (print_colored) {
418 85220b0e 2022-03-22 thomas printf("%d commit%s colored", ncolored,
419 85220b0e 2022-03-22 thomas ncolored == 1 ? "" : "s");
421 85220b0e 2022-03-22 thomas if (print_found) {
422 85220b0e 2022-03-22 thomas printf("%s%d object%s found",
423 85220b0e 2022-03-22 thomas ncolored > 0 ? "; " : "",
424 85220b0e 2022-03-22 thomas nfound, nfound == 1 ? "" : "s");
426 85220b0e 2022-03-22 thomas if (print_trees) {
427 85220b0e 2022-03-22 thomas printf("; %d tree%s scanned", ntrees,
428 85220b0e 2022-03-22 thomas ntrees == 1 ? "" : "s");
432 05118f5a 2021-06-22 stsp static const struct got_error *
433 85220b0e 2022-03-22 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
434 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
435 85220b0e 2022-03-22 thomas int nobj_written)
437 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
438 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
439 05118f5a 2021-06-22 stsp int p_deltify, p_written;
440 85220b0e 2022-03-22 thomas int print_colored = 0, print_found = 0, print_trees = 0;
441 05118f5a 2021-06-22 stsp int print_searching = 0, print_total = 0;
442 05118f5a 2021-06-22 stsp int print_deltify = 0, print_written = 0;
444 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
445 85220b0e 2022-03-22 thomas return NULL;
447 85220b0e 2022-03-22 thomas if (a->last_ncolored != ncolored) {
448 85220b0e 2022-03-22 thomas print_colored = 1;
449 85220b0e 2022-03-22 thomas a->last_ncolored = ncolored;
452 85220b0e 2022-03-22 thomas if (a->last_nfound != nfound) {
453 85220b0e 2022-03-22 thomas print_colored = 1;
454 85220b0e 2022-03-22 thomas print_found = 1;
455 85220b0e 2022-03-22 thomas a->last_nfound = nfound;
458 85220b0e 2022-03-22 thomas if (a->last_ntrees != ntrees) {
459 85220b0e 2022-03-22 thomas print_colored = 1;
460 85220b0e 2022-03-22 thomas print_found = 1;
461 85220b0e 2022-03-22 thomas print_trees = 1;
462 85220b0e 2022-03-22 thomas a->last_ntrees = ntrees;
465 85220b0e 2022-03-22 thomas if ((print_colored || print_found || print_trees) &&
466 85220b0e 2022-03-22 thomas !a->loading_done) {
467 85220b0e 2022-03-22 thomas printf("\r");
468 85220b0e 2022-03-22 thomas print_load_info(print_colored, print_found, print_trees,
469 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees);
470 85220b0e 2022-03-22 thomas a->printed_something = 1;
471 85220b0e 2022-03-22 thomas fflush(stdout);
472 05118f5a 2021-06-22 stsp return NULL;
473 85220b0e 2022-03-22 thomas } else if (!a->loading_done) {
474 85220b0e 2022-03-22 thomas printf("\r");
475 85220b0e 2022-03-22 thomas print_load_info(1, 1, 1, ncolored, nfound, ntrees);
476 85220b0e 2022-03-22 thomas printf("\n");
477 85220b0e 2022-03-22 thomas a->loading_done = 1;
480 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == -1)
481 05118f5a 2021-06-22 stsp return got_error_from_errno("fmt_scaled");
483 05118f5a 2021-06-22 stsp if (a->last_ncommits != ncommits) {
484 05118f5a 2021-06-22 stsp print_searching = 1;
485 05118f5a 2021-06-22 stsp a->last_ncommits = ncommits;
488 05118f5a 2021-06-22 stsp if (a->last_nobj_total != nobj_total) {
489 05118f5a 2021-06-22 stsp print_searching = 1;
490 05118f5a 2021-06-22 stsp print_total = 1;
491 05118f5a 2021-06-22 stsp a->last_nobj_total = nobj_total;
494 05118f5a 2021-06-22 stsp if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
495 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
496 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
497 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
498 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
501 05118f5a 2021-06-22 stsp if (nobj_deltify > 0 || nobj_written > 0) {
502 05118f5a 2021-06-22 stsp if (nobj_deltify > 0) {
503 05118f5a 2021-06-22 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
504 05118f5a 2021-06-22 stsp if (p_deltify != a->last_p_deltify) {
505 05118f5a 2021-06-22 stsp a->last_p_deltify = p_deltify;
506 05118f5a 2021-06-22 stsp print_searching = 1;
507 05118f5a 2021-06-22 stsp print_total = 1;
508 05118f5a 2021-06-22 stsp print_deltify = 1;
511 05118f5a 2021-06-22 stsp if (nobj_written > 0) {
512 05118f5a 2021-06-22 stsp p_written = (nobj_written * 100) / nobj_total;
513 05118f5a 2021-06-22 stsp if (p_written != a->last_p_written) {
514 05118f5a 2021-06-22 stsp a->last_p_written = p_written;
515 05118f5a 2021-06-22 stsp print_searching = 1;
516 05118f5a 2021-06-22 stsp print_total = 1;
517 05118f5a 2021-06-22 stsp print_deltify = 1;
518 05118f5a 2021-06-22 stsp print_written = 1;
523 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify || print_written)
524 05118f5a 2021-06-22 stsp printf("\r");
525 05118f5a 2021-06-22 stsp if (print_searching)
526 05118f5a 2021-06-22 stsp printf("packing %d reference%s", ncommits,
527 05118f5a 2021-06-22 stsp ncommits == 1 ? "" : "s");
528 05118f5a 2021-06-22 stsp if (print_total)
529 05118f5a 2021-06-22 stsp printf("; %d object%s", nobj_total,
530 05118f5a 2021-06-22 stsp nobj_total == 1 ? "" : "s");
531 05118f5a 2021-06-22 stsp if (print_deltify)
532 05118f5a 2021-06-22 stsp printf("; deltify: %d%%", p_deltify);
533 05118f5a 2021-06-22 stsp if (print_written)
534 d2f35ef7 2022-02-13 thomas printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
535 05118f5a 2021-06-22 stsp scaled_size, p_written);
536 05118f5a 2021-06-22 stsp if (print_searching || print_total || print_deltify ||
537 05118f5a 2021-06-22 stsp print_written) {
538 05118f5a 2021-06-22 stsp a->printed_something = 1;
539 05118f5a 2021-06-22 stsp fflush(stdout);
541 05118f5a 2021-06-22 stsp return NULL;
544 05118f5a 2021-06-22 stsp static const struct got_error *
545 05118f5a 2021-06-22 stsp pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
546 05118f5a 2021-06-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
548 05118f5a 2021-06-22 stsp struct got_pack_progress_arg *a = arg;
549 05118f5a 2021-06-22 stsp char scaled_size[FMT_SCALED_STRSIZE];
550 05118f5a 2021-06-22 stsp int p_indexed, p_resolved;
551 05118f5a 2021-06-22 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
553 05118f5a 2021-06-22 stsp if (a->verbosity < 0)
554 05118f5a 2021-06-22 stsp return NULL;
556 05118f5a 2021-06-22 stsp if (packfile_size > 0 || nobj_indexed > 0) {
557 05118f5a 2021-06-22 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
558 05118f5a 2021-06-22 stsp (a->last_scaled_size[0] == '\0' ||
559 05118f5a 2021-06-22 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
560 05118f5a 2021-06-22 stsp print_size = 1;
561 05118f5a 2021-06-22 stsp if (strlcpy(a->last_scaled_size, scaled_size,
562 05118f5a 2021-06-22 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
563 05118f5a 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
565 05118f5a 2021-06-22 stsp if (nobj_indexed > 0) {
566 05118f5a 2021-06-22 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
567 05118f5a 2021-06-22 stsp if (p_indexed != a->last_p_indexed) {
568 05118f5a 2021-06-22 stsp a->last_p_indexed = p_indexed;
569 05118f5a 2021-06-22 stsp print_indexed = 1;
570 05118f5a 2021-06-22 stsp print_size = 1;
573 05118f5a 2021-06-22 stsp if (nobj_resolved > 0) {
574 05118f5a 2021-06-22 stsp p_resolved = (nobj_resolved * 100) /
575 05118f5a 2021-06-22 stsp (nobj_total - nobj_loose);
576 05118f5a 2021-06-22 stsp if (p_resolved != a->last_p_resolved) {
577 05118f5a 2021-06-22 stsp a->last_p_resolved = p_resolved;
578 05118f5a 2021-06-22 stsp print_resolved = 1;
579 05118f5a 2021-06-22 stsp print_indexed = 1;
580 05118f5a 2021-06-22 stsp print_size = 1;
585 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
586 05118f5a 2021-06-22 stsp printf("\r");
587 05118f5a 2021-06-22 stsp if (print_size)
588 d2f35ef7 2022-02-13 thomas printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
589 05118f5a 2021-06-22 stsp if (print_indexed)
590 05118f5a 2021-06-22 stsp printf("; indexing %d%%", p_indexed);
591 05118f5a 2021-06-22 stsp if (print_resolved)
592 05118f5a 2021-06-22 stsp printf("; resolving deltas %d%%", p_resolved);
593 05118f5a 2021-06-22 stsp if (print_size || print_indexed || print_resolved)
594 05118f5a 2021-06-22 stsp fflush(stdout);
596 05118f5a 2021-06-22 stsp return NULL;
599 05118f5a 2021-06-22 stsp static const struct got_error *
600 05118f5a 2021-06-22 stsp add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
601 05118f5a 2021-06-22 stsp const char *refname, struct got_repository *repo)
603 05118f5a 2021-06-22 stsp const struct got_error *err;
604 05118f5a 2021-06-22 stsp struct got_reference *ref;
606 05118f5a 2021-06-22 stsp *new = NULL;
608 05118f5a 2021-06-22 stsp err = got_ref_open(&ref, repo, refname, 0);
610 05118f5a 2021-06-22 stsp if (err->code != GOT_ERR_NOT_REF)
611 05118f5a 2021-06-22 stsp return err;
613 05118f5a 2021-06-22 stsp /* Treat argument as a reference prefix. */
614 05118f5a 2021-06-22 stsp err = got_ref_list(refs, repo, refname,
615 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
617 72acb3d8 2021-08-06 stsp err = got_reflist_insert(new, refs, ref,
618 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
619 05118f5a 2021-06-22 stsp if (err || *new == NULL /* duplicate */)
620 05118f5a 2021-06-22 stsp got_ref_close(ref);
623 05118f5a 2021-06-22 stsp return err;
626 05118f5a 2021-06-22 stsp static const struct got_error *
627 05118f5a 2021-06-22 stsp cmd_pack(int argc, char *argv[])
629 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
630 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
631 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
632 c8fc6d14 2022-04-16 thomas int ch, i, loose_obj_only = 1, verbosity = 0;
633 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
634 05118f5a 2021-06-22 stsp char *id_str = NULL;
635 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
636 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
637 05118f5a 2021-06-22 stsp struct got_pathlist_head exclude_args;
638 05118f5a 2021-06-22 stsp struct got_pathlist_entry *pe;
639 05118f5a 2021-06-22 stsp struct got_reflist_head exclude_refs;
640 05118f5a 2021-06-22 stsp struct got_reflist_head include_refs;
641 05118f5a 2021-06-22 stsp struct got_reflist_entry *re, *new;
642 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
644 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_args);
645 05118f5a 2021-06-22 stsp TAILQ_INIT(&exclude_refs);
646 05118f5a 2021-06-22 stsp TAILQ_INIT(&include_refs);
648 c8fc6d14 2022-04-16 thomas while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
649 05118f5a 2021-06-22 stsp switch (ch) {
651 05118f5a 2021-06-22 stsp loose_obj_only = 0;
654 05118f5a 2021-06-22 stsp repo_path = realpath(optarg, NULL);
655 05118f5a 2021-06-22 stsp if (repo_path == NULL)
656 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath",
658 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(repo_path);
661 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(optarg);
662 05118f5a 2021-06-22 stsp error = got_pathlist_append(&exclude_args,
663 05118f5a 2021-06-22 stsp optarg, NULL);
665 05118f5a 2021-06-22 stsp return error;
667 c8fc6d14 2022-04-16 thomas case 'q':
668 c8fc6d14 2022-04-16 thomas verbosity = -1;
671 05118f5a 2021-06-22 stsp usage_pack();
672 05118f5a 2021-06-22 stsp /* NOTREACHED */
676 05118f5a 2021-06-22 stsp argc -= optind;
677 05118f5a 2021-06-22 stsp argv += optind;
679 05118f5a 2021-06-22 stsp #ifndef PROFILE
680 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
681 05118f5a 2021-06-22 stsp NULL) == -1)
682 05118f5a 2021-06-22 stsp err(1, "pledge");
684 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
685 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
686 1ea7ccc6 2021-11-15 thomas if (error)
687 1ea7ccc6 2021-11-15 thomas goto done;
689 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
690 7cd52833 2022-06-23 thomas if (error != NULL)
691 7cd52833 2022-06-23 thomas goto done;
692 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
696 bb5126ea 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
700 05118f5a 2021-06-22 stsp TAILQ_FOREACH(pe, &exclude_args, entry) {
701 05118f5a 2021-06-22 stsp const char *refname = pe->path;
702 05118f5a 2021-06-22 stsp error = add_ref(&new, &exclude_refs, refname, repo);
708 05118f5a 2021-06-22 stsp if (argc == 0) {
709 05118f5a 2021-06-22 stsp error = got_ref_list(&include_refs, repo, "",
710 05118f5a 2021-06-22 stsp got_ref_cmp_by_name, NULL);
714 05118f5a 2021-06-22 stsp for (i = 0; i < argc; i++) {
715 05118f5a 2021-06-22 stsp const char *refname;
716 05118f5a 2021-06-22 stsp got_path_strip_trailing_slashes(argv[i]);
717 05118f5a 2021-06-22 stsp refname = argv[i];
718 05118f5a 2021-06-22 stsp error = add_ref(&new, &include_refs, refname, repo);
724 05118f5a 2021-06-22 stsp /* Ignore references in the refs/got/ namespace. */
725 05118f5a 2021-06-22 stsp TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
726 05118f5a 2021-06-22 stsp const char *refname = got_ref_get_name(re->ref);
727 05118f5a 2021-06-22 stsp if (strncmp("refs/got/", refname, 9) != 0)
729 05118f5a 2021-06-22 stsp TAILQ_REMOVE(&include_refs, re, entry);
730 05118f5a 2021-06-22 stsp got_ref_close(re->ref);
734 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
735 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
736 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
737 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
738 c8fc6d14 2022-04-16 thomas ppa.verbosity = verbosity;
740 05118f5a 2021-06-22 stsp error = got_repo_pack_objects(&packfile, &pack_hash,
741 05118f5a 2021-06-22 stsp &include_refs, &exclude_refs, repo, loose_obj_only,
742 05118f5a 2021-06-22 stsp pack_progress, &ppa, check_cancelled, NULL);
743 05118f5a 2021-06-22 stsp if (error) {
744 05118f5a 2021-06-22 stsp if (ppa.printed_something)
745 05118f5a 2021-06-22 stsp printf("\n");
749 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
752 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
753 c8fc6d14 2022-04-16 thomas printf("\nWrote %s.pack\n", id_str);
755 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
756 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
759 c8fc6d14 2022-04-16 thomas if (verbosity >= 0)
760 c8fc6d14 2022-04-16 thomas printf("\nIndexed %s.pack\n", id_str);
762 c75a6067 2021-10-15 thomas if (repo)
763 c75a6067 2021-10-15 thomas got_repo_close(repo);
764 7cd52833 2022-06-23 thomas if (pack_fds) {
765 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
766 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
767 7cd52833 2022-06-23 thomas if (error == NULL)
768 7cd52833 2022-06-23 thomas error = pack_err;
770 05118f5a 2021-06-22 stsp got_pathlist_free(&exclude_args);
771 05118f5a 2021-06-22 stsp got_ref_list_free(&exclude_refs);
772 05118f5a 2021-06-22 stsp got_ref_list_free(&include_refs);
773 05118f5a 2021-06-22 stsp free(id_str);
774 05118f5a 2021-06-22 stsp free(pack_hash);
775 1ea7ccc6 2021-11-15 thomas free(repo_path);
776 05118f5a 2021-06-22 stsp return error;
779 05118f5a 2021-06-22 stsp __dead static void
780 05118f5a 2021-06-22 stsp usage_indexpack(void)
782 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s indexpack packfile-path\n",
783 05118f5a 2021-06-22 stsp getprogname());
787 05118f5a 2021-06-22 stsp static const struct got_error *
788 05118f5a 2021-06-22 stsp cmd_indexpack(int argc, char *argv[])
790 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
791 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
793 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
794 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
795 05118f5a 2021-06-22 stsp char *id_str = NULL;
796 05118f5a 2021-06-22 stsp struct got_pack_progress_arg ppa;
797 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
798 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
800 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "")) != -1) {
801 05118f5a 2021-06-22 stsp switch (ch) {
803 05118f5a 2021-06-22 stsp usage_indexpack();
804 05118f5a 2021-06-22 stsp /* NOTREACHED */
808 05118f5a 2021-06-22 stsp argc -= optind;
809 05118f5a 2021-06-22 stsp argv += optind;
811 05118f5a 2021-06-22 stsp if (argc != 1)
812 05118f5a 2021-06-22 stsp usage_indexpack();
814 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
815 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
816 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
818 05118f5a 2021-06-22 stsp #ifndef PROFILE
819 05118f5a 2021-06-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
820 05118f5a 2021-06-22 stsp NULL) == -1)
821 05118f5a 2021-06-22 stsp err(1, "pledge");
824 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
825 7cd52833 2022-06-23 thomas if (error != NULL)
826 7cd52833 2022-06-23 thomas goto done;
827 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
831 001a95cd 2021-10-15 thomas error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
835 05118f5a 2021-06-22 stsp memset(&ppa, 0, sizeof(ppa));
836 05118f5a 2021-06-22 stsp ppa.last_scaled_size[0] = '\0';
837 05118f5a 2021-06-22 stsp ppa.last_p_indexed = -1;
838 05118f5a 2021-06-22 stsp ppa.last_p_resolved = -1;
840 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
841 05118f5a 2021-06-22 stsp packfile_path);
845 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
849 05118f5a 2021-06-22 stsp error = got_repo_index_pack(packfile, pack_hash, repo,
850 05118f5a 2021-06-22 stsp pack_index_progress, &ppa, check_cancelled, NULL);
853 05118f5a 2021-06-22 stsp printf("\nIndexed %s.pack\n", id_str);
855 c75a6067 2021-10-15 thomas if (repo)
856 c75a6067 2021-10-15 thomas got_repo_close(repo);
857 7cd52833 2022-06-23 thomas if (pack_fds) {
858 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
859 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
860 7cd52833 2022-06-23 thomas if (error == NULL)
861 7cd52833 2022-06-23 thomas error = pack_err;
863 05118f5a 2021-06-22 stsp free(id_str);
864 05118f5a 2021-06-22 stsp free(pack_hash);
865 05118f5a 2021-06-22 stsp return error;
868 05118f5a 2021-06-22 stsp __dead static void
869 05118f5a 2021-06-22 stsp usage_listpack(void)
871 05118f5a 2021-06-22 stsp fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
872 05118f5a 2021-06-22 stsp getprogname());
876 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args {
877 05118f5a 2021-06-22 stsp int nblobs;
878 05118f5a 2021-06-22 stsp int ntrees;
879 05118f5a 2021-06-22 stsp int ncommits;
881 05118f5a 2021-06-22 stsp int noffdeltas;
882 05118f5a 2021-06-22 stsp int nrefdeltas;
883 05118f5a 2021-06-22 stsp int human_readable;
886 05118f5a 2021-06-22 stsp static const struct got_error *
887 05118f5a 2021-06-22 stsp list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
888 05118f5a 2021-06-22 stsp off_t size, off_t base_offset, struct got_object_id *base_id)
890 05118f5a 2021-06-22 stsp const struct got_error *err;
891 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args *a = arg;
892 05118f5a 2021-06-22 stsp char *id_str, *delta_str = NULL, *base_id_str = NULL;
893 05118f5a 2021-06-22 stsp const char *type_str;
895 7cd52833 2022-06-23 thomas err = got_object_id_str(&id_str, id);
897 05118f5a 2021-06-22 stsp return err;
899 05118f5a 2021-06-22 stsp switch (type) {
900 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_BLOB:
901 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_BLOB;
902 05118f5a 2021-06-22 stsp a->nblobs++;
904 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
905 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TREE;
906 05118f5a 2021-06-22 stsp a->ntrees++;
908 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
909 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_COMMIT;
910 05118f5a 2021-06-22 stsp a->ncommits++;
912 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TAG:
913 05118f5a 2021-06-22 stsp type_str = GOT_OBJ_LABEL_TAG;
914 05118f5a 2021-06-22 stsp a->ntags++;
916 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
917 05118f5a 2021-06-22 stsp type_str = "offset-delta";
918 c414a013 2021-09-25 thomas.ad if (asprintf(&delta_str, " base-offset %lld",
919 c414a013 2021-09-25 thomas.ad (long long)base_offset) == -1) {
920 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
923 05118f5a 2021-06-22 stsp a->noffdeltas++;
925 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_REF_DELTA:
926 05118f5a 2021-06-22 stsp type_str = "ref-delta";
927 05118f5a 2021-06-22 stsp err = got_object_id_str(&base_id_str, base_id);
930 05118f5a 2021-06-22 stsp if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
931 05118f5a 2021-06-22 stsp err = got_error_from_errno("asprintf");
934 05118f5a 2021-06-22 stsp a->nrefdeltas++;
937 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
940 05118f5a 2021-06-22 stsp if (a->human_readable) {
941 05118f5a 2021-06-22 stsp char scaled[FMT_SCALED_STRSIZE];
943 05118f5a 2021-06-22 stsp if (fmt_scaled(size, scaled) == -1) {
944 05118f5a 2021-06-22 stsp err = got_error_from_errno("fmt_scaled");
947 05118f5a 2021-06-22 stsp s = scaled;
948 05118f5a 2021-06-22 stsp while (isspace((unsigned char)*s))
950 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %s%s\n", id_str, type_str,
951 c414a013 2021-09-25 thomas.ad (long long)offset, s, delta_str ? delta_str : "");
953 c414a013 2021-09-25 thomas.ad printf("%s %s at %lld size %lld%s\n", id_str, type_str,
954 c414a013 2021-09-25 thomas.ad (long long)offset, (long long)size,
955 c414a013 2021-09-25 thomas.ad delta_str ? delta_str : "");
958 05118f5a 2021-06-22 stsp free(id_str);
959 05118f5a 2021-06-22 stsp free(base_id_str);
960 05118f5a 2021-06-22 stsp free(delta_str);
961 05118f5a 2021-06-22 stsp return err;
964 05118f5a 2021-06-22 stsp static const struct got_error *
965 05118f5a 2021-06-22 stsp cmd_listpack(int argc, char *argv[])
967 05118f5a 2021-06-22 stsp const struct got_error *error = NULL;
968 05118f5a 2021-06-22 stsp struct got_repository *repo = NULL;
970 05118f5a 2021-06-22 stsp struct got_object_id *pack_hash = NULL;
971 05118f5a 2021-06-22 stsp char *packfile_path = NULL;
972 05118f5a 2021-06-22 stsp char *id_str = NULL;
973 05118f5a 2021-06-22 stsp struct gotadmin_list_pack_cb_args lpa;
974 05118f5a 2021-06-22 stsp FILE *packfile = NULL;
975 05118f5a 2021-06-22 stsp int show_stats = 0, human_readable = 0;
976 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
978 05118f5a 2021-06-22 stsp while ((ch = getopt(argc, argv, "hs")) != -1) {
979 05118f5a 2021-06-22 stsp switch (ch) {
981 05118f5a 2021-06-22 stsp human_readable = 1;
984 05118f5a 2021-06-22 stsp show_stats = 1;
987 05118f5a 2021-06-22 stsp usage_listpack();
988 05118f5a 2021-06-22 stsp /* NOTREACHED */
992 05118f5a 2021-06-22 stsp argc -= optind;
993 05118f5a 2021-06-22 stsp argv += optind;
995 05118f5a 2021-06-22 stsp if (argc != 1)
996 05118f5a 2021-06-22 stsp usage_listpack();
997 05118f5a 2021-06-22 stsp packfile_path = realpath(argv[0], NULL);
998 05118f5a 2021-06-22 stsp if (packfile_path == NULL)
999 05118f5a 2021-06-22 stsp return got_error_from_errno2("realpath", argv[0]);
1001 05118f5a 2021-06-22 stsp #ifndef PROFILE
1002 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1003 05118f5a 2021-06-22 stsp NULL) == -1)
1004 05118f5a 2021-06-22 stsp err(1, "pledge");
1006 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1007 7cd52833 2022-06-23 thomas if (error != NULL)
1008 7cd52833 2022-06-23 thomas goto done;
1009 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1010 05118f5a 2021-06-22 stsp if (error)
1011 05118f5a 2021-06-22 stsp goto done;
1012 bfb5ee0b 2022-05-31 thomas #ifndef PROFILE
1013 bfb5ee0b 2022-05-31 thomas /* Remove "cpath" promise. */
1014 bfb5ee0b 2022-05-31 thomas if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1015 bfb5ee0b 2022-05-31 thomas NULL) == -1)
1016 bfb5ee0b 2022-05-31 thomas err(1, "pledge");
1018 05118f5a 2021-06-22 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1019 05118f5a 2021-06-22 stsp if (error)
1020 05118f5a 2021-06-22 stsp goto done;
1022 05118f5a 2021-06-22 stsp error = got_repo_find_pack(&packfile, &pack_hash, repo,
1023 05118f5a 2021-06-22 stsp packfile_path);
1024 05118f5a 2021-06-22 stsp if (error)
1025 05118f5a 2021-06-22 stsp goto done;
1026 05118f5a 2021-06-22 stsp error = got_object_id_str(&id_str, pack_hash);
1027 05118f5a 2021-06-22 stsp if (error)
1028 05118f5a 2021-06-22 stsp goto done;
1030 05118f5a 2021-06-22 stsp memset(&lpa, 0, sizeof(lpa));
1031 05118f5a 2021-06-22 stsp lpa.human_readable = human_readable;
1032 05118f5a 2021-06-22 stsp error = got_repo_list_pack(packfile, pack_hash, repo,
1033 05118f5a 2021-06-22 stsp list_pack_cb, &lpa, check_cancelled, NULL);
1034 05118f5a 2021-06-22 stsp if (error)
1035 05118f5a 2021-06-22 stsp goto done;
1036 05118f5a 2021-06-22 stsp if (show_stats) {
1037 05118f5a 2021-06-22 stsp printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1038 05118f5a 2021-06-22 stsp " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1039 05118f5a 2021-06-22 stsp lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1040 05118f5a 2021-06-22 stsp lpa.noffdeltas + lpa.nrefdeltas,
1041 05118f5a 2021-06-22 stsp lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1042 05118f5a 2021-06-22 stsp lpa.noffdeltas, lpa.nrefdeltas);
1045 c75a6067 2021-10-15 thomas if (repo)
1046 c75a6067 2021-10-15 thomas got_repo_close(repo);
1047 7cd52833 2022-06-23 thomas if (pack_fds) {
1048 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1049 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1050 7cd52833 2022-06-23 thomas if (error == NULL)
1051 7cd52833 2022-06-23 thomas error = pack_err;
1053 05118f5a 2021-06-22 stsp free(id_str);
1054 05118f5a 2021-06-22 stsp free(pack_hash);
1055 05118f5a 2021-06-22 stsp free(packfile_path);
1056 05118f5a 2021-06-22 stsp return error;
1059 b3d68e7f 2021-07-03 stsp __dead static void
1060 b3d68e7f 2021-07-03 stsp usage_cleanup(void)
1062 ef8ec606 2021-07-27 stsp fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1063 1124fe40 2021-07-07 stsp "[-q]\n", getprogname());
1067 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg {
1068 b3d68e7f 2021-07-03 stsp int last_nloose;
1069 b3d68e7f 2021-07-03 stsp int last_ncommits;
1070 b3d68e7f 2021-07-03 stsp int last_npurged;
1071 b3d68e7f 2021-07-03 stsp int verbosity;
1072 b3d68e7f 2021-07-03 stsp int printed_something;
1073 b3d68e7f 2021-07-03 stsp int dry_run;
1076 b3d68e7f 2021-07-03 stsp static const struct got_error *
1077 b3d68e7f 2021-07-03 stsp cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1079 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg *a = arg;
1080 b3d68e7f 2021-07-03 stsp int print_loose = 0, print_commits = 0, print_purged = 0;
1082 b3d68e7f 2021-07-03 stsp if (a->last_nloose != nloose) {
1083 b3d68e7f 2021-07-03 stsp print_loose = 1;
1084 b3d68e7f 2021-07-03 stsp a->last_nloose = nloose;
1086 b3d68e7f 2021-07-03 stsp if (a->last_ncommits != ncommits) {
1087 b3d68e7f 2021-07-03 stsp print_loose = 1;
1088 b3d68e7f 2021-07-03 stsp print_commits = 1;
1089 b3d68e7f 2021-07-03 stsp a->last_ncommits = ncommits;
1091 b3d68e7f 2021-07-03 stsp if (a->last_npurged != npurged) {
1092 b3d68e7f 2021-07-03 stsp print_loose = 1;
1093 b3d68e7f 2021-07-03 stsp print_commits = 1;
1094 b3d68e7f 2021-07-03 stsp print_purged = 1;
1095 b3d68e7f 2021-07-03 stsp a->last_npurged = npurged;
1098 b3d68e7f 2021-07-03 stsp if (a->verbosity < 0)
1099 b3d68e7f 2021-07-03 stsp return NULL;
1101 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged)
1102 b3d68e7f 2021-07-03 stsp printf("\r");
1103 b3d68e7f 2021-07-03 stsp if (print_loose)
1104 b3d68e7f 2021-07-03 stsp printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1105 b3d68e7f 2021-07-03 stsp if (print_commits)
1106 b3d68e7f 2021-07-03 stsp printf("; %d commit%s scanned", ncommits,
1107 b3d68e7f 2021-07-03 stsp ncommits == 1 ? "" : "s");
1108 b3d68e7f 2021-07-03 stsp if (print_purged) {
1109 b3d68e7f 2021-07-03 stsp if (a->dry_run) {
1110 b3d68e7f 2021-07-03 stsp printf("; %d object%s could be purged", npurged,
1111 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1113 b3d68e7f 2021-07-03 stsp printf("; %d object%s purged", npurged,
1114 b3d68e7f 2021-07-03 stsp npurged == 1 ? "" : "s");
1117 b3d68e7f 2021-07-03 stsp if (print_loose || print_commits || print_purged) {
1118 b3d68e7f 2021-07-03 stsp a->printed_something = 1;
1119 b3d68e7f 2021-07-03 stsp fflush(stdout);
1121 b3d68e7f 2021-07-03 stsp return NULL;
1124 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg {
1125 1124fe40 2021-07-07 stsp int verbosity;
1126 1124fe40 2021-07-07 stsp int printed_something;
1127 1124fe40 2021-07-07 stsp int dry_run;
1130 b3d68e7f 2021-07-03 stsp static const struct got_error *
1131 1124fe40 2021-07-07 stsp lonely_packidx_progress(void *arg, const char *path)
1133 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg *a = arg;
1135 1124fe40 2021-07-07 stsp if (a->verbosity < 0)
1136 1124fe40 2021-07-07 stsp return NULL;
1138 1124fe40 2021-07-07 stsp if (a->dry_run)
1139 1124fe40 2021-07-07 stsp printf("%s could be removed\n", path);
1141 1124fe40 2021-07-07 stsp printf("%s removed\n", path);
1143 1124fe40 2021-07-07 stsp a->printed_something = 1;
1144 1124fe40 2021-07-07 stsp return NULL;
1147 1124fe40 2021-07-07 stsp static const struct got_error *
1148 b3d68e7f 2021-07-03 stsp cmd_cleanup(int argc, char *argv[])
1150 b3d68e7f 2021-07-03 stsp const struct got_error *error = NULL;
1151 1ea7ccc6 2021-11-15 thomas char *repo_path = NULL;
1152 b3d68e7f 2021-07-03 stsp struct got_repository *repo = NULL;
1153 b3d68e7f 2021-07-03 stsp int ch, dry_run = 0, npacked = 0, verbosity = 0;
1154 ef8ec606 2021-07-27 stsp int remove_lonely_packidx = 0, ignore_mtime = 0;
1155 b3d68e7f 2021-07-03 stsp struct got_cleanup_progress_arg cpa;
1156 1124fe40 2021-07-07 stsp struct got_lonely_packidx_progress_arg lpa;
1157 b3d68e7f 2021-07-03 stsp off_t size_before, size_after;
1158 b3d68e7f 2021-07-03 stsp char scaled_before[FMT_SCALED_STRSIZE];
1159 b3d68e7f 2021-07-03 stsp char scaled_after[FMT_SCALED_STRSIZE];
1160 b3d68e7f 2021-07-03 stsp char scaled_diff[FMT_SCALED_STRSIZE];
1161 9188bd78 2021-07-03 stsp char **extensions;
1162 9188bd78 2021-07-03 stsp int nextensions, i;
1163 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
1165 ef8ec606 2021-07-27 stsp while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1166 b3d68e7f 2021-07-03 stsp switch (ch) {
1168 ef8ec606 2021-07-27 stsp ignore_mtime = 1;
1171 1124fe40 2021-07-07 stsp remove_lonely_packidx = 1;
1174 b3d68e7f 2021-07-03 stsp repo_path = realpath(optarg, NULL);
1175 b3d68e7f 2021-07-03 stsp if (repo_path == NULL)
1176 b3d68e7f 2021-07-03 stsp return got_error_from_errno2("realpath",
1178 b3d68e7f 2021-07-03 stsp got_path_strip_trailing_slashes(repo_path);
1181 b3d68e7f 2021-07-03 stsp dry_run = 1;
1184 b3d68e7f 2021-07-03 stsp verbosity = -1;
1187 b3d68e7f 2021-07-03 stsp usage_cleanup();
1188 b3d68e7f 2021-07-03 stsp /* NOTREACHED */
1192 b3d68e7f 2021-07-03 stsp argc -= optind;
1193 b3d68e7f 2021-07-03 stsp argv += optind;
1195 b3d68e7f 2021-07-03 stsp #ifndef PROFILE
1196 b3d68e7f 2021-07-03 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1197 b3d68e7f 2021-07-03 stsp NULL) == -1)
1198 b3d68e7f 2021-07-03 stsp err(1, "pledge");
1200 1ea7ccc6 2021-11-15 thomas if (repo_path == NULL) {
1201 1ea7ccc6 2021-11-15 thomas error = get_repo_path(&repo_path);
1202 1ea7ccc6 2021-11-15 thomas if (error)
1203 1ea7ccc6 2021-11-15 thomas goto done;
1205 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1206 7cd52833 2022-06-23 thomas if (error != NULL)
1207 7cd52833 2022-06-23 thomas goto done;
1208 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1209 b3d68e7f 2021-07-03 stsp if (error)
1210 b3d68e7f 2021-07-03 stsp goto done;
1212 b3d68e7f 2021-07-03 stsp error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1213 b3d68e7f 2021-07-03 stsp if (error)
1214 b3d68e7f 2021-07-03 stsp goto done;
1216 9188bd78 2021-07-03 stsp got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1218 9188bd78 2021-07-03 stsp for (i = 0; i < nextensions; i++) {
1219 9188bd78 2021-07-03 stsp if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1220 9188bd78 2021-07-03 stsp error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1221 9188bd78 2021-07-03 stsp "the preciousObjects Git extension is enabled; "
1222 9188bd78 2021-07-03 stsp "this implies that objects must not be deleted");
1223 9188bd78 2021-07-03 stsp goto done;
1227 1124fe40 2021-07-07 stsp if (remove_lonely_packidx) {
1228 1124fe40 2021-07-07 stsp memset(&lpa, 0, sizeof(lpa));
1229 1124fe40 2021-07-07 stsp lpa.dry_run = dry_run;
1230 1124fe40 2021-07-07 stsp lpa.verbosity = verbosity;
1231 1124fe40 2021-07-07 stsp error = got_repo_remove_lonely_packidx(repo, dry_run,
1232 1124fe40 2021-07-07 stsp lonely_packidx_progress, &lpa, check_cancelled, NULL);
1233 1124fe40 2021-07-07 stsp goto done;
1236 b3d68e7f 2021-07-03 stsp memset(&cpa, 0, sizeof(cpa));
1237 b3d68e7f 2021-07-03 stsp cpa.last_ncommits = -1;
1238 b3d68e7f 2021-07-03 stsp cpa.last_npurged = -1;
1239 b3d68e7f 2021-07-03 stsp cpa.dry_run = dry_run;
1240 b3d68e7f 2021-07-03 stsp cpa.verbosity = verbosity;
1241 b3d68e7f 2021-07-03 stsp error = got_repo_purge_unreferenced_loose_objects(repo,
1242 abc59930 2021-09-05 naddy &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1243 abc59930 2021-09-05 naddy cleanup_progress, &cpa, check_cancelled, NULL);
1244 b3d68e7f 2021-07-03 stsp if (cpa.printed_something)
1245 b3d68e7f 2021-07-03 stsp printf("\n");
1246 b3d68e7f 2021-07-03 stsp if (error)
1247 b3d68e7f 2021-07-03 stsp goto done;
1248 b3d68e7f 2021-07-03 stsp if (cpa.printed_something) {
1249 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before, scaled_before) == -1) {
1250 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1251 b3d68e7f 2021-07-03 stsp goto done;
1253 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_after, scaled_after) == -1) {
1254 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1255 b3d68e7f 2021-07-03 stsp goto done;
1257 b3d68e7f 2021-07-03 stsp if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1258 b3d68e7f 2021-07-03 stsp error = got_error_from_errno("fmt_scaled");
1259 b3d68e7f 2021-07-03 stsp goto done;
1261 b3d68e7f 2021-07-03 stsp printf("loose total size before: %s\n", scaled_before);
1262 b3d68e7f 2021-07-03 stsp printf("loose total size after: %s\n", scaled_after);
1263 b3d68e7f 2021-07-03 stsp if (dry_run) {
1264 b3d68e7f 2021-07-03 stsp printf("disk space which would be freed: %s\n",
1265 b3d68e7f 2021-07-03 stsp scaled_diff);
1267 b3d68e7f 2021-07-03 stsp printf("disk space freed: %s\n", scaled_diff);
1268 b3d68e7f 2021-07-03 stsp printf("loose objects also found in pack files: %d\n", npacked);
1272 b3d68e7f 2021-07-03 stsp got_repo_close(repo);
1273 7cd52833 2022-06-23 thomas if (pack_fds) {
1274 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
1275 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
1276 7cd52833 2022-06-23 thomas if (error == NULL)
1277 7cd52833 2022-06-23 thomas error = pack_err;
1279 1ea7ccc6 2021-11-15 thomas free(repo_path);
1280 b3d68e7f 2021-07-03 stsp return error;