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_info(void);
83 __dead static void usage_pack(void);
84 __dead static void usage_indexpack(void);
85 __dead static void usage_listpack(void);
86 __dead static void usage_cleanup(void);
88 static const struct got_error* cmd_info(int, char *[]);
89 static const struct got_error* cmd_pack(int, char *[]);
90 static const struct got_error* cmd_indexpack(int, char *[]);
91 static const struct got_error* cmd_listpack(int, char *[]);
92 static const struct got_error* cmd_cleanup(int, char *[]);
94 static const struct gotadmin_cmd gotadmin_commands[] = {
95 { "info", cmd_info, usage_info, "" },
96 { "pack", cmd_pack, usage_pack, "" },
97 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
98 { "listpack", cmd_listpack, usage_listpack, "ls" },
99 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
100 };
102 static void
103 list_commands(FILE *fp)
105 size_t i;
107 fprintf(fp, "commands:");
108 for (i = 0; i < nitems(gotadmin_commands); i++) {
109 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
110 fprintf(fp, " %s", cmd->cmd_name);
112 fputc('\n', fp);
115 int
116 main(int argc, char *argv[])
118 const struct gotadmin_cmd *cmd;
119 size_t i;
120 int ch;
121 int hflag = 0, Vflag = 0;
122 static const struct option longopts[] = {
123 { "version", no_argument, NULL, 'V' },
124 { NULL, 0, NULL, 0 }
125 };
127 setlocale(LC_CTYPE, "");
129 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
130 switch (ch) {
131 case 'h':
132 hflag = 1;
133 break;
134 case 'V':
135 Vflag = 1;
136 break;
137 default:
138 usage(hflag, 1);
139 /* NOTREACHED */
143 argc -= optind;
144 argv += optind;
145 optind = 1;
146 optreset = 1;
148 if (Vflag) {
149 got_version_print_str();
150 return 0;
153 if (argc <= 0)
154 usage(hflag, hflag ? 0 : 1);
156 signal(SIGINT, catch_sigint);
157 signal(SIGPIPE, catch_sigpipe);
159 for (i = 0; i < nitems(gotadmin_commands); i++) {
160 const struct got_error *error;
162 cmd = &gotadmin_commands[i];
164 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
165 strcmp(cmd->cmd_alias, argv[0]) != 0)
166 continue;
168 if (hflag)
169 cmd->cmd_usage();
171 error = cmd->cmd_main(argc, argv);
172 if (error && error->code != GOT_ERR_CANCELLED &&
173 error->code != GOT_ERR_PRIVSEP_EXIT &&
174 !(sigpipe_received &&
175 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
176 !(sigint_received &&
177 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
179 return 1;
182 return 0;
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 list_commands(stderr);
187 return 1;
190 __dead static void
191 usage(int hflag, int status)
193 FILE *fp = (status == 0) ? stdout : stderr;
195 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
196 getprogname());
197 if (hflag)
198 list_commands(fp);
199 exit(status);
202 static const struct got_error *
203 apply_unveil(const char *repo_path, int repo_read_only)
205 const struct got_error *err;
207 #ifdef PROFILE
208 if (unveil("gmon.out", "rwc") != 0)
209 return got_error_from_errno2("unveil", "gmon.out");
210 #endif
211 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
212 return got_error_from_errno2("unveil", repo_path);
214 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
215 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
217 err = got_privsep_unveil_exec_helpers();
218 if (err != NULL)
219 return err;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno("unveil");
224 return NULL;
227 __dead static void
228 usage_info(void)
230 fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 getprogname());
232 exit(1);
235 static const struct got_error *
236 get_repo_path(char **repo_path)
238 const struct got_error *err = NULL;
239 struct got_worktree *worktree = NULL;
240 char *cwd;
242 *repo_path = NULL;
244 cwd = getcwd(NULL, 0);
245 if (cwd == NULL)
246 return got_error_from_errno("getcwd");
248 err = got_worktree_open(&worktree, cwd);
249 if (err) {
250 if (err->code != GOT_ERR_NOT_WORKTREE)
251 goto done;
252 err = NULL;
255 if (worktree)
256 *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 else
258 *repo_path = strdup(cwd);
259 if (*repo_path == NULL)
260 err = got_error_from_errno("strdup");
261 done:
262 if (worktree)
263 got_worktree_close(worktree);
264 free(cwd);
265 return err;
268 static const struct got_error *
269 cmd_info(int argc, char *argv[])
271 const struct got_error *error = NULL;
272 char *repo_path = NULL;
273 struct got_repository *repo = NULL;
274 const struct got_gotconfig *gotconfig = NULL;
275 int ch, npackfiles, npackedobj, nobj;
276 off_t packsize, loose_size;
277 char scaled[FMT_SCALED_STRSIZE];
278 int *pack_fds = NULL;
280 while ((ch = getopt(argc, argv, "r:")) != -1) {
281 switch (ch) {
282 case 'r':
283 repo_path = realpath(optarg, NULL);
284 if (repo_path == NULL)
285 return got_error_from_errno2("realpath",
286 optarg);
287 got_path_strip_trailing_slashes(repo_path);
288 break;
289 default:
290 usage_info();
291 /* NOTREACHED */
295 argc -= optind;
296 argv += optind;
298 #ifndef PROFILE
299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
300 NULL) == -1)
301 err(1, "pledge");
302 #endif
303 if (repo_path == NULL) {
304 error = get_repo_path(&repo_path);
305 if (error)
306 goto done;
308 error = got_repo_pack_fds_open(&pack_fds);
309 if (error != NULL)
310 goto done;
311 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
312 if (error)
313 goto done;
314 #ifndef PROFILE
315 /* Remove "cpath" promise. */
316 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
317 NULL) == -1)
318 err(1, "pledge");
319 #endif
320 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
321 if (error)
322 goto done;
324 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
326 gotconfig = got_repo_get_gotconfig(repo);
327 if (gotconfig) {
328 const struct got_remote_repo *remotes;
329 int i, nremotes;
330 if (got_gotconfig_get_author(gotconfig)) {
331 printf("default author: %s\n",
332 got_gotconfig_get_author(gotconfig));
334 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
335 for (i = 0; i < nremotes; i++) {
336 const char *fetch_url = remotes[i].fetch_url;
337 const char *send_url = remotes[i].send_url;
338 if (strcmp(fetch_url, send_url) == 0) {
339 printf("remote \"%s\": %s\n", remotes[i].name,
340 remotes[i].fetch_url);
341 } else {
342 printf("remote \"%s\" (fetch): %s\n",
343 remotes[i].name, remotes[i].fetch_url);
344 printf("remote \"%s\" (send): %s\n",
345 remotes[i].name, remotes[i].send_url);
350 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
351 &packsize, repo);
352 if (error)
353 goto done;
354 printf("pack files: %d\n", npackfiles);
355 if (npackfiles > 0) {
356 if (fmt_scaled(packsize, scaled) == -1) {
357 error = got_error_from_errno("fmt_scaled");
358 goto done;
360 printf("packed objects: %d\n", npackedobj);
361 printf("packed total size: %s\n", scaled);
364 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
365 if (error)
366 goto done;
367 printf("loose objects: %d\n", nobj);
368 if (nobj > 0) {
369 if (fmt_scaled(loose_size, scaled) == -1) {
370 error = got_error_from_errno("fmt_scaled");
371 goto done;
373 printf("loose total size: %s\n", scaled);
375 done:
376 if (repo)
377 got_repo_close(repo);
378 if (pack_fds) {
379 const struct got_error *pack_err =
380 got_repo_pack_fds_close(pack_fds);
381 if (error == NULL)
382 error = pack_err;
385 free(repo_path);
386 return error;
389 __dead static void
390 usage_pack(void)
392 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
393 "[-x reference] [-q] [reference ...]\n",
394 getprogname());
395 exit(1);
398 struct got_pack_progress_arg {
399 char last_scaled_size[FMT_SCALED_STRSIZE];
400 int last_ncolored;
401 int last_nfound;
402 int last_ntrees;
403 int loading_done;
404 int last_ncommits;
405 int last_nobj_total;
406 int last_p_deltify;
407 int last_p_written;
408 int last_p_indexed;
409 int last_p_resolved;
410 int verbosity;
411 int printed_something;
412 };
414 static void
415 print_load_info(int print_colored, int print_found, int print_trees,
416 int ncolored, int nfound, int ntrees)
418 if (print_colored) {
419 printf("%d commit%s colored", ncolored,
420 ncolored == 1 ? "" : "s");
422 if (print_found) {
423 printf("%s%d object%s found",
424 ncolored > 0 ? "; " : "",
425 nfound, nfound == 1 ? "" : "s");
427 if (print_trees) {
428 printf("; %d tree%s scanned", ntrees,
429 ntrees == 1 ? "" : "s");
433 static const struct got_error *
434 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
435 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
436 int nobj_written)
438 struct got_pack_progress_arg *a = arg;
439 char scaled_size[FMT_SCALED_STRSIZE];
440 int p_deltify, p_written;
441 int print_colored = 0, print_found = 0, print_trees = 0;
442 int print_searching = 0, print_total = 0;
443 int print_deltify = 0, print_written = 0;
445 if (a->verbosity < 0)
446 return NULL;
448 if (a->last_ncolored != ncolored) {
449 print_colored = 1;
450 a->last_ncolored = ncolored;
453 if (a->last_nfound != nfound) {
454 print_colored = 1;
455 print_found = 1;
456 a->last_nfound = nfound;
459 if (a->last_ntrees != ntrees) {
460 print_colored = 1;
461 print_found = 1;
462 print_trees = 1;
463 a->last_ntrees = ntrees;
466 if ((print_colored || print_found || print_trees) &&
467 !a->loading_done) {
468 printf("\r");
469 print_load_info(print_colored, print_found, print_trees,
470 ncolored, nfound, ntrees);
471 a->printed_something = 1;
472 fflush(stdout);
473 return NULL;
474 } else if (!a->loading_done) {
475 printf("\r");
476 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
477 printf("\n");
478 a->loading_done = 1;
481 if (fmt_scaled(packfile_size, scaled_size) == -1)
482 return got_error_from_errno("fmt_scaled");
484 if (a->last_ncommits != ncommits) {
485 print_searching = 1;
486 a->last_ncommits = ncommits;
489 if (a->last_nobj_total != nobj_total) {
490 print_searching = 1;
491 print_total = 1;
492 a->last_nobj_total = nobj_total;
495 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
496 strcmp(scaled_size, a->last_scaled_size)) != 0) {
497 if (strlcpy(a->last_scaled_size, scaled_size,
498 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
499 return got_error(GOT_ERR_NO_SPACE);
502 if (nobj_deltify > 0 || nobj_written > 0) {
503 if (nobj_deltify > 0) {
504 p_deltify = (nobj_deltify * 100) / nobj_total;
505 if (p_deltify != a->last_p_deltify) {
506 a->last_p_deltify = p_deltify;
507 print_searching = 1;
508 print_total = 1;
509 print_deltify = 1;
512 if (nobj_written > 0) {
513 p_written = (nobj_written * 100) / nobj_total;
514 if (p_written != a->last_p_written) {
515 a->last_p_written = p_written;
516 print_searching = 1;
517 print_total = 1;
518 print_deltify = 1;
519 print_written = 1;
524 if (print_searching || print_total || print_deltify || print_written)
525 printf("\r");
526 if (print_searching)
527 printf("packing %d reference%s", ncommits,
528 ncommits == 1 ? "" : "s");
529 if (print_total)
530 printf("; %d object%s", nobj_total,
531 nobj_total == 1 ? "" : "s");
532 if (print_deltify)
533 printf("; deltify: %d%%", p_deltify);
534 if (print_written)
535 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
536 scaled_size, p_written);
537 if (print_searching || print_total || print_deltify ||
538 print_written) {
539 a->printed_something = 1;
540 fflush(stdout);
542 return NULL;
545 static const struct got_error *
546 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
547 int nobj_indexed, int nobj_loose, int nobj_resolved)
549 struct got_pack_progress_arg *a = arg;
550 char scaled_size[FMT_SCALED_STRSIZE];
551 int p_indexed, p_resolved;
552 int print_size = 0, print_indexed = 0, print_resolved = 0;
554 if (a->verbosity < 0)
555 return NULL;
557 if (packfile_size > 0 || nobj_indexed > 0) {
558 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
559 (a->last_scaled_size[0] == '\0' ||
560 strcmp(scaled_size, a->last_scaled_size)) != 0) {
561 print_size = 1;
562 if (strlcpy(a->last_scaled_size, scaled_size,
563 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
564 return got_error(GOT_ERR_NO_SPACE);
566 if (nobj_indexed > 0) {
567 p_indexed = (nobj_indexed * 100) / nobj_total;
568 if (p_indexed != a->last_p_indexed) {
569 a->last_p_indexed = p_indexed;
570 print_indexed = 1;
571 print_size = 1;
574 if (nobj_resolved > 0) {
575 p_resolved = (nobj_resolved * 100) /
576 (nobj_total - nobj_loose);
577 if (p_resolved != a->last_p_resolved) {
578 a->last_p_resolved = p_resolved;
579 print_resolved = 1;
580 print_indexed = 1;
581 print_size = 1;
586 if (print_size || print_indexed || print_resolved)
587 printf("\r");
588 if (print_size)
589 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
590 if (print_indexed)
591 printf("; indexing %d%%", p_indexed);
592 if (print_resolved)
593 printf("; resolving deltas %d%%", p_resolved);
594 if (print_size || print_indexed || print_resolved)
595 fflush(stdout);
597 return NULL;
600 static const struct got_error *
601 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
602 const char *refname, struct got_repository *repo)
604 const struct got_error *err;
605 struct got_reference *ref;
607 *new = NULL;
609 err = got_ref_open(&ref, repo, refname, 0);
610 if (err) {
611 if (err->code != GOT_ERR_NOT_REF)
612 return err;
614 /* Treat argument as a reference prefix. */
615 err = got_ref_list(refs, repo, refname,
616 got_ref_cmp_by_name, NULL);
617 } else {
618 err = got_reflist_insert(new, refs, ref,
619 got_ref_cmp_by_name, NULL);
620 if (err || *new == NULL /* duplicate */)
621 got_ref_close(ref);
624 return err;
627 static const struct got_error *
628 cmd_pack(int argc, char *argv[])
630 const struct got_error *error = NULL;
631 char *repo_path = NULL;
632 struct got_repository *repo = NULL;
633 int ch, i, loose_obj_only = 1, verbosity = 0;
634 struct got_object_id *pack_hash = NULL;
635 char *id_str = NULL;
636 struct got_pack_progress_arg ppa;
637 FILE *packfile = NULL;
638 struct got_pathlist_head exclude_args;
639 struct got_pathlist_entry *pe;
640 struct got_reflist_head exclude_refs;
641 struct got_reflist_head include_refs;
642 struct got_reflist_entry *re, *new;
643 int *pack_fds = NULL;
645 TAILQ_INIT(&exclude_args);
646 TAILQ_INIT(&exclude_refs);
647 TAILQ_INIT(&include_refs);
649 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
650 switch (ch) {
651 case 'a':
652 loose_obj_only = 0;
653 break;
654 case 'r':
655 repo_path = realpath(optarg, NULL);
656 if (repo_path == NULL)
657 return got_error_from_errno2("realpath",
658 optarg);
659 got_path_strip_trailing_slashes(repo_path);
660 break;
661 case 'x':
662 got_path_strip_trailing_slashes(optarg);
663 error = got_pathlist_append(&exclude_args,
664 optarg, NULL);
665 if (error)
666 return error;
667 break;
668 case 'q':
669 verbosity = -1;
670 break;
671 default:
672 usage_pack();
673 /* NOTREACHED */
677 argc -= optind;
678 argv += optind;
680 #ifndef PROFILE
681 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
682 NULL) == -1)
683 err(1, "pledge");
684 #endif
685 if (repo_path == NULL) {
686 error = get_repo_path(&repo_path);
687 if (error)
688 goto done;
690 error = got_repo_pack_fds_open(&pack_fds);
691 if (error != NULL)
692 goto done;
693 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
694 if (error)
695 goto done;
697 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
698 if (error)
699 goto done;
701 TAILQ_FOREACH(pe, &exclude_args, entry) {
702 const char *refname = pe->path;
703 error = add_ref(&new, &exclude_refs, refname, repo);
704 if (error)
705 goto done;
709 if (argc == 0) {
710 error = got_ref_list(&include_refs, repo, "",
711 got_ref_cmp_by_name, NULL);
712 if (error)
713 goto done;
714 } else {
715 for (i = 0; i < argc; i++) {
716 const char *refname;
717 got_path_strip_trailing_slashes(argv[i]);
718 refname = argv[i];
719 error = add_ref(&new, &include_refs, refname, repo);
720 if (error)
721 goto done;
725 /* Ignore references in the refs/got/ namespace. */
726 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
727 const char *refname = got_ref_get_name(re->ref);
728 if (strncmp("refs/got/", refname, 9) != 0)
729 continue;
730 TAILQ_REMOVE(&include_refs, re, entry);
731 got_ref_close(re->ref);
732 free(re);
735 memset(&ppa, 0, sizeof(ppa));
736 ppa.last_scaled_size[0] = '\0';
737 ppa.last_p_indexed = -1;
738 ppa.last_p_resolved = -1;
739 ppa.verbosity = verbosity;
741 error = got_repo_pack_objects(&packfile, &pack_hash,
742 &include_refs, &exclude_refs, repo, loose_obj_only,
743 pack_progress, &ppa, check_cancelled, NULL);
744 if (error) {
745 if (ppa.printed_something)
746 printf("\n");
747 goto done;
750 error = got_object_id_str(&id_str, pack_hash);
751 if (error)
752 goto done;
753 if (verbosity >= 0)
754 printf("\nWrote %s.pack\n", id_str);
756 error = got_repo_index_pack(packfile, pack_hash, repo,
757 pack_index_progress, &ppa, check_cancelled, NULL);
758 if (error)
759 goto done;
760 if (verbosity >= 0)
761 printf("\nIndexed %s.pack\n", id_str);
762 done:
763 if (repo)
764 got_repo_close(repo);
765 if (pack_fds) {
766 const struct got_error *pack_err =
767 got_repo_pack_fds_close(pack_fds);
768 if (error == NULL)
769 error = pack_err;
771 got_pathlist_free(&exclude_args);
772 got_ref_list_free(&exclude_refs);
773 got_ref_list_free(&include_refs);
774 free(id_str);
775 free(pack_hash);
776 free(repo_path);
777 return error;
780 __dead static void
781 usage_indexpack(void)
783 fprintf(stderr, "usage: %s indexpack packfile-path\n",
784 getprogname());
785 exit(1);
788 static const struct got_error *
789 cmd_indexpack(int argc, char *argv[])
791 const struct got_error *error = NULL;
792 struct got_repository *repo = NULL;
793 int ch;
794 struct got_object_id *pack_hash = NULL;
795 char *packfile_path = NULL;
796 char *id_str = NULL;
797 struct got_pack_progress_arg ppa;
798 FILE *packfile = NULL;
799 int *pack_fds = NULL;
801 while ((ch = getopt(argc, argv, "")) != -1) {
802 switch (ch) {
803 default:
804 usage_indexpack();
805 /* NOTREACHED */
809 argc -= optind;
810 argv += optind;
812 if (argc != 1)
813 usage_indexpack();
815 packfile_path = realpath(argv[0], NULL);
816 if (packfile_path == NULL)
817 return got_error_from_errno2("realpath", argv[0]);
819 #ifndef PROFILE
820 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
821 NULL) == -1)
822 err(1, "pledge");
823 #endif
825 error = got_repo_pack_fds_open(&pack_fds);
826 if (error != NULL)
827 goto done;
828 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
829 if (error)
830 goto done;
832 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
833 if (error)
834 goto done;
836 memset(&ppa, 0, sizeof(ppa));
837 ppa.last_scaled_size[0] = '\0';
838 ppa.last_p_indexed = -1;
839 ppa.last_p_resolved = -1;
841 error = got_repo_find_pack(&packfile, &pack_hash, repo,
842 packfile_path);
843 if (error)
844 goto done;
846 error = got_object_id_str(&id_str, pack_hash);
847 if (error)
848 goto done;
850 error = got_repo_index_pack(packfile, pack_hash, repo,
851 pack_index_progress, &ppa, check_cancelled, NULL);
852 if (error)
853 goto done;
854 printf("\nIndexed %s.pack\n", id_str);
855 done:
856 if (repo)
857 got_repo_close(repo);
858 if (pack_fds) {
859 const struct got_error *pack_err =
860 got_repo_pack_fds_close(pack_fds);
861 if (error == NULL)
862 error = pack_err;
864 free(id_str);
865 free(pack_hash);
866 return error;
869 __dead static void
870 usage_listpack(void)
872 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
873 getprogname());
874 exit(1);
877 struct gotadmin_list_pack_cb_args {
878 int nblobs;
879 int ntrees;
880 int ncommits;
881 int ntags;
882 int noffdeltas;
883 int nrefdeltas;
884 int human_readable;
885 };
887 static const struct got_error *
888 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
889 off_t size, off_t base_offset, struct got_object_id *base_id)
891 const struct got_error *err;
892 struct gotadmin_list_pack_cb_args *a = arg;
893 char *id_str, *delta_str = NULL, *base_id_str = NULL;
894 const char *type_str;
896 err = got_object_id_str(&id_str, id);
897 if (err)
898 return err;
900 switch (type) {
901 case GOT_OBJ_TYPE_BLOB:
902 type_str = GOT_OBJ_LABEL_BLOB;
903 a->nblobs++;
904 break;
905 case GOT_OBJ_TYPE_TREE:
906 type_str = GOT_OBJ_LABEL_TREE;
907 a->ntrees++;
908 break;
909 case GOT_OBJ_TYPE_COMMIT:
910 type_str = GOT_OBJ_LABEL_COMMIT;
911 a->ncommits++;
912 break;
913 case GOT_OBJ_TYPE_TAG:
914 type_str = GOT_OBJ_LABEL_TAG;
915 a->ntags++;
916 break;
917 case GOT_OBJ_TYPE_OFFSET_DELTA:
918 type_str = "offset-delta";
919 if (asprintf(&delta_str, " base-offset %lld",
920 (long long)base_offset) == -1) {
921 err = got_error_from_errno("asprintf");
922 goto done;
924 a->noffdeltas++;
925 break;
926 case GOT_OBJ_TYPE_REF_DELTA:
927 type_str = "ref-delta";
928 err = got_object_id_str(&base_id_str, base_id);
929 if (err)
930 goto done;
931 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
932 err = got_error_from_errno("asprintf");
933 goto done;
935 a->nrefdeltas++;
936 break;
937 default:
938 err = got_error(GOT_ERR_OBJ_TYPE);
939 goto done;
941 if (a->human_readable) {
942 char scaled[FMT_SCALED_STRSIZE];
943 char *s;;
944 if (fmt_scaled(size, scaled) == -1) {
945 err = got_error_from_errno("fmt_scaled");
946 goto done;
948 s = scaled;
949 while (isspace((unsigned char)*s))
950 s++;
951 printf("%s %s at %lld size %s%s\n", id_str, type_str,
952 (long long)offset, s, delta_str ? delta_str : "");
953 } else {
954 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
955 (long long)offset, (long long)size,
956 delta_str ? delta_str : "");
958 done:
959 free(id_str);
960 free(base_id_str);
961 free(delta_str);
962 return err;
965 static const struct got_error *
966 cmd_listpack(int argc, char *argv[])
968 const struct got_error *error = NULL;
969 struct got_repository *repo = NULL;
970 int ch;
971 struct got_object_id *pack_hash = NULL;
972 char *packfile_path = NULL;
973 char *id_str = NULL;
974 struct gotadmin_list_pack_cb_args lpa;
975 FILE *packfile = NULL;
976 int show_stats = 0, human_readable = 0;
977 int *pack_fds = NULL;
979 while ((ch = getopt(argc, argv, "hs")) != -1) {
980 switch (ch) {
981 case 'h':
982 human_readable = 1;
983 break;
984 case 's':
985 show_stats = 1;
986 break;
987 default:
988 usage_listpack();
989 /* NOTREACHED */
993 argc -= optind;
994 argv += optind;
996 if (argc != 1)
997 usage_listpack();
998 packfile_path = realpath(argv[0], NULL);
999 if (packfile_path == NULL)
1000 return got_error_from_errno2("realpath", argv[0]);
1002 #ifndef PROFILE
1003 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1004 NULL) == -1)
1005 err(1, "pledge");
1006 #endif
1007 error = got_repo_pack_fds_open(&pack_fds);
1008 if (error != NULL)
1009 goto done;
1010 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1011 if (error)
1012 goto done;
1013 #ifndef PROFILE
1014 /* Remove "cpath" promise. */
1015 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1016 NULL) == -1)
1017 err(1, "pledge");
1018 #endif
1019 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1020 if (error)
1021 goto done;
1023 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1024 packfile_path);
1025 if (error)
1026 goto done;
1027 error = got_object_id_str(&id_str, pack_hash);
1028 if (error)
1029 goto done;
1031 memset(&lpa, 0, sizeof(lpa));
1032 lpa.human_readable = human_readable;
1033 error = got_repo_list_pack(packfile, pack_hash, repo,
1034 list_pack_cb, &lpa, check_cancelled, NULL);
1035 if (error)
1036 goto done;
1037 if (show_stats) {
1038 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1039 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1040 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1041 lpa.noffdeltas + lpa.nrefdeltas,
1042 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1043 lpa.noffdeltas, lpa.nrefdeltas);
1045 done:
1046 if (repo)
1047 got_repo_close(repo);
1048 if (pack_fds) {
1049 const struct got_error *pack_err =
1050 got_repo_pack_fds_close(pack_fds);
1051 if (error == NULL)
1052 error = pack_err;
1054 free(id_str);
1055 free(pack_hash);
1056 free(packfile_path);
1057 return error;
1060 __dead static void
1061 usage_cleanup(void)
1063 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
1064 "[-q]\n", getprogname());
1065 exit(1);
1068 struct got_cleanup_progress_arg {
1069 int last_nloose;
1070 int last_ncommits;
1071 int last_npurged;
1072 int verbosity;
1073 int printed_something;
1074 int dry_run;
1077 static const struct got_error *
1078 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1080 struct got_cleanup_progress_arg *a = arg;
1081 int print_loose = 0, print_commits = 0, print_purged = 0;
1083 if (a->last_nloose != nloose) {
1084 print_loose = 1;
1085 a->last_nloose = nloose;
1087 if (a->last_ncommits != ncommits) {
1088 print_loose = 1;
1089 print_commits = 1;
1090 a->last_ncommits = ncommits;
1092 if (a->last_npurged != npurged) {
1093 print_loose = 1;
1094 print_commits = 1;
1095 print_purged = 1;
1096 a->last_npurged = npurged;
1099 if (a->verbosity < 0)
1100 return NULL;
1102 if (print_loose || print_commits || print_purged)
1103 printf("\r");
1104 if (print_loose)
1105 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1106 if (print_commits)
1107 printf("; %d commit%s scanned", ncommits,
1108 ncommits == 1 ? "" : "s");
1109 if (print_purged) {
1110 if (a->dry_run) {
1111 printf("; %d object%s could be purged", npurged,
1112 npurged == 1 ? "" : "s");
1113 } else {
1114 printf("; %d object%s purged", npurged,
1115 npurged == 1 ? "" : "s");
1118 if (print_loose || print_commits || print_purged) {
1119 a->printed_something = 1;
1120 fflush(stdout);
1122 return NULL;
1125 struct got_lonely_packidx_progress_arg {
1126 int verbosity;
1127 int printed_something;
1128 int dry_run;
1131 static const struct got_error *
1132 lonely_packidx_progress(void *arg, const char *path)
1134 struct got_lonely_packidx_progress_arg *a = arg;
1136 if (a->verbosity < 0)
1137 return NULL;
1139 if (a->dry_run)
1140 printf("%s could be removed\n", path);
1141 else
1142 printf("%s removed\n", path);
1144 a->printed_something = 1;
1145 return NULL;
1148 static const struct got_error *
1149 cmd_cleanup(int argc, char *argv[])
1151 const struct got_error *error = NULL;
1152 char *repo_path = NULL;
1153 struct got_repository *repo = NULL;
1154 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1155 int remove_lonely_packidx = 0, ignore_mtime = 0;
1156 struct got_cleanup_progress_arg cpa;
1157 struct got_lonely_packidx_progress_arg lpa;
1158 off_t size_before, size_after;
1159 char scaled_before[FMT_SCALED_STRSIZE];
1160 char scaled_after[FMT_SCALED_STRSIZE];
1161 char scaled_diff[FMT_SCALED_STRSIZE];
1162 char **extensions;
1163 int nextensions, i;
1164 int *pack_fds = NULL;
1166 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1167 switch (ch) {
1168 case 'a':
1169 ignore_mtime = 1;
1170 break;
1171 case 'p':
1172 remove_lonely_packidx = 1;
1173 break;
1174 case 'r':
1175 repo_path = realpath(optarg, NULL);
1176 if (repo_path == NULL)
1177 return got_error_from_errno2("realpath",
1178 optarg);
1179 got_path_strip_trailing_slashes(repo_path);
1180 break;
1181 case 'n':
1182 dry_run = 1;
1183 break;
1184 case 'q':
1185 verbosity = -1;
1186 break;
1187 default:
1188 usage_cleanup();
1189 /* NOTREACHED */
1193 argc -= optind;
1194 argv += optind;
1196 #ifndef PROFILE
1197 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1198 NULL) == -1)
1199 err(1, "pledge");
1200 #endif
1201 if (repo_path == NULL) {
1202 error = get_repo_path(&repo_path);
1203 if (error)
1204 goto done;
1206 error = got_repo_pack_fds_open(&pack_fds);
1207 if (error != NULL)
1208 goto done;
1209 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1210 if (error)
1211 goto done;
1213 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1214 if (error)
1215 goto done;
1217 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1218 repo);
1219 for (i = 0; i < nextensions; i++) {
1220 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1221 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1222 "the preciousObjects Git extension is enabled; "
1223 "this implies that objects must not be deleted");
1224 goto done;
1228 if (remove_lonely_packidx) {
1229 memset(&lpa, 0, sizeof(lpa));
1230 lpa.dry_run = dry_run;
1231 lpa.verbosity = verbosity;
1232 error = got_repo_remove_lonely_packidx(repo, dry_run,
1233 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1234 goto done;
1237 memset(&cpa, 0, sizeof(cpa));
1238 cpa.last_ncommits = -1;
1239 cpa.last_npurged = -1;
1240 cpa.dry_run = dry_run;
1241 cpa.verbosity = verbosity;
1242 error = got_repo_purge_unreferenced_loose_objects(repo,
1243 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1244 cleanup_progress, &cpa, check_cancelled, NULL);
1245 if (cpa.printed_something)
1246 printf("\n");
1247 if (error)
1248 goto done;
1249 if (cpa.printed_something) {
1250 if (fmt_scaled(size_before, scaled_before) == -1) {
1251 error = got_error_from_errno("fmt_scaled");
1252 goto done;
1254 if (fmt_scaled(size_after, scaled_after) == -1) {
1255 error = got_error_from_errno("fmt_scaled");
1256 goto done;
1258 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1259 error = got_error_from_errno("fmt_scaled");
1260 goto done;
1262 printf("loose total size before: %s\n", scaled_before);
1263 printf("loose total size after: %s\n", scaled_after);
1264 if (dry_run) {
1265 printf("disk space which would be freed: %s\n",
1266 scaled_diff);
1267 } else
1268 printf("disk space freed: %s\n", scaled_diff);
1269 printf("loose objects also found in pack files: %d\n", npacked);
1271 done:
1272 if (repo)
1273 got_repo_close(repo);
1274 if (pack_fds) {
1275 const struct got_error *pack_err =
1276 got_repo_pack_fds_close(pack_fds);
1277 if (error == NULL)
1278 error = pack_err;
1280 free(repo_path);
1281 return error;