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 cpath 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;
309 #ifndef PROFILE
310 /* Remove "cpath" promise. */
311 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
312 NULL) == -1)
313 err(1, "pledge");
314 #endif
315 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
316 if (error)
317 goto done;
319 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
321 gotconfig = got_repo_get_gotconfig(repo);
322 if (gotconfig) {
323 const struct got_remote_repo *remotes;
324 int i, nremotes;
325 if (got_gotconfig_get_author(gotconfig)) {
326 printf("default author: %s\n",
327 got_gotconfig_get_author(gotconfig));
329 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
330 for (i = 0; i < nremotes; i++) {
331 const char *fetch_url = remotes[i].fetch_url;
332 const char *send_url = remotes[i].send_url;
333 if (strcmp(fetch_url, send_url) == 0) {
334 printf("remote \"%s\": %s\n", remotes[i].name,
335 remotes[i].fetch_url);
336 } else {
337 printf("remote \"%s\" (fetch): %s\n",
338 remotes[i].name, remotes[i].fetch_url);
339 printf("remote \"%s\" (send): %s\n",
340 remotes[i].name, remotes[i].send_url);
345 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
346 &packsize, repo);
347 if (error)
348 goto done;
349 printf("pack files: %d\n", npackfiles);
350 if (npackfiles > 0) {
351 if (fmt_scaled(packsize, scaled) == -1) {
352 error = got_error_from_errno("fmt_scaled");
353 goto done;
355 printf("packed objects: %d\n", npackedobj);
356 printf("packed total size: %s\n", scaled);
359 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
360 if (error)
361 goto done;
362 printf("loose objects: %d\n", nobj);
363 if (nobj > 0) {
364 if (fmt_scaled(loose_size, scaled) == -1) {
365 error = got_error_from_errno("fmt_scaled");
366 goto done;
368 printf("loose total size: %s\n", scaled);
370 done:
371 if (repo)
372 got_repo_close(repo);
373 free(repo_path);
374 return error;
377 __dead static void
378 usage_pack(void)
380 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
381 "[-x reference] [-q] [reference ...]\n",
382 getprogname());
383 exit(1);
386 struct got_pack_progress_arg {
387 char last_scaled_size[FMT_SCALED_STRSIZE];
388 int last_ncolored;
389 int last_nfound;
390 int last_ntrees;
391 int loading_done;
392 int last_ncommits;
393 int last_nobj_total;
394 int last_p_deltify;
395 int last_p_written;
396 int last_p_indexed;
397 int last_p_resolved;
398 int verbosity;
399 int printed_something;
400 };
402 static void
403 print_load_info(int print_colored, int print_found, int print_trees,
404 int ncolored, int nfound, int ntrees)
406 if (print_colored) {
407 printf("%d commit%s colored", ncolored,
408 ncolored == 1 ? "" : "s");
410 if (print_found) {
411 printf("%s%d object%s found",
412 ncolored > 0 ? "; " : "",
413 nfound, nfound == 1 ? "" : "s");
415 if (print_trees) {
416 printf("; %d tree%s scanned", ntrees,
417 ntrees == 1 ? "" : "s");
421 static const struct got_error *
422 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
423 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
424 int nobj_written)
426 struct got_pack_progress_arg *a = arg;
427 char scaled_size[FMT_SCALED_STRSIZE];
428 int p_deltify, p_written;
429 int print_colored = 0, print_found = 0, print_trees = 0;
430 int print_searching = 0, print_total = 0;
431 int print_deltify = 0, print_written = 0;
433 if (a->verbosity < 0)
434 return NULL;
436 if (a->last_ncolored != ncolored) {
437 print_colored = 1;
438 a->last_ncolored = ncolored;
441 if (a->last_nfound != nfound) {
442 print_colored = 1;
443 print_found = 1;
444 a->last_nfound = nfound;
447 if (a->last_ntrees != ntrees) {
448 print_colored = 1;
449 print_found = 1;
450 print_trees = 1;
451 a->last_ntrees = ntrees;
454 if ((print_colored || print_found || print_trees) &&
455 !a->loading_done) {
456 printf("\r");
457 print_load_info(print_colored, print_found, print_trees,
458 ncolored, nfound, ntrees);
459 a->printed_something = 1;
460 fflush(stdout);
461 return NULL;
462 } else if (!a->loading_done) {
463 printf("\r");
464 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
465 printf("\n");
466 a->loading_done = 1;
469 if (fmt_scaled(packfile_size, scaled_size) == -1)
470 return got_error_from_errno("fmt_scaled");
472 if (a->last_ncommits != ncommits) {
473 print_searching = 1;
474 a->last_ncommits = ncommits;
477 if (a->last_nobj_total != nobj_total) {
478 print_searching = 1;
479 print_total = 1;
480 a->last_nobj_total = nobj_total;
483 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
484 strcmp(scaled_size, a->last_scaled_size)) != 0) {
485 if (strlcpy(a->last_scaled_size, scaled_size,
486 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
487 return got_error(GOT_ERR_NO_SPACE);
490 if (nobj_deltify > 0 || nobj_written > 0) {
491 if (nobj_deltify > 0) {
492 p_deltify = (nobj_deltify * 100) / nobj_total;
493 if (p_deltify != a->last_p_deltify) {
494 a->last_p_deltify = p_deltify;
495 print_searching = 1;
496 print_total = 1;
497 print_deltify = 1;
500 if (nobj_written > 0) {
501 p_written = (nobj_written * 100) / nobj_total;
502 if (p_written != a->last_p_written) {
503 a->last_p_written = p_written;
504 print_searching = 1;
505 print_total = 1;
506 print_deltify = 1;
507 print_written = 1;
512 if (print_searching || print_total || print_deltify || print_written)
513 printf("\r");
514 if (print_searching)
515 printf("packing %d reference%s", ncommits,
516 ncommits == 1 ? "" : "s");
517 if (print_total)
518 printf("; %d object%s", nobj_total,
519 nobj_total == 1 ? "" : "s");
520 if (print_deltify)
521 printf("; deltify: %d%%", p_deltify);
522 if (print_written)
523 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
524 scaled_size, p_written);
525 if (print_searching || print_total || print_deltify ||
526 print_written) {
527 a->printed_something = 1;
528 fflush(stdout);
530 return NULL;
533 static const struct got_error *
534 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
535 int nobj_indexed, int nobj_loose, int nobj_resolved)
537 struct got_pack_progress_arg *a = arg;
538 char scaled_size[FMT_SCALED_STRSIZE];
539 int p_indexed, p_resolved;
540 int print_size = 0, print_indexed = 0, print_resolved = 0;
542 if (a->verbosity < 0)
543 return NULL;
545 if (packfile_size > 0 || nobj_indexed > 0) {
546 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
547 (a->last_scaled_size[0] == '\0' ||
548 strcmp(scaled_size, a->last_scaled_size)) != 0) {
549 print_size = 1;
550 if (strlcpy(a->last_scaled_size, scaled_size,
551 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
552 return got_error(GOT_ERR_NO_SPACE);
554 if (nobj_indexed > 0) {
555 p_indexed = (nobj_indexed * 100) / nobj_total;
556 if (p_indexed != a->last_p_indexed) {
557 a->last_p_indexed = p_indexed;
558 print_indexed = 1;
559 print_size = 1;
562 if (nobj_resolved > 0) {
563 p_resolved = (nobj_resolved * 100) /
564 (nobj_total - nobj_loose);
565 if (p_resolved != a->last_p_resolved) {
566 a->last_p_resolved = p_resolved;
567 print_resolved = 1;
568 print_indexed = 1;
569 print_size = 1;
574 if (print_size || print_indexed || print_resolved)
575 printf("\r");
576 if (print_size)
577 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
578 if (print_indexed)
579 printf("; indexing %d%%", p_indexed);
580 if (print_resolved)
581 printf("; resolving deltas %d%%", p_resolved);
582 if (print_size || print_indexed || print_resolved)
583 fflush(stdout);
585 return NULL;
588 static const struct got_error *
589 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
590 const char *refname, struct got_repository *repo)
592 const struct got_error *err;
593 struct got_reference *ref;
595 *new = NULL;
597 err = got_ref_open(&ref, repo, refname, 0);
598 if (err) {
599 if (err->code != GOT_ERR_NOT_REF)
600 return err;
602 /* Treat argument as a reference prefix. */
603 err = got_ref_list(refs, repo, refname,
604 got_ref_cmp_by_name, NULL);
605 } else {
606 err = got_reflist_insert(new, refs, ref,
607 got_ref_cmp_by_name, NULL);
608 if (err || *new == NULL /* duplicate */)
609 got_ref_close(ref);
612 return err;
615 static const struct got_error *
616 cmd_pack(int argc, char *argv[])
618 const struct got_error *error = NULL;
619 char *repo_path = NULL;
620 struct got_repository *repo = NULL;
621 int ch, i, loose_obj_only = 1, verbosity = 0;
622 struct got_object_id *pack_hash = NULL;
623 char *id_str = NULL;
624 struct got_pack_progress_arg ppa;
625 FILE *packfile = NULL;
626 struct got_pathlist_head exclude_args;
627 struct got_pathlist_entry *pe;
628 struct got_reflist_head exclude_refs;
629 struct got_reflist_head include_refs;
630 struct got_reflist_entry *re, *new;
632 TAILQ_INIT(&exclude_args);
633 TAILQ_INIT(&exclude_refs);
634 TAILQ_INIT(&include_refs);
636 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
637 switch (ch) {
638 case 'a':
639 loose_obj_only = 0;
640 break;
641 case 'r':
642 repo_path = realpath(optarg, NULL);
643 if (repo_path == NULL)
644 return got_error_from_errno2("realpath",
645 optarg);
646 got_path_strip_trailing_slashes(repo_path);
647 break;
648 case 'x':
649 got_path_strip_trailing_slashes(optarg);
650 error = got_pathlist_append(&exclude_args,
651 optarg, NULL);
652 if (error)
653 return error;
654 break;
655 case 'q':
656 verbosity = -1;
657 break;
658 default:
659 usage_pack();
660 /* NOTREACHED */
664 argc -= optind;
665 argv += optind;
667 #ifndef PROFILE
668 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
669 NULL) == -1)
670 err(1, "pledge");
671 #endif
672 if (repo_path == NULL) {
673 error = get_repo_path(&repo_path);
674 if (error)
675 goto done;
677 error = got_repo_open(&repo, repo_path, NULL);
678 if (error)
679 goto done;
681 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
682 if (error)
683 goto done;
685 TAILQ_FOREACH(pe, &exclude_args, entry) {
686 const char *refname = pe->path;
687 error = add_ref(&new, &exclude_refs, refname, repo);
688 if (error)
689 goto done;
693 if (argc == 0) {
694 error = got_ref_list(&include_refs, repo, "",
695 got_ref_cmp_by_name, NULL);
696 if (error)
697 goto done;
698 } else {
699 for (i = 0; i < argc; i++) {
700 const char *refname;
701 got_path_strip_trailing_slashes(argv[i]);
702 refname = argv[i];
703 error = add_ref(&new, &include_refs, refname, repo);
704 if (error)
705 goto done;
709 /* Ignore references in the refs/got/ namespace. */
710 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
711 const char *refname = got_ref_get_name(re->ref);
712 if (strncmp("refs/got/", refname, 9) != 0)
713 continue;
714 TAILQ_REMOVE(&include_refs, re, entry);
715 got_ref_close(re->ref);
716 free(re);
719 memset(&ppa, 0, sizeof(ppa));
720 ppa.last_scaled_size[0] = '\0';
721 ppa.last_p_indexed = -1;
722 ppa.last_p_resolved = -1;
723 ppa.verbosity = verbosity;
725 error = got_repo_pack_objects(&packfile, &pack_hash,
726 &include_refs, &exclude_refs, repo, loose_obj_only,
727 pack_progress, &ppa, check_cancelled, NULL);
728 if (error) {
729 if (ppa.printed_something)
730 printf("\n");
731 goto done;
734 error = got_object_id_str(&id_str, pack_hash);
735 if (error)
736 goto done;
737 if (verbosity >= 0)
738 printf("\nWrote %s.pack\n", id_str);
740 error = got_repo_index_pack(packfile, pack_hash, repo,
741 pack_index_progress, &ppa, check_cancelled, NULL);
742 if (error)
743 goto done;
744 if (verbosity >= 0)
745 printf("\nIndexed %s.pack\n", id_str);
746 done:
747 if (repo)
748 got_repo_close(repo);
749 got_pathlist_free(&exclude_args);
750 got_ref_list_free(&exclude_refs);
751 got_ref_list_free(&include_refs);
752 free(id_str);
753 free(pack_hash);
754 free(repo_path);
755 return error;
758 __dead static void
759 usage_indexpack(void)
761 fprintf(stderr, "usage: %s indexpack packfile-path\n",
762 getprogname());
763 exit(1);
766 static const struct got_error *
767 cmd_indexpack(int argc, char *argv[])
769 const struct got_error *error = NULL;
770 struct got_repository *repo = NULL;
771 int ch;
772 struct got_object_id *pack_hash = NULL;
773 char *packfile_path = NULL;
774 char *id_str = NULL;
775 struct got_pack_progress_arg ppa;
776 FILE *packfile = NULL;
778 while ((ch = getopt(argc, argv, "")) != -1) {
779 switch (ch) {
780 default:
781 usage_indexpack();
782 /* NOTREACHED */
786 argc -= optind;
787 argv += optind;
789 if (argc != 1)
790 usage_indexpack();
792 packfile_path = realpath(argv[0], NULL);
793 if (packfile_path == NULL)
794 return got_error_from_errno2("realpath", argv[0]);
796 #ifndef PROFILE
797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
798 NULL) == -1)
799 err(1, "pledge");
800 #endif
802 error = got_repo_open(&repo, packfile_path, NULL);
803 if (error)
804 goto done;
806 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
807 if (error)
808 goto done;
810 memset(&ppa, 0, sizeof(ppa));
811 ppa.last_scaled_size[0] = '\0';
812 ppa.last_p_indexed = -1;
813 ppa.last_p_resolved = -1;
815 error = got_repo_find_pack(&packfile, &pack_hash, repo,
816 packfile_path);
817 if (error)
818 goto done;
820 error = got_object_id_str(&id_str, pack_hash);
821 if (error)
822 goto done;
824 error = got_repo_index_pack(packfile, pack_hash, repo,
825 pack_index_progress, &ppa, check_cancelled, NULL);
826 if (error)
827 goto done;
828 printf("\nIndexed %s.pack\n", id_str);
829 done:
830 if (repo)
831 got_repo_close(repo);
832 free(id_str);
833 free(pack_hash);
834 return error;
837 __dead static void
838 usage_listpack(void)
840 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
841 getprogname());
842 exit(1);
845 struct gotadmin_list_pack_cb_args {
846 int nblobs;
847 int ntrees;
848 int ncommits;
849 int ntags;
850 int noffdeltas;
851 int nrefdeltas;
852 int human_readable;
853 };
855 static const struct got_error *
856 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
857 off_t size, off_t base_offset, struct got_object_id *base_id)
859 const struct got_error *err;
860 struct gotadmin_list_pack_cb_args *a = arg;
861 char *id_str, *delta_str = NULL, *base_id_str = NULL;
862 const char *type_str;
864 err = got_object_id_str(&id_str, id);
865 if (err)
866 return err;
868 switch (type) {
869 case GOT_OBJ_TYPE_BLOB:
870 type_str = GOT_OBJ_LABEL_BLOB;
871 a->nblobs++;
872 break;
873 case GOT_OBJ_TYPE_TREE:
874 type_str = GOT_OBJ_LABEL_TREE;
875 a->ntrees++;
876 break;
877 case GOT_OBJ_TYPE_COMMIT:
878 type_str = GOT_OBJ_LABEL_COMMIT;
879 a->ncommits++;
880 break;
881 case GOT_OBJ_TYPE_TAG:
882 type_str = GOT_OBJ_LABEL_TAG;
883 a->ntags++;
884 break;
885 case GOT_OBJ_TYPE_OFFSET_DELTA:
886 type_str = "offset-delta";
887 if (asprintf(&delta_str, " base-offset %lld",
888 (long long)base_offset) == -1) {
889 err = got_error_from_errno("asprintf");
890 goto done;
892 a->noffdeltas++;
893 break;
894 case GOT_OBJ_TYPE_REF_DELTA:
895 type_str = "ref-delta";
896 err = got_object_id_str(&base_id_str, base_id);
897 if (err)
898 goto done;
899 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
900 err = got_error_from_errno("asprintf");
901 goto done;
903 a->nrefdeltas++;
904 break;
905 default:
906 err = got_error(GOT_ERR_OBJ_TYPE);
907 goto done;
909 if (a->human_readable) {
910 char scaled[FMT_SCALED_STRSIZE];
911 char *s;;
912 if (fmt_scaled(size, scaled) == -1) {
913 err = got_error_from_errno("fmt_scaled");
914 goto done;
916 s = scaled;
917 while (isspace((unsigned char)*s))
918 s++;
919 printf("%s %s at %lld size %s%s\n", id_str, type_str,
920 (long long)offset, s, delta_str ? delta_str : "");
921 } else {
922 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
923 (long long)offset, (long long)size,
924 delta_str ? delta_str : "");
926 done:
927 free(id_str);
928 free(base_id_str);
929 free(delta_str);
930 return err;
933 static const struct got_error *
934 cmd_listpack(int argc, char *argv[])
936 const struct got_error *error = NULL;
937 struct got_repository *repo = NULL;
938 int ch;
939 struct got_object_id *pack_hash = NULL;
940 char *packfile_path = NULL;
941 char *id_str = NULL;
942 struct gotadmin_list_pack_cb_args lpa;
943 FILE *packfile = NULL;
944 int show_stats = 0, human_readable = 0;
946 while ((ch = getopt(argc, argv, "hs")) != -1) {
947 switch (ch) {
948 case 'h':
949 human_readable = 1;
950 break;
951 case 's':
952 show_stats = 1;
953 break;
954 default:
955 usage_listpack();
956 /* NOTREACHED */
960 argc -= optind;
961 argv += optind;
963 if (argc != 1)
964 usage_listpack();
965 packfile_path = realpath(argv[0], NULL);
966 if (packfile_path == NULL)
967 return got_error_from_errno2("realpath", argv[0]);
969 #ifndef PROFILE
970 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
971 NULL) == -1)
972 err(1, "pledge");
973 #endif
974 error = got_repo_open(&repo, packfile_path, NULL);
975 if (error)
976 goto done;
977 #ifndef PROFILE
978 /* Remove "cpath" promise. */
979 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
980 NULL) == -1)
981 err(1, "pledge");
982 #endif
983 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
984 if (error)
985 goto done;
987 error = got_repo_find_pack(&packfile, &pack_hash, repo,
988 packfile_path);
989 if (error)
990 goto done;
991 error = got_object_id_str(&id_str, pack_hash);
992 if (error)
993 goto done;
995 memset(&lpa, 0, sizeof(lpa));
996 lpa.human_readable = human_readable;
997 error = got_repo_list_pack(packfile, pack_hash, repo,
998 list_pack_cb, &lpa, check_cancelled, NULL);
999 if (error)
1000 goto done;
1001 if (show_stats) {
1002 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1003 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1004 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1005 lpa.noffdeltas + lpa.nrefdeltas,
1006 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1007 lpa.noffdeltas, lpa.nrefdeltas);
1009 done:
1010 if (repo)
1011 got_repo_close(repo);
1012 free(id_str);
1013 free(pack_hash);
1014 free(packfile_path);
1015 return error;
1018 __dead static void
1019 usage_cleanup(void)
1021 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1022 "[-q]\n", getprogname());
1023 exit(1);
1026 struct got_cleanup_progress_arg {
1027 int last_nloose;
1028 int last_ncommits;
1029 int last_npurged;
1030 int verbosity;
1031 int printed_something;
1032 int dry_run;
1035 static const struct got_error *
1036 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1038 struct got_cleanup_progress_arg *a = arg;
1039 int print_loose = 0, print_commits = 0, print_purged = 0;
1041 if (a->last_nloose != nloose) {
1042 print_loose = 1;
1043 a->last_nloose = nloose;
1045 if (a->last_ncommits != ncommits) {
1046 print_loose = 1;
1047 print_commits = 1;
1048 a->last_ncommits = ncommits;
1050 if (a->last_npurged != npurged) {
1051 print_loose = 1;
1052 print_commits = 1;
1053 print_purged = 1;
1054 a->last_npurged = npurged;
1057 if (a->verbosity < 0)
1058 return NULL;
1060 if (print_loose || print_commits || print_purged)
1061 printf("\r");
1062 if (print_loose)
1063 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1064 if (print_commits)
1065 printf("; %d commit%s scanned", ncommits,
1066 ncommits == 1 ? "" : "s");
1067 if (print_purged) {
1068 if (a->dry_run) {
1069 printf("; %d object%s could be purged", npurged,
1070 npurged == 1 ? "" : "s");
1071 } else {
1072 printf("; %d object%s purged", npurged,
1073 npurged == 1 ? "" : "s");
1076 if (print_loose || print_commits || print_purged) {
1077 a->printed_something = 1;
1078 fflush(stdout);
1080 return NULL;
1083 struct got_lonely_packidx_progress_arg {
1084 int verbosity;
1085 int printed_something;
1086 int dry_run;
1089 static const struct got_error *
1090 lonely_packidx_progress(void *arg, const char *path)
1092 struct got_lonely_packidx_progress_arg *a = arg;
1094 if (a->verbosity < 0)
1095 return NULL;
1097 if (a->dry_run)
1098 printf("%s could be removed\n", path);
1099 else
1100 printf("%s removed\n", path);
1102 a->printed_something = 1;
1103 return NULL;
1106 static const struct got_error *
1107 cmd_cleanup(int argc, char *argv[])
1109 const struct got_error *error = NULL;
1110 char *repo_path = NULL;
1111 struct got_repository *repo = NULL;
1112 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1113 int remove_lonely_packidx = 0, ignore_mtime = 0;
1114 struct got_cleanup_progress_arg cpa;
1115 struct got_lonely_packidx_progress_arg lpa;
1116 off_t size_before, size_after;
1117 char scaled_before[FMT_SCALED_STRSIZE];
1118 char scaled_after[FMT_SCALED_STRSIZE];
1119 char scaled_diff[FMT_SCALED_STRSIZE];
1120 char **extensions;
1121 int nextensions, i;
1123 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1124 switch (ch) {
1125 case 'a':
1126 ignore_mtime = 1;
1127 break;
1128 case 'p':
1129 remove_lonely_packidx = 1;
1130 break;
1131 case 'r':
1132 repo_path = realpath(optarg, NULL);
1133 if (repo_path == NULL)
1134 return got_error_from_errno2("realpath",
1135 optarg);
1136 got_path_strip_trailing_slashes(repo_path);
1137 break;
1138 case 'n':
1139 dry_run = 1;
1140 break;
1141 case 'q':
1142 verbosity = -1;
1143 break;
1144 default:
1145 usage_cleanup();
1146 /* NOTREACHED */
1150 argc -= optind;
1151 argv += optind;
1153 #ifndef PROFILE
1154 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1155 NULL) == -1)
1156 err(1, "pledge");
1157 #endif
1158 if (repo_path == NULL) {
1159 error = get_repo_path(&repo_path);
1160 if (error)
1161 goto done;
1163 error = got_repo_open(&repo, repo_path, NULL);
1164 if (error)
1165 goto done;
1167 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1168 if (error)
1169 goto done;
1171 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1172 repo);
1173 for (i = 0; i < nextensions; i++) {
1174 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1175 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1176 "the preciousObjects Git extension is enabled; "
1177 "this implies that objects must not be deleted");
1178 goto done;
1182 if (remove_lonely_packidx) {
1183 memset(&lpa, 0, sizeof(lpa));
1184 lpa.dry_run = dry_run;
1185 lpa.verbosity = verbosity;
1186 error = got_repo_remove_lonely_packidx(repo, dry_run,
1187 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1188 goto done;
1191 memset(&cpa, 0, sizeof(cpa));
1192 cpa.last_ncommits = -1;
1193 cpa.last_npurged = -1;
1194 cpa.dry_run = dry_run;
1195 cpa.verbosity = verbosity;
1196 error = got_repo_purge_unreferenced_loose_objects(repo,
1197 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1198 cleanup_progress, &cpa, check_cancelled, NULL);
1199 if (cpa.printed_something)
1200 printf("\n");
1201 if (error)
1202 goto done;
1203 if (cpa.printed_something) {
1204 if (fmt_scaled(size_before, scaled_before) == -1) {
1205 error = got_error_from_errno("fmt_scaled");
1206 goto done;
1208 if (fmt_scaled(size_after, scaled_after) == -1) {
1209 error = got_error_from_errno("fmt_scaled");
1210 goto done;
1212 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1213 error = got_error_from_errno("fmt_scaled");
1214 goto done;
1216 printf("loose total size before: %s\n", scaled_before);
1217 printf("loose total size after: %s\n", scaled_after);
1218 if (dry_run) {
1219 printf("disk space which would be freed: %s\n",
1220 scaled_diff);
1221 } else
1222 printf("disk space freed: %s\n", scaled_diff);
1223 printf("loose objects also found in pack files: %d\n", npacked);
1225 done:
1226 if (repo)
1227 got_repo_close(repo);
1228 free(repo_path);
1229 return error;