Blob


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