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];
278 while ((ch = getopt(argc, argv, "r:")) != -1) {
281 repo_path = realpath(optarg, NULL);
282 if (repo_path == NULL)
283 return got_error_from_errno2("realpath",
285 got_path_strip_trailing_slashes(repo_path);
297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
301 if (repo_path == NULL) {
302 error = get_repo_path(&repo_path);
306 error = got_repo_open(&repo, repo_path, NULL);
310 /* Remove "cpath" promise. */
311 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
315 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
319 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
321 gotconfig = got_repo_get_gotconfig(repo);
323 const struct got_remote_repo *remotes;
325 if (got_gotconfig_get_author(gotconfig)) {
326 printf("default author: %s\n",
327 got_gotconfig_get_author(gotconfig));
329 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
330 for (i = 0; i < nremotes; i++) {
331 const char *fetch_url = remotes[i].fetch_url;
332 const char *send_url = remotes[i].send_url;
333 if (strcmp(fetch_url, send_url) == 0) {
334 printf("remote \"%s\": %s\n", remotes[i].name,
335 remotes[i].fetch_url);
337 printf("remote \"%s\" (fetch): %s\n",
338 remotes[i].name, remotes[i].fetch_url);
339 printf("remote \"%s\" (send): %s\n",
340 remotes[i].name, remotes[i].send_url);
345 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
349 printf("pack files: %d\n", npackfiles);
350 if (npackfiles > 0) {
351 if (fmt_scaled(packsize, scaled) == -1) {
352 error = got_error_from_errno("fmt_scaled");
355 printf("packed objects: %d\n", npackedobj);
356 printf("packed total size: %s\n", scaled);
359 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
362 printf("loose objects: %d\n", nobj);
364 if (fmt_scaled(loose_size, scaled) == -1) {
365 error = got_error_from_errno("fmt_scaled");
368 printf("loose total size: %s\n", scaled);
372 got_repo_close(repo);
380 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
381 "[-x reference] [-q] [reference ...]\n",
386 struct got_pack_progress_arg {
387 char last_scaled_size[FMT_SCALED_STRSIZE];
399 int printed_something;
403 print_load_info(int print_colored, int print_found, int print_trees,
404 int ncolored, int nfound, int ntrees)
407 printf("%d commit%s colored", ncolored,
408 ncolored == 1 ? "" : "s");
411 printf("%s%d object%s found",
412 ncolored > 0 ? "; " : "",
413 nfound, nfound == 1 ? "" : "s");
416 printf("; %d tree%s scanned", ntrees,
417 ntrees == 1 ? "" : "s");
421 static const struct got_error *
422 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
423 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
426 struct got_pack_progress_arg *a = arg;
427 char scaled_size[FMT_SCALED_STRSIZE];
428 int p_deltify, p_written;
429 int print_colored = 0, print_found = 0, print_trees = 0;
430 int print_searching = 0, print_total = 0;
431 int print_deltify = 0, print_written = 0;
433 if (a->verbosity < 0)
436 if (a->last_ncolored != ncolored) {
438 a->last_ncolored = ncolored;
441 if (a->last_nfound != nfound) {
444 a->last_nfound = nfound;
447 if (a->last_ntrees != ntrees) {
451 a->last_ntrees = ntrees;
454 if ((print_colored || print_found || print_trees) &&
457 print_load_info(print_colored, print_found, print_trees,
458 ncolored, nfound, ntrees);
459 a->printed_something = 1;
462 } else if (!a->loading_done) {
464 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
469 if (fmt_scaled(packfile_size, scaled_size) == -1)
470 return got_error_from_errno("fmt_scaled");
472 if (a->last_ncommits != ncommits) {
474 a->last_ncommits = ncommits;
477 if (a->last_nobj_total != nobj_total) {
480 a->last_nobj_total = nobj_total;
483 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
484 strcmp(scaled_size, a->last_scaled_size)) != 0) {
485 if (strlcpy(a->last_scaled_size, scaled_size,
486 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
487 return got_error(GOT_ERR_NO_SPACE);
490 if (nobj_deltify > 0 || nobj_written > 0) {
491 if (nobj_deltify > 0) {
492 p_deltify = (nobj_deltify * 100) / nobj_total;
493 if (p_deltify != a->last_p_deltify) {
494 a->last_p_deltify = p_deltify;
500 if (nobj_written > 0) {
501 p_written = (nobj_written * 100) / nobj_total;
502 if (p_written != a->last_p_written) {
503 a->last_p_written = p_written;
512 if (print_searching || print_total || print_deltify || print_written)
515 printf("packing %d reference%s", ncommits,
516 ncommits == 1 ? "" : "s");
518 printf("; %d object%s", nobj_total,
519 nobj_total == 1 ? "" : "s");
521 printf("; deltify: %d%%", p_deltify);
523 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
524 scaled_size, p_written);
525 if (print_searching || print_total || print_deltify ||
527 a->printed_something = 1;
533 static const struct got_error *
534 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
535 int nobj_indexed, int nobj_loose, int nobj_resolved)
537 struct got_pack_progress_arg *a = arg;
538 char scaled_size[FMT_SCALED_STRSIZE];
539 int p_indexed, p_resolved;
540 int print_size = 0, print_indexed = 0, print_resolved = 0;
542 if (a->verbosity < 0)
545 if (packfile_size > 0 || nobj_indexed > 0) {
546 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
547 (a->last_scaled_size[0] == '\0' ||
548 strcmp(scaled_size, a->last_scaled_size)) != 0) {
550 if (strlcpy(a->last_scaled_size, scaled_size,
551 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
552 return got_error(GOT_ERR_NO_SPACE);
554 if (nobj_indexed > 0) {
555 p_indexed = (nobj_indexed * 100) / nobj_total;
556 if (p_indexed != a->last_p_indexed) {
557 a->last_p_indexed = p_indexed;
562 if (nobj_resolved > 0) {
563 p_resolved = (nobj_resolved * 100) /
564 (nobj_total - nobj_loose);
565 if (p_resolved != a->last_p_resolved) {
566 a->last_p_resolved = p_resolved;
574 if (print_size || print_indexed || print_resolved)
577 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
579 printf("; indexing %d%%", p_indexed);
581 printf("; resolving deltas %d%%", p_resolved);
582 if (print_size || print_indexed || print_resolved)
588 static const struct got_error *
589 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
590 const char *refname, struct got_repository *repo)
592 const struct got_error *err;
593 struct got_reference *ref;
597 err = got_ref_open(&ref, repo, refname, 0);
599 if (err->code != GOT_ERR_NOT_REF)
602 /* Treat argument as a reference prefix. */
603 err = got_ref_list(refs, repo, refname,
604 got_ref_cmp_by_name, NULL);
606 err = got_reflist_insert(new, refs, ref,
607 got_ref_cmp_by_name, NULL);
608 if (err || *new == NULL /* duplicate */)
615 static const struct got_error *
616 cmd_pack(int argc, char *argv[])
618 const struct got_error *error = NULL;
619 char *repo_path = NULL;
620 struct got_repository *repo = NULL;
621 int ch, i, loose_obj_only = 1, verbosity = 0;
622 struct got_object_id *pack_hash = NULL;
624 struct got_pack_progress_arg ppa;
625 FILE *packfile = NULL;
626 struct got_pathlist_head exclude_args;
627 struct got_pathlist_entry *pe;
628 struct got_reflist_head exclude_refs;
629 struct got_reflist_head include_refs;
630 struct got_reflist_entry *re, *new;
632 TAILQ_INIT(&exclude_args);
633 TAILQ_INIT(&exclude_refs);
634 TAILQ_INIT(&include_refs);
636 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
642 repo_path = realpath(optarg, NULL);
643 if (repo_path == NULL)
644 return got_error_from_errno2("realpath",
646 got_path_strip_trailing_slashes(repo_path);
649 got_path_strip_trailing_slashes(optarg);
650 error = got_pathlist_append(&exclude_args,
668 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
672 if (repo_path == NULL) {
673 error = get_repo_path(&repo_path);
677 error = got_repo_open(&repo, repo_path, NULL);
681 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
685 TAILQ_FOREACH(pe, &exclude_args, entry) {
686 const char *refname = pe->path;
687 error = add_ref(&new, &exclude_refs, refname, repo);
694 error = got_ref_list(&include_refs, repo, "",
695 got_ref_cmp_by_name, NULL);
699 for (i = 0; i < argc; i++) {
701 got_path_strip_trailing_slashes(argv[i]);
703 error = add_ref(&new, &include_refs, refname, repo);
709 /* Ignore references in the refs/got/ namespace. */
710 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
711 const char *refname = got_ref_get_name(re->ref);
712 if (strncmp("refs/got/", refname, 9) != 0)
714 TAILQ_REMOVE(&include_refs, re, entry);
715 got_ref_close(re->ref);
719 memset(&ppa, 0, sizeof(ppa));
720 ppa.last_scaled_size[0] = '\0';
721 ppa.last_p_indexed = -1;
722 ppa.last_p_resolved = -1;
723 ppa.verbosity = verbosity;
725 error = got_repo_pack_objects(&packfile, &pack_hash,
726 &include_refs, &exclude_refs, repo, loose_obj_only,
727 pack_progress, &ppa, check_cancelled, NULL);
729 if (ppa.printed_something)
734 error = got_object_id_str(&id_str, pack_hash);
738 printf("\nWrote %s.pack\n", id_str);
740 error = got_repo_index_pack(packfile, pack_hash, repo,
741 pack_index_progress, &ppa, check_cancelled, NULL);
745 printf("\nIndexed %s.pack\n", id_str);
748 got_repo_close(repo);
749 got_pathlist_free(&exclude_args);
750 got_ref_list_free(&exclude_refs);
751 got_ref_list_free(&include_refs);
759 usage_indexpack(void)
761 fprintf(stderr, "usage: %s indexpack packfile-path\n",
766 static const struct got_error *
767 cmd_indexpack(int argc, char *argv[])
769 const struct got_error *error = NULL;
770 struct got_repository *repo = NULL;
772 struct got_object_id *pack_hash = NULL;
773 char *packfile_path = NULL;
775 struct got_pack_progress_arg ppa;
776 FILE *packfile = NULL;
778 while ((ch = getopt(argc, argv, "")) != -1) {
792 packfile_path = realpath(argv[0], NULL);
793 if (packfile_path == NULL)
794 return got_error_from_errno2("realpath", argv[0]);
797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
802 error = got_repo_open(&repo, packfile_path, NULL);
806 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
810 memset(&ppa, 0, sizeof(ppa));
811 ppa.last_scaled_size[0] = '\0';
812 ppa.last_p_indexed = -1;
813 ppa.last_p_resolved = -1;
815 error = got_repo_find_pack(&packfile, &pack_hash, repo,
820 error = got_object_id_str(&id_str, pack_hash);
824 error = got_repo_index_pack(packfile, pack_hash, repo,
825 pack_index_progress, &ppa, check_cancelled, NULL);
828 printf("\nIndexed %s.pack\n", id_str);
831 got_repo_close(repo);
840 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
845 struct gotadmin_list_pack_cb_args {
855 static const struct got_error *
856 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
857 off_t size, off_t base_offset, struct got_object_id *base_id)
859 const struct got_error *err;
860 struct gotadmin_list_pack_cb_args *a = arg;
861 char *id_str, *delta_str = NULL, *base_id_str = NULL;
862 const char *type_str;
864 err = got_object_id_str(&id_str, id);
869 case GOT_OBJ_TYPE_BLOB:
870 type_str = GOT_OBJ_LABEL_BLOB;
873 case GOT_OBJ_TYPE_TREE:
874 type_str = GOT_OBJ_LABEL_TREE;
877 case GOT_OBJ_TYPE_COMMIT:
878 type_str = GOT_OBJ_LABEL_COMMIT;
881 case GOT_OBJ_TYPE_TAG:
882 type_str = GOT_OBJ_LABEL_TAG;
885 case GOT_OBJ_TYPE_OFFSET_DELTA:
886 type_str = "offset-delta";
887 if (asprintf(&delta_str, " base-offset %lld",
888 (long long)base_offset) == -1) {
889 err = got_error_from_errno("asprintf");
894 case GOT_OBJ_TYPE_REF_DELTA:
895 type_str = "ref-delta";
896 err = got_object_id_str(&base_id_str, base_id);
899 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
900 err = got_error_from_errno("asprintf");
906 err = got_error(GOT_ERR_OBJ_TYPE);
909 if (a->human_readable) {
910 char scaled[FMT_SCALED_STRSIZE];
912 if (fmt_scaled(size, scaled) == -1) {
913 err = got_error_from_errno("fmt_scaled");
917 while (isspace((unsigned char)*s))
919 printf("%s %s at %lld size %s%s\n", id_str, type_str,
920 (long long)offset, s, delta_str ? delta_str : "");
922 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
923 (long long)offset, (long long)size,
924 delta_str ? delta_str : "");
933 static const struct got_error *
934 cmd_listpack(int argc, char *argv[])
936 const struct got_error *error = NULL;
937 struct got_repository *repo = NULL;
939 struct got_object_id *pack_hash = NULL;
940 char *packfile_path = NULL;
942 struct gotadmin_list_pack_cb_args lpa;
943 FILE *packfile = NULL;
944 int show_stats = 0, human_readable = 0;
946 while ((ch = getopt(argc, argv, "hs")) != -1) {
965 packfile_path = realpath(argv[0], NULL);
966 if (packfile_path == NULL)
967 return got_error_from_errno2("realpath", argv[0]);
970 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
974 error = got_repo_open(&repo, packfile_path, NULL);
978 /* Remove "cpath" promise. */
979 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
983 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
987 error = got_repo_find_pack(&packfile, &pack_hash, repo,
991 error = got_object_id_str(&id_str, pack_hash);
995 memset(&lpa, 0, sizeof(lpa));
996 lpa.human_readable = human_readable;
997 error = got_repo_list_pack(packfile, pack_hash, repo,
998 list_pack_cb, &lpa, check_cancelled, NULL);
1002 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1003 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1004 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1005 lpa.noffdeltas + lpa.nrefdeltas,
1006 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1007 lpa.noffdeltas, lpa.nrefdeltas);
1011 got_repo_close(repo);
1014 free(packfile_path);
1021 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1022 "[-q]\n", getprogname());
1026 struct got_cleanup_progress_arg {
1031 int printed_something;
1035 static const struct got_error *
1036 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1038 struct got_cleanup_progress_arg *a = arg;
1039 int print_loose = 0, print_commits = 0, print_purged = 0;
1041 if (a->last_nloose != nloose) {
1043 a->last_nloose = nloose;
1045 if (a->last_ncommits != ncommits) {
1048 a->last_ncommits = ncommits;
1050 if (a->last_npurged != npurged) {
1054 a->last_npurged = npurged;
1057 if (a->verbosity < 0)
1060 if (print_loose || print_commits || print_purged)
1063 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1065 printf("; %d commit%s scanned", ncommits,
1066 ncommits == 1 ? "" : "s");
1069 printf("; %d object%s could be purged", npurged,
1070 npurged == 1 ? "" : "s");
1072 printf("; %d object%s purged", npurged,
1073 npurged == 1 ? "" : "s");
1076 if (print_loose || print_commits || print_purged) {
1077 a->printed_something = 1;
1083 struct got_lonely_packidx_progress_arg {
1085 int printed_something;
1089 static const struct got_error *
1090 lonely_packidx_progress(void *arg, const char *path)
1092 struct got_lonely_packidx_progress_arg *a = arg;
1094 if (a->verbosity < 0)
1098 printf("%s could be removed\n", path);
1100 printf("%s removed\n", path);
1102 a->printed_something = 1;
1106 static const struct got_error *
1107 cmd_cleanup(int argc, char *argv[])
1109 const struct got_error *error = NULL;
1110 char *repo_path = NULL;
1111 struct got_repository *repo = NULL;
1112 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1113 int remove_lonely_packidx = 0, ignore_mtime = 0;
1114 struct got_cleanup_progress_arg cpa;
1115 struct got_lonely_packidx_progress_arg lpa;
1116 off_t size_before, size_after;
1117 char scaled_before[FMT_SCALED_STRSIZE];
1118 char scaled_after[FMT_SCALED_STRSIZE];
1119 char scaled_diff[FMT_SCALED_STRSIZE];
1123 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1129 remove_lonely_packidx = 1;
1132 repo_path = realpath(optarg, NULL);
1133 if (repo_path == NULL)
1134 return got_error_from_errno2("realpath",
1136 got_path_strip_trailing_slashes(repo_path);
1154 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1158 if (repo_path == NULL) {
1159 error = get_repo_path(&repo_path);
1163 error = got_repo_open(&repo, repo_path, NULL);
1167 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1171 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1173 for (i = 0; i < nextensions; i++) {
1174 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1175 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1176 "the preciousObjects Git extension is enabled; "
1177 "this implies that objects must not be deleted");
1182 if (remove_lonely_packidx) {
1183 memset(&lpa, 0, sizeof(lpa));
1184 lpa.dry_run = dry_run;
1185 lpa.verbosity = verbosity;
1186 error = got_repo_remove_lonely_packidx(repo, dry_run,
1187 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1191 memset(&cpa, 0, sizeof(cpa));
1192 cpa.last_ncommits = -1;
1193 cpa.last_npurged = -1;
1194 cpa.dry_run = dry_run;
1195 cpa.verbosity = verbosity;
1196 error = got_repo_purge_unreferenced_loose_objects(repo,
1197 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1198 cleanup_progress, &cpa, check_cancelled, NULL);
1199 if (cpa.printed_something)
1203 if (cpa.printed_something) {
1204 if (fmt_scaled(size_before, scaled_before) == -1) {
1205 error = got_error_from_errno("fmt_scaled");
1208 if (fmt_scaled(size_after, scaled_after) == -1) {
1209 error = got_error_from_errno("fmt_scaled");
1212 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1213 error = got_error_from_errno("fmt_scaled");
1216 printf("loose total size before: %s\n", scaled_before);
1217 printf("loose total size after: %s\n", scaled_after);
1219 printf("disk space which would be freed: %s\n",
1222 printf("disk space freed: %s\n", scaled_diff);
1223 printf("loose objects also found in pack files: %d\n", npacked);
1227 got_repo_close(repo);