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 const char *fetch_url = remotes[i].fetch_url;
295 const char *send_url = remotes[i].send_url;
296 if (strcmp(fetch_url, send_url) == 0) {
297 printf("remote \"%s\": %s\n", remotes[i].name,
298 remotes[i].fetch_url);
299 } else {
300 printf("remote \"%s\" (fetch): %s\n",
301 remotes[i].name, remotes[i].fetch_url);
302 printf("remote \"%s\" (send): %s\n",
303 remotes[i].name, remotes[i].send_url);
308 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
309 &packsize, repo);
310 if (error)
311 goto done;
312 printf("pack files: %d\n", npackfiles);
313 if (npackfiles > 0) {
314 if (fmt_scaled(packsize, scaled) == -1) {
315 error = got_error_from_errno("fmt_scaled");
316 goto done;
318 printf("packed objects: %d\n", npackedobj);
319 printf("packed total size: %s\n", scaled);
322 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
323 if (error)
324 goto done;
325 printf("loose objects: %d\n", nobj);
326 if (nobj > 0) {
327 if (fmt_scaled(loose_size, scaled) == -1) {
328 error = got_error_from_errno("fmt_scaled");
329 goto done;
331 printf("loose total size: %s\n", scaled);
333 done:
334 if (repo)
335 got_repo_close(repo);
336 free(cwd);
337 return error;
340 __dead static void
341 usage_pack(void)
343 fprintf(stderr, "usage: %s pack [-a] [-r repository-path] "
344 "[-x reference] [reference ...]\n",
345 getprogname());
346 exit(1);
349 struct got_pack_progress_arg {
350 char last_scaled_size[FMT_SCALED_STRSIZE];
351 int last_ncommits;
352 int last_nobj_total;
353 int last_p_deltify;
354 int last_p_written;
355 int last_p_indexed;
356 int last_p_resolved;
357 int verbosity;
358 int printed_something;
359 };
361 static const struct got_error *
362 pack_progress(void *arg, off_t packfile_size, int ncommits,
363 int nobj_total, int nobj_deltify, int nobj_written)
365 struct got_pack_progress_arg *a = arg;
366 char scaled_size[FMT_SCALED_STRSIZE];
367 int p_deltify, p_written;
368 int print_searching = 0, print_total = 0;
369 int print_deltify = 0, print_written = 0;
371 if (a->verbosity < 0)
372 return NULL;
374 if (fmt_scaled(packfile_size, scaled_size) == -1)
375 return got_error_from_errno("fmt_scaled");
377 if (a->last_ncommits != ncommits) {
378 print_searching = 1;
379 a->last_ncommits = ncommits;
382 if (a->last_nobj_total != nobj_total) {
383 print_searching = 1;
384 print_total = 1;
385 a->last_nobj_total = nobj_total;
388 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
389 strcmp(scaled_size, a->last_scaled_size)) != 0) {
390 if (strlcpy(a->last_scaled_size, scaled_size,
391 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
392 return got_error(GOT_ERR_NO_SPACE);
395 if (nobj_deltify > 0 || nobj_written > 0) {
396 if (nobj_deltify > 0) {
397 p_deltify = (nobj_deltify * 100) / nobj_total;
398 if (p_deltify != a->last_p_deltify) {
399 a->last_p_deltify = p_deltify;
400 print_searching = 1;
401 print_total = 1;
402 print_deltify = 1;
405 if (nobj_written > 0) {
406 p_written = (nobj_written * 100) / nobj_total;
407 if (p_written != a->last_p_written) {
408 a->last_p_written = p_written;
409 print_searching = 1;
410 print_total = 1;
411 print_deltify = 1;
412 print_written = 1;
417 if (print_searching || print_total || print_deltify || print_written)
418 printf("\r");
419 if (print_searching)
420 printf("packing %d reference%s", ncommits,
421 ncommits == 1 ? "" : "s");
422 if (print_total)
423 printf("; %d object%s", nobj_total,
424 nobj_total == 1 ? "" : "s");
425 if (print_deltify)
426 printf("; deltify: %d%%", p_deltify);
427 if (print_written)
428 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
429 scaled_size, p_written);
430 if (print_searching || print_total || print_deltify ||
431 print_written) {
432 a->printed_something = 1;
433 fflush(stdout);
435 return NULL;
438 static const struct got_error *
439 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
440 int nobj_indexed, int nobj_loose, int nobj_resolved)
442 struct got_pack_progress_arg *a = arg;
443 char scaled_size[FMT_SCALED_STRSIZE];
444 int p_indexed, p_resolved;
445 int print_size = 0, print_indexed = 0, print_resolved = 0;
447 if (a->verbosity < 0)
448 return NULL;
450 if (packfile_size > 0 || nobj_indexed > 0) {
451 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
452 (a->last_scaled_size[0] == '\0' ||
453 strcmp(scaled_size, a->last_scaled_size)) != 0) {
454 print_size = 1;
455 if (strlcpy(a->last_scaled_size, scaled_size,
456 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
457 return got_error(GOT_ERR_NO_SPACE);
459 if (nobj_indexed > 0) {
460 p_indexed = (nobj_indexed * 100) / nobj_total;
461 if (p_indexed != a->last_p_indexed) {
462 a->last_p_indexed = p_indexed;
463 print_indexed = 1;
464 print_size = 1;
467 if (nobj_resolved > 0) {
468 p_resolved = (nobj_resolved * 100) /
469 (nobj_total - nobj_loose);
470 if (p_resolved != a->last_p_resolved) {
471 a->last_p_resolved = p_resolved;
472 print_resolved = 1;
473 print_indexed = 1;
474 print_size = 1;
479 if (print_size || print_indexed || print_resolved)
480 printf("\r");
481 if (print_size)
482 printf("%*s packed", FMT_SCALED_STRSIZE, scaled_size);
483 if (print_indexed)
484 printf("; indexing %d%%", p_indexed);
485 if (print_resolved)
486 printf("; resolving deltas %d%%", p_resolved);
487 if (print_size || print_indexed || print_resolved)
488 fflush(stdout);
490 return NULL;
493 static const struct got_error *
494 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
495 const char *refname, struct got_repository *repo)
497 const struct got_error *err;
498 struct got_reference *ref;
500 *new = NULL;
502 err = got_ref_open(&ref, repo, refname, 0);
503 if (err) {
504 if (err->code != GOT_ERR_NOT_REF)
505 return err;
507 /* Treat argument as a reference prefix. */
508 err = got_ref_list(refs, repo, refname,
509 got_ref_cmp_by_name, NULL);
510 } else {
511 err = got_reflist_insert(new, refs, ref,
512 got_ref_cmp_by_name, NULL);
513 if (err || *new == NULL /* duplicate */)
514 got_ref_close(ref);
517 return err;
520 static const struct got_error *
521 cmd_pack(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *cwd = NULL, *repo_path = NULL;
525 struct got_repository *repo = NULL;
526 int ch, i, loose_obj_only = 1;
527 struct got_object_id *pack_hash = NULL;
528 char *id_str = NULL;
529 struct got_pack_progress_arg ppa;
530 FILE *packfile = NULL;
531 struct got_pathlist_head exclude_args;
532 struct got_pathlist_entry *pe;
533 struct got_reflist_head exclude_refs;
534 struct got_reflist_head include_refs;
535 struct got_reflist_entry *re, *new;
537 TAILQ_INIT(&exclude_args);
538 TAILQ_INIT(&exclude_refs);
539 TAILQ_INIT(&include_refs);
541 while ((ch = getopt(argc, argv, "ar:x:")) != -1) {
542 switch (ch) {
543 case 'a':
544 loose_obj_only = 0;
545 break;
546 case 'r':
547 repo_path = realpath(optarg, NULL);
548 if (repo_path == NULL)
549 return got_error_from_errno2("realpath",
550 optarg);
551 got_path_strip_trailing_slashes(repo_path);
552 break;
553 case 'x':
554 got_path_strip_trailing_slashes(optarg);
555 error = got_pathlist_append(&exclude_args,
556 optarg, NULL);
557 if (error)
558 return error;
559 break;
560 default:
561 usage_pack();
562 /* NOTREACHED */
566 argc -= optind;
567 argv += optind;
569 #ifndef PROFILE
570 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
571 NULL) == -1)
572 err(1, "pledge");
573 #endif
574 cwd = getcwd(NULL, 0);
575 if (cwd == NULL) {
576 error = got_error_from_errno("getcwd");
577 goto done;
580 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
581 if (error)
582 goto done;
584 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
585 if (error)
586 goto done;
588 TAILQ_FOREACH(pe, &exclude_args, entry) {
589 const char *refname = pe->path;
590 error = add_ref(&new, &exclude_refs, refname, repo);
591 if (error)
592 goto done;
596 if (argc == 0) {
597 error = got_ref_list(&include_refs, repo, "",
598 got_ref_cmp_by_name, NULL);
599 if (error)
600 goto done;
601 } else {
602 for (i = 0; i < argc; i++) {
603 const char *refname;
604 got_path_strip_trailing_slashes(argv[i]);
605 refname = argv[i];
606 error = add_ref(&new, &include_refs, refname, repo);
607 if (error)
608 goto done;
612 /* Ignore references in the refs/got/ namespace. */
613 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
614 const char *refname = got_ref_get_name(re->ref);
615 if (strncmp("refs/got/", refname, 9) != 0)
616 continue;
617 TAILQ_REMOVE(&include_refs, re, entry);
618 got_ref_close(re->ref);
619 free(re);
622 memset(&ppa, 0, sizeof(ppa));
623 ppa.last_scaled_size[0] = '\0';
624 ppa.last_p_indexed = -1;
625 ppa.last_p_resolved = -1;
627 error = got_repo_pack_objects(&packfile, &pack_hash,
628 &include_refs, &exclude_refs, repo, loose_obj_only,
629 pack_progress, &ppa, check_cancelled, NULL);
630 if (error) {
631 if (ppa.printed_something)
632 printf("\n");
633 goto done;
636 error = got_object_id_str(&id_str, pack_hash);
637 if (error)
638 goto done;
639 printf("\nWrote %s.pack\n", id_str);
641 error = got_repo_index_pack(packfile, pack_hash, repo,
642 pack_index_progress, &ppa, check_cancelled, NULL);
643 if (error)
644 goto done;
645 printf("\nIndexed %s.pack\n", id_str);
646 done:
647 if (repo)
648 got_repo_close(repo);
649 got_pathlist_free(&exclude_args);
650 got_ref_list_free(&exclude_refs);
651 got_ref_list_free(&include_refs);
652 free(id_str);
653 free(pack_hash);
654 free(cwd);
655 return error;
658 __dead static void
659 usage_indexpack(void)
661 fprintf(stderr, "usage: %s indexpack packfile-path\n",
662 getprogname());
663 exit(1);
666 static const struct got_error *
667 cmd_indexpack(int argc, char *argv[])
669 const struct got_error *error = NULL;
670 struct got_repository *repo = NULL;
671 int ch;
672 struct got_object_id *pack_hash = NULL;
673 char *packfile_path = NULL;
674 char *id_str = NULL;
675 struct got_pack_progress_arg ppa;
676 FILE *packfile = NULL;
678 while ((ch = getopt(argc, argv, "")) != -1) {
679 switch (ch) {
680 default:
681 usage_indexpack();
682 /* NOTREACHED */
686 argc -= optind;
687 argv += optind;
689 if (argc != 1)
690 usage_indexpack();
692 packfile_path = realpath(argv[0], NULL);
693 if (packfile_path == NULL)
694 return got_error_from_errno2("realpath", argv[0]);
696 #ifndef PROFILE
697 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
698 NULL) == -1)
699 err(1, "pledge");
700 #endif
702 error = got_repo_open(&repo, packfile_path, NULL);
703 if (error)
704 goto done;
706 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
707 if (error)
708 goto done;
710 memset(&ppa, 0, sizeof(ppa));
711 ppa.last_scaled_size[0] = '\0';
712 ppa.last_p_indexed = -1;
713 ppa.last_p_resolved = -1;
715 error = got_repo_find_pack(&packfile, &pack_hash, repo,
716 packfile_path);
717 if (error)
718 goto done;
720 error = got_object_id_str(&id_str, pack_hash);
721 if (error)
722 goto done;
724 error = got_repo_index_pack(packfile, pack_hash, repo,
725 pack_index_progress, &ppa, check_cancelled, NULL);
726 if (error)
727 goto done;
728 printf("\nIndexed %s.pack\n", id_str);
729 done:
730 if (repo)
731 got_repo_close(repo);
732 free(id_str);
733 free(pack_hash);
734 return error;
737 __dead static void
738 usage_listpack(void)
740 fprintf(stderr, "usage: %s listpack [-h] [-s] packfile-path\n",
741 getprogname());
742 exit(1);
745 struct gotadmin_list_pack_cb_args {
746 int nblobs;
747 int ntrees;
748 int ncommits;
749 int ntags;
750 int noffdeltas;
751 int nrefdeltas;
752 int human_readable;
753 };
755 static const struct got_error *
756 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
757 off_t size, off_t base_offset, struct got_object_id *base_id)
759 const struct got_error *err;
760 struct gotadmin_list_pack_cb_args *a = arg;
761 char *id_str, *delta_str = NULL, *base_id_str = NULL;
762 const char *type_str;
764 err = got_object_id_str(&id_str, id);
765 if (err)
766 return err;
768 switch (type) {
769 case GOT_OBJ_TYPE_BLOB:
770 type_str = GOT_OBJ_LABEL_BLOB;
771 a->nblobs++;
772 break;
773 case GOT_OBJ_TYPE_TREE:
774 type_str = GOT_OBJ_LABEL_TREE;
775 a->ntrees++;
776 break;
777 case GOT_OBJ_TYPE_COMMIT:
778 type_str = GOT_OBJ_LABEL_COMMIT;
779 a->ncommits++;
780 break;
781 case GOT_OBJ_TYPE_TAG:
782 type_str = GOT_OBJ_LABEL_TAG;
783 a->ntags++;
784 break;
785 case GOT_OBJ_TYPE_OFFSET_DELTA:
786 type_str = "offset-delta";
787 if (asprintf(&delta_str, " base-offset %lld",
788 (long long)base_offset) == -1) {
789 err = got_error_from_errno("asprintf");
790 goto done;
792 a->noffdeltas++;
793 break;
794 case GOT_OBJ_TYPE_REF_DELTA:
795 type_str = "ref-delta";
796 err = got_object_id_str(&base_id_str, base_id);
797 if (err)
798 goto done;
799 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
800 err = got_error_from_errno("asprintf");
801 goto done;
803 a->nrefdeltas++;
804 break;
805 default:
806 err = got_error(GOT_ERR_OBJ_TYPE);
807 goto done;
809 if (a->human_readable) {
810 char scaled[FMT_SCALED_STRSIZE];
811 char *s;;
812 if (fmt_scaled(size, scaled) == -1) {
813 err = got_error_from_errno("fmt_scaled");
814 goto done;
816 s = scaled;
817 while (isspace((unsigned char)*s))
818 s++;
819 printf("%s %s at %lld size %s%s\n", id_str, type_str,
820 (long long)offset, s, delta_str ? delta_str : "");
821 } else {
822 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
823 (long long)offset, (long long)size,
824 delta_str ? delta_str : "");
826 done:
827 free(id_str);
828 free(base_id_str);
829 free(delta_str);
830 return err;
833 static const struct got_error *
834 cmd_listpack(int argc, char *argv[])
836 const struct got_error *error = NULL;
837 struct got_repository *repo = NULL;
838 int ch;
839 struct got_object_id *pack_hash = NULL;
840 char *packfile_path = NULL;
841 char *id_str = NULL;
842 struct gotadmin_list_pack_cb_args lpa;
843 FILE *packfile = NULL;
844 int show_stats = 0, human_readable = 0;
846 while ((ch = getopt(argc, argv, "hs")) != -1) {
847 switch (ch) {
848 case 'h':
849 human_readable = 1;
850 break;
851 case 's':
852 show_stats = 1;
853 break;
854 default:
855 usage_listpack();
856 /* NOTREACHED */
860 argc -= optind;
861 argv += optind;
863 if (argc != 1)
864 usage_listpack();
865 packfile_path = realpath(argv[0], NULL);
866 if (packfile_path == NULL)
867 return got_error_from_errno2("realpath", argv[0]);
869 #ifndef PROFILE
870 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
871 NULL) == -1)
872 err(1, "pledge");
873 #endif
874 error = got_repo_open(&repo, packfile_path, NULL);
875 if (error)
876 goto done;
878 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
879 if (error)
880 goto done;
882 error = got_repo_find_pack(&packfile, &pack_hash, repo,
883 packfile_path);
884 if (error)
885 goto done;
886 error = got_object_id_str(&id_str, pack_hash);
887 if (error)
888 goto done;
890 memset(&lpa, 0, sizeof(lpa));
891 lpa.human_readable = human_readable;
892 error = got_repo_list_pack(packfile, pack_hash, repo,
893 list_pack_cb, &lpa, check_cancelled, NULL);
894 if (error)
895 goto done;
896 if (show_stats) {
897 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
898 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
899 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
900 lpa.noffdeltas + lpa.nrefdeltas,
901 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
902 lpa.noffdeltas, lpa.nrefdeltas);
904 done:
905 if (repo)
906 got_repo_close(repo);
907 free(id_str);
908 free(pack_hash);
909 free(packfile_path);
910 return error;
913 __dead static void
914 usage_cleanup(void)
916 fprintf(stderr, "usage: %s cleanup [-a] [-p] [-n] [-r repository-path] "
917 "[-q]\n", getprogname());
918 exit(1);
921 struct got_cleanup_progress_arg {
922 int last_nloose;
923 int last_ncommits;
924 int last_npurged;
925 int verbosity;
926 int printed_something;
927 int dry_run;
928 };
930 static const struct got_error *
931 cleanup_progress(void *arg, int nloose, int ncommits, int npurged)
933 struct got_cleanup_progress_arg *a = arg;
934 int print_loose = 0, print_commits = 0, print_purged = 0;
936 if (a->last_nloose != nloose) {
937 print_loose = 1;
938 a->last_nloose = nloose;
940 if (a->last_ncommits != ncommits) {
941 print_loose = 1;
942 print_commits = 1;
943 a->last_ncommits = ncommits;
945 if (a->last_npurged != npurged) {
946 print_loose = 1;
947 print_commits = 1;
948 print_purged = 1;
949 a->last_npurged = npurged;
952 if (a->verbosity < 0)
953 return NULL;
955 if (print_loose || print_commits || print_purged)
956 printf("\r");
957 if (print_loose)
958 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
959 if (print_commits)
960 printf("; %d commit%s scanned", ncommits,
961 ncommits == 1 ? "" : "s");
962 if (print_purged) {
963 if (a->dry_run) {
964 printf("; %d object%s could be purged", npurged,
965 npurged == 1 ? "" : "s");
966 } else {
967 printf("; %d object%s purged", npurged,
968 npurged == 1 ? "" : "s");
971 if (print_loose || print_commits || print_purged) {
972 a->printed_something = 1;
973 fflush(stdout);
975 return NULL;
978 struct got_lonely_packidx_progress_arg {
979 int verbosity;
980 int printed_something;
981 int dry_run;
982 };
984 static const struct got_error *
985 lonely_packidx_progress(void *arg, const char *path)
987 struct got_lonely_packidx_progress_arg *a = arg;
989 if (a->verbosity < 0)
990 return NULL;
992 if (a->dry_run)
993 printf("%s could be removed\n", path);
994 else
995 printf("%s removed\n", path);
997 a->printed_something = 1;
998 return NULL;
1001 static const struct got_error *
1002 cmd_cleanup(int argc, char *argv[])
1004 const struct got_error *error = NULL;
1005 char *cwd = NULL, *repo_path = NULL;
1006 struct got_repository *repo = NULL;
1007 int ch, dry_run = 0, npacked = 0, verbosity = 0;
1008 int remove_lonely_packidx = 0, ignore_mtime = 0;
1009 struct got_cleanup_progress_arg cpa;
1010 struct got_lonely_packidx_progress_arg lpa;
1011 off_t size_before, size_after;
1012 char scaled_before[FMT_SCALED_STRSIZE];
1013 char scaled_after[FMT_SCALED_STRSIZE];
1014 char scaled_diff[FMT_SCALED_STRSIZE];
1015 char **extensions;
1016 int nextensions, i;
1018 while ((ch = getopt(argc, argv, "apr:nq")) != -1) {
1019 switch (ch) {
1020 case 'a':
1021 ignore_mtime = 1;
1022 break;
1023 case 'p':
1024 remove_lonely_packidx = 1;
1025 break;
1026 case 'r':
1027 repo_path = realpath(optarg, NULL);
1028 if (repo_path == NULL)
1029 return got_error_from_errno2("realpath",
1030 optarg);
1031 got_path_strip_trailing_slashes(repo_path);
1032 break;
1033 case 'n':
1034 dry_run = 1;
1035 break;
1036 case 'q':
1037 verbosity = -1;
1038 break;
1039 default:
1040 usage_cleanup();
1041 /* NOTREACHED */
1045 argc -= optind;
1046 argv += optind;
1048 #ifndef PROFILE
1049 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1050 NULL) == -1)
1051 err(1, "pledge");
1052 #endif
1053 cwd = getcwd(NULL, 0);
1054 if (cwd == NULL) {
1055 error = got_error_from_errno("getcwd");
1056 goto done;
1059 error = got_repo_open(&repo, repo_path ? repo_path : cwd, NULL);
1060 if (error)
1061 goto done;
1063 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1064 if (error)
1065 goto done;
1067 got_repo_get_gitconfig_extensions(&extensions, &nextensions,
1068 repo);
1069 for (i = 0; i < nextensions; i++) {
1070 if (strcasecmp(extensions[i], "preciousObjects") == 0) {
1071 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1072 "the preciousObjects Git extension is enabled; "
1073 "this implies that objects must not be deleted");
1074 goto done;
1078 if (remove_lonely_packidx) {
1079 memset(&lpa, 0, sizeof(lpa));
1080 lpa.dry_run = dry_run;
1081 lpa.verbosity = verbosity;
1082 error = got_repo_remove_lonely_packidx(repo, dry_run,
1083 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1084 goto done;
1087 memset(&cpa, 0, sizeof(cpa));
1088 cpa.last_ncommits = -1;
1089 cpa.last_npurged = -1;
1090 cpa.dry_run = dry_run;
1091 cpa.verbosity = verbosity;
1092 error = got_repo_purge_unreferenced_loose_objects(repo,
1093 &size_before, &size_after, &npacked, dry_run, ignore_mtime,
1094 cleanup_progress, &cpa, check_cancelled, NULL);
1095 if (cpa.printed_something)
1096 printf("\n");
1097 if (error)
1098 goto done;
1099 if (cpa.printed_something) {
1100 if (fmt_scaled(size_before, scaled_before) == -1) {
1101 error = got_error_from_errno("fmt_scaled");
1102 goto done;
1104 if (fmt_scaled(size_after, scaled_after) == -1) {
1105 error = got_error_from_errno("fmt_scaled");
1106 goto done;
1108 if (fmt_scaled(size_before - size_after, scaled_diff) == -1) {
1109 error = got_error_from_errno("fmt_scaled");
1110 goto done;
1112 printf("loose total size before: %s\n", scaled_before);
1113 printf("loose total size after: %s\n", scaled_after);
1114 if (dry_run) {
1115 printf("disk space which would be freed: %s\n",
1116 scaled_diff);
1117 } else
1118 printf("disk space freed: %s\n", scaled_diff);
1119 printf("loose objects also found in pack files: %d\n", npacked);
1121 done:
1122 if (repo)
1123 got_repo_close(repo);
1124 free(cwd);
1125 return error;