Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
19 #include <ctype.h>
20 #include <getopt.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_compat.h"
33 #include "got_version.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_cancel.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_gotconfig.h"
41 #include "got_path.h"
42 #include "got_privsep.h"
43 #include "got_opentemp.h"
44 #include "got_worktree.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static void
60 catch_sigpipe(int signo)
61 {
62 sigpipe_received = 1;
63 }
65 static const struct got_error *
66 check_cancelled(void *arg)
67 {
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
70 return NULL;
71 }
73 struct gotadmin_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int, int);
81 __dead static void usage_info(void);
82 __dead static void usage_pack(void);
83 __dead static void usage_indexpack(void);
84 __dead static void usage_listpack(void);
85 __dead static void usage_cleanup(void);
87 static const struct got_error* cmd_info(int, char *[]);
88 static const struct got_error* cmd_pack(int, char *[]);
89 static const struct got_error* cmd_indexpack(int, char *[]);
90 static const struct got_error* cmd_listpack(int, char *[]);
91 static const struct got_error* cmd_cleanup(int, char *[]);
93 static const struct gotadmin_cmd gotadmin_commands[] = {
94 { "info", cmd_info, usage_info, "" },
95 { "pack", cmd_pack, usage_pack, "" },
96 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
97 { "listpack", cmd_listpack, usage_listpack, "ls" },
98 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
99 };
101 static void
102 list_commands(FILE *fp)
104 size_t i;
106 fprintf(fp, "commands:");
107 for (i = 0; i < nitems(gotadmin_commands); i++) {
108 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
109 fprintf(fp, " %s", cmd->cmd_name);
111 fputc('\n', fp);
114 int
115 main(int argc, char *argv[])
117 const struct gotadmin_cmd *cmd;
118 size_t i;
119 int ch;
120 int hflag = 0, Vflag = 0;
121 static const struct option longopts[] = {
122 { "version", no_argument, NULL, 'V' },
123 { NULL, 0, NULL, 0 }
124 };
126 setlocale(LC_CTYPE, "");
128 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
129 switch (ch) {
130 case 'h':
131 hflag = 1;
132 break;
133 case 'V':
134 Vflag = 1;
135 break;
136 default:
137 usage(hflag, 1);
138 /* NOTREACHED */
142 argc -= optind;
143 argv += optind;
144 optind = 1;
145 optreset = 1;
147 if (Vflag) {
148 got_version_print_str();
149 return 0;
152 if (argc <= 0)
153 usage(hflag, hflag ? 0 : 1);
155 signal(SIGINT, catch_sigint);
156 signal(SIGPIPE, catch_sigpipe);
158 for (i = 0; i < nitems(gotadmin_commands); i++) {
159 const struct got_error *error;
161 cmd = &gotadmin_commands[i];
163 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
164 strcmp(cmd->cmd_alias, argv[0]) != 0)
165 continue;
167 if (hflag)
168 cmd->cmd_usage();
170 error = cmd->cmd_main(argc, argv);
171 if (error && error->code != GOT_ERR_CANCELLED &&
172 error->code != GOT_ERR_PRIVSEP_EXIT &&
173 !(sigpipe_received &&
174 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
175 !(sigint_received &&
176 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
177 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
178 return 1;
181 return 0;
184 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 list_commands(stderr);
186 return 1;
189 __dead static void
190 usage(int hflag, int status)
192 FILE *fp = (status == 0) ? stdout : stderr;
194 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
195 getprogname());
196 if (hflag)
197 list_commands(fp);
198 exit(status);
201 static const struct got_error *
202 apply_unveil(const char *repo_path, int repo_read_only)
204 const struct got_error *err;
206 #ifdef PROFILE
207 if (unveil("gmon.out", "rwc") != 0)
208 return got_error_from_errno2("unveil", "gmon.out");
209 #endif
210 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
211 return got_error_from_errno2("unveil", repo_path);
213 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
214 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
216 err = got_privsep_unveil_exec_helpers();
217 if (err != NULL)
218 return err;
220 if (unveil(NULL, NULL) != 0)
221 return got_error_from_errno("unveil");
223 return NULL;
226 __dead static void
227 usage_info(void)
229 fprintf(stderr, "usage: %s info [-r repository-path]\n",
230 getprogname());
231 exit(1);
234 static const struct got_error *
235 get_repo_path(char **repo_path)
237 const struct got_error *err = NULL;
238 struct got_worktree *worktree = NULL;
239 char *cwd;
241 *repo_path = NULL;
243 cwd = getcwd(NULL, 0);
244 if (cwd == NULL)
245 return got_error_from_errno("getcwd");
247 err = got_worktree_open(&worktree, cwd);
248 if (err) {
249 if (err->code != GOT_ERR_NOT_WORKTREE)
250 goto done;
251 err = NULL;
254 if (worktree)
255 *repo_path = strdup(got_worktree_get_repo_path(worktree));
256 else
257 *repo_path = strdup(cwd);
258 if (*repo_path == NULL)
259 err = got_error_from_errno("strdup");
260 done:
261 if (worktree)
262 got_worktree_close(worktree);
263 free(cwd);
264 return err;
267 static const struct got_error *
268 cmd_info(int argc, char *argv[])
270 const struct got_error *error = NULL;
271 char *repo_path = NULL;
272 struct got_repository *repo = NULL;
273 const struct got_gotconfig *gotconfig = NULL;
274 int ch, npackfiles, npackedobj, nobj;
275 off_t packsize, loose_size;
276 char scaled[FMT_SCALED_STRSIZE];
278 while ((ch = getopt(argc, argv, "r:")) != -1) {
279 switch (ch) {
280 case 'r':
281 repo_path = realpath(optarg, NULL);
282 if (repo_path == NULL)
283 return got_error_from_errno2("realpath",
284 optarg);
285 got_path_strip_trailing_slashes(repo_path);
286 break;
287 default:
288 usage_info();
289 /* NOTREACHED */
293 argc -= optind;
294 argv += optind;
296 #ifndef PROFILE
297 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
298 NULL) == -1)
299 err(1, "pledge");
300 #endif
301 if (repo_path == NULL) {
302 error = get_repo_path(&repo_path);
303 if (error)
304 goto done;
306 error = got_repo_open(&repo, repo_path, NULL);
307 if (error)
308 goto done;
310 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
311 if (error)
312 goto done;
314 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
316 gotconfig = got_repo_get_gotconfig(repo);
317 if (gotconfig) {
318 const struct got_remote_repo *remotes;
319 int i, nremotes;
320 if (got_gotconfig_get_author(gotconfig)) {
321 printf("default author: %s\n",
322 got_gotconfig_get_author(gotconfig));
324 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
325 for (i = 0; i < nremotes; i++) {
326 const char *fetch_url = remotes[i].fetch_url;
327 const char *send_url = remotes[i].send_url;
328 if (strcmp(fetch_url, send_url) == 0) {
329 printf("remote \"%s\": %s\n", remotes[i].name,
330 remotes[i].fetch_url);
331 } else {
332 printf("remote \"%s\" (fetch): %s\n",
333 remotes[i].name, remotes[i].fetch_url);
334 printf("remote \"%s\" (send): %s\n",
335 remotes[i].name, remotes[i].send_url);
340 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
341 &packsize, repo);
342 if (error)
343 goto done;
344 printf("pack files: %d\n", npackfiles);
345 if (npackfiles > 0) {
346 if (fmt_scaled(packsize, scaled) == -1) {
347 error = got_error_from_errno("fmt_scaled");
348 goto done;
350 printf("packed objects: %d\n", npackedobj);
351 printf("packed total size: %s\n", scaled);
354 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
355 if (error)
356 goto done;
357 printf("loose objects: %d\n", nobj);
358 if (nobj > 0) {
359 if (fmt_scaled(loose_size, scaled) == -1) {
360 error = got_error_from_errno("fmt_scaled");
361 goto done;
363 printf("loose total size: %s\n", scaled);
365 done:
366 if (repo)
367 got_repo_close(repo);
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_pack(void)
375 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
376 "[-x reference] [-q] [reference ...]\n",
377 getprogname());
378 exit(1);
381 struct got_pack_progress_arg {
382 char last_scaled_size[FMT_SCALED_STRSIZE];
383 int last_ncolored;
384 int last_nfound;
385 int last_ntrees;
386 int loading_done;
387 int last_ncommits;
388 int last_nobj_total;
389 int last_p_deltify;
390 int last_p_written;
391 int last_p_indexed;
392 int last_p_resolved;
393 int verbosity;
394 int printed_something;
395 };
397 static void
398 print_load_info(int print_colored, int print_found, int print_trees,
399 int ncolored, int nfound, int ntrees)
401 if (print_colored) {
402 printf("%d commit%s colored", ncolored,
403 ncolored == 1 ? "" : "s");
405 if (print_found) {
406 printf("%s%d object%s found",
407 ncolored > 0 ? "; " : "",
408 nfound, nfound == 1 ? "" : "s");
410 if (print_trees) {
411 printf("; %d tree%s scanned", ntrees,
412 ntrees == 1 ? "" : "s");
416 static const struct got_error *
417 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
418 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
419 int nobj_written)
421 struct got_pack_progress_arg *a = arg;
422 char scaled_size[FMT_SCALED_STRSIZE];
423 int p_deltify, p_written;
424 int print_colored = 0, print_found = 0, print_trees = 0;
425 int print_searching = 0, print_total = 0;
426 int print_deltify = 0, print_written = 0;
428 if (a->verbosity < 0)
429 return NULL;
431 if (a->last_ncolored != ncolored) {
432 print_colored = 1;
433 a->last_ncolored = ncolored;
436 if (a->last_nfound != nfound) {
437 print_colored = 1;
438 print_found = 1;
439 a->last_nfound = nfound;
442 if (a->last_ntrees != ntrees) {
443 print_colored = 1;
444 print_found = 1;
445 print_trees = 1;
446 a->last_ntrees = ntrees;
449 if ((print_colored || print_found || print_trees) &&
450 !a->loading_done) {
451 printf("\r");
452 print_load_info(print_colored, print_found, print_trees,
453 ncolored, nfound, ntrees);
454 a->printed_something = 1;
455 fflush(stdout);
456 return NULL;
457 } else if (!a->loading_done) {
458 printf("\r");
459 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
460 printf("\n");
461 a->loading_done = 1;
464 if (fmt_scaled(packfile_size, scaled_size) == -1)
465 return got_error_from_errno("fmt_scaled");
467 if (a->last_ncommits != ncommits) {
468 print_searching = 1;
469 a->last_ncommits = ncommits;
472 if (a->last_nobj_total != nobj_total) {
473 print_searching = 1;
474 print_total = 1;
475 a->last_nobj_total = nobj_total;
478 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
479 strcmp(scaled_size, a->last_scaled_size)) != 0) {
480 if (strlcpy(a->last_scaled_size, scaled_size,
481 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
482 return got_error(GOT_ERR_NO_SPACE);
485 if (nobj_deltify > 0 || nobj_written > 0) {
486 if (nobj_deltify > 0) {
487 p_deltify = (nobj_deltify * 100) / nobj_total;
488 if (p_deltify != a->last_p_deltify) {
489 a->last_p_deltify = p_deltify;
490 print_searching = 1;
491 print_total = 1;
492 print_deltify = 1;
495 if (nobj_written > 0) {
496 p_written = (nobj_written * 100) / nobj_total;
497 if (p_written != a->last_p_written) {
498 a->last_p_written = p_written;
499 print_searching = 1;
500 print_total = 1;
501 print_deltify = 1;
502 print_written = 1;
507 if (print_searching || print_total || print_deltify || print_written)
508 printf("\r");
509 if (print_searching)
510 printf("packing %d reference%s", ncommits,
511 ncommits == 1 ? "" : "s");
512 if (print_total)
513 printf("; %d object%s", nobj_total,
514 nobj_total == 1 ? "" : "s");
515 if (print_deltify)
516 printf("; deltify: %d%%", p_deltify);
517 if (print_written)
518 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
519 scaled_size, p_written);
520 if (print_searching || print_total || print_deltify ||
521 print_written) {
522 a->printed_something = 1;
523 fflush(stdout);
525 return NULL;
528 static const struct got_error *
529 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
530 int nobj_indexed, int nobj_loose, int nobj_resolved)
532 struct got_pack_progress_arg *a = arg;
533 char scaled_size[FMT_SCALED_STRSIZE];
534 int p_indexed, p_resolved;
535 int print_size = 0, print_indexed = 0, print_resolved = 0;
537 if (a->verbosity < 0)
538 return NULL;
540 if (packfile_size > 0 || nobj_indexed > 0) {
541 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
542 (a->last_scaled_size[0] == '\0' ||
543 strcmp(scaled_size, a->last_scaled_size)) != 0) {
544 print_size = 1;
545 if (strlcpy(a->last_scaled_size, scaled_size,
546 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
547 return got_error(GOT_ERR_NO_SPACE);
549 if (nobj_indexed > 0) {
550 p_indexed = (nobj_indexed * 100) / nobj_total;
551 if (p_indexed != a->last_p_indexed) {
552 a->last_p_indexed = p_indexed;
553 print_indexed = 1;
554 print_size = 1;
557 if (nobj_resolved > 0) {
558 p_resolved = (nobj_resolved * 100) /
559 (nobj_total - nobj_loose);
560 if (p_resolved != a->last_p_resolved) {
561 a->last_p_resolved = p_resolved;
562 print_resolved = 1;
563 print_indexed = 1;
564 print_size = 1;
569 if (print_size || print_indexed || print_resolved)
570 printf("\r");
571 if (print_size)
572 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
573 if (print_indexed)
574 printf("; indexing %d%%", p_indexed);
575 if (print_resolved)
576 printf("; resolving deltas %d%%", p_resolved);
577 if (print_size || print_indexed || print_resolved)
578 fflush(stdout);
580 return NULL;
583 static const struct got_error *
584 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
585 const char *refname, struct got_repository *repo)
587 const struct got_error *err;
588 struct got_reference *ref;
590 *new = NULL;
592 err = got_ref_open(&ref, repo, refname, 0);
593 if (err) {
594 if (err->code != GOT_ERR_NOT_REF)
595 return err;
597 /* Treat argument as a reference prefix. */
598 err = got_ref_list(refs, repo, refname,
599 got_ref_cmp_by_name, NULL);
600 } else {
601 err = got_reflist_insert(new, refs, ref,
602 got_ref_cmp_by_name, NULL);
603 if (err || *new == NULL /* duplicate */)
604 got_ref_close(ref);
607 return err;
610 static const struct got_error *
611 cmd_pack(int argc, char *argv[])
613 const struct got_error *error = NULL;
614 char *repo_path = NULL;
615 struct got_repository *repo = NULL;
616 int ch, i, loose_obj_only = 1, verbosity = 0;
617 struct got_object_id *pack_hash = NULL;
618 char *id_str = NULL;
619 struct got_pack_progress_arg ppa;
620 FILE *packfile = NULL;
621 struct got_pathlist_head exclude_args;
622 struct got_pathlist_entry *pe;
623 struct got_reflist_head exclude_refs;
624 struct got_reflist_head include_refs;
625 struct got_reflist_entry *re, *new;
627 TAILQ_INIT(&exclude_args);
628 TAILQ_INIT(&exclude_refs);
629 TAILQ_INIT(&include_refs);
631 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
632 switch (ch) {
633 case 'a':
634 loose_obj_only = 0;
635 break;
636 case 'r':
637 repo_path = realpath(optarg, NULL);
638 if (repo_path == NULL)
639 return got_error_from_errno2("realpath",
640 optarg);
641 got_path_strip_trailing_slashes(repo_path);
642 break;
643 case 'x':
644 got_path_strip_trailing_slashes(optarg);
645 error = got_pathlist_append(&exclude_args,
646 optarg, NULL);
647 if (error)
648 return error;
649 break;
650 case 'q':
651 verbosity = -1;
652 break;
653 default:
654 usage_pack();
655 /* NOTREACHED */
659 argc -= optind;
660 argv += optind;
662 #ifndef PROFILE
663 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
664 NULL) == -1)
665 err(1, "pledge");
666 #endif
667 if (repo_path == NULL) {
668 error = get_repo_path(&repo_path);
669 if (error)
670 goto done;
672 error = got_repo_open(&repo, repo_path, NULL);
673 if (error)
674 goto done;
676 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
677 if (error)
678 goto done;
680 TAILQ_FOREACH(pe, &exclude_args, entry) {
681 const char *refname = pe->path;
682 error = add_ref(&new, &exclude_refs, refname, repo);
683 if (error)
684 goto done;
688 if (argc == 0) {
689 error = got_ref_list(&include_refs, repo, "",
690 got_ref_cmp_by_name, NULL);
691 if (error)
692 goto done;
693 } else {
694 for (i = 0; i < argc; i++) {
695 const char *refname;
696 got_path_strip_trailing_slashes(argv[i]);
697 refname = argv[i];
698 error = add_ref(&new, &include_refs, refname, repo);
699 if (error)
700 goto done;
704 /* Ignore references in the refs/got/ namespace. */
705 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
706 const char *refname = got_ref_get_name(re->ref);
707 if (strncmp("refs/got/", refname, 9) != 0)
708 continue;
709 TAILQ_REMOVE(&include_refs, re, entry);
710 got_ref_close(re->ref);
711 free(re);
714 memset(&ppa, 0, sizeof(ppa));
715 ppa.last_scaled_size[0] = '\0';
716 ppa.last_p_indexed = -1;
717 ppa.last_p_resolved = -1;
718 ppa.verbosity = verbosity;
720 error = got_repo_pack_objects(&packfile, &pack_hash,
721 &include_refs, &exclude_refs, repo, loose_obj_only,
722 pack_progress, &ppa, check_cancelled, NULL);
723 if (error) {
724 if (ppa.printed_something)
725 printf("\n");
726 goto done;
729 error = got_object_id_str(&id_str, pack_hash);
730 if (error)
731 goto done;
732 if (verbosity >= 0)
733 printf("\nWrote %s.pack\n", id_str);
735 error = got_repo_index_pack(packfile, pack_hash, repo,
736 pack_index_progress, &ppa, check_cancelled, NULL);
737 if (error)
738 goto done;
739 if (verbosity >= 0)
740 printf("\nIndexed %s.pack\n", id_str);
741 done:
742 if (repo)
743 got_repo_close(repo);
744 got_pathlist_free(&exclude_args);
745 got_ref_list_free(&exclude_refs);
746 got_ref_list_free(&include_refs);
747 free(id_str);
748 free(pack_hash);
749 free(repo_path);
750 return error;
753 __dead static void
754 usage_indexpack(void)
756 fprintf(stderr, "usage: %s indexpack packfile-path\n",
757 getprogname());
758 exit(1);
761 static const struct got_error *
762 cmd_indexpack(int argc, char *argv[])
764 const struct got_error *error = NULL;
765 struct got_repository *repo = NULL;
766 int ch;
767 struct got_object_id *pack_hash = NULL;
768 char *packfile_path = NULL;
769 char *id_str = NULL;
770 struct got_pack_progress_arg ppa;
771 FILE *packfile = NULL;
773 while ((ch = getopt(argc, argv, "")) != -1) {
774 switch (ch) {
775 default:
776 usage_indexpack();
777 /* NOTREACHED */
781 argc -= optind;
782 argv += optind;
784 if (argc != 1)
785 usage_indexpack();
787 packfile_path = realpath(argv[0], NULL);
788 if (packfile_path == NULL)
789 return got_error_from_errno2("realpath", argv[0]);
791 #ifndef PROFILE
792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
793 NULL) == -1)
794 err(1, "pledge");
795 #endif
797 error = got_repo_open(&repo, packfile_path, NULL);
798 if (error)
799 goto done;
801 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
802 if (error)
803 goto done;
805 memset(&ppa, 0, sizeof(ppa));
806 ppa.last_scaled_size[0] = '\0';
807 ppa.last_p_indexed = -1;
808 ppa.last_p_resolved = -1;
810 error = got_repo_find_pack(&packfile, &pack_hash, repo,
811 packfile_path);
812 if (error)
813 goto done;
815 error = got_object_id_str(&id_str, pack_hash);
816 if (error)
817 goto done;
819 error = got_repo_index_pack(packfile, pack_hash, repo,
820 pack_index_progress, &ppa, check_cancelled, NULL);
821 if (error)
822 goto done;
823 printf("\nIndexed %s.pack\n", id_str);
824 done:
825 if (repo)
826 got_repo_close(repo);
827 free(id_str);
828 free(pack_hash);
829 return error;
832 __dead static void
833 usage_listpack(void)
835 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
836 getprogname());
837 exit(1);
840 struct gotadmin_list_pack_cb_args {
841 int nblobs;
842 int ntrees;
843 int ncommits;
844 int ntags;
845 int noffdeltas;
846 int nrefdeltas;
847 int human_readable;
848 };
850 static const struct got_error *
851 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
852 off_t size, off_t base_offset, struct got_object_id *base_id)
854 const struct got_error *err;
855 struct gotadmin_list_pack_cb_args *a = arg;
856 char *id_str, *delta_str = NULL, *base_id_str = NULL;
857 const char *type_str;
859 err = got_object_id_str(&id_str, id);
860 if (err)
861 return err;
863 switch (type) {
864 case GOT_OBJ_TYPE_BLOB:
865 type_str = GOT_OBJ_LABEL_BLOB;
866 a->nblobs++;
867 break;
868 case GOT_OBJ_TYPE_TREE:
869 type_str = GOT_OBJ_LABEL_TREE;
870 a->ntrees++;
871 break;
872 case GOT_OBJ_TYPE_COMMIT:
873 type_str = GOT_OBJ_LABEL_COMMIT;
874 a->ncommits++;
875 break;
876 case GOT_OBJ_TYPE_TAG:
877 type_str = GOT_OBJ_LABEL_TAG;
878 a->ntags++;
879 break;
880 case GOT_OBJ_TYPE_OFFSET_DELTA:
881 type_str = "offset-delta";
882 if (asprintf(&delta_str, " base-offset %lld",
883 (long long)base_offset) == -1) {
884 err = got_error_from_errno("asprintf");
885 goto done;
887 a->noffdeltas++;
888 break;
889 case GOT_OBJ_TYPE_REF_DELTA:
890 type_str = "ref-delta";
891 err = got_object_id_str(&base_id_str, base_id);
892 if (err)
893 goto done;
894 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
895 err = got_error_from_errno("asprintf");
896 goto done;
898 a->nrefdeltas++;
899 break;
900 default:
901 err = got_error(GOT_ERR_OBJ_TYPE);
902 goto done;
904 if (a->human_readable) {
905 char scaled[FMT_SCALED_STRSIZE];
906 char *s;;
907 if (fmt_scaled(size, scaled) == -1) {
908 err = got_error_from_errno("fmt_scaled");
909 goto done;
911 s = scaled;
912 while (isspace((unsigned char)*s))
913 s++;
914 printf("%s %s at %lld size %s%s\n", id_str, type_str,
915 (long long)offset, s, delta_str ? delta_str : "");
916 } else {
917 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
918 (long long)offset, (long long)size,
919 delta_str ? delta_str : "");
921 done:
922 free(id_str);
923 free(base_id_str);
924 free(delta_str);
925 return err;
928 static const struct got_error *
929 cmd_listpack(int argc, char *argv[])
931 const struct got_error *error = NULL;
932 struct got_repository *repo = NULL;
933 int ch;
934 struct got_object_id *pack_hash = NULL;
935 char *packfile_path = NULL;
936 char *id_str = NULL;
937 struct gotadmin_list_pack_cb_args lpa;
938 FILE *packfile = NULL;
939 int show_stats = 0, human_readable = 0;
941 while ((ch = getopt(argc, argv, "hs")) != -1) {
942 switch (ch) {
943 case 'h':
944 human_readable = 1;
945 break;
946 case 's':
947 show_stats = 1;
948 break;
949 default:
950 usage_listpack();
951 /* NOTREACHED */
955 argc -= optind;
956 argv += optind;
958 if (argc != 1)
959 usage_listpack();
960 packfile_path = realpath(argv[0], NULL);
961 if (packfile_path == NULL)
962 return got_error_from_errno2("realpath", argv[0]);
964 #ifndef PROFILE
965 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
966 NULL) == -1)
967 err(1, "pledge");
968 #endif
969 error = got_repo_open(&repo, packfile_path, NULL);
970 if (error)
971 goto done;
973 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
974 if (error)
975 goto done;
977 error = got_repo_find_pack(&packfile, &pack_hash, repo,
978 packfile_path);
979 if (error)
980 goto done;
981 error = got_object_id_str(&id_str, pack_hash);
982 if (error)
983 goto done;
985 memset(&lpa, 0, sizeof(lpa));
986 lpa.human_readable = human_readable;
987 error = got_repo_list_pack(packfile, pack_hash, repo,
988 list_pack_cb, &lpa, check_cancelled, NULL);
989 if (error)
990 goto done;
991 if (show_stats) {
992 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
993 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
994 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
995 lpa.noffdeltas + lpa.nrefdeltas,
996 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
997 lpa.noffdeltas, lpa.nrefdeltas);
999 done:
1000 if (repo)
1001 got_repo_close(repo);
1002 free(id_str);
1003 free(pack_hash);
1004 free(packfile_path);
1005 return error;
1008 __dead static void
1009 usage_cleanup(void)
1011 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1012 "[-q]\n", getprogname());
1013 exit(1);
1016 struct got_cleanup_progress_arg {
1017 int last_nloose;
1018 int last_ncommits;
1019 int last_npurged;
1020 int verbosity;
1021 int printed_something;
1022 int dry_run;
1025 static const struct got_error *
1026 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1028 struct got_cleanup_progress_arg *a = arg;
1029 int print_loose = 0, print_commits = 0, print_purged = 0;
1031 if (a->last_nloose != nloose) {
1032 print_loose = 1;
1033 a->last_nloose = nloose;
1035 if (a->last_ncommits != ncommits) {
1036 print_loose = 1;
1037 print_commits = 1;
1038 a->last_ncommits = ncommits;
1040 if (a->last_npurged != npurged) {
1041 print_loose = 1;
1042 print_commits = 1;
1043 print_purged = 1;
1044 a->last_npurged = npurged;
1047 if (a->verbosity < 0)
1048 return NULL;
1050 if (print_loose || print_commits || print_purged)
1051 printf("\r");
1052 if (print_loose)
1053 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1054 if (print_commits)
1055 printf("; %d commit%s scanned", ncommits,
1056 ncommits == 1 ? "" : "s");
1057 if (print_purged) {
1058 if (a->dry_run) {
1059 printf("; %d object%s could be purged", npurged,
1060 npurged == 1 ? "" : "s");
1061 } else {
1062 printf("; %d object%s purged", npurged,
1063 npurged == 1 ? "" : "s");
1066 if (print_loose || print_commits || print_purged) {
1067 a->printed_something = 1;
1068 fflush(stdout);
1070 return NULL;
1073 struct got_lonely_packidx_progress_arg {
1074 int verbosity;
1075 int printed_something;
1076 int dry_run;
1079 static const struct got_error *
1080 lonely_packidx_progress(void *arg, const char *path)
1082 struct got_lonely_packidx_progress_arg *a = arg;
1084 if (a->verbosity < 0)
1085 return NULL;
1087 if (a->dry_run)
1088 printf("%s could be removed\n", path);
1089 else
1090 printf("%s removed\n", path);
1092 a->printed_something = 1;
1093 return NULL;
1096 static const struct got_error *
1097 cmd_cleanup(int argc, char *argv[])
1099 const struct got_error *error = NULL;
1100 char *repo_path = NULL;
1101 struct got_repository *repo = NULL;
1102 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1103 int remove_lonely_packidx = 0, ignore_mtime = 0;
1104 struct got_cleanup_progress_arg cpa;
1105 struct got_lonely_packidx_progress_arg lpa;
1106 off_t size_before, size_after;
1107 char scaled_before[FMT_SCALED_STRSIZE];
1108 char scaled_after[FMT_SCALED_STRSIZE];
1109 char scaled_diff[FMT_SCALED_STRSIZE];
1110 char **extensions;
1111 int nextensions, i;
1113 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1114 switch (ch) {
1115 case 'a':
1116 ignore_mtime = 1;
1117 break;
1118 case 'p':
1119 remove_lonely_packidx = 1;
1120 break;
1121 case 'r':
1122 repo_path = realpath(optarg, NULL);
1123 if (repo_path == NULL)
1124 return got_error_from_errno2("realpath",
1125 optarg);
1126 got_path_strip_trailing_slashes(repo_path);
1127 break;
1128 case 'n':
1129 dry_run = 1;
1130 break;
1131 case 'q':
1132 verbosity = -1;
1133 break;
1134 default:
1135 usage_cleanup();
1136 /* NOTREACHED */
1140 argc -= optind;
1141 argv += optind;
1143 #ifndef PROFILE
1144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1145 NULL) == -1)
1146 err(1, "pledge");
1147 #endif
1148 if (repo_path == NULL) {
1149 error = get_repo_path(&repo_path);
1150 if (error)
1151 goto done;
1153 error = got_repo_open(&repo, repo_path, NULL);
1154 if (error)
1155 goto done;
1157 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1158 if (error)
1159 goto done;
1161 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1162 repo);
1163 for (i = 0; i < nextensions; i++) {
1164 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1165 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1166 "the preciousObjects Git extension is enabled; "
1167 "this implies that objects must not be deleted");
1168 goto done;
1172 if (remove_lonely_packidx) {
1173 memset(&lpa, 0, sizeof(lpa));
1174 lpa.dry_run = dry_run;
1175 lpa.verbosity = verbosity;
1176 error = got_repo_remove_lonely_packidx(repo, dry_run,
1177 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1178 goto done;
1181 memset(&cpa, 0, sizeof(cpa));
1182 cpa.last_ncommits = -1;
1183 cpa.last_npurged = -1;
1184 cpa.dry_run = dry_run;
1185 cpa.verbosity = verbosity;
1186 error = got_repo_purge_unreferenced_loose_objects(repo,
1187 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1188 cleanup_progress, &cpa, check_cancelled, NULL);
1189 if (cpa.printed_something)
1190 printf("\n");
1191 if (error)
1192 goto done;
1193 if (cpa.printed_something) {
1194 if (fmt_scaled(size_before, scaled_before) == -1) {
1195 error = got_error_from_errno("fmt_scaled");
1196 goto done;
1198 if (fmt_scaled(size_after, scaled_after) == -1) {
1199 error = got_error_from_errno("fmt_scaled");
1200 goto done;
1202 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1203 error = got_error_from_errno("fmt_scaled");
1204 goto done;
1206 printf("loose total size before: %s\n", scaled_before);
1207 printf("loose total size after: %s\n", scaled_after);
1208 if (dry_run) {
1209 printf("disk space which would be freed: %s\n",
1210 scaled_diff);
1211 } else
1212 printf("disk space freed: %s\n", scaled_diff);
1213 printf("loose objects also found in pack files: %d\n", npacked);
1215 done:
1216 if (repo)
1217 got_repo_close(repo);
1218 free(repo_path);
1219 return error;