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>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_gotconfig.h"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
45 #include "got_worktree.h"
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 static volatile sig_atomic_t sigint_received;
52 static volatile sig_atomic_t sigpipe_received;
55 catch_sigint(int signo)
61 catch_sigpipe(int signo)
66 static const struct got_error *
67 check_cancelled(void *arg)
69 if (sigint_received || sigpipe_received)
70 return got_error(GOT_ERR_CANCELLED);
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
81 __dead static void usage(int, int);
82 __dead static void usage_info(void);
83 __dead static void usage_pack(void);
84 __dead static void usage_indexpack(void);
85 __dead static void usage_listpack(void);
86 __dead static void usage_cleanup(void);
88 static const struct got_error* cmd_info(int, char *[]);
89 static const struct got_error* cmd_pack(int, char *[]);
90 static const struct got_error* cmd_indexpack(int, char *[]);
91 static const struct got_error* cmd_listpack(int, char *[]);
92 static const struct got_error* cmd_cleanup(int, char *[]);
94 static const struct gotadmin_cmd gotadmin_commands[] = {
95 { "info", cmd_info, usage_info, "" },
96 { "pack", cmd_pack, usage_pack, "" },
97 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
98 { "listpack", cmd_listpack, usage_listpack, "ls" },
99 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
103 list_commands(FILE *fp)
107 fprintf(fp, "commands:");
108 for (i = 0; i < nitems(gotadmin_commands); i++) {
109 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
110 fprintf(fp, " %s", cmd->cmd_name);
116 main(int argc, char *argv[])
118 const struct gotadmin_cmd *cmd;
121 int hflag = 0, Vflag = 0;
122 static const struct option longopts[] = {
123 { "version", no_argument, NULL, 'V' },
127 setlocale(LC_CTYPE, "");
129 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
149 got_version_print_str();
154 usage(hflag, hflag ? 0 : 1);
156 signal(SIGINT, catch_sigint);
157 signal(SIGPIPE, catch_sigpipe);
159 for (i = 0; i < nitems(gotadmin_commands); i++) {
160 const struct got_error *error;
162 cmd = &gotadmin_commands[i];
164 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
165 strcmp(cmd->cmd_alias, argv[0]) != 0)
171 error = cmd->cmd_main(argc, argv);
172 if (error && error->code != GOT_ERR_CANCELLED &&
173 error->code != GOT_ERR_PRIVSEP_EXIT &&
174 !(sigpipe_received &&
175 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
177 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 list_commands(stderr);
191 usage(int hflag, int status)
193 FILE *fp = (status == 0) ? stdout : stderr;
195 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
202 static const struct got_error *
203 apply_unveil(const char *repo_path, int repo_read_only)
205 const struct got_error *err;
208 if (unveil("gmon.out", "rwc") != 0)
209 return got_error_from_errno2("unveil", "gmon.out");
211 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
212 return got_error_from_errno2("unveil", repo_path);
214 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
215 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
217 err = got_privsep_unveil_exec_helpers();
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno("unveil");
230 fprintf(stderr, "usage: %s info [-r repository-path]\n",
235 static const struct got_error *
236 get_repo_path(char **repo_path)
238 const struct got_error *err = NULL;
239 struct got_worktree *worktree = NULL;
244 cwd = getcwd(NULL, 0);
246 return got_error_from_errno("getcwd");
248 err = got_worktree_open(&worktree, cwd);
250 if (err->code != GOT_ERR_NOT_WORKTREE)
256 *repo_path = strdup(got_worktree_get_repo_path(worktree));
258 *repo_path = strdup(cwd);
259 if (*repo_path == NULL)
260 err = got_error_from_errno("strdup");
263 got_worktree_close(worktree);
268 static const struct got_error *
269 cmd_info(int argc, char *argv[])
271 const struct got_error *error = NULL;
272 char *repo_path = NULL;
273 struct got_repository *repo = NULL;
274 const struct got_gotconfig *gotconfig = NULL;
275 int ch, npackfiles, npackedobj, nobj;
276 off_t packsize, loose_size;
277 char scaled[FMT_SCALED_STRSIZE];
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 flock proc exec sendfd unveil",
302 if (repo_path == NULL) {
303 error = get_repo_path(&repo_path);
307 error = got_repo_open(&repo, repo_path, NULL);
311 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
315 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
317 gotconfig = got_repo_get_gotconfig(repo);
319 const struct got_remote_repo *remotes;
321 if (got_gotconfig_get_author(gotconfig)) {
322 printf("default author: %s\n",
323 got_gotconfig_get_author(gotconfig));
325 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
326 for (i = 0; i < nremotes; i++) {
327 const char *fetch_url = remotes[i].fetch_url;
328 const char *send_url = remotes[i].send_url;
329 if (strcmp(fetch_url, send_url) == 0) {
330 printf("remote \"%s\": %s\n", remotes[i].name,
331 remotes[i].fetch_url);
333 printf("remote \"%s\" (fetch): %s\n",
334 remotes[i].name, remotes[i].fetch_url);
335 printf("remote \"%s\" (send): %s\n",
336 remotes[i].name, remotes[i].send_url);
341 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
345 printf("pack files: %d\n", npackfiles);
346 if (npackfiles > 0) {
347 if (fmt_scaled(packsize, scaled) == -1) {
348 error = got_error_from_errno("fmt_scaled");
351 printf("packed objects: %d\n", npackedobj);
352 printf("packed total size: %s\n", scaled);
355 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
358 printf("loose objects: %d\n", nobj);
360 if (fmt_scaled(loose_size, scaled) == -1) {
361 error = got_error_from_errno("fmt_scaled");
364 printf("loose total size: %s\n", scaled);
368 got_repo_close(repo);
376 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
377 "[-x reference] [-q] [reference ...]\n",
382 struct got_pack_progress_arg {
383 char last_scaled_size[FMT_SCALED_STRSIZE];
395 int printed_something;
399 print_load_info(int print_colored, int print_found, int print_trees,
400 int ncolored, int nfound, int ntrees)
403 printf("%d commit%s colored", ncolored,
404 ncolored == 1 ? "" : "s");
407 printf("%s%d object%s found",
408 ncolored > 0 ? "; " : "",
409 nfound, nfound == 1 ? "" : "s");
412 printf("; %d tree%s scanned", ntrees,
413 ntrees == 1 ? "" : "s");
417 static const struct got_error *
418 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
419 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
422 struct got_pack_progress_arg *a = arg;
423 char scaled_size[FMT_SCALED_STRSIZE];
424 int p_deltify, p_written;
425 int print_colored = 0, print_found = 0, print_trees = 0;
426 int print_searching = 0, print_total = 0;
427 int print_deltify = 0, print_written = 0;
429 if (a->verbosity < 0)
432 if (a->last_ncolored != ncolored) {
434 a->last_ncolored = ncolored;
437 if (a->last_nfound != nfound) {
440 a->last_nfound = nfound;
443 if (a->last_ntrees != ntrees) {
447 a->last_ntrees = ntrees;
450 if ((print_colored || print_found || print_trees) &&
453 print_load_info(print_colored, print_found, print_trees,
454 ncolored, nfound, ntrees);
455 a->printed_something = 1;
458 } else if (!a->loading_done) {
460 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
465 if (fmt_scaled(packfile_size, scaled_size) == -1)
466 return got_error_from_errno("fmt_scaled");
468 if (a->last_ncommits != ncommits) {
470 a->last_ncommits = ncommits;
473 if (a->last_nobj_total != nobj_total) {
476 a->last_nobj_total = nobj_total;
479 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
480 strcmp(scaled_size, a->last_scaled_size)) != 0) {
481 if (strlcpy(a->last_scaled_size, scaled_size,
482 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
483 return got_error(GOT_ERR_NO_SPACE);
486 if (nobj_deltify > 0 || nobj_written > 0) {
487 if (nobj_deltify > 0) {
488 p_deltify = (nobj_deltify * 100) / nobj_total;
489 if (p_deltify != a->last_p_deltify) {
490 a->last_p_deltify = p_deltify;
496 if (nobj_written > 0) {
497 p_written = (nobj_written * 100) / nobj_total;
498 if (p_written != a->last_p_written) {
499 a->last_p_written = p_written;
508 if (print_searching || print_total || print_deltify || print_written)
511 printf("packing %d reference%s", ncommits,
512 ncommits == 1 ? "" : "s");
514 printf("; %d object%s", nobj_total,
515 nobj_total == 1 ? "" : "s");
517 printf("; deltify: %d%%", p_deltify);
519 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
520 scaled_size, p_written);
521 if (print_searching || print_total || print_deltify ||
523 a->printed_something = 1;
529 static const struct got_error *
530 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
531 int nobj_indexed, int nobj_loose, int nobj_resolved)
533 struct got_pack_progress_arg *a = arg;
534 char scaled_size[FMT_SCALED_STRSIZE];
535 int p_indexed, p_resolved;
536 int print_size = 0, print_indexed = 0, print_resolved = 0;
538 if (a->verbosity < 0)
541 if (packfile_size > 0 || nobj_indexed > 0) {
542 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
543 (a->last_scaled_size[0] == '\0' ||
544 strcmp(scaled_size, a->last_scaled_size)) != 0) {
546 if (strlcpy(a->last_scaled_size, scaled_size,
547 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
548 return got_error(GOT_ERR_NO_SPACE);
550 if (nobj_indexed > 0) {
551 p_indexed = (nobj_indexed * 100) / nobj_total;
552 if (p_indexed != a->last_p_indexed) {
553 a->last_p_indexed = p_indexed;
558 if (nobj_resolved > 0) {
559 p_resolved = (nobj_resolved * 100) /
560 (nobj_total - nobj_loose);
561 if (p_resolved != a->last_p_resolved) {
562 a->last_p_resolved = p_resolved;
570 if (print_size || print_indexed || print_resolved)
573 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
575 printf("; indexing %d%%", p_indexed);
577 printf("; resolving deltas %d%%", p_resolved);
578 if (print_size || print_indexed || print_resolved)
584 static const struct got_error *
585 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
586 const char *refname, struct got_repository *repo)
588 const struct got_error *err;
589 struct got_reference *ref;
593 err = got_ref_open(&ref, repo, refname, 0);
595 if (err->code != GOT_ERR_NOT_REF)
598 /* Treat argument as a reference prefix. */
599 err = got_ref_list(refs, repo, refname,
600 got_ref_cmp_by_name, NULL);
602 err = got_reflist_insert(new, refs, ref,
603 got_ref_cmp_by_name, NULL);
604 if (err || *new == NULL /* duplicate */)
611 static const struct got_error *
612 cmd_pack(int argc, char *argv[])
614 const struct got_error *error = NULL;
615 char *repo_path = NULL;
616 struct got_repository *repo = NULL;
617 int ch, i, loose_obj_only = 1, verbosity = 0;
618 struct got_object_id *pack_hash = NULL;
620 struct got_pack_progress_arg ppa;
621 FILE *packfile = NULL;
622 struct got_pathlist_head exclude_args;
623 struct got_pathlist_entry *pe;
624 struct got_reflist_head exclude_refs;
625 struct got_reflist_head include_refs;
626 struct got_reflist_entry *re, *new;
628 TAILQ_INIT(&exclude_args);
629 TAILQ_INIT(&exclude_refs);
630 TAILQ_INIT(&include_refs);
632 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
638 repo_path = realpath(optarg, NULL);
639 if (repo_path == NULL)
640 return got_error_from_errno2("realpath",
642 got_path_strip_trailing_slashes(repo_path);
645 got_path_strip_trailing_slashes(optarg);
646 error = got_pathlist_append(&exclude_args,
664 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
668 if (repo_path == NULL) {
669 error = get_repo_path(&repo_path);
673 error = got_repo_open(&repo, repo_path, NULL);
677 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
681 TAILQ_FOREACH(pe, &exclude_args, entry) {
682 const char *refname = pe->path;
683 error = add_ref(&new, &exclude_refs, refname, repo);
690 error = got_ref_list(&include_refs, repo, "",
691 got_ref_cmp_by_name, NULL);
695 for (i = 0; i < argc; i++) {
697 got_path_strip_trailing_slashes(argv[i]);
699 error = add_ref(&new, &include_refs, refname, repo);
705 /* Ignore references in the refs/got/ namespace. */
706 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
707 const char *refname = got_ref_get_name(re->ref);
708 if (strncmp("refs/got/", refname, 9) != 0)
710 TAILQ_REMOVE(&include_refs, re, entry);
711 got_ref_close(re->ref);
715 memset(&ppa, 0, sizeof(ppa));
716 ppa.last_scaled_size[0] = '\0';
717 ppa.last_p_indexed = -1;
718 ppa.last_p_resolved = -1;
719 ppa.verbosity = verbosity;
721 error = got_repo_pack_objects(&packfile, &pack_hash,
722 &include_refs, &exclude_refs, repo, loose_obj_only,
723 pack_progress, &ppa, check_cancelled, NULL);
725 if (ppa.printed_something)
730 error = got_object_id_str(&id_str, pack_hash);
734 printf("\nWrote %s.pack\n", id_str);
736 error = got_repo_index_pack(packfile, pack_hash, repo,
737 pack_index_progress, &ppa, check_cancelled, NULL);
741 printf("\nIndexed %s.pack\n", id_str);
744 got_repo_close(repo);
745 got_pathlist_free(&exclude_args);
746 got_ref_list_free(&exclude_refs);
747 got_ref_list_free(&include_refs);
755 usage_indexpack(void)
757 fprintf(stderr, "usage: %s indexpack packfile-path\n",
762 static const struct got_error *
763 cmd_indexpack(int argc, char *argv[])
765 const struct got_error *error = NULL;
766 struct got_repository *repo = NULL;
768 struct got_object_id *pack_hash = NULL;
769 char *packfile_path = NULL;
771 struct got_pack_progress_arg ppa;
772 FILE *packfile = NULL;
774 while ((ch = getopt(argc, argv, "")) != -1) {
788 packfile_path = realpath(argv[0], NULL);
789 if (packfile_path == NULL)
790 return got_error_from_errno2("realpath", argv[0]);
793 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
798 error = got_repo_open(&repo, packfile_path, NULL);
802 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
806 memset(&ppa, 0, sizeof(ppa));
807 ppa.last_scaled_size[0] = '\0';
808 ppa.last_p_indexed = -1;
809 ppa.last_p_resolved = -1;
811 error = got_repo_find_pack(&packfile, &pack_hash, repo,
816 error = got_object_id_str(&id_str, pack_hash);
820 error = got_repo_index_pack(packfile, pack_hash, repo,
821 pack_index_progress, &ppa, check_cancelled, NULL);
824 printf("\nIndexed %s.pack\n", id_str);
827 got_repo_close(repo);
836 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
841 struct gotadmin_list_pack_cb_args {
851 static const struct got_error *
852 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
853 off_t size, off_t base_offset, struct got_object_id *base_id)
855 const struct got_error *err;
856 struct gotadmin_list_pack_cb_args *a = arg;
857 char *id_str, *delta_str = NULL, *base_id_str = NULL;
858 const char *type_str;
860 err = got_object_id_str(&id_str, id);
865 case GOT_OBJ_TYPE_BLOB:
866 type_str = GOT_OBJ_LABEL_BLOB;
869 case GOT_OBJ_TYPE_TREE:
870 type_str = GOT_OBJ_LABEL_TREE;
873 case GOT_OBJ_TYPE_COMMIT:
874 type_str = GOT_OBJ_LABEL_COMMIT;
877 case GOT_OBJ_TYPE_TAG:
878 type_str = GOT_OBJ_LABEL_TAG;
881 case GOT_OBJ_TYPE_OFFSET_DELTA:
882 type_str = "offset-delta";
883 if (asprintf(&delta_str, " base-offset %lld",
884 (long long)base_offset) == -1) {
885 err = got_error_from_errno("asprintf");
890 case GOT_OBJ_TYPE_REF_DELTA:
891 type_str = "ref-delta";
892 err = got_object_id_str(&base_id_str, base_id);
895 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
896 err = got_error_from_errno("asprintf");
902 err = got_error(GOT_ERR_OBJ_TYPE);
905 if (a->human_readable) {
906 char scaled[FMT_SCALED_STRSIZE];
908 if (fmt_scaled(size, scaled) == -1) {
909 err = got_error_from_errno("fmt_scaled");
913 while (isspace((unsigned char)*s))
915 printf("%s %s at %lld size %s%s\n", id_str, type_str,
916 (long long)offset, s, delta_str ? delta_str : "");
918 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
919 (long long)offset, (long long)size,
920 delta_str ? delta_str : "");
929 static const struct got_error *
930 cmd_listpack(int argc, char *argv[])
932 const struct got_error *error = NULL;
933 struct got_repository *repo = NULL;
935 struct got_object_id *pack_hash = NULL;
936 char *packfile_path = NULL;
938 struct gotadmin_list_pack_cb_args lpa;
939 FILE *packfile = NULL;
940 int show_stats = 0, human_readable = 0;
942 while ((ch = getopt(argc, argv, "hs")) != -1) {
961 packfile_path = realpath(argv[0], NULL);
962 if (packfile_path == NULL)
963 return got_error_from_errno2("realpath", argv[0]);
966 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
970 error = got_repo_open(&repo, packfile_path, NULL);
974 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
978 error = got_repo_find_pack(&packfile, &pack_hash, repo,
982 error = got_object_id_str(&id_str, pack_hash);
986 memset(&lpa, 0, sizeof(lpa));
987 lpa.human_readable = human_readable;
988 error = got_repo_list_pack(packfile, pack_hash, repo,
989 list_pack_cb, &lpa, check_cancelled, NULL);
993 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
994 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
995 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
996 lpa.noffdeltas + lpa.nrefdeltas,
997 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
998 lpa.noffdeltas, lpa.nrefdeltas);
1002 got_repo_close(repo);
1005 free(packfile_path);
1012 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1013 "[-q]\n", getprogname());
1017 struct got_cleanup_progress_arg {
1022 int printed_something;
1026 static const struct got_error *
1027 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1029 struct got_cleanup_progress_arg *a = arg;
1030 int print_loose = 0, print_commits = 0, print_purged = 0;
1032 if (a->last_nloose != nloose) {
1034 a->last_nloose = nloose;
1036 if (a->last_ncommits != ncommits) {
1039 a->last_ncommits = ncommits;
1041 if (a->last_npurged != npurged) {
1045 a->last_npurged = npurged;
1048 if (a->verbosity < 0)
1051 if (print_loose || print_commits || print_purged)
1054 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1056 printf("; %d commit%s scanned", ncommits,
1057 ncommits == 1 ? "" : "s");
1060 printf("; %d object%s could be purged", npurged,
1061 npurged == 1 ? "" : "s");
1063 printf("; %d object%s purged", npurged,
1064 npurged == 1 ? "" : "s");
1067 if (print_loose || print_commits || print_purged) {
1068 a->printed_something = 1;
1074 struct got_lonely_packidx_progress_arg {
1076 int printed_something;
1080 static const struct got_error *
1081 lonely_packidx_progress(void *arg, const char *path)
1083 struct got_lonely_packidx_progress_arg *a = arg;
1085 if (a->verbosity < 0)
1089 printf("%s could be removed\n", path);
1091 printf("%s removed\n", path);
1093 a->printed_something = 1;
1097 static const struct got_error *
1098 cmd_cleanup(int argc, char *argv[])
1100 const struct got_error *error = NULL;
1101 char *repo_path = NULL;
1102 struct got_repository *repo = NULL;
1103 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1104 int remove_lonely_packidx = 0, ignore_mtime = 0;
1105 struct got_cleanup_progress_arg cpa;
1106 struct got_lonely_packidx_progress_arg lpa;
1107 off_t size_before, size_after;
1108 char scaled_before[FMT_SCALED_STRSIZE];
1109 char scaled_after[FMT_SCALED_STRSIZE];
1110 char scaled_diff[FMT_SCALED_STRSIZE];
1114 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1120 remove_lonely_packidx = 1;
1123 repo_path = realpath(optarg, NULL);
1124 if (repo_path == NULL)
1125 return got_error_from_errno2("realpath",
1127 got_path_strip_trailing_slashes(repo_path);
1145 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1149 if (repo_path == NULL) {
1150 error = get_repo_path(&repo_path);
1154 error = got_repo_open(&repo, repo_path, NULL);
1158 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1162 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1164 for (i = 0; i < nextensions; i++) {
1165 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1166 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1167 "the preciousObjects Git extension is enabled; "
1168 "this implies that objects must not be deleted");
1173 if (remove_lonely_packidx) {
1174 memset(&lpa, 0, sizeof(lpa));
1175 lpa.dry_run = dry_run;
1176 lpa.verbosity = verbosity;
1177 error = got_repo_remove_lonely_packidx(repo, dry_run,
1178 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1182 memset(&cpa, 0, sizeof(cpa));
1183 cpa.last_ncommits = -1;
1184 cpa.last_npurged = -1;
1185 cpa.dry_run = dry_run;
1186 cpa.verbosity = verbosity;
1187 error = got_repo_purge_unreferenced_loose_objects(repo,
1188 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1189 cleanup_progress, &cpa, check_cancelled, NULL);
1190 if (cpa.printed_something)
1194 if (cpa.printed_something) {
1195 if (fmt_scaled(size_before, scaled_before) == -1) {
1196 error = got_error_from_errno("fmt_scaled");
1199 if (fmt_scaled(size_after, scaled_after) == -1) {
1200 error = got_error_from_errno("fmt_scaled");
1203 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1204 error = got_error_from_errno("fmt_scaled");
1207 printf("loose total size before: %s\n", scaled_before);
1208 printf("loose total size after: %s\n", scaled_after);
1210 printf("disk space which would be freed: %s\n",
1213 printf("disk space freed: %s\n", scaled_diff);
1214 printf("loose objects also found in pack files: %d\n", npacked);
1218 got_repo_close(repo);