Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <sha2.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <util.h>
35 #include "got_version.h"
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_cancel.h"
40 #include "got_repository.h"
41 #include "got_repository_admin.h"
42 #include "got_gotconfig.h"
43 #include "got_path.h"
44 #include "got_privsep.h"
45 #include "got_opentemp.h"
46 #include "got_worktree.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
55 static void
56 catch_sigint(int signo)
57 {
58 sigint_received = 1;
59 }
61 static void
62 catch_sigpipe(int signo)
63 {
64 sigpipe_received = 1;
65 }
67 static const struct got_error *
68 check_cancelled(void *arg)
69 {
70 if (sigint_received || sigpipe_received)
71 return got_error(GOT_ERR_CANCELLED);
72 return NULL;
73 }
75 struct gotadmin_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int, int);
83 __dead static void usage_init(void);
84 __dead static void usage_info(void);
85 __dead static void usage_pack(void);
86 __dead static void usage_indexpack(void);
87 __dead static void usage_listpack(void);
88 __dead static void usage_cleanup(void);
90 static const struct got_error* cmd_init(int, char *[]);
91 static const struct got_error* cmd_info(int, char *[]);
92 static const struct got_error* cmd_pack(int, char *[]);
93 static const struct got_error* cmd_indexpack(int, char *[]);
94 static const struct got_error* cmd_listpack(int, char *[]);
95 static const struct got_error* cmd_cleanup(int, char *[]);
97 static const struct gotadmin_cmd gotadmin_commands[] = {
98 { "init", cmd_init, usage_init, "" },
99 { "info", cmd_info, usage_info, "" },
100 { "pack", cmd_pack, usage_pack, "" },
101 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
102 { "listpack", cmd_listpack, usage_listpack, "ls" },
103 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
104 };
106 static void
107 list_commands(FILE *fp)
109 size_t i;
111 fprintf(fp, "commands:");
112 for (i = 0; i < nitems(gotadmin_commands); i++) {
113 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
114 fprintf(fp, " %s", cmd->cmd_name);
116 fputc('\n', fp);
119 int
120 main(int argc, char *argv[])
122 const struct gotadmin_cmd *cmd;
123 size_t i;
124 int ch;
125 int hflag = 0, Vflag = 0;
126 static const struct option longopts[] = {
127 { "version", no_argument, NULL, 'V' },
128 { NULL, 0, NULL, 0 }
129 };
131 setlocale(LC_CTYPE, "");
133 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
134 switch (ch) {
135 case 'h':
136 hflag = 1;
137 break;
138 case 'V':
139 Vflag = 1;
140 break;
141 default:
142 usage(hflag, 1);
143 /* NOTREACHED */
147 argc -= optind;
148 argv += optind;
149 optind = 1;
150 optreset = 1;
152 if (Vflag) {
153 got_version_print_str();
154 return 0;
157 if (argc <= 0)
158 usage(hflag, hflag ? 0 : 1);
160 signal(SIGINT, catch_sigint);
161 signal(SIGPIPE, catch_sigpipe);
163 for (i = 0; i < nitems(gotadmin_commands); i++) {
164 const struct got_error *error;
166 cmd = &gotadmin_commands[i];
168 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
169 strcmp(cmd->cmd_alias, argv[0]) != 0)
170 continue;
172 if (hflag)
173 cmd->cmd_usage();
175 error = cmd->cmd_main(argc, argv);
176 if (error && error->code != GOT_ERR_CANCELLED &&
177 error->code != GOT_ERR_PRIVSEP_EXIT &&
178 !(sigpipe_received &&
179 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
180 !(sigint_received &&
181 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
182 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
183 return 1;
186 return 0;
189 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
190 list_commands(stderr);
191 return 1;
194 __dead static void
195 usage(int hflag, int status)
197 FILE *fp = (status == 0) ? stdout : stderr;
199 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
200 getprogname());
201 if (hflag)
202 list_commands(fp);
203 exit(status);
206 static const struct got_error *
207 apply_unveil(const char *repo_path, int repo_read_only)
209 const struct got_error *err;
211 #ifdef PROFILE
212 if (unveil("gmon.out", "rwc") != 0)
213 return got_error_from_errno2("unveil", "gmon.out");
214 #endif
215 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
216 return got_error_from_errno2("unveil", repo_path);
218 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
219 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
221 err = got_privsep_unveil_exec_helpers();
222 if (err != NULL)
223 return err;
225 if (unveil(NULL, NULL) != 0)
226 return got_error_from_errno("unveil");
228 return NULL;
231 __dead static void
232 usage_info(void)
234 fprintf(stderr, "usage: %s info [-r repository-path]\n",
235 getprogname());
236 exit(1);
239 static const struct got_error *
240 get_repo_path(char **repo_path)
242 const struct got_error *err = NULL;
243 struct got_worktree *worktree = NULL;
244 char *cwd;
246 *repo_path = NULL;
248 cwd = getcwd(NULL, 0);
249 if (cwd == NULL)
250 return got_error_from_errno("getcwd");
252 err = got_worktree_open(&worktree, cwd);
253 if (err) {
254 if (err->code != GOT_ERR_NOT_WORKTREE)
255 goto done;
256 err = NULL;
259 if (worktree)
260 *repo_path = strdup(got_worktree_get_repo_path(worktree));
261 else
262 *repo_path = strdup(cwd);
263 if (*repo_path == NULL)
264 err = got_error_from_errno("strdup");
265 done:
266 if (worktree)
267 got_worktree_close(worktree);
268 free(cwd);
269 return err;
272 __dead static void
273 usage_init(void)
275 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
276 getprogname());
277 exit(1);
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 const char *head_name = NULL;
285 char *repo_path = NULL;
286 int ch;
288 while ((ch = getopt(argc, argv, "b:")) != -1) {
289 switch (ch) {
290 case 'b':
291 head_name = optarg;
292 break;
293 default:
294 usage_init();
295 /* NOTREACHED */
299 argc -= optind;
300 argv += optind;
302 #ifndef PROFILE
303 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
304 err(1, "pledge");
305 #endif
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 while ((ch = getopt(argc, argv, "r:")) != -1) {
343 switch (ch) {
344 case 'r':
345 repo_path = realpath(optarg, NULL);
346 if (repo_path == NULL)
347 return got_error_from_errno2("realpath",
348 optarg);
349 got_path_strip_trailing_slashes(repo_path);
350 break;
351 default:
352 usage_info();
353 /* NOTREACHED */
357 argc -= optind;
358 argv += optind;
360 #ifndef PROFILE
361 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
362 NULL) == -1)
363 err(1, "pledge");
364 #endif
365 if (repo_path == NULL) {
366 error = get_repo_path(&repo_path);
367 if (error)
368 goto done;
370 error = got_repo_pack_fds_open(&pack_fds);
371 if (error != NULL)
372 goto done;
373 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
374 if (error)
375 goto done;
376 #ifndef PROFILE
377 /* Remove "cpath" promise. */
378 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
379 NULL) == -1)
380 err(1, "pledge");
381 #endif
382 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
383 if (error)
384 goto done;
386 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
388 gotconfig = got_repo_get_gotconfig(repo);
389 if (gotconfig) {
390 const struct got_remote_repo *remotes;
391 int i, nremotes;
392 if (got_gotconfig_get_author(gotconfig)) {
393 printf("default author: %s\n",
394 got_gotconfig_get_author(gotconfig));
396 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
397 for (i = 0; i < nremotes; i++) {
398 const char *fetch_url = remotes[i].fetch_url;
399 const char *send_url = remotes[i].send_url;
400 if (strcmp(fetch_url, send_url) == 0) {
401 printf("remote \"%s\": %s\n", remotes[i].name,
402 remotes[i].fetch_url);
403 } else {
404 printf("remote \"%s\" (fetch): %s\n",
405 remotes[i].name, remotes[i].fetch_url);
406 printf("remote \"%s\" (send): %s\n",
407 remotes[i].name, remotes[i].send_url);
412 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
413 &packsize, repo);
414 if (error)
415 goto done;
416 printf("pack files: %d\n", npackfiles);
417 if (npackfiles > 0) {
418 if (fmt_scaled(packsize, scaled) == -1) {
419 error = got_error_from_errno("fmt_scaled");
420 goto done;
422 printf("packed objects: %d\n", npackedobj);
423 printf("packed total size: %s\n", scaled);
426 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
427 if (error)
428 goto done;
429 printf("loose objects: %d\n", nobj);
430 if (nobj > 0) {
431 if (fmt_scaled(loose_size, scaled) == -1) {
432 error = got_error_from_errno("fmt_scaled");
433 goto done;
435 printf("loose total size: %s\n", scaled);
437 done:
438 if (repo)
439 got_repo_close(repo);
440 if (pack_fds) {
441 const struct got_error *pack_err =
442 got_repo_pack_fds_close(pack_fds);
443 if (error == NULL)
444 error = pack_err;
447 free(repo_path);
448 return error;
451 __dead static void
452 usage_pack(void)
454 fprintf(stderr, "usage: %s pack [-aq] [-r repository-path] "
455 "[-x reference] [reference ...]\n", getprogname());
456 exit(1);
459 struct got_pack_progress_arg {
460 char last_scaled_size[FMT_SCALED_STRSIZE];
461 int last_ncolored;
462 int last_nfound;
463 int last_ntrees;
464 int loading_done;
465 int last_ncommits;
466 int last_nobj_total;
467 int last_p_deltify;
468 int last_p_written;
469 int last_p_indexed;
470 int last_p_resolved;
471 int verbosity;
472 int printed_something;
473 };
475 static void
476 print_load_info(int print_colored, int print_found, int print_trees,
477 int ncolored, int nfound, int ntrees)
479 if (print_colored) {
480 printf("%d commit%s colored", ncolored,
481 ncolored == 1 ? "" : "s");
483 if (print_found) {
484 printf("%s%d object%s found",
485 ncolored > 0 ? "; " : "",
486 nfound, nfound == 1 ? "" : "s");
488 if (print_trees) {
489 printf("; %d tree%s scanned", ntrees,
490 ntrees == 1 ? "" : "s");
494 static const struct got_error *
495 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
496 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
497 int nobj_written)
499 struct got_pack_progress_arg *a = arg;
500 char scaled_size[FMT_SCALED_STRSIZE];
501 int p_deltify, p_written;
502 int print_colored = 0, print_found = 0, print_trees = 0;
503 int print_searching = 0, print_total = 0;
504 int print_deltify = 0, print_written = 0;
506 if (a->verbosity < 0)
507 return NULL;
509 if (a->last_ncolored != ncolored) {
510 print_colored = 1;
511 a->last_ncolored = ncolored;
514 if (a->last_nfound != nfound) {
515 print_colored = 1;
516 print_found = 1;
517 a->last_nfound = nfound;
520 if (a->last_ntrees != ntrees) {
521 print_colored = 1;
522 print_found = 1;
523 print_trees = 1;
524 a->last_ntrees = ntrees;
527 if ((print_colored || print_found || print_trees) &&
528 !a->loading_done) {
529 printf("\r");
530 print_load_info(print_colored, print_found, print_trees,
531 ncolored, nfound, ntrees);
532 a->printed_something = 1;
533 fflush(stdout);
534 return NULL;
535 } else if (!a->loading_done) {
536 printf("\r");
537 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
538 printf("\n");
539 a->loading_done = 1;
542 if (fmt_scaled(packfile_size, scaled_size) == -1)
543 return got_error_from_errno("fmt_scaled");
545 if (a->last_ncommits != ncommits) {
546 print_searching = 1;
547 a->last_ncommits = ncommits;
550 if (a->last_nobj_total != nobj_total) {
551 print_searching = 1;
552 print_total = 1;
553 a->last_nobj_total = nobj_total;
556 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
557 strcmp(scaled_size, a->last_scaled_size)) != 0) {
558 if (strlcpy(a->last_scaled_size, scaled_size,
559 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
560 return got_error(GOT_ERR_NO_SPACE);
563 if (nobj_deltify > 0 || nobj_written > 0) {
564 if (nobj_deltify > 0) {
565 p_deltify = (nobj_deltify * 100) / nobj_total;
566 if (p_deltify != a->last_p_deltify) {
567 a->last_p_deltify = p_deltify;
568 print_searching = 1;
569 print_total = 1;
570 print_deltify = 1;
573 if (nobj_written > 0) {
574 p_written = (nobj_written * 100) / nobj_total;
575 if (p_written != a->last_p_written) {
576 a->last_p_written = p_written;
577 print_searching = 1;
578 print_total = 1;
579 print_deltify = 1;
580 print_written = 1;
585 if (print_searching || print_total || print_deltify || print_written)
586 printf("\r");
587 if (print_searching)
588 printf("packing %d reference%s", ncommits,
589 ncommits == 1 ? "" : "s");
590 if (print_total)
591 printf("; %d object%s", nobj_total,
592 nobj_total == 1 ? "" : "s");
593 if (print_deltify)
594 printf("; deltify: %d%%", p_deltify);
595 if (print_written)
596 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
597 scaled_size, p_written);
598 if (print_searching || print_total || print_deltify ||
599 print_written) {
600 a->printed_something = 1;
601 fflush(stdout);
603 return NULL;
606 static const struct got_error *
607 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
608 int nobj_indexed, int nobj_loose, int nobj_resolved)
610 struct got_pack_progress_arg *a = arg;
611 char scaled_size[FMT_SCALED_STRSIZE];
612 int p_indexed, p_resolved;
613 int print_size = 0, print_indexed = 0, print_resolved = 0;
615 if (a->verbosity < 0)
616 return NULL;
618 if (packfile_size > 0 || nobj_indexed > 0) {
619 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
620 (a->last_scaled_size[0] == '\0' ||
621 strcmp(scaled_size, a->last_scaled_size)) != 0) {
622 print_size = 1;
623 if (strlcpy(a->last_scaled_size, scaled_size,
624 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
625 return got_error(GOT_ERR_NO_SPACE);
627 if (nobj_indexed > 0) {
628 p_indexed = (nobj_indexed * 100) / nobj_total;
629 if (p_indexed != a->last_p_indexed) {
630 a->last_p_indexed = p_indexed;
631 print_indexed = 1;
632 print_size = 1;
635 if (nobj_resolved > 0) {
636 p_resolved = (nobj_resolved * 100) /
637 (nobj_total - nobj_loose);
638 if (p_resolved != a->last_p_resolved) {
639 a->last_p_resolved = p_resolved;
640 print_resolved = 1;
641 print_indexed = 1;
642 print_size = 1;
647 if (print_size || print_indexed || print_resolved)
648 printf("\r");
649 if (print_size)
650 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
651 if (print_indexed)
652 printf("; indexing %d%%", p_indexed);
653 if (print_resolved)
654 printf("; resolving deltas %d%%", p_resolved);
655 if (print_size || print_indexed || print_resolved)
656 fflush(stdout);
658 return NULL;
661 static const struct got_error *
662 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
663 const char *refname, struct got_repository *repo)
665 const struct got_error *err;
666 struct got_reference *ref;
668 *new = NULL;
670 err = got_ref_open(&ref, repo, refname, 0);
671 if (err) {
672 if (err->code != GOT_ERR_NOT_REF)
673 return err;
675 /* Treat argument as a reference prefix. */
676 err = got_ref_list(refs, repo, refname,
677 got_ref_cmp_by_name, NULL);
678 } else {
679 err = got_reflist_insert(new, refs, ref,
680 got_ref_cmp_by_name, NULL);
681 if (err || *new == NULL /* duplicate */)
682 got_ref_close(ref);
685 return err;
688 static const struct got_error *
689 cmd_pack(int argc, char *argv[])
691 const struct got_error *error = NULL;
692 char *repo_path = NULL;
693 struct got_repository *repo = NULL;
694 int ch, i, loose_obj_only = 1, verbosity = 0;
695 struct got_object_id *pack_hash = NULL;
696 char *id_str = NULL;
697 struct got_pack_progress_arg ppa;
698 FILE *packfile = NULL;
699 struct got_pathlist_head exclude_args;
700 struct got_pathlist_entry *pe;
701 struct got_reflist_head exclude_refs;
702 struct got_reflist_head include_refs;
703 struct got_reflist_entry *re, *new;
704 int *pack_fds = NULL;
706 TAILQ_INIT(&exclude_args);
707 TAILQ_INIT(&exclude_refs);
708 TAILQ_INIT(&include_refs);
710 while ((ch = getopt(argc, argv, "aqr:x:")) != -1) {
711 switch (ch) {
712 case 'a':
713 loose_obj_only = 0;
714 break;
715 case 'q':
716 verbosity = -1;
717 break;
718 case 'r':
719 repo_path = realpath(optarg, NULL);
720 if (repo_path == NULL)
721 return got_error_from_errno2("realpath",
722 optarg);
723 got_path_strip_trailing_slashes(repo_path);
724 break;
725 case 'x':
726 got_path_strip_trailing_slashes(optarg);
727 error = got_pathlist_append(&exclude_args,
728 optarg, NULL);
729 if (error)
730 return error;
731 break;
732 default:
733 usage_pack();
734 /* NOTREACHED */
738 argc -= optind;
739 argv += optind;
741 #ifndef PROFILE
742 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
743 NULL) == -1)
744 err(1, "pledge");
745 #endif
746 if (repo_path == NULL) {
747 error = get_repo_path(&repo_path);
748 if (error)
749 goto done;
751 error = got_repo_pack_fds_open(&pack_fds);
752 if (error != NULL)
753 goto done;
754 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
755 if (error)
756 goto done;
758 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
759 if (error)
760 goto done;
762 TAILQ_FOREACH(pe, &exclude_args, entry) {
763 const char *refname = pe->path;
764 error = add_ref(&new, &exclude_refs, refname, repo);
765 if (error)
766 goto done;
769 if (argc == 0) {
770 error = got_ref_list(&include_refs, repo, "",
771 got_ref_cmp_by_name, NULL);
772 if (error)
773 goto done;
774 } else {
775 for (i = 0; i < argc; i++) {
776 const char *refname;
777 got_path_strip_trailing_slashes(argv[i]);
778 refname = argv[i];
779 error = add_ref(&new, &include_refs, refname, repo);
780 if (error)
781 goto done;
785 /* Ignore references in the refs/got/ namespace. */
786 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
787 const char *refname = got_ref_get_name(re->ref);
788 if (strncmp("refs/got/", refname, 9) != 0)
789 continue;
790 TAILQ_REMOVE(&include_refs, re, entry);
791 got_ref_close(re->ref);
792 free(re);
795 memset(&ppa, 0, sizeof(ppa));
796 ppa.last_scaled_size[0] = '\0';
797 ppa.last_p_indexed = -1;
798 ppa.last_p_resolved = -1;
799 ppa.verbosity = verbosity;
801 error = got_repo_pack_objects(&packfile, &pack_hash,
802 &include_refs, &exclude_refs, repo, loose_obj_only,
803 pack_progress, &ppa, check_cancelled, NULL);
804 if (error) {
805 if (ppa.printed_something)
806 printf("\n");
807 goto done;
810 error = got_object_id_str(&id_str, pack_hash);
811 if (error)
812 goto done;
813 if (verbosity >= 0)
814 printf("\nWrote %s.pack\n", id_str);
816 error = got_repo_index_pack(packfile, pack_hash, repo,
817 pack_index_progress, &ppa, check_cancelled, NULL);
818 if (error)
819 goto done;
820 if (verbosity >= 0)
821 printf("\nIndexed %s.pack\n", id_str);
822 done:
823 if (repo)
824 got_repo_close(repo);
825 if (pack_fds) {
826 const struct got_error *pack_err =
827 got_repo_pack_fds_close(pack_fds);
828 if (error == NULL)
829 error = pack_err;
831 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
832 got_ref_list_free(&exclude_refs);
833 got_ref_list_free(&include_refs);
834 free(id_str);
835 free(pack_hash);
836 free(repo_path);
837 return error;
840 __dead static void
841 usage_indexpack(void)
843 fprintf(stderr, "usage: %s indexpack packfile-path\n",
844 getprogname());
845 exit(1);
848 static const struct got_error *
849 cmd_indexpack(int argc, char *argv[])
851 const struct got_error *error = NULL;
852 struct got_repository *repo = NULL;
853 int ch;
854 struct got_object_id *pack_hash = NULL;
855 char *packfile_path = NULL;
856 char *id_str = NULL;
857 struct got_pack_progress_arg ppa;
858 FILE *packfile = NULL;
859 int *pack_fds = NULL;
861 while ((ch = getopt(argc, argv, "")) != -1) {
862 switch (ch) {
863 default:
864 usage_indexpack();
865 /* NOTREACHED */
869 argc -= optind;
870 argv += optind;
872 if (argc != 1)
873 usage_indexpack();
875 packfile_path = realpath(argv[0], NULL);
876 if (packfile_path == NULL)
877 return got_error_from_errno2("realpath", argv[0]);
879 #ifndef PROFILE
880 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
881 NULL) == -1)
882 err(1, "pledge");
883 #endif
885 error = got_repo_pack_fds_open(&pack_fds);
886 if (error != NULL)
887 goto done;
888 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
889 if (error)
890 goto done;
892 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
893 if (error)
894 goto done;
896 memset(&ppa, 0, sizeof(ppa));
897 ppa.last_scaled_size[0] = '\0';
898 ppa.last_p_indexed = -1;
899 ppa.last_p_resolved = -1;
901 error = got_repo_find_pack(&packfile, &pack_hash, repo,
902 packfile_path);
903 if (error)
904 goto done;
906 error = got_object_id_str(&id_str, pack_hash);
907 if (error)
908 goto done;
910 error = got_repo_index_pack(packfile, pack_hash, repo,
911 pack_index_progress, &ppa, check_cancelled, NULL);
912 if (error)
913 goto done;
914 printf("\nIndexed %s.pack\n", id_str);
915 done:
916 if (repo)
917 got_repo_close(repo);
918 if (pack_fds) {
919 const struct got_error *pack_err =
920 got_repo_pack_fds_close(pack_fds);
921 if (error == NULL)
922 error = pack_err;
924 free(id_str);
925 free(pack_hash);
926 return error;
929 __dead static void
930 usage_listpack(void)
932 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
933 getprogname());
934 exit(1);
937 struct gotadmin_list_pack_cb_args {
938 int nblobs;
939 int ntrees;
940 int ncommits;
941 int ntags;
942 int noffdeltas;
943 int nrefdeltas;
944 int human_readable;
945 };
947 static const struct got_error *
948 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
949 off_t size, off_t base_offset, struct got_object_id *base_id)
951 const struct got_error *err;
952 struct gotadmin_list_pack_cb_args *a = arg;
953 char *id_str, *delta_str = NULL, *base_id_str = NULL;
954 const char *type_str;
956 err = got_object_id_str(&id_str, id);
957 if (err)
958 return err;
960 switch (type) {
961 case GOT_OBJ_TYPE_BLOB:
962 type_str = GOT_OBJ_LABEL_BLOB;
963 a->nblobs++;
964 break;
965 case GOT_OBJ_TYPE_TREE:
966 type_str = GOT_OBJ_LABEL_TREE;
967 a->ntrees++;
968 break;
969 case GOT_OBJ_TYPE_COMMIT:
970 type_str = GOT_OBJ_LABEL_COMMIT;
971 a->ncommits++;
972 break;
973 case GOT_OBJ_TYPE_TAG:
974 type_str = GOT_OBJ_LABEL_TAG;
975 a->ntags++;
976 break;
977 case GOT_OBJ_TYPE_OFFSET_DELTA:
978 type_str = "offset-delta";
979 if (asprintf(&delta_str, " base-offset %lld",
980 (long long)base_offset) == -1) {
981 err = got_error_from_errno("asprintf");
982 goto done;
984 a->noffdeltas++;
985 break;
986 case GOT_OBJ_TYPE_REF_DELTA:
987 type_str = "ref-delta";
988 err = got_object_id_str(&base_id_str, base_id);
989 if (err)
990 goto done;
991 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
992 err = got_error_from_errno("asprintf");
993 goto done;
995 a->nrefdeltas++;
996 break;
997 default:
998 err = got_error(GOT_ERR_OBJ_TYPE);
999 goto done;
1001 if (a->human_readable) {
1002 char scaled[FMT_SCALED_STRSIZE];
1003 char *s;;
1004 if (fmt_scaled(size, scaled) == -1) {
1005 err = got_error_from_errno("fmt_scaled");
1006 goto done;
1008 s = scaled;
1009 while (isspace((unsigned char)*s))
1010 s++;
1011 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1012 (long long)offset, s, delta_str ? delta_str : "");
1013 } else {
1014 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1015 (long long)offset, (long long)size,
1016 delta_str ? delta_str : "");
1018 done:
1019 free(id_str);
1020 free(base_id_str);
1021 free(delta_str);
1022 return err;
1025 static const struct got_error *
1026 cmd_listpack(int argc, char *argv[])
1028 const struct got_error *error = NULL;
1029 struct got_repository *repo = NULL;
1030 int ch;
1031 struct got_object_id *pack_hash = NULL;
1032 char *packfile_path = NULL;
1033 char *id_str = NULL;
1034 struct gotadmin_list_pack_cb_args lpa;
1035 FILE *packfile = NULL;
1036 int show_stats = 0, human_readable = 0;
1037 int *pack_fds = NULL;
1039 while ((ch = getopt(argc, argv, "hs")) != -1) {
1040 switch (ch) {
1041 case 'h':
1042 human_readable = 1;
1043 break;
1044 case 's':
1045 show_stats = 1;
1046 break;
1047 default:
1048 usage_listpack();
1049 /* NOTREACHED */
1053 argc -= optind;
1054 argv += optind;
1056 if (argc != 1)
1057 usage_listpack();
1058 packfile_path = realpath(argv[0], NULL);
1059 if (packfile_path == NULL)
1060 return got_error_from_errno2("realpath", argv[0]);
1062 #ifndef PROFILE
1063 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1064 NULL) == -1)
1065 err(1, "pledge");
1066 #endif
1067 error = got_repo_pack_fds_open(&pack_fds);
1068 if (error != NULL)
1069 goto done;
1070 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1071 if (error)
1072 goto done;
1073 #ifndef PROFILE
1074 /* Remove "cpath" promise. */
1075 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1076 NULL) == -1)
1077 err(1, "pledge");
1078 #endif
1079 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1080 if (error)
1081 goto done;
1083 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1084 packfile_path);
1085 if (error)
1086 goto done;
1087 error = got_object_id_str(&id_str, pack_hash);
1088 if (error)
1089 goto done;
1091 memset(&lpa, 0, sizeof(lpa));
1092 lpa.human_readable = human_readable;
1093 error = got_repo_list_pack(packfile, pack_hash, repo,
1094 list_pack_cb, &lpa, check_cancelled, NULL);
1095 if (error)
1096 goto done;
1097 if (show_stats) {
1098 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1099 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1100 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1101 lpa.noffdeltas + lpa.nrefdeltas,
1102 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1103 lpa.noffdeltas, lpa.nrefdeltas);
1105 done:
1106 if (repo)
1107 got_repo_close(repo);
1108 if (pack_fds) {
1109 const struct got_error *pack_err =
1110 got_repo_pack_fds_close(pack_fds);
1111 if (error == NULL)
1112 error = pack_err;
1114 free(id_str);
1115 free(pack_hash);
1116 free(packfile_path);
1117 return error;
1120 __dead static void
1121 usage_cleanup(void)
1123 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1124 getprogname());
1125 exit(1);
1128 struct got_cleanup_progress_arg {
1129 int last_nloose;
1130 int last_ncommits;
1131 int last_npurged;
1132 int verbosity;
1133 int printed_something;
1134 int dry_run;
1137 static const struct got_error *
1138 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
1140 struct got_cleanup_progress_arg *a = arg;
1141 int print_loose = 0, print_commits = 0, print_purged = 0;
1143 if (a->last_nloose != nloose) {
1144 print_loose = 1;
1145 a->last_nloose = nloose;
1147 if (a->last_ncommits != ncommits) {
1148 print_loose = 1;
1149 print_commits = 1;
1150 a->last_ncommits = ncommits;
1152 if (a->last_npurged != npurged) {
1153 print_loose = 1;
1154 print_commits = 1;
1155 print_purged = 1;
1156 a->last_npurged = npurged;
1159 if (a->verbosity < 0)
1160 return NULL;
1162 if (print_loose || print_commits || print_purged)
1163 printf("\r");
1164 if (print_loose)
1165 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1166 if (print_commits)
1167 printf("; %d commit%s scanned", ncommits,
1168 ncommits == 1 ? "" : "s");
1169 if (print_purged) {
1170 if (a->dry_run) {
1171 printf("; %d object%s could be purged", npurged,
1172 npurged == 1 ? "" : "s");
1173 } else {
1174 printf("; %d object%s purged", npurged,
1175 npurged == 1 ? "" : "s");
1178 if (print_loose || print_commits || print_purged) {
1179 a->printed_something = 1;
1180 fflush(stdout);
1182 return NULL;
1185 struct got_lonely_packidx_progress_arg {
1186 int verbosity;
1187 int printed_something;
1188 int dry_run;
1191 static const struct got_error *
1192 lonely_packidx_progress(void *arg, const char *path)
1194 struct got_lonely_packidx_progress_arg *a = arg;
1196 if (a->verbosity < 0)
1197 return NULL;
1199 if (a->dry_run)
1200 printf("%s could be removed\n", path);
1201 else
1202 printf("%s removed\n", path);
1204 a->printed_something = 1;
1205 return NULL;
1208 static const struct got_error *
1209 cmd_cleanup(int argc, char *argv[])
1211 const struct got_error *error = NULL;
1212 char *repo_path = NULL;
1213 struct got_repository *repo = NULL;
1214 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1215 int remove_lonely_packidx = 0, ignore_mtime = 0;
1216 struct got_cleanup_progress_arg cpa;
1217 struct got_lonely_packidx_progress_arg lpa;
1218 off_t size_before, size_after;
1219 char scaled_before[FMT_SCALED_STRSIZE];
1220 char scaled_after[FMT_SCALED_STRSIZE];
1221 char scaled_diff[FMT_SCALED_STRSIZE];
1222 int *pack_fds = NULL;
1224 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1225 switch (ch) {
1226 case 'a':
1227 ignore_mtime = 1;
1228 break;
1229 case 'n':
1230 dry_run = 1;
1231 break;
1232 case 'p':
1233 remove_lonely_packidx = 1;
1234 break;
1235 case 'q':
1236 verbosity = -1;
1237 break;
1238 case 'r':
1239 repo_path = realpath(optarg, NULL);
1240 if (repo_path == NULL)
1241 return got_error_from_errno2("realpath",
1242 optarg);
1243 got_path_strip_trailing_slashes(repo_path);
1244 break;
1245 default:
1246 usage_cleanup();
1247 /* NOTREACHED */
1251 argc -= optind;
1252 argv += optind;
1254 #ifndef PROFILE
1255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1256 NULL) == -1)
1257 err(1, "pledge");
1258 #endif
1259 if (repo_path == NULL) {
1260 error = get_repo_path(&repo_path);
1261 if (error)
1262 goto done;
1264 error = got_repo_pack_fds_open(&pack_fds);
1265 if (error != NULL)
1266 goto done;
1267 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1268 if (error)
1269 goto done;
1271 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1272 if (error)
1273 goto done;
1275 if (got_repo_has_extension(repo, "preciousObjects")) {
1276 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1277 "the preciousObjects Git extension is enabled; "
1278 "this implies that objects must not be deleted");
1279 goto done;
1282 if (remove_lonely_packidx) {
1283 memset(&lpa, 0, sizeof(lpa));
1284 lpa.dry_run = dry_run;
1285 lpa.verbosity = verbosity;
1286 error = got_repo_remove_lonely_packidx(repo, dry_run,
1287 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1288 goto done;
1291 memset(&cpa, 0, sizeof(cpa));
1292 cpa.last_ncommits = -1;
1293 cpa.last_npurged = -1;
1294 cpa.dry_run = dry_run;
1295 cpa.verbosity = verbosity;
1296 error = got_repo_purge_unreferenced_loose_objects(repo,
1297 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1298 cleanup_progress, &cpa, check_cancelled, NULL);
1299 if (cpa.printed_something)
1300 printf("\n");
1301 if (error)
1302 goto done;
1303 if (cpa.printed_something) {
1304 if (fmt_scaled(size_before, scaled_before) == -1) {
1305 error = got_error_from_errno("fmt_scaled");
1306 goto done;
1308 if (fmt_scaled(size_after, scaled_after) == -1) {
1309 error = got_error_from_errno("fmt_scaled");
1310 goto done;
1312 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1313 error = got_error_from_errno("fmt_scaled");
1314 goto done;
1316 printf("loose total size before: %s\n", scaled_before);
1317 printf("loose total size after: %s\n", scaled_after);
1318 if (dry_run) {
1319 printf("disk space which would be freed: %s\n",
1320 scaled_diff);
1321 } else
1322 printf("disk space freed: %s\n", scaled_diff);
1323 printf("loose objects also found in pack files: %d\n", npacked);
1325 done:
1326 if (repo)
1327 got_repo_close(repo);
1328 if (pack_fds) {
1329 const struct got_error *pack_err =
1330 got_repo_pack_fds_close(pack_fds);
1331 if (error == NULL)
1332 error = pack_err;
1334 free(repo_path);
1335 return error;