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/types.h>
19 #include <ctype.h>
20 #include <getopt.h>
21 #include <err.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "got_compat.h"
33 #include "got_version.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_cancel.h"
38 #include "got_repository.h"
39 #include "got_repository_admin.h"
40 #include "got_gotconfig.h"
41 #include "got_path.h"
42 #include "got_privsep.h"
43 #include "got_opentemp.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 #endif
49 static volatile sig_atomic_t sigint_received;
50 static volatile sig_atomic_t sigpipe_received;
52 static void
53 catch_sigint(int signo)
54 {
55 sigint_received = 1;
56 }
58 static void
59 catch_sigpipe(int signo)
60 {
61 sigpipe_received = 1;
62 }
64 static const struct got_error *
65 check_cancelled(void *arg)
66 {
67 if (sigint_received || sigpipe_received)
68 return got_error(GOT_ERR_CANCELLED);
69 return NULL;
70 }
72 struct gotadmin_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_info(void);
81 __dead static void usage_pack(void);
82 __dead static void usage_indexpack(void);
83 __dead static void usage_listpack(void);
84 __dead static void usage_cleanup(void);
86 static const struct got_error* cmd_info(int, char *[]);
87 static const struct got_error* cmd_pack(int, char *[]);
88 static const struct got_error* cmd_indexpack(int, char *[]);
89 static const struct got_error* cmd_listpack(int, char *[]);
90 static const struct got_error* cmd_cleanup(int, char *[]);
92 static struct gotadmin_cmd gotadmin_commands[] = {
93 { "info", cmd_info, usage_info, "" },
94 { "pack", cmd_pack, usage_pack, "" },
95 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
96 { "listpack", cmd_listpack, usage_listpack, "ls" },
97 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
98 };
100 static void
101 list_commands(FILE *fp)
103 size_t i;
105 fprintf(fp, "commands:");
106 for (i = 0; i < nitems(gotadmin_commands); i++) {
107 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
108 fprintf(fp, " %s", cmd->cmd_name);
110 fputc('\n', fp);
113 int
114 main(int argc, char *argv[])
116 struct gotadmin_cmd *cmd;
117 size_t i;
118 int ch;
119 int hflag = 0, Vflag = 0;
120 static struct option longopts[] = {
121 { "version", no_argument, NULL, 'V' },
122 { NULL, 0, NULL, 0 }
123 };
125 setlocale(LC_CTYPE, "");
127 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
128 switch (ch) {
129 case 'h':
130 hflag = 1;
131 break;
132 case 'V':
133 Vflag = 1;
134 break;
135 default:
136 usage(hflag, 1);
137 /* NOTREACHED */
141 argc -= optind;
142 argv += optind;
143 optind = 1;
144 optreset = 1;
146 if (Vflag) {
147 got_version_print_str();
148 return 0;
151 if (argc <= 0)
152 usage(hflag, hflag ? 0 : 1);
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(gotadmin_commands); i++) {
158 const struct got_error *error;
160 cmd = &gotadmin_commands[i];
162 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
163 strcmp(cmd->cmd_alias, argv[0]) != 0)
164 continue;
166 if (hflag)
167 gotadmin_commands[i].cmd_usage();
169 error = gotadmin_commands[i].cmd_main(argc, argv);
170 if (error && error->code != GOT_ERR_CANCELLED &&
171 error->code != GOT_ERR_PRIVSEP_EXIT &&
172 !(sigpipe_received &&
173 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
174 !(sigint_received &&
175 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
176 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
177 return 1;
180 return 0;
183 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
184 list_commands(stderr);
185 return 1;
188 __dead static void
189 usage(int hflag, int status)
191 FILE *fp = (status == 0) ? stdout : stderr;
193 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
194 getprogname());
195 if (hflag)
196 list_commands(fp);
197 exit(status);
200 static const struct got_error *
201 apply_unveil(const char *repo_path, int repo_read_only)
203 const struct got_error *err;
205 #ifdef PROFILE
206 if (unveil("gmon.out", "rwc") != 0)
207 return got_error_from_errno2("unveil", "gmon.out");
208 #endif
209 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
210 return got_error_from_errno2("unveil", repo_path);
212 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
213 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
215 err = got_privsep_unveil_exec_helpers();
216 if (err != NULL)
217 return err;
219 if (unveil(NULL, NULL) != 0)
220 return got_error_from_errno("unveil");
222 return NULL;
225 __dead static void
226 usage_info(void)
228 fprintf(stderr, "usage: %s info [-r repository-path]\n",
229 getprogname());
230 exit(1);
233 static const struct got_error *
234 cmd_info(int argc, char *argv[])
236 const struct got_error *error = NULL;
237 char *cwd = NULL, *repo_path = NULL;
238 struct got_repository *repo = NULL;
239 const struct got_gotconfig *gotconfig = NULL;
240 int ch, npackfiles, npackedobj, nobj;
241 off_t packsize, loose_size;
242 char scaled[FMT_SCALED_STRSIZE];
244 while ((ch = getopt(argc, argv, "r:")) != -1) {
245 switch (ch) {
246 case 'r':
247 repo_path = realpath(optarg, NULL);
248 if (repo_path == NULL)
249 return got_error_from_errno2("realpath",
250 optarg);
251 got_path_strip_trailing_slashes(repo_path);
252 break;
253 default:
254 usage_info();
255 /* NOTREACHED */
259 argc -= optind;
260 argv += optind;
262 #ifndef PROFILE
263 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
264 NULL) == -1)
265 err(1, "pledge");
266 #endif
267 cwd = getcwd(NULL, 0);
268 if (cwd == NULL) {
269 error = got_error_from_errno("getcwd");
270 goto done;
273 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
274 if (error)
275 goto done;
277 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
278 if (error)
279 goto done;
281 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
283 gotconfig = got_repo_get_gotconfig(repo);
284 if (gotconfig) {
285 const struct got_remote_repo *remotes;
286 int i, nremotes;
287 if (got_gotconfig_get_author(gotconfig)) {
288 printf("default author: %s\n",
289 got_gotconfig_get_author(gotconfig));
291 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
292 for (i = 0; i < nremotes; i++) {
293 const char *fetch_url = remotes[i].fetch_url;
294 const char *send_url = remotes[i].send_url;
295 if (strcmp(fetch_url, send_url) == 0) {
296 printf("remote \"%s\": %s\n", remotes[i].name,
297 remotes[i].fetch_url);
298 } else {
299 printf("remote \"%s\" (fetch): %s\n",
300 remotes[i].name, remotes[i].fetch_url);
301 printf("remote \"%s\" (send): %s\n",
302 remotes[i].name, remotes[i].send_url);
307 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
308 &packsize, repo);
309 if (error)
310 goto done;
311 printf("pack files: %d\n", npackfiles);
312 if (npackfiles > 0) {
313 if (fmt_scaled(packsize, scaled) == -1) {
314 error = got_error_from_errno("fmt_scaled");
315 goto done;
317 printf("packed objects: %d\n", npackedobj);
318 printf("packed total size: %s\n", scaled);
321 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
322 if (error)
323 goto done;
324 printf("loose objects: %d\n", nobj);
325 if (nobj > 0) {
326 if (fmt_scaled(loose_size, scaled) == -1) {
327 error = got_error_from_errno("fmt_scaled");
328 goto done;
330 printf("loose total size: %s\n", scaled);
332 done:
333 if (repo)
334 got_repo_close(repo);
335 free(cwd);
336 return error;
339 __dead static void
340 usage_pack(void)
342 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
343 "[-x reference] [reference ...]\n",
344 getprogname());
345 exit(1);
348 struct got_pack_progress_arg {
349 char last_scaled_size[FMT_SCALED_STRSIZE];
350 int last_ncommits;
351 int last_nobj_total;
352 int last_p_deltify;
353 int last_p_written;
354 int last_p_indexed;
355 int last_p_resolved;
356 int verbosity;
357 int printed_something;
358 };
360 static const struct got_error *
361 pack_progress(void *arg, off_t packfile_size, int ncommits,
362 int nobj_total, int nobj_deltify, int nobj_written)
364 struct got_pack_progress_arg *a = arg;
365 char scaled_size[FMT_SCALED_STRSIZE];
366 int p_deltify, p_written;
367 int print_searching = 0, print_total = 0;
368 int print_deltify = 0, print_written = 0;
370 if (a->verbosity < 0)
371 return NULL;
373 if (fmt_scaled(packfile_size, scaled_size) == -1)
374 return got_error_from_errno("fmt_scaled");
376 if (a->last_ncommits != ncommits) {
377 print_searching = 1;
378 a->last_ncommits = ncommits;
381 if (a->last_nobj_total != nobj_total) {
382 print_searching = 1;
383 print_total = 1;
384 a->last_nobj_total = nobj_total;
387 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
388 strcmp(scaled_size, a->last_scaled_size)) != 0) {
389 if (strlcpy(a->last_scaled_size, scaled_size,
390 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
391 return got_error(GOT_ERR_NO_SPACE);
394 if (nobj_deltify > 0 || nobj_written > 0) {
395 if (nobj_deltify > 0) {
396 p_deltify = (nobj_deltify * 100) / nobj_total;
397 if (p_deltify != a->last_p_deltify) {
398 a->last_p_deltify = p_deltify;
399 print_searching = 1;
400 print_total = 1;
401 print_deltify = 1;
404 if (nobj_written > 0) {
405 p_written = (nobj_written * 100) / nobj_total;
406 if (p_written != a->last_p_written) {
407 a->last_p_written = p_written;
408 print_searching = 1;
409 print_total = 1;
410 print_deltify = 1;
411 print_written = 1;
416 if (print_searching || print_total || print_deltify || print_written)
417 printf("\r");
418 if (print_searching)
419 printf("packing %d reference%s", ncommits,
420 ncommits == 1 ? "" : "s");
421 if (print_total)
422 printf("; %d object%s", nobj_total,
423 nobj_total == 1 ? "" : "s");
424 if (print_deltify)
425 printf("; deltify: %d%%", p_deltify);
426 if (print_written)
427 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
428 scaled_size, p_written);
429 if (print_searching || print_total || print_deltify ||
430 print_written) {
431 a->printed_something = 1;
432 fflush(stdout);
434 return NULL;
437 static const struct got_error *
438 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
439 int nobj_indexed, int nobj_loose, int nobj_resolved)
441 struct got_pack_progress_arg *a = arg;
442 char scaled_size[FMT_SCALED_STRSIZE];
443 int p_indexed, p_resolved;
444 int print_size = 0, print_indexed = 0, print_resolved = 0;
446 if (a->verbosity < 0)
447 return NULL;
449 if (packfile_size > 0 || nobj_indexed > 0) {
450 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
451 (a->last_scaled_size[0] == '\0' ||
452 strcmp(scaled_size, a->last_scaled_size)) != 0) {
453 print_size = 1;
454 if (strlcpy(a->last_scaled_size, scaled_size,
455 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
456 return got_error(GOT_ERR_NO_SPACE);
458 if (nobj_indexed > 0) {
459 p_indexed = (nobj_indexed * 100) / nobj_total;
460 if (p_indexed != a->last_p_indexed) {
461 a->last_p_indexed = p_indexed;
462 print_indexed = 1;
463 print_size = 1;
466 if (nobj_resolved > 0) {
467 p_resolved = (nobj_resolved * 100) /
468 (nobj_total - nobj_loose);
469 if (p_resolved != a->last_p_resolved) {
470 a->last_p_resolved = p_resolved;
471 print_resolved = 1;
472 print_indexed = 1;
473 print_size = 1;
478 if (print_size || print_indexed || print_resolved)
479 printf("\r");
480 if (print_size)
481 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
482 if (print_indexed)
483 printf("; indexing %d%%", p_indexed);
484 if (print_resolved)
485 printf("; resolving deltas %d%%", p_resolved);
486 if (print_size || print_indexed || print_resolved)
487 fflush(stdout);
489 return NULL;
492 static const struct got_error *
493 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
494 const char *refname, struct got_repository *repo)
496 const struct got_error *err;
497 struct got_reference *ref;
499 *new = NULL;
501 err = got_ref_open(&ref, repo, refname, 0);
502 if (err) {
503 if (err->code != GOT_ERR_NOT_REF)
504 return err;
506 /* Treat argument as a reference prefix. */
507 err = got_ref_list(refs, repo, refname,
508 got_ref_cmp_by_name, NULL);
509 } else {
510 err = got_reflist_insert(new, refs, ref,
511 got_ref_cmp_by_name, NULL);
512 if (err || *new == NULL /* duplicate */)
513 got_ref_close(ref);
516 return err;
519 static const struct got_error *
520 cmd_pack(int argc, char *argv[])
522 const struct got_error *error = NULL;
523 char *cwd = NULL, *repo_path = NULL;
524 struct got_repository *repo = NULL;
525 int ch, i, loose_obj_only = 1;
526 struct got_object_id *pack_hash = NULL;
527 char *id_str = NULL;
528 struct got_pack_progress_arg ppa;
529 FILE *packfile = NULL;
530 struct got_pathlist_head exclude_args;
531 struct got_pathlist_entry *pe;
532 struct got_reflist_head exclude_refs;
533 struct got_reflist_head include_refs;
534 struct got_reflist_entry *re, *new;
536 TAILQ_INIT(&exclude_args);
537 TAILQ_INIT(&exclude_refs);
538 TAILQ_INIT(&include_refs);
540 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
541 switch (ch) {
542 case 'a':
543 loose_obj_only = 0;
544 break;
545 case 'r':
546 repo_path = realpath(optarg, NULL);
547 if (repo_path == NULL)
548 return got_error_from_errno2("realpath",
549 optarg);
550 got_path_strip_trailing_slashes(repo_path);
551 break;
552 case 'x':
553 got_path_strip_trailing_slashes(optarg);
554 error = got_pathlist_append(&exclude_args,
555 optarg, NULL);
556 if (error)
557 return error;
558 break;
559 default:
560 usage_pack();
561 /* NOTREACHED */
565 argc -= optind;
566 argv += optind;
568 #ifndef PROFILE
569 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
570 NULL) == -1)
571 err(1, "pledge");
572 #endif
573 cwd = getcwd(NULL, 0);
574 if (cwd == NULL) {
575 error = got_error_from_errno("getcwd");
576 goto done;
579 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
580 if (error)
581 goto done;
583 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
584 if (error)
585 goto done;
587 TAILQ_FOREACH(pe, &exclude_args, entry) {
588 const char *refname = pe->path;
589 error = add_ref(&new, &exclude_refs, refname, repo);
590 if (error)
591 goto done;
595 if (argc == 0) {
596 error = got_ref_list(&include_refs, repo, "",
597 got_ref_cmp_by_name, NULL);
598 if (error)
599 goto done;
600 } else {
601 for (i = 0; i < argc; i++) {
602 const char *refname;
603 got_path_strip_trailing_slashes(argv[i]);
604 refname = argv[i];
605 error = add_ref(&new, &include_refs, refname, repo);
606 if (error)
607 goto done;
611 /* Ignore references in the refs/got/ namespace. */
612 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
613 const char *refname = got_ref_get_name(re->ref);
614 if (strncmp("refs/got/", refname, 9) != 0)
615 continue;
616 TAILQ_REMOVE(&include_refs, re, entry);
617 got_ref_close(re->ref);
618 free(re);
621 memset(&ppa, 0, sizeof(ppa));
622 ppa.last_scaled_size[0] = '\0';
623 ppa.last_p_indexed = -1;
624 ppa.last_p_resolved = -1;
626 error = got_repo_pack_objects(&packfile, &pack_hash,
627 &include_refs, &exclude_refs, repo, loose_obj_only,
628 pack_progress, &ppa, check_cancelled, NULL);
629 if (error) {
630 if (ppa.printed_something)
631 printf("\n");
632 goto done;
635 error = got_object_id_str(&id_str, pack_hash);
636 if (error)
637 goto done;
638 printf("\nWrote %s.pack\n", id_str);
640 error = got_repo_index_pack(packfile, pack_hash, repo,
641 pack_index_progress, &ppa, check_cancelled, NULL);
642 if (error)
643 goto done;
644 printf("\nIndexed %s.pack\n", id_str);
645 done:
646 if (repo)
647 got_repo_close(repo);
648 got_pathlist_free(&exclude_args);
649 got_ref_list_free(&exclude_refs);
650 got_ref_list_free(&include_refs);
651 free(id_str);
652 free(pack_hash);
653 free(cwd);
654 return error;
657 __dead static void
658 usage_indexpack(void)
660 fprintf(stderr, "usage: %s indexpack packfile-path\n",
661 getprogname());
662 exit(1);
665 static const struct got_error *
666 cmd_indexpack(int argc, char *argv[])
668 const struct got_error *error = NULL;
669 struct got_repository *repo = NULL;
670 int ch;
671 struct got_object_id *pack_hash = NULL;
672 char *packfile_path = NULL;
673 char *id_str = NULL;
674 struct got_pack_progress_arg ppa;
675 FILE *packfile = NULL;
677 while ((ch = getopt(argc, argv, "")) != -1) {
678 switch (ch) {
679 default:
680 usage_indexpack();
681 /* NOTREACHED */
685 argc -= optind;
686 argv += optind;
688 if (argc != 1)
689 usage_indexpack();
691 packfile_path = realpath(argv[0], NULL);
692 if (packfile_path == NULL)
693 return got_error_from_errno2("realpath", argv[0]);
695 #ifndef PROFILE
696 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
697 NULL) == -1)
698 err(1, "pledge");
699 #endif
701 error = got_repo_open(&repo, packfile_path, NULL);
702 if (error)
703 goto done;
705 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
706 if (error)
707 goto done;
709 memset(&ppa, 0, sizeof(ppa));
710 ppa.last_scaled_size[0] = '\0';
711 ppa.last_p_indexed = -1;
712 ppa.last_p_resolved = -1;
714 error = got_repo_find_pack(&packfile, &pack_hash, repo,
715 packfile_path);
716 if (error)
717 goto done;
719 error = got_object_id_str(&id_str, pack_hash);
720 if (error)
721 goto done;
723 error = got_repo_index_pack(packfile, pack_hash, repo,
724 pack_index_progress, &ppa, check_cancelled, NULL);
725 if (error)
726 goto done;
727 printf("\nIndexed %s.pack\n", id_str);
728 done:
729 if (repo)
730 got_repo_close(repo);
731 free(id_str);
732 free(pack_hash);
733 return error;
736 __dead static void
737 usage_listpack(void)
739 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
740 getprogname());
741 exit(1);
744 struct gotadmin_list_pack_cb_args {
745 int nblobs;
746 int ntrees;
747 int ncommits;
748 int ntags;
749 int noffdeltas;
750 int nrefdeltas;
751 int human_readable;
752 };
754 static const struct got_error *
755 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
756 off_t size, off_t base_offset, struct got_object_id *base_id)
758 const struct got_error *err;
759 struct gotadmin_list_pack_cb_args *a = arg;
760 char *id_str, *delta_str = NULL, *base_id_str = NULL;
761 const char *type_str;
763 err = got_object_id_str(&id_str, id);
764 if (err)
765 return err;
767 switch (type) {
768 case GOT_OBJ_TYPE_BLOB:
769 type_str = GOT_OBJ_LABEL_BLOB;
770 a->nblobs++;
771 break;
772 case GOT_OBJ_TYPE_TREE:
773 type_str = GOT_OBJ_LABEL_TREE;
774 a->ntrees++;
775 break;
776 case GOT_OBJ_TYPE_COMMIT:
777 type_str = GOT_OBJ_LABEL_COMMIT;
778 a->ncommits++;
779 break;
780 case GOT_OBJ_TYPE_TAG:
781 type_str = GOT_OBJ_LABEL_TAG;
782 a->ntags++;
783 break;
784 case GOT_OBJ_TYPE_OFFSET_DELTA:
785 type_str = "offset-delta";
786 if (asprintf(&delta_str, " base-offset %lld",
787 (long long)base_offset) == -1) {
788 err = got_error_from_errno("asprintf");
789 goto done;
791 a->noffdeltas++;
792 break;
793 case GOT_OBJ_TYPE_REF_DELTA:
794 type_str = "ref-delta";
795 err = got_object_id_str(&base_id_str, base_id);
796 if (err)
797 goto done;
798 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
799 err = got_error_from_errno("asprintf");
800 goto done;
802 a->nrefdeltas++;
803 break;
804 default:
805 err = got_error(GOT_ERR_OBJ_TYPE);
806 goto done;
808 if (a->human_readable) {
809 char scaled[FMT_SCALED_STRSIZE];
810 char *s;;
811 if (fmt_scaled(size, scaled) == -1) {
812 err = got_error_from_errno("fmt_scaled");
813 goto done;
815 s = scaled;
816 while (isspace((unsigned char)*s))
817 s++;
818 printf("%s %s at %lld size %s%s\n", id_str, type_str,
819 (long long)offset, s, delta_str ? delta_str : "");
820 } else {
821 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
822 (long long)offset, (long long)size,
823 delta_str ? delta_str : "");
825 done:
826 free(id_str);
827 free(base_id_str);
828 free(delta_str);
829 return err;
832 static const struct got_error *
833 cmd_listpack(int argc, char *argv[])
835 const struct got_error *error = NULL;
836 struct got_repository *repo = NULL;
837 int ch;
838 struct got_object_id *pack_hash = NULL;
839 char *packfile_path = NULL;
840 char *id_str = NULL;
841 struct gotadmin_list_pack_cb_args lpa;
842 FILE *packfile = NULL;
843 int show_stats = 0, human_readable = 0;
845 while ((ch = getopt(argc, argv, "hs")) != -1) {
846 switch (ch) {
847 case 'h':
848 human_readable = 1;
849 break;
850 case 's':
851 show_stats = 1;
852 break;
853 default:
854 usage_listpack();
855 /* NOTREACHED */
859 argc -= optind;
860 argv += optind;
862 if (argc != 1)
863 usage_listpack();
864 packfile_path = realpath(argv[0], NULL);
865 if (packfile_path == NULL)
866 return got_error_from_errno2("realpath", argv[0]);
868 #ifndef PROFILE
869 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
870 NULL) == -1)
871 err(1, "pledge");
872 #endif
873 error = got_repo_open(&repo, packfile_path, NULL);
874 if (error)
875 goto done;
877 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
878 if (error)
879 goto done;
881 error = got_repo_find_pack(&packfile, &pack_hash, repo,
882 packfile_path);
883 if (error)
884 goto done;
885 error = got_object_id_str(&id_str, pack_hash);
886 if (error)
887 goto done;
889 memset(&lpa, 0, sizeof(lpa));
890 lpa.human_readable = human_readable;
891 error = got_repo_list_pack(packfile, pack_hash, repo,
892 list_pack_cb, &lpa, check_cancelled, NULL);
893 if (error)
894 goto done;
895 if (show_stats) {
896 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
897 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
898 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
899 lpa.noffdeltas + lpa.nrefdeltas,
900 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
901 lpa.noffdeltas, lpa.nrefdeltas);
903 done:
904 if (repo)
905 got_repo_close(repo);
906 free(id_str);
907 free(pack_hash);
908 free(packfile_path);
909 return error;
912 __dead static void
913 usage_cleanup(void)
915 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
916 "[-q]\n", getprogname());
917 exit(1);
920 struct got_cleanup_progress_arg {
921 int last_nloose;
922 int last_ncommits;
923 int last_npurged;
924 int verbosity;
925 int printed_something;
926 int dry_run;
927 };
929 static const struct got_error *
930 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
932 struct got_cleanup_progress_arg *a = arg;
933 int print_loose = 0, print_commits = 0, print_purged = 0;
935 if (a->last_nloose != nloose) {
936 print_loose = 1;
937 a->last_nloose = nloose;
939 if (a->last_ncommits != ncommits) {
940 print_loose = 1;
941 print_commits = 1;
942 a->last_ncommits = ncommits;
944 if (a->last_npurged != npurged) {
945 print_loose = 1;
946 print_commits = 1;
947 print_purged = 1;
948 a->last_npurged = npurged;
951 if (a->verbosity < 0)
952 return NULL;
954 if (print_loose || print_commits || print_purged)
955 printf("\r");
956 if (print_loose)
957 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
958 if (print_commits)
959 printf("; %d commit%s scanned", ncommits,
960 ncommits == 1 ? "" : "s");
961 if (print_purged) {
962 if (a->dry_run) {
963 printf("; %d object%s could be purged", npurged,
964 npurged == 1 ? "" : "s");
965 } else {
966 printf("; %d object%s purged", npurged,
967 npurged == 1 ? "" : "s");
970 if (print_loose || print_commits || print_purged) {
971 a->printed_something = 1;
972 fflush(stdout);
974 return NULL;
977 struct got_lonely_packidx_progress_arg {
978 int verbosity;
979 int printed_something;
980 int dry_run;
981 };
983 static const struct got_error *
984 lonely_packidx_progress(void *arg, const char *path)
986 struct got_lonely_packidx_progress_arg *a = arg;
988 if (a->verbosity < 0)
989 return NULL;
991 if (a->dry_run)
992 printf("%s could be removed\n", path);
993 else
994 printf("%s removed\n", path);
996 a->printed_something = 1;
997 return NULL;
1000 static const struct got_error *
1001 cmd_cleanup(int argc, char *argv[])
1003 const struct got_error *error = NULL;
1004 char *cwd = NULL, *repo_path = NULL;
1005 struct got_repository *repo = NULL;
1006 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1007 int remove_lonely_packidx = 0, ignore_mtime = 0;
1008 struct got_cleanup_progress_arg cpa;
1009 struct got_lonely_packidx_progress_arg lpa;
1010 off_t size_before, size_after;
1011 char scaled_before[FMT_SCALED_STRSIZE];
1012 char scaled_after[FMT_SCALED_STRSIZE];
1013 char scaled_diff[FMT_SCALED_STRSIZE];
1014 char **extensions;
1015 int nextensions, i;
1017 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1018 switch (ch) {
1019 case 'a':
1020 ignore_mtime = 1;
1021 break;
1022 case 'p':
1023 remove_lonely_packidx = 1;
1024 break;
1025 case 'r':
1026 repo_path = realpath(optarg, NULL);
1027 if (repo_path == NULL)
1028 return got_error_from_errno2("realpath",
1029 optarg);
1030 got_path_strip_trailing_slashes(repo_path);
1031 break;
1032 case 'n':
1033 dry_run = 1;
1034 break;
1035 case 'q':
1036 verbosity = -1;
1037 break;
1038 default:
1039 usage_cleanup();
1040 /* NOTREACHED */
1044 argc -= optind;
1045 argv += optind;
1047 #ifndef PROFILE
1048 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1049 NULL) == -1)
1050 err(1, "pledge");
1051 #endif
1052 cwd = getcwd(NULL, 0);
1053 if (cwd == NULL) {
1054 error = got_error_from_errno("getcwd");
1055 goto done;
1058 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
1059 if (error)
1060 goto done;
1062 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1063 if (error)
1064 goto done;
1066 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1067 repo);
1068 for (i = 0; i < nextensions; i++) {
1069 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1070 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1071 "the preciousObjects Git extension is enabled; "
1072 "this implies that objects must not be deleted");
1073 goto done;
1077 if (remove_lonely_packidx) {
1078 memset(&lpa, 0, sizeof(lpa));
1079 lpa.dry_run = dry_run;
1080 lpa.verbosity = verbosity;
1081 error = got_repo_remove_lonely_packidx(repo, dry_run,
1082 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1083 goto done;
1086 memset(&cpa, 0, sizeof(cpa));
1087 cpa.last_ncommits = -1;
1088 cpa.last_npurged = -1;
1089 cpa.dry_run = dry_run;
1090 cpa.verbosity = verbosity;
1091 error = got_repo_purge_unreferenced_loose_objects(repo,
1092 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1093 cleanup_progress, &cpa, check_cancelled, NULL);
1094 if (cpa.printed_something)
1095 printf("\n");
1096 if (error)
1097 goto done;
1098 if (cpa.printed_something) {
1099 if (fmt_scaled(size_before, scaled_before) == -1) {
1100 error = got_error_from_errno("fmt_scaled");
1101 goto done;
1103 if (fmt_scaled(size_after, scaled_after) == -1) {
1104 error = got_error_from_errno("fmt_scaled");
1105 goto done;
1107 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1108 error = got_error_from_errno("fmt_scaled");
1109 goto done;
1111 printf("loose total size before: %s\n", scaled_before);
1112 printf("loose total size after: %s\n", scaled_after);
1113 if (dry_run) {
1114 printf("disk space which would be freed: %s\n",
1115 scaled_diff);
1116 } else
1117 printf("disk space freed: %s\n", scaled_diff);
1118 printf("loose objects also found in pack files: %d\n", npacked);
1120 done:
1121 if (repo)
1122 got_repo_close(repo);
1123 free(cwd);
1124 return error;