2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.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"
42 #include "got_privsep.h"
43 #include "got_opentemp.h"
44 #include "got_worktree.h"
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
54 catch_sigint(int signo)
60 catch_sigpipe(int signo)
65 static const struct got_error *
66 check_cancelled(void *arg)
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
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" },
102 list_commands(FILE *fp)
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);
115 main(int argc, char *argv[])
117 const struct gotadmin_cmd *cmd;
120 int hflag = 0, Vflag = 0;
121 static const struct option longopts[] = {
122 { "version", no_argument, NULL, 'V' },
126 setlocale(LC_CTYPE, "");
128 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
148 got_version_print_str();
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)
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) &&
176 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
177 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
184 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 list_commands(stderr);
190 usage(int hflag, int status)
192 FILE *fp = (status == 0) ? stdout : stderr;
194 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
201 static const struct got_error *
202 apply_unveil(const char *repo_path, int repo_read_only)
204 const struct got_error *err;
207 if (unveil("gmon.out", "rwc") != 0)
208 return got_error_from_errno2("unveil", "gmon.out");
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();
220 if (unveil(NULL, NULL) != 0)
221 return got_error_from_errno("unveil");
229 fprintf(stderr, "usage: %s info [-r repository-path]\n",
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;
243 cwd = getcwd(NULL, 0);
245 return got_error_from_errno("getcwd");
247 err = got_worktree_open(&worktree, cwd);
249 if (err->code != GOT_ERR_NOT_WORKTREE)
255 *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 *repo_path = strdup(cwd);
258 if (*repo_path == NULL)
259 err = got_error_from_errno("strdup");
262 got_worktree_close(worktree);
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) {
282 repo_path = realpath(optarg, NULL);
283 if (repo_path == NULL)
284 return got_error_from_errno2("realpath",
286 got_path_strip_trailing_slashes(repo_path);
298 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
302 if (repo_path == NULL) {
303 error = get_repo_path(&repo_path);
307 error = got_repo_pack_fds_open(&pack_fds);
310 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
314 /* Remove "cpath" promise. */
315 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
319 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
323 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
325 gotconfig = got_repo_get_gotconfig(repo);
327 const struct got_remote_repo *remotes;
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);
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,
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");
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);
366 printf("loose objects: %d\n", nobj);
368 if (fmt_scaled(loose_size, scaled) == -1) {
369 error = got_error_from_errno("fmt_scaled");
372 printf("loose total size: %s\n", scaled);
376 got_repo_close(repo);
378 const struct got_error *pack_err =
379 got_repo_pack_fds_close(pack_fds);
391 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
392 "[-x reference] [-q] [reference ...]\n",
397 struct got_pack_progress_arg {
398 char last_scaled_size[FMT_SCALED_STRSIZE];
410 int printed_something;
414 print_load_info(int print_colored, int print_found, int print_trees,
415 int ncolored, int nfound, int ntrees)
418 printf("%d commit%s colored", ncolored,
419 ncolored == 1 ? "" : "s");
422 printf("%s%d object%s found",
423 ncolored > 0 ? "; " : "",
424 nfound, nfound == 1 ? "" : "s");
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,
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)
447 if (a->last_ncolored != ncolored) {
449 a->last_ncolored = ncolored;
452 if (a->last_nfound != nfound) {
455 a->last_nfound = nfound;
458 if (a->last_ntrees != ntrees) {
462 a->last_ntrees = ntrees;
465 if ((print_colored || print_found || print_trees) &&
468 print_load_info(print_colored, print_found, print_trees,
469 ncolored, nfound, ntrees);
470 a->printed_something = 1;
473 } else if (!a->loading_done) {
475 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
480 if (fmt_scaled(packfile_size, scaled_size) == -1)
481 return got_error_from_errno("fmt_scaled");
483 if (a->last_ncommits != ncommits) {
485 a->last_ncommits = ncommits;
488 if (a->last_nobj_total != nobj_total) {
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;
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;
523 if (print_searching || print_total || print_deltify || print_written)
526 printf("packing %d reference%s", ncommits,
527 ncommits == 1 ? "" : "s");
529 printf("; %d object%s", nobj_total,
530 nobj_total == 1 ? "" : "s");
532 printf("; deltify: %d%%", p_deltify);
534 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
535 scaled_size, p_written);
536 if (print_searching || print_total || print_deltify ||
538 a->printed_something = 1;
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)
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) {
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;
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;
585 if (print_size || print_indexed || print_resolved)
588 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
590 printf("; indexing %d%%", p_indexed);
592 printf("; resolving deltas %d%%", p_resolved);
593 if (print_size || print_indexed || print_resolved)
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;
608 err = got_ref_open(&ref, repo, refname, 0);
610 if (err->code != GOT_ERR_NOT_REF)
613 /* Treat argument as a reference prefix. */
614 err = got_ref_list(refs, repo, refname,
615 got_ref_cmp_by_name, NULL);
617 err = got_reflist_insert(new, refs, ref,
618 got_ref_cmp_by_name, NULL);
619 if (err || *new == NULL /* duplicate */)
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;
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) {
654 repo_path = realpath(optarg, NULL);
655 if (repo_path == NULL)
656 return got_error_from_errno2("realpath",
658 got_path_strip_trailing_slashes(repo_path);
661 got_path_strip_trailing_slashes(optarg);
662 error = got_pathlist_append(&exclude_args,
680 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
684 if (repo_path == NULL) {
685 error = get_repo_path(&repo_path);
689 error = got_repo_pack_fds_open(&pack_fds);
692 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
696 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
700 TAILQ_FOREACH(pe, &exclude_args, entry) {
701 const char *refname = pe->path;
702 error = add_ref(&new, &exclude_refs, refname, repo);
709 error = got_ref_list(&include_refs, repo, "",
710 got_ref_cmp_by_name, NULL);
714 for (i = 0; i < argc; i++) {
716 got_path_strip_trailing_slashes(argv[i]);
718 error = add_ref(&new, &include_refs, refname, repo);
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)
729 TAILQ_REMOVE(&include_refs, re, entry);
730 got_ref_close(re->ref);
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);
744 if (ppa.printed_something)
749 error = got_object_id_str(&id_str, pack_hash);
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);
760 printf("\nIndexed %s.pack\n", id_str);
763 got_repo_close(repo);
765 const struct got_error *pack_err =
766 got_repo_pack_fds_close(pack_fds);
770 got_pathlist_free(&exclude_args);
771 got_ref_list_free(&exclude_refs);
772 got_ref_list_free(&include_refs);
780 usage_indexpack(void)
782 fprintf(stderr, "usage: %s indexpack packfile-path\n",
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;
793 struct got_object_id *pack_hash = NULL;
794 char *packfile_path = NULL;
796 struct got_pack_progress_arg ppa;
797 FILE *packfile = NULL;
798 int *pack_fds = NULL;
800 while ((ch = getopt(argc, argv, "")) != -1) {
814 packfile_path = realpath(argv[0], NULL);
815 if (packfile_path == NULL)
816 return got_error_from_errno2("realpath", argv[0]);
819 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
824 error = got_repo_pack_fds_open(&pack_fds);
827 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
831 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
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,
845 error = got_object_id_str(&id_str, pack_hash);
849 error = got_repo_index_pack(packfile, pack_hash, repo,
850 pack_index_progress, &ppa, check_cancelled, NULL);
853 printf("\nIndexed %s.pack\n", id_str);
856 got_repo_close(repo);
858 const struct got_error *pack_err =
859 got_repo_pack_fds_close(pack_fds);
871 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
876 struct gotadmin_list_pack_cb_args {
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);
900 case GOT_OBJ_TYPE_BLOB:
901 type_str = GOT_OBJ_LABEL_BLOB;
904 case GOT_OBJ_TYPE_TREE:
905 type_str = GOT_OBJ_LABEL_TREE;
908 case GOT_OBJ_TYPE_COMMIT:
909 type_str = GOT_OBJ_LABEL_COMMIT;
912 case GOT_OBJ_TYPE_TAG:
913 type_str = GOT_OBJ_LABEL_TAG;
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");
925 case GOT_OBJ_TYPE_REF_DELTA:
926 type_str = "ref-delta";
927 err = got_object_id_str(&base_id_str, base_id);
930 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
931 err = got_error_from_errno("asprintf");
937 err = got_error(GOT_ERR_OBJ_TYPE);
940 if (a->human_readable) {
941 char scaled[FMT_SCALED_STRSIZE];
943 if (fmt_scaled(size, scaled) == -1) {
944 err = got_error_from_errno("fmt_scaled");
948 while (isspace((unsigned char)*s))
950 printf("%s %s at %lld size %s%s\n", id_str, type_str,
951 (long long)offset, s, delta_str ? delta_str : "");
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 : "");
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;
970 struct got_object_id *pack_hash = NULL;
971 char *packfile_path = 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) {
997 packfile_path = realpath(argv[0], NULL);
998 if (packfile_path == NULL)
999 return got_error_from_errno2("realpath", argv[0]);
1002 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1006 error = got_repo_pack_fds_open(&pack_fds);
1009 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1013 /* Remove "cpath" promise. */
1014 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1018 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1022 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1026 error = got_object_id_str(&id_str, pack_hash);
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);
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);
1046 got_repo_close(repo);
1048 const struct got_error *pack_err =
1049 got_repo_pack_fds_close(pack_fds);
1055 free(packfile_path);
1062 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1063 "[-q]\n", getprogname());
1067 struct got_cleanup_progress_arg {
1072 int printed_something;
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) {
1084 a->last_nloose = nloose;
1086 if (a->last_ncommits != ncommits) {
1089 a->last_ncommits = ncommits;
1091 if (a->last_npurged != npurged) {
1095 a->last_npurged = npurged;
1098 if (a->verbosity < 0)
1101 if (print_loose || print_commits || print_purged)
1104 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1106 printf("; %d commit%s scanned", ncommits,
1107 ncommits == 1 ? "" : "s");
1110 printf("; %d object%s could be purged", npurged,
1111 npurged == 1 ? "" : "s");
1113 printf("; %d object%s purged", npurged,
1114 npurged == 1 ? "" : "s");
1117 if (print_loose || print_commits || print_purged) {
1118 a->printed_something = 1;
1124 struct got_lonely_packidx_progress_arg {
1126 int printed_something;
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)
1139 printf("%s could be removed\n", path);
1141 printf("%s removed\n", path);
1143 a->printed_something = 1;
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];
1163 int *pack_fds = NULL;
1165 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1171 remove_lonely_packidx = 1;
1174 repo_path = realpath(optarg, NULL);
1175 if (repo_path == NULL)
1176 return got_error_from_errno2("realpath",
1178 got_path_strip_trailing_slashes(repo_path);
1196 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1200 if (repo_path == NULL) {
1201 error = get_repo_path(&repo_path);
1205 error = got_repo_pack_fds_open(&pack_fds);
1208 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1212 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1216 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
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");
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);
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)
1248 if (cpa.printed_something) {
1249 if (fmt_scaled(size_before, scaled_before) == -1) {
1250 error = got_error_from_errno("fmt_scaled");
1253 if (fmt_scaled(size_after, scaled_after) == -1) {
1254 error = got_error_from_errno("fmt_scaled");
1257 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1258 error = got_error_from_errno("fmt_scaled");
1261 printf("loose total size before: %s\n", scaled_before);
1262 printf("loose total size after: %s\n", scaled_after);
1264 printf("disk space which would be freed: %s\n",
1267 printf("disk space freed: %s\n", scaled_diff);
1268 printf("loose objects also found in pack files: %d\n", npacked);
1272 got_repo_close(repo);
1274 const struct got_error *pack_err =
1275 got_repo_pack_fds_close(pack_fds);