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 [-hV] 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 [-b branch] repository-path\n",
275 getprogname());
276 exit(1);
279 static const struct got_error *
280 cmd_init(int argc, char *argv[])
282 const struct got_error *error = NULL;
283 const char *head_name = NULL;
284 char *repo_path = NULL;
285 int ch;
287 #ifndef PROFILE
288 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
289 err(1, "pledge");
290 #endif
292 while ((ch = getopt(argc, argv, "b:")) != -1) {
293 switch (ch) {
294 case 'b':
295 head_name = optarg;
296 break;
297 default:
298 usage_init();
299 /* NOTREACHED */
303 argc -= optind;
304 argv += optind;
306 if (argc != 1)
307 usage_init();
309 repo_path = strdup(argv[0]);
310 if (repo_path == NULL)
311 return got_error_from_errno("strdup");
313 got_path_strip_trailing_slashes(repo_path);
315 error = got_path_mkdir(repo_path);
316 if (error &&
317 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
318 goto done;
320 error = apply_unveil(repo_path, 0);
321 if (error)
322 goto done;
324 error = got_repo_init(repo_path, head_name);
325 done:
326 free(repo_path);
327 return error;
330 static const struct got_error *
331 cmd_info(int argc, char *argv[])
333 const struct got_error *error = NULL;
334 char *repo_path = NULL;
335 struct got_repository *repo = NULL;
336 const struct got_gotconfig *gotconfig = NULL;
337 int ch, npackfiles, npackedobj, nobj;
338 off_t packsize, loose_size;
339 char scaled[FMT_SCALED_STRSIZE];
340 int *pack_fds = NULL;
342 #ifndef PROFILE
343 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
344 NULL) == -1)
345 err(1, "pledge");
346 #endif
348 while ((ch = getopt(argc, argv, "r:")) != -1) {
349 switch (ch) {
350 case 'r':
351 repo_path = realpath(optarg, NULL);
352 if (repo_path == NULL)
353 return got_error_from_errno2("realpath",
354 optarg);
355 got_path_strip_trailing_slashes(repo_path);
356 break;
357 default:
358 usage_info();
359 /* NOTREACHED */
363 argc -= optind;
364 argv += optind;
366 if (repo_path == NULL) {
367 error = get_repo_path(&repo_path);
368 if (error)
369 goto done;
371 error = got_repo_pack_fds_open(&pack_fds);
372 if (error != NULL)
373 goto done;
374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
375 if (error)
376 goto done;
377 #ifndef PROFILE
378 /* Remove "cpath" promise. */
379 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
380 NULL) == -1)
381 err(1, "pledge");
382 #endif
383 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
384 if (error)
385 goto done;
387 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
389 gotconfig = got_repo_get_gotconfig(repo);
390 if (gotconfig) {
391 const struct got_remote_repo *remotes;
392 int i, nremotes;
393 if (got_gotconfig_get_author(gotconfig)) {
394 printf("default author: %s\n",
395 got_gotconfig_get_author(gotconfig));
397 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
398 for (i = 0; i < nremotes; i++) {
399 const char *fetch_url = remotes[i].fetch_url;
400 const char *send_url = remotes[i].send_url;
401 if (strcmp(fetch_url, send_url) == 0) {
402 printf("remote \"%s\": %s\n", remotes[i].name,
403 remotes[i].fetch_url);
404 } else {
405 printf("remote \"%s\" (fetch): %s\n",
406 remotes[i].name, remotes[i].fetch_url);
407 printf("remote \"%s\" (send): %s\n",
408 remotes[i].name, remotes[i].send_url);
413 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
414 &packsize, repo);
415 if (error)
416 goto done;
417 printf("pack files: %d\n", npackfiles);
418 if (npackfiles > 0) {
419 if (fmt_scaled(packsize, scaled) == -1) {
420 error = got_error_from_errno("fmt_scaled");
421 goto done;
423 printf("packed objects: %d\n", npackedobj);
424 printf("packed total size: %s\n", scaled);
427 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
428 if (error)
429 goto done;
430 printf("loose objects: %d\n", nobj);
431 if (nobj > 0) {
432 if (fmt_scaled(loose_size, scaled) == -1) {
433 error = got_error_from_errno("fmt_scaled");
434 goto done;
436 printf("loose total size: %s\n", scaled);
438 done:
439 if (repo)
440 got_repo_close(repo);
441 if (pack_fds) {
442 const struct got_error *pack_err =
443 got_repo_pack_fds_close(pack_fds);
444 if (error == NULL)
445 error = pack_err;
448 free(repo_path);
449 return error;
452 __dead static void
453 usage_pack(void)
455 fprintf(stderr, "usage: %s pack [-aq] [-r repository-path] "
456 "[-x reference] [reference ...]\n", getprogname());
457 exit(1);
460 struct got_pack_progress_arg {
461 char last_scaled_size[FMT_SCALED_STRSIZE];
462 int last_ncolored;
463 int last_nfound;
464 int last_ntrees;
465 int loading_done;
466 int last_ncommits;
467 int last_nobj_total;
468 int last_p_deltify;
469 int last_p_written;
470 int last_p_indexed;
471 int last_p_resolved;
472 int verbosity;
473 int printed_something;
474 };
476 static void
477 print_load_info(int print_colored, int print_found, int print_trees,
478 int ncolored, int nfound, int ntrees)
480 if (print_colored) {
481 printf("%d commit%s colored", ncolored,
482 ncolored == 1 ? "" : "s");
484 if (print_found) {
485 printf("%s%d object%s found",
486 ncolored > 0 ? "; " : "",
487 nfound, nfound == 1 ? "" : "s");
489 if (print_trees) {
490 printf("; %d tree%s scanned", ntrees,
491 ntrees == 1 ? "" : "s");
495 static const struct got_error *
496 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
497 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
498 int nobj_written)
500 struct got_pack_progress_arg *a = arg;
501 char scaled_size[FMT_SCALED_STRSIZE];
502 int p_deltify, p_written;
503 int print_colored = 0, print_found = 0, print_trees = 0;
504 int print_searching = 0, print_total = 0;
505 int print_deltify = 0, print_written = 0;
507 if (a->verbosity < 0)
508 return NULL;
510 if (a->last_ncolored != ncolored) {
511 print_colored = 1;
512 a->last_ncolored = ncolored;
515 if (a->last_nfound != nfound) {
516 print_colored = 1;
517 print_found = 1;
518 a->last_nfound = nfound;
521 if (a->last_ntrees != ntrees) {
522 print_colored = 1;
523 print_found = 1;
524 print_trees = 1;
525 a->last_ntrees = ntrees;
528 if ((print_colored || print_found || print_trees) &&
529 !a->loading_done) {
530 printf("\r");
531 print_load_info(print_colored, print_found, print_trees,
532 ncolored, nfound, ntrees);
533 a->printed_something = 1;
534 fflush(stdout);
535 return NULL;
536 } else if (!a->loading_done) {
537 printf("\r");
538 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
539 printf("\n");
540 a->loading_done = 1;
543 if (fmt_scaled(packfile_size, scaled_size) == -1)
544 return got_error_from_errno("fmt_scaled");
546 if (a->last_ncommits != ncommits) {
547 print_searching = 1;
548 a->last_ncommits = ncommits;
551 if (a->last_nobj_total != nobj_total) {
552 print_searching = 1;
553 print_total = 1;
554 a->last_nobj_total = nobj_total;
557 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
558 strcmp(scaled_size, a->last_scaled_size)) != 0) {
559 if (strlcpy(a->last_scaled_size, scaled_size,
560 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
561 return got_error(GOT_ERR_NO_SPACE);
564 if (nobj_deltify > 0 || nobj_written > 0) {
565 if (nobj_deltify > 0) {
566 p_deltify = (nobj_deltify * 100) / nobj_total;
567 if (p_deltify != a->last_p_deltify) {
568 a->last_p_deltify = p_deltify;
569 print_searching = 1;
570 print_total = 1;
571 print_deltify = 1;
574 if (nobj_written > 0) {
575 p_written = (nobj_written * 100) / nobj_total;
576 if (p_written != a->last_p_written) {
577 a->last_p_written = p_written;
578 print_searching = 1;
579 print_total = 1;
580 print_deltify = 1;
581 print_written = 1;
586 if (print_searching || print_total || print_deltify || print_written)
587 printf("\r");
588 if (print_searching)
589 printf("packing %d reference%s", ncommits,
590 ncommits == 1 ? "" : "s");
591 if (print_total)
592 printf("; %d object%s", nobj_total,
593 nobj_total == 1 ? "" : "s");
594 if (print_deltify)
595 printf("; deltify: %d%%", p_deltify);
596 if (print_written)
597 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
598 scaled_size, p_written);
599 if (print_searching || print_total || print_deltify ||
600 print_written) {
601 a->printed_something = 1;
602 fflush(stdout);
604 return NULL;
607 static const struct got_error *
608 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
609 int nobj_indexed, int nobj_loose, int nobj_resolved)
611 struct got_pack_progress_arg *a = arg;
612 char scaled_size[FMT_SCALED_STRSIZE];
613 int p_indexed, p_resolved;
614 int print_size = 0, print_indexed = 0, print_resolved = 0;
616 if (a->verbosity < 0)
617 return NULL;
619 if (packfile_size > 0 || nobj_indexed > 0) {
620 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
621 (a->last_scaled_size[0] == '\0' ||
622 strcmp(scaled_size, a->last_scaled_size)) != 0) {
623 print_size = 1;
624 if (strlcpy(a->last_scaled_size, scaled_size,
625 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
626 return got_error(GOT_ERR_NO_SPACE);
628 if (nobj_indexed > 0) {
629 p_indexed = (nobj_indexed * 100) / nobj_total;
630 if (p_indexed != a->last_p_indexed) {
631 a->last_p_indexed = p_indexed;
632 print_indexed = 1;
633 print_size = 1;
636 if (nobj_resolved > 0) {
637 p_resolved = (nobj_resolved * 100) /
638 (nobj_total - nobj_loose);
639 if (p_resolved != a->last_p_resolved) {
640 a->last_p_resolved = p_resolved;
641 print_resolved = 1;
642 print_indexed = 1;
643 print_size = 1;
648 if (print_size || print_indexed || print_resolved)
649 printf("\r");
650 if (print_size)
651 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
652 if (print_indexed)
653 printf("; indexing %d%%", p_indexed);
654 if (print_resolved)
655 printf("; resolving deltas %d%%", p_resolved);
656 if (print_size || print_indexed || print_resolved)
657 fflush(stdout);
659 return NULL;
662 static const struct got_error *
663 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
664 const char *refname, struct got_repository *repo)
666 const struct got_error *err;
667 struct got_reference *ref;
669 *new = NULL;
671 err = got_ref_open(&ref, repo, refname, 0);
672 if (err) {
673 if (err->code != GOT_ERR_NOT_REF)
674 return err;
676 /* Treat argument as a reference prefix. */
677 err = got_ref_list(refs, repo, refname,
678 got_ref_cmp_by_name, NULL);
679 } else {
680 err = got_reflist_insert(new, refs, ref,
681 got_ref_cmp_by_name, NULL);
682 if (err || *new == NULL /* duplicate */)
683 got_ref_close(ref);
686 return err;
689 static const struct got_error *
690 cmd_pack(int argc, char *argv[])
692 const struct got_error *error = NULL;
693 char *repo_path = NULL;
694 struct got_repository *repo = NULL;
695 int ch, i, loose_obj_only = 1, verbosity = 0;
696 struct got_object_id *pack_hash = NULL;
697 char *id_str = NULL;
698 struct got_pack_progress_arg ppa;
699 FILE *packfile = NULL;
700 struct got_pathlist_head exclude_args;
701 struct got_pathlist_entry *pe;
702 struct got_reflist_head exclude_refs;
703 struct got_reflist_head include_refs;
704 struct got_reflist_entry *re, *new;
705 int *pack_fds = NULL;
707 TAILQ_INIT(&exclude_args);
708 TAILQ_INIT(&exclude_refs);
709 TAILQ_INIT(&include_refs);
711 #ifndef PROFILE
712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
713 NULL) == -1)
714 err(1, "pledge");
715 #endif
717 while ((ch = getopt(argc, argv, "aqr:x:")) != -1) {
718 switch (ch) {
719 case 'a':
720 loose_obj_only = 0;
721 break;
722 case 'q':
723 verbosity = -1;
724 break;
725 case 'r':
726 repo_path = realpath(optarg, NULL);
727 if (repo_path == NULL)
728 return got_error_from_errno2("realpath",
729 optarg);
730 got_path_strip_trailing_slashes(repo_path);
731 break;
732 case 'x':
733 got_path_strip_trailing_slashes(optarg);
734 error = got_pathlist_append(&exclude_args,
735 optarg, NULL);
736 if (error)
737 return error;
738 break;
739 default:
740 usage_pack();
741 /* NOTREACHED */
745 argc -= optind;
746 argv += optind;
748 if (repo_path == NULL) {
749 error = get_repo_path(&repo_path);
750 if (error)
751 goto done;
753 error = got_repo_pack_fds_open(&pack_fds);
754 if (error != NULL)
755 goto done;
756 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
757 if (error)
758 goto done;
760 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
761 if (error)
762 goto done;
764 TAILQ_FOREACH(pe, &exclude_args, entry) {
765 const char *refname = pe->path;
766 error = add_ref(&new, &exclude_refs, refname, repo);
767 if (error)
768 goto done;
771 if (argc == 0) {
772 error = got_ref_list(&include_refs, repo, "",
773 got_ref_cmp_by_name, NULL);
774 if (error)
775 goto done;
776 } else {
777 for (i = 0; i < argc; i++) {
778 const char *refname;
779 got_path_strip_trailing_slashes(argv[i]);
780 refname = argv[i];
781 error = add_ref(&new, &include_refs, refname, repo);
782 if (error)
783 goto done;
787 /* Ignore references in the refs/got/ namespace. */
788 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
789 const char *refname = got_ref_get_name(re->ref);
790 if (strncmp("refs/got/", refname, 9) != 0)
791 continue;
792 TAILQ_REMOVE(&include_refs, re, entry);
793 got_ref_close(re->ref);
794 free(re);
797 memset(&ppa, 0, sizeof(ppa));
798 ppa.last_scaled_size[0] = '\0';
799 ppa.last_p_indexed = -1;
800 ppa.last_p_resolved = -1;
801 ppa.verbosity = verbosity;
803 error = got_repo_pack_objects(&packfile, &pack_hash,
804 &include_refs, &exclude_refs, repo, loose_obj_only,
805 pack_progress, &ppa, check_cancelled, NULL);
806 if (error) {
807 if (ppa.printed_something)
808 printf("\n");
809 goto done;
812 error = got_object_id_str(&id_str, pack_hash);
813 if (error)
814 goto done;
815 if (verbosity >= 0)
816 printf("\nWrote %s.pack\n", id_str);
818 error = got_repo_index_pack(packfile, pack_hash, repo,
819 pack_index_progress, &ppa, check_cancelled, NULL);
820 if (error)
821 goto done;
822 if (verbosity >= 0)
823 printf("\nIndexed %s.pack\n", id_str);
824 done:
825 if (repo)
826 got_repo_close(repo);
827 if (pack_fds) {
828 const struct got_error *pack_err =
829 got_repo_pack_fds_close(pack_fds);
830 if (error == NULL)
831 error = pack_err;
833 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
834 got_ref_list_free(&exclude_refs);
835 got_ref_list_free(&include_refs);
836 free(id_str);
837 free(pack_hash);
838 free(repo_path);
839 return error;
842 __dead static void
843 usage_indexpack(void)
845 fprintf(stderr, "usage: %s indexpack packfile-path\n",
846 getprogname());
847 exit(1);
850 static const struct got_error *
851 cmd_indexpack(int argc, char *argv[])
853 const struct got_error *error = NULL;
854 struct got_repository *repo = NULL;
855 int ch;
856 struct got_object_id *pack_hash = NULL;
857 char *packfile_path = NULL;
858 char *id_str = NULL;
859 struct got_pack_progress_arg ppa;
860 FILE *packfile = NULL;
861 int *pack_fds = NULL;
863 while ((ch = getopt(argc, argv, "")) != -1) {
864 switch (ch) {
865 default:
866 usage_indexpack();
867 /* NOTREACHED */
871 argc -= optind;
872 argv += optind;
874 if (argc != 1)
875 usage_indexpack();
877 packfile_path = realpath(argv[0], NULL);
878 if (packfile_path == NULL)
879 return got_error_from_errno2("realpath", argv[0]);
881 #ifndef PROFILE
882 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
883 NULL) == -1)
884 err(1, "pledge");
885 #endif
887 error = got_repo_pack_fds_open(&pack_fds);
888 if (error != NULL)
889 goto done;
890 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
891 if (error)
892 goto done;
894 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
895 if (error)
896 goto done;
898 memset(&ppa, 0, sizeof(ppa));
899 ppa.last_scaled_size[0] = '\0';
900 ppa.last_p_indexed = -1;
901 ppa.last_p_resolved = -1;
903 error = got_repo_find_pack(&packfile, &pack_hash, repo,
904 packfile_path);
905 if (error)
906 goto done;
908 error = got_object_id_str(&id_str, pack_hash);
909 if (error)
910 goto done;
912 error = got_repo_index_pack(packfile, pack_hash, repo,
913 pack_index_progress, &ppa, check_cancelled, NULL);
914 if (error)
915 goto done;
916 printf("\nIndexed %s.pack\n", id_str);
917 done:
918 if (repo)
919 got_repo_close(repo);
920 if (pack_fds) {
921 const struct got_error *pack_err =
922 got_repo_pack_fds_close(pack_fds);
923 if (error == NULL)
924 error = pack_err;
926 free(id_str);
927 free(pack_hash);
928 return error;
931 __dead static void
932 usage_listpack(void)
934 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
935 getprogname());
936 exit(1);
939 struct gotadmin_list_pack_cb_args {
940 int nblobs;
941 int ntrees;
942 int ncommits;
943 int ntags;
944 int noffdeltas;
945 int nrefdeltas;
946 int human_readable;
947 };
949 static const struct got_error *
950 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
951 off_t size, off_t base_offset, struct got_object_id *base_id)
953 const struct got_error *err;
954 struct gotadmin_list_pack_cb_args *a = arg;
955 char *id_str, *delta_str = NULL, *base_id_str = NULL;
956 const char *type_str;
958 err = got_object_id_str(&id_str, id);
959 if (err)
960 return err;
962 switch (type) {
963 case GOT_OBJ_TYPE_BLOB:
964 type_str = GOT_OBJ_LABEL_BLOB;
965 a->nblobs++;
966 break;
967 case GOT_OBJ_TYPE_TREE:
968 type_str = GOT_OBJ_LABEL_TREE;
969 a->ntrees++;
970 break;
971 case GOT_OBJ_TYPE_COMMIT:
972 type_str = GOT_OBJ_LABEL_COMMIT;
973 a->ncommits++;
974 break;
975 case GOT_OBJ_TYPE_TAG:
976 type_str = GOT_OBJ_LABEL_TAG;
977 a->ntags++;
978 break;
979 case GOT_OBJ_TYPE_OFFSET_DELTA:
980 type_str = "offset-delta";
981 if (asprintf(&delta_str, " base-offset %lld",
982 (long long)base_offset) == -1) {
983 err = got_error_from_errno("asprintf");
984 goto done;
986 a->noffdeltas++;
987 break;
988 case GOT_OBJ_TYPE_REF_DELTA:
989 type_str = "ref-delta";
990 err = got_object_id_str(&base_id_str, base_id);
991 if (err)
992 goto done;
993 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
994 err = got_error_from_errno("asprintf");
995 goto done;
997 a->nrefdeltas++;
998 break;
999 default:
1000 err = got_error(GOT_ERR_OBJ_TYPE);
1001 goto done;
1003 if (a->human_readable) {
1004 char scaled[FMT_SCALED_STRSIZE];
1005 char *s;;
1006 if (fmt_scaled(size, scaled) == -1) {
1007 err = got_error_from_errno("fmt_scaled");
1008 goto done;
1010 s = scaled;
1011 while (isspace((unsigned char)*s))
1012 s++;
1013 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1014 (long long)offset, s, delta_str ? delta_str : "");
1015 } else {
1016 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1017 (long long)offset, (long long)size,
1018 delta_str ? delta_str : "");
1020 done:
1021 free(id_str);
1022 free(base_id_str);
1023 free(delta_str);
1024 return err;
1027 static const struct got_error *
1028 cmd_listpack(int argc, char *argv[])
1030 const struct got_error *error = NULL;
1031 struct got_repository *repo = NULL;
1032 int ch;
1033 struct got_object_id *pack_hash = NULL;
1034 char *packfile_path = NULL;
1035 char *id_str = NULL;
1036 struct gotadmin_list_pack_cb_args lpa;
1037 FILE *packfile = NULL;
1038 int show_stats = 0, human_readable = 0;
1039 int *pack_fds = NULL;
1041 #ifndef PROFILE
1042 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1043 NULL) == -1)
1044 err(1, "pledge");
1045 #endif
1047 while ((ch = getopt(argc, argv, "hs")) != -1) {
1048 switch (ch) {
1049 case 'h':
1050 human_readable = 1;
1051 break;
1052 case 's':
1053 show_stats = 1;
1054 break;
1055 default:
1056 usage_listpack();
1057 /* NOTREACHED */
1061 argc -= optind;
1062 argv += optind;
1064 if (argc != 1)
1065 usage_listpack();
1066 packfile_path = realpath(argv[0], NULL);
1067 if (packfile_path == NULL)
1068 return got_error_from_errno2("realpath", argv[0]);
1070 error = got_repo_pack_fds_open(&pack_fds);
1071 if (error != NULL)
1072 goto done;
1073 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1074 if (error)
1075 goto done;
1076 #ifndef PROFILE
1077 /* Remove "cpath" promise. */
1078 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1079 NULL) == -1)
1080 err(1, "pledge");
1081 #endif
1082 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1083 if (error)
1084 goto done;
1086 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1087 packfile_path);
1088 if (error)
1089 goto done;
1090 error = got_object_id_str(&id_str, pack_hash);
1091 if (error)
1092 goto done;
1094 memset(&lpa, 0, sizeof(lpa));
1095 lpa.human_readable = human_readable;
1096 error = got_repo_list_pack(packfile, pack_hash, repo,
1097 list_pack_cb, &lpa, check_cancelled, NULL);
1098 if (error)
1099 goto done;
1100 if (show_stats) {
1101 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1102 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1103 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1104 lpa.noffdeltas + lpa.nrefdeltas,
1105 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1106 lpa.noffdeltas, lpa.nrefdeltas);
1108 done:
1109 if (repo)
1110 got_repo_close(repo);
1111 if (pack_fds) {
1112 const struct got_error *pack_err =
1113 got_repo_pack_fds_close(pack_fds);
1114 if (error == NULL)
1115 error = pack_err;
1117 free(id_str);
1118 free(pack_hash);
1119 free(packfile_path);
1120 return error;
1123 __dead static void
1124 usage_cleanup(void)
1126 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1127 getprogname());
1128 exit(1);
1131 struct got_cleanup_progress_arg {
1132 int last_nloose;
1133 int last_ncommits;
1134 int last_npurged;
1135 int verbosity;
1136 int printed_something;
1137 int dry_run;
1140 static const struct got_error *
1141 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1143 struct got_cleanup_progress_arg *a = arg;
1144 int print_loose = 0, print_commits = 0, print_purged = 0;
1146 if (a->last_nloose != nloose) {
1147 print_loose = 1;
1148 a->last_nloose = nloose;
1150 if (a->last_ncommits != ncommits) {
1151 print_loose = 1;
1152 print_commits = 1;
1153 a->last_ncommits = ncommits;
1155 if (a->last_npurged != npurged) {
1156 print_loose = 1;
1157 print_commits = 1;
1158 print_purged = 1;
1159 a->last_npurged = npurged;
1162 if (a->verbosity < 0)
1163 return NULL;
1165 if (print_loose || print_commits || print_purged)
1166 printf("\r");
1167 if (print_loose)
1168 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1169 if (print_commits)
1170 printf("; %d commit%s scanned", ncommits,
1171 ncommits == 1 ? "" : "s");
1172 if (print_purged) {
1173 if (a->dry_run) {
1174 printf("; %d object%s could be purged", npurged,
1175 npurged == 1 ? "" : "s");
1176 } else {
1177 printf("; %d object%s purged", npurged,
1178 npurged == 1 ? "" : "s");
1181 if (print_loose || print_commits || print_purged) {
1182 a->printed_something = 1;
1183 fflush(stdout);
1185 return NULL;
1188 struct got_lonely_packidx_progress_arg {
1189 int verbosity;
1190 int printed_something;
1191 int dry_run;
1194 static const struct got_error *
1195 lonely_packidx_progress(void *arg, const char *path)
1197 struct got_lonely_packidx_progress_arg *a = arg;
1199 if (a->verbosity < 0)
1200 return NULL;
1202 if (a->dry_run)
1203 printf("%s could be removed\n", path);
1204 else
1205 printf("%s removed\n", path);
1207 a->printed_something = 1;
1208 return NULL;
1211 static const struct got_error *
1212 cmd_cleanup(int argc, char *argv[])
1214 const struct got_error *error = NULL;
1215 char *repo_path = NULL;
1216 struct got_repository *repo = NULL;
1217 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1218 int remove_lonely_packidx = 0, ignore_mtime = 0;
1219 struct got_cleanup_progress_arg cpa;
1220 struct got_lonely_packidx_progress_arg lpa;
1221 off_t size_before, size_after;
1222 char scaled_before[FMT_SCALED_STRSIZE];
1223 char scaled_after[FMT_SCALED_STRSIZE];
1224 char scaled_diff[FMT_SCALED_STRSIZE];
1225 int *pack_fds = NULL;
1227 #ifndef PROFILE
1228 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1229 NULL) == -1)
1230 err(1, "pledge");
1231 #endif
1233 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1234 switch (ch) {
1235 case 'a':
1236 ignore_mtime = 1;
1237 break;
1238 case 'n':
1239 dry_run = 1;
1240 break;
1241 case 'p':
1242 remove_lonely_packidx = 1;
1243 break;
1244 case 'q':
1245 verbosity = -1;
1246 break;
1247 case 'r':
1248 repo_path = realpath(optarg, NULL);
1249 if (repo_path == NULL)
1250 return got_error_from_errno2("realpath",
1251 optarg);
1252 got_path_strip_trailing_slashes(repo_path);
1253 break;
1254 default:
1255 usage_cleanup();
1256 /* NOTREACHED */
1260 argc -= optind;
1261 argv += optind;
1263 if (repo_path == NULL) {
1264 error = get_repo_path(&repo_path);
1265 if (error)
1266 goto done;
1268 error = got_repo_pack_fds_open(&pack_fds);
1269 if (error != NULL)
1270 goto done;
1271 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1272 if (error)
1273 goto done;
1275 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1276 if (error)
1277 goto done;
1279 if (got_repo_has_extension(repo, "preciousObjects")) {
1280 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1281 "the preciousObjects Git extension is enabled; "
1282 "this implies that objects must not be deleted");
1283 goto done;
1286 if (remove_lonely_packidx) {
1287 memset(&lpa, 0, sizeof(lpa));
1288 lpa.dry_run = dry_run;
1289 lpa.verbosity = verbosity;
1290 error = got_repo_remove_lonely_packidx(repo, dry_run,
1291 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1292 goto done;
1295 memset(&cpa, 0, sizeof(cpa));
1296 cpa.last_ncommits = -1;
1297 cpa.last_npurged = -1;
1298 cpa.dry_run = dry_run;
1299 cpa.verbosity = verbosity;
1300 error = got_repo_purge_unreferenced_loose_objects(repo,
1301 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1302 cleanup_progress, &cpa, check_cancelled, NULL);
1303 if (cpa.printed_something)
1304 printf("\n");
1305 if (error)
1306 goto done;
1307 if (cpa.printed_something) {
1308 if (fmt_scaled(size_before, scaled_before) == -1) {
1309 error = got_error_from_errno("fmt_scaled");
1310 goto done;
1312 if (fmt_scaled(size_after, scaled_after) == -1) {
1313 error = got_error_from_errno("fmt_scaled");
1314 goto done;
1316 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1317 error = got_error_from_errno("fmt_scaled");
1318 goto done;
1320 printf("loose total size before: %s\n", scaled_before);
1321 printf("loose total size after: %s\n", scaled_after);
1322 if (dry_run) {
1323 printf("disk space which would be freed: %s\n",
1324 scaled_diff);
1325 } else
1326 printf("disk space freed: %s\n", scaled_diff);
1327 printf("loose objects also found in pack files: %d\n", npacked);
1329 done:
1330 if (repo)
1331 got_repo_close(repo);
1332 if (pack_fds) {
1333 const struct got_error *pack_err =
1334 got_repo_pack_fds_close(pack_fds);
1335 if (error == NULL)
1336 error = pack_err;
1338 free(repo_path);
1339 return error;