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 "got_compat.h"
19 #include <sys/queue.h>
20 #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_repository_dump.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);
89 __dead static void usage_dump(void);
91 static const struct got_error* cmd_init(int, char *[]);
92 static const struct got_error* cmd_info(int, char *[]);
93 static const struct got_error* cmd_pack(int, char *[]);
94 static const struct got_error* cmd_indexpack(int, char *[]);
95 static const struct got_error* cmd_listpack(int, char *[]);
96 static const struct got_error* cmd_cleanup(int, char *[]);
97 static const struct got_error* cmd_dump(int, char *[]);
99 static const struct gotadmin_cmd gotadmin_commands[] = {
100 { "init", cmd_init, usage_init, "" },
101 { "info", cmd_info, usage_info, "" },
102 { "pack", cmd_pack, usage_pack, "" },
103 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
104 { "listpack", cmd_listpack, usage_listpack, "ls" },
105 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
106 { "dump", cmd_dump, usage_dump, "" },
110 list_commands(FILE *fp)
114 fprintf(fp, "commands:");
115 for (i = 0; i < nitems(gotadmin_commands); i++) {
116 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
117 fprintf(fp, " %s", cmd->cmd_name);
123 main(int argc, char *argv[])
125 const struct gotadmin_cmd *cmd;
128 int hflag = 0, Vflag = 0;
129 static const struct option longopts[] = {
130 { "version", no_argument, NULL, 'V' },
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
156 got_version_print_str();
161 usage(hflag, hflag ? 0 : 1);
163 signal(SIGINT, catch_sigint);
164 signal(SIGPIPE, catch_sigpipe);
166 for (i = 0; i < nitems(gotadmin_commands); i++) {
167 const struct got_error *error;
169 cmd = &gotadmin_commands[i];
171 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
172 strcmp(cmd->cmd_alias, argv[0]) != 0)
178 error = cmd->cmd_main(argc, argv);
179 if (error && error->code != GOT_ERR_CANCELLED &&
180 error->code != GOT_ERR_PRIVSEP_EXIT &&
181 !(sigpipe_received &&
182 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
184 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
185 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
192 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
193 list_commands(stderr);
198 usage(int hflag, int status)
200 FILE *fp = (status == 0) ? stdout : stderr;
202 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
209 static const struct got_error *
210 apply_unveil(const char *repo_path, int repo_read_only)
212 const struct got_error *err;
215 if (unveil("gmon.out", "rwc") != 0)
216 return got_error_from_errno2("unveil", "gmon.out");
218 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
219 return got_error_from_errno2("unveil", repo_path);
221 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
222 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
224 err = got_privsep_unveil_exec_helpers();
228 if (unveil(NULL, NULL) != 0)
229 return got_error_from_errno("unveil");
237 fprintf(stderr, "usage: %s info [-r repository-path]\n",
242 static const struct got_error *
243 get_repo_path(char **repo_path)
245 const struct got_error *err = NULL;
246 struct got_worktree *worktree = NULL;
251 cwd = getcwd(NULL, 0);
253 return got_error_from_errno("getcwd");
255 err = got_worktree_open(&worktree, cwd);
257 if (err->code != GOT_ERR_NOT_WORKTREE)
263 *repo_path = strdup(got_worktree_get_repo_path(worktree));
265 *repo_path = strdup(cwd);
266 if (*repo_path == NULL)
267 err = got_error_from_errno("strdup");
270 got_worktree_close(worktree);
278 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
283 static const struct got_error *
284 cmd_init(int argc, char *argv[])
286 const struct got_error *error = NULL;
287 const char *head_name = NULL;
288 char *repo_path = NULL;
292 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
296 while ((ch = getopt(argc, argv, "b:")) != -1) {
313 repo_path = strdup(argv[0]);
314 if (repo_path == NULL)
315 return got_error_from_errno("strdup");
317 got_path_strip_trailing_slashes(repo_path);
319 error = got_path_mkdir(repo_path);
321 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
324 error = apply_unveil(repo_path, 0);
328 error = got_repo_init(repo_path, head_name);
334 static const struct got_error *
335 cmd_info(int argc, char *argv[])
337 const struct got_error *error = NULL;
338 char *repo_path = NULL;
339 struct got_repository *repo = NULL;
340 const struct got_gotconfig *gotconfig = NULL;
341 int ch, npackfiles, npackedobj, nobj;
342 off_t packsize, loose_size;
343 char scaled[FMT_SCALED_STRSIZE];
344 int *pack_fds = NULL;
347 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
352 while ((ch = getopt(argc, argv, "r:")) != -1) {
355 repo_path = realpath(optarg, NULL);
356 if (repo_path == NULL)
357 return got_error_from_errno2("realpath",
359 got_path_strip_trailing_slashes(repo_path);
370 if (repo_path == NULL) {
371 error = get_repo_path(&repo_path);
375 error = got_repo_pack_fds_open(&pack_fds);
378 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
382 /* Remove "cpath" promise. */
383 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
387 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
391 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
393 gotconfig = got_repo_get_gotconfig(repo);
395 const struct got_remote_repo *remotes;
397 if (got_gotconfig_get_author(gotconfig)) {
398 printf("default author: %s\n",
399 got_gotconfig_get_author(gotconfig));
401 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
402 for (i = 0; i < nremotes; i++) {
403 const char *fetch_url = remotes[i].fetch_url;
404 const char *send_url = remotes[i].send_url;
405 if (strcmp(fetch_url, send_url) == 0) {
406 printf("remote \"%s\": %s\n", remotes[i].name,
407 remotes[i].fetch_url);
409 printf("remote \"%s\" (fetch): %s\n",
410 remotes[i].name, remotes[i].fetch_url);
411 printf("remote \"%s\" (send): %s\n",
412 remotes[i].name, remotes[i].send_url);
417 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
421 printf("pack files: %d\n", npackfiles);
422 if (npackfiles > 0) {
423 if (fmt_scaled(packsize, scaled) == -1) {
424 error = got_error_from_errno("fmt_scaled");
427 printf("packed objects: %d\n", npackedobj);
428 printf("packed total size: %s\n", scaled);
431 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
434 printf("loose objects: %d\n", nobj);
436 if (fmt_scaled(loose_size, scaled) == -1) {
437 error = got_error_from_errno("fmt_scaled");
440 printf("loose total size: %s\n", scaled);
444 got_repo_close(repo);
446 const struct got_error *pack_err =
447 got_repo_pack_fds_close(pack_fds);
459 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
460 "[-x reference] [reference ...]\n", getprogname());
464 struct got_pack_progress_arg {
466 char last_scaled_size[FMT_SCALED_STRSIZE];
478 int printed_something;
482 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
483 int ncolored, int nfound, int ntrees)
486 fprintf(out, "%d commit%s colored", ncolored,
487 ncolored == 1 ? "" : "s");
490 fprintf(out, "%s%d object%s found",
491 ncolored > 0 ? "; " : "",
492 nfound, nfound == 1 ? "" : "s");
495 fprintf(out, "; %d tree%s scanned", ntrees,
496 ntrees == 1 ? "" : "s");
500 static const struct got_error *
501 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
502 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
505 struct got_pack_progress_arg *a = arg;
506 char scaled_size[FMT_SCALED_STRSIZE];
507 int p_deltify, p_written;
508 int print_colored = 0, print_found = 0, print_trees = 0;
509 int print_searching = 0, print_total = 0;
510 int print_deltify = 0, print_written = 0;
512 if (a->verbosity < 0)
515 if (a->last_ncolored != ncolored) {
517 a->last_ncolored = ncolored;
520 if (a->last_nfound != nfound) {
523 a->last_nfound = nfound;
526 if (a->last_ntrees != ntrees) {
530 a->last_ntrees = ntrees;
533 if ((print_colored || print_found || print_trees) &&
535 fprintf(a->out, "\r");
536 print_load_info(a->out, print_colored, print_found,
537 print_trees, ncolored, nfound, ntrees);
538 a->printed_something = 1;
541 } else if (!a->loading_done) {
542 fprintf(a->out, "\r");
543 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
544 fprintf(a->out, "\n");
548 if (fmt_scaled(packfile_size, scaled_size) == -1)
549 return got_error_from_errno("fmt_scaled");
551 if (a->last_ncommits != ncommits) {
553 a->last_ncommits = ncommits;
556 if (a->last_nobj_total != nobj_total) {
559 a->last_nobj_total = nobj_total;
562 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
563 strcmp(scaled_size, a->last_scaled_size)) != 0) {
564 if (strlcpy(a->last_scaled_size, scaled_size,
565 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
566 return got_error(GOT_ERR_NO_SPACE);
569 if (nobj_deltify > 0 || nobj_written > 0) {
570 if (nobj_deltify > 0) {
571 p_deltify = (nobj_deltify * 100) / nobj_total;
572 if (p_deltify != a->last_p_deltify) {
573 a->last_p_deltify = p_deltify;
579 if (nobj_written > 0) {
580 p_written = (nobj_written * 100) / nobj_total;
581 if (p_written != a->last_p_written) {
582 a->last_p_written = p_written;
591 if (print_searching || print_total || print_deltify || print_written)
592 fprintf(a->out, "\r");
594 fprintf(a->out, "packing %d reference%s", ncommits,
595 ncommits == 1 ? "" : "s");
597 fprintf(a->out, "; %d object%s", nobj_total,
598 nobj_total == 1 ? "" : "s");
600 fprintf(a->out, "; deltify: %d%%", p_deltify);
602 fprintf(a->out, "; writing pack: %*s %d%%",
603 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
604 if (print_searching || print_total || print_deltify ||
606 a->printed_something = 1;
612 static const struct got_error *
613 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
614 int nobj_indexed, int nobj_loose, int nobj_resolved)
616 struct got_pack_progress_arg *a = arg;
617 char scaled_size[FMT_SCALED_STRSIZE];
618 int p_indexed, p_resolved;
619 int print_size = 0, print_indexed = 0, print_resolved = 0;
621 if (a->verbosity < 0)
624 if (packfile_size > 0 || nobj_indexed > 0) {
625 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
626 (a->last_scaled_size[0] == '\0' ||
627 strcmp(scaled_size, a->last_scaled_size)) != 0) {
629 if (strlcpy(a->last_scaled_size, scaled_size,
630 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
631 return got_error(GOT_ERR_NO_SPACE);
633 if (nobj_indexed > 0) {
634 p_indexed = (nobj_indexed * 100) / nobj_total;
635 if (p_indexed != a->last_p_indexed) {
636 a->last_p_indexed = p_indexed;
641 if (nobj_resolved > 0) {
642 p_resolved = (nobj_resolved * 100) /
643 (nobj_total - nobj_loose);
644 if (p_resolved != a->last_p_resolved) {
645 a->last_p_resolved = p_resolved;
653 if (print_size || print_indexed || print_resolved)
656 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
658 printf("; indexing %d%%", p_indexed);
660 printf("; resolving deltas %d%%", p_resolved);
661 if (print_size || print_indexed || print_resolved)
667 static const struct got_error *
668 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
669 const char *refname, struct got_repository *repo)
671 const struct got_error *err;
672 struct got_reference *ref;
676 err = got_ref_open(&ref, repo, refname, 0);
678 if (err->code != GOT_ERR_NOT_REF)
681 /* Treat argument as a reference prefix. */
682 err = got_ref_list(refs, repo, refname,
683 got_ref_cmp_by_name, NULL);
685 err = got_reflist_insert(new, refs, ref,
686 got_ref_cmp_by_name, NULL);
687 if (err || *new == NULL /* duplicate */)
694 static const struct got_error *
695 cmd_pack(int argc, char *argv[])
697 const struct got_error *error = NULL;
698 char *repo_path = NULL;
699 struct got_repository *repo = NULL;
700 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
701 struct got_object_id *pack_hash = NULL;
703 struct got_pack_progress_arg ppa;
704 FILE *packfile = NULL;
705 struct got_pathlist_head exclude_args;
706 struct got_pathlist_entry *pe;
707 struct got_reflist_head exclude_refs;
708 struct got_reflist_head include_refs;
709 struct got_reflist_entry *re, *new;
710 int *pack_fds = NULL;
712 TAILQ_INIT(&exclude_args);
713 TAILQ_INIT(&exclude_refs);
714 TAILQ_INIT(&include_refs);
717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
722 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
734 repo_path = realpath(optarg, NULL);
735 if (repo_path == NULL)
736 return got_error_from_errno2("realpath",
738 got_path_strip_trailing_slashes(repo_path);
741 got_path_strip_trailing_slashes(optarg);
742 error = got_pathlist_append(&exclude_args,
756 if (repo_path == NULL) {
757 error = get_repo_path(&repo_path);
761 error = got_repo_pack_fds_open(&pack_fds);
764 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
768 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
772 TAILQ_FOREACH(pe, &exclude_args, entry) {
773 const char *refname = pe->path;
774 error = add_ref(&new, &exclude_refs, refname, repo);
780 error = got_ref_list(&include_refs, repo, "",
781 got_ref_cmp_by_name, NULL);
785 for (i = 0; i < argc; i++) {
787 got_path_strip_trailing_slashes(argv[i]);
789 error = add_ref(&new, &include_refs, refname, repo);
795 /* Ignore references in the refs/got/ namespace. */
796 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
797 const char *refname = got_ref_get_name(re->ref);
798 if (strncmp("refs/got/", refname, 9) != 0)
800 TAILQ_REMOVE(&include_refs, re, entry);
801 got_ref_close(re->ref);
805 memset(&ppa, 0, sizeof(ppa));
807 ppa.last_scaled_size[0] = '\0';
808 ppa.last_p_indexed = -1;
809 ppa.last_p_resolved = -1;
810 ppa.verbosity = verbosity;
812 error = got_repo_pack_objects(&packfile, &pack_hash,
813 &include_refs, &exclude_refs, repo, loose_obj_only,
814 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
816 if (ppa.printed_something)
821 error = got_object_id_str(&id_str, pack_hash);
825 printf("\nWrote %s.pack\n", id_str);
827 error = got_repo_index_pack(packfile, pack_hash, repo,
828 pack_index_progress, &ppa, check_cancelled, NULL);
832 printf("\nIndexed %s.pack\n", id_str);
835 got_repo_close(repo);
837 const struct got_error *pack_err =
838 got_repo_pack_fds_close(pack_fds);
842 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
843 got_ref_list_free(&exclude_refs);
844 got_ref_list_free(&include_refs);
852 usage_indexpack(void)
854 fprintf(stderr, "usage: %s indexpack packfile-path\n",
859 static const struct got_error *
860 cmd_indexpack(int argc, char *argv[])
862 const struct got_error *error = NULL;
863 struct got_repository *repo = NULL;
865 struct got_object_id *pack_hash = NULL;
866 char *packfile_path = NULL;
868 struct got_pack_progress_arg ppa;
869 FILE *packfile = NULL;
870 int *pack_fds = NULL;
872 while ((ch = getopt(argc, argv, "")) != -1) {
886 packfile_path = realpath(argv[0], NULL);
887 if (packfile_path == NULL)
888 return got_error_from_errno2("realpath", argv[0]);
891 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
896 error = got_repo_pack_fds_open(&pack_fds);
899 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
903 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
907 memset(&ppa, 0, sizeof(ppa));
909 ppa.last_scaled_size[0] = '\0';
910 ppa.last_p_indexed = -1;
911 ppa.last_p_resolved = -1;
913 error = got_repo_find_pack(&packfile, &pack_hash, repo,
918 error = got_object_id_str(&id_str, pack_hash);
922 error = got_repo_index_pack(packfile, pack_hash, repo,
923 pack_index_progress, &ppa, check_cancelled, NULL);
926 printf("\nIndexed %s.pack\n", id_str);
929 got_repo_close(repo);
931 const struct got_error *pack_err =
932 got_repo_pack_fds_close(pack_fds);
944 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
949 struct gotadmin_list_pack_cb_args {
959 static const struct got_error *
960 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
961 off_t size, off_t base_offset, struct got_object_id *base_id)
963 const struct got_error *err;
964 struct gotadmin_list_pack_cb_args *a = arg;
965 char *id_str, *delta_str = NULL, *base_id_str = NULL;
966 const char *type_str;
968 err = got_object_id_str(&id_str, id);
973 case GOT_OBJ_TYPE_BLOB:
974 type_str = GOT_OBJ_LABEL_BLOB;
977 case GOT_OBJ_TYPE_TREE:
978 type_str = GOT_OBJ_LABEL_TREE;
981 case GOT_OBJ_TYPE_COMMIT:
982 type_str = GOT_OBJ_LABEL_COMMIT;
985 case GOT_OBJ_TYPE_TAG:
986 type_str = GOT_OBJ_LABEL_TAG;
989 case GOT_OBJ_TYPE_OFFSET_DELTA:
990 type_str = "offset-delta";
991 if (asprintf(&delta_str, " base-offset %lld",
992 (long long)base_offset) == -1) {
993 err = got_error_from_errno("asprintf");
998 case GOT_OBJ_TYPE_REF_DELTA:
999 type_str = "ref-delta";
1000 err = got_object_id_str(&base_id_str, base_id);
1003 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1004 err = got_error_from_errno("asprintf");
1010 err = got_error(GOT_ERR_OBJ_TYPE);
1013 if (a->human_readable) {
1014 char scaled[FMT_SCALED_STRSIZE];
1016 if (fmt_scaled(size, scaled) == -1) {
1017 err = got_error_from_errno("fmt_scaled");
1021 while (isspace((unsigned char)*s))
1023 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1024 (long long)offset, s, delta_str ? delta_str : "");
1026 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1027 (long long)offset, (long long)size,
1028 delta_str ? delta_str : "");
1037 static const struct got_error *
1038 cmd_listpack(int argc, char *argv[])
1040 const struct got_error *error = NULL;
1041 struct got_repository *repo = NULL;
1043 struct got_object_id *pack_hash = NULL;
1044 char *packfile_path = NULL;
1045 char *id_str = NULL;
1046 struct gotadmin_list_pack_cb_args lpa;
1047 FILE *packfile = NULL;
1048 int show_stats = 0, human_readable = 0;
1049 int *pack_fds = NULL;
1052 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1057 while ((ch = getopt(argc, argv, "hs")) != -1) {
1076 packfile_path = realpath(argv[0], NULL);
1077 if (packfile_path == NULL)
1078 return got_error_from_errno2("realpath", argv[0]);
1080 error = got_repo_pack_fds_open(&pack_fds);
1083 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1087 /* Remove "cpath" promise. */
1088 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1092 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1096 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1100 error = got_object_id_str(&id_str, pack_hash);
1104 memset(&lpa, 0, sizeof(lpa));
1105 lpa.human_readable = human_readable;
1106 error = got_repo_list_pack(packfile, pack_hash, repo,
1107 list_pack_cb, &lpa, check_cancelled, NULL);
1111 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1112 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1113 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1114 lpa.noffdeltas + lpa.nrefdeltas,
1115 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1116 lpa.noffdeltas, lpa.nrefdeltas);
1120 got_repo_close(repo);
1122 const struct got_error *pack_err =
1123 got_repo_pack_fds_close(pack_fds);
1129 free(packfile_path);
1136 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1141 struct got_cleanup_progress_arg {
1145 int last_nredundant;
1147 int printed_something;
1151 static const struct got_error *
1152 cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1155 struct got_cleanup_progress_arg *a = arg;
1156 int print_loose = 0, print_commits = 0, print_purged = 0;
1157 int print_redundant = 0;
1159 if (a->last_nloose != nloose) {
1161 a->last_nloose = nloose;
1163 if (a->last_ncommits != ncommits) {
1166 a->last_ncommits = ncommits;
1168 if (a->last_npurged != npurged) {
1172 a->last_npurged = npurged;
1174 if (a->last_nredundant != nredundant) {
1178 print_redundant = 1;
1179 a->last_nredundant = nredundant;
1182 if (a->verbosity < 0)
1185 if (print_loose || print_commits || print_purged || print_redundant)
1188 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1190 printf("; %d commit%s scanned", ncommits,
1191 ncommits == 1 ? "" : "s");
1192 if (print_purged || print_redundant) {
1194 printf("; could purge %d object%s", npurged,
1195 npurged == 1 ? "" : "s");
1197 printf("; purged %d object%s", npurged,
1198 npurged == 1 ? "" : "s");
1201 if (print_redundant) {
1203 printf(", %d pack file%s", nredundant,
1204 nredundant == 1 ? "" : "s");
1206 printf(", %d pack file%s", nredundant,
1207 nredundant == 1 ? "" : "s");
1210 if (print_loose || print_commits || print_purged || print_redundant) {
1211 a->printed_something = 1;
1217 struct got_lonely_packidx_progress_arg {
1219 int printed_something;
1223 static const struct got_error *
1224 lonely_packidx_progress(void *arg, const char *path)
1226 struct got_lonely_packidx_progress_arg *a = arg;
1228 if (a->verbosity < 0)
1232 printf("%s could be removed\n", path);
1234 printf("%s removed\n", path);
1236 a->printed_something = 1;
1240 static const struct got_error *
1241 cmd_cleanup(int argc, char *argv[])
1243 const struct got_error *error = NULL;
1244 char *repo_path = NULL;
1245 struct got_repository *repo = NULL;
1246 int ch, dry_run = 0, verbosity = 0;
1247 int ncommits = 0, nloose = 0, npacked = 0;
1248 int remove_lonely_packidx = 0, ignore_mtime = 0;
1249 struct got_lockfile *lock = NULL;
1250 struct got_cleanup_progress_arg cpa;
1251 struct got_lonely_packidx_progress_arg lpa;
1252 off_t loose_before, loose_after;
1253 off_t pack_before, pack_after;
1255 char loose_before_scaled[FMT_SCALED_STRSIZE];
1256 char loose_after_scaled[FMT_SCALED_STRSIZE];
1257 char pack_before_scaled[FMT_SCALED_STRSIZE];
1258 char pack_after_scaled[FMT_SCALED_STRSIZE];
1259 char total_size_scaled[FMT_SCALED_STRSIZE];
1260 int *pack_fds = NULL;
1263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1268 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1277 remove_lonely_packidx = 1;
1283 repo_path = realpath(optarg, NULL);
1284 if (repo_path == NULL)
1285 return got_error_from_errno2("realpath",
1287 got_path_strip_trailing_slashes(repo_path);
1298 if (repo_path == NULL) {
1299 error = get_repo_path(&repo_path);
1303 error = got_repo_pack_fds_open(&pack_fds);
1306 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1310 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1314 if (got_repo_has_extension(repo, "preciousObjects")) {
1315 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1316 "the preciousObjects Git extension is enabled; "
1317 "this implies that objects must not be deleted");
1321 error = got_repo_cleanup_prepare(repo, &lock);
1325 if (remove_lonely_packidx) {
1326 memset(&lpa, 0, sizeof(lpa));
1327 lpa.dry_run = dry_run;
1328 lpa.verbosity = verbosity;
1329 error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1334 memset(&cpa, 0, sizeof(cpa));
1335 cpa.last_ncommits = -1;
1336 cpa.last_npurged = -1;
1337 cpa.last_nredundant = -1;
1338 cpa.dry_run = dry_run;
1339 cpa.verbosity = verbosity;
1341 error = got_repo_purge_unreferenced_loose_objects(repo,
1342 &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1343 dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 check_cancelled, NULL);
1346 if (cpa.printed_something)
1351 error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1352 &pack_after, dry_run, ncommits, nloose, npacked,
1353 cleanup_progress, &cpa, check_cancelled, NULL);
1354 if (cpa.printed_something)
1359 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1361 if (cpa.printed_something) {
1362 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1363 error = got_error_from_errno("fmt_scaled");
1366 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1367 error = got_error_from_errno("fmt_scaled");
1370 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1371 error = got_error_from_errno("fmt_scaled");
1374 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1375 error = got_error_from_errno("fmt_scaled");
1378 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1379 error = got_error_from_errno("fmt_scaled");
1382 printf("loose total size before: %s\n", loose_before_scaled);
1383 printf("loose total size after: %s\n", loose_after_scaled);
1384 printf("pack files total size before: %s\n",
1385 pack_before_scaled);
1386 printf("pack files total size after: %s\n", pack_after_scaled);
1388 printf("disk space which would be freed: %s\n",
1391 printf("disk space freed: %s\n", total_size_scaled);
1392 printf("loose objects also found in pack files: %d\n", npacked);
1396 got_repo_cleanup_complete(repo, lock);
1398 got_repo_close(repo);
1400 const struct got_error *pack_err =
1401 got_repo_pack_fds_close(pack_fds);
1412 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1413 "[-x reference] [reference]...\n", getprogname());
1417 static const struct got_error *
1418 cmd_dump(int argc, char *argv[])
1420 const struct got_error *error = NULL;
1421 struct got_pack_progress_arg ppa;
1422 struct got_repository *repo = NULL;
1423 struct got_pathlist_head exclude_args;
1424 struct got_pathlist_entry *pe;
1425 struct got_reflist_head exclude_refs;
1426 struct got_reflist_head include_refs;
1427 struct got_reflist_entry *re, *new;
1428 const char *refname;
1429 char *repo_path = NULL;
1430 int *pack_fds = NULL;
1434 TAILQ_INIT(&exclude_args);
1435 TAILQ_INIT(&exclude_refs);
1436 TAILQ_INIT(&include_refs);
1439 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1444 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1450 repo_path = realpath(optarg, NULL);
1451 if (repo_path == NULL)
1452 return got_error_from_errno2("realpath",
1454 got_path_strip_trailing_slashes(repo_path);
1457 error = got_pathlist_append(&exclude_args,
1470 if (repo_path == NULL) {
1471 error = get_repo_path(&repo_path);
1475 error = got_repo_pack_fds_open(&pack_fds);
1478 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1482 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1486 TAILQ_FOREACH(pe, &exclude_args, entry) {
1488 error = add_ref(&new, &exclude_refs, refname, repo);
1494 error = got_ref_list(&include_refs, repo, "",
1495 got_ref_cmp_by_name, NULL);
1499 for (i = 0; i < argc; i++) {
1500 got_path_strip_trailing_slashes(argv[i]);
1502 error = add_ref(&new, &include_refs, refname, repo);
1508 /* Ignore references in the refs/got/ namespace. */
1509 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1510 refname = got_ref_get_name(re->ref);
1511 if (strncmp("refs/got/", refname, 9) != 0)
1513 TAILQ_REMOVE(&include_refs, re, entry);
1514 got_ref_close(re->ref);
1518 memset(&ppa, 0, sizeof(ppa));
1520 ppa.verbosity = verbosity;
1522 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1523 repo, pack_progress, &ppa, check_cancelled, NULL);
1524 if (ppa.printed_something)
1525 fprintf(stderr, "\n");
1528 got_repo_close(repo);
1531 const struct got_error *pack_err;
1533 pack_err = got_repo_pack_fds_close(pack_fds);
1538 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1539 got_ref_list_free(&exclude_refs);
1540 got_ref_list_free(&include_refs);