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/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <util.h>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_gotconfig.h"
42 #include "got_path.h"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
45 #include "got_worktree.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static volatile sig_atomic_t sigint_received;
52 static volatile sig_atomic_t sigpipe_received;
54 static void
55 catch_sigint(int signo)
56 {
57 sigint_received = 1;
58 }
60 static void
61 catch_sigpipe(int signo)
62 {
63 sigpipe_received = 1;
64 }
66 static const struct got_error *
67 check_cancelled(void *arg)
68 {
69 if (sigint_received || sigpipe_received)
70 return got_error(GOT_ERR_CANCELLED);
71 return NULL;
72 }
74 struct gotadmin_cmd {
75 const char *cmd_name;
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
79 };
81 __dead static void usage(int, int);
82 __dead static void usage_info(void);
83 __dead static void usage_pack(void);
84 __dead static void usage_indexpack(void);
85 __dead static void usage_listpack(void);
86 __dead static void usage_cleanup(void);
88 static const struct got_error* cmd_info(int, char *[]);
89 static const struct got_error* cmd_pack(int, char *[]);
90 static const struct got_error* cmd_indexpack(int, char *[]);
91 static const struct got_error* cmd_listpack(int, char *[]);
92 static const struct got_error* cmd_cleanup(int, char *[]);
94 static const struct gotadmin_cmd gotadmin_commands[] = {
95 { "info", cmd_info, usage_info, "" },
96 { "pack", cmd_pack, usage_pack, "" },
97 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
98 { "listpack", cmd_listpack, usage_listpack, "ls" },
99 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
100 };
102 static void
103 list_commands(FILE *fp)
105 size_t i;
107 fprintf(fp, "commands:");
108 for (i = 0; i < nitems(gotadmin_commands); i++) {
109 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
110 fprintf(fp, " %s", cmd->cmd_name);
112 fputc('\n', fp);
115 int
116 main(int argc, char *argv[])
118 const struct gotadmin_cmd *cmd;
119 size_t i;
120 int ch;
121 int hflag = 0, Vflag = 0;
122 static const struct option longopts[] = {
123 { "version", no_argument, NULL, 'V' },
124 { NULL, 0, NULL, 0 }
125 };
127 setlocale(LC_CTYPE, "");
129 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
130 switch (ch) {
131 case 'h':
132 hflag = 1;
133 break;
134 case 'V':
135 Vflag = 1;
136 break;
137 default:
138 usage(hflag, 1);
139 /* NOTREACHED */
143 argc -= optind;
144 argv += optind;
145 optind = 1;
146 optreset = 1;
148 if (Vflag) {
149 got_version_print_str();
150 return 0;
153 if (argc <= 0)
154 usage(hflag, hflag ? 0 : 1);
156 signal(SIGINT, catch_sigint);
157 signal(SIGPIPE, catch_sigpipe);
159 for (i = 0; i < nitems(gotadmin_commands); i++) {
160 const struct got_error *error;
162 cmd = &gotadmin_commands[i];
164 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
165 strcmp(cmd->cmd_alias, argv[0]) != 0)
166 continue;
168 if (hflag)
169 cmd->cmd_usage();
171 error = cmd->cmd_main(argc, argv);
172 if (error && error->code != GOT_ERR_CANCELLED &&
173 error->code != GOT_ERR_PRIVSEP_EXIT &&
174 !(sigpipe_received &&
175 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
176 !(sigint_received &&
177 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
179 return 1;
182 return 0;
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 list_commands(stderr);
187 return 1;
190 __dead static void
191 usage(int hflag, int status)
193 FILE *fp = (status == 0) ? stdout : stderr;
195 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
196 getprogname());
197 if (hflag)
198 list_commands(fp);
199 exit(status);
202 static const struct got_error *
203 apply_unveil(const char *repo_path, int repo_read_only)
205 const struct got_error *err;
207 #ifdef PROFILE
208 if (unveil("gmon.out", "rwc") != 0)
209 return got_error_from_errno2("unveil", "gmon.out");
210 #endif
211 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
212 return got_error_from_errno2("unveil", repo_path);
214 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
215 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
217 err = got_privsep_unveil_exec_helpers();
218 if (err != NULL)
219 return err;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno("unveil");
224 return NULL;
227 __dead static void
228 usage_info(void)
230 fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 getprogname());
232 exit(1);
235 static const struct got_error *
236 get_repo_path(char **repo_path)
238 const struct got_error *err = NULL;
239 struct got_worktree *worktree = NULL;
240 char *cwd;
242 *repo_path = NULL;
244 cwd = getcwd(NULL, 0);
245 if (cwd == NULL)
246 return got_error_from_errno("getcwd");
248 err = got_worktree_open(&worktree, cwd);
249 if (err) {
250 if (err->code != GOT_ERR_NOT_WORKTREE)
251 goto done;
252 err = NULL;
255 if (worktree)
256 *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 else
258 *repo_path = strdup(cwd);
259 if (*repo_path == NULL)
260 err = got_error_from_errno("strdup");
261 done:
262 if (worktree)
263 got_worktree_close(worktree);
264 free(cwd);
265 return err;
268 static const struct got_error *
269 cmd_info(int argc, char *argv[])
271 const struct got_error *error = NULL;
272 char *repo_path = NULL;
273 struct got_repository *repo = NULL;
274 const struct got_gotconfig *gotconfig = NULL;
275 int ch, npackfiles, npackedobj, nobj;
276 off_t packsize, loose_size;
277 char scaled[FMT_SCALED_STRSIZE];
279 while ((ch = getopt(argc, argv, "r:")) != -1) {
280 switch (ch) {
281 case 'r':
282 repo_path = realpath(optarg, NULL);
283 if (repo_path == NULL)
284 return got_error_from_errno2("realpath",
285 optarg);
286 got_path_strip_trailing_slashes(repo_path);
287 break;
288 default:
289 usage_info();
290 /* NOTREACHED */
294 argc -= optind;
295 argv += optind;
297 #ifndef PROFILE
298 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
299 NULL) == -1)
300 err(1, "pledge");
301 #endif
302 if (repo_path == NULL) {
303 error = get_repo_path(&repo_path);
304 if (error)
305 goto done;
307 error = got_repo_open(&repo, repo_path, NULL);
308 if (error)
309 goto done;
310 #ifndef PROFILE
311 /* Remove "cpath" promise. */
312 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
313 NULL) == -1)
314 err(1, "pledge");
315 #endif
316 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
317 if (error)
318 goto done;
320 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
322 gotconfig = got_repo_get_gotconfig(repo);
323 if (gotconfig) {
324 const struct got_remote_repo *remotes;
325 int i, nremotes;
326 if (got_gotconfig_get_author(gotconfig)) {
327 printf("default author: %s\n",
328 got_gotconfig_get_author(gotconfig));
330 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
331 for (i = 0; i < nremotes; i++) {
332 const char *fetch_url = remotes[i].fetch_url;
333 const char *send_url = remotes[i].send_url;
334 if (strcmp(fetch_url, send_url) == 0) {
335 printf("remote \"%s\": %s\n", remotes[i].name,
336 remotes[i].fetch_url);
337 } else {
338 printf("remote \"%s\" (fetch): %s\n",
339 remotes[i].name, remotes[i].fetch_url);
340 printf("remote \"%s\" (send): %s\n",
341 remotes[i].name, remotes[i].send_url);
346 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
347 &packsize, repo);
348 if (error)
349 goto done;
350 printf("pack files: %d\n", npackfiles);
351 if (npackfiles > 0) {
352 if (fmt_scaled(packsize, scaled) == -1) {
353 error = got_error_from_errno("fmt_scaled");
354 goto done;
356 printf("packed objects: %d\n", npackedobj);
357 printf("packed total size: %s\n", scaled);
360 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
361 if (error)
362 goto done;
363 printf("loose objects: %d\n", nobj);
364 if (nobj > 0) {
365 if (fmt_scaled(loose_size, scaled) == -1) {
366 error = got_error_from_errno("fmt_scaled");
367 goto done;
369 printf("loose total size: %s\n", scaled);
371 done:
372 if (repo)
373 got_repo_close(repo);
374 free(repo_path);
375 return error;
378 __dead static void
379 usage_pack(void)
381 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
382 "[-x reference] [-q] [reference ...]\n",
383 getprogname());
384 exit(1);
387 struct got_pack_progress_arg {
388 char last_scaled_size[FMT_SCALED_STRSIZE];
389 int last_ncolored;
390 int last_nfound;
391 int last_ntrees;
392 int loading_done;
393 int last_ncommits;
394 int last_nobj_total;
395 int last_p_deltify;
396 int last_p_written;
397 int last_p_indexed;
398 int last_p_resolved;
399 int verbosity;
400 int printed_something;
401 };
403 static void
404 print_load_info(int print_colored, int print_found, int print_trees,
405 int ncolored, int nfound, int ntrees)
407 if (print_colored) {
408 printf("%d commit%s colored", ncolored,
409 ncolored == 1 ? "" : "s");
411 if (print_found) {
412 printf("%s%d object%s found",
413 ncolored > 0 ? "; " : "",
414 nfound, nfound == 1 ? "" : "s");
416 if (print_trees) {
417 printf("; %d tree%s scanned", ntrees,
418 ntrees == 1 ? "" : "s");
422 static const struct got_error *
423 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
424 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
425 int nobj_written)
427 struct got_pack_progress_arg *a = arg;
428 char scaled_size[FMT_SCALED_STRSIZE];
429 int p_deltify, p_written;
430 int print_colored = 0, print_found = 0, print_trees = 0;
431 int print_searching = 0, print_total = 0;
432 int print_deltify = 0, print_written = 0;
434 if (a->verbosity < 0)
435 return NULL;
437 if (a->last_ncolored != ncolored) {
438 print_colored = 1;
439 a->last_ncolored = ncolored;
442 if (a->last_nfound != nfound) {
443 print_colored = 1;
444 print_found = 1;
445 a->last_nfound = nfound;
448 if (a->last_ntrees != ntrees) {
449 print_colored = 1;
450 print_found = 1;
451 print_trees = 1;
452 a->last_ntrees = ntrees;
455 if ((print_colored || print_found || print_trees) &&
456 !a->loading_done) {
457 printf("\r");
458 print_load_info(print_colored, print_found, print_trees,
459 ncolored, nfound, ntrees);
460 a->printed_something = 1;
461 fflush(stdout);
462 return NULL;
463 } else if (!a->loading_done) {
464 printf("\r");
465 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
466 printf("\n");
467 a->loading_done = 1;
470 if (fmt_scaled(packfile_size, scaled_size) == -1)
471 return got_error_from_errno("fmt_scaled");
473 if (a->last_ncommits != ncommits) {
474 print_searching = 1;
475 a->last_ncommits = ncommits;
478 if (a->last_nobj_total != nobj_total) {
479 print_searching = 1;
480 print_total = 1;
481 a->last_nobj_total = nobj_total;
484 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
485 strcmp(scaled_size, a->last_scaled_size)) != 0) {
486 if (strlcpy(a->last_scaled_size, scaled_size,
487 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
488 return got_error(GOT_ERR_NO_SPACE);
491 if (nobj_deltify > 0 || nobj_written > 0) {
492 if (nobj_deltify > 0) {
493 p_deltify = (nobj_deltify * 100) / nobj_total;
494 if (p_deltify != a->last_p_deltify) {
495 a->last_p_deltify = p_deltify;
496 print_searching = 1;
497 print_total = 1;
498 print_deltify = 1;
501 if (nobj_written > 0) {
502 p_written = (nobj_written * 100) / nobj_total;
503 if (p_written != a->last_p_written) {
504 a->last_p_written = p_written;
505 print_searching = 1;
506 print_total = 1;
507 print_deltify = 1;
508 print_written = 1;
513 if (print_searching || print_total || print_deltify || print_written)
514 printf("\r");
515 if (print_searching)
516 printf("packing %d reference%s", ncommits,
517 ncommits == 1 ? "" : "s");
518 if (print_total)
519 printf("; %d object%s", nobj_total,
520 nobj_total == 1 ? "" : "s");
521 if (print_deltify)
522 printf("; deltify: %d%%", p_deltify);
523 if (print_written)
524 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
525 scaled_size, p_written);
526 if (print_searching || print_total || print_deltify ||
527 print_written) {
528 a->printed_something = 1;
529 fflush(stdout);
531 return NULL;
534 static const struct got_error *
535 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
536 int nobj_indexed, int nobj_loose, int nobj_resolved)
538 struct got_pack_progress_arg *a = arg;
539 char scaled_size[FMT_SCALED_STRSIZE];
540 int p_indexed, p_resolved;
541 int print_size = 0, print_indexed = 0, print_resolved = 0;
543 if (a->verbosity < 0)
544 return NULL;
546 if (packfile_size > 0 || nobj_indexed > 0) {
547 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
548 (a->last_scaled_size[0] == '\0' ||
549 strcmp(scaled_size, a->last_scaled_size)) != 0) {
550 print_size = 1;
551 if (strlcpy(a->last_scaled_size, scaled_size,
552 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
553 return got_error(GOT_ERR_NO_SPACE);
555 if (nobj_indexed > 0) {
556 p_indexed = (nobj_indexed * 100) / nobj_total;
557 if (p_indexed != a->last_p_indexed) {
558 a->last_p_indexed = p_indexed;
559 print_indexed = 1;
560 print_size = 1;
563 if (nobj_resolved > 0) {
564 p_resolved = (nobj_resolved * 100) /
565 (nobj_total - nobj_loose);
566 if (p_resolved != a->last_p_resolved) {
567 a->last_p_resolved = p_resolved;
568 print_resolved = 1;
569 print_indexed = 1;
570 print_size = 1;
575 if (print_size || print_indexed || print_resolved)
576 printf("\r");
577 if (print_size)
578 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
579 if (print_indexed)
580 printf("; indexing %d%%", p_indexed);
581 if (print_resolved)
582 printf("; resolving deltas %d%%", p_resolved);
583 if (print_size || print_indexed || print_resolved)
584 fflush(stdout);
586 return NULL;
589 static const struct got_error *
590 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
591 const char *refname, struct got_repository *repo)
593 const struct got_error *err;
594 struct got_reference *ref;
596 *new = NULL;
598 err = got_ref_open(&ref, repo, refname, 0);
599 if (err) {
600 if (err->code != GOT_ERR_NOT_REF)
601 return err;
603 /* Treat argument as a reference prefix. */
604 err = got_ref_list(refs, repo, refname,
605 got_ref_cmp_by_name, NULL);
606 } else {
607 err = got_reflist_insert(new, refs, ref,
608 got_ref_cmp_by_name, NULL);
609 if (err || *new == NULL /* duplicate */)
610 got_ref_close(ref);
613 return err;
616 static const struct got_error *
617 cmd_pack(int argc, char *argv[])
619 const struct got_error *error = NULL;
620 char *repo_path = NULL;
621 struct got_repository *repo = NULL;
622 int ch, i, loose_obj_only = 1, verbosity = 0;
623 struct got_object_id *pack_hash = NULL;
624 char *id_str = NULL;
625 struct got_pack_progress_arg ppa;
626 FILE *packfile = NULL;
627 struct got_pathlist_head exclude_args;
628 struct got_pathlist_entry *pe;
629 struct got_reflist_head exclude_refs;
630 struct got_reflist_head include_refs;
631 struct got_reflist_entry *re, *new;
633 TAILQ_INIT(&exclude_args);
634 TAILQ_INIT(&exclude_refs);
635 TAILQ_INIT(&include_refs);
637 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
638 switch (ch) {
639 case 'a':
640 loose_obj_only = 0;
641 break;
642 case 'r':
643 repo_path = realpath(optarg, NULL);
644 if (repo_path == NULL)
645 return got_error_from_errno2("realpath",
646 optarg);
647 got_path_strip_trailing_slashes(repo_path);
648 break;
649 case 'x':
650 got_path_strip_trailing_slashes(optarg);
651 error = got_pathlist_append(&exclude_args,
652 optarg, NULL);
653 if (error)
654 return error;
655 break;
656 case 'q':
657 verbosity = -1;
658 break;
659 default:
660 usage_pack();
661 /* NOTREACHED */
665 argc -= optind;
666 argv += optind;
668 #ifndef PROFILE
669 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
670 NULL) == -1)
671 err(1, "pledge");
672 #endif
673 if (repo_path == NULL) {
674 error = get_repo_path(&repo_path);
675 if (error)
676 goto done;
678 error = got_repo_open(&repo, repo_path, NULL);
679 if (error)
680 goto done;
682 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
683 if (error)
684 goto done;
686 TAILQ_FOREACH(pe, &exclude_args, entry) {
687 const char *refname = pe->path;
688 error = add_ref(&new, &exclude_refs, refname, repo);
689 if (error)
690 goto done;
694 if (argc == 0) {
695 error = got_ref_list(&include_refs, repo, "",
696 got_ref_cmp_by_name, NULL);
697 if (error)
698 goto done;
699 } else {
700 for (i = 0; i < argc; i++) {
701 const char *refname;
702 got_path_strip_trailing_slashes(argv[i]);
703 refname = argv[i];
704 error = add_ref(&new, &include_refs, refname, repo);
705 if (error)
706 goto done;
710 /* Ignore references in the refs/got/ namespace. */
711 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
712 const char *refname = got_ref_get_name(re->ref);
713 if (strncmp("refs/got/", refname, 9) != 0)
714 continue;
715 TAILQ_REMOVE(&include_refs, re, entry);
716 got_ref_close(re->ref);
717 free(re);
720 memset(&ppa, 0, sizeof(ppa));
721 ppa.last_scaled_size[0] = '\0';
722 ppa.last_p_indexed = -1;
723 ppa.last_p_resolved = -1;
724 ppa.verbosity = verbosity;
726 error = got_repo_pack_objects(&packfile, &pack_hash,
727 &include_refs, &exclude_refs, repo, loose_obj_only,
728 pack_progress, &ppa, check_cancelled, NULL);
729 if (error) {
730 if (ppa.printed_something)
731 printf("\n");
732 goto done;
735 error = got_object_id_str(&id_str, pack_hash);
736 if (error)
737 goto done;
738 if (verbosity >= 0)
739 printf("\nWrote %s.pack\n", id_str);
741 error = got_repo_index_pack(packfile, pack_hash, repo,
742 pack_index_progress, &ppa, check_cancelled, NULL);
743 if (error)
744 goto done;
745 if (verbosity >= 0)
746 printf("\nIndexed %s.pack\n", id_str);
747 done:
748 if (repo)
749 got_repo_close(repo);
750 got_pathlist_free(&exclude_args);
751 got_ref_list_free(&exclude_refs);
752 got_ref_list_free(&include_refs);
753 free(id_str);
754 free(pack_hash);
755 free(repo_path);
756 return error;
759 __dead static void
760 usage_indexpack(void)
762 fprintf(stderr, "usage: %s indexpack packfile-path\n",
763 getprogname());
764 exit(1);
767 static const struct got_error *
768 cmd_indexpack(int argc, char *argv[])
770 const struct got_error *error = NULL;
771 struct got_repository *repo = NULL;
772 int ch;
773 struct got_object_id *pack_hash = NULL;
774 char *packfile_path = NULL;
775 char *id_str = NULL;
776 struct got_pack_progress_arg ppa;
777 FILE *packfile = NULL;
779 while ((ch = getopt(argc, argv, "")) != -1) {
780 switch (ch) {
781 default:
782 usage_indexpack();
783 /* NOTREACHED */
787 argc -= optind;
788 argv += optind;
790 if (argc != 1)
791 usage_indexpack();
793 packfile_path = realpath(argv[0], NULL);
794 if (packfile_path == NULL)
795 return got_error_from_errno2("realpath", argv[0]);
797 #ifndef PROFILE
798 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
799 NULL) == -1)
800 err(1, "pledge");
801 #endif
803 error = got_repo_open(&repo, packfile_path, NULL);
804 if (error)
805 goto done;
807 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
808 if (error)
809 goto done;
811 memset(&ppa, 0, sizeof(ppa));
812 ppa.last_scaled_size[0] = '\0';
813 ppa.last_p_indexed = -1;
814 ppa.last_p_resolved = -1;
816 error = got_repo_find_pack(&packfile, &pack_hash, repo,
817 packfile_path);
818 if (error)
819 goto done;
821 error = got_object_id_str(&id_str, pack_hash);
822 if (error)
823 goto done;
825 error = got_repo_index_pack(packfile, pack_hash, repo,
826 pack_index_progress, &ppa, check_cancelled, NULL);
827 if (error)
828 goto done;
829 printf("\nIndexed %s.pack\n", id_str);
830 done:
831 if (repo)
832 got_repo_close(repo);
833 free(id_str);
834 free(pack_hash);
835 return error;
838 __dead static void
839 usage_listpack(void)
841 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
842 getprogname());
843 exit(1);
846 struct gotadmin_list_pack_cb_args {
847 int nblobs;
848 int ntrees;
849 int ncommits;
850 int ntags;
851 int noffdeltas;
852 int nrefdeltas;
853 int human_readable;
854 };
856 static const struct got_error *
857 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
858 off_t size, off_t base_offset, struct got_object_id *base_id)
860 const struct got_error *err;
861 struct gotadmin_list_pack_cb_args *a = arg;
862 char *id_str, *delta_str = NULL, *base_id_str = NULL;
863 const char *type_str;
865 err = got_object_id_str(&id_str, id);
866 if (err)
867 return err;
869 switch (type) {
870 case GOT_OBJ_TYPE_BLOB:
871 type_str = GOT_OBJ_LABEL_BLOB;
872 a->nblobs++;
873 break;
874 case GOT_OBJ_TYPE_TREE:
875 type_str = GOT_OBJ_LABEL_TREE;
876 a->ntrees++;
877 break;
878 case GOT_OBJ_TYPE_COMMIT:
879 type_str = GOT_OBJ_LABEL_COMMIT;
880 a->ncommits++;
881 break;
882 case GOT_OBJ_TYPE_TAG:
883 type_str = GOT_OBJ_LABEL_TAG;
884 a->ntags++;
885 break;
886 case GOT_OBJ_TYPE_OFFSET_DELTA:
887 type_str = "offset-delta";
888 if (asprintf(&delta_str, " base-offset %lld",
889 (long long)base_offset) == -1) {
890 err = got_error_from_errno("asprintf");
891 goto done;
893 a->noffdeltas++;
894 break;
895 case GOT_OBJ_TYPE_REF_DELTA:
896 type_str = "ref-delta";
897 err = got_object_id_str(&base_id_str, base_id);
898 if (err)
899 goto done;
900 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
901 err = got_error_from_errno("asprintf");
902 goto done;
904 a->nrefdeltas++;
905 break;
906 default:
907 err = got_error(GOT_ERR_OBJ_TYPE);
908 goto done;
910 if (a->human_readable) {
911 char scaled[FMT_SCALED_STRSIZE];
912 char *s;;
913 if (fmt_scaled(size, scaled) == -1) {
914 err = got_error_from_errno("fmt_scaled");
915 goto done;
917 s = scaled;
918 while (isspace((unsigned char)*s))
919 s++;
920 printf("%s %s at %lld size %s%s\n", id_str, type_str,
921 (long long)offset, s, delta_str ? delta_str : "");
922 } else {
923 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
924 (long long)offset, (long long)size,
925 delta_str ? delta_str : "");
927 done:
928 free(id_str);
929 free(base_id_str);
930 free(delta_str);
931 return err;
934 static const struct got_error *
935 cmd_listpack(int argc, char *argv[])
937 const struct got_error *error = NULL;
938 struct got_repository *repo = NULL;
939 int ch;
940 struct got_object_id *pack_hash = NULL;
941 char *packfile_path = NULL;
942 char *id_str = NULL;
943 struct gotadmin_list_pack_cb_args lpa;
944 FILE *packfile = NULL;
945 int show_stats = 0, human_readable = 0;
947 while ((ch = getopt(argc, argv, "hs")) != -1) {
948 switch (ch) {
949 case 'h':
950 human_readable = 1;
951 break;
952 case 's':
953 show_stats = 1;
954 break;
955 default:
956 usage_listpack();
957 /* NOTREACHED */
961 argc -= optind;
962 argv += optind;
964 if (argc != 1)
965 usage_listpack();
966 packfile_path = realpath(argv[0], NULL);
967 if (packfile_path == NULL)
968 return got_error_from_errno2("realpath", argv[0]);
970 #ifndef PROFILE
971 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
972 NULL) == -1)
973 err(1, "pledge");
974 #endif
975 error = got_repo_open(&repo, packfile_path, NULL);
976 if (error)
977 goto done;
978 #ifndef PROFILE
979 /* Remove "cpath" promise. */
980 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
981 NULL) == -1)
982 err(1, "pledge");
983 #endif
984 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
985 if (error)
986 goto done;
988 error = got_repo_find_pack(&packfile, &pack_hash, repo,
989 packfile_path);
990 if (error)
991 goto done;
992 error = got_object_id_str(&id_str, pack_hash);
993 if (error)
994 goto done;
996 memset(&lpa, 0, sizeof(lpa));
997 lpa.human_readable = human_readable;
998 error = got_repo_list_pack(packfile, pack_hash, repo,
999 list_pack_cb, &lpa, check_cancelled, NULL);
1000 if (error)
1001 goto done;
1002 if (show_stats) {
1003 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1004 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1005 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1006 lpa.noffdeltas + lpa.nrefdeltas,
1007 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1008 lpa.noffdeltas, lpa.nrefdeltas);
1010 done:
1011 if (repo)
1012 got_repo_close(repo);
1013 free(id_str);
1014 free(pack_hash);
1015 free(packfile_path);
1016 return error;
1019 __dead static void
1020 usage_cleanup(void)
1022 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1023 "[-q]\n", getprogname());
1024 exit(1);
1027 struct got_cleanup_progress_arg {
1028 int last_nloose;
1029 int last_ncommits;
1030 int last_npurged;
1031 int verbosity;
1032 int printed_something;
1033 int dry_run;
1036 static const struct got_error *
1037 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1039 struct got_cleanup_progress_arg *a = arg;
1040 int print_loose = 0, print_commits = 0, print_purged = 0;
1042 if (a->last_nloose != nloose) {
1043 print_loose = 1;
1044 a->last_nloose = nloose;
1046 if (a->last_ncommits != ncommits) {
1047 print_loose = 1;
1048 print_commits = 1;
1049 a->last_ncommits = ncommits;
1051 if (a->last_npurged != npurged) {
1052 print_loose = 1;
1053 print_commits = 1;
1054 print_purged = 1;
1055 a->last_npurged = npurged;
1058 if (a->verbosity < 0)
1059 return NULL;
1061 if (print_loose || print_commits || print_purged)
1062 printf("\r");
1063 if (print_loose)
1064 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1065 if (print_commits)
1066 printf("; %d commit%s scanned", ncommits,
1067 ncommits == 1 ? "" : "s");
1068 if (print_purged) {
1069 if (a->dry_run) {
1070 printf("; %d object%s could be purged", npurged,
1071 npurged == 1 ? "" : "s");
1072 } else {
1073 printf("; %d object%s purged", npurged,
1074 npurged == 1 ? "" : "s");
1077 if (print_loose || print_commits || print_purged) {
1078 a->printed_something = 1;
1079 fflush(stdout);
1081 return NULL;
1084 struct got_lonely_packidx_progress_arg {
1085 int verbosity;
1086 int printed_something;
1087 int dry_run;
1090 static const struct got_error *
1091 lonely_packidx_progress(void *arg, const char *path)
1093 struct got_lonely_packidx_progress_arg *a = arg;
1095 if (a->verbosity < 0)
1096 return NULL;
1098 if (a->dry_run)
1099 printf("%s could be removed\n", path);
1100 else
1101 printf("%s removed\n", path);
1103 a->printed_something = 1;
1104 return NULL;
1107 static const struct got_error *
1108 cmd_cleanup(int argc, char *argv[])
1110 const struct got_error *error = NULL;
1111 char *repo_path = NULL;
1112 struct got_repository *repo = NULL;
1113 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1114 int remove_lonely_packidx = 0, ignore_mtime = 0;
1115 struct got_cleanup_progress_arg cpa;
1116 struct got_lonely_packidx_progress_arg lpa;
1117 off_t size_before, size_after;
1118 char scaled_before[FMT_SCALED_STRSIZE];
1119 char scaled_after[FMT_SCALED_STRSIZE];
1120 char scaled_diff[FMT_SCALED_STRSIZE];
1121 char **extensions;
1122 int nextensions, i;
1124 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1125 switch (ch) {
1126 case 'a':
1127 ignore_mtime = 1;
1128 break;
1129 case 'p':
1130 remove_lonely_packidx = 1;
1131 break;
1132 case 'r':
1133 repo_path = realpath(optarg, NULL);
1134 if (repo_path == NULL)
1135 return got_error_from_errno2("realpath",
1136 optarg);
1137 got_path_strip_trailing_slashes(repo_path);
1138 break;
1139 case 'n':
1140 dry_run = 1;
1141 break;
1142 case 'q':
1143 verbosity = -1;
1144 break;
1145 default:
1146 usage_cleanup();
1147 /* NOTREACHED */
1151 argc -= optind;
1152 argv += optind;
1154 #ifndef PROFILE
1155 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1156 NULL) == -1)
1157 err(1, "pledge");
1158 #endif
1159 if (repo_path == NULL) {
1160 error = get_repo_path(&repo_path);
1161 if (error)
1162 goto done;
1164 error = got_repo_open(&repo, repo_path, NULL);
1165 if (error)
1166 goto done;
1168 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1169 if (error)
1170 goto done;
1172 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1173 repo);
1174 for (i = 0; i < nextensions; i++) {
1175 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1176 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1177 "the preciousObjects Git extension is enabled; "
1178 "this implies that objects must not be deleted");
1179 goto done;
1183 if (remove_lonely_packidx) {
1184 memset(&lpa, 0, sizeof(lpa));
1185 lpa.dry_run = dry_run;
1186 lpa.verbosity = verbosity;
1187 error = got_repo_remove_lonely_packidx(repo, dry_run,
1188 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1189 goto done;
1192 memset(&cpa, 0, sizeof(cpa));
1193 cpa.last_ncommits = -1;
1194 cpa.last_npurged = -1;
1195 cpa.dry_run = dry_run;
1196 cpa.verbosity = verbosity;
1197 error = got_repo_purge_unreferenced_loose_objects(repo,
1198 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1199 cleanup_progress, &cpa, check_cancelled, NULL);
1200 if (cpa.printed_something)
1201 printf("\n");
1202 if (error)
1203 goto done;
1204 if (cpa.printed_something) {
1205 if (fmt_scaled(size_before, scaled_before) == -1) {
1206 error = got_error_from_errno("fmt_scaled");
1207 goto done;
1209 if (fmt_scaled(size_after, scaled_after) == -1) {
1210 error = got_error_from_errno("fmt_scaled");
1211 goto done;
1213 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1214 error = got_error_from_errno("fmt_scaled");
1215 goto done;
1217 printf("loose total size before: %s\n", scaled_before);
1218 printf("loose total size after: %s\n", scaled_after);
1219 if (dry_run) {
1220 printf("disk space which would be freed: %s\n",
1221 scaled_diff);
1222 } else
1223 printf("disk space freed: %s\n", scaled_diff);
1224 printf("loose objects also found in pack files: %d\n", npacked);
1226 done:
1227 if (repo)
1228 got_repo_close(repo);
1229 free(repo_path);
1230 return error;