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_repository_dump.h"
43 #include "got_gotconfig.h"
44 #include "got_path.h"
45 #include "got_privsep.h"
46 #include "got_opentemp.h"
47 #include "got_worktree.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
68 static const struct got_error *
69 check_cancelled(void *arg)
70 {
71 if (sigint_received || sigpipe_received)
72 return got_error(GOT_ERR_CANCELLED);
73 return NULL;
74 }
76 struct gotadmin_cmd {
77 const char *cmd_name;
78 const struct got_error *(*cmd_main)(int, char *[]);
79 void (*cmd_usage)(void);
80 const char *cmd_alias;
81 };
83 __dead static void usage(int, int);
84 __dead static void usage_init(void);
85 __dead static void usage_info(void);
86 __dead static void usage_pack(void);
87 __dead static void usage_indexpack(void);
88 __dead static void usage_listpack(void);
89 __dead static void usage_cleanup(void);
90 __dead static void usage_dump(void);
92 static const struct got_error* cmd_init(int, char *[]);
93 static const struct got_error* cmd_info(int, char *[]);
94 static const struct got_error* cmd_pack(int, char *[]);
95 static const struct got_error* cmd_indexpack(int, char *[]);
96 static const struct got_error* cmd_listpack(int, char *[]);
97 static const struct got_error* cmd_cleanup(int, char *[]);
98 static const struct got_error* cmd_dump(int, char *[]);
100 static const struct gotadmin_cmd gotadmin_commands[] = {
101 { "init", cmd_init, usage_init, "" },
102 { "info", cmd_info, usage_info, "" },
103 { "pack", cmd_pack, usage_pack, "" },
104 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
105 { "listpack", cmd_listpack, usage_listpack, "ls" },
106 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
107 { "dump", cmd_dump, usage_dump, "" },
108 };
110 static void
111 list_commands(FILE *fp)
113 size_t i;
115 fprintf(fp, "commands:");
116 for (i = 0; i < nitems(gotadmin_commands); i++) {
117 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
118 fprintf(fp, " %s", cmd->cmd_name);
120 fputc('\n', fp);
123 int
124 main(int argc, char *argv[])
126 const struct gotadmin_cmd *cmd;
127 size_t i;
128 int ch;
129 int hflag = 0, Vflag = 0;
130 static const struct option longopts[] = {
131 { "version", no_argument, NULL, 'V' },
132 { NULL, 0, NULL, 0 }
133 };
135 setlocale(LC_CTYPE, "");
137 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
138 switch (ch) {
139 case 'h':
140 hflag = 1;
141 break;
142 case 'V':
143 Vflag = 1;
144 break;
145 default:
146 usage(hflag, 1);
147 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
153 optind = 1;
154 optreset = 1;
156 if (Vflag) {
157 got_version_print_str();
158 return 0;
161 if (argc <= 0)
162 usage(hflag, hflag ? 0 : 1);
164 signal(SIGINT, catch_sigint);
165 signal(SIGPIPE, catch_sigpipe);
167 for (i = 0; i < nitems(gotadmin_commands); i++) {
168 const struct got_error *error;
170 cmd = &gotadmin_commands[i];
172 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
173 strcmp(cmd->cmd_alias, argv[0]) != 0)
174 continue;
176 if (hflag)
177 cmd->cmd_usage();
179 error = cmd->cmd_main(argc, argv);
180 if (error && error->code != GOT_ERR_CANCELLED &&
181 error->code != GOT_ERR_PRIVSEP_EXIT &&
182 !(sigpipe_received &&
183 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
184 !(sigint_received &&
185 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
186 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
187 return 1;
190 return 0;
193 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
194 list_commands(stderr);
195 return 1;
198 __dead static void
199 usage(int hflag, int status)
201 FILE *fp = (status == 0) ? stdout : stderr;
203 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
204 getprogname());
205 if (hflag)
206 list_commands(fp);
207 exit(status);
210 static const struct got_error *
211 apply_unveil(const char *repo_path, int repo_read_only)
213 const struct got_error *err;
215 #ifdef PROFILE
216 if (unveil("gmon.out", "rwc") != 0)
217 return got_error_from_errno2("unveil", "gmon.out");
218 #endif
219 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
220 return got_error_from_errno2("unveil", repo_path);
222 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
223 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
225 err = got_privsep_unveil_exec_helpers();
226 if (err != NULL)
227 return err;
229 if (unveil(NULL, NULL) != 0)
230 return got_error_from_errno("unveil");
232 return NULL;
235 __dead static void
236 usage_info(void)
238 fprintf(stderr, "usage: %s info [-r repository-path]\n",
239 getprogname());
240 exit(1);
243 static const struct got_error *
244 get_repo_path(char **repo_path)
246 const struct got_error *err = NULL;
247 struct got_worktree *worktree = NULL;
248 char *cwd;
250 *repo_path = NULL;
252 cwd = getcwd(NULL, 0);
253 if (cwd == NULL)
254 return got_error_from_errno("getcwd");
256 err = got_worktree_open(&worktree, cwd);
257 if (err) {
258 if (err->code != GOT_ERR_NOT_WORKTREE)
259 goto done;
260 err = NULL;
263 if (worktree)
264 *repo_path = strdup(got_worktree_get_repo_path(worktree));
265 else
266 *repo_path = strdup(cwd);
267 if (*repo_path == NULL)
268 err = got_error_from_errno("strdup");
269 done:
270 if (worktree)
271 got_worktree_close(worktree);
272 free(cwd);
273 return err;
276 __dead static void
277 usage_init(void)
279 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
280 getprogname());
281 exit(1);
284 static const struct got_error *
285 cmd_init(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 const char *head_name = NULL;
289 char *repo_path = NULL;
290 int ch;
292 #ifndef PROFILE
293 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
294 err(1, "pledge");
295 #endif
297 while ((ch = getopt(argc, argv, "b:")) != -1) {
298 switch (ch) {
299 case 'b':
300 head_name = optarg;
301 break;
302 default:
303 usage_init();
304 /* NOTREACHED */
308 argc -= optind;
309 argv += optind;
311 if (argc != 1)
312 usage_init();
314 repo_path = strdup(argv[0]);
315 if (repo_path == NULL)
316 return got_error_from_errno("strdup");
318 got_path_strip_trailing_slashes(repo_path);
320 error = got_path_mkdir(repo_path);
321 if (error &&
322 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
323 goto done;
325 error = apply_unveil(repo_path, 0);
326 if (error)
327 goto done;
329 error = got_repo_init(repo_path, head_name);
330 done:
331 free(repo_path);
332 return error;
335 static const struct got_error *
336 cmd_info(int argc, char *argv[])
338 const struct got_error *error = NULL;
339 char *repo_path = NULL;
340 struct got_repository *repo = NULL;
341 const struct got_gotconfig *gotconfig = NULL;
342 int ch, npackfiles, npackedobj, nobj;
343 off_t packsize, loose_size;
344 char scaled[FMT_SCALED_STRSIZE];
345 int *pack_fds = NULL;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
349 NULL) == -1)
350 err(1, "pledge");
351 #endif
353 while ((ch = getopt(argc, argv, "r:")) != -1) {
354 switch (ch) {
355 case 'r':
356 repo_path = realpath(optarg, NULL);
357 if (repo_path == NULL)
358 return got_error_from_errno2("realpath",
359 optarg);
360 got_path_strip_trailing_slashes(repo_path);
361 break;
362 default:
363 usage_info();
364 /* NOTREACHED */
368 argc -= optind;
369 argv += optind;
371 if (repo_path == NULL) {
372 error = get_repo_path(&repo_path);
373 if (error)
374 goto done;
376 error = got_repo_pack_fds_open(&pack_fds);
377 if (error != NULL)
378 goto done;
379 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
380 if (error)
381 goto done;
382 #ifndef PROFILE
383 /* Remove "cpath" promise. */
384 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
385 NULL) == -1)
386 err(1, "pledge");
387 #endif
388 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
389 if (error)
390 goto done;
392 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
394 gotconfig = got_repo_get_gotconfig(repo);
395 if (gotconfig) {
396 const struct got_remote_repo *remotes;
397 int i, nremotes;
398 if (got_gotconfig_get_author(gotconfig)) {
399 printf("default author: %s\n",
400 got_gotconfig_get_author(gotconfig));
402 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
403 for (i = 0; i < nremotes; i++) {
404 const char *fetch_url = remotes[i].fetch_url;
405 const char *send_url = remotes[i].send_url;
406 if (strcmp(fetch_url, send_url) == 0) {
407 printf("remote \"%s\": %s\n", remotes[i].name,
408 remotes[i].fetch_url);
409 } else {
410 printf("remote \"%s\" (fetch): %s\n",
411 remotes[i].name, remotes[i].fetch_url);
412 printf("remote \"%s\" (send): %s\n",
413 remotes[i].name, remotes[i].send_url);
418 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
419 &packsize, repo);
420 if (error)
421 goto done;
422 printf("pack files: %d\n", npackfiles);
423 if (npackfiles > 0) {
424 if (fmt_scaled(packsize, scaled) == -1) {
425 error = got_error_from_errno("fmt_scaled");
426 goto done;
428 printf("packed objects: %d\n", npackedobj);
429 printf("packed total size: %s\n", scaled);
432 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
433 if (error)
434 goto done;
435 printf("loose objects: %d\n", nobj);
436 if (nobj > 0) {
437 if (fmt_scaled(loose_size, scaled) == -1) {
438 error = got_error_from_errno("fmt_scaled");
439 goto done;
441 printf("loose total size: %s\n", scaled);
443 done:
444 if (repo)
445 got_repo_close(repo);
446 if (pack_fds) {
447 const struct got_error *pack_err =
448 got_repo_pack_fds_close(pack_fds);
449 if (error == NULL)
450 error = pack_err;
453 free(repo_path);
454 return error;
457 __dead static void
458 usage_pack(void)
460 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
461 "[-x reference] [reference ...]\n", getprogname());
462 exit(1);
465 struct got_pack_progress_arg {
466 FILE *out;
467 char last_scaled_size[FMT_SCALED_STRSIZE];
468 int last_ncolored;
469 int last_nfound;
470 int last_ntrees;
471 int loading_done;
472 int last_ncommits;
473 int last_nobj_total;
474 int last_p_deltify;
475 int last_p_written;
476 int last_p_indexed;
477 int last_p_resolved;
478 int verbosity;
479 int printed_something;
480 };
482 static void
483 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
484 int ncolored, int nfound, int ntrees)
486 if (print_colored) {
487 fprintf(out, "%d commit%s colored", ncolored,
488 ncolored == 1 ? "" : "s");
490 if (print_found) {
491 fprintf(out, "%s%d object%s found",
492 ncolored > 0 ? "; " : "",
493 nfound, nfound == 1 ? "" : "s");
495 if (print_trees) {
496 fprintf(out, "; %d tree%s scanned", ntrees,
497 ntrees == 1 ? "" : "s");
501 static const struct got_error *
502 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
503 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
504 int nobj_written)
506 struct got_pack_progress_arg *a = arg;
507 char scaled_size[FMT_SCALED_STRSIZE];
508 int p_deltify, p_written;
509 int print_colored = 0, print_found = 0, print_trees = 0;
510 int print_searching = 0, print_total = 0;
511 int print_deltify = 0, print_written = 0;
513 if (a->verbosity < 0)
514 return NULL;
516 if (a->last_ncolored != ncolored) {
517 print_colored = 1;
518 a->last_ncolored = ncolored;
521 if (a->last_nfound != nfound) {
522 print_colored = 1;
523 print_found = 1;
524 a->last_nfound = nfound;
527 if (a->last_ntrees != ntrees) {
528 print_colored = 1;
529 print_found = 1;
530 print_trees = 1;
531 a->last_ntrees = ntrees;
534 if ((print_colored || print_found || print_trees) &&
535 !a->loading_done) {
536 fprintf(a->out, "\r");
537 print_load_info(a->out, print_colored, print_found,
538 print_trees, ncolored, nfound, ntrees);
539 a->printed_something = 1;
540 fflush(a->out);
541 return NULL;
542 } else if (!a->loading_done) {
543 fprintf(a->out, "\r");
544 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
545 fprintf(a->out, "\n");
546 a->loading_done = 1;
549 if (fmt_scaled(packfile_size, scaled_size) == -1)
550 return got_error_from_errno("fmt_scaled");
552 if (a->last_ncommits != ncommits) {
553 print_searching = 1;
554 a->last_ncommits = ncommits;
557 if (a->last_nobj_total != nobj_total) {
558 print_searching = 1;
559 print_total = 1;
560 a->last_nobj_total = nobj_total;
563 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
564 strcmp(scaled_size, a->last_scaled_size)) != 0) {
565 if (strlcpy(a->last_scaled_size, scaled_size,
566 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
567 return got_error(GOT_ERR_NO_SPACE);
570 if (nobj_deltify > 0 || nobj_written > 0) {
571 if (nobj_deltify > 0) {
572 p_deltify = (nobj_deltify * 100) / nobj_total;
573 if (p_deltify != a->last_p_deltify) {
574 a->last_p_deltify = p_deltify;
575 print_searching = 1;
576 print_total = 1;
577 print_deltify = 1;
580 if (nobj_written > 0) {
581 p_written = (nobj_written * 100) / nobj_total;
582 if (p_written != a->last_p_written) {
583 a->last_p_written = p_written;
584 print_searching = 1;
585 print_total = 1;
586 print_deltify = 1;
587 print_written = 1;
592 if (print_searching || print_total || print_deltify || print_written)
593 fprintf(a->out, "\r");
594 if (print_searching)
595 fprintf(a->out, "packing %d reference%s", ncommits,
596 ncommits == 1 ? "" : "s");
597 if (print_total)
598 fprintf(a->out, "; %d object%s", nobj_total,
599 nobj_total == 1 ? "" : "s");
600 if (print_deltify)
601 fprintf(a->out, "; deltify: %d%%", p_deltify);
602 if (print_written)
603 fprintf(a->out, "; writing pack: %*s %d%%",
604 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
605 if (print_searching || print_total || print_deltify ||
606 print_written) {
607 a->printed_something = 1;
608 fflush(a->out);
610 return NULL;
613 static const struct got_error *
614 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
615 int nobj_indexed, int nobj_loose, int nobj_resolved)
617 struct got_pack_progress_arg *a = arg;
618 char scaled_size[FMT_SCALED_STRSIZE];
619 int p_indexed, p_resolved;
620 int print_size = 0, print_indexed = 0, print_resolved = 0;
622 if (a->verbosity < 0)
623 return NULL;
625 if (packfile_size > 0 || nobj_indexed > 0) {
626 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
627 (a->last_scaled_size[0] == '\0' ||
628 strcmp(scaled_size, a->last_scaled_size)) != 0) {
629 print_size = 1;
630 if (strlcpy(a->last_scaled_size, scaled_size,
631 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
632 return got_error(GOT_ERR_NO_SPACE);
634 if (nobj_indexed > 0) {
635 p_indexed = (nobj_indexed * 100) / nobj_total;
636 if (p_indexed != a->last_p_indexed) {
637 a->last_p_indexed = p_indexed;
638 print_indexed = 1;
639 print_size = 1;
642 if (nobj_resolved > 0) {
643 p_resolved = (nobj_resolved * 100) /
644 (nobj_total - nobj_loose);
645 if (p_resolved != a->last_p_resolved) {
646 a->last_p_resolved = p_resolved;
647 print_resolved = 1;
648 print_indexed = 1;
649 print_size = 1;
654 if (print_size || print_indexed || print_resolved)
655 printf("\r");
656 if (print_size)
657 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
658 if (print_indexed)
659 printf("; indexing %d%%", p_indexed);
660 if (print_resolved)
661 printf("; resolving deltas %d%%", p_resolved);
662 if (print_size || print_indexed || print_resolved)
663 fflush(stdout);
665 return NULL;
668 static const struct got_error *
669 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
670 const char *refname, struct got_repository *repo)
672 const struct got_error *err;
673 struct got_reference *ref;
675 *new = NULL;
677 err = got_ref_open(&ref, repo, refname, 0);
678 if (err) {
679 if (err->code != GOT_ERR_NOT_REF)
680 return err;
682 /* Treat argument as a reference prefix. */
683 err = got_ref_list(refs, repo, refname,
684 got_ref_cmp_by_name, NULL);
685 } else {
686 err = got_reflist_insert(new, refs, ref,
687 got_ref_cmp_by_name, NULL);
688 if (err || *new == NULL /* duplicate */)
689 got_ref_close(ref);
692 return err;
695 static const struct got_error *
696 cmd_pack(int argc, char *argv[])
698 const struct got_error *error = NULL;
699 char *repo_path = NULL;
700 struct got_repository *repo = NULL;
701 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
702 struct got_object_id *pack_hash = NULL;
703 char *id_str = NULL;
704 struct got_pack_progress_arg ppa;
705 FILE *packfile = NULL;
706 struct got_pathlist_head exclude_args;
707 struct got_pathlist_entry *pe;
708 struct got_reflist_head exclude_refs;
709 struct got_reflist_head include_refs;
710 struct got_reflist_entry *re, *new;
711 int *pack_fds = NULL;
713 TAILQ_INIT(&exclude_args);
714 TAILQ_INIT(&exclude_refs);
715 TAILQ_INIT(&include_refs);
717 #ifndef PROFILE
718 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
719 NULL) == -1)
720 err(1, "pledge");
721 #endif
723 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
724 switch (ch) {
725 case 'a':
726 loose_obj_only = 0;
727 break;
728 case 'D':
729 force_refdelta = 1;
730 break;
731 case 'q':
732 verbosity = -1;
733 break;
734 case 'r':
735 repo_path = realpath(optarg, NULL);
736 if (repo_path == NULL)
737 return got_error_from_errno2("realpath",
738 optarg);
739 got_path_strip_trailing_slashes(repo_path);
740 break;
741 case 'x':
742 got_path_strip_trailing_slashes(optarg);
743 error = got_pathlist_append(&exclude_args,
744 optarg, NULL);
745 if (error)
746 return error;
747 break;
748 default:
749 usage_pack();
750 /* NOTREACHED */
754 argc -= optind;
755 argv += optind;
757 if (repo_path == NULL) {
758 error = get_repo_path(&repo_path);
759 if (error)
760 goto done;
762 error = got_repo_pack_fds_open(&pack_fds);
763 if (error != NULL)
764 goto done;
765 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
766 if (error)
767 goto done;
769 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
770 if (error)
771 goto done;
773 TAILQ_FOREACH(pe, &exclude_args, entry) {
774 const char *refname = pe->path;
775 error = add_ref(&new, &exclude_refs, refname, repo);
776 if (error)
777 goto done;
780 if (argc == 0) {
781 error = got_ref_list(&include_refs, repo, "",
782 got_ref_cmp_by_name, NULL);
783 if (error)
784 goto done;
785 } else {
786 for (i = 0; i < argc; i++) {
787 const char *refname;
788 got_path_strip_trailing_slashes(argv[i]);
789 refname = argv[i];
790 error = add_ref(&new, &include_refs, refname, repo);
791 if (error)
792 goto done;
796 /* Ignore references in the refs/got/ namespace. */
797 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
798 const char *refname = got_ref_get_name(re->ref);
799 if (strncmp("refs/got/", refname, 9) != 0)
800 continue;
801 TAILQ_REMOVE(&include_refs, re, entry);
802 got_ref_close(re->ref);
803 free(re);
806 memset(&ppa, 0, sizeof(ppa));
807 ppa.out = stdout;
808 ppa.last_scaled_size[0] = '\0';
809 ppa.last_p_indexed = -1;
810 ppa.last_p_resolved = -1;
811 ppa.verbosity = verbosity;
813 error = got_repo_pack_objects(&packfile, &pack_hash,
814 &include_refs, &exclude_refs, repo, loose_obj_only,
815 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
816 if (error) {
817 if (ppa.printed_something)
818 printf("\n");
819 goto done;
822 error = got_object_id_str(&id_str, pack_hash);
823 if (error)
824 goto done;
825 if (verbosity >= 0)
826 printf("\nWrote %s.pack\n", id_str);
828 error = got_repo_index_pack(packfile, pack_hash, repo,
829 pack_index_progress, &ppa, check_cancelled, NULL);
830 if (error)
831 goto done;
832 if (verbosity >= 0)
833 printf("\nIndexed %s.pack\n", id_str);
834 done:
835 if (repo)
836 got_repo_close(repo);
837 if (pack_fds) {
838 const struct got_error *pack_err =
839 got_repo_pack_fds_close(pack_fds);
840 if (error == NULL)
841 error = pack_err;
843 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
844 got_ref_list_free(&exclude_refs);
845 got_ref_list_free(&include_refs);
846 free(id_str);
847 free(pack_hash);
848 free(repo_path);
849 return error;
852 __dead static void
853 usage_indexpack(void)
855 fprintf(stderr, "usage: %s indexpack packfile-path\n",
856 getprogname());
857 exit(1);
860 static const struct got_error *
861 cmd_indexpack(int argc, char *argv[])
863 const struct got_error *error = NULL;
864 struct got_repository *repo = NULL;
865 int ch;
866 struct got_object_id *pack_hash = NULL;
867 char *packfile_path = NULL;
868 char *id_str = NULL;
869 struct got_pack_progress_arg ppa;
870 FILE *packfile = NULL;
871 int *pack_fds = NULL;
873 while ((ch = getopt(argc, argv, "")) != -1) {
874 switch (ch) {
875 default:
876 usage_indexpack();
877 /* NOTREACHED */
881 argc -= optind;
882 argv += optind;
884 if (argc != 1)
885 usage_indexpack();
887 packfile_path = realpath(argv[0], NULL);
888 if (packfile_path == NULL)
889 return got_error_from_errno2("realpath", argv[0]);
891 #ifndef PROFILE
892 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
893 NULL) == -1)
894 err(1, "pledge");
895 #endif
897 error = got_repo_pack_fds_open(&pack_fds);
898 if (error != NULL)
899 goto done;
900 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
901 if (error)
902 goto done;
904 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
905 if (error)
906 goto done;
908 memset(&ppa, 0, sizeof(ppa));
909 ppa.out = stdout;
910 ppa.last_scaled_size[0] = '\0';
911 ppa.last_p_indexed = -1;
912 ppa.last_p_resolved = -1;
914 error = got_repo_find_pack(&packfile, &pack_hash, repo,
915 packfile_path);
916 if (error)
917 goto done;
919 error = got_object_id_str(&id_str, pack_hash);
920 if (error)
921 goto done;
923 error = got_repo_index_pack(packfile, pack_hash, repo,
924 pack_index_progress, &ppa, check_cancelled, NULL);
925 if (error)
926 goto done;
927 printf("\nIndexed %s.pack\n", id_str);
928 done:
929 if (repo)
930 got_repo_close(repo);
931 if (pack_fds) {
932 const struct got_error *pack_err =
933 got_repo_pack_fds_close(pack_fds);
934 if (error == NULL)
935 error = pack_err;
937 free(id_str);
938 free(pack_hash);
939 return error;
942 __dead static void
943 usage_listpack(void)
945 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
946 getprogname());
947 exit(1);
950 struct gotadmin_list_pack_cb_args {
951 int nblobs;
952 int ntrees;
953 int ncommits;
954 int ntags;
955 int noffdeltas;
956 int nrefdeltas;
957 int human_readable;
958 };
960 static const struct got_error *
961 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
962 off_t size, off_t base_offset, struct got_object_id *base_id)
964 const struct got_error *err;
965 struct gotadmin_list_pack_cb_args *a = arg;
966 char *id_str, *delta_str = NULL, *base_id_str = NULL;
967 const char *type_str;
969 err = got_object_id_str(&id_str, id);
970 if (err)
971 return err;
973 switch (type) {
974 case GOT_OBJ_TYPE_BLOB:
975 type_str = GOT_OBJ_LABEL_BLOB;
976 a->nblobs++;
977 break;
978 case GOT_OBJ_TYPE_TREE:
979 type_str = GOT_OBJ_LABEL_TREE;
980 a->ntrees++;
981 break;
982 case GOT_OBJ_TYPE_COMMIT:
983 type_str = GOT_OBJ_LABEL_COMMIT;
984 a->ncommits++;
985 break;
986 case GOT_OBJ_TYPE_TAG:
987 type_str = GOT_OBJ_LABEL_TAG;
988 a->ntags++;
989 break;
990 case GOT_OBJ_TYPE_OFFSET_DELTA:
991 type_str = "offset-delta";
992 if (asprintf(&delta_str, " base-offset %lld",
993 (long long)base_offset) == -1) {
994 err = got_error_from_errno("asprintf");
995 goto done;
997 a->noffdeltas++;
998 break;
999 case GOT_OBJ_TYPE_REF_DELTA:
1000 type_str = "ref-delta";
1001 err = got_object_id_str(&base_id_str, base_id);
1002 if (err)
1003 goto done;
1004 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1005 err = got_error_from_errno("asprintf");
1006 goto done;
1008 a->nrefdeltas++;
1009 break;
1010 default:
1011 err = got_error(GOT_ERR_OBJ_TYPE);
1012 goto done;
1014 if (a->human_readable) {
1015 char scaled[FMT_SCALED_STRSIZE];
1016 char *s;;
1017 if (fmt_scaled(size, scaled) == -1) {
1018 err = got_error_from_errno("fmt_scaled");
1019 goto done;
1021 s = scaled;
1022 while (isspace((unsigned char)*s))
1023 s++;
1024 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1025 (long long)offset, s, delta_str ? delta_str : "");
1026 } else {
1027 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1028 (long long)offset, (long long)size,
1029 delta_str ? delta_str : "");
1031 done:
1032 free(id_str);
1033 free(base_id_str);
1034 free(delta_str);
1035 return err;
1038 static const struct got_error *
1039 cmd_listpack(int argc, char *argv[])
1041 const struct got_error *error = NULL;
1042 struct got_repository *repo = NULL;
1043 int ch;
1044 struct got_object_id *pack_hash = NULL;
1045 char *packfile_path = NULL;
1046 char *id_str = NULL;
1047 struct gotadmin_list_pack_cb_args lpa;
1048 FILE *packfile = NULL;
1049 int show_stats = 0, human_readable = 0;
1050 int *pack_fds = NULL;
1052 #ifndef PROFILE
1053 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1054 NULL) == -1)
1055 err(1, "pledge");
1056 #endif
1058 while ((ch = getopt(argc, argv, "hs")) != -1) {
1059 switch (ch) {
1060 case 'h':
1061 human_readable = 1;
1062 break;
1063 case 's':
1064 show_stats = 1;
1065 break;
1066 default:
1067 usage_listpack();
1068 /* NOTREACHED */
1072 argc -= optind;
1073 argv += optind;
1075 if (argc != 1)
1076 usage_listpack();
1077 packfile_path = realpath(argv[0], NULL);
1078 if (packfile_path == NULL)
1079 return got_error_from_errno2("realpath", argv[0]);
1081 error = got_repo_pack_fds_open(&pack_fds);
1082 if (error != NULL)
1083 goto done;
1084 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1085 if (error)
1086 goto done;
1087 #ifndef PROFILE
1088 /* Remove "cpath" promise. */
1089 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1090 NULL) == -1)
1091 err(1, "pledge");
1092 #endif
1093 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1094 if (error)
1095 goto done;
1097 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1098 packfile_path);
1099 if (error)
1100 goto done;
1101 error = got_object_id_str(&id_str, pack_hash);
1102 if (error)
1103 goto done;
1105 memset(&lpa, 0, sizeof(lpa));
1106 lpa.human_readable = human_readable;
1107 error = got_repo_list_pack(packfile, pack_hash, repo,
1108 list_pack_cb, &lpa, check_cancelled, NULL);
1109 if (error)
1110 goto done;
1111 if (show_stats) {
1112 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1113 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1114 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1115 lpa.noffdeltas + lpa.nrefdeltas,
1116 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1117 lpa.noffdeltas, lpa.nrefdeltas);
1119 done:
1120 if (repo)
1121 got_repo_close(repo);
1122 if (pack_fds) {
1123 const struct got_error *pack_err =
1124 got_repo_pack_fds_close(pack_fds);
1125 if (error == NULL)
1126 error = pack_err;
1128 free(id_str);
1129 free(pack_hash);
1130 free(packfile_path);
1131 return error;
1134 __dead static void
1135 usage_cleanup(void)
1137 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1138 getprogname());
1139 exit(1);
1142 struct got_cleanup_progress_arg {
1143 int last_nloose;
1144 int last_ncommits;
1145 int last_npurged;
1146 int last_nredundant;
1147 int verbosity;
1148 int printed_something;
1149 int dry_run;
1152 static const struct got_error *
1153 cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1154 int nredundant)
1156 struct got_cleanup_progress_arg *a = arg;
1157 int print_loose = 0, print_commits = 0, print_purged = 0;
1158 int print_redundant = 0;
1160 if (a->last_nloose != nloose) {
1161 print_loose = 1;
1162 a->last_nloose = nloose;
1164 if (a->last_ncommits != ncommits) {
1165 print_loose = 1;
1166 print_commits = 1;
1167 a->last_ncommits = ncommits;
1169 if (a->last_npurged != npurged) {
1170 print_loose = 1;
1171 print_commits = 1;
1172 print_purged = 1;
1173 a->last_npurged = npurged;
1175 if (a->last_nredundant != nredundant) {
1176 print_loose = 1;
1177 print_commits = 1;
1178 print_purged = 1;
1179 print_redundant = 1;
1180 a->last_nredundant = nredundant;
1183 if (a->verbosity < 0)
1184 return NULL;
1186 if (print_loose || print_commits || print_purged || print_redundant)
1187 printf("\r");
1188 if (print_loose)
1189 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1190 if (print_commits)
1191 printf("; %d commit%s scanned", ncommits,
1192 ncommits == 1 ? "" : "s");
1193 if (print_purged || print_redundant) {
1194 if (a->dry_run) {
1195 printf("; could purge %d object%s", npurged,
1196 npurged == 1 ? "" : "s");
1197 } else {
1198 printf("; purged %d object%s", npurged,
1199 npurged == 1 ? "" : "s");
1202 if (print_redundant) {
1203 if (a->dry_run) {
1204 printf(", %d pack file%s", nredundant,
1205 nredundant == 1 ? "" : "s");
1206 } else {
1207 printf(", %d pack file%s", nredundant,
1208 nredundant == 1 ? "" : "s");
1211 if (print_loose || print_commits || print_purged || print_redundant) {
1212 a->printed_something = 1;
1213 fflush(stdout);
1215 return NULL;
1218 struct got_lonely_packidx_progress_arg {
1219 int verbosity;
1220 int printed_something;
1221 int dry_run;
1224 static const struct got_error *
1225 lonely_packidx_progress(void *arg, const char *path)
1227 struct got_lonely_packidx_progress_arg *a = arg;
1229 if (a->verbosity < 0)
1230 return NULL;
1232 if (a->dry_run)
1233 printf("%s could be removed\n", path);
1234 else
1235 printf("%s removed\n", path);
1237 a->printed_something = 1;
1238 return NULL;
1241 static const struct got_error *
1242 cmd_cleanup(int argc, char *argv[])
1244 const struct got_error *error = NULL;
1245 char *repo_path = NULL;
1246 struct got_repository *repo = NULL;
1247 int ch, dry_run = 0, verbosity = 0;
1248 int ncommits = 0, nloose = 0, npacked = 0;
1249 int remove_lonely_packidx = 0, ignore_mtime = 0;
1250 struct got_lockfile *lock = NULL;
1251 struct got_cleanup_progress_arg cpa;
1252 struct got_lonely_packidx_progress_arg lpa;
1253 off_t loose_before, loose_after;
1254 off_t pack_before, pack_after;
1255 off_t total_size;
1256 char loose_before_scaled[FMT_SCALED_STRSIZE];
1257 char loose_after_scaled[FMT_SCALED_STRSIZE];
1258 char pack_before_scaled[FMT_SCALED_STRSIZE];
1259 char pack_after_scaled[FMT_SCALED_STRSIZE];
1260 char total_size_scaled[FMT_SCALED_STRSIZE];
1261 int *pack_fds = NULL;
1263 #ifndef PROFILE
1264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1265 NULL) == -1)
1266 err(1, "pledge");
1267 #endif
1269 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1270 switch (ch) {
1271 case 'a':
1272 ignore_mtime = 1;
1273 break;
1274 case 'n':
1275 dry_run = 1;
1276 break;
1277 case 'p':
1278 remove_lonely_packidx = 1;
1279 break;
1280 case 'q':
1281 verbosity = -1;
1282 break;
1283 case 'r':
1284 repo_path = realpath(optarg, NULL);
1285 if (repo_path == NULL)
1286 return got_error_from_errno2("realpath",
1287 optarg);
1288 got_path_strip_trailing_slashes(repo_path);
1289 break;
1290 default:
1291 usage_cleanup();
1292 /* NOTREACHED */
1296 argc -= optind;
1297 argv += optind;
1299 if (repo_path == NULL) {
1300 error = get_repo_path(&repo_path);
1301 if (error)
1302 goto done;
1304 error = got_repo_pack_fds_open(&pack_fds);
1305 if (error != NULL)
1306 goto done;
1307 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1308 if (error)
1309 goto done;
1311 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1312 if (error)
1313 goto done;
1315 if (got_repo_has_extension(repo, "preciousObjects")) {
1316 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1317 "the preciousObjects Git extension is enabled; "
1318 "this implies that objects must not be deleted");
1319 goto done;
1322 error = got_repo_cleanup_prepare(repo, &lock);
1323 if (error)
1324 goto done;
1326 if (remove_lonely_packidx) {
1327 memset(&lpa, 0, sizeof(lpa));
1328 lpa.dry_run = dry_run;
1329 lpa.verbosity = verbosity;
1330 error = got_repo_remove_lonely_packidx(repo, dry_run,
1331 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1332 goto done;
1335 memset(&cpa, 0, sizeof(cpa));
1336 cpa.last_ncommits = -1;
1337 cpa.last_npurged = -1;
1338 cpa.last_nredundant = -1;
1339 cpa.dry_run = dry_run;
1340 cpa.verbosity = verbosity;
1342 error = got_repo_purge_unreferenced_loose_objects(repo,
1343 &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1344 dry_run, ignore_mtime, cleanup_progress, &cpa,
1345 check_cancelled, NULL);
1346 if (error) {
1347 if (cpa.printed_something)
1348 printf("\n");
1349 goto done;
1352 error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1353 &pack_after, dry_run, ncommits, nloose, npacked,
1354 cleanup_progress, &cpa, check_cancelled, NULL);
1355 if (cpa.printed_something)
1356 printf("\n");
1357 if (error)
1358 goto done;
1360 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1362 if (cpa.printed_something) {
1363 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1364 error = got_error_from_errno("fmt_scaled");
1365 goto done;
1367 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1368 error = got_error_from_errno("fmt_scaled");
1369 goto done;
1371 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1372 error = got_error_from_errno("fmt_scaled");
1373 goto done;
1375 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1376 error = got_error_from_errno("fmt_scaled");
1377 goto done;
1379 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1380 error = got_error_from_errno("fmt_scaled");
1381 goto done;
1383 printf("loose total size before: %s\n", loose_before_scaled);
1384 printf("loose total size after: %s\n", loose_after_scaled);
1385 printf("pack files total size before: %s\n",
1386 pack_before_scaled);
1387 printf("pack files total size after: %s\n", pack_after_scaled);
1388 if (dry_run) {
1389 printf("disk space which would be freed: %s\n",
1390 total_size_scaled);
1391 } else
1392 printf("disk space freed: %s\n", total_size_scaled);
1393 printf("loose objects also found in pack files: %d\n", npacked);
1396 done:
1397 got_repo_cleanup_complete(repo, lock);
1398 if (repo)
1399 got_repo_close(repo);
1400 if (pack_fds) {
1401 const struct got_error *pack_err =
1402 got_repo_pack_fds_close(pack_fds);
1403 if (error == NULL)
1404 error = pack_err;
1406 free(repo_path);
1407 return error;
1410 __dead static void
1411 usage_dump(void)
1413 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1414 "[-x reference] [reference]...\n", getprogname());
1415 exit(1);
1418 static const struct got_error *
1419 cmd_dump(int argc, char *argv[])
1421 const struct got_error *error = NULL;
1422 struct got_pack_progress_arg ppa;
1423 struct got_repository *repo = NULL;
1424 struct got_pathlist_head exclude_args;
1425 struct got_pathlist_entry *pe;
1426 struct got_reflist_head exclude_refs;
1427 struct got_reflist_head include_refs;
1428 struct got_reflist_entry *re, *new;
1429 const char *refname;
1430 char *repo_path = NULL;
1431 int *pack_fds = NULL;
1432 int verbosity = 0;
1433 int i, ch;
1435 TAILQ_INIT(&exclude_args);
1436 TAILQ_INIT(&exclude_refs);
1437 TAILQ_INIT(&include_refs);
1439 #ifndef PROFILE
1440 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1441 NULL) == -1)
1442 err(1, "pledge");
1443 #endif
1445 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1446 switch (ch) {
1447 case 'q':
1448 verbosity = -1;
1449 break;
1450 case 'r':
1451 repo_path = realpath(optarg, NULL);
1452 if (repo_path == NULL)
1453 return got_error_from_errno2("realpath",
1454 optarg);
1455 got_path_strip_trailing_slashes(repo_path);
1456 break;
1457 case 'x':
1458 error = got_pathlist_append(&exclude_args,
1459 optarg, NULL);
1460 if (error)
1461 return error;
1462 break;
1463 default:
1464 usage_dump();
1465 /* NOTREACHED */
1468 argc -= optind;
1469 argv += optind;
1471 if (repo_path == NULL) {
1472 error = get_repo_path(&repo_path);
1473 if (error)
1474 goto done;
1476 error = got_repo_pack_fds_open(&pack_fds);
1477 if (error != NULL)
1478 goto done;
1479 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1480 if (error)
1481 goto done;
1483 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1484 if (error)
1485 goto done;
1487 TAILQ_FOREACH(pe, &exclude_args, entry) {
1488 refname = pe->path;
1489 error = add_ref(&new, &exclude_refs, refname, repo);
1490 if (error)
1491 goto done;
1494 if (argc == 0) {
1495 error = got_ref_list(&include_refs, repo, "",
1496 got_ref_cmp_by_name, NULL);
1497 if (error)
1498 goto done;
1499 } else {
1500 for (i = 0; i < argc; i++) {
1501 got_path_strip_trailing_slashes(argv[i]);
1502 refname = argv[i];
1503 error = add_ref(&new, &include_refs, refname, repo);
1504 if (error)
1505 goto done;
1509 /* Ignore references in the refs/got/ namespace. */
1510 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1511 refname = got_ref_get_name(re->ref);
1512 if (strncmp("refs/got/", refname, 9) != 0)
1513 continue;
1514 TAILQ_REMOVE(&include_refs, re, entry);
1515 got_ref_close(re->ref);
1516 free(re);
1519 memset(&ppa, 0, sizeof(ppa));
1520 ppa.out = stderr;
1521 ppa.verbosity = verbosity;
1523 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1524 repo, pack_progress, &ppa, check_cancelled, NULL);
1525 if (ppa.printed_something)
1526 fprintf(stderr, "\n");
1527 done:
1528 if (repo)
1529 got_repo_close(repo);
1531 if (pack_fds) {
1532 const struct got_error *pack_err;
1534 pack_err = got_repo_pack_fds_close(pack_fds);
1535 if (error == NULL)
1536 error = pack_err;
1539 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1540 got_ref_list_free(&exclude_refs);
1541 got_ref_list_free(&include_refs);
1542 free(repo_path);
1544 return error;