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 <sha2.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <util.h>
35 #include "got_version.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_cancel.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_gotconfig.h"
43 #include "got_path.h"
44 #include "got_privsep.h"
45 #include "got_opentemp.h"
46 #include "got_worktree.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
55 static void
56 catch_sigint(int signo)
57 {
58 sigint_received = 1;
59 }
61 static void
62 catch_sigpipe(int signo)
63 {
64 sigpipe_received = 1;
65 }
67 static const struct got_error *
68 check_cancelled(void *arg)
69 {
70 if (sigint_received || sigpipe_received)
71 return got_error(GOT_ERR_CANCELLED);
72 return NULL;
73 }
75 struct gotadmin_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int, int);
83 __dead static void usage_init(void);
84 __dead static void usage_info(void);
85 __dead static void usage_pack(void);
86 __dead static void usage_indexpack(void);
87 __dead static void usage_listpack(void);
88 __dead static void usage_cleanup(void);
90 static const struct got_error* cmd_init(int, char *[]);
91 static const struct got_error* cmd_info(int, char *[]);
92 static const struct got_error* cmd_pack(int, char *[]);
93 static const struct got_error* cmd_indexpack(int, char *[]);
94 static const struct got_error* cmd_listpack(int, char *[]);
95 static const struct got_error* cmd_cleanup(int, char *[]);
97 static const struct gotadmin_cmd gotadmin_commands[] = {
98 { "init", cmd_init, usage_init, "" },
99 { "info", cmd_info, usage_info, "" },
100 { "pack", cmd_pack, usage_pack, "" },
101 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
102 { "listpack", cmd_listpack, usage_listpack, "ls" },
103 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
104 };
106 static void
107 list_commands(FILE *fp)
109 size_t i;
111 fprintf(fp, "commands:");
112 for (i = 0; i < nitems(gotadmin_commands); i++) {
113 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
114 fprintf(fp, " %s", cmd->cmd_name);
116 fputc('\n', fp);
119 int
120 main(int argc, char *argv[])
122 const struct gotadmin_cmd *cmd;
123 size_t i;
124 int ch;
125 int hflag = 0, Vflag = 0;
126 static const struct option longopts[] = {
127 { "version", no_argument, NULL, 'V' },
128 { NULL, 0, NULL, 0 }
129 };
131 setlocale(LC_CTYPE, "");
133 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
134 switch (ch) {
135 case 'h':
136 hflag = 1;
137 break;
138 case 'V':
139 Vflag = 1;
140 break;
141 default:
142 usage(hflag, 1);
143 /* NOTREACHED */
147 argc -= optind;
148 argv += optind;
149 optind = 1;
150 optreset = 1;
152 if (Vflag) {
153 got_version_print_str();
154 return 0;
157 if (argc <= 0)
158 usage(hflag, hflag ? 0 : 1);
160 signal(SIGINT, catch_sigint);
161 signal(SIGPIPE, catch_sigpipe);
163 for (i = 0; i < nitems(gotadmin_commands); i++) {
164 const struct got_error *error;
166 cmd = &gotadmin_commands[i];
168 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
169 strcmp(cmd->cmd_alias, argv[0]) != 0)
170 continue;
172 if (hflag)
173 cmd->cmd_usage();
175 error = cmd->cmd_main(argc, argv);
176 if (error && error->code != GOT_ERR_CANCELLED &&
177 error->code != GOT_ERR_PRIVSEP_EXIT &&
178 !(sigpipe_received &&
179 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
180 !(sigint_received &&
181 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
183 return 1;
186 return 0;
189 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 list_commands(stderr);
191 return 1;
194 __dead static void
195 usage(int hflag, int status)
197 FILE *fp = (status == 0) ? stdout : stderr;
199 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
200 getprogname());
201 if (hflag)
202 list_commands(fp);
203 exit(status);
206 static const struct got_error *
207 apply_unveil(const char *repo_path, int repo_read_only)
209 const struct got_error *err;
211 #ifdef PROFILE
212 if (unveil("gmon.out", "rwc") != 0)
213 return got_error_from_errno2("unveil", "gmon.out");
214 #endif
215 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
216 return got_error_from_errno2("unveil", repo_path);
218 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
219 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
221 err = got_privsep_unveil_exec_helpers();
222 if (err != NULL)
223 return err;
225 if (unveil(NULL, NULL) != 0)
226 return got_error_from_errno("unveil");
228 return NULL;
231 __dead static void
232 usage_info(void)
234 fprintf(stderr, "usage: %s info [-r repository-path]\n",
235 getprogname());
236 exit(1);
239 static const struct got_error *
240 get_repo_path(char **repo_path)
242 const struct got_error *err = NULL;
243 struct got_worktree *worktree = NULL;
244 char *cwd;
246 *repo_path = NULL;
248 cwd = getcwd(NULL, 0);
249 if (cwd == NULL)
250 return got_error_from_errno("getcwd");
252 err = got_worktree_open(&worktree, cwd);
253 if (err) {
254 if (err->code != GOT_ERR_NOT_WORKTREE)
255 goto done;
256 err = NULL;
259 if (worktree)
260 *repo_path = strdup(got_worktree_get_repo_path(worktree));
261 else
262 *repo_path = strdup(cwd);
263 if (*repo_path == NULL)
264 err = got_error_from_errno("strdup");
265 done:
266 if (worktree)
267 got_worktree_close(worktree);
268 free(cwd);
269 return err;
272 __dead static void
273 usage_init(void)
275 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
276 getprogname());
277 exit(1);
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 const char *head_name = NULL;
285 char *repo_path = NULL;
286 int ch;
288 #ifndef PROFILE
289 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
290 err(1, "pledge");
291 #endif
293 while ((ch = getopt(argc, argv, "b:")) != -1) {
294 switch (ch) {
295 case 'b':
296 head_name = optarg;
297 break;
298 default:
299 usage_init();
300 /* NOTREACHED */
304 argc -= optind;
305 argv += optind;
307 if (argc != 1)
308 usage_init();
310 repo_path = strdup(argv[0]);
311 if (repo_path == NULL)
312 return got_error_from_errno("strdup");
314 got_path_strip_trailing_slashes(repo_path);
316 error = got_path_mkdir(repo_path);
317 if (error &&
318 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
319 goto done;
321 error = apply_unveil(repo_path, 0);
322 if (error)
323 goto done;
325 error = got_repo_init(repo_path, head_name);
326 done:
327 free(repo_path);
328 return error;
331 static const struct got_error *
332 cmd_info(int argc, char *argv[])
334 const struct got_error *error = NULL;
335 char *repo_path = NULL;
336 struct got_repository *repo = NULL;
337 const struct got_gotconfig *gotconfig = NULL;
338 int ch, npackfiles, npackedobj, nobj;
339 off_t packsize, loose_size;
340 char scaled[FMT_SCALED_STRSIZE];
341 int *pack_fds = NULL;
343 #ifndef PROFILE
344 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
345 NULL) == -1)
346 err(1, "pledge");
347 #endif
349 while ((ch = getopt(argc, argv, "r:")) != -1) {
350 switch (ch) {
351 case 'r':
352 repo_path = realpath(optarg, NULL);
353 if (repo_path == NULL)
354 return got_error_from_errno2("realpath",
355 optarg);
356 got_path_strip_trailing_slashes(repo_path);
357 break;
358 default:
359 usage_info();
360 /* NOTREACHED */
364 argc -= optind;
365 argv += optind;
367 if (repo_path == NULL) {
368 error = get_repo_path(&repo_path);
369 if (error)
370 goto done;
372 error = got_repo_pack_fds_open(&pack_fds);
373 if (error != NULL)
374 goto done;
375 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
376 if (error)
377 goto done;
378 #ifndef PROFILE
379 /* Remove "cpath" promise. */
380 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
381 NULL) == -1)
382 err(1, "pledge");
383 #endif
384 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
385 if (error)
386 goto done;
388 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
390 gotconfig = got_repo_get_gotconfig(repo);
391 if (gotconfig) {
392 const struct got_remote_repo *remotes;
393 int i, nremotes;
394 if (got_gotconfig_get_author(gotconfig)) {
395 printf("default author: %s\n",
396 got_gotconfig_get_author(gotconfig));
398 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
399 for (i = 0; i < nremotes; i++) {
400 const char *fetch_url = remotes[i].fetch_url;
401 const char *send_url = remotes[i].send_url;
402 if (strcmp(fetch_url, send_url) == 0) {
403 printf("remote \"%s\": %s\n", remotes[i].name,
404 remotes[i].fetch_url);
405 } else {
406 printf("remote \"%s\" (fetch): %s\n",
407 remotes[i].name, remotes[i].fetch_url);
408 printf("remote \"%s\" (send): %s\n",
409 remotes[i].name, remotes[i].send_url);
414 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
415 &packsize, repo);
416 if (error)
417 goto done;
418 printf("pack files: %d\n", npackfiles);
419 if (npackfiles > 0) {
420 if (fmt_scaled(packsize, scaled) == -1) {
421 error = got_error_from_errno("fmt_scaled");
422 goto done;
424 printf("packed objects: %d\n", npackedobj);
425 printf("packed total size: %s\n", scaled);
428 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
429 if (error)
430 goto done;
431 printf("loose objects: %d\n", nobj);
432 if (nobj > 0) {
433 if (fmt_scaled(loose_size, scaled) == -1) {
434 error = got_error_from_errno("fmt_scaled");
435 goto done;
437 printf("loose total size: %s\n", scaled);
439 done:
440 if (repo)
441 got_repo_close(repo);
442 if (pack_fds) {
443 const struct got_error *pack_err =
444 got_repo_pack_fds_close(pack_fds);
445 if (error == NULL)
446 error = pack_err;
449 free(repo_path);
450 return error;
453 __dead static void
454 usage_pack(void)
456 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
457 "[-x reference] [reference ...]\n", getprogname());
458 exit(1);
461 struct got_pack_progress_arg {
462 char last_scaled_size[FMT_SCALED_STRSIZE];
463 int last_ncolored;
464 int last_nfound;
465 int last_ntrees;
466 int loading_done;
467 int last_ncommits;
468 int last_nobj_total;
469 int last_p_deltify;
470 int last_p_written;
471 int last_p_indexed;
472 int last_p_resolved;
473 int verbosity;
474 int printed_something;
475 };
477 static void
478 print_load_info(int print_colored, int print_found, int print_trees,
479 int ncolored, int nfound, int ntrees)
481 if (print_colored) {
482 printf("%d commit%s colored", ncolored,
483 ncolored == 1 ? "" : "s");
485 if (print_found) {
486 printf("%s%d object%s found",
487 ncolored > 0 ? "; " : "",
488 nfound, nfound == 1 ? "" : "s");
490 if (print_trees) {
491 printf("; %d tree%s scanned", ntrees,
492 ntrees == 1 ? "" : "s");
496 static const struct got_error *
497 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
498 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
499 int nobj_written)
501 struct got_pack_progress_arg *a = arg;
502 char scaled_size[FMT_SCALED_STRSIZE];
503 int p_deltify, p_written;
504 int print_colored = 0, print_found = 0, print_trees = 0;
505 int print_searching = 0, print_total = 0;
506 int print_deltify = 0, print_written = 0;
508 if (a->verbosity < 0)
509 return NULL;
511 if (a->last_ncolored != ncolored) {
512 print_colored = 1;
513 a->last_ncolored = ncolored;
516 if (a->last_nfound != nfound) {
517 print_colored = 1;
518 print_found = 1;
519 a->last_nfound = nfound;
522 if (a->last_ntrees != ntrees) {
523 print_colored = 1;
524 print_found = 1;
525 print_trees = 1;
526 a->last_ntrees = ntrees;
529 if ((print_colored || print_found || print_trees) &&
530 !a->loading_done) {
531 printf("\r");
532 print_load_info(print_colored, print_found, print_trees,
533 ncolored, nfound, ntrees);
534 a->printed_something = 1;
535 fflush(stdout);
536 return NULL;
537 } else if (!a->loading_done) {
538 printf("\r");
539 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
540 printf("\n");
541 a->loading_done = 1;
544 if (fmt_scaled(packfile_size, scaled_size) == -1)
545 return got_error_from_errno("fmt_scaled");
547 if (a->last_ncommits != ncommits) {
548 print_searching = 1;
549 a->last_ncommits = ncommits;
552 if (a->last_nobj_total != nobj_total) {
553 print_searching = 1;
554 print_total = 1;
555 a->last_nobj_total = nobj_total;
558 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
559 strcmp(scaled_size, a->last_scaled_size)) != 0) {
560 if (strlcpy(a->last_scaled_size, scaled_size,
561 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
562 return got_error(GOT_ERR_NO_SPACE);
565 if (nobj_deltify > 0 || nobj_written > 0) {
566 if (nobj_deltify > 0) {
567 p_deltify = (nobj_deltify * 100) / nobj_total;
568 if (p_deltify != a->last_p_deltify) {
569 a->last_p_deltify = p_deltify;
570 print_searching = 1;
571 print_total = 1;
572 print_deltify = 1;
575 if (nobj_written > 0) {
576 p_written = (nobj_written * 100) / nobj_total;
577 if (p_written != a->last_p_written) {
578 a->last_p_written = p_written;
579 print_searching = 1;
580 print_total = 1;
581 print_deltify = 1;
582 print_written = 1;
587 if (print_searching || print_total || print_deltify || print_written)
588 printf("\r");
589 if (print_searching)
590 printf("packing %d reference%s", ncommits,
591 ncommits == 1 ? "" : "s");
592 if (print_total)
593 printf("; %d object%s", nobj_total,
594 nobj_total == 1 ? "" : "s");
595 if (print_deltify)
596 printf("; deltify: %d%%", p_deltify);
597 if (print_written)
598 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
599 scaled_size, p_written);
600 if (print_searching || print_total || print_deltify ||
601 print_written) {
602 a->printed_something = 1;
603 fflush(stdout);
605 return NULL;
608 static const struct got_error *
609 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
610 int nobj_indexed, int nobj_loose, int nobj_resolved)
612 struct got_pack_progress_arg *a = arg;
613 char scaled_size[FMT_SCALED_STRSIZE];
614 int p_indexed, p_resolved;
615 int print_size = 0, print_indexed = 0, print_resolved = 0;
617 if (a->verbosity < 0)
618 return NULL;
620 if (packfile_size > 0 || nobj_indexed > 0) {
621 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
622 (a->last_scaled_size[0] == '\0' ||
623 strcmp(scaled_size, a->last_scaled_size)) != 0) {
624 print_size = 1;
625 if (strlcpy(a->last_scaled_size, scaled_size,
626 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
627 return got_error(GOT_ERR_NO_SPACE);
629 if (nobj_indexed > 0) {
630 p_indexed = (nobj_indexed * 100) / nobj_total;
631 if (p_indexed != a->last_p_indexed) {
632 a->last_p_indexed = p_indexed;
633 print_indexed = 1;
634 print_size = 1;
637 if (nobj_resolved > 0) {
638 p_resolved = (nobj_resolved * 100) /
639 (nobj_total - nobj_loose);
640 if (p_resolved != a->last_p_resolved) {
641 a->last_p_resolved = p_resolved;
642 print_resolved = 1;
643 print_indexed = 1;
644 print_size = 1;
649 if (print_size || print_indexed || print_resolved)
650 printf("\r");
651 if (print_size)
652 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
653 if (print_indexed)
654 printf("; indexing %d%%", p_indexed);
655 if (print_resolved)
656 printf("; resolving deltas %d%%", p_resolved);
657 if (print_size || print_indexed || print_resolved)
658 fflush(stdout);
660 return NULL;
663 static const struct got_error *
664 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
665 const char *refname, struct got_repository *repo)
667 const struct got_error *err;
668 struct got_reference *ref;
670 *new = NULL;
672 err = got_ref_open(&ref, repo, refname, 0);
673 if (err) {
674 if (err->code != GOT_ERR_NOT_REF)
675 return err;
677 /* Treat argument as a reference prefix. */
678 err = got_ref_list(refs, repo, refname,
679 got_ref_cmp_by_name, NULL);
680 } else {
681 err = got_reflist_insert(new, refs, ref,
682 got_ref_cmp_by_name, NULL);
683 if (err || *new == NULL /* duplicate */)
684 got_ref_close(ref);
687 return err;
690 static const struct got_error *
691 cmd_pack(int argc, char *argv[])
693 const struct got_error *error = NULL;
694 char *repo_path = NULL;
695 struct got_repository *repo = NULL;
696 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
697 struct got_object_id *pack_hash = NULL;
698 char *id_str = NULL;
699 struct got_pack_progress_arg ppa;
700 FILE *packfile = NULL;
701 struct got_pathlist_head exclude_args;
702 struct got_pathlist_entry *pe;
703 struct got_reflist_head exclude_refs;
704 struct got_reflist_head include_refs;
705 struct got_reflist_entry *re, *new;
706 int *pack_fds = NULL;
708 TAILQ_INIT(&exclude_args);
709 TAILQ_INIT(&exclude_refs);
710 TAILQ_INIT(&include_refs);
712 #ifndef PROFILE
713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
714 NULL) == -1)
715 err(1, "pledge");
716 #endif
718 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
719 switch (ch) {
720 case 'a':
721 loose_obj_only = 0;
722 break;
723 case 'D':
724 force_refdelta = 1;
725 break;
726 case 'q':
727 verbosity = -1;
728 break;
729 case 'r':
730 repo_path = realpath(optarg, NULL);
731 if (repo_path == NULL)
732 return got_error_from_errno2("realpath",
733 optarg);
734 got_path_strip_trailing_slashes(repo_path);
735 break;
736 case 'x':
737 got_path_strip_trailing_slashes(optarg);
738 error = got_pathlist_append(&exclude_args,
739 optarg, NULL);
740 if (error)
741 return error;
742 break;
743 default:
744 usage_pack();
745 /* NOTREACHED */
749 argc -= optind;
750 argv += optind;
752 if (repo_path == NULL) {
753 error = get_repo_path(&repo_path);
754 if (error)
755 goto done;
757 error = got_repo_pack_fds_open(&pack_fds);
758 if (error != NULL)
759 goto done;
760 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
761 if (error)
762 goto done;
764 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
765 if (error)
766 goto done;
768 TAILQ_FOREACH(pe, &exclude_args, entry) {
769 const char *refname = pe->path;
770 error = add_ref(&new, &exclude_refs, refname, repo);
771 if (error)
772 goto done;
775 if (argc == 0) {
776 error = got_ref_list(&include_refs, repo, "",
777 got_ref_cmp_by_name, NULL);
778 if (error)
779 goto done;
780 } else {
781 for (i = 0; i < argc; i++) {
782 const char *refname;
783 got_path_strip_trailing_slashes(argv[i]);
784 refname = argv[i];
785 error = add_ref(&new, &include_refs, refname, repo);
786 if (error)
787 goto done;
791 /* Ignore references in the refs/got/ namespace. */
792 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
793 const char *refname = got_ref_get_name(re->ref);
794 if (strncmp("refs/got/", refname, 9) != 0)
795 continue;
796 TAILQ_REMOVE(&include_refs, re, entry);
797 got_ref_close(re->ref);
798 free(re);
801 memset(&ppa, 0, sizeof(ppa));
802 ppa.last_scaled_size[0] = '\0';
803 ppa.last_p_indexed = -1;
804 ppa.last_p_resolved = -1;
805 ppa.verbosity = verbosity;
807 error = got_repo_pack_objects(&packfile, &pack_hash,
808 &include_refs, &exclude_refs, repo, loose_obj_only,
809 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
810 if (error) {
811 if (ppa.printed_something)
812 printf("\n");
813 goto done;
816 error = got_object_id_str(&id_str, pack_hash);
817 if (error)
818 goto done;
819 if (verbosity >= 0)
820 printf("\nWrote %s.pack\n", id_str);
822 error = got_repo_index_pack(packfile, pack_hash, repo,
823 pack_index_progress, &ppa, check_cancelled, NULL);
824 if (error)
825 goto done;
826 if (verbosity >= 0)
827 printf("\nIndexed %s.pack\n", id_str);
828 done:
829 if (repo)
830 got_repo_close(repo);
831 if (pack_fds) {
832 const struct got_error *pack_err =
833 got_repo_pack_fds_close(pack_fds);
834 if (error == NULL)
835 error = pack_err;
837 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
838 got_ref_list_free(&exclude_refs);
839 got_ref_list_free(&include_refs);
840 free(id_str);
841 free(pack_hash);
842 free(repo_path);
843 return error;
846 __dead static void
847 usage_indexpack(void)
849 fprintf(stderr, "usage: %s indexpack packfile-path\n",
850 getprogname());
851 exit(1);
854 static const struct got_error *
855 cmd_indexpack(int argc, char *argv[])
857 const struct got_error *error = NULL;
858 struct got_repository *repo = NULL;
859 int ch;
860 struct got_object_id *pack_hash = NULL;
861 char *packfile_path = NULL;
862 char *id_str = NULL;
863 struct got_pack_progress_arg ppa;
864 FILE *packfile = NULL;
865 int *pack_fds = NULL;
867 while ((ch = getopt(argc, argv, "")) != -1) {
868 switch (ch) {
869 default:
870 usage_indexpack();
871 /* NOTREACHED */
875 argc -= optind;
876 argv += optind;
878 if (argc != 1)
879 usage_indexpack();
881 packfile_path = realpath(argv[0], NULL);
882 if (packfile_path == NULL)
883 return got_error_from_errno2("realpath", argv[0]);
885 #ifndef PROFILE
886 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
887 NULL) == -1)
888 err(1, "pledge");
889 #endif
891 error = got_repo_pack_fds_open(&pack_fds);
892 if (error != NULL)
893 goto done;
894 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
895 if (error)
896 goto done;
898 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
899 if (error)
900 goto done;
902 memset(&ppa, 0, sizeof(ppa));
903 ppa.last_scaled_size[0] = '\0';
904 ppa.last_p_indexed = -1;
905 ppa.last_p_resolved = -1;
907 error = got_repo_find_pack(&packfile, &pack_hash, repo,
908 packfile_path);
909 if (error)
910 goto done;
912 error = got_object_id_str(&id_str, pack_hash);
913 if (error)
914 goto done;
916 error = got_repo_index_pack(packfile, pack_hash, repo,
917 pack_index_progress, &ppa, check_cancelled, NULL);
918 if (error)
919 goto done;
920 printf("\nIndexed %s.pack\n", id_str);
921 done:
922 if (repo)
923 got_repo_close(repo);
924 if (pack_fds) {
925 const struct got_error *pack_err =
926 got_repo_pack_fds_close(pack_fds);
927 if (error == NULL)
928 error = pack_err;
930 free(id_str);
931 free(pack_hash);
932 return error;
935 __dead static void
936 usage_listpack(void)
938 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
939 getprogname());
940 exit(1);
943 struct gotadmin_list_pack_cb_args {
944 int nblobs;
945 int ntrees;
946 int ncommits;
947 int ntags;
948 int noffdeltas;
949 int nrefdeltas;
950 int human_readable;
951 };
953 static const struct got_error *
954 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
955 off_t size, off_t base_offset, struct got_object_id *base_id)
957 const struct got_error *err;
958 struct gotadmin_list_pack_cb_args *a = arg;
959 char *id_str, *delta_str = NULL, *base_id_str = NULL;
960 const char *type_str;
962 err = got_object_id_str(&id_str, id);
963 if (err)
964 return err;
966 switch (type) {
967 case GOT_OBJ_TYPE_BLOB:
968 type_str = GOT_OBJ_LABEL_BLOB;
969 a->nblobs++;
970 break;
971 case GOT_OBJ_TYPE_TREE:
972 type_str = GOT_OBJ_LABEL_TREE;
973 a->ntrees++;
974 break;
975 case GOT_OBJ_TYPE_COMMIT:
976 type_str = GOT_OBJ_LABEL_COMMIT;
977 a->ncommits++;
978 break;
979 case GOT_OBJ_TYPE_TAG:
980 type_str = GOT_OBJ_LABEL_TAG;
981 a->ntags++;
982 break;
983 case GOT_OBJ_TYPE_OFFSET_DELTA:
984 type_str = "offset-delta";
985 if (asprintf(&delta_str, " base-offset %lld",
986 (long long)base_offset) == -1) {
987 err = got_error_from_errno("asprintf");
988 goto done;
990 a->noffdeltas++;
991 break;
992 case GOT_OBJ_TYPE_REF_DELTA:
993 type_str = "ref-delta";
994 err = got_object_id_str(&base_id_str, base_id);
995 if (err)
996 goto done;
997 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
998 err = got_error_from_errno("asprintf");
999 goto done;
1001 a->nrefdeltas++;
1002 break;
1003 default:
1004 err = got_error(GOT_ERR_OBJ_TYPE);
1005 goto done;
1007 if (a->human_readable) {
1008 char scaled[FMT_SCALED_STRSIZE];
1009 char *s;;
1010 if (fmt_scaled(size, scaled) == -1) {
1011 err = got_error_from_errno("fmt_scaled");
1012 goto done;
1014 s = scaled;
1015 while (isspace((unsigned char)*s))
1016 s++;
1017 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1018 (long long)offset, s, delta_str ? delta_str : "");
1019 } else {
1020 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1021 (long long)offset, (long long)size,
1022 delta_str ? delta_str : "");
1024 done:
1025 free(id_str);
1026 free(base_id_str);
1027 free(delta_str);
1028 return err;
1031 static const struct got_error *
1032 cmd_listpack(int argc, char *argv[])
1034 const struct got_error *error = NULL;
1035 struct got_repository *repo = NULL;
1036 int ch;
1037 struct got_object_id *pack_hash = NULL;
1038 char *packfile_path = NULL;
1039 char *id_str = NULL;
1040 struct gotadmin_list_pack_cb_args lpa;
1041 FILE *packfile = NULL;
1042 int show_stats = 0, human_readable = 0;
1043 int *pack_fds = NULL;
1045 #ifndef PROFILE
1046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1047 NULL) == -1)
1048 err(1, "pledge");
1049 #endif
1051 while ((ch = getopt(argc, argv, "hs")) != -1) {
1052 switch (ch) {
1053 case 'h':
1054 human_readable = 1;
1055 break;
1056 case 's':
1057 show_stats = 1;
1058 break;
1059 default:
1060 usage_listpack();
1061 /* NOTREACHED */
1065 argc -= optind;
1066 argv += optind;
1068 if (argc != 1)
1069 usage_listpack();
1070 packfile_path = realpath(argv[0], NULL);
1071 if (packfile_path == NULL)
1072 return got_error_from_errno2("realpath", argv[0]);
1074 error = got_repo_pack_fds_open(&pack_fds);
1075 if (error != NULL)
1076 goto done;
1077 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1078 if (error)
1079 goto done;
1080 #ifndef PROFILE
1081 /* Remove "cpath" promise. */
1082 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1083 NULL) == -1)
1084 err(1, "pledge");
1085 #endif
1086 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1087 if (error)
1088 goto done;
1090 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1091 packfile_path);
1092 if (error)
1093 goto done;
1094 error = got_object_id_str(&id_str, pack_hash);
1095 if (error)
1096 goto done;
1098 memset(&lpa, 0, sizeof(lpa));
1099 lpa.human_readable = human_readable;
1100 error = got_repo_list_pack(packfile, pack_hash, repo,
1101 list_pack_cb, &lpa, check_cancelled, NULL);
1102 if (error)
1103 goto done;
1104 if (show_stats) {
1105 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1106 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1107 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1108 lpa.noffdeltas + lpa.nrefdeltas,
1109 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1110 lpa.noffdeltas, lpa.nrefdeltas);
1112 done:
1113 if (repo)
1114 got_repo_close(repo);
1115 if (pack_fds) {
1116 const struct got_error *pack_err =
1117 got_repo_pack_fds_close(pack_fds);
1118 if (error == NULL)
1119 error = pack_err;
1121 free(id_str);
1122 free(pack_hash);
1123 free(packfile_path);
1124 return error;
1127 __dead static void
1128 usage_cleanup(void)
1130 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1131 getprogname());
1132 exit(1);
1135 struct got_cleanup_progress_arg {
1136 int last_nloose;
1137 int last_ncommits;
1138 int last_npurged;
1139 int verbosity;
1140 int printed_something;
1141 int dry_run;
1144 static const struct got_error *
1145 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1147 struct got_cleanup_progress_arg *a = arg;
1148 int print_loose = 0, print_commits = 0, print_purged = 0;
1150 if (a->last_nloose != nloose) {
1151 print_loose = 1;
1152 a->last_nloose = nloose;
1154 if (a->last_ncommits != ncommits) {
1155 print_loose = 1;
1156 print_commits = 1;
1157 a->last_ncommits = ncommits;
1159 if (a->last_npurged != npurged) {
1160 print_loose = 1;
1161 print_commits = 1;
1162 print_purged = 1;
1163 a->last_npurged = npurged;
1166 if (a->verbosity < 0)
1167 return NULL;
1169 if (print_loose || print_commits || print_purged)
1170 printf("\r");
1171 if (print_loose)
1172 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1173 if (print_commits)
1174 printf("; %d commit%s scanned", ncommits,
1175 ncommits == 1 ? "" : "s");
1176 if (print_purged) {
1177 if (a->dry_run) {
1178 printf("; %d object%s could be purged", npurged,
1179 npurged == 1 ? "" : "s");
1180 } else {
1181 printf("; %d object%s purged", npurged,
1182 npurged == 1 ? "" : "s");
1185 if (print_loose || print_commits || print_purged) {
1186 a->printed_something = 1;
1187 fflush(stdout);
1189 return NULL;
1192 struct got_lonely_packidx_progress_arg {
1193 int verbosity;
1194 int printed_something;
1195 int dry_run;
1198 static const struct got_error *
1199 lonely_packidx_progress(void *arg, const char *path)
1201 struct got_lonely_packidx_progress_arg *a = arg;
1203 if (a->verbosity < 0)
1204 return NULL;
1206 if (a->dry_run)
1207 printf("%s could be removed\n", path);
1208 else
1209 printf("%s removed\n", path);
1211 a->printed_something = 1;
1212 return NULL;
1215 static const struct got_error *
1216 cmd_cleanup(int argc, char *argv[])
1218 const struct got_error *error = NULL;
1219 char *repo_path = NULL;
1220 struct got_repository *repo = NULL;
1221 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1222 int remove_lonely_packidx = 0, ignore_mtime = 0;
1223 struct got_cleanup_progress_arg cpa;
1224 struct got_lonely_packidx_progress_arg lpa;
1225 off_t size_before, size_after;
1226 char scaled_before[FMT_SCALED_STRSIZE];
1227 char scaled_after[FMT_SCALED_STRSIZE];
1228 char scaled_diff[FMT_SCALED_STRSIZE];
1229 int *pack_fds = NULL;
1231 #ifndef PROFILE
1232 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1233 NULL) == -1)
1234 err(1, "pledge");
1235 #endif
1237 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1238 switch (ch) {
1239 case 'a':
1240 ignore_mtime = 1;
1241 break;
1242 case 'n':
1243 dry_run = 1;
1244 break;
1245 case 'p':
1246 remove_lonely_packidx = 1;
1247 break;
1248 case 'q':
1249 verbosity = -1;
1250 break;
1251 case 'r':
1252 repo_path = realpath(optarg, NULL);
1253 if (repo_path == NULL)
1254 return got_error_from_errno2("realpath",
1255 optarg);
1256 got_path_strip_trailing_slashes(repo_path);
1257 break;
1258 default:
1259 usage_cleanup();
1260 /* NOTREACHED */
1264 argc -= optind;
1265 argv += optind;
1267 if (repo_path == NULL) {
1268 error = get_repo_path(&repo_path);
1269 if (error)
1270 goto done;
1272 error = got_repo_pack_fds_open(&pack_fds);
1273 if (error != NULL)
1274 goto done;
1275 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1276 if (error)
1277 goto done;
1279 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1280 if (error)
1281 goto done;
1283 if (got_repo_has_extension(repo, "preciousObjects")) {
1284 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1285 "the preciousObjects Git extension is enabled; "
1286 "this implies that objects must not be deleted");
1287 goto done;
1290 if (remove_lonely_packidx) {
1291 memset(&lpa, 0, sizeof(lpa));
1292 lpa.dry_run = dry_run;
1293 lpa.verbosity = verbosity;
1294 error = got_repo_remove_lonely_packidx(repo, dry_run,
1295 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1296 goto done;
1299 memset(&cpa, 0, sizeof(cpa));
1300 cpa.last_ncommits = -1;
1301 cpa.last_npurged = -1;
1302 cpa.dry_run = dry_run;
1303 cpa.verbosity = verbosity;
1304 error = got_repo_purge_unreferenced_loose_objects(repo,
1305 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1306 cleanup_progress, &cpa, check_cancelled, NULL);
1307 if (cpa.printed_something)
1308 printf("\n");
1309 if (error)
1310 goto done;
1311 if (cpa.printed_something) {
1312 if (fmt_scaled(size_before, scaled_before) == -1) {
1313 error = got_error_from_errno("fmt_scaled");
1314 goto done;
1316 if (fmt_scaled(size_after, scaled_after) == -1) {
1317 error = got_error_from_errno("fmt_scaled");
1318 goto done;
1320 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1321 error = got_error_from_errno("fmt_scaled");
1322 goto done;
1324 printf("loose total size before: %s\n", scaled_before);
1325 printf("loose total size after: %s\n", scaled_after);
1326 if (dry_run) {
1327 printf("disk space which would be freed: %s\n",
1328 scaled_diff);
1329 } else
1330 printf("disk space freed: %s\n", scaled_diff);
1331 printf("loose objects also found in pack files: %d\n", npacked);
1333 done:
1334 if (repo)
1335 got_repo_close(repo);
1336 if (pack_fds) {
1337 const struct got_error *pack_err =
1338 got_repo_pack_fds_close(pack_fds);
1339 if (error == NULL)
1340 error = pack_err;
1342 free(repo_path);
1343 return error;