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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <ctype.h>
23 #include <getopt.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_repository_dump.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);
89 __dead static void usage_dump(void);
91 static const struct got_error* cmd_init(int, char *[]);
92 static const struct got_error* cmd_info(int, char *[]);
93 static const struct got_error* cmd_pack(int, char *[]);
94 static const struct got_error* cmd_indexpack(int, char *[]);
95 static const struct got_error* cmd_listpack(int, char *[]);
96 static const struct got_error* cmd_cleanup(int, char *[]);
97 static const struct got_error* cmd_dump(int, char *[]);
99 static const struct gotadmin_cmd gotadmin_commands[] = {
100 { "init", cmd_init, usage_init, "" },
101 { "info", cmd_info, usage_info, "" },
102 { "pack", cmd_pack, usage_pack, "" },
103 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
104 { "listpack", cmd_listpack, usage_listpack, "ls" },
105 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
106 { "dump", cmd_dump, usage_dump, "" },
107 };
109 static void
110 list_commands(FILE *fp)
112 size_t i;
114 fprintf(fp, "commands:");
115 for (i = 0; i < nitems(gotadmin_commands); i++) {
116 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
117 fprintf(fp, " %s", cmd->cmd_name);
119 fputc('\n', fp);
122 int
123 main(int argc, char *argv[])
125 const struct gotadmin_cmd *cmd;
126 size_t i;
127 int ch;
128 int hflag = 0, Vflag = 0;
129 static const struct option longopts[] = {
130 { "version", no_argument, NULL, 'V' },
131 { NULL, 0, NULL, 0 }
132 };
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
137 switch (ch) {
138 case 'h':
139 hflag = 1;
140 break;
141 case 'V':
142 Vflag = 1;
143 break;
144 default:
145 usage(hflag, 1);
146 /* NOTREACHED */
150 argc -= optind;
151 argv += optind;
152 optind = 1;
153 optreset = 1;
155 if (Vflag) {
156 got_version_print_str();
157 return 0;
160 if (argc <= 0)
161 usage(hflag, hflag ? 0 : 1);
163 signal(SIGINT, catch_sigint);
164 signal(SIGPIPE, catch_sigpipe);
166 for (i = 0; i < nitems(gotadmin_commands); i++) {
167 const struct got_error *error;
169 cmd = &gotadmin_commands[i];
171 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
172 strcmp(cmd->cmd_alias, argv[0]) != 0)
173 continue;
175 if (hflag)
176 cmd->cmd_usage();
178 error = cmd->cmd_main(argc, argv);
179 if (error && error->code != GOT_ERR_CANCELLED &&
180 error->code != GOT_ERR_PRIVSEP_EXIT &&
181 !(sigpipe_received &&
182 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
183 !(sigint_received &&
184 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
185 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
186 return 1;
189 return 0;
192 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
193 list_commands(stderr);
194 return 1;
197 __dead static void
198 usage(int hflag, int status)
200 FILE *fp = (status == 0) ? stdout : stderr;
202 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
203 getprogname());
204 if (hflag)
205 list_commands(fp);
206 exit(status);
209 static const struct got_error *
210 apply_unveil(const char *repo_path, int repo_read_only)
212 const struct got_error *err;
214 #ifdef PROFILE
215 if (unveil("gmon.out", "rwc") != 0)
216 return got_error_from_errno2("unveil", "gmon.out");
217 #endif
218 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
219 return got_error_from_errno2("unveil", repo_path);
221 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
222 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
224 err = got_privsep_unveil_exec_helpers();
225 if (err != NULL)
226 return err;
228 if (unveil(NULL, NULL) != 0)
229 return got_error_from_errno("unveil");
231 return NULL;
234 __dead static void
235 usage_info(void)
237 fprintf(stderr, "usage: %s info [-r repository-path]\n",
238 getprogname());
239 exit(1);
242 static const struct got_error *
243 get_repo_path(char **repo_path)
245 const struct got_error *err = NULL;
246 struct got_worktree *worktree = NULL;
247 char *cwd;
249 *repo_path = NULL;
251 cwd = getcwd(NULL, 0);
252 if (cwd == NULL)
253 return got_error_from_errno("getcwd");
255 err = got_worktree_open(&worktree, cwd);
256 if (err) {
257 if (err->code != GOT_ERR_NOT_WORKTREE)
258 goto done;
259 err = NULL;
262 if (worktree)
263 *repo_path = strdup(got_worktree_get_repo_path(worktree));
264 else
265 *repo_path = strdup(cwd);
266 if (*repo_path == NULL)
267 err = got_error_from_errno("strdup");
268 done:
269 if (worktree)
270 got_worktree_close(worktree);
271 free(cwd);
272 return err;
275 __dead static void
276 usage_init(void)
278 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
279 getprogname());
280 exit(1);
283 static const struct got_error *
284 cmd_init(int argc, char *argv[])
286 const struct got_error *error = NULL;
287 const char *head_name = NULL;
288 char *repo_path = NULL;
289 int ch;
291 #ifndef PROFILE
292 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
293 err(1, "pledge");
294 #endif
296 while ((ch = getopt(argc, argv, "b:")) != -1) {
297 switch (ch) {
298 case 'b':
299 head_name = optarg;
300 break;
301 default:
302 usage_init();
303 /* NOTREACHED */
307 argc -= optind;
308 argv += optind;
310 if (argc != 1)
311 usage_init();
313 repo_path = strdup(argv[0]);
314 if (repo_path == NULL)
315 return got_error_from_errno("strdup");
317 got_path_strip_trailing_slashes(repo_path);
319 error = got_path_mkdir(repo_path);
320 if (error &&
321 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
322 goto done;
324 error = apply_unveil(repo_path, 0);
325 if (error)
326 goto done;
328 error = got_repo_init(repo_path, head_name);
329 done:
330 free(repo_path);
331 return error;
334 static const struct got_error *
335 cmd_info(int argc, char *argv[])
337 const struct got_error *error = NULL;
338 char *repo_path = NULL;
339 struct got_repository *repo = NULL;
340 const struct got_gotconfig *gotconfig = NULL;
341 int ch, npackfiles, npackedobj, nobj;
342 off_t packsize, loose_size;
343 char scaled[FMT_SCALED_STRSIZE];
344 int *pack_fds = NULL;
346 #ifndef PROFILE
347 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
348 NULL) == -1)
349 err(1, "pledge");
350 #endif
352 while ((ch = getopt(argc, argv, "r:")) != -1) {
353 switch (ch) {
354 case 'r':
355 repo_path = realpath(optarg, NULL);
356 if (repo_path == NULL)
357 return got_error_from_errno2("realpath",
358 optarg);
359 got_path_strip_trailing_slashes(repo_path);
360 break;
361 default:
362 usage_info();
363 /* NOTREACHED */
367 argc -= optind;
368 argv += optind;
370 if (repo_path == NULL) {
371 error = get_repo_path(&repo_path);
372 if (error)
373 goto done;
375 error = got_repo_pack_fds_open(&pack_fds);
376 if (error != NULL)
377 goto done;
378 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
379 if (error)
380 goto done;
381 #ifndef PROFILE
382 /* Remove "cpath" promise. */
383 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
384 NULL) == -1)
385 err(1, "pledge");
386 #endif
387 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
388 if (error)
389 goto done;
391 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
393 gotconfig = got_repo_get_gotconfig(repo);
394 if (gotconfig) {
395 const struct got_remote_repo *remotes;
396 int i, nremotes;
397 if (got_gotconfig_get_author(gotconfig)) {
398 printf("default author: %s\n",
399 got_gotconfig_get_author(gotconfig));
401 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
402 for (i = 0; i < nremotes; i++) {
403 const char *fetch_url = remotes[i].fetch_url;
404 const char *send_url = remotes[i].send_url;
405 if (strcmp(fetch_url, send_url) == 0) {
406 printf("remote \"%s\": %s\n", remotes[i].name,
407 remotes[i].fetch_url);
408 } else {
409 printf("remote \"%s\" (fetch): %s\n",
410 remotes[i].name, remotes[i].fetch_url);
411 printf("remote \"%s\" (send): %s\n",
412 remotes[i].name, remotes[i].send_url);
417 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
418 &packsize, repo);
419 if (error)
420 goto done;
421 printf("pack files: %d\n", npackfiles);
422 if (npackfiles > 0) {
423 if (fmt_scaled(packsize, scaled) == -1) {
424 error = got_error_from_errno("fmt_scaled");
425 goto done;
427 printf("packed objects: %d\n", npackedobj);
428 printf("packed total size: %s\n", scaled);
431 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
432 if (error)
433 goto done;
434 printf("loose objects: %d\n", nobj);
435 if (nobj > 0) {
436 if (fmt_scaled(loose_size, scaled) == -1) {
437 error = got_error_from_errno("fmt_scaled");
438 goto done;
440 printf("loose total size: %s\n", scaled);
442 done:
443 if (repo)
444 got_repo_close(repo);
445 if (pack_fds) {
446 const struct got_error *pack_err =
447 got_repo_pack_fds_close(pack_fds);
448 if (error == NULL)
449 error = pack_err;
452 free(repo_path);
453 return error;
456 __dead static void
457 usage_pack(void)
459 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
460 "[-x reference] [reference ...]\n", getprogname());
461 exit(1);
464 struct got_pack_progress_arg {
465 FILE *out;
466 char last_scaled_size[FMT_SCALED_STRSIZE];
467 int last_ncolored;
468 int last_nfound;
469 int last_ntrees;
470 int loading_done;
471 int last_ncommits;
472 int last_nobj_total;
473 int last_p_deltify;
474 int last_p_written;
475 int last_p_indexed;
476 int last_p_resolved;
477 int verbosity;
478 int printed_something;
479 };
481 static void
482 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
483 int ncolored, int nfound, int ntrees)
485 if (print_colored) {
486 fprintf(out, "%d commit%s colored", ncolored,
487 ncolored == 1 ? "" : "s");
489 if (print_found) {
490 fprintf(out, "%s%d object%s found",
491 ncolored > 0 ? "; " : "",
492 nfound, nfound == 1 ? "" : "s");
494 if (print_trees) {
495 fprintf(out, "; %d tree%s scanned", ntrees,
496 ntrees == 1 ? "" : "s");
500 static const struct got_error *
501 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
502 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
503 int nobj_written)
505 struct got_pack_progress_arg *a = arg;
506 char scaled_size[FMT_SCALED_STRSIZE];
507 int p_deltify, p_written;
508 int print_colored = 0, print_found = 0, print_trees = 0;
509 int print_searching = 0, print_total = 0;
510 int print_deltify = 0, print_written = 0;
512 if (a->verbosity < 0)
513 return NULL;
515 if (a->last_ncolored != ncolored) {
516 print_colored = 1;
517 a->last_ncolored = ncolored;
520 if (a->last_nfound != nfound) {
521 print_colored = 1;
522 print_found = 1;
523 a->last_nfound = nfound;
526 if (a->last_ntrees != ntrees) {
527 print_colored = 1;
528 print_found = 1;
529 print_trees = 1;
530 a->last_ntrees = ntrees;
533 if ((print_colored || print_found || print_trees) &&
534 !a->loading_done) {
535 fprintf(a->out, "\r");
536 print_load_info(a->out, print_colored, print_found,
537 print_trees, ncolored, nfound, ntrees);
538 a->printed_something = 1;
539 fflush(a->out);
540 return NULL;
541 } else if (!a->loading_done) {
542 fprintf(a->out, "\r");
543 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
544 fprintf(a->out, "\n");
545 a->loading_done = 1;
548 if (fmt_scaled(packfile_size, scaled_size) == -1)
549 return got_error_from_errno("fmt_scaled");
551 if (a->last_ncommits != ncommits) {
552 print_searching = 1;
553 a->last_ncommits = ncommits;
556 if (a->last_nobj_total != nobj_total) {
557 print_searching = 1;
558 print_total = 1;
559 a->last_nobj_total = nobj_total;
562 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
563 strcmp(scaled_size, a->last_scaled_size)) != 0) {
564 if (strlcpy(a->last_scaled_size, scaled_size,
565 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
566 return got_error(GOT_ERR_NO_SPACE);
569 if (nobj_deltify > 0 || nobj_written > 0) {
570 if (nobj_deltify > 0) {
571 p_deltify = (nobj_deltify * 100) / nobj_total;
572 if (p_deltify != a->last_p_deltify) {
573 a->last_p_deltify = p_deltify;
574 print_searching = 1;
575 print_total = 1;
576 print_deltify = 1;
579 if (nobj_written > 0) {
580 p_written = (nobj_written * 100) / nobj_total;
581 if (p_written != a->last_p_written) {
582 a->last_p_written = p_written;
583 print_searching = 1;
584 print_total = 1;
585 print_deltify = 1;
586 print_written = 1;
591 if (print_searching || print_total || print_deltify || print_written)
592 fprintf(a->out, "\r");
593 if (print_searching)
594 fprintf(a->out, "packing %d reference%s", ncommits,
595 ncommits == 1 ? "" : "s");
596 if (print_total)
597 fprintf(a->out, "; %d object%s", nobj_total,
598 nobj_total == 1 ? "" : "s");
599 if (print_deltify)
600 fprintf(a->out, "; deltify: %d%%", p_deltify);
601 if (print_written)
602 fprintf(a->out, "; writing pack: %*s %d%%",
603 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
604 if (print_searching || print_total || print_deltify ||
605 print_written) {
606 a->printed_something = 1;
607 fflush(a->out);
609 return NULL;
612 static const struct got_error *
613 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
614 int nobj_indexed, int nobj_loose, int nobj_resolved)
616 struct got_pack_progress_arg *a = arg;
617 char scaled_size[FMT_SCALED_STRSIZE];
618 int p_indexed, p_resolved;
619 int print_size = 0, print_indexed = 0, print_resolved = 0;
621 if (a->verbosity < 0)
622 return NULL;
624 if (packfile_size > 0 || nobj_indexed > 0) {
625 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
626 (a->last_scaled_size[0] == '\0' ||
627 strcmp(scaled_size, a->last_scaled_size)) != 0) {
628 print_size = 1;
629 if (strlcpy(a->last_scaled_size, scaled_size,
630 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
631 return got_error(GOT_ERR_NO_SPACE);
633 if (nobj_indexed > 0) {
634 p_indexed = (nobj_indexed * 100) / nobj_total;
635 if (p_indexed != a->last_p_indexed) {
636 a->last_p_indexed = p_indexed;
637 print_indexed = 1;
638 print_size = 1;
641 if (nobj_resolved > 0) {
642 p_resolved = (nobj_resolved * 100) /
643 (nobj_total - nobj_loose);
644 if (p_resolved != a->last_p_resolved) {
645 a->last_p_resolved = p_resolved;
646 print_resolved = 1;
647 print_indexed = 1;
648 print_size = 1;
653 if (print_size || print_indexed || print_resolved)
654 printf("\r");
655 if (print_size)
656 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
657 if (print_indexed)
658 printf("; indexing %d%%", p_indexed);
659 if (print_resolved)
660 printf("; resolving deltas %d%%", p_resolved);
661 if (print_size || print_indexed || print_resolved)
662 fflush(stdout);
664 return NULL;
667 static const struct got_error *
668 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
669 const char *refname, struct got_repository *repo)
671 const struct got_error *err;
672 struct got_reference *ref;
674 *new = NULL;
676 err = got_ref_open(&ref, repo, refname, 0);
677 if (err) {
678 if (err->code != GOT_ERR_NOT_REF)
679 return err;
681 /* Treat argument as a reference prefix. */
682 err = got_ref_list(refs, repo, refname,
683 got_ref_cmp_by_name, NULL);
684 } else {
685 err = got_reflist_insert(new, refs, ref,
686 got_ref_cmp_by_name, NULL);
687 if (err || *new == NULL /* duplicate */)
688 got_ref_close(ref);
691 return err;
694 static const struct got_error *
695 cmd_pack(int argc, char *argv[])
697 const struct got_error *error = NULL;
698 char *repo_path = NULL;
699 struct got_repository *repo = NULL;
700 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
701 struct got_object_id *pack_hash = NULL;
702 char *id_str = NULL;
703 struct got_pack_progress_arg ppa;
704 FILE *packfile = NULL;
705 struct got_pathlist_head exclude_args;
706 struct got_pathlist_entry *pe;
707 struct got_reflist_head exclude_refs;
708 struct got_reflist_head include_refs;
709 struct got_reflist_entry *re, *new;
710 int *pack_fds = NULL;
712 TAILQ_INIT(&exclude_args);
713 TAILQ_INIT(&exclude_refs);
714 TAILQ_INIT(&include_refs);
716 #ifndef PROFILE
717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
718 NULL) == -1)
719 err(1, "pledge");
720 #endif
722 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
723 switch (ch) {
724 case 'a':
725 loose_obj_only = 0;
726 break;
727 case 'D':
728 force_refdelta = 1;
729 break;
730 case 'q':
731 verbosity = -1;
732 break;
733 case 'r':
734 repo_path = realpath(optarg, NULL);
735 if (repo_path == NULL)
736 return got_error_from_errno2("realpath",
737 optarg);
738 got_path_strip_trailing_slashes(repo_path);
739 break;
740 case 'x':
741 got_path_strip_trailing_slashes(optarg);
742 error = got_pathlist_append(&exclude_args,
743 optarg, NULL);
744 if (error)
745 return error;
746 break;
747 default:
748 usage_pack();
749 /* NOTREACHED */
753 argc -= optind;
754 argv += optind;
756 if (repo_path == NULL) {
757 error = get_repo_path(&repo_path);
758 if (error)
759 goto done;
761 error = got_repo_pack_fds_open(&pack_fds);
762 if (error != NULL)
763 goto done;
764 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
765 if (error)
766 goto done;
768 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
769 if (error)
770 goto done;
772 TAILQ_FOREACH(pe, &exclude_args, entry) {
773 const char *refname = pe->path;
774 error = add_ref(&new, &exclude_refs, refname, repo);
775 if (error)
776 goto done;
779 if (argc == 0) {
780 error = got_ref_list(&include_refs, repo, "",
781 got_ref_cmp_by_name, NULL);
782 if (error)
783 goto done;
784 } else {
785 for (i = 0; i < argc; i++) {
786 const char *refname;
787 got_path_strip_trailing_slashes(argv[i]);
788 refname = argv[i];
789 error = add_ref(&new, &include_refs, refname, repo);
790 if (error)
791 goto done;
795 /* Ignore references in the refs/got/ namespace. */
796 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
797 const char *refname = got_ref_get_name(re->ref);
798 if (strncmp("refs/got/", refname, 9) != 0)
799 continue;
800 TAILQ_REMOVE(&include_refs, re, entry);
801 got_ref_close(re->ref);
802 free(re);
805 memset(&ppa, 0, sizeof(ppa));
806 ppa.out = stdout;
807 ppa.last_scaled_size[0] = '\0';
808 ppa.last_p_indexed = -1;
809 ppa.last_p_resolved = -1;
810 ppa.verbosity = verbosity;
812 error = got_repo_pack_objects(&packfile, &pack_hash,
813 &include_refs, &exclude_refs, repo, loose_obj_only,
814 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
815 if (error) {
816 if (ppa.printed_something)
817 printf("\n");
818 goto done;
821 error = got_object_id_str(&id_str, pack_hash);
822 if (error)
823 goto done;
824 if (verbosity >= 0)
825 printf("\nWrote %s.pack\n", id_str);
827 error = got_repo_index_pack(packfile, pack_hash, repo,
828 pack_index_progress, &ppa, check_cancelled, NULL);
829 if (error)
830 goto done;
831 if (verbosity >= 0)
832 printf("\nIndexed %s.pack\n", id_str);
833 done:
834 if (repo)
835 got_repo_close(repo);
836 if (pack_fds) {
837 const struct got_error *pack_err =
838 got_repo_pack_fds_close(pack_fds);
839 if (error == NULL)
840 error = pack_err;
842 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
843 got_ref_list_free(&exclude_refs);
844 got_ref_list_free(&include_refs);
845 free(id_str);
846 free(pack_hash);
847 free(repo_path);
848 return error;
851 __dead static void
852 usage_indexpack(void)
854 fprintf(stderr, "usage: %s indexpack packfile-path\n",
855 getprogname());
856 exit(1);
859 static const struct got_error *
860 cmd_indexpack(int argc, char *argv[])
862 const struct got_error *error = NULL;
863 struct got_repository *repo = NULL;
864 int ch;
865 struct got_object_id *pack_hash = NULL;
866 char *packfile_path = NULL;
867 char *id_str = NULL;
868 struct got_pack_progress_arg ppa;
869 FILE *packfile = NULL;
870 int *pack_fds = NULL;
872 while ((ch = getopt(argc, argv, "")) != -1) {
873 switch (ch) {
874 default:
875 usage_indexpack();
876 /* NOTREACHED */
880 argc -= optind;
881 argv += optind;
883 if (argc != 1)
884 usage_indexpack();
886 packfile_path = realpath(argv[0], NULL);
887 if (packfile_path == NULL)
888 return got_error_from_errno2("realpath", argv[0]);
890 #ifndef PROFILE
891 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
892 NULL) == -1)
893 err(1, "pledge");
894 #endif
896 error = got_repo_pack_fds_open(&pack_fds);
897 if (error != NULL)
898 goto done;
899 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
900 if (error)
901 goto done;
903 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
904 if (error)
905 goto done;
907 memset(&ppa, 0, sizeof(ppa));
908 ppa.out = stdout;
909 ppa.last_scaled_size[0] = '\0';
910 ppa.last_p_indexed = -1;
911 ppa.last_p_resolved = -1;
913 error = got_repo_find_pack(&packfile, &pack_hash, repo,
914 packfile_path);
915 if (error)
916 goto done;
918 error = got_object_id_str(&id_str, pack_hash);
919 if (error)
920 goto done;
922 error = got_repo_index_pack(packfile, pack_hash, repo,
923 pack_index_progress, &ppa, check_cancelled, NULL);
924 if (error)
925 goto done;
926 printf("\nIndexed %s.pack\n", id_str);
927 done:
928 if (repo)
929 got_repo_close(repo);
930 if (pack_fds) {
931 const struct got_error *pack_err =
932 got_repo_pack_fds_close(pack_fds);
933 if (error == NULL)
934 error = pack_err;
936 free(id_str);
937 free(pack_hash);
938 return error;
941 __dead static void
942 usage_listpack(void)
944 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
945 getprogname());
946 exit(1);
949 struct gotadmin_list_pack_cb_args {
950 int nblobs;
951 int ntrees;
952 int ncommits;
953 int ntags;
954 int noffdeltas;
955 int nrefdeltas;
956 int human_readable;
957 };
959 static const struct got_error *
960 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
961 off_t size, off_t base_offset, struct got_object_id *base_id)
963 const struct got_error *err;
964 struct gotadmin_list_pack_cb_args *a = arg;
965 char *id_str, *delta_str = NULL, *base_id_str = NULL;
966 const char *type_str;
968 err = got_object_id_str(&id_str, id);
969 if (err)
970 return err;
972 switch (type) {
973 case GOT_OBJ_TYPE_BLOB:
974 type_str = GOT_OBJ_LABEL_BLOB;
975 a->nblobs++;
976 break;
977 case GOT_OBJ_TYPE_TREE:
978 type_str = GOT_OBJ_LABEL_TREE;
979 a->ntrees++;
980 break;
981 case GOT_OBJ_TYPE_COMMIT:
982 type_str = GOT_OBJ_LABEL_COMMIT;
983 a->ncommits++;
984 break;
985 case GOT_OBJ_TYPE_TAG:
986 type_str = GOT_OBJ_LABEL_TAG;
987 a->ntags++;
988 break;
989 case GOT_OBJ_TYPE_OFFSET_DELTA:
990 type_str = "offset-delta";
991 if (asprintf(&delta_str, " base-offset %lld",
992 (long long)base_offset) == -1) {
993 err = got_error_from_errno("asprintf");
994 goto done;
996 a->noffdeltas++;
997 break;
998 case GOT_OBJ_TYPE_REF_DELTA:
999 type_str = "ref-delta";
1000 err = got_object_id_str(&base_id_str, base_id);
1001 if (err)
1002 goto done;
1003 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1007 a->nrefdeltas++;
1008 break;
1009 default:
1010 err = got_error(GOT_ERR_OBJ_TYPE);
1011 goto done;
1013 if (a->human_readable) {
1014 char scaled[FMT_SCALED_STRSIZE];
1015 char *s;;
1016 if (fmt_scaled(size, scaled) == -1) {
1017 err = got_error_from_errno("fmt_scaled");
1018 goto done;
1020 s = scaled;
1021 while (isspace((unsigned char)*s))
1022 s++;
1023 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1024 (long long)offset, s, delta_str ? delta_str : "");
1025 } else {
1026 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1027 (long long)offset, (long long)size,
1028 delta_str ? delta_str : "");
1030 done:
1031 free(id_str);
1032 free(base_id_str);
1033 free(delta_str);
1034 return err;
1037 static const struct got_error *
1038 cmd_listpack(int argc, char *argv[])
1040 const struct got_error *error = NULL;
1041 struct got_repository *repo = NULL;
1042 int ch;
1043 struct got_object_id *pack_hash = NULL;
1044 char *packfile_path = NULL;
1045 char *id_str = NULL;
1046 struct gotadmin_list_pack_cb_args lpa;
1047 FILE *packfile = NULL;
1048 int show_stats = 0, human_readable = 0;
1049 int *pack_fds = NULL;
1051 #ifndef PROFILE
1052 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1053 NULL) == -1)
1054 err(1, "pledge");
1055 #endif
1057 while ((ch = getopt(argc, argv, "hs")) != -1) {
1058 switch (ch) {
1059 case 'h':
1060 human_readable = 1;
1061 break;
1062 case 's':
1063 show_stats = 1;
1064 break;
1065 default:
1066 usage_listpack();
1067 /* NOTREACHED */
1071 argc -= optind;
1072 argv += optind;
1074 if (argc != 1)
1075 usage_listpack();
1076 packfile_path = realpath(argv[0], NULL);
1077 if (packfile_path == NULL)
1078 return got_error_from_errno2("realpath", argv[0]);
1080 error = got_repo_pack_fds_open(&pack_fds);
1081 if (error != NULL)
1082 goto done;
1083 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1084 if (error)
1085 goto done;
1086 #ifndef PROFILE
1087 /* Remove "cpath" promise. */
1088 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1089 NULL) == -1)
1090 err(1, "pledge");
1091 #endif
1092 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1093 if (error)
1094 goto done;
1096 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1097 packfile_path);
1098 if (error)
1099 goto done;
1100 error = got_object_id_str(&id_str, pack_hash);
1101 if (error)
1102 goto done;
1104 memset(&lpa, 0, sizeof(lpa));
1105 lpa.human_readable = human_readable;
1106 error = got_repo_list_pack(packfile, pack_hash, repo,
1107 list_pack_cb, &lpa, check_cancelled, NULL);
1108 if (error)
1109 goto done;
1110 if (show_stats) {
1111 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1112 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1113 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1114 lpa.noffdeltas + lpa.nrefdeltas,
1115 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1116 lpa.noffdeltas, lpa.nrefdeltas);
1118 done:
1119 if (repo)
1120 got_repo_close(repo);
1121 if (pack_fds) {
1122 const struct got_error *pack_err =
1123 got_repo_pack_fds_close(pack_fds);
1124 if (error == NULL)
1125 error = pack_err;
1127 free(id_str);
1128 free(pack_hash);
1129 free(packfile_path);
1130 return error;
1133 __dead static void
1134 usage_cleanup(void)
1136 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1137 getprogname());
1138 exit(1);
1141 struct got_cleanup_progress_arg {
1142 int last_nloose;
1143 int last_ncommits;
1144 int last_npurged;
1145 int last_nredundant;
1146 int verbosity;
1147 int printed_something;
1148 int dry_run;
1151 static const struct got_error *
1152 cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1153 int nredundant)
1155 struct got_cleanup_progress_arg *a = arg;
1156 int print_loose = 0, print_commits = 0, print_purged = 0;
1157 int print_redundant = 0;
1159 if (a->last_nloose != nloose) {
1160 print_loose = 1;
1161 a->last_nloose = nloose;
1163 if (a->last_ncommits != ncommits) {
1164 print_loose = 1;
1165 print_commits = 1;
1166 a->last_ncommits = ncommits;
1168 if (a->last_npurged != npurged) {
1169 print_loose = 1;
1170 print_commits = 1;
1171 print_purged = 1;
1172 a->last_npurged = npurged;
1174 if (a->last_nredundant != nredundant) {
1175 print_loose = 1;
1176 print_commits = 1;
1177 print_purged = 1;
1178 print_redundant = 1;
1179 a->last_nredundant = nredundant;
1182 if (a->verbosity < 0)
1183 return NULL;
1185 if (print_loose || print_commits || print_purged || print_redundant)
1186 printf("\r");
1187 if (print_loose)
1188 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1189 if (print_commits)
1190 printf("; %d commit%s scanned", ncommits,
1191 ncommits == 1 ? "" : "s");
1192 if (print_purged || print_redundant) {
1193 if (a->dry_run) {
1194 printf("; could purge %d object%s", npurged,
1195 npurged == 1 ? "" : "s");
1196 } else {
1197 printf("; purged %d object%s", npurged,
1198 npurged == 1 ? "" : "s");
1201 if (print_redundant) {
1202 if (a->dry_run) {
1203 printf(", %d pack file%s", nredundant,
1204 nredundant == 1 ? "" : "s");
1205 } else {
1206 printf(", %d pack file%s", nredundant,
1207 nredundant == 1 ? "" : "s");
1210 if (print_loose || print_commits || print_purged || print_redundant) {
1211 a->printed_something = 1;
1212 fflush(stdout);
1214 return NULL;
1217 struct got_lonely_packidx_progress_arg {
1218 int verbosity;
1219 int printed_something;
1220 int dry_run;
1223 static const struct got_error *
1224 lonely_packidx_progress(void *arg, const char *path)
1226 struct got_lonely_packidx_progress_arg *a = arg;
1228 if (a->verbosity < 0)
1229 return NULL;
1231 if (a->dry_run)
1232 printf("%s could be removed\n", path);
1233 else
1234 printf("%s removed\n", path);
1236 a->printed_something = 1;
1237 return NULL;
1240 static const struct got_error *
1241 cmd_cleanup(int argc, char *argv[])
1243 const struct got_error *error = NULL;
1244 char *repo_path = NULL;
1245 struct got_repository *repo = NULL;
1246 int ch, dry_run = 0, verbosity = 0;
1247 int ncommits = 0, nloose = 0, npacked = 0;
1248 int remove_lonely_packidx = 0, ignore_mtime = 0;
1249 struct got_lockfile *lock = NULL;
1250 struct got_cleanup_progress_arg cpa;
1251 struct got_lonely_packidx_progress_arg lpa;
1252 off_t loose_before, loose_after;
1253 off_t pack_before, pack_after;
1254 off_t total_size;
1255 char loose_before_scaled[FMT_SCALED_STRSIZE];
1256 char loose_after_scaled[FMT_SCALED_STRSIZE];
1257 char pack_before_scaled[FMT_SCALED_STRSIZE];
1258 char pack_after_scaled[FMT_SCALED_STRSIZE];
1259 char total_size_scaled[FMT_SCALED_STRSIZE];
1260 int *pack_fds = NULL;
1262 #ifndef PROFILE
1263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1264 NULL) == -1)
1265 err(1, "pledge");
1266 #endif
1268 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1269 switch (ch) {
1270 case 'a':
1271 ignore_mtime = 1;
1272 break;
1273 case 'n':
1274 dry_run = 1;
1275 break;
1276 case 'p':
1277 remove_lonely_packidx = 1;
1278 break;
1279 case 'q':
1280 verbosity = -1;
1281 break;
1282 case 'r':
1283 repo_path = realpath(optarg, NULL);
1284 if (repo_path == NULL)
1285 return got_error_from_errno2("realpath",
1286 optarg);
1287 got_path_strip_trailing_slashes(repo_path);
1288 break;
1289 default:
1290 usage_cleanup();
1291 /* NOTREACHED */
1295 argc -= optind;
1296 argv += optind;
1298 if (repo_path == NULL) {
1299 error = get_repo_path(&repo_path);
1300 if (error)
1301 goto done;
1303 error = got_repo_pack_fds_open(&pack_fds);
1304 if (error != NULL)
1305 goto done;
1306 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1307 if (error)
1308 goto done;
1310 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1311 if (error)
1312 goto done;
1314 if (got_repo_has_extension(repo, "preciousObjects")) {
1315 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1316 "the preciousObjects Git extension is enabled; "
1317 "this implies that objects must not be deleted");
1318 goto done;
1321 error = got_repo_cleanup_prepare(repo, &lock);
1322 if (error)
1323 goto done;
1325 if (remove_lonely_packidx) {
1326 memset(&lpa, 0, sizeof(lpa));
1327 lpa.dry_run = dry_run;
1328 lpa.verbosity = verbosity;
1329 error = got_repo_remove_lonely_packidx(repo, dry_run,
1330 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1331 goto done;
1334 memset(&cpa, 0, sizeof(cpa));
1335 cpa.last_ncommits = -1;
1336 cpa.last_npurged = -1;
1337 cpa.last_nredundant = -1;
1338 cpa.dry_run = dry_run;
1339 cpa.verbosity = verbosity;
1341 error = got_repo_purge_unreferenced_loose_objects(repo,
1342 &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1343 dry_run, ignore_mtime, cleanup_progress, &cpa,
1344 check_cancelled, NULL);
1345 if (error) {
1346 if (cpa.printed_something)
1347 printf("\n");
1348 goto done;
1351 error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1352 &pack_after, dry_run, ncommits, nloose, npacked,
1353 cleanup_progress, &cpa, check_cancelled, NULL);
1354 if (cpa.printed_something)
1355 printf("\n");
1356 if (error)
1357 goto done;
1359 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1361 if (cpa.printed_something) {
1362 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1363 error = got_error_from_errno("fmt_scaled");
1364 goto done;
1366 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1367 error = got_error_from_errno("fmt_scaled");
1368 goto done;
1370 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1371 error = got_error_from_errno("fmt_scaled");
1372 goto done;
1374 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1375 error = got_error_from_errno("fmt_scaled");
1376 goto done;
1378 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1379 error = got_error_from_errno("fmt_scaled");
1380 goto done;
1382 printf("loose total size before: %s\n", loose_before_scaled);
1383 printf("loose total size after: %s\n", loose_after_scaled);
1384 printf("pack files total size before: %s\n",
1385 pack_before_scaled);
1386 printf("pack files total size after: %s\n", pack_after_scaled);
1387 if (dry_run) {
1388 printf("disk space which would be freed: %s\n",
1389 total_size_scaled);
1390 } else
1391 printf("disk space freed: %s\n", total_size_scaled);
1392 printf("loose objects also found in pack files: %d\n", npacked);
1395 done:
1396 got_repo_cleanup_complete(repo, lock);
1397 if (repo)
1398 got_repo_close(repo);
1399 if (pack_fds) {
1400 const struct got_error *pack_err =
1401 got_repo_pack_fds_close(pack_fds);
1402 if (error == NULL)
1403 error = pack_err;
1405 free(repo_path);
1406 return error;
1409 __dead static void
1410 usage_dump(void)
1412 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1413 "[-x reference] [reference]...\n", getprogname());
1414 exit(1);
1417 static const struct got_error *
1418 cmd_dump(int argc, char *argv[])
1420 const struct got_error *error = NULL;
1421 struct got_pack_progress_arg ppa;
1422 struct got_repository *repo = NULL;
1423 struct got_pathlist_head exclude_args;
1424 struct got_pathlist_entry *pe;
1425 struct got_reflist_head exclude_refs;
1426 struct got_reflist_head include_refs;
1427 struct got_reflist_entry *re, *new;
1428 const char *refname;
1429 char *repo_path = NULL;
1430 int *pack_fds = NULL;
1431 int verbosity = 0;
1432 int i, ch;
1434 TAILQ_INIT(&exclude_args);
1435 TAILQ_INIT(&exclude_refs);
1436 TAILQ_INIT(&include_refs);
1438 #ifndef PROFILE
1439 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1440 NULL) == -1)
1441 err(1, "pledge");
1442 #endif
1444 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1445 switch (ch) {
1446 case 'q':
1447 verbosity = -1;
1448 break;
1449 case 'r':
1450 repo_path = realpath(optarg, NULL);
1451 if (repo_path == NULL)
1452 return got_error_from_errno2("realpath",
1453 optarg);
1454 got_path_strip_trailing_slashes(repo_path);
1455 break;
1456 case 'x':
1457 error = got_pathlist_append(&exclude_args,
1458 optarg, NULL);
1459 if (error)
1460 return error;
1461 break;
1462 default:
1463 usage_dump();
1464 /* NOTREACHED */
1467 argc -= optind;
1468 argv += optind;
1470 if (repo_path == NULL) {
1471 error = get_repo_path(&repo_path);
1472 if (error)
1473 goto done;
1475 error = got_repo_pack_fds_open(&pack_fds);
1476 if (error != NULL)
1477 goto done;
1478 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1479 if (error)
1480 goto done;
1482 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1483 if (error)
1484 goto done;
1486 TAILQ_FOREACH(pe, &exclude_args, entry) {
1487 refname = pe->path;
1488 error = add_ref(&new, &exclude_refs, refname, repo);
1489 if (error)
1490 goto done;
1493 if (argc == 0) {
1494 error = got_ref_list(&include_refs, repo, "",
1495 got_ref_cmp_by_name, NULL);
1496 if (error)
1497 goto done;
1498 } else {
1499 for (i = 0; i < argc; i++) {
1500 got_path_strip_trailing_slashes(argv[i]);
1501 refname = argv[i];
1502 error = add_ref(&new, &include_refs, refname, repo);
1503 if (error)
1504 goto done;
1508 /* Ignore references in the refs/got/ namespace. */
1509 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1510 refname = got_ref_get_name(re->ref);
1511 if (strncmp("refs/got/", refname, 9) != 0)
1512 continue;
1513 TAILQ_REMOVE(&include_refs, re, entry);
1514 got_ref_close(re->ref);
1515 free(re);
1518 memset(&ppa, 0, sizeof(ppa));
1519 ppa.out = stderr;
1520 ppa.verbosity = verbosity;
1522 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1523 repo, pack_progress, &ppa, check_cancelled, NULL);
1524 if (ppa.printed_something)
1525 fprintf(stderr, "\n");
1526 done:
1527 if (repo)
1528 got_repo_close(repo);
1530 if (pack_fds) {
1531 const struct got_error *pack_err;
1533 pack_err = got_repo_pack_fds_close(pack_fds);
1534 if (error == NULL)
1535 error = pack_err;
1538 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1539 got_ref_list_free(&exclude_refs);
1540 got_ref_list_free(&include_refs);
1541 free(repo_path);
1543 return error;