Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
19 #include <ctype.h>
20 #include <getopt.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_compat.h"
33 #include "got_version.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_cancel.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_gotconfig.h"
41 #include "got_path.h"
42 #include "got_privsep.h"
43 #include "got_opentemp.h"
44 #include "got_worktree.h"
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static void
60 catch_sigpipe(int signo)
61 {
62 sigpipe_received = 1;
63 }
65 static const struct got_error *
66 check_cancelled(void *arg)
67 {
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
70 return NULL;
71 }
73 struct gotadmin_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int, int);
81 __dead static void usage_init(void);
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_init(int, char *[]);
89 static const struct got_error* cmd_info(int, char *[]);
90 static const struct got_error* cmd_pack(int, char *[]);
91 static const struct got_error* cmd_indexpack(int, char *[]);
92 static const struct got_error* cmd_listpack(int, char *[]);
93 static const struct got_error* cmd_cleanup(int, char *[]);
95 static const struct gotadmin_cmd gotadmin_commands[] = {
96 { "init", cmd_init, usage_init, "" },
97 { "info", cmd_info, usage_info, "" },
98 { "pack", cmd_pack, usage_pack, "" },
99 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
100 { "listpack", cmd_listpack, usage_listpack, "ls" },
101 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
102 };
104 static void
105 list_commands(FILE *fp)
107 size_t i;
109 fprintf(fp, "commands:");
110 for (i = 0; i < nitems(gotadmin_commands); i++) {
111 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
112 fprintf(fp, " %s", cmd->cmd_name);
114 fputc('\n', fp);
117 int
118 main(int argc, char *argv[])
120 const struct gotadmin_cmd *cmd;
121 size_t i;
122 int ch;
123 int hflag = 0, Vflag = 0;
124 static const struct option longopts[] = {
125 { "version", no_argument, NULL, 'V' },
126 { NULL, 0, NULL, 0 }
127 };
129 setlocale(LC_CTYPE, "");
131 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
132 switch (ch) {
133 case 'h':
134 hflag = 1;
135 break;
136 case 'V':
137 Vflag = 1;
138 break;
139 default:
140 usage(hflag, 1);
141 /* NOTREACHED */
145 argc -= optind;
146 argv += optind;
147 optind = 1;
148 optreset = 1;
150 if (Vflag) {
151 got_version_print_str();
152 return 0;
155 if (argc <= 0)
156 usage(hflag, hflag ? 0 : 1);
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(gotadmin_commands); i++) {
162 const struct got_error *error;
164 cmd = &gotadmin_commands[i];
166 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
167 strcmp(cmd->cmd_alias, argv[0]) != 0)
168 continue;
170 if (hflag)
171 cmd->cmd_usage();
173 error = cmd->cmd_main(argc, argv);
174 if (error && error->code != GOT_ERR_CANCELLED &&
175 error->code != GOT_ERR_PRIVSEP_EXIT &&
176 !(sigpipe_received &&
177 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
178 !(sigint_received &&
179 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
180 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
181 return 1;
184 return 0;
187 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
188 list_commands(stderr);
189 return 1;
192 __dead static void
193 usage(int hflag, int status)
195 FILE *fp = (status == 0) ? stdout : stderr;
197 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
198 getprogname());
199 if (hflag)
200 list_commands(fp);
201 exit(status);
204 static const struct got_error *
205 apply_unveil(const char *repo_path, int repo_read_only)
207 const struct got_error *err;
209 #ifdef PROFILE
210 if (unveil("gmon.out", "rwc") != 0)
211 return got_error_from_errno2("unveil", "gmon.out");
212 #endif
213 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
214 return got_error_from_errno2("unveil", repo_path);
216 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
217 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
219 err = got_privsep_unveil_exec_helpers();
220 if (err != NULL)
221 return err;
223 if (unveil(NULL, NULL) != 0)
224 return got_error_from_errno("unveil");
226 return NULL;
229 __dead static void
230 usage_info(void)
232 fprintf(stderr, "usage: %s info [-r repository-path]\n",
233 getprogname());
234 exit(1);
237 static const struct got_error *
238 get_repo_path(char **repo_path)
240 const struct got_error *err = NULL;
241 struct got_worktree *worktree = NULL;
242 char *cwd;
244 *repo_path = NULL;
246 cwd = getcwd(NULL, 0);
247 if (cwd == NULL)
248 return got_error_from_errno("getcwd");
250 err = got_worktree_open(&worktree, cwd);
251 if (err) {
252 if (err->code != GOT_ERR_NOT_WORKTREE)
253 goto done;
254 err = NULL;
257 if (worktree)
258 *repo_path = strdup(got_worktree_get_repo_path(worktree));
259 else
260 *repo_path = strdup(cwd);
261 if (*repo_path == NULL)
262 err = got_error_from_errno("strdup");
263 done:
264 if (worktree)
265 got_worktree_close(worktree);
266 free(cwd);
267 return err;
270 __dead static void
271 usage_init(void)
273 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
274 exit(1);
277 static const struct got_error *
278 cmd_init(int argc, char *argv[])
280 const struct got_error *error = NULL;
281 char *repo_path = NULL;
282 int ch;
284 while ((ch = getopt(argc, argv, "")) != -1) {
285 switch (ch) {
286 default:
287 usage_init();
288 /* NOTREACHED */
292 argc -= optind;
293 argv += optind;
295 #ifndef PROFILE
296 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
297 err(1, "pledge");
298 #endif
299 if (argc != 1)
300 usage_init();
302 repo_path = strdup(argv[0]);
303 if (repo_path == NULL)
304 return got_error_from_errno("strdup");
306 got_path_strip_trailing_slashes(repo_path);
308 error = got_path_mkdir(repo_path);
309 if (error &&
310 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
311 goto done;
313 error = apply_unveil(repo_path, 0);
314 if (error)
315 goto done;
317 error = got_repo_init(repo_path);
318 done:
319 free(repo_path);
320 return error;
323 static const struct got_error *
324 cmd_info(int argc, char *argv[])
326 const struct got_error *error = NULL;
327 char *repo_path = NULL;
328 struct got_repository *repo = NULL;
329 const struct got_gotconfig *gotconfig = NULL;
330 int ch, npackfiles, npackedobj, nobj;
331 off_t packsize, loose_size;
332 char scaled[FMT_SCALED_STRSIZE];
333 int *pack_fds = NULL;
335 while ((ch = getopt(argc, argv, "r:")) != -1) {
336 switch (ch) {
337 case 'r':
338 repo_path = realpath(optarg, NULL);
339 if (repo_path == NULL)
340 return got_error_from_errno2("realpath",
341 optarg);
342 got_path_strip_trailing_slashes(repo_path);
343 break;
344 default:
345 usage_info();
346 /* NOTREACHED */
350 argc -= optind;
351 argv += optind;
353 #ifndef PROFILE
354 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
355 NULL) == -1)
356 err(1, "pledge");
357 #endif
358 if (repo_path == NULL) {
359 error = get_repo_path(&repo_path);
360 if (error)
361 goto done;
363 error = got_repo_pack_fds_open(&pack_fds);
364 if (error != NULL)
365 goto done;
366 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
367 if (error)
368 goto done;
369 #ifndef PROFILE
370 /* Remove "cpath" promise. */
371 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
372 NULL) == -1)
373 err(1, "pledge");
374 #endif
375 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
376 if (error)
377 goto done;
379 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
381 gotconfig = got_repo_get_gotconfig(repo);
382 if (gotconfig) {
383 const struct got_remote_repo *remotes;
384 int i, nremotes;
385 if (got_gotconfig_get_author(gotconfig)) {
386 printf("default author: %s\n",
387 got_gotconfig_get_author(gotconfig));
389 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
390 for (i = 0; i < nremotes; i++) {
391 const char *fetch_url = remotes[i].fetch_url;
392 const char *send_url = remotes[i].send_url;
393 if (strcmp(fetch_url, send_url) == 0) {
394 printf("remote \"%s\": %s\n", remotes[i].name,
395 remotes[i].fetch_url);
396 } else {
397 printf("remote \"%s\" (fetch): %s\n",
398 remotes[i].name, remotes[i].fetch_url);
399 printf("remote \"%s\" (send): %s\n",
400 remotes[i].name, remotes[i].send_url);
405 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
406 &packsize, repo);
407 if (error)
408 goto done;
409 printf("pack files: %d\n", npackfiles);
410 if (npackfiles > 0) {
411 if (fmt_scaled(packsize, scaled) == -1) {
412 error = got_error_from_errno("fmt_scaled");
413 goto done;
415 printf("packed objects: %d\n", npackedobj);
416 printf("packed total size: %s\n", scaled);
419 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
420 if (error)
421 goto done;
422 printf("loose objects: %d\n", nobj);
423 if (nobj > 0) {
424 if (fmt_scaled(loose_size, scaled) == -1) {
425 error = got_error_from_errno("fmt_scaled");
426 goto done;
428 printf("loose total size: %s\n", scaled);
430 done:
431 if (repo)
432 got_repo_close(repo);
433 if (pack_fds) {
434 const struct got_error *pack_err =
435 got_repo_pack_fds_close(pack_fds);
436 if (error == NULL)
437 error = pack_err;
440 free(repo_path);
441 return error;
444 __dead static void
445 usage_pack(void)
447 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
448 "[-x reference] [-q] [reference ...]\n",
449 getprogname());
450 exit(1);
453 struct got_pack_progress_arg {
454 char last_scaled_size[FMT_SCALED_STRSIZE];
455 int last_ncolored;
456 int last_nfound;
457 int last_ntrees;
458 int loading_done;
459 int last_ncommits;
460 int last_nobj_total;
461 int last_p_deltify;
462 int last_p_written;
463 int last_p_indexed;
464 int last_p_resolved;
465 int verbosity;
466 int printed_something;
467 };
469 static void
470 print_load_info(int print_colored, int print_found, int print_trees,
471 int ncolored, int nfound, int ntrees)
473 if (print_colored) {
474 printf("%d commit%s colored", ncolored,
475 ncolored == 1 ? "" : "s");
477 if (print_found) {
478 printf("%s%d object%s found",
479 ncolored > 0 ? "; " : "",
480 nfound, nfound == 1 ? "" : "s");
482 if (print_trees) {
483 printf("; %d tree%s scanned", ntrees,
484 ntrees == 1 ? "" : "s");
488 static const struct got_error *
489 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
490 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
491 int nobj_written)
493 struct got_pack_progress_arg *a = arg;
494 char scaled_size[FMT_SCALED_STRSIZE];
495 int p_deltify, p_written;
496 int print_colored = 0, print_found = 0, print_trees = 0;
497 int print_searching = 0, print_total = 0;
498 int print_deltify = 0, print_written = 0;
500 if (a->verbosity < 0)
501 return NULL;
503 if (a->last_ncolored != ncolored) {
504 print_colored = 1;
505 a->last_ncolored = ncolored;
508 if (a->last_nfound != nfound) {
509 print_colored = 1;
510 print_found = 1;
511 a->last_nfound = nfound;
514 if (a->last_ntrees != ntrees) {
515 print_colored = 1;
516 print_found = 1;
517 print_trees = 1;
518 a->last_ntrees = ntrees;
521 if ((print_colored || print_found || print_trees) &&
522 !a->loading_done) {
523 printf("\r");
524 print_load_info(print_colored, print_found, print_trees,
525 ncolored, nfound, ntrees);
526 a->printed_something = 1;
527 fflush(stdout);
528 return NULL;
529 } else if (!a->loading_done) {
530 printf("\r");
531 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
532 printf("\n");
533 a->loading_done = 1;
536 if (fmt_scaled(packfile_size, scaled_size) == -1)
537 return got_error_from_errno("fmt_scaled");
539 if (a->last_ncommits != ncommits) {
540 print_searching = 1;
541 a->last_ncommits = ncommits;
544 if (a->last_nobj_total != nobj_total) {
545 print_searching = 1;
546 print_total = 1;
547 a->last_nobj_total = nobj_total;
550 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
551 strcmp(scaled_size, a->last_scaled_size)) != 0) {
552 if (strlcpy(a->last_scaled_size, scaled_size,
553 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
554 return got_error(GOT_ERR_NO_SPACE);
557 if (nobj_deltify > 0 || nobj_written > 0) {
558 if (nobj_deltify > 0) {
559 p_deltify = (nobj_deltify * 100) / nobj_total;
560 if (p_deltify != a->last_p_deltify) {
561 a->last_p_deltify = p_deltify;
562 print_searching = 1;
563 print_total = 1;
564 print_deltify = 1;
567 if (nobj_written > 0) {
568 p_written = (nobj_written * 100) / nobj_total;
569 if (p_written != a->last_p_written) {
570 a->last_p_written = p_written;
571 print_searching = 1;
572 print_total = 1;
573 print_deltify = 1;
574 print_written = 1;
579 if (print_searching || print_total || print_deltify || print_written)
580 printf("\r");
581 if (print_searching)
582 printf("packing %d reference%s", ncommits,
583 ncommits == 1 ? "" : "s");
584 if (print_total)
585 printf("; %d object%s", nobj_total,
586 nobj_total == 1 ? "" : "s");
587 if (print_deltify)
588 printf("; deltify: %d%%", p_deltify);
589 if (print_written)
590 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
591 scaled_size, p_written);
592 if (print_searching || print_total || print_deltify ||
593 print_written) {
594 a->printed_something = 1;
595 fflush(stdout);
597 return NULL;
600 static const struct got_error *
601 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
602 int nobj_indexed, int nobj_loose, int nobj_resolved)
604 struct got_pack_progress_arg *a = arg;
605 char scaled_size[FMT_SCALED_STRSIZE];
606 int p_indexed, p_resolved;
607 int print_size = 0, print_indexed = 0, print_resolved = 0;
609 if (a->verbosity < 0)
610 return NULL;
612 if (packfile_size > 0 || nobj_indexed > 0) {
613 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
614 (a->last_scaled_size[0] == '\0' ||
615 strcmp(scaled_size, a->last_scaled_size)) != 0) {
616 print_size = 1;
617 if (strlcpy(a->last_scaled_size, scaled_size,
618 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
619 return got_error(GOT_ERR_NO_SPACE);
621 if (nobj_indexed > 0) {
622 p_indexed = (nobj_indexed * 100) / nobj_total;
623 if (p_indexed != a->last_p_indexed) {
624 a->last_p_indexed = p_indexed;
625 print_indexed = 1;
626 print_size = 1;
629 if (nobj_resolved > 0) {
630 p_resolved = (nobj_resolved * 100) /
631 (nobj_total - nobj_loose);
632 if (p_resolved != a->last_p_resolved) {
633 a->last_p_resolved = p_resolved;
634 print_resolved = 1;
635 print_indexed = 1;
636 print_size = 1;
641 if (print_size || print_indexed || print_resolved)
642 printf("\r");
643 if (print_size)
644 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
645 if (print_indexed)
646 printf("; indexing %d%%", p_indexed);
647 if (print_resolved)
648 printf("; resolving deltas %d%%", p_resolved);
649 if (print_size || print_indexed || print_resolved)
650 fflush(stdout);
652 return NULL;
655 static const struct got_error *
656 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
657 const char *refname, struct got_repository *repo)
659 const struct got_error *err;
660 struct got_reference *ref;
662 *new = NULL;
664 err = got_ref_open(&ref, repo, refname, 0);
665 if (err) {
666 if (err->code != GOT_ERR_NOT_REF)
667 return err;
669 /* Treat argument as a reference prefix. */
670 err = got_ref_list(refs, repo, refname,
671 got_ref_cmp_by_name, NULL);
672 } else {
673 err = got_reflist_insert(new, refs, ref,
674 got_ref_cmp_by_name, NULL);
675 if (err || *new == NULL /* duplicate */)
676 got_ref_close(ref);
679 return err;
682 static const struct got_error *
683 cmd_pack(int argc, char *argv[])
685 const struct got_error *error = NULL;
686 char *repo_path = NULL;
687 struct got_repository *repo = NULL;
688 int ch, i, loose_obj_only = 1, verbosity = 0;
689 struct got_object_id *pack_hash = NULL;
690 char *id_str = NULL;
691 struct got_pack_progress_arg ppa;
692 FILE *packfile = NULL;
693 struct got_pathlist_head exclude_args;
694 struct got_pathlist_entry *pe;
695 struct got_reflist_head exclude_refs;
696 struct got_reflist_head include_refs;
697 struct got_reflist_entry *re, *new;
698 int *pack_fds = NULL;
700 TAILQ_INIT(&exclude_args);
701 TAILQ_INIT(&exclude_refs);
702 TAILQ_INIT(&include_refs);
704 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
705 switch (ch) {
706 case 'a':
707 loose_obj_only = 0;
708 break;
709 case 'r':
710 repo_path = realpath(optarg, NULL);
711 if (repo_path == NULL)
712 return got_error_from_errno2("realpath",
713 optarg);
714 got_path_strip_trailing_slashes(repo_path);
715 break;
716 case 'x':
717 got_path_strip_trailing_slashes(optarg);
718 error = got_pathlist_append(&exclude_args,
719 optarg, NULL);
720 if (error)
721 return error;
722 break;
723 case 'q':
724 verbosity = -1;
725 break;
726 default:
727 usage_pack();
728 /* NOTREACHED */
732 argc -= optind;
733 argv += optind;
735 #ifndef PROFILE
736 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
737 NULL) == -1)
738 err(1, "pledge");
739 #endif
740 if (repo_path == NULL) {
741 error = get_repo_path(&repo_path);
742 if (error)
743 goto done;
745 error = got_repo_pack_fds_open(&pack_fds);
746 if (error != NULL)
747 goto done;
748 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
749 if (error)
750 goto done;
752 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
753 if (error)
754 goto done;
756 TAILQ_FOREACH(pe, &exclude_args, entry) {
757 const char *refname = pe->path;
758 error = add_ref(&new, &exclude_refs, refname, repo);
759 if (error)
760 goto done;
764 if (argc == 0) {
765 error = got_ref_list(&include_refs, repo, "",
766 got_ref_cmp_by_name, NULL);
767 if (error)
768 goto done;
769 } else {
770 for (i = 0; i < argc; i++) {
771 const char *refname;
772 got_path_strip_trailing_slashes(argv[i]);
773 refname = argv[i];
774 error = add_ref(&new, &include_refs, refname, repo);
775 if (error)
776 goto done;
780 /* Ignore references in the refs/got/ namespace. */
781 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
782 const char *refname = got_ref_get_name(re->ref);
783 if (strncmp("refs/got/", refname, 9) != 0)
784 continue;
785 TAILQ_REMOVE(&include_refs, re, entry);
786 got_ref_close(re->ref);
787 free(re);
790 memset(&ppa, 0, sizeof(ppa));
791 ppa.last_scaled_size[0] = '\0';
792 ppa.last_p_indexed = -1;
793 ppa.last_p_resolved = -1;
794 ppa.verbosity = verbosity;
796 error = got_repo_pack_objects(&packfile, &pack_hash,
797 &include_refs, &exclude_refs, repo, loose_obj_only,
798 pack_progress, &ppa, check_cancelled, NULL);
799 if (error) {
800 if (ppa.printed_something)
801 printf("\n");
802 goto done;
805 error = got_object_id_str(&id_str, pack_hash);
806 if (error)
807 goto done;
808 if (verbosity >= 0)
809 printf("\nWrote %s.pack\n", id_str);
811 error = got_repo_index_pack(packfile, pack_hash, repo,
812 pack_index_progress, &ppa, check_cancelled, NULL);
813 if (error)
814 goto done;
815 if (verbosity >= 0)
816 printf("\nIndexed %s.pack\n", id_str);
817 done:
818 if (repo)
819 got_repo_close(repo);
820 if (pack_fds) {
821 const struct got_error *pack_err =
822 got_repo_pack_fds_close(pack_fds);
823 if (error == NULL)
824 error = pack_err;
826 got_pathlist_free(&exclude_args);
827 got_ref_list_free(&exclude_refs);
828 got_ref_list_free(&include_refs);
829 free(id_str);
830 free(pack_hash);
831 free(repo_path);
832 return error;
835 __dead static void
836 usage_indexpack(void)
838 fprintf(stderr, "usage: %s indexpack packfile-path\n",
839 getprogname());
840 exit(1);
843 static const struct got_error *
844 cmd_indexpack(int argc, char *argv[])
846 const struct got_error *error = NULL;
847 struct got_repository *repo = NULL;
848 int ch;
849 struct got_object_id *pack_hash = NULL;
850 char *packfile_path = NULL;
851 char *id_str = NULL;
852 struct got_pack_progress_arg ppa;
853 FILE *packfile = NULL;
854 int *pack_fds = NULL;
856 while ((ch = getopt(argc, argv, "")) != -1) {
857 switch (ch) {
858 default:
859 usage_indexpack();
860 /* NOTREACHED */
864 argc -= optind;
865 argv += optind;
867 if (argc != 1)
868 usage_indexpack();
870 packfile_path = realpath(argv[0], NULL);
871 if (packfile_path == NULL)
872 return got_error_from_errno2("realpath", argv[0]);
874 #ifndef PROFILE
875 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
876 NULL) == -1)
877 err(1, "pledge");
878 #endif
880 error = got_repo_pack_fds_open(&pack_fds);
881 if (error != NULL)
882 goto done;
883 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
884 if (error)
885 goto done;
887 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
888 if (error)
889 goto done;
891 memset(&ppa, 0, sizeof(ppa));
892 ppa.last_scaled_size[0] = '\0';
893 ppa.last_p_indexed = -1;
894 ppa.last_p_resolved = -1;
896 error = got_repo_find_pack(&packfile, &pack_hash, repo,
897 packfile_path);
898 if (error)
899 goto done;
901 error = got_object_id_str(&id_str, pack_hash);
902 if (error)
903 goto done;
905 error = got_repo_index_pack(packfile, pack_hash, repo,
906 pack_index_progress, &ppa, check_cancelled, NULL);
907 if (error)
908 goto done;
909 printf("\nIndexed %s.pack\n", id_str);
910 done:
911 if (repo)
912 got_repo_close(repo);
913 if (pack_fds) {
914 const struct got_error *pack_err =
915 got_repo_pack_fds_close(pack_fds);
916 if (error == NULL)
917 error = pack_err;
919 free(id_str);
920 free(pack_hash);
921 return error;
924 __dead static void
925 usage_listpack(void)
927 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
928 getprogname());
929 exit(1);
932 struct gotadmin_list_pack_cb_args {
933 int nblobs;
934 int ntrees;
935 int ncommits;
936 int ntags;
937 int noffdeltas;
938 int nrefdeltas;
939 int human_readable;
940 };
942 static const struct got_error *
943 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
944 off_t size, off_t base_offset, struct got_object_id *base_id)
946 const struct got_error *err;
947 struct gotadmin_list_pack_cb_args *a = arg;
948 char *id_str, *delta_str = NULL, *base_id_str = NULL;
949 const char *type_str;
951 err = got_object_id_str(&id_str, id);
952 if (err)
953 return err;
955 switch (type) {
956 case GOT_OBJ_TYPE_BLOB:
957 type_str = GOT_OBJ_LABEL_BLOB;
958 a->nblobs++;
959 break;
960 case GOT_OBJ_TYPE_TREE:
961 type_str = GOT_OBJ_LABEL_TREE;
962 a->ntrees++;
963 break;
964 case GOT_OBJ_TYPE_COMMIT:
965 type_str = GOT_OBJ_LABEL_COMMIT;
966 a->ncommits++;
967 break;
968 case GOT_OBJ_TYPE_TAG:
969 type_str = GOT_OBJ_LABEL_TAG;
970 a->ntags++;
971 break;
972 case GOT_OBJ_TYPE_OFFSET_DELTA:
973 type_str = "offset-delta";
974 if (asprintf(&delta_str, " base-offset %lld",
975 (long long)base_offset) == -1) {
976 err = got_error_from_errno("asprintf");
977 goto done;
979 a->noffdeltas++;
980 break;
981 case GOT_OBJ_TYPE_REF_DELTA:
982 type_str = "ref-delta";
983 err = got_object_id_str(&base_id_str, base_id);
984 if (err)
985 goto done;
986 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
987 err = got_error_from_errno("asprintf");
988 goto done;
990 a->nrefdeltas++;
991 break;
992 default:
993 err = got_error(GOT_ERR_OBJ_TYPE);
994 goto done;
996 if (a->human_readable) {
997 char scaled[FMT_SCALED_STRSIZE];
998 char *s;;
999 if (fmt_scaled(size, scaled) == -1) {
1000 err = got_error_from_errno("fmt_scaled");
1001 goto done;
1003 s = scaled;
1004 while (isspace((unsigned char)*s))
1005 s++;
1006 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1007 (long long)offset, s, delta_str ? delta_str : "");
1008 } else {
1009 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1010 (long long)offset, (long long)size,
1011 delta_str ? delta_str : "");
1013 done:
1014 free(id_str);
1015 free(base_id_str);
1016 free(delta_str);
1017 return err;
1020 static const struct got_error *
1021 cmd_listpack(int argc, char *argv[])
1023 const struct got_error *error = NULL;
1024 struct got_repository *repo = NULL;
1025 int ch;
1026 struct got_object_id *pack_hash = NULL;
1027 char *packfile_path = NULL;
1028 char *id_str = NULL;
1029 struct gotadmin_list_pack_cb_args lpa;
1030 FILE *packfile = NULL;
1031 int show_stats = 0, human_readable = 0;
1032 int *pack_fds = NULL;
1034 while ((ch = getopt(argc, argv, "hs")) != -1) {
1035 switch (ch) {
1036 case 'h':
1037 human_readable = 1;
1038 break;
1039 case 's':
1040 show_stats = 1;
1041 break;
1042 default:
1043 usage_listpack();
1044 /* NOTREACHED */
1048 argc -= optind;
1049 argv += optind;
1051 if (argc != 1)
1052 usage_listpack();
1053 packfile_path = realpath(argv[0], NULL);
1054 if (packfile_path == NULL)
1055 return got_error_from_errno2("realpath", argv[0]);
1057 #ifndef PROFILE
1058 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1059 NULL) == -1)
1060 err(1, "pledge");
1061 #endif
1062 error = got_repo_pack_fds_open(&pack_fds);
1063 if (error != NULL)
1064 goto done;
1065 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1066 if (error)
1067 goto done;
1068 #ifndef PROFILE
1069 /* Remove "cpath" promise. */
1070 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1071 NULL) == -1)
1072 err(1, "pledge");
1073 #endif
1074 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1075 if (error)
1076 goto done;
1078 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1079 packfile_path);
1080 if (error)
1081 goto done;
1082 error = got_object_id_str(&id_str, pack_hash);
1083 if (error)
1084 goto done;
1086 memset(&lpa, 0, sizeof(lpa));
1087 lpa.human_readable = human_readable;
1088 error = got_repo_list_pack(packfile, pack_hash, repo,
1089 list_pack_cb, &lpa, check_cancelled, NULL);
1090 if (error)
1091 goto done;
1092 if (show_stats) {
1093 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1094 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1095 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1096 lpa.noffdeltas + lpa.nrefdeltas,
1097 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1098 lpa.noffdeltas, lpa.nrefdeltas);
1100 done:
1101 if (repo)
1102 got_repo_close(repo);
1103 if (pack_fds) {
1104 const struct got_error *pack_err =
1105 got_repo_pack_fds_close(pack_fds);
1106 if (error == NULL)
1107 error = pack_err;
1109 free(id_str);
1110 free(pack_hash);
1111 free(packfile_path);
1112 return error;
1115 __dead static void
1116 usage_cleanup(void)
1118 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1119 "[-q]\n", getprogname());
1120 exit(1);
1123 struct got_cleanup_progress_arg {
1124 int last_nloose;
1125 int last_ncommits;
1126 int last_npurged;
1127 int verbosity;
1128 int printed_something;
1129 int dry_run;
1132 static const struct got_error *
1133 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1135 struct got_cleanup_progress_arg *a = arg;
1136 int print_loose = 0, print_commits = 0, print_purged = 0;
1138 if (a->last_nloose != nloose) {
1139 print_loose = 1;
1140 a->last_nloose = nloose;
1142 if (a->last_ncommits != ncommits) {
1143 print_loose = 1;
1144 print_commits = 1;
1145 a->last_ncommits = ncommits;
1147 if (a->last_npurged != npurged) {
1148 print_loose = 1;
1149 print_commits = 1;
1150 print_purged = 1;
1151 a->last_npurged = npurged;
1154 if (a->verbosity < 0)
1155 return NULL;
1157 if (print_loose || print_commits || print_purged)
1158 printf("\r");
1159 if (print_loose)
1160 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1161 if (print_commits)
1162 printf("; %d commit%s scanned", ncommits,
1163 ncommits == 1 ? "" : "s");
1164 if (print_purged) {
1165 if (a->dry_run) {
1166 printf("; %d object%s could be purged", npurged,
1167 npurged == 1 ? "" : "s");
1168 } else {
1169 printf("; %d object%s purged", npurged,
1170 npurged == 1 ? "" : "s");
1173 if (print_loose || print_commits || print_purged) {
1174 a->printed_something = 1;
1175 fflush(stdout);
1177 return NULL;
1180 struct got_lonely_packidx_progress_arg {
1181 int verbosity;
1182 int printed_something;
1183 int dry_run;
1186 static const struct got_error *
1187 lonely_packidx_progress(void *arg, const char *path)
1189 struct got_lonely_packidx_progress_arg *a = arg;
1191 if (a->verbosity < 0)
1192 return NULL;
1194 if (a->dry_run)
1195 printf("%s could be removed\n", path);
1196 else
1197 printf("%s removed\n", path);
1199 a->printed_something = 1;
1200 return NULL;
1203 static const struct got_error *
1204 cmd_cleanup(int argc, char *argv[])
1206 const struct got_error *error = NULL;
1207 char *repo_path = NULL;
1208 struct got_repository *repo = NULL;
1209 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1210 int remove_lonely_packidx = 0, ignore_mtime = 0;
1211 struct got_cleanup_progress_arg cpa;
1212 struct got_lonely_packidx_progress_arg lpa;
1213 off_t size_before, size_after;
1214 char scaled_before[FMT_SCALED_STRSIZE];
1215 char scaled_after[FMT_SCALED_STRSIZE];
1216 char scaled_diff[FMT_SCALED_STRSIZE];
1217 char **extensions;
1218 int nextensions, i;
1219 int *pack_fds = NULL;
1221 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1222 switch (ch) {
1223 case 'a':
1224 ignore_mtime = 1;
1225 break;
1226 case 'p':
1227 remove_lonely_packidx = 1;
1228 break;
1229 case 'r':
1230 repo_path = realpath(optarg, NULL);
1231 if (repo_path == NULL)
1232 return got_error_from_errno2("realpath",
1233 optarg);
1234 got_path_strip_trailing_slashes(repo_path);
1235 break;
1236 case 'n':
1237 dry_run = 1;
1238 break;
1239 case 'q':
1240 verbosity = -1;
1241 break;
1242 default:
1243 usage_cleanup();
1244 /* NOTREACHED */
1248 argc -= optind;
1249 argv += optind;
1251 #ifndef PROFILE
1252 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1253 NULL) == -1)
1254 err(1, "pledge");
1255 #endif
1256 if (repo_path == NULL) {
1257 error = get_repo_path(&repo_path);
1258 if (error)
1259 goto done;
1261 error = got_repo_pack_fds_open(&pack_fds);
1262 if (error != NULL)
1263 goto done;
1264 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1265 if (error)
1266 goto done;
1268 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1269 if (error)
1270 goto done;
1272 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1273 repo);
1274 for (i = 0; i < nextensions; i++) {
1275 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1276 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1277 "the preciousObjects Git extension is enabled; "
1278 "this implies that objects must not be deleted");
1279 goto done;
1283 if (remove_lonely_packidx) {
1284 memset(&lpa, 0, sizeof(lpa));
1285 lpa.dry_run = dry_run;
1286 lpa.verbosity = verbosity;
1287 error = got_repo_remove_lonely_packidx(repo, dry_run,
1288 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1289 goto done;
1292 memset(&cpa, 0, sizeof(cpa));
1293 cpa.last_ncommits = -1;
1294 cpa.last_npurged = -1;
1295 cpa.dry_run = dry_run;
1296 cpa.verbosity = verbosity;
1297 error = got_repo_purge_unreferenced_loose_objects(repo,
1298 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1299 cleanup_progress, &cpa, check_cancelled, NULL);
1300 if (cpa.printed_something)
1301 printf("\n");
1302 if (error)
1303 goto done;
1304 if (cpa.printed_something) {
1305 if (fmt_scaled(size_before, scaled_before) == -1) {
1306 error = got_error_from_errno("fmt_scaled");
1307 goto done;
1309 if (fmt_scaled(size_after, scaled_after) == -1) {
1310 error = got_error_from_errno("fmt_scaled");
1311 goto done;
1313 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1314 error = got_error_from_errno("fmt_scaled");
1315 goto done;
1317 printf("loose total size before: %s\n", scaled_before);
1318 printf("loose total size after: %s\n", scaled_after);
1319 if (dry_run) {
1320 printf("disk space which would be freed: %s\n",
1321 scaled_diff);
1322 } else
1323 printf("disk space freed: %s\n", scaled_diff);
1324 printf("loose objects also found in pack files: %d\n", npacked);
1326 done:
1327 if (repo)
1328 got_repo_close(repo);
1329 if (pack_fds) {
1330 const struct got_error *pack_err =
1331 got_repo_pack_fds_close(pack_fds);
1332 if (error == NULL)
1333 error = pack_err;
1335 free(repo_path);
1336 return error;