Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <util.h>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_gotconfig.h"
42 #include "got_path.h"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
45 #include "got_worktree.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static volatile sig_atomic_t sigint_received;
52 static volatile sig_atomic_t sigpipe_received;
54 static void
55 catch_sigint(int signo)
56 {
57 sigint_received = 1;
58 }
60 static void
61 catch_sigpipe(int signo)
62 {
63 sigpipe_received = 1;
64 }
66 static const struct got_error *
67 check_cancelled(void *arg)
68 {
69 if (sigint_received || sigpipe_received)
70 return got_error(GOT_ERR_CANCELLED);
71 return NULL;
72 }
74 struct gotadmin_cmd {
75 const char *cmd_name;
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
79 };
81 __dead static void usage(int, int);
82 __dead static void usage_init(void);
83 __dead static void usage_info(void);
84 __dead static void usage_pack(void);
85 __dead static void usage_indexpack(void);
86 __dead static void usage_listpack(void);
87 __dead static void usage_cleanup(void);
89 static const struct got_error* cmd_init(int, char *[]);
90 static const struct got_error* cmd_info(int, char *[]);
91 static const struct got_error* cmd_pack(int, char *[]);
92 static const struct got_error* cmd_indexpack(int, char *[]);
93 static const struct got_error* cmd_listpack(int, char *[]);
94 static const struct got_error* cmd_cleanup(int, char *[]);
96 static const struct gotadmin_cmd gotadmin_commands[] = {
97 { "init", cmd_init, usage_init, "" },
98 { "info", cmd_info, usage_info, "" },
99 { "pack", cmd_pack, usage_pack, "" },
100 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
101 { "listpack", cmd_listpack, usage_listpack, "ls" },
102 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
103 };
105 static void
106 list_commands(FILE *fp)
108 size_t i;
110 fprintf(fp, "commands:");
111 for (i = 0; i < nitems(gotadmin_commands); i++) {
112 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
113 fprintf(fp, " %s", cmd->cmd_name);
115 fputc('\n', fp);
118 int
119 main(int argc, char *argv[])
121 const struct gotadmin_cmd *cmd;
122 size_t i;
123 int ch;
124 int hflag = 0, Vflag = 0;
125 static const struct option longopts[] = {
126 { "version", no_argument, NULL, 'V' },
127 { NULL, 0, NULL, 0 }
128 };
130 setlocale(LC_CTYPE, "");
132 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
133 switch (ch) {
134 case 'h':
135 hflag = 1;
136 break;
137 case 'V':
138 Vflag = 1;
139 break;
140 default:
141 usage(hflag, 1);
142 /* NOTREACHED */
146 argc -= optind;
147 argv += optind;
148 optind = 1;
149 optreset = 1;
151 if (Vflag) {
152 got_version_print_str();
153 return 0;
156 if (argc <= 0)
157 usage(hflag, hflag ? 0 : 1);
159 signal(SIGINT, catch_sigint);
160 signal(SIGPIPE, catch_sigpipe);
162 for (i = 0; i < nitems(gotadmin_commands); i++) {
163 const struct got_error *error;
165 cmd = &gotadmin_commands[i];
167 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
168 strcmp(cmd->cmd_alias, argv[0]) != 0)
169 continue;
171 if (hflag)
172 cmd->cmd_usage();
174 error = cmd->cmd_main(argc, argv);
175 if (error && error->code != GOT_ERR_CANCELLED &&
176 error->code != GOT_ERR_PRIVSEP_EXIT &&
177 !(sigpipe_received &&
178 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
179 !(sigint_received &&
180 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
182 return 1;
185 return 0;
188 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
189 list_commands(stderr);
190 return 1;
193 __dead static void
194 usage(int hflag, int status)
196 FILE *fp = (status == 0) ? stdout : stderr;
198 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
199 getprogname());
200 if (hflag)
201 list_commands(fp);
202 exit(status);
205 static const struct got_error *
206 apply_unveil(const char *repo_path, int repo_read_only)
208 const struct got_error *err;
210 #ifdef PROFILE
211 if (unveil("gmon.out", "rwc") != 0)
212 return got_error_from_errno2("unveil", "gmon.out");
213 #endif
214 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
215 return got_error_from_errno2("unveil", repo_path);
217 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
218 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
220 err = got_privsep_unveil_exec_helpers();
221 if (err != NULL)
222 return err;
224 if (unveil(NULL, NULL) != 0)
225 return got_error_from_errno("unveil");
227 return NULL;
230 __dead static void
231 usage_info(void)
233 fprintf(stderr, "usage: %s info [-r repository-path]\n",
234 getprogname());
235 exit(1);
238 static const struct got_error *
239 get_repo_path(char **repo_path)
241 const struct got_error *err = NULL;
242 struct got_worktree *worktree = NULL;
243 char *cwd;
245 *repo_path = NULL;
247 cwd = getcwd(NULL, 0);
248 if (cwd == NULL)
249 return got_error_from_errno("getcwd");
251 err = got_worktree_open(&worktree, cwd);
252 if (err) {
253 if (err->code != GOT_ERR_NOT_WORKTREE)
254 goto done;
255 err = NULL;
258 if (worktree)
259 *repo_path = strdup(got_worktree_get_repo_path(worktree));
260 else
261 *repo_path = strdup(cwd);
262 if (*repo_path == NULL)
263 err = got_error_from_errno("strdup");
264 done:
265 if (worktree)
266 got_worktree_close(worktree);
267 free(cwd);
268 return err;
271 __dead static void
272 usage_init(void)
274 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
275 exit(1);
278 static const struct got_error *
279 cmd_init(int argc, char *argv[])
281 const struct got_error *error = NULL;
282 char *repo_path = NULL;
283 int ch;
285 while ((ch = getopt(argc, argv, "")) != -1) {
286 switch (ch) {
287 default:
288 usage_init();
289 /* NOTREACHED */
293 argc -= optind;
294 argv += optind;
296 #ifndef PROFILE
297 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
298 err(1, "pledge");
299 #endif
300 if (argc != 1)
301 usage_init();
303 repo_path = strdup(argv[0]);
304 if (repo_path == NULL)
305 return got_error_from_errno("strdup");
307 got_path_strip_trailing_slashes(repo_path);
309 error = got_path_mkdir(repo_path);
310 if (error &&
311 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
312 goto done;
314 error = apply_unveil(repo_path, 0);
315 if (error)
316 goto done;
318 error = got_repo_init(repo_path);
319 done:
320 free(repo_path);
321 return error;
324 static const struct got_error *
325 cmd_info(int argc, char *argv[])
327 const struct got_error *error = NULL;
328 char *repo_path = NULL;
329 struct got_repository *repo = NULL;
330 const struct got_gotconfig *gotconfig = NULL;
331 int ch, npackfiles, npackedobj, nobj;
332 off_t packsize, loose_size;
333 char scaled[FMT_SCALED_STRSIZE];
334 int *pack_fds = NULL;
336 while ((ch = getopt(argc, argv, "r:")) != -1) {
337 switch (ch) {
338 case 'r':
339 repo_path = realpath(optarg, NULL);
340 if (repo_path == NULL)
341 return got_error_from_errno2("realpath",
342 optarg);
343 got_path_strip_trailing_slashes(repo_path);
344 break;
345 default:
346 usage_info();
347 /* NOTREACHED */
351 argc -= optind;
352 argv += optind;
354 #ifndef PROFILE
355 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
356 NULL) == -1)
357 err(1, "pledge");
358 #endif
359 if (repo_path == NULL) {
360 error = get_repo_path(&repo_path);
361 if (error)
362 goto done;
364 error = got_repo_pack_fds_open(&pack_fds);
365 if (error != NULL)
366 goto done;
367 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
368 if (error)
369 goto done;
370 #ifndef PROFILE
371 /* Remove "cpath" promise. */
372 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
373 NULL) == -1)
374 err(1, "pledge");
375 #endif
376 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
377 if (error)
378 goto done;
380 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
382 gotconfig = got_repo_get_gotconfig(repo);
383 if (gotconfig) {
384 const struct got_remote_repo *remotes;
385 int i, nremotes;
386 if (got_gotconfig_get_author(gotconfig)) {
387 printf("default author: %s\n",
388 got_gotconfig_get_author(gotconfig));
390 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
391 for (i = 0; i < nremotes; i++) {
392 const char *fetch_url = remotes[i].fetch_url;
393 const char *send_url = remotes[i].send_url;
394 if (strcmp(fetch_url, send_url) == 0) {
395 printf("remote \"%s\": %s\n", remotes[i].name,
396 remotes[i].fetch_url);
397 } else {
398 printf("remote \"%s\" (fetch): %s\n",
399 remotes[i].name, remotes[i].fetch_url);
400 printf("remote \"%s\" (send): %s\n",
401 remotes[i].name, remotes[i].send_url);
406 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
407 &packsize, repo);
408 if (error)
409 goto done;
410 printf("pack files: %d\n", npackfiles);
411 if (npackfiles > 0) {
412 if (fmt_scaled(packsize, scaled) == -1) {
413 error = got_error_from_errno("fmt_scaled");
414 goto done;
416 printf("packed objects: %d\n", npackedobj);
417 printf("packed total size: %s\n", scaled);
420 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
421 if (error)
422 goto done;
423 printf("loose objects: %d\n", nobj);
424 if (nobj > 0) {
425 if (fmt_scaled(loose_size, scaled) == -1) {
426 error = got_error_from_errno("fmt_scaled");
427 goto done;
429 printf("loose total size: %s\n", scaled);
431 done:
432 if (repo)
433 got_repo_close(repo);
434 if (pack_fds) {
435 const struct got_error *pack_err =
436 got_repo_pack_fds_close(pack_fds);
437 if (error == NULL)
438 error = pack_err;
441 free(repo_path);
442 return error;
445 __dead static void
446 usage_pack(void)
448 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
449 "[-x reference] [-q] [reference ...]\n",
450 getprogname());
451 exit(1);
454 struct got_pack_progress_arg {
455 char last_scaled_size[FMT_SCALED_STRSIZE];
456 int last_ncolored;
457 int last_nfound;
458 int last_ntrees;
459 int loading_done;
460 int last_ncommits;
461 int last_nobj_total;
462 int last_p_deltify;
463 int last_p_written;
464 int last_p_indexed;
465 int last_p_resolved;
466 int verbosity;
467 int printed_something;
468 };
470 static void
471 print_load_info(int print_colored, int print_found, int print_trees,
472 int ncolored, int nfound, int ntrees)
474 if (print_colored) {
475 printf("%d commit%s colored", ncolored,
476 ncolored == 1 ? "" : "s");
478 if (print_found) {
479 printf("%s%d object%s found",
480 ncolored > 0 ? "; " : "",
481 nfound, nfound == 1 ? "" : "s");
483 if (print_trees) {
484 printf("; %d tree%s scanned", ntrees,
485 ntrees == 1 ? "" : "s");
489 static const struct got_error *
490 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
491 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
492 int nobj_written)
494 struct got_pack_progress_arg *a = arg;
495 char scaled_size[FMT_SCALED_STRSIZE];
496 int p_deltify, p_written;
497 int print_colored = 0, print_found = 0, print_trees = 0;
498 int print_searching = 0, print_total = 0;
499 int print_deltify = 0, print_written = 0;
501 if (a->verbosity < 0)
502 return NULL;
504 if (a->last_ncolored != ncolored) {
505 print_colored = 1;
506 a->last_ncolored = ncolored;
509 if (a->last_nfound != nfound) {
510 print_colored = 1;
511 print_found = 1;
512 a->last_nfound = nfound;
515 if (a->last_ntrees != ntrees) {
516 print_colored = 1;
517 print_found = 1;
518 print_trees = 1;
519 a->last_ntrees = ntrees;
522 if ((print_colored || print_found || print_trees) &&
523 !a->loading_done) {
524 printf("\r");
525 print_load_info(print_colored, print_found, print_trees,
526 ncolored, nfound, ntrees);
527 a->printed_something = 1;
528 fflush(stdout);
529 return NULL;
530 } else if (!a->loading_done) {
531 printf("\r");
532 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
533 printf("\n");
534 a->loading_done = 1;
537 if (fmt_scaled(packfile_size, scaled_size) == -1)
538 return got_error_from_errno("fmt_scaled");
540 if (a->last_ncommits != ncommits) {
541 print_searching = 1;
542 a->last_ncommits = ncommits;
545 if (a->last_nobj_total != nobj_total) {
546 print_searching = 1;
547 print_total = 1;
548 a->last_nobj_total = nobj_total;
551 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
552 strcmp(scaled_size, a->last_scaled_size)) != 0) {
553 if (strlcpy(a->last_scaled_size, scaled_size,
554 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
555 return got_error(GOT_ERR_NO_SPACE);
558 if (nobj_deltify > 0 || nobj_written > 0) {
559 if (nobj_deltify > 0) {
560 p_deltify = (nobj_deltify * 100) / nobj_total;
561 if (p_deltify != a->last_p_deltify) {
562 a->last_p_deltify = p_deltify;
563 print_searching = 1;
564 print_total = 1;
565 print_deltify = 1;
568 if (nobj_written > 0) {
569 p_written = (nobj_written * 100) / nobj_total;
570 if (p_written != a->last_p_written) {
571 a->last_p_written = p_written;
572 print_searching = 1;
573 print_total = 1;
574 print_deltify = 1;
575 print_written = 1;
580 if (print_searching || print_total || print_deltify || print_written)
581 printf("\r");
582 if (print_searching)
583 printf("packing %d reference%s", ncommits,
584 ncommits == 1 ? "" : "s");
585 if (print_total)
586 printf("; %d object%s", nobj_total,
587 nobj_total == 1 ? "" : "s");
588 if (print_deltify)
589 printf("; deltify: %d%%", p_deltify);
590 if (print_written)
591 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
592 scaled_size, p_written);
593 if (print_searching || print_total || print_deltify ||
594 print_written) {
595 a->printed_something = 1;
596 fflush(stdout);
598 return NULL;
601 static const struct got_error *
602 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
603 int nobj_indexed, int nobj_loose, int nobj_resolved)
605 struct got_pack_progress_arg *a = arg;
606 char scaled_size[FMT_SCALED_STRSIZE];
607 int p_indexed, p_resolved;
608 int print_size = 0, print_indexed = 0, print_resolved = 0;
610 if (a->verbosity < 0)
611 return NULL;
613 if (packfile_size > 0 || nobj_indexed > 0) {
614 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
615 (a->last_scaled_size[0] == '\0' ||
616 strcmp(scaled_size, a->last_scaled_size)) != 0) {
617 print_size = 1;
618 if (strlcpy(a->last_scaled_size, scaled_size,
619 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
620 return got_error(GOT_ERR_NO_SPACE);
622 if (nobj_indexed > 0) {
623 p_indexed = (nobj_indexed * 100) / nobj_total;
624 if (p_indexed != a->last_p_indexed) {
625 a->last_p_indexed = p_indexed;
626 print_indexed = 1;
627 print_size = 1;
630 if (nobj_resolved > 0) {
631 p_resolved = (nobj_resolved * 100) /
632 (nobj_total - nobj_loose);
633 if (p_resolved != a->last_p_resolved) {
634 a->last_p_resolved = p_resolved;
635 print_resolved = 1;
636 print_indexed = 1;
637 print_size = 1;
642 if (print_size || print_indexed || print_resolved)
643 printf("\r");
644 if (print_size)
645 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
646 if (print_indexed)
647 printf("; indexing %d%%", p_indexed);
648 if (print_resolved)
649 printf("; resolving deltas %d%%", p_resolved);
650 if (print_size || print_indexed || print_resolved)
651 fflush(stdout);
653 return NULL;
656 static const struct got_error *
657 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
658 const char *refname, struct got_repository *repo)
660 const struct got_error *err;
661 struct got_reference *ref;
663 *new = NULL;
665 err = got_ref_open(&ref, repo, refname, 0);
666 if (err) {
667 if (err->code != GOT_ERR_NOT_REF)
668 return err;
670 /* Treat argument as a reference prefix. */
671 err = got_ref_list(refs, repo, refname,
672 got_ref_cmp_by_name, NULL);
673 } else {
674 err = got_reflist_insert(new, refs, ref,
675 got_ref_cmp_by_name, NULL);
676 if (err || *new == NULL /* duplicate */)
677 got_ref_close(ref);
680 return err;
683 static const struct got_error *
684 cmd_pack(int argc, char *argv[])
686 const struct got_error *error = NULL;
687 char *repo_path = NULL;
688 struct got_repository *repo = NULL;
689 int ch, i, loose_obj_only = 1, verbosity = 0;
690 struct got_object_id *pack_hash = NULL;
691 char *id_str = NULL;
692 struct got_pack_progress_arg ppa;
693 FILE *packfile = NULL;
694 struct got_pathlist_head exclude_args;
695 struct got_pathlist_entry *pe;
696 struct got_reflist_head exclude_refs;
697 struct got_reflist_head include_refs;
698 struct got_reflist_entry *re, *new;
699 int *pack_fds = NULL;
701 TAILQ_INIT(&exclude_args);
702 TAILQ_INIT(&exclude_refs);
703 TAILQ_INIT(&include_refs);
705 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
706 switch (ch) {
707 case 'a':
708 loose_obj_only = 0;
709 break;
710 case 'r':
711 repo_path = realpath(optarg, NULL);
712 if (repo_path == NULL)
713 return got_error_from_errno2("realpath",
714 optarg);
715 got_path_strip_trailing_slashes(repo_path);
716 break;
717 case 'x':
718 got_path_strip_trailing_slashes(optarg);
719 error = got_pathlist_append(&exclude_args,
720 optarg, NULL);
721 if (error)
722 return error;
723 break;
724 case 'q':
725 verbosity = -1;
726 break;
727 default:
728 usage_pack();
729 /* NOTREACHED */
733 argc -= optind;
734 argv += optind;
736 #ifndef PROFILE
737 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
738 NULL) == -1)
739 err(1, "pledge");
740 #endif
741 if (repo_path == NULL) {
742 error = get_repo_path(&repo_path);
743 if (error)
744 goto done;
746 error = got_repo_pack_fds_open(&pack_fds);
747 if (error != NULL)
748 goto done;
749 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
750 if (error)
751 goto done;
753 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
754 if (error)
755 goto done;
757 TAILQ_FOREACH(pe, &exclude_args, entry) {
758 const char *refname = pe->path;
759 error = add_ref(&new, &exclude_refs, refname, repo);
760 if (error)
761 goto done;
765 if (argc == 0) {
766 error = got_ref_list(&include_refs, repo, "",
767 got_ref_cmp_by_name, NULL);
768 if (error)
769 goto done;
770 } else {
771 for (i = 0; i < argc; i++) {
772 const char *refname;
773 got_path_strip_trailing_slashes(argv[i]);
774 refname = argv[i];
775 error = add_ref(&new, &include_refs, refname, repo);
776 if (error)
777 goto done;
781 /* Ignore references in the refs/got/ namespace. */
782 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
783 const char *refname = got_ref_get_name(re->ref);
784 if (strncmp("refs/got/", refname, 9) != 0)
785 continue;
786 TAILQ_REMOVE(&include_refs, re, entry);
787 got_ref_close(re->ref);
788 free(re);
791 memset(&ppa, 0, sizeof(ppa));
792 ppa.last_scaled_size[0] = '\0';
793 ppa.last_p_indexed = -1;
794 ppa.last_p_resolved = -1;
795 ppa.verbosity = verbosity;
797 error = got_repo_pack_objects(&packfile, &pack_hash,
798 &include_refs, &exclude_refs, repo, loose_obj_only,
799 pack_progress, &ppa, check_cancelled, NULL);
800 if (error) {
801 if (ppa.printed_something)
802 printf("\n");
803 goto done;
806 error = got_object_id_str(&id_str, pack_hash);
807 if (error)
808 goto done;
809 if (verbosity >= 0)
810 printf("\nWrote %s.pack\n", id_str);
812 error = got_repo_index_pack(packfile, pack_hash, repo,
813 pack_index_progress, &ppa, check_cancelled, NULL);
814 if (error)
815 goto done;
816 if (verbosity >= 0)
817 printf("\nIndexed %s.pack\n", id_str);
818 done:
819 if (repo)
820 got_repo_close(repo);
821 if (pack_fds) {
822 const struct got_error *pack_err =
823 got_repo_pack_fds_close(pack_fds);
824 if (error == NULL)
825 error = pack_err;
827 got_pathlist_free(&exclude_args);
828 got_ref_list_free(&exclude_refs);
829 got_ref_list_free(&include_refs);
830 free(id_str);
831 free(pack_hash);
832 free(repo_path);
833 return error;
836 __dead static void
837 usage_indexpack(void)
839 fprintf(stderr, "usage: %s indexpack packfile-path\n",
840 getprogname());
841 exit(1);
844 static const struct got_error *
845 cmd_indexpack(int argc, char *argv[])
847 const struct got_error *error = NULL;
848 struct got_repository *repo = NULL;
849 int ch;
850 struct got_object_id *pack_hash = NULL;
851 char *packfile_path = NULL;
852 char *id_str = NULL;
853 struct got_pack_progress_arg ppa;
854 FILE *packfile = NULL;
855 int *pack_fds = NULL;
857 while ((ch = getopt(argc, argv, "")) != -1) {
858 switch (ch) {
859 default:
860 usage_indexpack();
861 /* NOTREACHED */
865 argc -= optind;
866 argv += optind;
868 if (argc != 1)
869 usage_indexpack();
871 packfile_path = realpath(argv[0], NULL);
872 if (packfile_path == NULL)
873 return got_error_from_errno2("realpath", argv[0]);
875 #ifndef PROFILE
876 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
877 NULL) == -1)
878 err(1, "pledge");
879 #endif
881 error = got_repo_pack_fds_open(&pack_fds);
882 if (error != NULL)
883 goto done;
884 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
885 if (error)
886 goto done;
888 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
889 if (error)
890 goto done;
892 memset(&ppa, 0, sizeof(ppa));
893 ppa.last_scaled_size[0] = '\0';
894 ppa.last_p_indexed = -1;
895 ppa.last_p_resolved = -1;
897 error = got_repo_find_pack(&packfile, &pack_hash, repo,
898 packfile_path);
899 if (error)
900 goto done;
902 error = got_object_id_str(&id_str, pack_hash);
903 if (error)
904 goto done;
906 error = got_repo_index_pack(packfile, pack_hash, repo,
907 pack_index_progress, &ppa, check_cancelled, NULL);
908 if (error)
909 goto done;
910 printf("\nIndexed %s.pack\n", id_str);
911 done:
912 if (repo)
913 got_repo_close(repo);
914 if (pack_fds) {
915 const struct got_error *pack_err =
916 got_repo_pack_fds_close(pack_fds);
917 if (error == NULL)
918 error = pack_err;
920 free(id_str);
921 free(pack_hash);
922 return error;
925 __dead static void
926 usage_listpack(void)
928 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
929 getprogname());
930 exit(1);
933 struct gotadmin_list_pack_cb_args {
934 int nblobs;
935 int ntrees;
936 int ncommits;
937 int ntags;
938 int noffdeltas;
939 int nrefdeltas;
940 int human_readable;
941 };
943 static const struct got_error *
944 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
945 off_t size, off_t base_offset, struct got_object_id *base_id)
947 const struct got_error *err;
948 struct gotadmin_list_pack_cb_args *a = arg;
949 char *id_str, *delta_str = NULL, *base_id_str = NULL;
950 const char *type_str;
952 err = got_object_id_str(&id_str, id);
953 if (err)
954 return err;
956 switch (type) {
957 case GOT_OBJ_TYPE_BLOB:
958 type_str = GOT_OBJ_LABEL_BLOB;
959 a->nblobs++;
960 break;
961 case GOT_OBJ_TYPE_TREE:
962 type_str = GOT_OBJ_LABEL_TREE;
963 a->ntrees++;
964 break;
965 case GOT_OBJ_TYPE_COMMIT:
966 type_str = GOT_OBJ_LABEL_COMMIT;
967 a->ncommits++;
968 break;
969 case GOT_OBJ_TYPE_TAG:
970 type_str = GOT_OBJ_LABEL_TAG;
971 a->ntags++;
972 break;
973 case GOT_OBJ_TYPE_OFFSET_DELTA:
974 type_str = "offset-delta";
975 if (asprintf(&delta_str, " base-offset %lld",
976 (long long)base_offset) == -1) {
977 err = got_error_from_errno("asprintf");
978 goto done;
980 a->noffdeltas++;
981 break;
982 case GOT_OBJ_TYPE_REF_DELTA:
983 type_str = "ref-delta";
984 err = got_object_id_str(&base_id_str, base_id);
985 if (err)
986 goto done;
987 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
988 err = got_error_from_errno("asprintf");
989 goto done;
991 a->nrefdeltas++;
992 break;
993 default:
994 err = got_error(GOT_ERR_OBJ_TYPE);
995 goto done;
997 if (a->human_readable) {
998 char scaled[FMT_SCALED_STRSIZE];
999 char *s;;
1000 if (fmt_scaled(size, scaled) == -1) {
1001 err = got_error_from_errno("fmt_scaled");
1002 goto done;
1004 s = scaled;
1005 while (isspace((unsigned char)*s))
1006 s++;
1007 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1008 (long long)offset, s, delta_str ? delta_str : "");
1009 } else {
1010 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1011 (long long)offset, (long long)size,
1012 delta_str ? delta_str : "");
1014 done:
1015 free(id_str);
1016 free(base_id_str);
1017 free(delta_str);
1018 return err;
1021 static const struct got_error *
1022 cmd_listpack(int argc, char *argv[])
1024 const struct got_error *error = NULL;
1025 struct got_repository *repo = NULL;
1026 int ch;
1027 struct got_object_id *pack_hash = NULL;
1028 char *packfile_path = NULL;
1029 char *id_str = NULL;
1030 struct gotadmin_list_pack_cb_args lpa;
1031 FILE *packfile = NULL;
1032 int show_stats = 0, human_readable = 0;
1033 int *pack_fds = NULL;
1035 while ((ch = getopt(argc, argv, "hs")) != -1) {
1036 switch (ch) {
1037 case 'h':
1038 human_readable = 1;
1039 break;
1040 case 's':
1041 show_stats = 1;
1042 break;
1043 default:
1044 usage_listpack();
1045 /* NOTREACHED */
1049 argc -= optind;
1050 argv += optind;
1052 if (argc != 1)
1053 usage_listpack();
1054 packfile_path = realpath(argv[0], NULL);
1055 if (packfile_path == NULL)
1056 return got_error_from_errno2("realpath", argv[0]);
1058 #ifndef PROFILE
1059 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1060 NULL) == -1)
1061 err(1, "pledge");
1062 #endif
1063 error = got_repo_pack_fds_open(&pack_fds);
1064 if (error != NULL)
1065 goto done;
1066 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1067 if (error)
1068 goto done;
1069 #ifndef PROFILE
1070 /* Remove "cpath" promise. */
1071 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1072 NULL) == -1)
1073 err(1, "pledge");
1074 #endif
1075 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1076 if (error)
1077 goto done;
1079 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1080 packfile_path);
1081 if (error)
1082 goto done;
1083 error = got_object_id_str(&id_str, pack_hash);
1084 if (error)
1085 goto done;
1087 memset(&lpa, 0, sizeof(lpa));
1088 lpa.human_readable = human_readable;
1089 error = got_repo_list_pack(packfile, pack_hash, repo,
1090 list_pack_cb, &lpa, check_cancelled, NULL);
1091 if (error)
1092 goto done;
1093 if (show_stats) {
1094 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1095 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1096 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1097 lpa.noffdeltas + lpa.nrefdeltas,
1098 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1099 lpa.noffdeltas, lpa.nrefdeltas);
1101 done:
1102 if (repo)
1103 got_repo_close(repo);
1104 if (pack_fds) {
1105 const struct got_error *pack_err =
1106 got_repo_pack_fds_close(pack_fds);
1107 if (error == NULL)
1108 error = pack_err;
1110 free(id_str);
1111 free(pack_hash);
1112 free(packfile_path);
1113 return error;
1116 __dead static void
1117 usage_cleanup(void)
1119 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1120 "[-q]\n", getprogname());
1121 exit(1);
1124 struct got_cleanup_progress_arg {
1125 int last_nloose;
1126 int last_ncommits;
1127 int last_npurged;
1128 int verbosity;
1129 int printed_something;
1130 int dry_run;
1133 static const struct got_error *
1134 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1136 struct got_cleanup_progress_arg *a = arg;
1137 int print_loose = 0, print_commits = 0, print_purged = 0;
1139 if (a->last_nloose != nloose) {
1140 print_loose = 1;
1141 a->last_nloose = nloose;
1143 if (a->last_ncommits != ncommits) {
1144 print_loose = 1;
1145 print_commits = 1;
1146 a->last_ncommits = ncommits;
1148 if (a->last_npurged != npurged) {
1149 print_loose = 1;
1150 print_commits = 1;
1151 print_purged = 1;
1152 a->last_npurged = npurged;
1155 if (a->verbosity < 0)
1156 return NULL;
1158 if (print_loose || print_commits || print_purged)
1159 printf("\r");
1160 if (print_loose)
1161 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1162 if (print_commits)
1163 printf("; %d commit%s scanned", ncommits,
1164 ncommits == 1 ? "" : "s");
1165 if (print_purged) {
1166 if (a->dry_run) {
1167 printf("; %d object%s could be purged", npurged,
1168 npurged == 1 ? "" : "s");
1169 } else {
1170 printf("; %d object%s purged", npurged,
1171 npurged == 1 ? "" : "s");
1174 if (print_loose || print_commits || print_purged) {
1175 a->printed_something = 1;
1176 fflush(stdout);
1178 return NULL;
1181 struct got_lonely_packidx_progress_arg {
1182 int verbosity;
1183 int printed_something;
1184 int dry_run;
1187 static const struct got_error *
1188 lonely_packidx_progress(void *arg, const char *path)
1190 struct got_lonely_packidx_progress_arg *a = arg;
1192 if (a->verbosity < 0)
1193 return NULL;
1195 if (a->dry_run)
1196 printf("%s could be removed\n", path);
1197 else
1198 printf("%s removed\n", path);
1200 a->printed_something = 1;
1201 return NULL;
1204 static const struct got_error *
1205 cmd_cleanup(int argc, char *argv[])
1207 const struct got_error *error = NULL;
1208 char *repo_path = NULL;
1209 struct got_repository *repo = NULL;
1210 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1211 int remove_lonely_packidx = 0, ignore_mtime = 0;
1212 struct got_cleanup_progress_arg cpa;
1213 struct got_lonely_packidx_progress_arg lpa;
1214 off_t size_before, size_after;
1215 char scaled_before[FMT_SCALED_STRSIZE];
1216 char scaled_after[FMT_SCALED_STRSIZE];
1217 char scaled_diff[FMT_SCALED_STRSIZE];
1218 char **extensions;
1219 int nextensions, i;
1220 int *pack_fds = NULL;
1222 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1223 switch (ch) {
1224 case 'a':
1225 ignore_mtime = 1;
1226 break;
1227 case 'p':
1228 remove_lonely_packidx = 1;
1229 break;
1230 case 'r':
1231 repo_path = realpath(optarg, NULL);
1232 if (repo_path == NULL)
1233 return got_error_from_errno2("realpath",
1234 optarg);
1235 got_path_strip_trailing_slashes(repo_path);
1236 break;
1237 case 'n':
1238 dry_run = 1;
1239 break;
1240 case 'q':
1241 verbosity = -1;
1242 break;
1243 default:
1244 usage_cleanup();
1245 /* NOTREACHED */
1249 argc -= optind;
1250 argv += optind;
1252 #ifndef PROFILE
1253 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1254 NULL) == -1)
1255 err(1, "pledge");
1256 #endif
1257 if (repo_path == NULL) {
1258 error = get_repo_path(&repo_path);
1259 if (error)
1260 goto done;
1262 error = got_repo_pack_fds_open(&pack_fds);
1263 if (error != NULL)
1264 goto done;
1265 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1266 if (error)
1267 goto done;
1269 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1270 if (error)
1271 goto done;
1273 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1274 repo);
1275 for (i = 0; i < nextensions; i++) {
1276 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1277 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1278 "the preciousObjects Git extension is enabled; "
1279 "this implies that objects must not be deleted");
1280 goto done;
1284 if (remove_lonely_packidx) {
1285 memset(&lpa, 0, sizeof(lpa));
1286 lpa.dry_run = dry_run;
1287 lpa.verbosity = verbosity;
1288 error = got_repo_remove_lonely_packidx(repo, dry_run,
1289 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1290 goto done;
1293 memset(&cpa, 0, sizeof(cpa));
1294 cpa.last_ncommits = -1;
1295 cpa.last_npurged = -1;
1296 cpa.dry_run = dry_run;
1297 cpa.verbosity = verbosity;
1298 error = got_repo_purge_unreferenced_loose_objects(repo,
1299 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1300 cleanup_progress, &cpa, check_cancelled, NULL);
1301 if (cpa.printed_something)
1302 printf("\n");
1303 if (error)
1304 goto done;
1305 if (cpa.printed_something) {
1306 if (fmt_scaled(size_before, scaled_before) == -1) {
1307 error = got_error_from_errno("fmt_scaled");
1308 goto done;
1310 if (fmt_scaled(size_after, scaled_after) == -1) {
1311 error = got_error_from_errno("fmt_scaled");
1312 goto done;
1314 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1315 error = got_error_from_errno("fmt_scaled");
1316 goto done;
1318 printf("loose total size before: %s\n", scaled_before);
1319 printf("loose total size after: %s\n", scaled_after);
1320 if (dry_run) {
1321 printf("disk space which would be freed: %s\n",
1322 scaled_diff);
1323 } else
1324 printf("disk space freed: %s\n", scaled_diff);
1325 printf("loose objects also found in pack files: %d\n", npacked);
1327 done:
1328 if (repo)
1329 got_repo_close(repo);
1330 if (pack_fds) {
1331 const struct got_error *pack_err =
1332 got_repo_pack_fds_close(pack_fds);
1333 if (error == NULL)
1334 error = pack_err;
1336 free(repo_path);
1337 return error;