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/queue.h>
18 #include <sys/types.h>
35 #include "got_version.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_cancel.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_gotconfig.h"
44 #include "got_privsep.h"
45 #include "got_opentemp.h"
46 #include "got_worktree.h"
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
56 catch_sigint(int signo)
62 catch_sigpipe(int signo)
67 static const struct got_error *
68 check_cancelled(void *arg)
70 if (sigint_received || sigpipe_received)
71 return got_error(GOT_ERR_CANCELLED);
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
82 __dead static void usage(int, int);
83 __dead static void usage_init(void);
84 __dead static void usage_info(void);
85 __dead static void usage_pack(void);
86 __dead static void usage_indexpack(void);
87 __dead static void usage_listpack(void);
88 __dead static void usage_cleanup(void);
90 static const struct got_error* cmd_init(int, char *[]);
91 static const struct got_error* cmd_info(int, char *[]);
92 static const struct got_error* cmd_pack(int, char *[]);
93 static const struct got_error* cmd_indexpack(int, char *[]);
94 static const struct got_error* cmd_listpack(int, char *[]);
95 static const struct got_error* cmd_cleanup(int, char *[]);
97 static const struct gotadmin_cmd gotadmin_commands[] = {
98 { "init", cmd_init, usage_init, "" },
99 { "info", cmd_info, usage_info, "" },
100 { "pack", cmd_pack, usage_pack, "" },
101 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
102 { "listpack", cmd_listpack, usage_listpack, "ls" },
103 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
107 list_commands(FILE *fp)
111 fprintf(fp, "commands:");
112 for (i = 0; i < nitems(gotadmin_commands); i++) {
113 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
114 fprintf(fp, " %s", cmd->cmd_name);
120 main(int argc, char *argv[])
122 const struct gotadmin_cmd *cmd;
125 int hflag = 0, Vflag = 0;
126 static const struct option longopts[] = {
127 { "version", no_argument, NULL, 'V' },
131 setlocale(LC_CTYPE, "");
133 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
153 got_version_print_str();
158 usage(hflag, hflag ? 0 : 1);
160 signal(SIGINT, catch_sigint);
161 signal(SIGPIPE, catch_sigpipe);
163 for (i = 0; i < nitems(gotadmin_commands); i++) {
164 const struct got_error *error;
166 cmd = &gotadmin_commands[i];
168 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
169 strcmp(cmd->cmd_alias, argv[0]) != 0)
175 error = cmd->cmd_main(argc, argv);
176 if (error && error->code != GOT_ERR_CANCELLED &&
177 error->code != GOT_ERR_PRIVSEP_EXIT &&
178 !(sigpipe_received &&
179 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
181 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
189 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 list_commands(stderr);
195 usage(int hflag, int status)
197 FILE *fp = (status == 0) ? stdout : stderr;
199 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
206 static const struct got_error *
207 apply_unveil(const char *repo_path, int repo_read_only)
209 const struct got_error *err;
212 if (unveil("gmon.out", "rwc") != 0)
213 return got_error_from_errno2("unveil", "gmon.out");
215 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
216 return got_error_from_errno2("unveil", repo_path);
218 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
219 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
221 err = got_privsep_unveil_exec_helpers();
225 if (unveil(NULL, NULL) != 0)
226 return got_error_from_errno("unveil");
234 fprintf(stderr, "usage: %s info [-r repository-path]\n",
239 static const struct got_error *
240 get_repo_path(char **repo_path)
242 const struct got_error *err = NULL;
243 struct got_worktree *worktree = NULL;
248 cwd = getcwd(NULL, 0);
250 return got_error_from_errno("getcwd");
252 err = got_worktree_open(&worktree, cwd);
254 if (err->code != GOT_ERR_NOT_WORKTREE)
260 *repo_path = strdup(got_worktree_get_repo_path(worktree));
262 *repo_path = strdup(cwd);
263 if (*repo_path == NULL)
264 err = got_error_from_errno("strdup");
267 got_worktree_close(worktree);
275 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 const char *head_name = NULL;
285 char *repo_path = NULL;
289 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
293 while ((ch = getopt(argc, argv, "b:")) != -1) {
310 repo_path = strdup(argv[0]);
311 if (repo_path == NULL)
312 return got_error_from_errno("strdup");
314 got_path_strip_trailing_slashes(repo_path);
316 error = got_path_mkdir(repo_path);
318 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
321 error = apply_unveil(repo_path, 0);
325 error = got_repo_init(repo_path, head_name);
331 static const struct got_error *
332 cmd_info(int argc, char *argv[])
334 const struct got_error *error = NULL;
335 char *repo_path = NULL;
336 struct got_repository *repo = NULL;
337 const struct got_gotconfig *gotconfig = NULL;
338 int ch, npackfiles, npackedobj, nobj;
339 off_t packsize, loose_size;
340 char scaled[FMT_SCALED_STRSIZE];
341 int *pack_fds = NULL;
344 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
349 while ((ch = getopt(argc, argv, "r:")) != -1) {
352 repo_path = realpath(optarg, NULL);
353 if (repo_path == NULL)
354 return got_error_from_errno2("realpath",
356 got_path_strip_trailing_slashes(repo_path);
367 if (repo_path == NULL) {
368 error = get_repo_path(&repo_path);
372 error = got_repo_pack_fds_open(&pack_fds);
375 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
379 /* Remove "cpath" promise. */
380 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
384 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
388 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
390 gotconfig = got_repo_get_gotconfig(repo);
392 const struct got_remote_repo *remotes;
394 if (got_gotconfig_get_author(gotconfig)) {
395 printf("default author: %s\n",
396 got_gotconfig_get_author(gotconfig));
398 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
399 for (i = 0; i < nremotes; i++) {
400 const char *fetch_url = remotes[i].fetch_url;
401 const char *send_url = remotes[i].send_url;
402 if (strcmp(fetch_url, send_url) == 0) {
403 printf("remote \"%s\": %s\n", remotes[i].name,
404 remotes[i].fetch_url);
406 printf("remote \"%s\" (fetch): %s\n",
407 remotes[i].name, remotes[i].fetch_url);
408 printf("remote \"%s\" (send): %s\n",
409 remotes[i].name, remotes[i].send_url);
414 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
418 printf("pack files: %d\n", npackfiles);
419 if (npackfiles > 0) {
420 if (fmt_scaled(packsize, scaled) == -1) {
421 error = got_error_from_errno("fmt_scaled");
424 printf("packed objects: %d\n", npackedobj);
425 printf("packed total size: %s\n", scaled);
428 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
431 printf("loose objects: %d\n", nobj);
433 if (fmt_scaled(loose_size, scaled) == -1) {
434 error = got_error_from_errno("fmt_scaled");
437 printf("loose total size: %s\n", scaled);
441 got_repo_close(repo);
443 const struct got_error *pack_err =
444 got_repo_pack_fds_close(pack_fds);
456 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
457 "[-x reference] [reference ...]\n", getprogname());
461 struct got_pack_progress_arg {
462 char last_scaled_size[FMT_SCALED_STRSIZE];
474 int printed_something;
478 print_load_info(int print_colored, int print_found, int print_trees,
479 int ncolored, int nfound, int ntrees)
482 printf("%d commit%s colored", ncolored,
483 ncolored == 1 ? "" : "s");
486 printf("%s%d object%s found",
487 ncolored > 0 ? "; " : "",
488 nfound, nfound == 1 ? "" : "s");
491 printf("; %d tree%s scanned", ntrees,
492 ntrees == 1 ? "" : "s");
496 static const struct got_error *
497 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
498 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
501 struct got_pack_progress_arg *a = arg;
502 char scaled_size[FMT_SCALED_STRSIZE];
503 int p_deltify, p_written;
504 int print_colored = 0, print_found = 0, print_trees = 0;
505 int print_searching = 0, print_total = 0;
506 int print_deltify = 0, print_written = 0;
508 if (a->verbosity < 0)
511 if (a->last_ncolored != ncolored) {
513 a->last_ncolored = ncolored;
516 if (a->last_nfound != nfound) {
519 a->last_nfound = nfound;
522 if (a->last_ntrees != ntrees) {
526 a->last_ntrees = ntrees;
529 if ((print_colored || print_found || print_trees) &&
532 print_load_info(print_colored, print_found, print_trees,
533 ncolored, nfound, ntrees);
534 a->printed_something = 1;
537 } else if (!a->loading_done) {
539 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
544 if (fmt_scaled(packfile_size, scaled_size) == -1)
545 return got_error_from_errno("fmt_scaled");
547 if (a->last_ncommits != ncommits) {
549 a->last_ncommits = ncommits;
552 if (a->last_nobj_total != nobj_total) {
555 a->last_nobj_total = nobj_total;
558 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
559 strcmp(scaled_size, a->last_scaled_size)) != 0) {
560 if (strlcpy(a->last_scaled_size, scaled_size,
561 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
562 return got_error(GOT_ERR_NO_SPACE);
565 if (nobj_deltify > 0 || nobj_written > 0) {
566 if (nobj_deltify > 0) {
567 p_deltify = (nobj_deltify * 100) / nobj_total;
568 if (p_deltify != a->last_p_deltify) {
569 a->last_p_deltify = p_deltify;
575 if (nobj_written > 0) {
576 p_written = (nobj_written * 100) / nobj_total;
577 if (p_written != a->last_p_written) {
578 a->last_p_written = p_written;
587 if (print_searching || print_total || print_deltify || print_written)
590 printf("packing %d reference%s", ncommits,
591 ncommits == 1 ? "" : "s");
593 printf("; %d object%s", nobj_total,
594 nobj_total == 1 ? "" : "s");
596 printf("; deltify: %d%%", p_deltify);
598 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
599 scaled_size, p_written);
600 if (print_searching || print_total || print_deltify ||
602 a->printed_something = 1;
608 static const struct got_error *
609 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
610 int nobj_indexed, int nobj_loose, int nobj_resolved)
612 struct got_pack_progress_arg *a = arg;
613 char scaled_size[FMT_SCALED_STRSIZE];
614 int p_indexed, p_resolved;
615 int print_size = 0, print_indexed = 0, print_resolved = 0;
617 if (a->verbosity < 0)
620 if (packfile_size > 0 || nobj_indexed > 0) {
621 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
622 (a->last_scaled_size[0] == '\0' ||
623 strcmp(scaled_size, a->last_scaled_size)) != 0) {
625 if (strlcpy(a->last_scaled_size, scaled_size,
626 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
627 return got_error(GOT_ERR_NO_SPACE);
629 if (nobj_indexed > 0) {
630 p_indexed = (nobj_indexed * 100) / nobj_total;
631 if (p_indexed != a->last_p_indexed) {
632 a->last_p_indexed = p_indexed;
637 if (nobj_resolved > 0) {
638 p_resolved = (nobj_resolved * 100) /
639 (nobj_total - nobj_loose);
640 if (p_resolved != a->last_p_resolved) {
641 a->last_p_resolved = p_resolved;
649 if (print_size || print_indexed || print_resolved)
652 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
654 printf("; indexing %d%%", p_indexed);
656 printf("; resolving deltas %d%%", p_resolved);
657 if (print_size || print_indexed || print_resolved)
663 static const struct got_error *
664 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
665 const char *refname, struct got_repository *repo)
667 const struct got_error *err;
668 struct got_reference *ref;
672 err = got_ref_open(&ref, repo, refname, 0);
674 if (err->code != GOT_ERR_NOT_REF)
677 /* Treat argument as a reference prefix. */
678 err = got_ref_list(refs, repo, refname,
679 got_ref_cmp_by_name, NULL);
681 err = got_reflist_insert(new, refs, ref,
682 got_ref_cmp_by_name, NULL);
683 if (err || *new == NULL /* duplicate */)
690 static const struct got_error *
691 cmd_pack(int argc, char *argv[])
693 const struct got_error *error = NULL;
694 char *repo_path = NULL;
695 struct got_repository *repo = NULL;
696 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
697 struct got_object_id *pack_hash = NULL;
699 struct got_pack_progress_arg ppa;
700 FILE *packfile = NULL;
701 struct got_pathlist_head exclude_args;
702 struct got_pathlist_entry *pe;
703 struct got_reflist_head exclude_refs;
704 struct got_reflist_head include_refs;
705 struct got_reflist_entry *re, *new;
706 int *pack_fds = NULL;
708 TAILQ_INIT(&exclude_args);
709 TAILQ_INIT(&exclude_refs);
710 TAILQ_INIT(&include_refs);
713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
718 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
730 repo_path = realpath(optarg, NULL);
731 if (repo_path == NULL)
732 return got_error_from_errno2("realpath",
734 got_path_strip_trailing_slashes(repo_path);
737 got_path_strip_trailing_slashes(optarg);
738 error = got_pathlist_append(&exclude_args,
752 if (repo_path == NULL) {
753 error = get_repo_path(&repo_path);
757 error = got_repo_pack_fds_open(&pack_fds);
760 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
764 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
768 TAILQ_FOREACH(pe, &exclude_args, entry) {
769 const char *refname = pe->path;
770 error = add_ref(&new, &exclude_refs, refname, repo);
776 error = got_ref_list(&include_refs, repo, "",
777 got_ref_cmp_by_name, NULL);
781 for (i = 0; i < argc; i++) {
783 got_path_strip_trailing_slashes(argv[i]);
785 error = add_ref(&new, &include_refs, refname, repo);
791 /* Ignore references in the refs/got/ namespace. */
792 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
793 const char *refname = got_ref_get_name(re->ref);
794 if (strncmp("refs/got/", refname, 9) != 0)
796 TAILQ_REMOVE(&include_refs, re, entry);
797 got_ref_close(re->ref);
801 memset(&ppa, 0, sizeof(ppa));
802 ppa.last_scaled_size[0] = '\0';
803 ppa.last_p_indexed = -1;
804 ppa.last_p_resolved = -1;
805 ppa.verbosity = verbosity;
807 error = got_repo_pack_objects(&packfile, &pack_hash,
808 &include_refs, &exclude_refs, repo, loose_obj_only,
809 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
811 if (ppa.printed_something)
816 error = got_object_id_str(&id_str, pack_hash);
820 printf("\nWrote %s.pack\n", id_str);
822 error = got_repo_index_pack(packfile, pack_hash, repo,
823 pack_index_progress, &ppa, check_cancelled, NULL);
827 printf("\nIndexed %s.pack\n", id_str);
830 got_repo_close(repo);
832 const struct got_error *pack_err =
833 got_repo_pack_fds_close(pack_fds);
837 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
838 got_ref_list_free(&exclude_refs);
839 got_ref_list_free(&include_refs);
847 usage_indexpack(void)
849 fprintf(stderr, "usage: %s indexpack packfile-path\n",
854 static const struct got_error *
855 cmd_indexpack(int argc, char *argv[])
857 const struct got_error *error = NULL;
858 struct got_repository *repo = NULL;
860 struct got_object_id *pack_hash = NULL;
861 char *packfile_path = NULL;
863 struct got_pack_progress_arg ppa;
864 FILE *packfile = NULL;
865 int *pack_fds = NULL;
867 while ((ch = getopt(argc, argv, "")) != -1) {
881 packfile_path = realpath(argv[0], NULL);
882 if (packfile_path == NULL)
883 return got_error_from_errno2("realpath", argv[0]);
886 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
891 error = got_repo_pack_fds_open(&pack_fds);
894 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
898 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
902 memset(&ppa, 0, sizeof(ppa));
903 ppa.last_scaled_size[0] = '\0';
904 ppa.last_p_indexed = -1;
905 ppa.last_p_resolved = -1;
907 error = got_repo_find_pack(&packfile, &pack_hash, repo,
912 error = got_object_id_str(&id_str, pack_hash);
916 error = got_repo_index_pack(packfile, pack_hash, repo,
917 pack_index_progress, &ppa, check_cancelled, NULL);
920 printf("\nIndexed %s.pack\n", id_str);
923 got_repo_close(repo);
925 const struct got_error *pack_err =
926 got_repo_pack_fds_close(pack_fds);
938 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
943 struct gotadmin_list_pack_cb_args {
953 static const struct got_error *
954 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
955 off_t size, off_t base_offset, struct got_object_id *base_id)
957 const struct got_error *err;
958 struct gotadmin_list_pack_cb_args *a = arg;
959 char *id_str, *delta_str = NULL, *base_id_str = NULL;
960 const char *type_str;
962 err = got_object_id_str(&id_str, id);
967 case GOT_OBJ_TYPE_BLOB:
968 type_str = GOT_OBJ_LABEL_BLOB;
971 case GOT_OBJ_TYPE_TREE:
972 type_str = GOT_OBJ_LABEL_TREE;
975 case GOT_OBJ_TYPE_COMMIT:
976 type_str = GOT_OBJ_LABEL_COMMIT;
979 case GOT_OBJ_TYPE_TAG:
980 type_str = GOT_OBJ_LABEL_TAG;
983 case GOT_OBJ_TYPE_OFFSET_DELTA:
984 type_str = "offset-delta";
985 if (asprintf(&delta_str, " base-offset %lld",
986 (long long)base_offset) == -1) {
987 err = got_error_from_errno("asprintf");
992 case GOT_OBJ_TYPE_REF_DELTA:
993 type_str = "ref-delta";
994 err = got_object_id_str(&base_id_str, base_id);
997 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
998 err = got_error_from_errno("asprintf");
1004 err = got_error(GOT_ERR_OBJ_TYPE);
1007 if (a->human_readable) {
1008 char scaled[FMT_SCALED_STRSIZE];
1010 if (fmt_scaled(size, scaled) == -1) {
1011 err = got_error_from_errno("fmt_scaled");
1015 while (isspace((unsigned char)*s))
1017 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1018 (long long)offset, s, delta_str ? delta_str : "");
1020 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1021 (long long)offset, (long long)size,
1022 delta_str ? delta_str : "");
1031 static const struct got_error *
1032 cmd_listpack(int argc, char *argv[])
1034 const struct got_error *error = NULL;
1035 struct got_repository *repo = NULL;
1037 struct got_object_id *pack_hash = NULL;
1038 char *packfile_path = NULL;
1039 char *id_str = NULL;
1040 struct gotadmin_list_pack_cb_args lpa;
1041 FILE *packfile = NULL;
1042 int show_stats = 0, human_readable = 0;
1043 int *pack_fds = NULL;
1046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1051 while ((ch = getopt(argc, argv, "hs")) != -1) {
1070 packfile_path = realpath(argv[0], NULL);
1071 if (packfile_path == NULL)
1072 return got_error_from_errno2("realpath", argv[0]);
1074 error = got_repo_pack_fds_open(&pack_fds);
1077 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1081 /* Remove "cpath" promise. */
1082 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1086 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1090 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1094 error = got_object_id_str(&id_str, pack_hash);
1098 memset(&lpa, 0, sizeof(lpa));
1099 lpa.human_readable = human_readable;
1100 error = got_repo_list_pack(packfile, pack_hash, repo,
1101 list_pack_cb, &lpa, check_cancelled, NULL);
1105 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1106 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1107 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1108 lpa.noffdeltas + lpa.nrefdeltas,
1109 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1110 lpa.noffdeltas, lpa.nrefdeltas);
1114 got_repo_close(repo);
1116 const struct got_error *pack_err =
1117 got_repo_pack_fds_close(pack_fds);
1123 free(packfile_path);
1130 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1135 struct got_cleanup_progress_arg {
1140 int printed_something;
1144 static const struct got_error *
1145 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1147 struct got_cleanup_progress_arg *a = arg;
1148 int print_loose = 0, print_commits = 0, print_purged = 0;
1150 if (a->last_nloose != nloose) {
1152 a->last_nloose = nloose;
1154 if (a->last_ncommits != ncommits) {
1157 a->last_ncommits = ncommits;
1159 if (a->last_npurged != npurged) {
1163 a->last_npurged = npurged;
1166 if (a->verbosity < 0)
1169 if (print_loose || print_commits || print_purged)
1172 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1174 printf("; %d commit%s scanned", ncommits,
1175 ncommits == 1 ? "" : "s");
1178 printf("; %d object%s could be purged", npurged,
1179 npurged == 1 ? "" : "s");
1181 printf("; %d object%s purged", npurged,
1182 npurged == 1 ? "" : "s");
1185 if (print_loose || print_commits || print_purged) {
1186 a->printed_something = 1;
1192 struct got_lonely_packidx_progress_arg {
1194 int printed_something;
1198 static const struct got_error *
1199 lonely_packidx_progress(void *arg, const char *path)
1201 struct got_lonely_packidx_progress_arg *a = arg;
1203 if (a->verbosity < 0)
1207 printf("%s could be removed\n", path);
1209 printf("%s removed\n", path);
1211 a->printed_something = 1;
1215 static const struct got_error *
1216 cmd_cleanup(int argc, char *argv[])
1218 const struct got_error *error = NULL;
1219 char *repo_path = NULL;
1220 struct got_repository *repo = NULL;
1221 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1222 int remove_lonely_packidx = 0, ignore_mtime = 0;
1223 struct got_cleanup_progress_arg cpa;
1224 struct got_lonely_packidx_progress_arg lpa;
1225 off_t size_before, size_after;
1226 char scaled_before[FMT_SCALED_STRSIZE];
1227 char scaled_after[FMT_SCALED_STRSIZE];
1228 char scaled_diff[FMT_SCALED_STRSIZE];
1229 int *pack_fds = NULL;
1232 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1237 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1246 remove_lonely_packidx = 1;
1252 repo_path = realpath(optarg, NULL);
1253 if (repo_path == NULL)
1254 return got_error_from_errno2("realpath",
1256 got_path_strip_trailing_slashes(repo_path);
1267 if (repo_path == NULL) {
1268 error = get_repo_path(&repo_path);
1272 error = got_repo_pack_fds_open(&pack_fds);
1275 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1279 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1283 if (got_repo_has_extension(repo, "preciousObjects")) {
1284 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1285 "the preciousObjects Git extension is enabled; "
1286 "this implies that objects must not be deleted");
1290 if (remove_lonely_packidx) {
1291 memset(&lpa, 0, sizeof(lpa));
1292 lpa.dry_run = dry_run;
1293 lpa.verbosity = verbosity;
1294 error = got_repo_remove_lonely_packidx(repo, dry_run,
1295 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1299 memset(&cpa, 0, sizeof(cpa));
1300 cpa.last_ncommits = -1;
1301 cpa.last_npurged = -1;
1302 cpa.dry_run = dry_run;
1303 cpa.verbosity = verbosity;
1304 error = got_repo_purge_unreferenced_loose_objects(repo,
1305 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1306 cleanup_progress, &cpa, check_cancelled, NULL);
1307 if (cpa.printed_something)
1311 if (cpa.printed_something) {
1312 if (fmt_scaled(size_before, scaled_before) == -1) {
1313 error = got_error_from_errno("fmt_scaled");
1316 if (fmt_scaled(size_after, scaled_after) == -1) {
1317 error = got_error_from_errno("fmt_scaled");
1320 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1321 error = got_error_from_errno("fmt_scaled");
1324 printf("loose total size before: %s\n", scaled_before);
1325 printf("loose total size after: %s\n", scaled_after);
1327 printf("disk space which would be freed: %s\n",
1330 printf("disk space freed: %s\n", scaled_diff);
1331 printf("loose objects also found in pack files: %d\n", npacked);
1335 got_repo_close(repo);
1337 const struct got_error *pack_err =
1338 got_repo_pack_fds_close(pack_fds);