Blob


1 /*
2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/types.h>
20 #include <ctype.h>
21 #include <getopt.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <locale.h>
25 #include <inttypes.h>
26 #include <sha1.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <util.h>
34 #include "got_version.h"
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_cancel.h"
39 #include "got_repository.h"
40 #include "got_repository_admin.h"
41 #include "got_gotconfig.h"
42 #include "got_path.h"
43 #include "got_privsep.h"
44 #include "got_opentemp.h"
45 #include "got_worktree.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static volatile sig_atomic_t sigint_received;
52 static volatile sig_atomic_t sigpipe_received;
54 static void
55 catch_sigint(int signo)
56 {
57 sigint_received = 1;
58 }
60 static void
61 catch_sigpipe(int signo)
62 {
63 sigpipe_received = 1;
64 }
66 static const struct got_error *
67 check_cancelled(void *arg)
68 {
69 if (sigint_received || sigpipe_received)
70 return got_error(GOT_ERR_CANCELLED);
71 return NULL;
72 }
74 struct gotadmin_cmd {
75 const char *cmd_name;
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
79 };
81 __dead static void usage(int, int);
82 __dead static void usage_init(void);
83 __dead static void usage_info(void);
84 __dead static void usage_pack(void);
85 __dead static void usage_indexpack(void);
86 __dead static void usage_listpack(void);
87 __dead static void usage_cleanup(void);
89 static const struct got_error* cmd_init(int, char *[]);
90 static const struct got_error* cmd_info(int, char *[]);
91 static const struct got_error* cmd_pack(int, char *[]);
92 static const struct got_error* cmd_indexpack(int, char *[]);
93 static const struct got_error* cmd_listpack(int, char *[]);
94 static const struct got_error* cmd_cleanup(int, char *[]);
96 static const struct gotadmin_cmd gotadmin_commands[] = {
97 { "init", cmd_init, usage_init, "" },
98 { "info", cmd_info, usage_info, "" },
99 { "pack", cmd_pack, usage_pack, "" },
100 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
101 { "listpack", cmd_listpack, usage_listpack, "ls" },
102 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
103 };
105 static void
106 list_commands(FILE *fp)
108 size_t i;
110 fprintf(fp, "commands:");
111 for (i = 0; i < nitems(gotadmin_commands); i++) {
112 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
113 fprintf(fp, " %s", cmd->cmd_name);
115 fputc('\n', fp);
118 int
119 main(int argc, char *argv[])
121 const struct gotadmin_cmd *cmd;
122 size_t i;
123 int ch;
124 int hflag = 0, Vflag = 0;
125 static const struct option longopts[] = {
126 { "version", no_argument, NULL, 'V' },
127 { NULL, 0, NULL, 0 }
128 };
130 setlocale(LC_CTYPE, "");
132 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
133 switch (ch) {
134 case 'h':
135 hflag = 1;
136 break;
137 case 'V':
138 Vflag = 1;
139 break;
140 default:
141 usage(hflag, 1);
142 /* NOTREACHED */
146 argc -= optind;
147 argv += optind;
148 optind = 1;
149 optreset = 1;
151 if (Vflag) {
152 got_version_print_str();
153 return 0;
156 if (argc <= 0)
157 usage(hflag, hflag ? 0 : 1);
159 signal(SIGINT, catch_sigint);
160 signal(SIGPIPE, catch_sigpipe);
162 for (i = 0; i < nitems(gotadmin_commands); i++) {
163 const struct got_error *error;
165 cmd = &gotadmin_commands[i];
167 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
168 strcmp(cmd->cmd_alias, argv[0]) != 0)
169 continue;
171 if (hflag)
172 cmd->cmd_usage();
174 error = cmd->cmd_main(argc, argv);
175 if (error && error->code != GOT_ERR_CANCELLED &&
176 error->code != GOT_ERR_PRIVSEP_EXIT &&
177 !(sigpipe_received &&
178 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
179 !(sigint_received &&
180 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
182 return 1;
185 return 0;
188 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
189 list_commands(stderr);
190 return 1;
193 __dead static void
194 usage(int hflag, int status)
196 FILE *fp = (status == 0) ? stdout : stderr;
198 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
199 getprogname());
200 if (hflag)
201 list_commands(fp);
202 exit(status);
205 static const struct got_error *
206 apply_unveil(const char *repo_path, int repo_read_only)
208 const struct got_error *err;
210 #ifdef PROFILE
211 if (unveil("gmon.out", "rwc") != 0)
212 return got_error_from_errno2("unveil", "gmon.out");
213 #endif
214 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
215 return got_error_from_errno2("unveil", repo_path);
217 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
218 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
220 err = got_privsep_unveil_exec_helpers();
221 if (err != NULL)
222 return err;
224 if (unveil(NULL, NULL) != 0)
225 return got_error_from_errno("unveil");
227 return NULL;
230 __dead static void
231 usage_info(void)
233 fprintf(stderr, "usage: %s info [-r repository-path]\n",
234 getprogname());
235 exit(1);
238 static const struct got_error *
239 get_repo_path(char **repo_path)
241 const struct got_error *err = NULL;
242 struct got_worktree *worktree = NULL;
243 char *cwd;
245 *repo_path = NULL;
247 cwd = getcwd(NULL, 0);
248 if (cwd == NULL)
249 return got_error_from_errno("getcwd");
251 err = got_worktree_open(&worktree, cwd);
252 if (err) {
253 if (err->code != GOT_ERR_NOT_WORKTREE)
254 goto done;
255 err = NULL;
258 if (worktree)
259 *repo_path = strdup(got_worktree_get_repo_path(worktree));
260 else
261 *repo_path = strdup(cwd);
262 if (*repo_path == NULL)
263 err = got_error_from_errno("strdup");
264 done:
265 if (worktree)
266 got_worktree_close(worktree);
267 free(cwd);
268 return err;
271 __dead static void
272 usage_init(void)
274 fprintf(stderr, "usage: %s init [-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 while ((ch = getopt(argc, argv, "b:")) != -1) {
288 switch (ch) {
289 case 'b':
290 head_name = optarg;
291 break;
292 default:
293 usage_init();
294 /* NOTREACHED */
298 argc -= optind;
299 argv += optind;
301 #ifndef PROFILE
302 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
303 err(1, "pledge");
304 #endif
305 if (argc != 1)
306 usage_init();
308 repo_path = strdup(argv[0]);
309 if (repo_path == NULL)
310 return got_error_from_errno("strdup");
312 got_path_strip_trailing_slashes(repo_path);
314 error = got_path_mkdir(repo_path);
315 if (error &&
316 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
317 goto done;
319 error = apply_unveil(repo_path, 0);
320 if (error)
321 goto done;
323 error = got_repo_init(repo_path, head_name);
324 done:
325 free(repo_path);
326 return error;
329 static const struct got_error *
330 cmd_info(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
334 struct got_repository *repo = NULL;
335 const struct got_gotconfig *gotconfig = NULL;
336 int ch, npackfiles, npackedobj, nobj;
337 off_t packsize, loose_size;
338 char scaled[FMT_SCALED_STRSIZE];
339 int *pack_fds = NULL;
341 while ((ch = getopt(argc, argv, "r:")) != -1) {
342 switch (ch) {
343 case 'r':
344 repo_path = realpath(optarg, NULL);
345 if (repo_path == NULL)
346 return got_error_from_errno2("realpath",
347 optarg);
348 got_path_strip_trailing_slashes(repo_path);
349 break;
350 default:
351 usage_info();
352 /* NOTREACHED */
356 argc -= optind;
357 argv += optind;
359 #ifndef PROFILE
360 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
361 NULL) == -1)
362 err(1, "pledge");
363 #endif
364 if (repo_path == NULL) {
365 error = get_repo_path(&repo_path);
366 if (error)
367 goto done;
369 error = got_repo_pack_fds_open(&pack_fds);
370 if (error != NULL)
371 goto done;
372 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
373 if (error)
374 goto done;
375 #ifndef PROFILE
376 /* Remove "cpath" promise. */
377 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
378 NULL) == -1)
379 err(1, "pledge");
380 #endif
381 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
382 if (error)
383 goto done;
385 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
387 gotconfig = got_repo_get_gotconfig(repo);
388 if (gotconfig) {
389 const struct got_remote_repo *remotes;
390 int i, nremotes;
391 if (got_gotconfig_get_author(gotconfig)) {
392 printf("default author: %s\n",
393 got_gotconfig_get_author(gotconfig));
395 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
396 for (i = 0; i < nremotes; i++) {
397 const char *fetch_url = remotes[i].fetch_url;
398 const char *send_url = remotes[i].send_url;
399 if (strcmp(fetch_url, send_url) == 0) {
400 printf("remote \"%s\": %s\n", remotes[i].name,
401 remotes[i].fetch_url);
402 } else {
403 printf("remote \"%s\" (fetch): %s\n",
404 remotes[i].name, remotes[i].fetch_url);
405 printf("remote \"%s\" (send): %s\n",
406 remotes[i].name, remotes[i].send_url);
411 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
412 &packsize, repo);
413 if (error)
414 goto done;
415 printf("pack files: %d\n", npackfiles);
416 if (npackfiles > 0) {
417 if (fmt_scaled(packsize, scaled) == -1) {
418 error = got_error_from_errno("fmt_scaled");
419 goto done;
421 printf("packed objects: %d\n", npackedobj);
422 printf("packed total size: %s\n", scaled);
425 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
426 if (error)
427 goto done;
428 printf("loose objects: %d\n", nobj);
429 if (nobj > 0) {
430 if (fmt_scaled(loose_size, scaled) == -1) {
431 error = got_error_from_errno("fmt_scaled");
432 goto done;
434 printf("loose total size: %s\n", scaled);
436 done:
437 if (repo)
438 got_repo_close(repo);
439 if (pack_fds) {
440 const struct got_error *pack_err =
441 got_repo_pack_fds_close(pack_fds);
442 if (error == NULL)
443 error = pack_err;
446 free(repo_path);
447 return error;
450 __dead static void
451 usage_pack(void)
453 fprintf(stderr, "usage: %s pack [-aq] [-r repository-path] "
454 "[-x reference] [reference ...]\n", getprogname());
455 exit(1);
458 struct got_pack_progress_arg {
459 char last_scaled_size[FMT_SCALED_STRSIZE];
460 int last_ncolored;
461 int last_nfound;
462 int last_ntrees;
463 int loading_done;
464 int last_ncommits;
465 int last_nobj_total;
466 int last_p_deltify;
467 int last_p_written;
468 int last_p_indexed;
469 int last_p_resolved;
470 int verbosity;
471 int printed_something;
472 };
474 static void
475 print_load_info(int print_colored, int print_found, int print_trees,
476 int ncolored, int nfound, int ntrees)
478 if (print_colored) {
479 printf("%d commit%s colored", ncolored,
480 ncolored == 1 ? "" : "s");
482 if (print_found) {
483 printf("%s%d object%s found",
484 ncolored > 0 ? "; " : "",
485 nfound, nfound == 1 ? "" : "s");
487 if (print_trees) {
488 printf("; %d tree%s scanned", ntrees,
489 ntrees == 1 ? "" : "s");
493 static const struct got_error *
494 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
495 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
496 int nobj_written)
498 struct got_pack_progress_arg *a = arg;
499 char scaled_size[FMT_SCALED_STRSIZE];
500 int p_deltify, p_written;
501 int print_colored = 0, print_found = 0, print_trees = 0;
502 int print_searching = 0, print_total = 0;
503 int print_deltify = 0, print_written = 0;
505 if (a->verbosity < 0)
506 return NULL;
508 if (a->last_ncolored != ncolored) {
509 print_colored = 1;
510 a->last_ncolored = ncolored;
513 if (a->last_nfound != nfound) {
514 print_colored = 1;
515 print_found = 1;
516 a->last_nfound = nfound;
519 if (a->last_ntrees != ntrees) {
520 print_colored = 1;
521 print_found = 1;
522 print_trees = 1;
523 a->last_ntrees = ntrees;
526 if ((print_colored || print_found || print_trees) &&
527 !a->loading_done) {
528 printf("\r");
529 print_load_info(print_colored, print_found, print_trees,
530 ncolored, nfound, ntrees);
531 a->printed_something = 1;
532 fflush(stdout);
533 return NULL;
534 } else if (!a->loading_done) {
535 printf("\r");
536 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
537 printf("\n");
538 a->loading_done = 1;
541 if (fmt_scaled(packfile_size, scaled_size) == -1)
542 return got_error_from_errno("fmt_scaled");
544 if (a->last_ncommits != ncommits) {
545 print_searching = 1;
546 a->last_ncommits = ncommits;
549 if (a->last_nobj_total != nobj_total) {
550 print_searching = 1;
551 print_total = 1;
552 a->last_nobj_total = nobj_total;
555 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
556 strcmp(scaled_size, a->last_scaled_size)) != 0) {
557 if (strlcpy(a->last_scaled_size, scaled_size,
558 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
559 return got_error(GOT_ERR_NO_SPACE);
562 if (nobj_deltify > 0 || nobj_written > 0) {
563 if (nobj_deltify > 0) {
564 p_deltify = (nobj_deltify * 100) / nobj_total;
565 if (p_deltify != a->last_p_deltify) {
566 a->last_p_deltify = p_deltify;
567 print_searching = 1;
568 print_total = 1;
569 print_deltify = 1;
572 if (nobj_written > 0) {
573 p_written = (nobj_written * 100) / nobj_total;
574 if (p_written != a->last_p_written) {
575 a->last_p_written = p_written;
576 print_searching = 1;
577 print_total = 1;
578 print_deltify = 1;
579 print_written = 1;
584 if (print_searching || print_total || print_deltify || print_written)
585 printf("\r");
586 if (print_searching)
587 printf("packing %d reference%s", ncommits,
588 ncommits == 1 ? "" : "s");
589 if (print_total)
590 printf("; %d object%s", nobj_total,
591 nobj_total == 1 ? "" : "s");
592 if (print_deltify)
593 printf("; deltify: %d%%", p_deltify);
594 if (print_written)
595 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
596 scaled_size, p_written);
597 if (print_searching || print_total || print_deltify ||
598 print_written) {
599 a->printed_something = 1;
600 fflush(stdout);
602 return NULL;
605 static const struct got_error *
606 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
607 int nobj_indexed, int nobj_loose, int nobj_resolved)
609 struct got_pack_progress_arg *a = arg;
610 char scaled_size[FMT_SCALED_STRSIZE];
611 int p_indexed, p_resolved;
612 int print_size = 0, print_indexed = 0, print_resolved = 0;
614 if (a->verbosity < 0)
615 return NULL;
617 if (packfile_size > 0 || nobj_indexed > 0) {
618 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
619 (a->last_scaled_size[0] == '\0' ||
620 strcmp(scaled_size, a->last_scaled_size)) != 0) {
621 print_size = 1;
622 if (strlcpy(a->last_scaled_size, scaled_size,
623 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
624 return got_error(GOT_ERR_NO_SPACE);
626 if (nobj_indexed > 0) {
627 p_indexed = (nobj_indexed * 100) / nobj_total;
628 if (p_indexed != a->last_p_indexed) {
629 a->last_p_indexed = p_indexed;
630 print_indexed = 1;
631 print_size = 1;
634 if (nobj_resolved > 0) {
635 p_resolved = (nobj_resolved * 100) /
636 (nobj_total - nobj_loose);
637 if (p_resolved != a->last_p_resolved) {
638 a->last_p_resolved = p_resolved;
639 print_resolved = 1;
640 print_indexed = 1;
641 print_size = 1;
646 if (print_size || print_indexed || print_resolved)
647 printf("\r");
648 if (print_size)
649 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
650 if (print_indexed)
651 printf("; indexing %d%%", p_indexed);
652 if (print_resolved)
653 printf("; resolving deltas %d%%", p_resolved);
654 if (print_size || print_indexed || print_resolved)
655 fflush(stdout);
657 return NULL;
660 static const struct got_error *
661 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
662 const char *refname, struct got_repository *repo)
664 const struct got_error *err;
665 struct got_reference *ref;
667 *new = NULL;
669 err = got_ref_open(&ref, repo, refname, 0);
670 if (err) {
671 if (err->code != GOT_ERR_NOT_REF)
672 return err;
674 /* Treat argument as a reference prefix. */
675 err = got_ref_list(refs, repo, refname,
676 got_ref_cmp_by_name, NULL);
677 } else {
678 err = got_reflist_insert(new, refs, ref,
679 got_ref_cmp_by_name, NULL);
680 if (err || *new == NULL /* duplicate */)
681 got_ref_close(ref);
684 return err;
687 static const struct got_error *
688 cmd_pack(int argc, char *argv[])
690 const struct got_error *error = NULL;
691 char *repo_path = NULL;
692 struct got_repository *repo = NULL;
693 int ch, i, loose_obj_only = 1, verbosity = 0;
694 struct got_object_id *pack_hash = NULL;
695 char *id_str = NULL;
696 struct got_pack_progress_arg ppa;
697 FILE *packfile = NULL;
698 struct got_pathlist_head exclude_args;
699 struct got_pathlist_entry *pe;
700 struct got_reflist_head exclude_refs;
701 struct got_reflist_head include_refs;
702 struct got_reflist_entry *re, *new;
703 int *pack_fds = NULL;
705 TAILQ_INIT(&exclude_args);
706 TAILQ_INIT(&exclude_refs);
707 TAILQ_INIT(&include_refs);
709 while ((ch = getopt(argc, argv, "ar:x:q")) != -1) {
710 switch (ch) {
711 case 'a':
712 loose_obj_only = 0;
713 break;
714 case 'r':
715 repo_path = realpath(optarg, NULL);
716 if (repo_path == NULL)
717 return got_error_from_errno2("realpath",
718 optarg);
719 got_path_strip_trailing_slashes(repo_path);
720 break;
721 case 'x':
722 got_path_strip_trailing_slashes(optarg);
723 error = got_pathlist_append(&exclude_args,
724 optarg, NULL);
725 if (error)
726 return error;
727 break;
728 case 'q':
729 verbosity = -1;
730 break;
731 default:
732 usage_pack();
733 /* NOTREACHED */
737 argc -= optind;
738 argv += optind;
740 #ifndef PROFILE
741 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
742 NULL) == -1)
743 err(1, "pledge");
744 #endif
745 if (repo_path == NULL) {
746 error = get_repo_path(&repo_path);
747 if (error)
748 goto done;
750 error = got_repo_pack_fds_open(&pack_fds);
751 if (error != NULL)
752 goto done;
753 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
754 if (error)
755 goto done;
757 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
758 if (error)
759 goto done;
761 TAILQ_FOREACH(pe, &exclude_args, entry) {
762 const char *refname = pe->path;
763 error = add_ref(&new, &exclude_refs, refname, repo);
764 if (error)
765 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);
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 char **extensions;
1223 int nextensions, i;
1224 int *pack_fds = NULL;
1226 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1227 switch (ch) {
1228 case 'a':
1229 ignore_mtime = 1;
1230 break;
1231 case 'p':
1232 remove_lonely_packidx = 1;
1233 break;
1234 case 'r':
1235 repo_path = realpath(optarg, NULL);
1236 if (repo_path == NULL)
1237 return got_error_from_errno2("realpath",
1238 optarg);
1239 got_path_strip_trailing_slashes(repo_path);
1240 break;
1241 case 'n':
1242 dry_run = 1;
1243 break;
1244 case 'q':
1245 verbosity = -1;
1246 break;
1247 default:
1248 usage_cleanup();
1249 /* NOTREACHED */
1253 argc -= optind;
1254 argv += optind;
1256 #ifndef PROFILE
1257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1258 NULL) == -1)
1259 err(1, "pledge");
1260 #endif
1261 if (repo_path == NULL) {
1262 error = get_repo_path(&repo_path);
1263 if (error)
1264 goto done;
1266 error = got_repo_pack_fds_open(&pack_fds);
1267 if (error != NULL)
1268 goto done;
1269 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1270 if (error)
1271 goto done;
1273 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1274 if (error)
1275 goto done;
1277 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1278 repo);
1279 for (i = 0; i < nextensions; i++) {
1280 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1281 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1282 "the preciousObjects Git extension is enabled; "
1283 "this implies that objects must not be deleted");
1284 goto done;
1288 if (remove_lonely_packidx) {
1289 memset(&lpa, 0, sizeof(lpa));
1290 lpa.dry_run = dry_run;
1291 lpa.verbosity = verbosity;
1292 error = got_repo_remove_lonely_packidx(repo, dry_run,
1293 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1294 goto done;
1297 memset(&cpa, 0, sizeof(cpa));
1298 cpa.last_ncommits = -1;
1299 cpa.last_npurged = -1;
1300 cpa.dry_run = dry_run;
1301 cpa.verbosity = verbosity;
1302 error = got_repo_purge_unreferenced_loose_objects(repo,
1303 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1304 cleanup_progress, &cpa, check_cancelled, NULL);
1305 if (cpa.printed_something)
1306 printf("\n");
1307 if (error)
1308 goto done;
1309 if (cpa.printed_something) {
1310 if (fmt_scaled(size_before, scaled_before) == -1) {
1311 error = got_error_from_errno("fmt_scaled");
1312 goto done;
1314 if (fmt_scaled(size_after, scaled_after) == -1) {
1315 error = got_error_from_errno("fmt_scaled");
1316 goto done;
1318 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1319 error = got_error_from_errno("fmt_scaled");
1320 goto done;
1322 printf("loose total size before: %s\n", scaled_before);
1323 printf("loose total size after: %s\n", scaled_after);
1324 if (dry_run) {
1325 printf("disk space which would be freed: %s\n",
1326 scaled_diff);
1327 } else
1328 printf("disk space freed: %s\n", scaled_diff);
1329 printf("loose objects also found in pack files: %d\n", npacked);
1331 done:
1332 if (repo)
1333 got_repo_close(repo);
1334 if (pack_fds) {
1335 const struct got_error *pack_err =
1336 got_repo_pack_fds_close(pack_fds);
1337 if (error == NULL)
1338 error = pack_err;
1340 free(repo_path);
1341 return error;