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"
46 #ifndef nitems
47 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 #endif
50 static volatile sig_atomic_t sigint_received;
51 static volatile sig_atomic_t sigpipe_received;
53 static void
54 catch_sigint(int signo)
55 {
56 sigint_received = 1;
57 }
59 static void
60 catch_sigpipe(int signo)
61 {
62 sigpipe_received = 1;
63 }
65 static const struct got_error *
66 check_cancelled(void *arg)
67 {
68 if (sigint_received || sigpipe_received)
69 return got_error(GOT_ERR_CANCELLED);
70 return NULL;
71 }
73 struct gotadmin_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int, int);
81 __dead static void usage_info(void);
82 __dead static void usage_pack(void);
83 __dead static void usage_indexpack(void);
84 __dead static void usage_listpack(void);
85 __dead static void usage_cleanup(void);
87 static const struct got_error* cmd_info(int, char *[]);
88 static const struct got_error* cmd_pack(int, char *[]);
89 static const struct got_error* cmd_indexpack(int, char *[]);
90 static const struct got_error* cmd_listpack(int, char *[]);
91 static const struct got_error* cmd_cleanup(int, char *[]);
93 static struct gotadmin_cmd gotadmin_commands[] = {
94 { "info", cmd_info, usage_info, "" },
95 { "pack", cmd_pack, usage_pack, "" },
96 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
97 { "listpack", cmd_listpack, usage_listpack, "ls" },
98 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
99 };
101 static void
102 list_commands(FILE *fp)
104 size_t i;
106 fprintf(fp, "commands:");
107 for (i = 0; i < nitems(gotadmin_commands); i++) {
108 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
109 fprintf(fp, " %s", cmd->cmd_name);
111 fputc('\n', fp);
114 int
115 main(int argc, char *argv[])
117 struct gotadmin_cmd *cmd;
118 size_t i;
119 int ch;
120 int hflag = 0, Vflag = 0;
121 static struct option longopts[] = {
122 { "version", no_argument, NULL, 'V' },
123 { NULL, 0, NULL, 0 }
124 };
126 setlocale(LC_CTYPE, "");
128 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
129 switch (ch) {
130 case 'h':
131 hflag = 1;
132 break;
133 case 'V':
134 Vflag = 1;
135 break;
136 default:
137 usage(hflag, 1);
138 /* NOTREACHED */
142 argc -= optind;
143 argv += optind;
144 optind = 1;
145 optreset = 1;
147 if (Vflag) {
148 got_version_print_str();
149 return 0;
152 if (argc <= 0)
153 usage(hflag, hflag ? 0 : 1);
155 signal(SIGINT, catch_sigint);
156 signal(SIGPIPE, catch_sigpipe);
158 for (i = 0; i < nitems(gotadmin_commands); i++) {
159 const struct got_error *error;
161 cmd = &gotadmin_commands[i];
163 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
164 strcmp(cmd->cmd_alias, argv[0]) != 0)
165 continue;
167 if (hflag)
168 gotadmin_commands[i].cmd_usage();
170 error = gotadmin_commands[i].cmd_main(argc, argv);
171 if (error && error->code != GOT_ERR_CANCELLED &&
172 error->code != GOT_ERR_PRIVSEP_EXIT &&
173 !(sigpipe_received &&
174 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
175 !(sigint_received &&
176 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
177 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
178 return 1;
181 return 0;
184 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
185 list_commands(stderr);
186 return 1;
189 __dead static void
190 usage(int hflag, int status)
192 FILE *fp = (status == 0) ? stdout : stderr;
194 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
195 getprogname());
196 if (hflag)
197 list_commands(fp);
198 exit(status);
201 static const struct got_error *
202 apply_unveil(const char *repo_path, int repo_read_only)
204 const struct got_error *err;
206 #ifdef PROFILE
207 if (unveil("gmon.out", "rwc") != 0)
208 return got_error_from_errno2("unveil", "gmon.out");
209 #endif
210 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
211 return got_error_from_errno2("unveil", repo_path);
213 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
214 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
216 err = got_privsep_unveil_exec_helpers();
217 if (err != NULL)
218 return err;
220 if (unveil(NULL, NULL) != 0)
221 return got_error_from_errno("unveil");
223 return NULL;
226 __dead static void
227 usage_info(void)
229 fprintf(stderr, "usage: %s info [-r repository-path]\n",
230 getprogname());
231 exit(1);
234 static const struct got_error *
235 cmd_info(int argc, char *argv[])
237 const struct got_error *error = NULL;
238 char *cwd = NULL, *repo_path = NULL;
239 struct got_repository *repo = NULL;
240 const struct got_gotconfig *gotconfig = NULL;
241 int ch, npackfiles, npackedobj, nobj;
242 off_t packsize, loose_size;
243 char scaled[FMT_SCALED_STRSIZE];
245 while ((ch = getopt(argc, argv, "r:")) != -1) {
246 switch (ch) {
247 case 'r':
248 repo_path = realpath(optarg, NULL);
249 if (repo_path == NULL)
250 return got_error_from_errno2("realpath",
251 optarg);
252 got_path_strip_trailing_slashes(repo_path);
253 break;
254 default:
255 usage_info();
256 /* NOTREACHED */
260 argc -= optind;
261 argv += optind;
263 #ifndef PROFILE
264 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
265 NULL) == -1)
266 err(1, "pledge");
267 #endif
268 cwd = getcwd(NULL, 0);
269 if (cwd == NULL) {
270 error = got_error_from_errno("getcwd");
271 goto done;
274 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
275 if (error)
276 goto done;
278 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
279 if (error)
280 goto done;
282 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
284 gotconfig = got_repo_get_gotconfig(repo);
285 if (gotconfig) {
286 const struct got_remote_repo *remotes;
287 int i, nremotes;
288 if (got_gotconfig_get_author(gotconfig)) {
289 printf("default author: %s\n",
290 got_gotconfig_get_author(gotconfig));
292 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
293 for (i = 0; i < nremotes; i++) {
294 printf("remote \"%s\": %s\n", remotes[i].name,
295 remotes[i].url);
299 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
300 &packsize, repo);
301 if (error)
302 goto done;
303 printf("pack files: %d\n", npackfiles);
304 if (npackfiles > 0) {
305 if (fmt_scaled(packsize, scaled) == -1) {
306 error = got_error_from_errno("fmt_scaled");
307 goto done;
309 printf("packed objects: %d\n", npackedobj);
310 printf("packed total size: %s\n", scaled);
313 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
314 if (error)
315 goto done;
316 printf("loose objects: %d\n", nobj);
317 if (nobj > 0) {
318 if (fmt_scaled(loose_size, scaled) == -1) {
319 error = got_error_from_errno("fmt_scaled");
320 goto done;
322 printf("loose total size: %s\n", scaled);
324 done:
325 if (repo)
326 got_repo_close(repo);
327 free(cwd);
328 return error;
331 __dead static void
332 usage_pack(void)
334 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
335 "[-x reference] [reference ...]\n",
336 getprogname());
337 exit(1);
340 struct got_pack_progress_arg {
341 char last_scaled_size[FMT_SCALED_STRSIZE];
342 int last_ncommits;
343 int last_nobj_total;
344 int last_p_deltify;
345 int last_p_written;
346 int last_p_indexed;
347 int last_p_resolved;
348 int verbosity;
349 int printed_something;
350 };
352 static const struct got_error *
353 pack_progress(void *arg, off_t packfile_size, int ncommits,
354 int nobj_total, int nobj_deltify, int nobj_written)
356 struct got_pack_progress_arg *a = arg;
357 char scaled_size[FMT_SCALED_STRSIZE];
358 int p_deltify, p_written;
359 int print_searching = 0, print_total = 0;
360 int print_deltify = 0, print_written = 0;
362 if (a->verbosity < 0)
363 return NULL;
365 if (fmt_scaled(packfile_size, scaled_size) == -1)
366 return got_error_from_errno("fmt_scaled");
368 if (a->last_ncommits != ncommits) {
369 print_searching = 1;
370 a->last_ncommits = ncommits;
373 if (a->last_nobj_total != nobj_total) {
374 print_searching = 1;
375 print_total = 1;
376 a->last_nobj_total = nobj_total;
379 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
380 strcmp(scaled_size, a->last_scaled_size)) != 0) {
381 if (strlcpy(a->last_scaled_size, scaled_size,
382 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
383 return got_error(GOT_ERR_NO_SPACE);
386 if (nobj_deltify > 0 || nobj_written > 0) {
387 if (nobj_deltify > 0) {
388 p_deltify = (nobj_deltify * 100) / nobj_total;
389 if (p_deltify != a->last_p_deltify) {
390 a->last_p_deltify = p_deltify;
391 print_searching = 1;
392 print_total = 1;
393 print_deltify = 1;
396 if (nobj_written > 0) {
397 p_written = (nobj_written * 100) / nobj_total;
398 if (p_written != a->last_p_written) {
399 a->last_p_written = p_written;
400 print_searching = 1;
401 print_total = 1;
402 print_deltify = 1;
403 print_written = 1;
408 if (print_searching || print_total || print_deltify || print_written)
409 printf("\r");
410 if (print_searching)
411 printf("packing %d reference%s", ncommits,
412 ncommits == 1 ? "" : "s");
413 if (print_total)
414 printf("; %d object%s", nobj_total,
415 nobj_total == 1 ? "" : "s");
416 if (print_deltify)
417 printf("; deltify: %d%%", p_deltify);
418 if (print_written)
419 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
420 scaled_size, p_written);
421 if (print_searching || print_total || print_deltify ||
422 print_written) {
423 a->printed_something = 1;
424 fflush(stdout);
426 return NULL;
429 static const struct got_error *
430 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
431 int nobj_indexed, int nobj_loose, int nobj_resolved)
433 struct got_pack_progress_arg *a = arg;
434 char scaled_size[FMT_SCALED_STRSIZE];
435 int p_indexed, p_resolved;
436 int print_size = 0, print_indexed = 0, print_resolved = 0;
438 if (a->verbosity < 0)
439 return NULL;
441 if (packfile_size > 0 || nobj_indexed > 0) {
442 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
443 (a->last_scaled_size[0] == '\0' ||
444 strcmp(scaled_size, a->last_scaled_size)) != 0) {
445 print_size = 1;
446 if (strlcpy(a->last_scaled_size, scaled_size,
447 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
448 return got_error(GOT_ERR_NO_SPACE);
450 if (nobj_indexed > 0) {
451 p_indexed = (nobj_indexed * 100) / nobj_total;
452 if (p_indexed != a->last_p_indexed) {
453 a->last_p_indexed = p_indexed;
454 print_indexed = 1;
455 print_size = 1;
458 if (nobj_resolved > 0) {
459 p_resolved = (nobj_resolved * 100) /
460 (nobj_total - nobj_loose);
461 if (p_resolved != a->last_p_resolved) {
462 a->last_p_resolved = p_resolved;
463 print_resolved = 1;
464 print_indexed = 1;
465 print_size = 1;
470 if (print_size || print_indexed || print_resolved)
471 printf("\r");
472 if (print_size)
473 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
474 if (print_indexed)
475 printf("; indexing %d%%", p_indexed);
476 if (print_resolved)
477 printf("; resolving deltas %d%%", p_resolved);
478 if (print_size || print_indexed || print_resolved)
479 fflush(stdout);
481 return NULL;
484 static const struct got_error *
485 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
486 const char *refname, struct got_repository *repo)
488 const struct got_error *err;
489 struct got_reference *ref;
491 *new = NULL;
493 err = got_ref_open(&ref, repo, refname, 0);
494 if (err) {
495 if (err->code != GOT_ERR_NOT_REF)
496 return err;
498 /* Treat argument as a reference prefix. */
499 err = got_ref_list(refs, repo, refname,
500 got_ref_cmp_by_name, NULL);
501 } else {
502 err = got_reflist_insert(new, refs, ref, repo,
503 got_ref_cmp_by_name, NULL);
504 if (err || *new == NULL /* duplicate */)
505 got_ref_close(ref);
508 return err;
511 static const struct got_error *
512 cmd_pack(int argc, char *argv[])
514 const struct got_error *error = NULL;
515 char *cwd = NULL, *repo_path = NULL;
516 struct got_repository *repo = NULL;
517 int ch, i, loose_obj_only = 1;
518 struct got_object_id *pack_hash = NULL;
519 char *id_str = NULL;
520 struct got_pack_progress_arg ppa;
521 FILE *packfile = NULL;
522 struct got_pathlist_head exclude_args;
523 struct got_pathlist_entry *pe;
524 struct got_reflist_head exclude_refs;
525 struct got_reflist_head include_refs;
526 struct got_reflist_entry *re, *new;
528 TAILQ_INIT(&exclude_args);
529 TAILQ_INIT(&exclude_refs);
530 TAILQ_INIT(&include_refs);
532 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
533 switch (ch) {
534 case 'a':
535 loose_obj_only = 0;
536 break;
537 case 'r':
538 repo_path = realpath(optarg, NULL);
539 if (repo_path == NULL)
540 return got_error_from_errno2("realpath",
541 optarg);
542 got_path_strip_trailing_slashes(repo_path);
543 break;
544 case 'x':
545 got_path_strip_trailing_slashes(optarg);
546 error = got_pathlist_append(&exclude_args,
547 optarg, NULL);
548 if (error)
549 return error;
550 break;
551 default:
552 usage_pack();
553 /* NOTREACHED */
557 argc -= optind;
558 argv += optind;
560 #ifndef PROFILE
561 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
562 NULL) == -1)
563 err(1, "pledge");
564 #endif
565 cwd = getcwd(NULL, 0);
566 if (cwd == NULL) {
567 error = got_error_from_errno("getcwd");
568 goto done;
571 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
572 if (error)
573 goto done;
575 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
576 if (error)
577 goto done;
579 TAILQ_FOREACH(pe, &exclude_args, entry) {
580 const char *refname = pe->path;
581 error = add_ref(&new, &exclude_refs, refname, repo);
582 if (error)
583 goto done;
587 if (argc == 0) {
588 error = got_ref_list(&include_refs, repo, "",
589 got_ref_cmp_by_name, NULL);
590 if (error)
591 goto done;
592 } else {
593 for (i = 0; i < argc; i++) {
594 const char *refname;
595 got_path_strip_trailing_slashes(argv[i]);
596 refname = argv[i];
597 error = add_ref(&new, &include_refs, refname, repo);
598 if (error)
599 goto done;
603 /* Ignore references in the refs/got/ namespace. */
604 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
605 const char *refname = got_ref_get_name(re->ref);
606 if (strncmp("refs/got/", refname, 9) != 0)
607 continue;
608 TAILQ_REMOVE(&include_refs, re, entry);
609 got_ref_close(re->ref);
610 free(re);
613 memset(&ppa, 0, sizeof(ppa));
614 ppa.last_scaled_size[0] = '\0';
615 ppa.last_p_indexed = -1;
616 ppa.last_p_resolved = -1;
618 error = got_repo_pack_objects(&packfile, &pack_hash,
619 &include_refs, &exclude_refs, repo, loose_obj_only,
620 pack_progress, &ppa, check_cancelled, NULL);
621 if (error) {
622 if (ppa.printed_something)
623 printf("\n");
624 goto done;
627 error = got_object_id_str(&id_str, pack_hash);
628 if (error)
629 goto done;
630 printf("\nWrote %s.pack\n", id_str);
632 error = got_repo_index_pack(packfile, pack_hash, repo,
633 pack_index_progress, &ppa, check_cancelled, NULL);
634 if (error)
635 goto done;
636 printf("\nIndexed %s.pack\n", id_str);
637 done:
638 got_pathlist_free(&exclude_args);
639 got_ref_list_free(&exclude_refs);
640 got_ref_list_free(&include_refs);
641 free(id_str);
642 free(pack_hash);
643 free(cwd);
644 return error;
647 __dead static void
648 usage_indexpack(void)
650 fprintf(stderr, "usage: %s indexpack packfile-path\n",
651 getprogname());
652 exit(1);
655 static const struct got_error *
656 cmd_indexpack(int argc, char *argv[])
658 const struct got_error *error = NULL;
659 struct got_repository *repo = NULL;
660 int ch;
661 struct got_object_id *pack_hash = NULL;
662 char *packfile_path = NULL;
663 char *id_str = NULL;
664 struct got_pack_progress_arg ppa;
665 FILE *packfile = NULL;
667 while ((ch = getopt(argc, argv, "")) != -1) {
668 switch (ch) {
669 default:
670 usage_indexpack();
671 /* NOTREACHED */
675 argc -= optind;
676 argv += optind;
678 if (argc != 1)
679 usage_indexpack();
681 packfile_path = realpath(argv[0], NULL);
682 if (packfile_path == NULL)
683 return got_error_from_errno2("realpath", argv[0]);
685 #ifndef PROFILE
686 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
687 NULL) == -1)
688 err(1, "pledge");
689 #endif
691 error = got_repo_open(&repo, packfile_path, NULL);
692 if (error)
693 goto done;
695 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
696 if (error)
697 goto done;
699 memset(&ppa, 0, sizeof(ppa));
700 ppa.last_scaled_size[0] = '\0';
701 ppa.last_p_indexed = -1;
702 ppa.last_p_resolved = -1;
704 error = got_repo_find_pack(&packfile, &pack_hash, repo,
705 packfile_path);
706 if (error)
707 goto done;
709 error = got_object_id_str(&id_str, pack_hash);
710 if (error)
711 goto done;
713 error = got_repo_index_pack(packfile, pack_hash, repo,
714 pack_index_progress, &ppa, check_cancelled, NULL);
715 if (error)
716 goto done;
717 printf("\nIndexed %s.pack\n", id_str);
718 done:
719 free(id_str);
720 free(pack_hash);
721 return error;
724 __dead static void
725 usage_listpack(void)
727 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
728 getprogname());
729 exit(1);
732 struct gotadmin_list_pack_cb_args {
733 int nblobs;
734 int ntrees;
735 int ncommits;
736 int ntags;
737 int noffdeltas;
738 int nrefdeltas;
739 int human_readable;
740 };
742 static const struct got_error *
743 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
744 off_t size, off_t base_offset, struct got_object_id *base_id)
746 const struct got_error *err;
747 struct gotadmin_list_pack_cb_args *a = arg;
748 char *id_str, *delta_str = NULL, *base_id_str = NULL;
749 const char *type_str;
751 err = got_object_id_str(&id_str, id);
752 if (err)
753 return err;
755 switch (type) {
756 case GOT_OBJ_TYPE_BLOB:
757 type_str = GOT_OBJ_LABEL_BLOB;
758 a->nblobs++;
759 break;
760 case GOT_OBJ_TYPE_TREE:
761 type_str = GOT_OBJ_LABEL_TREE;
762 a->ntrees++;
763 break;
764 case GOT_OBJ_TYPE_COMMIT:
765 type_str = GOT_OBJ_LABEL_COMMIT;
766 a->ncommits++;
767 break;
768 case GOT_OBJ_TYPE_TAG:
769 type_str = GOT_OBJ_LABEL_TAG;
770 a->ntags++;
771 break;
772 case GOT_OBJ_TYPE_OFFSET_DELTA:
773 type_str = "offset-delta";
774 if (asprintf(&delta_str, " base-offset %llu",
775 base_offset) == -1) {
776 err = got_error_from_errno("asprintf");
777 goto done;
779 a->noffdeltas++;
780 break;
781 case GOT_OBJ_TYPE_REF_DELTA:
782 type_str = "ref-delta";
783 err = got_object_id_str(&base_id_str, base_id);
784 if (err)
785 goto done;
786 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
787 err = got_error_from_errno("asprintf");
788 goto done;
790 a->nrefdeltas++;
791 break;
792 default:
793 err = got_error(GOT_ERR_OBJ_TYPE);
794 goto done;
796 if (a->human_readable) {
797 char scaled[FMT_SCALED_STRSIZE];
798 char *s;;
799 if (fmt_scaled(size, scaled) == -1) {
800 err = got_error_from_errno("fmt_scaled");
801 goto done;
803 s = scaled;
804 while (isspace((unsigned char)*s))
805 s++;
806 printf("%s %s at %llu size %s%s\n", id_str, type_str, offset,
807 s, delta_str ? delta_str : "");
808 } else {
809 printf("%s %s at %llu size %llu%s\n", id_str, type_str, offset,
810 size, delta_str ? delta_str : "");
812 done:
813 free(id_str);
814 free(base_id_str);
815 free(delta_str);
816 return err;
819 static const struct got_error *
820 cmd_listpack(int argc, char *argv[])
822 const struct got_error *error = NULL;
823 struct got_repository *repo = NULL;
824 int ch;
825 struct got_object_id *pack_hash = NULL;
826 char *packfile_path = NULL;
827 char *id_str = NULL;
828 struct gotadmin_list_pack_cb_args lpa;
829 FILE *packfile = NULL;
830 int show_stats = 0, human_readable = 0;
832 while ((ch = getopt(argc, argv, "hs")) != -1) {
833 switch (ch) {
834 case 'h':
835 human_readable = 1;
836 break;
837 case 's':
838 show_stats = 1;
839 break;
840 default:
841 usage_listpack();
842 /* NOTREACHED */
846 argc -= optind;
847 argv += optind;
849 if (argc != 1)
850 usage_listpack();
851 packfile_path = realpath(argv[0], NULL);
852 if (packfile_path == NULL)
853 return got_error_from_errno2("realpath", argv[0]);
855 #ifndef PROFILE
856 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
857 NULL) == -1)
858 err(1, "pledge");
859 #endif
860 error = got_repo_open(&repo, packfile_path, NULL);
861 if (error)
862 goto done;
864 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
865 if (error)
866 goto done;
868 error = got_repo_find_pack(&packfile, &pack_hash, repo,
869 packfile_path);
870 if (error)
871 goto done;
872 error = got_object_id_str(&id_str, pack_hash);
873 if (error)
874 goto done;
876 memset(&lpa, 0, sizeof(lpa));
877 lpa.human_readable = human_readable;
878 error = got_repo_list_pack(packfile, pack_hash, repo,
879 list_pack_cb, &lpa, check_cancelled, NULL);
880 if (error)
881 goto done;
882 if (show_stats) {
883 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
884 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
885 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
886 lpa.noffdeltas + lpa.nrefdeltas,
887 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
888 lpa.noffdeltas, lpa.nrefdeltas);
890 done:
891 free(id_str);
892 free(pack_hash);
893 free(packfile_path);
894 return error;
897 __dead static void
898 usage_cleanup(void)
900 fprintf(stderr, "usage: %s cleanup [-p] [-n] [-r repository-path] "
901 "[-q]\n", getprogname());
902 exit(1);
905 struct got_cleanup_progress_arg {
906 int last_nloose;
907 int last_ncommits;
908 int last_npurged;
909 int verbosity;
910 int printed_something;
911 int dry_run;
912 };
914 static const struct got_error *
915 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
917 struct got_cleanup_progress_arg *a = arg;
918 int print_loose = 0, print_commits = 0, print_purged = 0;
920 if (a->last_nloose != nloose) {
921 print_loose = 1;
922 a->last_nloose = nloose;
924 if (a->last_ncommits != ncommits) {
925 print_loose = 1;
926 print_commits = 1;
927 a->last_ncommits = ncommits;
929 if (a->last_npurged != npurged) {
930 print_loose = 1;
931 print_commits = 1;
932 print_purged = 1;
933 a->last_npurged = npurged;
936 if (a->verbosity < 0)
937 return NULL;
939 if (print_loose || print_commits || print_purged)
940 printf("\r");
941 if (print_loose)
942 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
943 if (print_commits)
944 printf("; %d commit%s scanned", ncommits,
945 ncommits == 1 ? "" : "s");
946 if (print_purged) {
947 if (a->dry_run) {
948 printf("; %d object%s could be purged", npurged,
949 npurged == 1 ? "" : "s");
950 } else {
951 printf("; %d object%s purged", npurged,
952 npurged == 1 ? "" : "s");
955 if (print_loose || print_commits || print_purged) {
956 a->printed_something = 1;
957 fflush(stdout);
959 return NULL;
962 struct got_lonely_packidx_progress_arg {
963 int verbosity;
964 int printed_something;
965 int dry_run;
966 };
968 static const struct got_error *
969 lonely_packidx_progress(void *arg, const char *path)
971 struct got_lonely_packidx_progress_arg *a = arg;
973 if (a->verbosity < 0)
974 return NULL;
976 if (a->dry_run)
977 printf("%s could be removed\n", path);
978 else
979 printf("%s removed\n", path);
981 a->printed_something = 1;
982 return NULL;
985 static const struct got_error *
986 cmd_cleanup(int argc, char *argv[])
988 const struct got_error *error = NULL;
989 char *cwd = NULL, *repo_path = NULL;
990 struct got_repository *repo = NULL;
991 int ch, dry_run = 0, npacked = 0, verbosity = 0;
992 int remove_lonely_packidx = 0;
993 struct got_cleanup_progress_arg cpa;
994 struct got_lonely_packidx_progress_arg lpa;
995 off_t size_before, size_after;
996 char scaled_before[FMT_SCALED_STRSIZE];
997 char scaled_after[FMT_SCALED_STRSIZE];
998 char scaled_diff[FMT_SCALED_STRSIZE];
999 char **extensions;
1000 int nextensions, i;
1002 while ((ch = getopt(argc, argv, "pr:nq")) != -1) {
1003 switch (ch) {
1004 case 'p':
1005 remove_lonely_packidx = 1;
1006 break;
1007 case 'r':
1008 repo_path = realpath(optarg, NULL);
1009 if (repo_path == NULL)
1010 return got_error_from_errno2("realpath",
1011 optarg);
1012 got_path_strip_trailing_slashes(repo_path);
1013 break;
1014 case 'n':
1015 dry_run = 1;
1016 break;
1017 case 'q':
1018 verbosity = -1;
1019 break;
1020 default:
1021 usage_cleanup();
1022 /* NOTREACHED */
1026 argc -= optind;
1027 argv += optind;
1029 #ifndef PROFILE
1030 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1031 NULL) == -1)
1032 err(1, "pledge");
1033 #endif
1034 cwd = getcwd(NULL, 0);
1035 if (cwd == NULL) {
1036 error = got_error_from_errno("getcwd");
1037 goto done;
1040 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
1041 if (error)
1042 goto done;
1044 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1045 if (error)
1046 goto done;
1048 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1049 repo);
1050 for (i = 0; i < nextensions; i++) {
1051 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1052 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1053 "the preciousObjects Git extension is enabled; "
1054 "this implies that objects must not be deleted");
1055 goto done;
1059 if (remove_lonely_packidx) {
1060 memset(&lpa, 0, sizeof(lpa));
1061 lpa.dry_run = dry_run;
1062 lpa.verbosity = verbosity;
1063 error = got_repo_remove_lonely_packidx(repo, dry_run,
1064 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1065 goto done;
1068 memset(&cpa, 0, sizeof(cpa));
1069 cpa.last_ncommits = -1;
1070 cpa.last_npurged = -1;
1071 cpa.dry_run = dry_run;
1072 cpa.verbosity = verbosity;
1073 error = got_repo_purge_unreferenced_loose_objects(repo,
1074 &size_before, &size_after, &npacked, dry_run,
1075 cleanup_progress, &cpa, check_cancelled, NULL);
1076 if (cpa.printed_something)
1077 printf("\n");
1078 if (error)
1079 goto done;
1080 if (cpa.printed_something) {
1081 if (fmt_scaled(size_before, scaled_before) == -1) {
1082 error = got_error_from_errno("fmt_scaled");
1083 goto done;
1085 if (fmt_scaled(size_after, scaled_after) == -1) {
1086 error = got_error_from_errno("fmt_scaled");
1087 goto done;
1089 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1090 error = got_error_from_errno("fmt_scaled");
1091 goto done;
1093 printf("loose total size before: %s\n", scaled_before);
1094 printf("loose total size after: %s\n", scaled_after);
1095 if (dry_run) {
1096 printf("disk space which would be freed: %s\n",
1097 scaled_diff);
1098 } else
1099 printf("disk space freed: %s\n", scaled_diff);
1100 printf("loose objects also found in pack files: %d\n", npacked);
1102 done:
1103 if (repo)
1104 got_repo_close(repo);
1105 free(cwd);
1106 return error;