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"
44 #include "got_worktree.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 get_repo_path(char **repo_path)
237 const struct got_error *err = NULL;
238 struct got_worktree *worktree = NULL;
239 char *cwd;
241 *repo_path = NULL;
243 cwd = getcwd(NULL, 0);
244 if (cwd == NULL)
245 return got_error_from_errno("getcwd");
247 err = got_worktree_open(&worktree, cwd);
248 if (err) {
249 if (err->code != GOT_ERR_NOT_WORKTREE)
250 goto done;
251 err = NULL;
254 if (worktree)
255 *repo_path = strdup(got_worktree_get_repo_path(worktree));
256 else
257 *repo_path = strdup(cwd);
258 if (*repo_path == NULL)
259 err = got_error_from_errno("strdup");
260 done:
261 if (worktree)
262 got_worktree_close(worktree);
263 free(cwd);
264 return err;
267 static const struct got_error *
268 cmd_info(int argc, char *argv[])
270 const struct got_error *error = NULL;
271 char *repo_path = NULL;
272 struct got_repository *repo = NULL;
273 const struct got_gotconfig *gotconfig = NULL;
274 int ch, npackfiles, npackedobj, nobj;
275 off_t packsize, loose_size;
276 char scaled[FMT_SCALED_STRSIZE];
278 while ((ch = getopt(argc, argv, "r:")) != -1) {
279 switch (ch) {
280 case 'r':
281 repo_path = realpath(optarg, NULL);
282 if (repo_path == NULL)
283 return got_error_from_errno2("realpath",
284 optarg);
285 got_path_strip_trailing_slashes(repo_path);
286 break;
287 default:
288 usage_info();
289 /* NOTREACHED */
293 argc -= optind;
294 argv += optind;
296 #ifndef PROFILE
297 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
298 NULL) == -1)
299 err(1, "pledge");
300 #endif
301 if (repo_path == NULL) {
302 error = get_repo_path(&repo_path);
303 if (error)
304 goto done;
306 error = got_repo_open(&repo, repo_path, NULL);
307 if (error)
308 goto done;
310 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
311 if (error)
312 goto done;
314 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
316 gotconfig = got_repo_get_gotconfig(repo);
317 if (gotconfig) {
318 const struct got_remote_repo *remotes;
319 int i, nremotes;
320 if (got_gotconfig_get_author(gotconfig)) {
321 printf("default author: %s\n",
322 got_gotconfig_get_author(gotconfig));
324 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
325 for (i = 0; i < nremotes; i++) {
326 const char *fetch_url = remotes[i].fetch_url;
327 const char *send_url = remotes[i].send_url;
328 if (strcmp(fetch_url, send_url) == 0) {
329 printf("remote \"%s\": %s\n", remotes[i].name,
330 remotes[i].fetch_url);
331 } else {
332 printf("remote \"%s\" (fetch): %s\n",
333 remotes[i].name, remotes[i].fetch_url);
334 printf("remote \"%s\" (send): %s\n",
335 remotes[i].name, remotes[i].send_url);
340 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
341 &packsize, repo);
342 if (error)
343 goto done;
344 printf("pack files: %d\n", npackfiles);
345 if (npackfiles > 0) {
346 if (fmt_scaled(packsize, scaled) == -1) {
347 error = got_error_from_errno("fmt_scaled");
348 goto done;
350 printf("packed objects: %d\n", npackedobj);
351 printf("packed total size: %s\n", scaled);
354 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
355 if (error)
356 goto done;
357 printf("loose objects: %d\n", nobj);
358 if (nobj > 0) {
359 if (fmt_scaled(loose_size, scaled) == -1) {
360 error = got_error_from_errno("fmt_scaled");
361 goto done;
363 printf("loose total size: %s\n", scaled);
365 done:
366 if (repo)
367 got_repo_close(repo);
368 free(repo_path);
369 return error;
372 __dead static void
373 usage_pack(void)
375 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
376 "[-x reference] [reference ...]\n",
377 getprogname());
378 exit(1);
381 struct got_pack_progress_arg {
382 char last_scaled_size[FMT_SCALED_STRSIZE];
383 int last_ncommits;
384 int last_nobj_total;
385 int last_p_deltify;
386 int last_p_written;
387 int last_p_indexed;
388 int last_p_resolved;
389 int verbosity;
390 int printed_something;
391 };
393 static const struct got_error *
394 pack_progress(void *arg, off_t packfile_size, int ncommits,
395 int nobj_total, int nobj_deltify, int nobj_written)
397 struct got_pack_progress_arg *a = arg;
398 char scaled_size[FMT_SCALED_STRSIZE];
399 int p_deltify, p_written;
400 int print_searching = 0, print_total = 0;
401 int print_deltify = 0, print_written = 0;
403 if (a->verbosity < 0)
404 return NULL;
406 if (fmt_scaled(packfile_size, scaled_size) == -1)
407 return got_error_from_errno("fmt_scaled");
409 if (a->last_ncommits != ncommits) {
410 print_searching = 1;
411 a->last_ncommits = ncommits;
414 if (a->last_nobj_total != nobj_total) {
415 print_searching = 1;
416 print_total = 1;
417 a->last_nobj_total = nobj_total;
420 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
421 strcmp(scaled_size, a->last_scaled_size)) != 0) {
422 if (strlcpy(a->last_scaled_size, scaled_size,
423 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
424 return got_error(GOT_ERR_NO_SPACE);
427 if (nobj_deltify > 0 || nobj_written > 0) {
428 if (nobj_deltify > 0) {
429 p_deltify = (nobj_deltify * 100) / nobj_total;
430 if (p_deltify != a->last_p_deltify) {
431 a->last_p_deltify = p_deltify;
432 print_searching = 1;
433 print_total = 1;
434 print_deltify = 1;
437 if (nobj_written > 0) {
438 p_written = (nobj_written * 100) / nobj_total;
439 if (p_written != a->last_p_written) {
440 a->last_p_written = p_written;
441 print_searching = 1;
442 print_total = 1;
443 print_deltify = 1;
444 print_written = 1;
449 if (print_searching || print_total || print_deltify || print_written)
450 printf("\r");
451 if (print_searching)
452 printf("packing %d reference%s", ncommits,
453 ncommits == 1 ? "" : "s");
454 if (print_total)
455 printf("; %d object%s", nobj_total,
456 nobj_total == 1 ? "" : "s");
457 if (print_deltify)
458 printf("; deltify: %d%%", p_deltify);
459 if (print_written)
460 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
461 scaled_size, p_written);
462 if (print_searching || print_total || print_deltify ||
463 print_written) {
464 a->printed_something = 1;
465 fflush(stdout);
467 return NULL;
470 static const struct got_error *
471 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
472 int nobj_indexed, int nobj_loose, int nobj_resolved)
474 struct got_pack_progress_arg *a = arg;
475 char scaled_size[FMT_SCALED_STRSIZE];
476 int p_indexed, p_resolved;
477 int print_size = 0, print_indexed = 0, print_resolved = 0;
479 if (a->verbosity < 0)
480 return NULL;
482 if (packfile_size > 0 || nobj_indexed > 0) {
483 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
484 (a->last_scaled_size[0] == '\0' ||
485 strcmp(scaled_size, a->last_scaled_size)) != 0) {
486 print_size = 1;
487 if (strlcpy(a->last_scaled_size, scaled_size,
488 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
489 return got_error(GOT_ERR_NO_SPACE);
491 if (nobj_indexed > 0) {
492 p_indexed = (nobj_indexed * 100) / nobj_total;
493 if (p_indexed != a->last_p_indexed) {
494 a->last_p_indexed = p_indexed;
495 print_indexed = 1;
496 print_size = 1;
499 if (nobj_resolved > 0) {
500 p_resolved = (nobj_resolved * 100) /
501 (nobj_total - nobj_loose);
502 if (p_resolved != a->last_p_resolved) {
503 a->last_p_resolved = p_resolved;
504 print_resolved = 1;
505 print_indexed = 1;
506 print_size = 1;
511 if (print_size || print_indexed || print_resolved)
512 printf("\r");
513 if (print_size)
514 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
515 if (print_indexed)
516 printf("; indexing %d%%", p_indexed);
517 if (print_resolved)
518 printf("; resolving deltas %d%%", p_resolved);
519 if (print_size || print_indexed || print_resolved)
520 fflush(stdout);
522 return NULL;
525 static const struct got_error *
526 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
527 const char *refname, struct got_repository *repo)
529 const struct got_error *err;
530 struct got_reference *ref;
532 *new = NULL;
534 err = got_ref_open(&ref, repo, refname, 0);
535 if (err) {
536 if (err->code != GOT_ERR_NOT_REF)
537 return err;
539 /* Treat argument as a reference prefix. */
540 err = got_ref_list(refs, repo, refname,
541 got_ref_cmp_by_name, NULL);
542 } else {
543 err = got_reflist_insert(new, refs, ref,
544 got_ref_cmp_by_name, NULL);
545 if (err || *new == NULL /* duplicate */)
546 got_ref_close(ref);
549 return err;
552 static const struct got_error *
553 cmd_pack(int argc, char *argv[])
555 const struct got_error *error = NULL;
556 char *repo_path = NULL;
557 struct got_repository *repo = NULL;
558 int ch, i, loose_obj_only = 1;
559 struct got_object_id *pack_hash = NULL;
560 char *id_str = NULL;
561 struct got_pack_progress_arg ppa;
562 FILE *packfile = NULL;
563 struct got_pathlist_head exclude_args;
564 struct got_pathlist_entry *pe;
565 struct got_reflist_head exclude_refs;
566 struct got_reflist_head include_refs;
567 struct got_reflist_entry *re, *new;
569 TAILQ_INIT(&exclude_args);
570 TAILQ_INIT(&exclude_refs);
571 TAILQ_INIT(&include_refs);
573 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
574 switch (ch) {
575 case 'a':
576 loose_obj_only = 0;
577 break;
578 case 'r':
579 repo_path = realpath(optarg, NULL);
580 if (repo_path == NULL)
581 return got_error_from_errno2("realpath",
582 optarg);
583 got_path_strip_trailing_slashes(repo_path);
584 break;
585 case 'x':
586 got_path_strip_trailing_slashes(optarg);
587 error = got_pathlist_append(&exclude_args,
588 optarg, NULL);
589 if (error)
590 return error;
591 break;
592 default:
593 usage_pack();
594 /* NOTREACHED */
598 argc -= optind;
599 argv += optind;
601 #ifndef PROFILE
602 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
603 NULL) == -1)
604 err(1, "pledge");
605 #endif
606 if (repo_path == NULL) {
607 error = get_repo_path(&repo_path);
608 if (error)
609 goto done;
611 error = got_repo_open(&repo, repo_path, NULL);
612 if (error)
613 goto done;
615 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
616 if (error)
617 goto done;
619 TAILQ_FOREACH(pe, &exclude_args, entry) {
620 const char *refname = pe->path;
621 error = add_ref(&new, &exclude_refs, refname, repo);
622 if (error)
623 goto done;
627 if (argc == 0) {
628 error = got_ref_list(&include_refs, repo, "",
629 got_ref_cmp_by_name, NULL);
630 if (error)
631 goto done;
632 } else {
633 for (i = 0; i < argc; i++) {
634 const char *refname;
635 got_path_strip_trailing_slashes(argv[i]);
636 refname = argv[i];
637 error = add_ref(&new, &include_refs, refname, repo);
638 if (error)
639 goto done;
643 /* Ignore references in the refs/got/ namespace. */
644 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
645 const char *refname = got_ref_get_name(re->ref);
646 if (strncmp("refs/got/", refname, 9) != 0)
647 continue;
648 TAILQ_REMOVE(&include_refs, re, entry);
649 got_ref_close(re->ref);
650 free(re);
653 memset(&ppa, 0, sizeof(ppa));
654 ppa.last_scaled_size[0] = '\0';
655 ppa.last_p_indexed = -1;
656 ppa.last_p_resolved = -1;
658 error = got_repo_pack_objects(&packfile, &pack_hash,
659 &include_refs, &exclude_refs, repo, loose_obj_only,
660 pack_progress, &ppa, check_cancelled, NULL);
661 if (error) {
662 if (ppa.printed_something)
663 printf("\n");
664 goto done;
667 error = got_object_id_str(&id_str, pack_hash);
668 if (error)
669 goto done;
670 printf("\nWrote %s.pack\n", id_str);
672 error = got_repo_index_pack(packfile, pack_hash, repo,
673 pack_index_progress, &ppa, check_cancelled, NULL);
674 if (error)
675 goto done;
676 printf("\nIndexed %s.pack\n", id_str);
677 done:
678 if (repo)
679 got_repo_close(repo);
680 got_pathlist_free(&exclude_args);
681 got_ref_list_free(&exclude_refs);
682 got_ref_list_free(&include_refs);
683 free(id_str);
684 free(pack_hash);
685 free(repo_path);
686 return error;
689 __dead static void
690 usage_indexpack(void)
692 fprintf(stderr, "usage: %s indexpack packfile-path\n",
693 getprogname());
694 exit(1);
697 static const struct got_error *
698 cmd_indexpack(int argc, char *argv[])
700 const struct got_error *error = NULL;
701 struct got_repository *repo = NULL;
702 int ch;
703 struct got_object_id *pack_hash = NULL;
704 char *packfile_path = NULL;
705 char *id_str = NULL;
706 struct got_pack_progress_arg ppa;
707 FILE *packfile = NULL;
709 while ((ch = getopt(argc, argv, "")) != -1) {
710 switch (ch) {
711 default:
712 usage_indexpack();
713 /* NOTREACHED */
717 argc -= optind;
718 argv += optind;
720 if (argc != 1)
721 usage_indexpack();
723 packfile_path = realpath(argv[0], NULL);
724 if (packfile_path == NULL)
725 return got_error_from_errno2("realpath", argv[0]);
727 #ifndef PROFILE
728 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
729 NULL) == -1)
730 err(1, "pledge");
731 #endif
733 error = got_repo_open(&repo, packfile_path, NULL);
734 if (error)
735 goto done;
737 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
738 if (error)
739 goto done;
741 memset(&ppa, 0, sizeof(ppa));
742 ppa.last_scaled_size[0] = '\0';
743 ppa.last_p_indexed = -1;
744 ppa.last_p_resolved = -1;
746 error = got_repo_find_pack(&packfile, &pack_hash, repo,
747 packfile_path);
748 if (error)
749 goto done;
751 error = got_object_id_str(&id_str, pack_hash);
752 if (error)
753 goto done;
755 error = got_repo_index_pack(packfile, pack_hash, repo,
756 pack_index_progress, &ppa, check_cancelled, NULL);
757 if (error)
758 goto done;
759 printf("\nIndexed %s.pack\n", id_str);
760 done:
761 if (repo)
762 got_repo_close(repo);
763 free(id_str);
764 free(pack_hash);
765 return error;
768 __dead static void
769 usage_listpack(void)
771 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
772 getprogname());
773 exit(1);
776 struct gotadmin_list_pack_cb_args {
777 int nblobs;
778 int ntrees;
779 int ncommits;
780 int ntags;
781 int noffdeltas;
782 int nrefdeltas;
783 int human_readable;
784 };
786 static const struct got_error *
787 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
788 off_t size, off_t base_offset, struct got_object_id *base_id)
790 const struct got_error *err;
791 struct gotadmin_list_pack_cb_args *a = arg;
792 char *id_str, *delta_str = NULL, *base_id_str = NULL;
793 const char *type_str;
795 err = got_object_id_str(&id_str, id);
796 if (err)
797 return err;
799 switch (type) {
800 case GOT_OBJ_TYPE_BLOB:
801 type_str = GOT_OBJ_LABEL_BLOB;
802 a->nblobs++;
803 break;
804 case GOT_OBJ_TYPE_TREE:
805 type_str = GOT_OBJ_LABEL_TREE;
806 a->ntrees++;
807 break;
808 case GOT_OBJ_TYPE_COMMIT:
809 type_str = GOT_OBJ_LABEL_COMMIT;
810 a->ncommits++;
811 break;
812 case GOT_OBJ_TYPE_TAG:
813 type_str = GOT_OBJ_LABEL_TAG;
814 a->ntags++;
815 break;
816 case GOT_OBJ_TYPE_OFFSET_DELTA:
817 type_str = "offset-delta";
818 if (asprintf(&delta_str, " base-offset %lld",
819 (long long)base_offset) == -1) {
820 err = got_error_from_errno("asprintf");
821 goto done;
823 a->noffdeltas++;
824 break;
825 case GOT_OBJ_TYPE_REF_DELTA:
826 type_str = "ref-delta";
827 err = got_object_id_str(&base_id_str, base_id);
828 if (err)
829 goto done;
830 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
831 err = got_error_from_errno("asprintf");
832 goto done;
834 a->nrefdeltas++;
835 break;
836 default:
837 err = got_error(GOT_ERR_OBJ_TYPE);
838 goto done;
840 if (a->human_readable) {
841 char scaled[FMT_SCALED_STRSIZE];
842 char *s;;
843 if (fmt_scaled(size, scaled) == -1) {
844 err = got_error_from_errno("fmt_scaled");
845 goto done;
847 s = scaled;
848 while (isspace((unsigned char)*s))
849 s++;
850 printf("%s %s at %lld size %s%s\n", id_str, type_str,
851 (long long)offset, s, delta_str ? delta_str : "");
852 } else {
853 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
854 (long long)offset, (long long)size,
855 delta_str ? delta_str : "");
857 done:
858 free(id_str);
859 free(base_id_str);
860 free(delta_str);
861 return err;
864 static const struct got_error *
865 cmd_listpack(int argc, char *argv[])
867 const struct got_error *error = NULL;
868 struct got_repository *repo = NULL;
869 int ch;
870 struct got_object_id *pack_hash = NULL;
871 char *packfile_path = NULL;
872 char *id_str = NULL;
873 struct gotadmin_list_pack_cb_args lpa;
874 FILE *packfile = NULL;
875 int show_stats = 0, human_readable = 0;
877 while ((ch = getopt(argc, argv, "hs")) != -1) {
878 switch (ch) {
879 case 'h':
880 human_readable = 1;
881 break;
882 case 's':
883 show_stats = 1;
884 break;
885 default:
886 usage_listpack();
887 /* NOTREACHED */
891 argc -= optind;
892 argv += optind;
894 if (argc != 1)
895 usage_listpack();
896 packfile_path = realpath(argv[0], NULL);
897 if (packfile_path == NULL)
898 return got_error_from_errno2("realpath", argv[0]);
900 #ifndef PROFILE
901 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
902 NULL) == -1)
903 err(1, "pledge");
904 #endif
905 error = got_repo_open(&repo, packfile_path, NULL);
906 if (error)
907 goto done;
909 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
910 if (error)
911 goto done;
913 error = got_repo_find_pack(&packfile, &pack_hash, repo,
914 packfile_path);
915 if (error)
916 goto done;
917 error = got_object_id_str(&id_str, pack_hash);
918 if (error)
919 goto done;
921 memset(&lpa, 0, sizeof(lpa));
922 lpa.human_readable = human_readable;
923 error = got_repo_list_pack(packfile, pack_hash, repo,
924 list_pack_cb, &lpa, check_cancelled, NULL);
925 if (error)
926 goto done;
927 if (show_stats) {
928 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
929 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
930 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
931 lpa.noffdeltas + lpa.nrefdeltas,
932 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
933 lpa.noffdeltas, lpa.nrefdeltas);
935 done:
936 if (repo)
937 got_repo_close(repo);
938 free(id_str);
939 free(pack_hash);
940 free(packfile_path);
941 return error;
944 __dead static void
945 usage_cleanup(void)
947 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
948 "[-q]\n", getprogname());
949 exit(1);
952 struct got_cleanup_progress_arg {
953 int last_nloose;
954 int last_ncommits;
955 int last_npurged;
956 int verbosity;
957 int printed_something;
958 int dry_run;
959 };
961 static const struct got_error *
962 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
964 struct got_cleanup_progress_arg *a = arg;
965 int print_loose = 0, print_commits = 0, print_purged = 0;
967 if (a->last_nloose != nloose) {
968 print_loose = 1;
969 a->last_nloose = nloose;
971 if (a->last_ncommits != ncommits) {
972 print_loose = 1;
973 print_commits = 1;
974 a->last_ncommits = ncommits;
976 if (a->last_npurged != npurged) {
977 print_loose = 1;
978 print_commits = 1;
979 print_purged = 1;
980 a->last_npurged = npurged;
983 if (a->verbosity < 0)
984 return NULL;
986 if (print_loose || print_commits || print_purged)
987 printf("\r");
988 if (print_loose)
989 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
990 if (print_commits)
991 printf("; %d commit%s scanned", ncommits,
992 ncommits == 1 ? "" : "s");
993 if (print_purged) {
994 if (a->dry_run) {
995 printf("; %d object%s could be purged", npurged,
996 npurged == 1 ? "" : "s");
997 } else {
998 printf("; %d object%s purged", npurged,
999 npurged == 1 ? "" : "s");
1002 if (print_loose || print_commits || print_purged) {
1003 a->printed_something = 1;
1004 fflush(stdout);
1006 return NULL;
1009 struct got_lonely_packidx_progress_arg {
1010 int verbosity;
1011 int printed_something;
1012 int dry_run;
1015 static const struct got_error *
1016 lonely_packidx_progress(void *arg, const char *path)
1018 struct got_lonely_packidx_progress_arg *a = arg;
1020 if (a->verbosity < 0)
1021 return NULL;
1023 if (a->dry_run)
1024 printf("%s could be removed\n", path);
1025 else
1026 printf("%s removed\n", path);
1028 a->printed_something = 1;
1029 return NULL;
1032 static const struct got_error *
1033 cmd_cleanup(int argc, char *argv[])
1035 const struct got_error *error = NULL;
1036 char *repo_path = NULL;
1037 struct got_repository *repo = NULL;
1038 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1039 int remove_lonely_packidx = 0, ignore_mtime = 0;
1040 struct got_cleanup_progress_arg cpa;
1041 struct got_lonely_packidx_progress_arg lpa;
1042 off_t size_before, size_after;
1043 char scaled_before[FMT_SCALED_STRSIZE];
1044 char scaled_after[FMT_SCALED_STRSIZE];
1045 char scaled_diff[FMT_SCALED_STRSIZE];
1046 char **extensions;
1047 int nextensions, i;
1049 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1050 switch (ch) {
1051 case 'a':
1052 ignore_mtime = 1;
1053 break;
1054 case 'p':
1055 remove_lonely_packidx = 1;
1056 break;
1057 case 'r':
1058 repo_path = realpath(optarg, NULL);
1059 if (repo_path == NULL)
1060 return got_error_from_errno2("realpath",
1061 optarg);
1062 got_path_strip_trailing_slashes(repo_path);
1063 break;
1064 case 'n':
1065 dry_run = 1;
1066 break;
1067 case 'q':
1068 verbosity = -1;
1069 break;
1070 default:
1071 usage_cleanup();
1072 /* NOTREACHED */
1076 argc -= optind;
1077 argv += optind;
1079 #ifndef PROFILE
1080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1081 NULL) == -1)
1082 err(1, "pledge");
1083 #endif
1084 if (repo_path == NULL) {
1085 error = get_repo_path(&repo_path);
1086 if (error)
1087 goto done;
1089 error = got_repo_open(&repo, repo_path, NULL);
1090 if (error)
1091 goto done;
1093 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1094 if (error)
1095 goto done;
1097 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1098 repo);
1099 for (i = 0; i < nextensions; i++) {
1100 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1101 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1102 "the preciousObjects Git extension is enabled; "
1103 "this implies that objects must not be deleted");
1104 goto done;
1108 if (remove_lonely_packidx) {
1109 memset(&lpa, 0, sizeof(lpa));
1110 lpa.dry_run = dry_run;
1111 lpa.verbosity = verbosity;
1112 error = got_repo_remove_lonely_packidx(repo, dry_run,
1113 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1114 goto done;
1117 memset(&cpa, 0, sizeof(cpa));
1118 cpa.last_ncommits = -1;
1119 cpa.last_npurged = -1;
1120 cpa.dry_run = dry_run;
1121 cpa.verbosity = verbosity;
1122 error = got_repo_purge_unreferenced_loose_objects(repo,
1123 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1124 cleanup_progress, &cpa, check_cancelled, NULL);
1125 if (cpa.printed_something)
1126 printf("\n");
1127 if (error)
1128 goto done;
1129 if (cpa.printed_something) {
1130 if (fmt_scaled(size_before, scaled_before) == -1) {
1131 error = got_error_from_errno("fmt_scaled");
1132 goto done;
1134 if (fmt_scaled(size_after, scaled_after) == -1) {
1135 error = got_error_from_errno("fmt_scaled");
1136 goto done;
1138 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1139 error = got_error_from_errno("fmt_scaled");
1140 goto done;
1142 printf("loose total size before: %s\n", scaled_before);
1143 printf("loose total size after: %s\n", scaled_after);
1144 if (dry_run) {
1145 printf("disk space which would be freed: %s\n",
1146 scaled_diff);
1147 } else
1148 printf("disk space freed: %s\n", scaled_diff);
1149 printf("loose objects also found in pack files: %d\n", npacked);
1151 done:
1152 if (repo)
1153 got_repo_close(repo);
1154 free(repo_path);
1155 return error;