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];
278 int *pack_fds = NULL;
280 while ((ch = getopt(argc, argv, "r:")) != -1) {
281 switch (ch) {
282 case 'r':
283 repo_path = realpath(optarg, NULL);
284 if (repo_path == NULL)
285 return got_error_from_errno2("realpath",
286 optarg);
287 got_path_strip_trailing_slashes(repo_path);
288 break;
289 default:
290 usage_info();
291 /* NOTREACHED */
295 argc -= optind;
296 argv += optind;
298 #ifndef PROFILE
299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
300 NULL) == -1)
301 err(1, "pledge");
302 #endif
303 if (repo_path == NULL) {
304 error = get_repo_path(&repo_path);
305 if (error)
306 goto done;
308 error = got_repo_pack_fds_open(&pack_fds);
309 if (error != NULL)
310 goto done;
311 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
312 if (error)
313 goto done;
314 #ifndef PROFILE
315 /* Remove "cpath" promise. */
316 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
317 NULL) == -1)
318 err(1, "pledge");
319 #endif
320 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
321 if (error)
322 goto done;
324 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
326 gotconfig = got_repo_get_gotconfig(repo);
327 if (gotconfig) {
328 const struct got_remote_repo *remotes;
329 int i, nremotes;
330 if (got_gotconfig_get_author(gotconfig)) {
331 printf("default author: %s\n",
332 got_gotconfig_get_author(gotconfig));
334 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
335 for (i = 0; i < nremotes; i++) {
336 const char *fetch_url = remotes[i].fetch_url;
337 const char *send_url = remotes[i].send_url;
338 if (strcmp(fetch_url, send_url) == 0) {
339 printf("remote \"%s\": %s\n", remotes[i].name,
340 remotes[i].fetch_url);
341 } else {
342 printf("remote \"%s\" (fetch): %s\n",
343 remotes[i].name, remotes[i].fetch_url);
344 printf("remote \"%s\" (send): %s\n",
345 remotes[i].name, remotes[i].send_url);
350 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
351 &packsize, repo);
352 if (error)
353 goto done;
354 printf("pack files: %d\n", npackfiles);
355 if (npackfiles > 0) {
356 if (fmt_scaled(packsize, scaled) == -1) {
357 error = got_error_from_errno("fmt_scaled");
358 goto done;
360 printf("packed objects: %d\n", npackedobj);
361 printf("packed total size: %s\n", scaled);
364 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
365 if (error)
366 goto done;
367 printf("loose objects: %d\n", nobj);
368 if (nobj > 0) {
369 if (fmt_scaled(loose_size, scaled) == -1) {
370 error = got_error_from_errno("fmt_scaled");
371 goto done;
373 printf("loose total size: %s\n", scaled);
375 done:
376 if (repo)
377 got_repo_close(repo);
378 if (pack_fds) {
379 const struct got_error *pack_err =
380 got_repo_pack_fds_close(pack_fds);
381 if (error == NULL)
382 error = pack_err;
383 pack_fds = NULL;
386 free(repo_path);
387 return error;
390 __dead static void
391 usage_pack(void)
393 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
394 "[-x reference] [-q] [reference ...]\n",
395 getprogname());
396 exit(1);
399 struct got_pack_progress_arg {
400 char last_scaled_size[FMT_SCALED_STRSIZE];
401 int last_ncolored;
402 int last_nfound;
403 int last_ntrees;
404 int loading_done;
405 int last_ncommits;
406 int last_nobj_total;
407 int last_p_deltify;
408 int last_p_written;
409 int last_p_indexed;
410 int last_p_resolved;
411 int verbosity;
412 int printed_something;
413 };
415 static void
416 print_load_info(int print_colored, int print_found, int print_trees,
417 int ncolored, int nfound, int ntrees)
419 if (print_colored) {
420 printf("%d commit%s colored", ncolored,
421 ncolored == 1 ? "" : "s");
423 if (print_found) {
424 printf("%s%d object%s found",
425 ncolored > 0 ? "; " : "",
426 nfound, nfound == 1 ? "" : "s");
428 if (print_trees) {
429 printf("; %d tree%s scanned", ntrees,
430 ntrees == 1 ? "" : "s");
434 static const struct got_error *
435 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
436 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
437 int nobj_written)
439 struct got_pack_progress_arg *a = arg;
440 char scaled_size[FMT_SCALED_STRSIZE];
441 int p_deltify, p_written;
442 int print_colored = 0, print_found = 0, print_trees = 0;
443 int print_searching = 0, print_total = 0;
444 int print_deltify = 0, print_written = 0;
446 if (a->verbosity < 0)
447 return NULL;
449 if (a->last_ncolored != ncolored) {
450 print_colored = 1;
451 a->last_ncolored = ncolored;
454 if (a->last_nfound != nfound) {
455 print_colored = 1;
456 print_found = 1;
457 a->last_nfound = nfound;
460 if (a->last_ntrees != ntrees) {
461 print_colored = 1;
462 print_found = 1;
463 print_trees = 1;
464 a->last_ntrees = ntrees;
467 if ((print_colored || print_found || print_trees) &&
468 !a->loading_done) {
469 printf("\r");
470 print_load_info(print_colored, print_found, print_trees,
471 ncolored, nfound, ntrees);
472 a->printed_something = 1;
473 fflush(stdout);
474 return NULL;
475 } else if (!a->loading_done) {
476 printf("\r");
477 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
478 printf("\n");
479 a->loading_done = 1;
482 if (fmt_scaled(packfile_size, scaled_size) == -1)
483 return got_error_from_errno("fmt_scaled");
485 if (a->last_ncommits != ncommits) {
486 print_searching = 1;
487 a->last_ncommits = ncommits;
490 if (a->last_nobj_total != nobj_total) {
491 print_searching = 1;
492 print_total = 1;
493 a->last_nobj_total = nobj_total;
496 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
497 strcmp(scaled_size, a->last_scaled_size)) != 0) {
498 if (strlcpy(a->last_scaled_size, scaled_size,
499 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
500 return got_error(GOT_ERR_NO_SPACE);
503 if (nobj_deltify > 0 || nobj_written > 0) {
504 if (nobj_deltify > 0) {
505 p_deltify = (nobj_deltify * 100) / nobj_total;
506 if (p_deltify != a->last_p_deltify) {
507 a->last_p_deltify = p_deltify;
508 print_searching = 1;
509 print_total = 1;
510 print_deltify = 1;
513 if (nobj_written > 0) {
514 p_written = (nobj_written * 100) / nobj_total;
515 if (p_written != a->last_p_written) {
516 a->last_p_written = p_written;
517 print_searching = 1;
518 print_total = 1;
519 print_deltify = 1;
520 print_written = 1;
525 if (print_searching || print_total || print_deltify || print_written)
526 printf("\r");
527 if (print_searching)
528 printf("packing %d reference%s", ncommits,
529 ncommits == 1 ? "" : "s");
530 if (print_total)
531 printf("; %d object%s", nobj_total,
532 nobj_total == 1 ? "" : "s");
533 if (print_deltify)
534 printf("; deltify: %d%%", p_deltify);
535 if (print_written)
536 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
537 scaled_size, p_written);
538 if (print_searching || print_total || print_deltify ||
539 print_written) {
540 a->printed_something = 1;
541 fflush(stdout);
543 return NULL;
546 static const struct got_error *
547 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
548 int nobj_indexed, int nobj_loose, int nobj_resolved)
550 struct got_pack_progress_arg *a = arg;
551 char scaled_size[FMT_SCALED_STRSIZE];
552 int p_indexed, p_resolved;
553 int print_size = 0, print_indexed = 0, print_resolved = 0;
555 if (a->verbosity < 0)
556 return NULL;
558 if (packfile_size > 0 || nobj_indexed > 0) {
559 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
560 (a->last_scaled_size[0] == '\0' ||
561 strcmp(scaled_size, a->last_scaled_size)) != 0) {
562 print_size = 1;
563 if (strlcpy(a->last_scaled_size, scaled_size,
564 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
565 return got_error(GOT_ERR_NO_SPACE);
567 if (nobj_indexed > 0) {
568 p_indexed = (nobj_indexed * 100) / nobj_total;
569 if (p_indexed != a->last_p_indexed) {
570 a->last_p_indexed = p_indexed;
571 print_indexed = 1;
572 print_size = 1;
575 if (nobj_resolved > 0) {
576 p_resolved = (nobj_resolved * 100) /
577 (nobj_total - nobj_loose);
578 if (p_resolved != a->last_p_resolved) {
579 a->last_p_resolved = p_resolved;
580 print_resolved = 1;
581 print_indexed = 1;
582 print_size = 1;
587 if (print_size || print_indexed || print_resolved)
588 printf("\r");
589 if (print_size)
590 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
591 if (print_indexed)
592 printf("; indexing %d%%", p_indexed);
593 if (print_resolved)
594 printf("; resolving deltas %d%%", p_resolved);
595 if (print_size || print_indexed || print_resolved)
596 fflush(stdout);
598 return NULL;
601 static const struct got_error *
602 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
603 const char *refname, struct got_repository *repo)
605 const struct got_error *err;
606 struct got_reference *ref;
608 *new = NULL;
610 err = got_ref_open(&ref, repo, refname, 0);
611 if (err) {
612 if (err->code != GOT_ERR_NOT_REF)
613 return err;
615 /* Treat argument as a reference prefix. */
616 err = got_ref_list(refs, repo, refname,
617 got_ref_cmp_by_name, NULL);
618 } else {
619 err = got_reflist_insert(new, refs, ref,
620 got_ref_cmp_by_name, NULL);
621 if (err || *new == NULL /* duplicate */)
622 got_ref_close(ref);
625 return err;
628 static const struct got_error *
629 cmd_pack(int argc, char *argv[])
631 const struct got_error *error = NULL;
632 char *repo_path = NULL;
633 struct got_repository *repo = NULL;
634 int ch, i, loose_obj_only = 1, verbosity = 0;
635 struct got_object_id *pack_hash = NULL;
636 char *id_str = NULL;
637 struct got_pack_progress_arg ppa;
638 FILE *packfile = NULL;
639 struct got_pathlist_head exclude_args;
640 struct got_pathlist_entry *pe;
641 struct got_reflist_head exclude_refs;
642 struct got_reflist_head include_refs;
643 struct got_reflist_entry *re, *new;
644 int *pack_fds = NULL;
646 TAILQ_INIT(&exclude_args);
647 TAILQ_INIT(&exclude_refs);
648 TAILQ_INIT(&include_refs);
650 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
651 switch (ch) {
652 case 'a':
653 loose_obj_only = 0;
654 break;
655 case 'r':
656 repo_path = realpath(optarg, NULL);
657 if (repo_path == NULL)
658 return got_error_from_errno2("realpath",
659 optarg);
660 got_path_strip_trailing_slashes(repo_path);
661 break;
662 case 'x':
663 got_path_strip_trailing_slashes(optarg);
664 error = got_pathlist_append(&exclude_args,
665 optarg, NULL);
666 if (error)
667 return error;
668 break;
669 case 'q':
670 verbosity = -1;
671 break;
672 default:
673 usage_pack();
674 /* NOTREACHED */
678 argc -= optind;
679 argv += optind;
681 #ifndef PROFILE
682 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
683 NULL) == -1)
684 err(1, "pledge");
685 #endif
686 if (repo_path == NULL) {
687 error = get_repo_path(&repo_path);
688 if (error)
689 goto done;
691 error = got_repo_pack_fds_open(&pack_fds);
692 if (error != NULL)
693 goto done;
694 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
695 if (error)
696 goto done;
698 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
699 if (error)
700 goto done;
702 TAILQ_FOREACH(pe, &exclude_args, entry) {
703 const char *refname = pe->path;
704 error = add_ref(&new, &exclude_refs, refname, repo);
705 if (error)
706 goto done;
710 if (argc == 0) {
711 error = got_ref_list(&include_refs, repo, "",
712 got_ref_cmp_by_name, NULL);
713 if (error)
714 goto done;
715 } else {
716 for (i = 0; i < argc; i++) {
717 const char *refname;
718 got_path_strip_trailing_slashes(argv[i]);
719 refname = argv[i];
720 error = add_ref(&new, &include_refs, refname, repo);
721 if (error)
722 goto done;
726 /* Ignore references in the refs/got/ namespace. */
727 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
728 const char *refname = got_ref_get_name(re->ref);
729 if (strncmp("refs/got/", refname, 9) != 0)
730 continue;
731 TAILQ_REMOVE(&include_refs, re, entry);
732 got_ref_close(re->ref);
733 free(re);
736 memset(&ppa, 0, sizeof(ppa));
737 ppa.last_scaled_size[0] = '\0';
738 ppa.last_p_indexed = -1;
739 ppa.last_p_resolved = -1;
740 ppa.verbosity = verbosity;
742 error = got_repo_pack_objects(&packfile, &pack_hash,
743 &include_refs, &exclude_refs, repo, loose_obj_only,
744 pack_progress, &ppa, check_cancelled, NULL);
745 if (error) {
746 if (ppa.printed_something)
747 printf("\n");
748 goto done;
751 error = got_object_id_str(&id_str, pack_hash);
752 if (error)
753 goto done;
754 if (verbosity >= 0)
755 printf("\nWrote %s.pack\n", id_str);
757 error = got_repo_index_pack(packfile, pack_hash, repo,
758 pack_index_progress, &ppa, check_cancelled, NULL);
759 if (error)
760 goto done;
761 if (verbosity >= 0)
762 printf("\nIndexed %s.pack\n", id_str);
763 done:
764 if (repo)
765 got_repo_close(repo);
766 if (pack_fds) {
767 const struct got_error *pack_err =
768 got_repo_pack_fds_close(pack_fds);
769 if (error == NULL)
770 error = pack_err;
771 pack_fds = NULL;
773 got_pathlist_free(&exclude_args);
774 got_ref_list_free(&exclude_refs);
775 got_ref_list_free(&include_refs);
776 free(id_str);
777 free(pack_hash);
778 free(repo_path);
779 return error;
782 __dead static void
783 usage_indexpack(void)
785 fprintf(stderr, "usage: %s indexpack packfile-path\n",
786 getprogname());
787 exit(1);
790 static const struct got_error *
791 cmd_indexpack(int argc, char *argv[])
793 const struct got_error *error = NULL;
794 struct got_repository *repo = NULL;
795 int ch;
796 struct got_object_id *pack_hash = NULL;
797 char *packfile_path = NULL;
798 char *id_str = NULL;
799 struct got_pack_progress_arg ppa;
800 FILE *packfile = NULL;
801 int *pack_fds = NULL;
803 while ((ch = getopt(argc, argv, "")) != -1) {
804 switch (ch) {
805 default:
806 usage_indexpack();
807 /* NOTREACHED */
811 argc -= optind;
812 argv += optind;
814 if (argc != 1)
815 usage_indexpack();
817 packfile_path = realpath(argv[0], NULL);
818 if (packfile_path == NULL)
819 return got_error_from_errno2("realpath", argv[0]);
821 #ifndef PROFILE
822 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
823 NULL) == -1)
824 err(1, "pledge");
825 #endif
827 error = got_repo_pack_fds_open(&pack_fds);
828 if (error != NULL)
829 goto done;
830 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
831 if (error)
832 goto done;
834 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
835 if (error)
836 goto done;
838 memset(&ppa, 0, sizeof(ppa));
839 ppa.last_scaled_size[0] = '\0';
840 ppa.last_p_indexed = -1;
841 ppa.last_p_resolved = -1;
843 error = got_repo_find_pack(&packfile, &pack_hash, repo,
844 packfile_path);
845 if (error)
846 goto done;
848 error = got_object_id_str(&id_str, pack_hash);
849 if (error)
850 goto done;
852 error = got_repo_index_pack(packfile, pack_hash, repo,
853 pack_index_progress, &ppa, check_cancelled, NULL);
854 if (error)
855 goto done;
856 printf("\nIndexed %s.pack\n", id_str);
857 done:
858 if (repo)
859 got_repo_close(repo);
860 if (pack_fds) {
861 const struct got_error *pack_err =
862 got_repo_pack_fds_close(pack_fds);
863 if (error == NULL)
864 error = pack_err;
865 pack_fds = NULL;
867 free(id_str);
868 free(pack_hash);
869 return error;
872 __dead static void
873 usage_listpack(void)
875 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
876 getprogname());
877 exit(1);
880 struct gotadmin_list_pack_cb_args {
881 int nblobs;
882 int ntrees;
883 int ncommits;
884 int ntags;
885 int noffdeltas;
886 int nrefdeltas;
887 int human_readable;
888 };
890 static const struct got_error *
891 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
892 off_t size, off_t base_offset, struct got_object_id *base_id)
894 const struct got_error *err;
895 struct gotadmin_list_pack_cb_args *a = arg;
896 char *id_str, *delta_str = NULL, *base_id_str = NULL;
897 const char *type_str;
899 err = got_object_id_str(&id_str, id);
900 if (err)
901 return err;
903 switch (type) {
904 case GOT_OBJ_TYPE_BLOB:
905 type_str = GOT_OBJ_LABEL_BLOB;
906 a->nblobs++;
907 break;
908 case GOT_OBJ_TYPE_TREE:
909 type_str = GOT_OBJ_LABEL_TREE;
910 a->ntrees++;
911 break;
912 case GOT_OBJ_TYPE_COMMIT:
913 type_str = GOT_OBJ_LABEL_COMMIT;
914 a->ncommits++;
915 break;
916 case GOT_OBJ_TYPE_TAG:
917 type_str = GOT_OBJ_LABEL_TAG;
918 a->ntags++;
919 break;
920 case GOT_OBJ_TYPE_OFFSET_DELTA:
921 type_str = "offset-delta";
922 if (asprintf(&delta_str, " base-offset %lld",
923 (long long)base_offset) == -1) {
924 err = got_error_from_errno("asprintf");
925 goto done;
927 a->noffdeltas++;
928 break;
929 case GOT_OBJ_TYPE_REF_DELTA:
930 type_str = "ref-delta";
931 err = got_object_id_str(&base_id_str, base_id);
932 if (err)
933 goto done;
934 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
935 err = got_error_from_errno("asprintf");
936 goto done;
938 a->nrefdeltas++;
939 break;
940 default:
941 err = got_error(GOT_ERR_OBJ_TYPE);
942 goto done;
944 if (a->human_readable) {
945 char scaled[FMT_SCALED_STRSIZE];
946 char *s;;
947 if (fmt_scaled(size, scaled) == -1) {
948 err = got_error_from_errno("fmt_scaled");
949 goto done;
951 s = scaled;
952 while (isspace((unsigned char)*s))
953 s++;
954 printf("%s %s at %lld size %s%s\n", id_str, type_str,
955 (long long)offset, s, delta_str ? delta_str : "");
956 } else {
957 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
958 (long long)offset, (long long)size,
959 delta_str ? delta_str : "");
961 done:
962 free(id_str);
963 free(base_id_str);
964 free(delta_str);
965 return err;
968 static const struct got_error *
969 cmd_listpack(int argc, char *argv[])
971 const struct got_error *error = NULL;
972 struct got_repository *repo = NULL;
973 int ch;
974 struct got_object_id *pack_hash = NULL;
975 char *packfile_path = NULL;
976 char *id_str = NULL;
977 struct gotadmin_list_pack_cb_args lpa;
978 FILE *packfile = NULL;
979 int show_stats = 0, human_readable = 0;
980 int *pack_fds = NULL;
982 while ((ch = getopt(argc, argv, "hs")) != -1) {
983 switch (ch) {
984 case 'h':
985 human_readable = 1;
986 break;
987 case 's':
988 show_stats = 1;
989 break;
990 default:
991 usage_listpack();
992 /* NOTREACHED */
996 argc -= optind;
997 argv += optind;
999 if (argc != 1)
1000 usage_listpack();
1001 packfile_path = realpath(argv[0], NULL);
1002 if (packfile_path == NULL)
1003 return got_error_from_errno2("realpath", argv[0]);
1005 #ifndef PROFILE
1006 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1007 NULL) == -1)
1008 err(1, "pledge");
1009 #endif
1010 error = got_repo_pack_fds_open(&pack_fds);
1011 if (error != NULL)
1012 goto done;
1013 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1014 if (error)
1015 goto done;
1016 #ifndef PROFILE
1017 /* Remove "cpath" promise. */
1018 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1019 NULL) == -1)
1020 err(1, "pledge");
1021 #endif
1022 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1023 if (error)
1024 goto done;
1026 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1027 packfile_path);
1028 if (error)
1029 goto done;
1030 error = got_object_id_str(&id_str, pack_hash);
1031 if (error)
1032 goto done;
1034 memset(&lpa, 0, sizeof(lpa));
1035 lpa.human_readable = human_readable;
1036 error = got_repo_list_pack(packfile, pack_hash, repo,
1037 list_pack_cb, &lpa, check_cancelled, NULL);
1038 if (error)
1039 goto done;
1040 if (show_stats) {
1041 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1042 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1043 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1044 lpa.noffdeltas + lpa.nrefdeltas,
1045 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1046 lpa.noffdeltas, lpa.nrefdeltas);
1048 done:
1049 if (repo)
1050 got_repo_close(repo);
1051 if (pack_fds) {
1052 const struct got_error *pack_err =
1053 got_repo_pack_fds_close(pack_fds);
1054 if (error == NULL)
1055 error = pack_err;
1056 pack_fds = NULL;
1058 free(id_str);
1059 free(pack_hash);
1060 free(packfile_path);
1061 return error;
1064 __dead static void
1065 usage_cleanup(void)
1067 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1068 "[-q]\n", getprogname());
1069 exit(1);
1072 struct got_cleanup_progress_arg {
1073 int last_nloose;
1074 int last_ncommits;
1075 int last_npurged;
1076 int verbosity;
1077 int printed_something;
1078 int dry_run;
1081 static const struct got_error *
1082 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1084 struct got_cleanup_progress_arg *a = arg;
1085 int print_loose = 0, print_commits = 0, print_purged = 0;
1087 if (a->last_nloose != nloose) {
1088 print_loose = 1;
1089 a->last_nloose = nloose;
1091 if (a->last_ncommits != ncommits) {
1092 print_loose = 1;
1093 print_commits = 1;
1094 a->last_ncommits = ncommits;
1096 if (a->last_npurged != npurged) {
1097 print_loose = 1;
1098 print_commits = 1;
1099 print_purged = 1;
1100 a->last_npurged = npurged;
1103 if (a->verbosity < 0)
1104 return NULL;
1106 if (print_loose || print_commits || print_purged)
1107 printf("\r");
1108 if (print_loose)
1109 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1110 if (print_commits)
1111 printf("; %d commit%s scanned", ncommits,
1112 ncommits == 1 ? "" : "s");
1113 if (print_purged) {
1114 if (a->dry_run) {
1115 printf("; %d object%s could be purged", npurged,
1116 npurged == 1 ? "" : "s");
1117 } else {
1118 printf("; %d object%s purged", npurged,
1119 npurged == 1 ? "" : "s");
1122 if (print_loose || print_commits || print_purged) {
1123 a->printed_something = 1;
1124 fflush(stdout);
1126 return NULL;
1129 struct got_lonely_packidx_progress_arg {
1130 int verbosity;
1131 int printed_something;
1132 int dry_run;
1135 static const struct got_error *
1136 lonely_packidx_progress(void *arg, const char *path)
1138 struct got_lonely_packidx_progress_arg *a = arg;
1140 if (a->verbosity < 0)
1141 return NULL;
1143 if (a->dry_run)
1144 printf("%s could be removed\n", path);
1145 else
1146 printf("%s removed\n", path);
1148 a->printed_something = 1;
1149 return NULL;
1152 static const struct got_error *
1153 cmd_cleanup(int argc, char *argv[])
1155 const struct got_error *error = NULL;
1156 char *repo_path = NULL;
1157 struct got_repository *repo = NULL;
1158 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1159 int remove_lonely_packidx = 0, ignore_mtime = 0;
1160 struct got_cleanup_progress_arg cpa;
1161 struct got_lonely_packidx_progress_arg lpa;
1162 off_t size_before, size_after;
1163 char scaled_before[FMT_SCALED_STRSIZE];
1164 char scaled_after[FMT_SCALED_STRSIZE];
1165 char scaled_diff[FMT_SCALED_STRSIZE];
1166 char **extensions;
1167 int nextensions, i;
1168 int *pack_fds = NULL;
1170 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1171 switch (ch) {
1172 case 'a':
1173 ignore_mtime = 1;
1174 break;
1175 case 'p':
1176 remove_lonely_packidx = 1;
1177 break;
1178 case 'r':
1179 repo_path = realpath(optarg, NULL);
1180 if (repo_path == NULL)
1181 return got_error_from_errno2("realpath",
1182 optarg);
1183 got_path_strip_trailing_slashes(repo_path);
1184 break;
1185 case 'n':
1186 dry_run = 1;
1187 break;
1188 case 'q':
1189 verbosity = -1;
1190 break;
1191 default:
1192 usage_cleanup();
1193 /* NOTREACHED */
1197 argc -= optind;
1198 argv += optind;
1200 #ifndef PROFILE
1201 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1202 NULL) == -1)
1203 err(1, "pledge");
1204 #endif
1205 if (repo_path == NULL) {
1206 error = get_repo_path(&repo_path);
1207 if (error)
1208 goto done;
1210 error = got_repo_pack_fds_open(&pack_fds);
1211 if (error != NULL)
1212 goto done;
1213 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1214 if (error)
1215 goto done;
1217 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1218 if (error)
1219 goto done;
1221 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1222 repo);
1223 for (i = 0; i < nextensions; i++) {
1224 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1225 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1226 "the preciousObjects Git extension is enabled; "
1227 "this implies that objects must not be deleted");
1228 goto done;
1232 if (remove_lonely_packidx) {
1233 memset(&lpa, 0, sizeof(lpa));
1234 lpa.dry_run = dry_run;
1235 lpa.verbosity = verbosity;
1236 error = got_repo_remove_lonely_packidx(repo, dry_run,
1237 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1238 goto done;
1241 memset(&cpa, 0, sizeof(cpa));
1242 cpa.last_ncommits = -1;
1243 cpa.last_npurged = -1;
1244 cpa.dry_run = dry_run;
1245 cpa.verbosity = verbosity;
1246 error = got_repo_purge_unreferenced_loose_objects(repo,
1247 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1248 cleanup_progress, &cpa, check_cancelled, NULL);
1249 if (cpa.printed_something)
1250 printf("\n");
1251 if (error)
1252 goto done;
1253 if (cpa.printed_something) {
1254 if (fmt_scaled(size_before, scaled_before) == -1) {
1255 error = got_error_from_errno("fmt_scaled");
1256 goto done;
1258 if (fmt_scaled(size_after, scaled_after) == -1) {
1259 error = got_error_from_errno("fmt_scaled");
1260 goto done;
1262 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1263 error = got_error_from_errno("fmt_scaled");
1264 goto done;
1266 printf("loose total size before: %s\n", scaled_before);
1267 printf("loose total size after: %s\n", scaled_after);
1268 if (dry_run) {
1269 printf("disk space which would be freed: %s\n",
1270 scaled_diff);
1271 } else
1272 printf("disk space freed: %s\n", scaled_diff);
1273 printf("loose objects also found in pack files: %d\n", npacked);
1275 done:
1276 if (repo)
1277 got_repo_close(repo);
1278 if (pack_fds) {
1279 const struct got_error *pack_err =
1280 got_repo_pack_fds_close(pack_fds);
1281 if (error == NULL)
1282 error = pack_err;
1283 pack_fds = NULL;
1285 free(repo_path);
1286 return error;