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_repository_dump.h"
43 #include "got_gotconfig.h"
45 #include "got_privsep.h"
46 #include "got_opentemp.h"
47 #include "got_worktree.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
57 catch_sigint(int signo)
63 catch_sigpipe(int signo)
68 static const struct got_error *
69 check_cancelled(void *arg)
71 if (sigint_received || sigpipe_received)
72 return got_error(GOT_ERR_CANCELLED);
78 const struct got_error *(*cmd_main)(int, char *[]);
79 void (*cmd_usage)(void);
80 const char *cmd_alias;
83 __dead static void usage(int, int);
84 __dead static void usage_init(void);
85 __dead static void usage_info(void);
86 __dead static void usage_pack(void);
87 __dead static void usage_indexpack(void);
88 __dead static void usage_listpack(void);
89 __dead static void usage_cleanup(void);
90 __dead static void usage_dump(void);
92 static const struct got_error* cmd_init(int, char *[]);
93 static const struct got_error* cmd_info(int, char *[]);
94 static const struct got_error* cmd_pack(int, char *[]);
95 static const struct got_error* cmd_indexpack(int, char *[]);
96 static const struct got_error* cmd_listpack(int, char *[]);
97 static const struct got_error* cmd_cleanup(int, char *[]);
98 static const struct got_error* cmd_dump(int, char *[]);
100 static const struct gotadmin_cmd gotadmin_commands[] = {
101 { "init", cmd_init, usage_init, "" },
102 { "info", cmd_info, usage_info, "" },
103 { "pack", cmd_pack, usage_pack, "" },
104 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
105 { "listpack", cmd_listpack, usage_listpack, "ls" },
106 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
107 { "dump", cmd_dump, usage_dump, "" },
111 list_commands(FILE *fp)
115 fprintf(fp, "commands:");
116 for (i = 0; i < nitems(gotadmin_commands); i++) {
117 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
118 fprintf(fp, " %s", cmd->cmd_name);
124 main(int argc, char *argv[])
126 const struct gotadmin_cmd *cmd;
129 int hflag = 0, Vflag = 0;
130 static const struct option longopts[] = {
131 { "version", no_argument, NULL, 'V' },
135 setlocale(LC_CTYPE, "");
137 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
157 got_version_print_str();
162 usage(hflag, hflag ? 0 : 1);
164 signal(SIGINT, catch_sigint);
165 signal(SIGPIPE, catch_sigpipe);
167 for (i = 0; i < nitems(gotadmin_commands); i++) {
168 const struct got_error *error;
170 cmd = &gotadmin_commands[i];
172 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
173 strcmp(cmd->cmd_alias, argv[0]) != 0)
179 error = cmd->cmd_main(argc, argv);
180 if (error && error->code != GOT_ERR_CANCELLED &&
181 error->code != GOT_ERR_PRIVSEP_EXIT &&
182 !(sigpipe_received &&
183 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
185 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
186 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
193 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
194 list_commands(stderr);
199 usage(int hflag, int status)
201 FILE *fp = (status == 0) ? stdout : stderr;
203 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
210 static const struct got_error *
211 apply_unveil(const char *repo_path, int repo_read_only)
213 const struct got_error *err;
216 if (unveil("gmon.out", "rwc") != 0)
217 return got_error_from_errno2("unveil", "gmon.out");
219 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
220 return got_error_from_errno2("unveil", repo_path);
222 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
223 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
225 err = got_privsep_unveil_exec_helpers();
229 if (unveil(NULL, NULL) != 0)
230 return got_error_from_errno("unveil");
238 fprintf(stderr, "usage: %s info [-r repository-path]\n",
243 static const struct got_error *
244 get_repo_path(char **repo_path)
246 const struct got_error *err = NULL;
247 struct got_worktree *worktree = NULL;
252 cwd = getcwd(NULL, 0);
254 return got_error_from_errno("getcwd");
256 err = got_worktree_open(&worktree, cwd);
258 if (err->code != GOT_ERR_NOT_WORKTREE)
264 *repo_path = strdup(got_worktree_get_repo_path(worktree));
266 *repo_path = strdup(cwd);
267 if (*repo_path == NULL)
268 err = got_error_from_errno("strdup");
271 got_worktree_close(worktree);
279 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
284 static const struct got_error *
285 cmd_init(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 const char *head_name = NULL;
289 char *repo_path = NULL;
293 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
297 while ((ch = getopt(argc, argv, "b:")) != -1) {
314 repo_path = strdup(argv[0]);
315 if (repo_path == NULL)
316 return got_error_from_errno("strdup");
318 got_path_strip_trailing_slashes(repo_path);
320 error = got_path_mkdir(repo_path);
322 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
325 error = apply_unveil(repo_path, 0);
329 error = got_repo_init(repo_path, head_name);
335 static const struct got_error *
336 cmd_info(int argc, char *argv[])
338 const struct got_error *error = NULL;
339 char *repo_path = NULL;
340 struct got_repository *repo = NULL;
341 const struct got_gotconfig *gotconfig = NULL;
342 int ch, npackfiles, npackedobj, nobj;
343 off_t packsize, loose_size;
344 char scaled[FMT_SCALED_STRSIZE];
345 int *pack_fds = NULL;
348 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
353 while ((ch = getopt(argc, argv, "r:")) != -1) {
356 repo_path = realpath(optarg, NULL);
357 if (repo_path == NULL)
358 return got_error_from_errno2("realpath",
360 got_path_strip_trailing_slashes(repo_path);
371 if (repo_path == NULL) {
372 error = get_repo_path(&repo_path);
376 error = got_repo_pack_fds_open(&pack_fds);
379 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
383 /* Remove "cpath" promise. */
384 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
388 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
392 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
394 gotconfig = got_repo_get_gotconfig(repo);
396 const struct got_remote_repo *remotes;
398 if (got_gotconfig_get_author(gotconfig)) {
399 printf("default author: %s\n",
400 got_gotconfig_get_author(gotconfig));
402 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
403 for (i = 0; i < nremotes; i++) {
404 const char *fetch_url = remotes[i].fetch_url;
405 const char *send_url = remotes[i].send_url;
406 if (strcmp(fetch_url, send_url) == 0) {
407 printf("remote \"%s\": %s\n", remotes[i].name,
408 remotes[i].fetch_url);
410 printf("remote \"%s\" (fetch): %s\n",
411 remotes[i].name, remotes[i].fetch_url);
412 printf("remote \"%s\" (send): %s\n",
413 remotes[i].name, remotes[i].send_url);
418 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
422 printf("pack files: %d\n", npackfiles);
423 if (npackfiles > 0) {
424 if (fmt_scaled(packsize, scaled) == -1) {
425 error = got_error_from_errno("fmt_scaled");
428 printf("packed objects: %d\n", npackedobj);
429 printf("packed total size: %s\n", scaled);
432 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
435 printf("loose objects: %d\n", nobj);
437 if (fmt_scaled(loose_size, scaled) == -1) {
438 error = got_error_from_errno("fmt_scaled");
441 printf("loose total size: %s\n", scaled);
445 got_repo_close(repo);
447 const struct got_error *pack_err =
448 got_repo_pack_fds_close(pack_fds);
460 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
461 "[-x reference] [reference ...]\n", getprogname());
465 struct got_pack_progress_arg {
467 char last_scaled_size[FMT_SCALED_STRSIZE];
479 int printed_something;
483 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
484 int ncolored, int nfound, int ntrees)
487 fprintf(out, "%d commit%s colored", ncolored,
488 ncolored == 1 ? "" : "s");
491 fprintf(out, "%s%d object%s found",
492 ncolored > 0 ? "; " : "",
493 nfound, nfound == 1 ? "" : "s");
496 fprintf(out, "; %d tree%s scanned", ntrees,
497 ntrees == 1 ? "" : "s");
501 static const struct got_error *
502 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
503 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
506 struct got_pack_progress_arg *a = arg;
507 char scaled_size[FMT_SCALED_STRSIZE];
508 int p_deltify, p_written;
509 int print_colored = 0, print_found = 0, print_trees = 0;
510 int print_searching = 0, print_total = 0;
511 int print_deltify = 0, print_written = 0;
513 if (a->verbosity < 0)
516 if (a->last_ncolored != ncolored) {
518 a->last_ncolored = ncolored;
521 if (a->last_nfound != nfound) {
524 a->last_nfound = nfound;
527 if (a->last_ntrees != ntrees) {
531 a->last_ntrees = ntrees;
534 if ((print_colored || print_found || print_trees) &&
536 fprintf(a->out, "\r");
537 print_load_info(a->out, print_colored, print_found,
538 print_trees, ncolored, nfound, ntrees);
539 a->printed_something = 1;
542 } else if (!a->loading_done) {
543 fprintf(a->out, "\r");
544 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
545 fprintf(a->out, "\n");
549 if (fmt_scaled(packfile_size, scaled_size) == -1)
550 return got_error_from_errno("fmt_scaled");
552 if (a->last_ncommits != ncommits) {
554 a->last_ncommits = ncommits;
557 if (a->last_nobj_total != nobj_total) {
560 a->last_nobj_total = nobj_total;
563 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
564 strcmp(scaled_size, a->last_scaled_size)) != 0) {
565 if (strlcpy(a->last_scaled_size, scaled_size,
566 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
567 return got_error(GOT_ERR_NO_SPACE);
570 if (nobj_deltify > 0 || nobj_written > 0) {
571 if (nobj_deltify > 0) {
572 p_deltify = (nobj_deltify * 100) / nobj_total;
573 if (p_deltify != a->last_p_deltify) {
574 a->last_p_deltify = p_deltify;
580 if (nobj_written > 0) {
581 p_written = (nobj_written * 100) / nobj_total;
582 if (p_written != a->last_p_written) {
583 a->last_p_written = p_written;
592 if (print_searching || print_total || print_deltify || print_written)
593 fprintf(a->out, "\r");
595 fprintf(a->out, "packing %d reference%s", ncommits,
596 ncommits == 1 ? "" : "s");
598 fprintf(a->out, "; %d object%s", nobj_total,
599 nobj_total == 1 ? "" : "s");
601 fprintf(a->out, "; deltify: %d%%", p_deltify);
603 fprintf(a->out, "; writing pack: %*s %d%%",
604 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
605 if (print_searching || print_total || print_deltify ||
607 a->printed_something = 1;
613 static const struct got_error *
614 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
615 int nobj_indexed, int nobj_loose, int nobj_resolved)
617 struct got_pack_progress_arg *a = arg;
618 char scaled_size[FMT_SCALED_STRSIZE];
619 int p_indexed, p_resolved;
620 int print_size = 0, print_indexed = 0, print_resolved = 0;
622 if (a->verbosity < 0)
625 if (packfile_size > 0 || nobj_indexed > 0) {
626 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
627 (a->last_scaled_size[0] == '\0' ||
628 strcmp(scaled_size, a->last_scaled_size)) != 0) {
630 if (strlcpy(a->last_scaled_size, scaled_size,
631 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
632 return got_error(GOT_ERR_NO_SPACE);
634 if (nobj_indexed > 0) {
635 p_indexed = (nobj_indexed * 100) / nobj_total;
636 if (p_indexed != a->last_p_indexed) {
637 a->last_p_indexed = p_indexed;
642 if (nobj_resolved > 0) {
643 p_resolved = (nobj_resolved * 100) /
644 (nobj_total - nobj_loose);
645 if (p_resolved != a->last_p_resolved) {
646 a->last_p_resolved = p_resolved;
654 if (print_size || print_indexed || print_resolved)
657 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
659 printf("; indexing %d%%", p_indexed);
661 printf("; resolving deltas %d%%", p_resolved);
662 if (print_size || print_indexed || print_resolved)
668 static const struct got_error *
669 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
670 const char *refname, struct got_repository *repo)
672 const struct got_error *err;
673 struct got_reference *ref;
677 err = got_ref_open(&ref, repo, refname, 0);
679 if (err->code != GOT_ERR_NOT_REF)
682 /* Treat argument as a reference prefix. */
683 err = got_ref_list(refs, repo, refname,
684 got_ref_cmp_by_name, NULL);
686 err = got_reflist_insert(new, refs, ref,
687 got_ref_cmp_by_name, NULL);
688 if (err || *new == NULL /* duplicate */)
695 static const struct got_error *
696 cmd_pack(int argc, char *argv[])
698 const struct got_error *error = NULL;
699 char *repo_path = NULL;
700 struct got_repository *repo = NULL;
701 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
702 struct got_object_id *pack_hash = NULL;
704 struct got_pack_progress_arg ppa;
705 FILE *packfile = NULL;
706 struct got_pathlist_head exclude_args;
707 struct got_pathlist_entry *pe;
708 struct got_reflist_head exclude_refs;
709 struct got_reflist_head include_refs;
710 struct got_reflist_entry *re, *new;
711 int *pack_fds = NULL;
713 TAILQ_INIT(&exclude_args);
714 TAILQ_INIT(&exclude_refs);
715 TAILQ_INIT(&include_refs);
718 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
723 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
735 repo_path = realpath(optarg, NULL);
736 if (repo_path == NULL)
737 return got_error_from_errno2("realpath",
739 got_path_strip_trailing_slashes(repo_path);
742 got_path_strip_trailing_slashes(optarg);
743 error = got_pathlist_append(&exclude_args,
757 if (repo_path == NULL) {
758 error = get_repo_path(&repo_path);
762 error = got_repo_pack_fds_open(&pack_fds);
765 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
769 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
773 TAILQ_FOREACH(pe, &exclude_args, entry) {
774 const char *refname = pe->path;
775 error = add_ref(&new, &exclude_refs, refname, repo);
781 error = got_ref_list(&include_refs, repo, "",
782 got_ref_cmp_by_name, NULL);
786 for (i = 0; i < argc; i++) {
788 got_path_strip_trailing_slashes(argv[i]);
790 error = add_ref(&new, &include_refs, refname, repo);
796 /* Ignore references in the refs/got/ namespace. */
797 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
798 const char *refname = got_ref_get_name(re->ref);
799 if (strncmp("refs/got/", refname, 9) != 0)
801 TAILQ_REMOVE(&include_refs, re, entry);
802 got_ref_close(re->ref);
806 memset(&ppa, 0, sizeof(ppa));
808 ppa.last_scaled_size[0] = '\0';
809 ppa.last_p_indexed = -1;
810 ppa.last_p_resolved = -1;
811 ppa.verbosity = verbosity;
813 error = got_repo_pack_objects(&packfile, &pack_hash,
814 &include_refs, &exclude_refs, repo, loose_obj_only,
815 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
817 if (ppa.printed_something)
822 error = got_object_id_str(&id_str, pack_hash);
826 printf("\nWrote %s.pack\n", id_str);
828 error = got_repo_index_pack(packfile, pack_hash, repo,
829 pack_index_progress, &ppa, check_cancelled, NULL);
833 printf("\nIndexed %s.pack\n", id_str);
836 got_repo_close(repo);
838 const struct got_error *pack_err =
839 got_repo_pack_fds_close(pack_fds);
843 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
844 got_ref_list_free(&exclude_refs);
845 got_ref_list_free(&include_refs);
853 usage_indexpack(void)
855 fprintf(stderr, "usage: %s indexpack packfile-path\n",
860 static const struct got_error *
861 cmd_indexpack(int argc, char *argv[])
863 const struct got_error *error = NULL;
864 struct got_repository *repo = NULL;
866 struct got_object_id *pack_hash = NULL;
867 char *packfile_path = NULL;
869 struct got_pack_progress_arg ppa;
870 FILE *packfile = NULL;
871 int *pack_fds = NULL;
873 while ((ch = getopt(argc, argv, "")) != -1) {
887 packfile_path = realpath(argv[0], NULL);
888 if (packfile_path == NULL)
889 return got_error_from_errno2("realpath", argv[0]);
892 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
897 error = got_repo_pack_fds_open(&pack_fds);
900 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
904 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
908 memset(&ppa, 0, sizeof(ppa));
910 ppa.last_scaled_size[0] = '\0';
911 ppa.last_p_indexed = -1;
912 ppa.last_p_resolved = -1;
914 error = got_repo_find_pack(&packfile, &pack_hash, repo,
919 error = got_object_id_str(&id_str, pack_hash);
923 error = got_repo_index_pack(packfile, pack_hash, repo,
924 pack_index_progress, &ppa, check_cancelled, NULL);
927 printf("\nIndexed %s.pack\n", id_str);
930 got_repo_close(repo);
932 const struct got_error *pack_err =
933 got_repo_pack_fds_close(pack_fds);
945 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
950 struct gotadmin_list_pack_cb_args {
960 static const struct got_error *
961 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
962 off_t size, off_t base_offset, struct got_object_id *base_id)
964 const struct got_error *err;
965 struct gotadmin_list_pack_cb_args *a = arg;
966 char *id_str, *delta_str = NULL, *base_id_str = NULL;
967 const char *type_str;
969 err = got_object_id_str(&id_str, id);
974 case GOT_OBJ_TYPE_BLOB:
975 type_str = GOT_OBJ_LABEL_BLOB;
978 case GOT_OBJ_TYPE_TREE:
979 type_str = GOT_OBJ_LABEL_TREE;
982 case GOT_OBJ_TYPE_COMMIT:
983 type_str = GOT_OBJ_LABEL_COMMIT;
986 case GOT_OBJ_TYPE_TAG:
987 type_str = GOT_OBJ_LABEL_TAG;
990 case GOT_OBJ_TYPE_OFFSET_DELTA:
991 type_str = "offset-delta";
992 if (asprintf(&delta_str, " base-offset %lld",
993 (long long)base_offset) == -1) {
994 err = got_error_from_errno("asprintf");
999 case GOT_OBJ_TYPE_REF_DELTA:
1000 type_str = "ref-delta";
1001 err = got_object_id_str(&base_id_str, base_id);
1004 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1005 err = got_error_from_errno("asprintf");
1011 err = got_error(GOT_ERR_OBJ_TYPE);
1014 if (a->human_readable) {
1015 char scaled[FMT_SCALED_STRSIZE];
1017 if (fmt_scaled(size, scaled) == -1) {
1018 err = got_error_from_errno("fmt_scaled");
1022 while (isspace((unsigned char)*s))
1024 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1025 (long long)offset, s, delta_str ? delta_str : "");
1027 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1028 (long long)offset, (long long)size,
1029 delta_str ? delta_str : "");
1038 static const struct got_error *
1039 cmd_listpack(int argc, char *argv[])
1041 const struct got_error *error = NULL;
1042 struct got_repository *repo = NULL;
1044 struct got_object_id *pack_hash = NULL;
1045 char *packfile_path = NULL;
1046 char *id_str = NULL;
1047 struct gotadmin_list_pack_cb_args lpa;
1048 FILE *packfile = NULL;
1049 int show_stats = 0, human_readable = 0;
1050 int *pack_fds = NULL;
1053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1058 while ((ch = getopt(argc, argv, "hs")) != -1) {
1077 packfile_path = realpath(argv[0], NULL);
1078 if (packfile_path == NULL)
1079 return got_error_from_errno2("realpath", argv[0]);
1081 error = got_repo_pack_fds_open(&pack_fds);
1084 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1088 /* Remove "cpath" promise. */
1089 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1093 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1097 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1101 error = got_object_id_str(&id_str, pack_hash);
1105 memset(&lpa, 0, sizeof(lpa));
1106 lpa.human_readable = human_readable;
1107 error = got_repo_list_pack(packfile, pack_hash, repo,
1108 list_pack_cb, &lpa, check_cancelled, NULL);
1112 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1113 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1114 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1115 lpa.noffdeltas + lpa.nrefdeltas,
1116 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1117 lpa.noffdeltas, lpa.nrefdeltas);
1121 got_repo_close(repo);
1123 const struct got_error *pack_err =
1124 got_repo_pack_fds_close(pack_fds);
1130 free(packfile_path);
1137 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1142 struct got_cleanup_progress_arg {
1146 int last_nredundant;
1148 int printed_something;
1152 static const struct got_error *
1153 cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1156 struct got_cleanup_progress_arg *a = arg;
1157 int print_loose = 0, print_commits = 0, print_purged = 0;
1158 int print_redundant = 0;
1160 if (a->last_nloose != nloose) {
1162 a->last_nloose = nloose;
1164 if (a->last_ncommits != ncommits) {
1167 a->last_ncommits = ncommits;
1169 if (a->last_npurged != npurged) {
1173 a->last_npurged = npurged;
1175 if (a->last_nredundant != nredundant) {
1179 print_redundant = 1;
1180 a->last_nredundant = nredundant;
1183 if (a->verbosity < 0)
1186 if (print_loose || print_commits || print_purged || print_redundant)
1189 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1191 printf("; %d commit%s scanned", ncommits,
1192 ncommits == 1 ? "" : "s");
1193 if (print_purged || print_redundant) {
1195 printf("; could purge %d object%s", npurged,
1196 npurged == 1 ? "" : "s");
1198 printf("; purged %d object%s", npurged,
1199 npurged == 1 ? "" : "s");
1202 if (print_redundant) {
1204 printf(", %d pack file%s", nredundant,
1205 nredundant == 1 ? "" : "s");
1207 printf(", %d pack file%s", nredundant,
1208 nredundant == 1 ? "" : "s");
1211 if (print_loose || print_commits || print_purged || print_redundant) {
1212 a->printed_something = 1;
1218 struct got_lonely_packidx_progress_arg {
1220 int printed_something;
1224 static const struct got_error *
1225 lonely_packidx_progress(void *arg, const char *path)
1227 struct got_lonely_packidx_progress_arg *a = arg;
1229 if (a->verbosity < 0)
1233 printf("%s could be removed\n", path);
1235 printf("%s removed\n", path);
1237 a->printed_something = 1;
1241 static const struct got_error *
1242 cmd_cleanup(int argc, char *argv[])
1244 const struct got_error *error = NULL;
1245 char *repo_path = NULL;
1246 struct got_repository *repo = NULL;
1247 int ch, dry_run = 0, verbosity = 0;
1248 int ncommits = 0, nloose = 0, npacked = 0;
1249 int remove_lonely_packidx = 0, ignore_mtime = 0;
1250 struct got_lockfile *lock = NULL;
1251 struct got_cleanup_progress_arg cpa;
1252 struct got_lonely_packidx_progress_arg lpa;
1253 off_t loose_before, loose_after;
1254 off_t pack_before, pack_after;
1256 char loose_before_scaled[FMT_SCALED_STRSIZE];
1257 char loose_after_scaled[FMT_SCALED_STRSIZE];
1258 char pack_before_scaled[FMT_SCALED_STRSIZE];
1259 char pack_after_scaled[FMT_SCALED_STRSIZE];
1260 char total_size_scaled[FMT_SCALED_STRSIZE];
1261 int *pack_fds = NULL;
1264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1269 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1278 remove_lonely_packidx = 1;
1284 repo_path = realpath(optarg, NULL);
1285 if (repo_path == NULL)
1286 return got_error_from_errno2("realpath",
1288 got_path_strip_trailing_slashes(repo_path);
1299 if (repo_path == NULL) {
1300 error = get_repo_path(&repo_path);
1304 error = got_repo_pack_fds_open(&pack_fds);
1307 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1311 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1315 if (got_repo_has_extension(repo, "preciousObjects")) {
1316 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1317 "the preciousObjects Git extension is enabled; "
1318 "this implies that objects must not be deleted");
1322 error = got_repo_cleanup_prepare(repo, &lock);
1326 if (remove_lonely_packidx) {
1327 memset(&lpa, 0, sizeof(lpa));
1328 lpa.dry_run = dry_run;
1329 lpa.verbosity = verbosity;
1330 error = got_repo_remove_lonely_packidx(repo, dry_run,
1331 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1335 memset(&cpa, 0, sizeof(cpa));
1336 cpa.last_ncommits = -1;
1337 cpa.last_npurged = -1;
1338 cpa.last_nredundant = -1;
1339 cpa.dry_run = dry_run;
1340 cpa.verbosity = verbosity;
1342 error = got_repo_purge_unreferenced_loose_objects(repo,
1343 &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1344 dry_run, ignore_mtime, cleanup_progress, &cpa,
1345 check_cancelled, NULL);
1347 if (cpa.printed_something)
1352 error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1353 &pack_after, dry_run, ncommits, nloose, npacked,
1354 cleanup_progress, &cpa, check_cancelled, NULL);
1355 if (cpa.printed_something)
1360 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1362 if (cpa.printed_something) {
1363 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1364 error = got_error_from_errno("fmt_scaled");
1367 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1368 error = got_error_from_errno("fmt_scaled");
1371 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1372 error = got_error_from_errno("fmt_scaled");
1375 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1376 error = got_error_from_errno("fmt_scaled");
1379 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1380 error = got_error_from_errno("fmt_scaled");
1383 printf("loose total size before: %s\n", loose_before_scaled);
1384 printf("loose total size after: %s\n", loose_after_scaled);
1385 printf("pack files total size before: %s\n",
1386 pack_before_scaled);
1387 printf("pack files total size after: %s\n", pack_after_scaled);
1389 printf("disk space which would be freed: %s\n",
1392 printf("disk space freed: %s\n", total_size_scaled);
1393 printf("loose objects also found in pack files: %d\n", npacked);
1397 got_repo_cleanup_complete(repo, lock);
1399 got_repo_close(repo);
1401 const struct got_error *pack_err =
1402 got_repo_pack_fds_close(pack_fds);
1413 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1414 "[-x reference] [reference]...\n", getprogname());
1418 static const struct got_error *
1419 cmd_dump(int argc, char *argv[])
1421 const struct got_error *error = NULL;
1422 struct got_pack_progress_arg ppa;
1423 struct got_repository *repo = NULL;
1424 struct got_pathlist_head exclude_args;
1425 struct got_pathlist_entry *pe;
1426 struct got_reflist_head exclude_refs;
1427 struct got_reflist_head include_refs;
1428 struct got_reflist_entry *re, *new;
1429 const char *refname;
1430 char *repo_path = NULL;
1431 int *pack_fds = NULL;
1435 TAILQ_INIT(&exclude_args);
1436 TAILQ_INIT(&exclude_refs);
1437 TAILQ_INIT(&include_refs);
1440 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1445 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1451 repo_path = realpath(optarg, NULL);
1452 if (repo_path == NULL)
1453 return got_error_from_errno2("realpath",
1455 got_path_strip_trailing_slashes(repo_path);
1458 error = got_pathlist_append(&exclude_args,
1471 if (repo_path == NULL) {
1472 error = get_repo_path(&repo_path);
1476 error = got_repo_pack_fds_open(&pack_fds);
1479 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1483 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1487 TAILQ_FOREACH(pe, &exclude_args, entry) {
1489 error = add_ref(&new, &exclude_refs, refname, repo);
1495 error = got_ref_list(&include_refs, repo, "",
1496 got_ref_cmp_by_name, NULL);
1500 for (i = 0; i < argc; i++) {
1501 got_path_strip_trailing_slashes(argv[i]);
1503 error = add_ref(&new, &include_refs, refname, repo);
1509 /* Ignore references in the refs/got/ namespace. */
1510 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1511 refname = got_ref_get_name(re->ref);
1512 if (strncmp("refs/got/", refname, 9) != 0)
1514 TAILQ_REMOVE(&include_refs, re, entry);
1515 got_ref_close(re->ref);
1519 memset(&ppa, 0, sizeof(ppa));
1521 ppa.verbosity = verbosity;
1523 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1524 repo, pack_progress, &ppa, check_cancelled, NULL);
1525 if (ppa.printed_something)
1526 fprintf(stderr, "\n");
1529 got_repo_close(repo);
1532 const struct got_error *pack_err;
1534 pack_err = got_repo_pack_fds_close(pack_fds);
1539 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1540 got_ref_list_free(&exclude_refs);
1541 got_ref_list_free(&include_refs);