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_info(void);
83 __dead static void usage_pack(void);
84 __dead static void usage_indexpack(void);
85 __dead static void usage_listpack(void);
86 __dead static void usage_cleanup(void);
88 static const struct got_error* cmd_info(int, char *[]);
89 static const struct got_error* cmd_pack(int, char *[]);
90 static const struct got_error* cmd_indexpack(int, char *[]);
91 static const struct got_error* cmd_listpack(int, char *[]);
92 static const struct got_error* cmd_cleanup(int, char *[]);
94 static struct gotadmin_cmd gotadmin_commands[] = {
95 { "info", cmd_info, usage_info, "" },
96 { "pack", cmd_pack, usage_pack, "" },
97 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
98 { "listpack", cmd_listpack, usage_listpack, "ls" },
99 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
100 };
102 static void
103 list_commands(FILE *fp)
105 size_t i;
107 fprintf(fp, "commands:");
108 for (i = 0; i < nitems(gotadmin_commands); i++) {
109 struct gotadmin_cmd *cmd = &gotadmin_commands[i];
110 fprintf(fp, " %s", cmd->cmd_name);
112 fputc('\n', fp);
115 int
116 main(int argc, char *argv[])
118 struct gotadmin_cmd *cmd;
119 size_t i;
120 int ch;
121 int hflag = 0, Vflag = 0;
122 static struct option longopts[] = {
123 { "version", no_argument, NULL, 'V' },
124 { NULL, 0, NULL, 0 }
125 };
127 setlocale(LC_CTYPE, "");
129 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
130 switch (ch) {
131 case 'h':
132 hflag = 1;
133 break;
134 case 'V':
135 Vflag = 1;
136 break;
137 default:
138 usage(hflag, 1);
139 /* NOTREACHED */
143 argc -= optind;
144 argv += optind;
145 optind = 1;
146 optreset = 1;
148 if (Vflag) {
149 got_version_print_str();
150 return 0;
153 if (argc <= 0)
154 usage(hflag, hflag ? 0 : 1);
156 signal(SIGINT, catch_sigint);
157 signal(SIGPIPE, catch_sigpipe);
159 for (i = 0; i < nitems(gotadmin_commands); i++) {
160 const struct got_error *error;
162 cmd = &gotadmin_commands[i];
164 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
165 strcmp(cmd->cmd_alias, argv[0]) != 0)
166 continue;
168 if (hflag)
169 gotadmin_commands[i].cmd_usage();
171 error = gotadmin_commands[i].cmd_main(argc, argv);
172 if (error && error->code != GOT_ERR_CANCELLED &&
173 error->code != GOT_ERR_PRIVSEP_EXIT &&
174 !(sigpipe_received &&
175 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
176 !(sigint_received &&
177 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
178 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
179 return 1;
182 return 0;
185 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
186 list_commands(stderr);
187 return 1;
190 __dead static void
191 usage(int hflag, int status)
193 FILE *fp = (status == 0) ? stdout : stderr;
195 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
196 getprogname());
197 if (hflag)
198 list_commands(fp);
199 exit(status);
202 static const struct got_error *
203 apply_unveil(const char *repo_path, int repo_read_only)
205 const struct got_error *err;
207 #ifdef PROFILE
208 if (unveil("gmon.out", "rwc") != 0)
209 return got_error_from_errno2("unveil", "gmon.out");
210 #endif
211 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
212 return got_error_from_errno2("unveil", repo_path);
214 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
215 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
217 err = got_privsep_unveil_exec_helpers();
218 if (err != NULL)
219 return err;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno("unveil");
224 return NULL;
227 __dead static void
228 usage_info(void)
230 fprintf(stderr, "usage: %s info [-r repository-path]\n",
231 getprogname());
232 exit(1);
235 static const struct got_error *
236 get_repo_path(char **repo_path)
238 const struct got_error *err = NULL;
239 struct got_worktree *worktree = NULL;
240 char *cwd;
242 *repo_path = NULL;
244 cwd = getcwd(NULL, 0);
245 if (cwd == NULL)
246 return got_error_from_errno("getcwd");
248 err = got_worktree_open(&worktree, cwd);
249 if (err) {
250 if (err->code != GOT_ERR_NOT_WORKTREE)
251 goto done;
252 err = NULL;
255 if (worktree)
256 *repo_path = strdup(got_worktree_get_repo_path(worktree));
257 else
258 *repo_path = strdup(cwd);
259 if (*repo_path == NULL)
260 err = got_error_from_errno("strdup");
261 done:
262 if (worktree)
263 got_worktree_close(worktree);
264 free(cwd);
265 return err;
268 static const struct got_error *
269 cmd_info(int argc, char *argv[])
271 const struct got_error *error = NULL;
272 char *repo_path = NULL;
273 struct got_repository *repo = NULL;
274 const struct got_gotconfig *gotconfig = NULL;
275 int ch, npackfiles, npackedobj, nobj;
276 off_t packsize, loose_size;
277 char scaled[FMT_SCALED_STRSIZE];
279 while ((ch = getopt(argc, argv, "r:")) != -1) {
280 switch (ch) {
281 case 'r':
282 repo_path = realpath(optarg, NULL);
283 if (repo_path == NULL)
284 return got_error_from_errno2("realpath",
285 optarg);
286 got_path_strip_trailing_slashes(repo_path);
287 break;
288 default:
289 usage_info();
290 /* NOTREACHED */
294 argc -= optind;
295 argv += optind;
297 #ifndef PROFILE
298 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
299 NULL) == -1)
300 err(1, "pledge");
301 #endif
302 if (repo_path == NULL) {
303 error = get_repo_path(&repo_path);
304 if (error)
305 goto done;
307 error = got_repo_open(&repo, repo_path, NULL);
308 if (error)
309 goto done;
311 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
312 if (error)
313 goto done;
315 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
317 gotconfig = got_repo_get_gotconfig(repo);
318 if (gotconfig) {
319 const struct got_remote_repo *remotes;
320 int i, nremotes;
321 if (got_gotconfig_get_author(gotconfig)) {
322 printf("default author: %s\n",
323 got_gotconfig_get_author(gotconfig));
325 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
326 for (i = 0; i < nremotes; i++) {
327 const char *fetch_url = remotes[i].fetch_url;
328 const char *send_url = remotes[i].send_url;
329 if (strcmp(fetch_url, send_url) == 0) {
330 printf("remote \"%s\": %s\n", remotes[i].name,
331 remotes[i].fetch_url);
332 } else {
333 printf("remote \"%s\" (fetch): %s\n",
334 remotes[i].name, remotes[i].fetch_url);
335 printf("remote \"%s\" (send): %s\n",
336 remotes[i].name, remotes[i].send_url);
341 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
342 &packsize, repo);
343 if (error)
344 goto done;
345 printf("pack files: %d\n", npackfiles);
346 if (npackfiles > 0) {
347 if (fmt_scaled(packsize, scaled) == -1) {
348 error = got_error_from_errno("fmt_scaled");
349 goto done;
351 printf("packed objects: %d\n", npackedobj);
352 printf("packed total size: %s\n", scaled);
355 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
356 if (error)
357 goto done;
358 printf("loose objects: %d\n", nobj);
359 if (nobj > 0) {
360 if (fmt_scaled(loose_size, scaled) == -1) {
361 error = got_error_from_errno("fmt_scaled");
362 goto done;
364 printf("loose total size: %s\n", scaled);
366 done:
367 if (repo)
368 got_repo_close(repo);
369 free(repo_path);
370 return error;
373 __dead static void
374 usage_pack(void)
376 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
377 "[-x reference] [reference ...]\n",
378 getprogname());
379 exit(1);
382 struct got_pack_progress_arg {
383 char last_scaled_size[FMT_SCALED_STRSIZE];
384 int last_ncommits;
385 int last_nobj_total;
386 int last_p_deltify;
387 int last_p_written;
388 int last_p_indexed;
389 int last_p_resolved;
390 int verbosity;
391 int printed_something;
392 };
394 static const struct got_error *
395 pack_progress(void *arg, off_t packfile_size, int ncommits,
396 int nobj_total, int nobj_deltify, int nobj_written)
398 struct got_pack_progress_arg *a = arg;
399 char scaled_size[FMT_SCALED_STRSIZE];
400 int p_deltify, p_written;
401 int print_searching = 0, print_total = 0;
402 int print_deltify = 0, print_written = 0;
404 if (a->verbosity < 0)
405 return NULL;
407 if (fmt_scaled(packfile_size, scaled_size) == -1)
408 return got_error_from_errno("fmt_scaled");
410 if (a->last_ncommits != ncommits) {
411 print_searching = 1;
412 a->last_ncommits = ncommits;
415 if (a->last_nobj_total != nobj_total) {
416 print_searching = 1;
417 print_total = 1;
418 a->last_nobj_total = nobj_total;
421 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
422 strcmp(scaled_size, a->last_scaled_size)) != 0) {
423 if (strlcpy(a->last_scaled_size, scaled_size,
424 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
425 return got_error(GOT_ERR_NO_SPACE);
428 if (nobj_deltify > 0 || nobj_written > 0) {
429 if (nobj_deltify > 0) {
430 p_deltify = (nobj_deltify * 100) / nobj_total;
431 if (p_deltify != a->last_p_deltify) {
432 a->last_p_deltify = p_deltify;
433 print_searching = 1;
434 print_total = 1;
435 print_deltify = 1;
438 if (nobj_written > 0) {
439 p_written = (nobj_written * 100) / nobj_total;
440 if (p_written != a->last_p_written) {
441 a->last_p_written = p_written;
442 print_searching = 1;
443 print_total = 1;
444 print_deltify = 1;
445 print_written = 1;
450 if (print_searching || print_total || print_deltify || print_written)
451 printf("\r");
452 if (print_searching)
453 printf("packing %d reference%s", ncommits,
454 ncommits == 1 ? "" : "s");
455 if (print_total)
456 printf("; %d object%s", nobj_total,
457 nobj_total == 1 ? "" : "s");
458 if (print_deltify)
459 printf("; deltify: %d%%", p_deltify);
460 if (print_written)
461 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
462 scaled_size, p_written);
463 if (print_searching || print_total || print_deltify ||
464 print_written) {
465 a->printed_something = 1;
466 fflush(stdout);
468 return NULL;
471 static const struct got_error *
472 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
473 int nobj_indexed, int nobj_loose, int nobj_resolved)
475 struct got_pack_progress_arg *a = arg;
476 char scaled_size[FMT_SCALED_STRSIZE];
477 int p_indexed, p_resolved;
478 int print_size = 0, print_indexed = 0, print_resolved = 0;
480 if (a->verbosity < 0)
481 return NULL;
483 if (packfile_size > 0 || nobj_indexed > 0) {
484 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
485 (a->last_scaled_size[0] == '\0' ||
486 strcmp(scaled_size, a->last_scaled_size)) != 0) {
487 print_size = 1;
488 if (strlcpy(a->last_scaled_size, scaled_size,
489 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
490 return got_error(GOT_ERR_NO_SPACE);
492 if (nobj_indexed > 0) {
493 p_indexed = (nobj_indexed * 100) / nobj_total;
494 if (p_indexed != a->last_p_indexed) {
495 a->last_p_indexed = p_indexed;
496 print_indexed = 1;
497 print_size = 1;
500 if (nobj_resolved > 0) {
501 p_resolved = (nobj_resolved * 100) /
502 (nobj_total - nobj_loose);
503 if (p_resolved != a->last_p_resolved) {
504 a->last_p_resolved = p_resolved;
505 print_resolved = 1;
506 print_indexed = 1;
507 print_size = 1;
512 if (print_size || print_indexed || print_resolved)
513 printf("\r");
514 if (print_size)
515 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
516 if (print_indexed)
517 printf("; indexing %d%%", p_indexed);
518 if (print_resolved)
519 printf("; resolving deltas %d%%", p_resolved);
520 if (print_size || print_indexed || print_resolved)
521 fflush(stdout);
523 return NULL;
526 static const struct got_error *
527 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
528 const char *refname, struct got_repository *repo)
530 const struct got_error *err;
531 struct got_reference *ref;
533 *new = NULL;
535 err = got_ref_open(&ref, repo, refname, 0);
536 if (err) {
537 if (err->code != GOT_ERR_NOT_REF)
538 return err;
540 /* Treat argument as a reference prefix. */
541 err = got_ref_list(refs, repo, refname,
542 got_ref_cmp_by_name, NULL);
543 } else {
544 err = got_reflist_insert(new, refs, ref,
545 got_ref_cmp_by_name, NULL);
546 if (err || *new == NULL /* duplicate */)
547 got_ref_close(ref);
550 return err;
553 static const struct got_error *
554 cmd_pack(int argc, char *argv[])
556 const struct got_error *error = NULL;
557 char *repo_path = NULL;
558 struct got_repository *repo = NULL;
559 int ch, i, loose_obj_only = 1;
560 struct got_object_id *pack_hash = NULL;
561 char *id_str = NULL;
562 struct got_pack_progress_arg ppa;
563 FILE *packfile = NULL;
564 struct got_pathlist_head exclude_args;
565 struct got_pathlist_entry *pe;
566 struct got_reflist_head exclude_refs;
567 struct got_reflist_head include_refs;
568 struct got_reflist_entry *re, *new;
570 TAILQ_INIT(&exclude_args);
571 TAILQ_INIT(&exclude_refs);
572 TAILQ_INIT(&include_refs);
574 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
575 switch (ch) {
576 case 'a':
577 loose_obj_only = 0;
578 break;
579 case 'r':
580 repo_path = realpath(optarg, NULL);
581 if (repo_path == NULL)
582 return got_error_from_errno2("realpath",
583 optarg);
584 got_path_strip_trailing_slashes(repo_path);
585 break;
586 case 'x':
587 got_path_strip_trailing_slashes(optarg);
588 error = got_pathlist_append(&exclude_args,
589 optarg, NULL);
590 if (error)
591 return error;
592 break;
593 default:
594 usage_pack();
595 /* NOTREACHED */
599 argc -= optind;
600 argv += optind;
602 #ifndef PROFILE
603 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
604 NULL) == -1)
605 err(1, "pledge");
606 #endif
607 if (repo_path == NULL) {
608 error = get_repo_path(&repo_path);
609 if (error)
610 goto done;
612 error = got_repo_open(&repo, repo_path, NULL);
613 if (error)
614 goto done;
616 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
617 if (error)
618 goto done;
620 TAILQ_FOREACH(pe, &exclude_args, entry) {
621 const char *refname = pe->path;
622 error = add_ref(&new, &exclude_refs, refname, repo);
623 if (error)
624 goto done;
628 if (argc == 0) {
629 error = got_ref_list(&include_refs, repo, "",
630 got_ref_cmp_by_name, NULL);
631 if (error)
632 goto done;
633 } else {
634 for (i = 0; i < argc; i++) {
635 const char *refname;
636 got_path_strip_trailing_slashes(argv[i]);
637 refname = argv[i];
638 error = add_ref(&new, &include_refs, refname, repo);
639 if (error)
640 goto done;
644 /* Ignore references in the refs/got/ namespace. */
645 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
646 const char *refname = got_ref_get_name(re->ref);
647 if (strncmp("refs/got/", refname, 9) != 0)
648 continue;
649 TAILQ_REMOVE(&include_refs, re, entry);
650 got_ref_close(re->ref);
651 free(re);
654 memset(&ppa, 0, sizeof(ppa));
655 ppa.last_scaled_size[0] = '\0';
656 ppa.last_p_indexed = -1;
657 ppa.last_p_resolved = -1;
659 error = got_repo_pack_objects(&packfile, &pack_hash,
660 &include_refs, &exclude_refs, repo, loose_obj_only,
661 pack_progress, &ppa, check_cancelled, NULL);
662 if (error) {
663 if (ppa.printed_something)
664 printf("\n");
665 goto done;
668 error = got_object_id_str(&id_str, pack_hash);
669 if (error)
670 goto done;
671 printf("\nWrote %s.pack\n", id_str);
673 error = got_repo_index_pack(packfile, pack_hash, repo,
674 pack_index_progress, &ppa, check_cancelled, NULL);
675 if (error)
676 goto done;
677 printf("\nIndexed %s.pack\n", id_str);
678 done:
679 if (repo)
680 got_repo_close(repo);
681 got_pathlist_free(&exclude_args);
682 got_ref_list_free(&exclude_refs);
683 got_ref_list_free(&include_refs);
684 free(id_str);
685 free(pack_hash);
686 free(repo_path);
687 return error;
690 __dead static void
691 usage_indexpack(void)
693 fprintf(stderr, "usage: %s indexpack packfile-path\n",
694 getprogname());
695 exit(1);
698 static const struct got_error *
699 cmd_indexpack(int argc, char *argv[])
701 const struct got_error *error = NULL;
702 struct got_repository *repo = NULL;
703 int ch;
704 struct got_object_id *pack_hash = NULL;
705 char *packfile_path = NULL;
706 char *id_str = NULL;
707 struct got_pack_progress_arg ppa;
708 FILE *packfile = NULL;
710 while ((ch = getopt(argc, argv, "")) != -1) {
711 switch (ch) {
712 default:
713 usage_indexpack();
714 /* NOTREACHED */
718 argc -= optind;
719 argv += optind;
721 if (argc != 1)
722 usage_indexpack();
724 packfile_path = realpath(argv[0], NULL);
725 if (packfile_path == NULL)
726 return got_error_from_errno2("realpath", argv[0]);
728 #ifndef PROFILE
729 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
730 NULL) == -1)
731 err(1, "pledge");
732 #endif
734 error = got_repo_open(&repo, packfile_path, NULL);
735 if (error)
736 goto done;
738 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
739 if (error)
740 goto done;
742 memset(&ppa, 0, sizeof(ppa));
743 ppa.last_scaled_size[0] = '\0';
744 ppa.last_p_indexed = -1;
745 ppa.last_p_resolved = -1;
747 error = got_repo_find_pack(&packfile, &pack_hash, repo,
748 packfile_path);
749 if (error)
750 goto done;
752 error = got_object_id_str(&id_str, pack_hash);
753 if (error)
754 goto done;
756 error = got_repo_index_pack(packfile, pack_hash, repo,
757 pack_index_progress, &ppa, check_cancelled, NULL);
758 if (error)
759 goto done;
760 printf("\nIndexed %s.pack\n", id_str);
761 done:
762 if (repo)
763 got_repo_close(repo);
764 free(id_str);
765 free(pack_hash);
766 return error;
769 __dead static void
770 usage_listpack(void)
772 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
773 getprogname());
774 exit(1);
777 struct gotadmin_list_pack_cb_args {
778 int nblobs;
779 int ntrees;
780 int ncommits;
781 int ntags;
782 int noffdeltas;
783 int nrefdeltas;
784 int human_readable;
785 };
787 static const struct got_error *
788 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
789 off_t size, off_t base_offset, struct got_object_id *base_id)
791 const struct got_error *err;
792 struct gotadmin_list_pack_cb_args *a = arg;
793 char *id_str, *delta_str = NULL, *base_id_str = NULL;
794 const char *type_str;
796 err = got_object_id_str(&id_str, id);
797 if (err)
798 return err;
800 switch (type) {
801 case GOT_OBJ_TYPE_BLOB:
802 type_str = GOT_OBJ_LABEL_BLOB;
803 a->nblobs++;
804 break;
805 case GOT_OBJ_TYPE_TREE:
806 type_str = GOT_OBJ_LABEL_TREE;
807 a->ntrees++;
808 break;
809 case GOT_OBJ_TYPE_COMMIT:
810 type_str = GOT_OBJ_LABEL_COMMIT;
811 a->ncommits++;
812 break;
813 case GOT_OBJ_TYPE_TAG:
814 type_str = GOT_OBJ_LABEL_TAG;
815 a->ntags++;
816 break;
817 case GOT_OBJ_TYPE_OFFSET_DELTA:
818 type_str = "offset-delta";
819 if (asprintf(&delta_str, " base-offset %lld",
820 (long long)base_offset) == -1) {
821 err = got_error_from_errno("asprintf");
822 goto done;
824 a->noffdeltas++;
825 break;
826 case GOT_OBJ_TYPE_REF_DELTA:
827 type_str = "ref-delta";
828 err = got_object_id_str(&base_id_str, base_id);
829 if (err)
830 goto done;
831 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
832 err = got_error_from_errno("asprintf");
833 goto done;
835 a->nrefdeltas++;
836 break;
837 default:
838 err = got_error(GOT_ERR_OBJ_TYPE);
839 goto done;
841 if (a->human_readable) {
842 char scaled[FMT_SCALED_STRSIZE];
843 char *s;;
844 if (fmt_scaled(size, scaled) == -1) {
845 err = got_error_from_errno("fmt_scaled");
846 goto done;
848 s = scaled;
849 while (isspace((unsigned char)*s))
850 s++;
851 printf("%s %s at %lld size %s%s\n", id_str, type_str,
852 (long long)offset, s, delta_str ? delta_str : "");
853 } else {
854 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
855 (long long)offset, (long long)size,
856 delta_str ? delta_str : "");
858 done:
859 free(id_str);
860 free(base_id_str);
861 free(delta_str);
862 return err;
865 static const struct got_error *
866 cmd_listpack(int argc, char *argv[])
868 const struct got_error *error = NULL;
869 struct got_repository *repo = NULL;
870 int ch;
871 struct got_object_id *pack_hash = NULL;
872 char *packfile_path = NULL;
873 char *id_str = NULL;
874 struct gotadmin_list_pack_cb_args lpa;
875 FILE *packfile = NULL;
876 int show_stats = 0, human_readable = 0;
878 while ((ch = getopt(argc, argv, "hs")) != -1) {
879 switch (ch) {
880 case 'h':
881 human_readable = 1;
882 break;
883 case 's':
884 show_stats = 1;
885 break;
886 default:
887 usage_listpack();
888 /* NOTREACHED */
892 argc -= optind;
893 argv += optind;
895 if (argc != 1)
896 usage_listpack();
897 packfile_path = realpath(argv[0], NULL);
898 if (packfile_path == NULL)
899 return got_error_from_errno2("realpath", argv[0]);
901 #ifndef PROFILE
902 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
903 NULL) == -1)
904 err(1, "pledge");
905 #endif
906 error = got_repo_open(&repo, packfile_path, NULL);
907 if (error)
908 goto done;
910 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
911 if (error)
912 goto done;
914 error = got_repo_find_pack(&packfile, &pack_hash, repo,
915 packfile_path);
916 if (error)
917 goto done;
918 error = got_object_id_str(&id_str, pack_hash);
919 if (error)
920 goto done;
922 memset(&lpa, 0, sizeof(lpa));
923 lpa.human_readable = human_readable;
924 error = got_repo_list_pack(packfile, pack_hash, repo,
925 list_pack_cb, &lpa, check_cancelled, NULL);
926 if (error)
927 goto done;
928 if (show_stats) {
929 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
930 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
931 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
932 lpa.noffdeltas + lpa.nrefdeltas,
933 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
934 lpa.noffdeltas, lpa.nrefdeltas);
936 done:
937 if (repo)
938 got_repo_close(repo);
939 free(id_str);
940 free(pack_hash);
941 free(packfile_path);
942 return error;
945 __dead static void
946 usage_cleanup(void)
948 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
949 "[-q]\n", getprogname());
950 exit(1);
953 struct got_cleanup_progress_arg {
954 int last_nloose;
955 int last_ncommits;
956 int last_npurged;
957 int verbosity;
958 int printed_something;
959 int dry_run;
960 };
962 static const struct got_error *
963 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
965 struct got_cleanup_progress_arg *a = arg;
966 int print_loose = 0, print_commits = 0, print_purged = 0;
968 if (a->last_nloose != nloose) {
969 print_loose = 1;
970 a->last_nloose = nloose;
972 if (a->last_ncommits != ncommits) {
973 print_loose = 1;
974 print_commits = 1;
975 a->last_ncommits = ncommits;
977 if (a->last_npurged != npurged) {
978 print_loose = 1;
979 print_commits = 1;
980 print_purged = 1;
981 a->last_npurged = npurged;
984 if (a->verbosity < 0)
985 return NULL;
987 if (print_loose || print_commits || print_purged)
988 printf("\r");
989 if (print_loose)
990 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
991 if (print_commits)
992 printf("; %d commit%s scanned", ncommits,
993 ncommits == 1 ? "" : "s");
994 if (print_purged) {
995 if (a->dry_run) {
996 printf("; %d object%s could be purged", npurged,
997 npurged == 1 ? "" : "s");
998 } else {
999 printf("; %d object%s purged", npurged,
1000 npurged == 1 ? "" : "s");
1003 if (print_loose || print_commits || print_purged) {
1004 a->printed_something = 1;
1005 fflush(stdout);
1007 return NULL;
1010 struct got_lonely_packidx_progress_arg {
1011 int verbosity;
1012 int printed_something;
1013 int dry_run;
1016 static const struct got_error *
1017 lonely_packidx_progress(void *arg, const char *path)
1019 struct got_lonely_packidx_progress_arg *a = arg;
1021 if (a->verbosity < 0)
1022 return NULL;
1024 if (a->dry_run)
1025 printf("%s could be removed\n", path);
1026 else
1027 printf("%s removed\n", path);
1029 a->printed_something = 1;
1030 return NULL;
1033 static const struct got_error *
1034 cmd_cleanup(int argc, char *argv[])
1036 const struct got_error *error = NULL;
1037 char *repo_path = NULL;
1038 struct got_repository *repo = NULL;
1039 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1040 int remove_lonely_packidx = 0, ignore_mtime = 0;
1041 struct got_cleanup_progress_arg cpa;
1042 struct got_lonely_packidx_progress_arg lpa;
1043 off_t size_before, size_after;
1044 char scaled_before[FMT_SCALED_STRSIZE];
1045 char scaled_after[FMT_SCALED_STRSIZE];
1046 char scaled_diff[FMT_SCALED_STRSIZE];
1047 char **extensions;
1048 int nextensions, i;
1050 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1051 switch (ch) {
1052 case 'a':
1053 ignore_mtime = 1;
1054 break;
1055 case 'p':
1056 remove_lonely_packidx = 1;
1057 break;
1058 case 'r':
1059 repo_path = realpath(optarg, NULL);
1060 if (repo_path == NULL)
1061 return got_error_from_errno2("realpath",
1062 optarg);
1063 got_path_strip_trailing_slashes(repo_path);
1064 break;
1065 case 'n':
1066 dry_run = 1;
1067 break;
1068 case 'q':
1069 verbosity = -1;
1070 break;
1071 default:
1072 usage_cleanup();
1073 /* NOTREACHED */
1077 argc -= optind;
1078 argv += optind;
1080 #ifndef PROFILE
1081 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1082 NULL) == -1)
1083 err(1, "pledge");
1084 #endif
1085 if (repo_path == NULL) {
1086 error = get_repo_path(&repo_path);
1087 if (error)
1088 goto done;
1090 error = got_repo_open(&repo, repo_path, NULL);
1091 if (error)
1092 goto done;
1094 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1095 if (error)
1096 goto done;
1098 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1099 repo);
1100 for (i = 0; i < nextensions; i++) {
1101 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1102 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1103 "the preciousObjects Git extension is enabled; "
1104 "this implies that objects must not be deleted");
1105 goto done;
1109 if (remove_lonely_packidx) {
1110 memset(&lpa, 0, sizeof(lpa));
1111 lpa.dry_run = dry_run;
1112 lpa.verbosity = verbosity;
1113 error = got_repo_remove_lonely_packidx(repo, dry_run,
1114 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1115 goto done;
1118 memset(&cpa, 0, sizeof(cpa));
1119 cpa.last_ncommits = -1;
1120 cpa.last_npurged = -1;
1121 cpa.dry_run = dry_run;
1122 cpa.verbosity = verbosity;
1123 error = got_repo_purge_unreferenced_loose_objects(repo,
1124 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1125 cleanup_progress, &cpa, check_cancelled, NULL);
1126 if (cpa.printed_something)
1127 printf("\n");
1128 if (error)
1129 goto done;
1130 if (cpa.printed_something) {
1131 if (fmt_scaled(size_before, scaled_before) == -1) {
1132 error = got_error_from_errno("fmt_scaled");
1133 goto done;
1135 if (fmt_scaled(size_after, scaled_after) == -1) {
1136 error = got_error_from_errno("fmt_scaled");
1137 goto done;
1139 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1140 error = got_error_from_errno("fmt_scaled");
1141 goto done;
1143 printf("loose total size before: %s\n", scaled_before);
1144 printf("loose total size after: %s\n", scaled_after);
1145 if (dry_run) {
1146 printf("disk space which would be freed: %s\n",
1147 scaled_diff);
1148 } else
1149 printf("disk space freed: %s\n", scaled_diff);
1150 printf("loose objects also found in pack files: %d\n", npacked);
1152 done:
1153 if (repo)
1154 got_repo_close(repo);
1155 free(repo_path);
1156 return error;