2 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/types.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_repository_dump.h"
42 #include "got_repository_load.h"
43 #include "got_gotconfig.h"
45 #include "got_privsep.h"
46 #include "got_opentemp.h"
47 #include "got_worktree.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
57 catch_sigint(int signo)
63 catch_sigpipe(int signo)
68 static const struct got_error *
69 check_cancelled(void *arg)
71 if (sigint_received || sigpipe_received)
72 return got_error(GOT_ERR_CANCELLED);
78 const struct got_error *(*cmd_main)(int, char *[]);
79 void (*cmd_usage)(void);
80 const char *cmd_alias;
83 __dead static void usage(int, int);
84 __dead static void usage_init(void);
85 __dead static void usage_info(void);
86 __dead static void usage_pack(void);
87 __dead static void usage_indexpack(void);
88 __dead static void usage_listpack(void);
89 __dead static void usage_cleanup(void);
90 __dead static void usage_dump(void);
91 __dead static void usage_load(void);
93 static const struct got_error* cmd_init(int, char *[]);
94 static const struct got_error* cmd_info(int, char *[]);
95 static const struct got_error* cmd_pack(int, char *[]);
96 static const struct got_error* cmd_indexpack(int, char *[]);
97 static const struct got_error* cmd_listpack(int, char *[]);
98 static const struct got_error* cmd_cleanup(int, char *[]);
99 static const struct got_error* cmd_dump(int, char *[]);
100 static const struct got_error* cmd_load(int, char *[]);
102 static const struct gotadmin_cmd gotadmin_commands[] = {
103 { "init", cmd_init, usage_init, "" },
104 { "info", cmd_info, usage_info, "" },
105 { "pack", cmd_pack, usage_pack, "" },
106 { "indexpack", cmd_indexpack, usage_indexpack,"ix" },
107 { "listpack", cmd_listpack, usage_listpack, "ls" },
108 { "cleanup", cmd_cleanup, usage_cleanup, "cl" },
109 { "dump", cmd_dump, usage_dump, "" },
110 { "load", cmd_load, usage_load, "" },
114 list_commands(FILE *fp)
118 fprintf(fp, "commands:");
119 for (i = 0; i < nitems(gotadmin_commands); i++) {
120 const struct gotadmin_cmd *cmd = &gotadmin_commands[i];
121 fprintf(fp, " %s", cmd->cmd_name);
127 main(int argc, char *argv[])
129 const struct gotadmin_cmd *cmd;
132 int hflag = 0, Vflag = 0;
133 static const struct option longopts[] = {
134 { "version", no_argument, NULL, 'V' },
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
160 got_version_print_str();
165 usage(hflag, hflag ? 0 : 1);
167 signal(SIGINT, catch_sigint);
168 signal(SIGPIPE, catch_sigpipe);
170 for (i = 0; i < nitems(gotadmin_commands); i++) {
171 const struct got_error *error;
173 cmd = &gotadmin_commands[i];
175 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
176 strcmp(cmd->cmd_alias, argv[0]) != 0)
182 error = cmd->cmd_main(argc, argv);
183 if (error && error->code != GOT_ERR_CANCELLED &&
184 error->code != GOT_ERR_PRIVSEP_EXIT &&
185 !(sigpipe_received &&
186 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
188 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
189 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
196 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
197 list_commands(stderr);
202 usage(int hflag, int status)
204 FILE *fp = (status == 0) ? stdout : stderr;
206 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
213 static const struct got_error *
214 apply_unveil(const char *repo_path, int repo_read_only)
216 const struct got_error *err;
219 if (unveil("gmon.out", "rwc") != 0)
220 return got_error_from_errno2("unveil", "gmon.out");
222 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
223 return got_error_from_errno2("unveil", repo_path);
225 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
226 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
228 err = got_privsep_unveil_exec_helpers();
232 if (unveil(NULL, NULL) != 0)
233 return got_error_from_errno("unveil");
241 fprintf(stderr, "usage: %s info [-r repository-path]\n",
246 static const struct got_error *
247 get_repo_path(char **repo_path)
249 const struct got_error *err = NULL;
250 struct got_worktree *worktree = NULL;
255 cwd = getcwd(NULL, 0);
257 return got_error_from_errno("getcwd");
259 err = got_worktree_open(&worktree, cwd);
261 if (err->code != GOT_ERR_NOT_WORKTREE)
267 *repo_path = strdup(got_worktree_get_repo_path(worktree));
269 *repo_path = strdup(cwd);
270 if (*repo_path == NULL)
271 err = got_error_from_errno("strdup");
274 got_worktree_close(worktree);
282 fprintf(stderr, "usage: %s init [-b branch] repository-path\n",
287 static const struct got_error *
288 cmd_init(int argc, char *argv[])
290 const struct got_error *error = NULL;
291 const char *head_name = NULL;
292 char *repo_path = NULL;
296 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
300 while ((ch = getopt(argc, argv, "b:")) != -1) {
317 repo_path = strdup(argv[0]);
318 if (repo_path == NULL)
319 return got_error_from_errno("strdup");
321 got_path_strip_trailing_slashes(repo_path);
323 error = got_path_mkdir(repo_path);
325 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
328 error = apply_unveil(repo_path, 0);
332 error = got_repo_init(repo_path, head_name);
338 static const struct got_error *
339 cmd_info(int argc, char *argv[])
341 const struct got_error *error = NULL;
342 char *repo_path = NULL;
343 struct got_repository *repo = NULL;
344 const struct got_gotconfig *gotconfig = NULL;
345 int ch, npackfiles, npackedobj, nobj;
346 off_t packsize, loose_size;
347 char scaled[FMT_SCALED_STRSIZE];
348 int *pack_fds = NULL;
351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
356 while ((ch = getopt(argc, argv, "r:")) != -1) {
359 repo_path = realpath(optarg, NULL);
360 if (repo_path == NULL)
361 return got_error_from_errno2("realpath",
363 got_path_strip_trailing_slashes(repo_path);
374 if (repo_path == NULL) {
375 error = get_repo_path(&repo_path);
379 error = got_repo_pack_fds_open(&pack_fds);
382 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
386 /* Remove "cpath" promise. */
387 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
391 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
395 printf("repository: %s\n", got_repo_get_path_git_dir(repo));
397 gotconfig = got_repo_get_gotconfig(repo);
399 const struct got_remote_repo *remotes;
401 if (got_gotconfig_get_author(gotconfig)) {
402 printf("default author: %s\n",
403 got_gotconfig_get_author(gotconfig));
405 got_gotconfig_get_remotes(&nremotes, &remotes, gotconfig);
406 for (i = 0; i < nremotes; i++) {
407 const char *fetch_url = remotes[i].fetch_url;
408 const char *send_url = remotes[i].send_url;
409 if (strcmp(fetch_url, send_url) == 0) {
410 printf("remote \"%s\": %s\n", remotes[i].name,
411 remotes[i].fetch_url);
413 printf("remote \"%s\" (fetch): %s\n",
414 remotes[i].name, remotes[i].fetch_url);
415 printf("remote \"%s\" (send): %s\n",
416 remotes[i].name, remotes[i].send_url);
421 error = got_repo_get_packfile_info(&npackfiles, &npackedobj,
425 printf("pack files: %d\n", npackfiles);
426 if (npackfiles > 0) {
427 if (fmt_scaled(packsize, scaled) == -1) {
428 error = got_error_from_errno("fmt_scaled");
431 printf("packed objects: %d\n", npackedobj);
432 printf("packed total size: %s\n", scaled);
435 error = got_repo_get_loose_object_info(&nobj, &loose_size, repo);
438 printf("loose objects: %d\n", nobj);
440 if (fmt_scaled(loose_size, scaled) == -1) {
441 error = got_error_from_errno("fmt_scaled");
444 printf("loose total size: %s\n", scaled);
448 got_repo_close(repo);
450 const struct got_error *pack_err =
451 got_repo_pack_fds_close(pack_fds);
463 fprintf(stderr, "usage: %s pack [-aDq] [-r repository-path] "
464 "[-x reference] [reference ...]\n", getprogname());
468 struct got_pack_progress_arg {
470 char last_scaled_size[FMT_SCALED_STRSIZE];
482 int printed_something;
486 print_load_info(FILE *out, int print_colored, int print_found, int print_trees,
487 int ncolored, int nfound, int ntrees)
490 fprintf(out, "%d commit%s colored", ncolored,
491 ncolored == 1 ? "" : "s");
494 fprintf(out, "%s%d object%s found",
495 ncolored > 0 ? "; " : "",
496 nfound, nfound == 1 ? "" : "s");
499 fprintf(out, "; %d tree%s scanned", ntrees,
500 ntrees == 1 ? "" : "s");
504 static const struct got_error *
505 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
506 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
509 struct got_pack_progress_arg *a = arg;
510 char scaled_size[FMT_SCALED_STRSIZE];
511 int p_deltify, p_written;
512 int print_colored = 0, print_found = 0, print_trees = 0;
513 int print_searching = 0, print_total = 0;
514 int print_deltify = 0, print_written = 0;
516 if (a->verbosity < 0)
519 if (a->last_ncolored != ncolored) {
521 a->last_ncolored = ncolored;
524 if (a->last_nfound != nfound) {
527 a->last_nfound = nfound;
530 if (a->last_ntrees != ntrees) {
534 a->last_ntrees = ntrees;
537 if ((print_colored || print_found || print_trees) &&
539 fprintf(a->out, "\r");
540 print_load_info(a->out, print_colored, print_found,
541 print_trees, ncolored, nfound, ntrees);
542 a->printed_something = 1;
545 } else if (!a->loading_done) {
546 fprintf(a->out, "\r");
547 print_load_info(a->out, 1, 1, 1, ncolored, nfound, ntrees);
548 fprintf(a->out, "\n");
552 if (fmt_scaled(packfile_size, scaled_size) == -1)
553 return got_error_from_errno("fmt_scaled");
555 if (a->last_ncommits != ncommits) {
557 a->last_ncommits = ncommits;
560 if (a->last_nobj_total != nobj_total) {
563 a->last_nobj_total = nobj_total;
566 if (packfile_size > 0 && (a->last_scaled_size[0] == '\0' ||
567 strcmp(scaled_size, a->last_scaled_size)) != 0) {
568 if (strlcpy(a->last_scaled_size, scaled_size,
569 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
570 return got_error(GOT_ERR_NO_SPACE);
573 if (nobj_deltify > 0 || nobj_written > 0) {
574 if (nobj_deltify > 0) {
575 p_deltify = (nobj_deltify * 100) / nobj_total;
576 if (p_deltify != a->last_p_deltify) {
577 a->last_p_deltify = p_deltify;
583 if (nobj_written > 0) {
584 p_written = (nobj_written * 100) / nobj_total;
585 if (p_written != a->last_p_written) {
586 a->last_p_written = p_written;
595 if (print_searching || print_total || print_deltify || print_written)
596 fprintf(a->out, "\r");
598 fprintf(a->out, "packing %d reference%s", ncommits,
599 ncommits == 1 ? "" : "s");
601 fprintf(a->out, "; %d object%s", nobj_total,
602 nobj_total == 1 ? "" : "s");
604 fprintf(a->out, "; deltify: %d%%", p_deltify);
606 fprintf(a->out, "; writing pack: %*s %d%%",
607 FMT_SCALED_STRSIZE - 2, scaled_size, p_written);
608 if (print_searching || print_total || print_deltify ||
610 a->printed_something = 1;
616 static const struct got_error *
617 pack_index_progress(void *arg, off_t packfile_size, int nobj_total,
618 int nobj_indexed, int nobj_loose, int nobj_resolved)
620 struct got_pack_progress_arg *a = arg;
621 char scaled_size[FMT_SCALED_STRSIZE];
622 int p_indexed, p_resolved;
623 int print_size = 0, print_indexed = 0, print_resolved = 0;
625 if (a->verbosity < 0)
628 if (packfile_size > 0 || nobj_indexed > 0) {
629 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
630 (a->last_scaled_size[0] == '\0' ||
631 strcmp(scaled_size, a->last_scaled_size)) != 0) {
633 if (strlcpy(a->last_scaled_size, scaled_size,
634 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
635 return got_error(GOT_ERR_NO_SPACE);
637 if (nobj_indexed > 0) {
638 p_indexed = (nobj_indexed * 100) / nobj_total;
639 if (p_indexed != a->last_p_indexed) {
640 a->last_p_indexed = p_indexed;
645 if (nobj_resolved > 0) {
646 p_resolved = (nobj_resolved * 100) /
647 (nobj_total - nobj_loose);
648 if (p_resolved != a->last_p_resolved) {
649 a->last_p_resolved = p_resolved;
657 if (print_size || print_indexed || print_resolved)
660 printf("%*s packed", FMT_SCALED_STRSIZE - 2, scaled_size);
662 printf("; indexing %d%%", p_indexed);
664 printf("; resolving deltas %d%%", p_resolved);
665 if (print_size || print_indexed || print_resolved)
671 static const struct got_error *
672 add_ref(struct got_reflist_entry **new, struct got_reflist_head *refs,
673 const char *refname, struct got_repository *repo)
675 const struct got_error *err;
676 struct got_reference *ref;
680 err = got_ref_open(&ref, repo, refname, 0);
682 if (err->code != GOT_ERR_NOT_REF)
685 /* Treat argument as a reference prefix. */
686 err = got_ref_list(refs, repo, refname,
687 got_ref_cmp_by_name, NULL);
689 err = got_reflist_insert(new, refs, ref,
690 got_ref_cmp_by_name, NULL);
691 if (err || *new == NULL /* duplicate */)
698 static const struct got_error *
699 cmd_pack(int argc, char *argv[])
701 const struct got_error *error = NULL;
702 char *repo_path = NULL;
703 struct got_repository *repo = NULL;
704 int ch, i, loose_obj_only = 1, force_refdelta = 0, verbosity = 0;
705 struct got_object_id *pack_hash = NULL;
707 struct got_pack_progress_arg ppa;
708 FILE *packfile = NULL;
709 struct got_pathlist_head exclude_args;
710 struct got_pathlist_entry *pe;
711 struct got_reflist_head exclude_refs;
712 struct got_reflist_head include_refs;
713 struct got_reflist_entry *re, *new;
714 int *pack_fds = NULL;
716 TAILQ_INIT(&exclude_args);
717 TAILQ_INIT(&exclude_refs);
718 TAILQ_INIT(&include_refs);
721 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
726 while ((ch = getopt(argc, argv, "aDqr:x:")) != -1) {
738 repo_path = realpath(optarg, NULL);
739 if (repo_path == NULL)
740 return got_error_from_errno2("realpath",
742 got_path_strip_trailing_slashes(repo_path);
745 got_path_strip_trailing_slashes(optarg);
746 error = got_pathlist_append(&exclude_args,
760 if (repo_path == NULL) {
761 error = get_repo_path(&repo_path);
765 error = got_repo_pack_fds_open(&pack_fds);
768 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
772 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
776 TAILQ_FOREACH(pe, &exclude_args, entry) {
777 const char *refname = pe->path;
778 error = add_ref(&new, &exclude_refs, refname, repo);
784 error = got_ref_list(&include_refs, repo, "",
785 got_ref_cmp_by_name, NULL);
789 for (i = 0; i < argc; i++) {
791 got_path_strip_trailing_slashes(argv[i]);
793 error = add_ref(&new, &include_refs, refname, repo);
799 /* Ignore references in the refs/got/ namespace. */
800 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
801 const char *refname = got_ref_get_name(re->ref);
802 if (strncmp("refs/got/", refname, 9) != 0)
804 TAILQ_REMOVE(&include_refs, re, entry);
805 got_ref_close(re->ref);
809 memset(&ppa, 0, sizeof(ppa));
811 ppa.last_scaled_size[0] = '\0';
812 ppa.last_p_indexed = -1;
813 ppa.last_p_resolved = -1;
814 ppa.verbosity = verbosity;
816 error = got_repo_pack_objects(&packfile, &pack_hash,
817 &include_refs, &exclude_refs, repo, loose_obj_only,
818 force_refdelta, pack_progress, &ppa, check_cancelled, NULL);
820 if (ppa.printed_something)
825 error = got_object_id_str(&id_str, pack_hash);
829 printf("\nWrote %s.pack\n", id_str);
831 error = got_repo_index_pack(packfile, pack_hash, repo,
832 pack_index_progress, &ppa, check_cancelled, NULL);
836 printf("\nIndexed %s.pack\n", id_str);
839 got_repo_close(repo);
841 const struct got_error *pack_err =
842 got_repo_pack_fds_close(pack_fds);
846 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
847 got_ref_list_free(&exclude_refs);
848 got_ref_list_free(&include_refs);
856 usage_indexpack(void)
858 fprintf(stderr, "usage: %s indexpack packfile-path\n",
863 static const struct got_error *
864 cmd_indexpack(int argc, char *argv[])
866 const struct got_error *error = NULL;
867 struct got_repository *repo = NULL;
869 struct got_object_id *pack_hash = NULL;
870 char *packfile_path = NULL;
872 struct got_pack_progress_arg ppa;
873 FILE *packfile = NULL;
874 int *pack_fds = NULL;
876 while ((ch = getopt(argc, argv, "")) != -1) {
890 packfile_path = realpath(argv[0], NULL);
891 if (packfile_path == NULL)
892 return got_error_from_errno2("realpath", argv[0]);
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd unveil",
900 error = got_repo_pack_fds_open(&pack_fds);
903 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
907 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
911 memset(&ppa, 0, sizeof(ppa));
913 ppa.last_scaled_size[0] = '\0';
914 ppa.last_p_indexed = -1;
915 ppa.last_p_resolved = -1;
917 error = got_repo_find_pack(&packfile, &pack_hash, repo,
922 error = got_object_id_str(&id_str, pack_hash);
926 error = got_repo_index_pack(packfile, pack_hash, repo,
927 pack_index_progress, &ppa, check_cancelled, NULL);
930 printf("\nIndexed %s.pack\n", id_str);
933 got_repo_close(repo);
935 const struct got_error *pack_err =
936 got_repo_pack_fds_close(pack_fds);
948 fprintf(stderr, "usage: %s listpack [-hs] packfile-path\n",
953 struct gotadmin_list_pack_cb_args {
963 static const struct got_error *
964 list_pack_cb(void *arg, struct got_object_id *id, int type, off_t offset,
965 off_t size, off_t base_offset, struct got_object_id *base_id)
967 const struct got_error *err;
968 struct gotadmin_list_pack_cb_args *a = arg;
969 char *id_str, *delta_str = NULL, *base_id_str = NULL;
970 const char *type_str;
972 err = got_object_id_str(&id_str, id);
977 case GOT_OBJ_TYPE_BLOB:
978 type_str = GOT_OBJ_LABEL_BLOB;
981 case GOT_OBJ_TYPE_TREE:
982 type_str = GOT_OBJ_LABEL_TREE;
985 case GOT_OBJ_TYPE_COMMIT:
986 type_str = GOT_OBJ_LABEL_COMMIT;
989 case GOT_OBJ_TYPE_TAG:
990 type_str = GOT_OBJ_LABEL_TAG;
993 case GOT_OBJ_TYPE_OFFSET_DELTA:
994 type_str = "offset-delta";
995 if (asprintf(&delta_str, " base-offset %lld",
996 (long long)base_offset) == -1) {
997 err = got_error_from_errno("asprintf");
1002 case GOT_OBJ_TYPE_REF_DELTA:
1003 type_str = "ref-delta";
1004 err = got_object_id_str(&base_id_str, base_id);
1007 if (asprintf(&delta_str, " base-id %s", base_id_str) == -1) {
1008 err = got_error_from_errno("asprintf");
1014 err = got_error(GOT_ERR_OBJ_TYPE);
1017 if (a->human_readable) {
1018 char scaled[FMT_SCALED_STRSIZE];
1020 if (fmt_scaled(size, scaled) == -1) {
1021 err = got_error_from_errno("fmt_scaled");
1025 while (isspace((unsigned char)*s))
1027 printf("%s %s at %lld size %s%s\n", id_str, type_str,
1028 (long long)offset, s, delta_str ? delta_str : "");
1030 printf("%s %s at %lld size %lld%s\n", id_str, type_str,
1031 (long long)offset, (long long)size,
1032 delta_str ? delta_str : "");
1041 static const struct got_error *
1042 cmd_listpack(int argc, char *argv[])
1044 const struct got_error *error = NULL;
1045 struct got_repository *repo = NULL;
1047 struct got_object_id *pack_hash = NULL;
1048 char *packfile_path = NULL;
1049 char *id_str = NULL;
1050 struct gotadmin_list_pack_cb_args lpa;
1051 FILE *packfile = NULL;
1052 int show_stats = 0, human_readable = 0;
1053 int *pack_fds = NULL;
1056 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1061 while ((ch = getopt(argc, argv, "hs")) != -1) {
1080 packfile_path = realpath(argv[0], NULL);
1081 if (packfile_path == NULL)
1082 return got_error_from_errno2("realpath", argv[0]);
1084 error = got_repo_pack_fds_open(&pack_fds);
1087 error = got_repo_open(&repo, packfile_path, NULL, pack_fds);
1091 /* Remove "cpath" promise. */
1092 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1096 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1100 error = got_repo_find_pack(&packfile, &pack_hash, repo,
1104 error = got_object_id_str(&id_str, pack_hash);
1108 memset(&lpa, 0, sizeof(lpa));
1109 lpa.human_readable = human_readable;
1110 error = got_repo_list_pack(packfile, pack_hash, repo,
1111 list_pack_cb, &lpa, check_cancelled, NULL);
1115 printf("objects: %d\n blobs: %d\n trees: %d\n commits: %d\n"
1116 " tags: %d\n offset-deltas: %d\n ref-deltas: %d\n",
1117 lpa.nblobs + lpa.ntrees + lpa.ncommits + lpa.ntags +
1118 lpa.noffdeltas + lpa.nrefdeltas,
1119 lpa.nblobs, lpa.ntrees, lpa.ncommits, lpa.ntags,
1120 lpa.noffdeltas, lpa.nrefdeltas);
1124 got_repo_close(repo);
1126 const struct got_error *pack_err =
1127 got_repo_pack_fds_close(pack_fds);
1133 free(packfile_path);
1140 fprintf(stderr, "usage: %s cleanup [-anpq] [-r repository-path]\n",
1145 struct got_cleanup_progress_arg {
1149 int last_nredundant;
1151 int printed_something;
1155 static const struct got_error *
1156 cleanup_progress(void *arg, int nloose, int ncommits, int npurged,
1159 struct got_cleanup_progress_arg *a = arg;
1160 int print_loose = 0, print_commits = 0, print_purged = 0;
1161 int print_redundant = 0;
1163 if (a->last_nloose != nloose) {
1165 a->last_nloose = nloose;
1167 if (a->last_ncommits != ncommits) {
1170 a->last_ncommits = ncommits;
1172 if (a->last_npurged != npurged) {
1176 a->last_npurged = npurged;
1178 if (a->last_nredundant != nredundant) {
1182 print_redundant = 1;
1183 a->last_nredundant = nredundant;
1186 if (a->verbosity < 0)
1189 if (print_loose || print_commits || print_purged || print_redundant)
1192 printf("%d loose object%s", nloose, nloose == 1 ? "" : "s");
1194 printf("; %d commit%s scanned", ncommits,
1195 ncommits == 1 ? "" : "s");
1196 if (print_purged || print_redundant) {
1198 printf("; could purge %d object%s", npurged,
1199 npurged == 1 ? "" : "s");
1201 printf("; purged %d object%s", npurged,
1202 npurged == 1 ? "" : "s");
1205 if (print_redundant) {
1207 printf(", %d pack file%s", nredundant,
1208 nredundant == 1 ? "" : "s");
1210 printf(", %d pack file%s", nredundant,
1211 nredundant == 1 ? "" : "s");
1214 if (print_loose || print_commits || print_purged || print_redundant) {
1215 a->printed_something = 1;
1221 struct got_lonely_packidx_progress_arg {
1223 int printed_something;
1227 static const struct got_error *
1228 lonely_packidx_progress(void *arg, const char *path)
1230 struct got_lonely_packidx_progress_arg *a = arg;
1232 if (a->verbosity < 0)
1236 printf("%s could be removed\n", path);
1238 printf("%s removed\n", path);
1240 a->printed_something = 1;
1244 static const struct got_error *
1245 cmd_cleanup(int argc, char *argv[])
1247 const struct got_error *error = NULL;
1248 char *repo_path = NULL;
1249 struct got_repository *repo = NULL;
1250 int ch, dry_run = 0, verbosity = 0;
1251 int ncommits = 0, nloose = 0, npacked = 0;
1252 int remove_lonely_packidx = 0, ignore_mtime = 0;
1253 struct got_lockfile *lock = NULL;
1254 struct got_cleanup_progress_arg cpa;
1255 struct got_lonely_packidx_progress_arg lpa;
1256 off_t loose_before, loose_after;
1257 off_t pack_before, pack_after;
1259 char loose_before_scaled[FMT_SCALED_STRSIZE];
1260 char loose_after_scaled[FMT_SCALED_STRSIZE];
1261 char pack_before_scaled[FMT_SCALED_STRSIZE];
1262 char pack_after_scaled[FMT_SCALED_STRSIZE];
1263 char total_size_scaled[FMT_SCALED_STRSIZE];
1264 int *pack_fds = NULL;
1267 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1272 while ((ch = getopt(argc, argv, "anpqr:")) != -1) {
1281 remove_lonely_packidx = 1;
1287 repo_path = realpath(optarg, NULL);
1288 if (repo_path == NULL)
1289 return got_error_from_errno2("realpath",
1291 got_path_strip_trailing_slashes(repo_path);
1302 if (repo_path == NULL) {
1303 error = get_repo_path(&repo_path);
1307 error = got_repo_pack_fds_open(&pack_fds);
1310 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1314 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1318 if (got_repo_has_extension(repo, "preciousObjects")) {
1319 error = got_error_msg(GOT_ERR_GIT_REPO_EXT,
1320 "the preciousObjects Git extension is enabled; "
1321 "this implies that objects must not be deleted");
1325 error = got_repo_cleanup_prepare(repo, &lock);
1329 if (remove_lonely_packidx) {
1330 memset(&lpa, 0, sizeof(lpa));
1331 lpa.dry_run = dry_run;
1332 lpa.verbosity = verbosity;
1333 error = got_repo_remove_lonely_packidx(repo, dry_run,
1334 lonely_packidx_progress, &lpa, check_cancelled, NULL);
1338 memset(&cpa, 0, sizeof(cpa));
1339 cpa.last_ncommits = -1;
1340 cpa.last_npurged = -1;
1341 cpa.last_nredundant = -1;
1342 cpa.dry_run = dry_run;
1343 cpa.verbosity = verbosity;
1345 error = got_repo_purge_unreferenced_loose_objects(repo,
1346 &loose_before, &loose_after, &ncommits, &nloose, &npacked,
1347 dry_run, ignore_mtime, cleanup_progress, &cpa,
1348 check_cancelled, NULL);
1350 if (cpa.printed_something)
1355 error = got_repo_purge_redundant_packfiles(repo, &pack_before,
1356 &pack_after, dry_run, ncommits, nloose, npacked,
1357 cleanup_progress, &cpa, check_cancelled, NULL);
1358 if (cpa.printed_something)
1363 total_size = (loose_before - loose_after) + (pack_before - pack_after);
1365 if (cpa.printed_something) {
1366 if (fmt_scaled(loose_before, loose_before_scaled) == -1) {
1367 error = got_error_from_errno("fmt_scaled");
1370 if (fmt_scaled(loose_after, loose_after_scaled) == -1) {
1371 error = got_error_from_errno("fmt_scaled");
1374 if (fmt_scaled(pack_before, pack_before_scaled) == -1) {
1375 error = got_error_from_errno("fmt_scaled");
1378 if (fmt_scaled(pack_after, pack_after_scaled) == -1) {
1379 error = got_error_from_errno("fmt_scaled");
1382 if (fmt_scaled(total_size, total_size_scaled) == -1) {
1383 error = got_error_from_errno("fmt_scaled");
1386 printf("loose total size before: %s\n", loose_before_scaled);
1387 printf("loose total size after: %s\n", loose_after_scaled);
1388 printf("pack files total size before: %s\n",
1389 pack_before_scaled);
1390 printf("pack files total size after: %s\n", pack_after_scaled);
1392 printf("disk space which would be freed: %s\n",
1395 printf("disk space freed: %s\n", total_size_scaled);
1396 printf("loose objects also found in pack files: %d\n", npacked);
1400 got_repo_cleanup_complete(repo, lock);
1402 got_repo_close(repo);
1404 const struct got_error *pack_err =
1405 got_repo_pack_fds_close(pack_fds);
1416 fprintf(stderr, "usage: %s dump [-q] [-r repository-path] "
1417 "[-x reference] [reference]...\n", getprogname());
1421 static const struct got_error *
1422 cmd_dump(int argc, char *argv[])
1424 const struct got_error *error = NULL;
1425 struct got_pack_progress_arg ppa;
1426 struct got_repository *repo = NULL;
1427 struct got_pathlist_head exclude_args;
1428 struct got_pathlist_entry *pe;
1429 struct got_reflist_head exclude_refs;
1430 struct got_reflist_head include_refs;
1431 struct got_reflist_entry *re, *new;
1432 const char *refname;
1433 char *repo_path = NULL;
1434 int *pack_fds = NULL;
1438 TAILQ_INIT(&exclude_args);
1439 TAILQ_INIT(&exclude_refs);
1440 TAILQ_INIT(&include_refs);
1443 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1448 while ((ch = getopt(argc, argv, "qr:x:")) != -1) {
1454 repo_path = realpath(optarg, NULL);
1455 if (repo_path == NULL)
1456 return got_error_from_errno2("realpath",
1458 got_path_strip_trailing_slashes(repo_path);
1461 error = got_pathlist_append(&exclude_args,
1474 if (repo_path == NULL) {
1475 error = get_repo_path(&repo_path);
1479 error = got_repo_pack_fds_open(&pack_fds);
1482 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1486 error = apply_unveil(got_repo_get_path_git_dir(repo), 1);
1490 TAILQ_FOREACH(pe, &exclude_args, entry) {
1492 error = add_ref(&new, &exclude_refs, refname, repo);
1498 error = got_ref_list(&include_refs, repo, "",
1499 got_ref_cmp_by_name, NULL);
1503 for (i = 0; i < argc; i++) {
1504 got_path_strip_trailing_slashes(argv[i]);
1506 error = add_ref(&new, &include_refs, refname, repo);
1512 /* Ignore references in the refs/got/ namespace. */
1513 TAILQ_FOREACH_SAFE(re, &include_refs, entry, new) {
1514 refname = got_ref_get_name(re->ref);
1515 if (strncmp("refs/got/", refname, 9) != 0)
1517 TAILQ_REMOVE(&include_refs, re, entry);
1518 got_ref_close(re->ref);
1522 memset(&ppa, 0, sizeof(ppa));
1524 ppa.verbosity = verbosity;
1526 error = got_repo_dump(stdout, &include_refs, &exclude_refs,
1527 repo, pack_progress, &ppa, check_cancelled, NULL);
1528 if (ppa.printed_something)
1529 fprintf(stderr, "\n");
1532 got_repo_close(repo);
1535 const struct got_error *pack_err;
1537 pack_err = got_repo_pack_fds_close(pack_fds);
1542 got_pathlist_free(&exclude_args, GOT_PATHLIST_FREE_NONE);
1543 got_ref_list_free(&exclude_refs);
1544 got_ref_list_free(&include_refs);
1553 fprintf(stderr, "usage: %s load [-lnq] [-b reference] "
1554 "[-r repository-path] [file]\n",
1559 static const struct got_error *
1560 load_progress(void *arg, off_t packfile_size, int nobj_total,
1561 int nobj_indexed, int nobj_loose, int nobj_resolved)
1563 return pack_index_progress(arg, packfile_size, nobj_total,
1564 nobj_indexed, nobj_loose, nobj_resolved);
1568 is_wanted_ref(struct got_pathlist_head *wanted, const char *ref)
1570 struct got_pathlist_entry *pe;
1572 if (TAILQ_EMPTY(wanted))
1575 TAILQ_FOREACH(pe, wanted, entry) {
1576 if (strcmp(pe->path, ref) == 0)
1583 static const struct got_error *
1584 create_ref(const char *refname, struct got_object_id *id,
1585 int verbosity, struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 struct got_reference *ref;
1591 err = got_object_id_str(&id_str, id);
1595 err = got_ref_alloc(&ref, refname, id);
1599 err = got_ref_write(ref, repo);
1602 if (err == NULL && verbosity >= 0)
1603 printf("Created reference %s: %s\n", refname, id_str);
1609 static const struct got_error *
1610 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1611 int replace_tags, int verbosity, struct got_repository *repo)
1613 const struct got_error *err = NULL;
1614 char *new_id_str = NULL;
1615 struct got_object_id *old_id = NULL;
1617 err = got_object_id_str(&new_id_str, new_id);
1621 if (!replace_tags &&
1622 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1623 err = got_ref_resolve(&old_id, repo, ref);
1626 if (got_object_id_cmp(old_id, new_id) == 0)
1628 if (verbosity >= 0) {
1629 printf("Rejecting update of existing tag %s: %s\n",
1630 got_ref_get_name(ref), new_id_str);
1635 if (got_ref_is_symbolic(ref)) {
1636 if (verbosity >= 0) {
1637 printf("Replacing reference %s: %s\n",
1638 got_ref_get_name(ref),
1639 got_ref_get_symref_target(ref));
1641 err = got_ref_change_symref_to_ref(ref, new_id);
1644 err = got_ref_write(ref, repo);
1648 err = got_ref_resolve(&old_id, repo, ref);
1651 if (got_object_id_cmp(old_id, new_id) == 0)
1654 err = got_ref_change_ref(ref, new_id);
1657 err = got_ref_write(ref, repo);
1663 printf("Updated %s: %s\n", got_ref_get_name(ref),
1671 static const struct got_error *
1672 cmd_load(int argc, char *argv[])
1674 const struct got_error *error = NULL;
1675 struct got_repository *repo = NULL;
1676 struct got_pathlist_head include_args;
1677 struct got_pathlist_head available_refs;
1678 struct got_pathlist_entry *pe;
1679 struct got_pack_progress_arg ppa;
1681 int *pack_fds = NULL;
1682 char *repo_path = NULL;
1683 int list_refs_only = 0;
1688 TAILQ_INIT(&include_args);
1689 TAILQ_INIT(&available_refs);
1692 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1693 "sendfd unveil", NULL) == -1)
1697 while ((ch = getopt(argc, argv, "b:lnqr:")) != -1) {
1700 error = got_pathlist_append(&include_args,
1715 repo_path = realpath(optarg, NULL);
1716 if (repo_path == NULL)
1717 return got_error_from_errno2("realpath",
1719 got_path_strip_trailing_slashes(repo_path);
1729 if (list_refs_only && !TAILQ_EMPTY(&include_args))
1730 errx(1, "-b and -l are mutually exclusive");
1732 if (list_refs_only && noop)
1733 errx(1, "-n and -l are mutually exclusive");
1738 in = fopen(argv[0], "re");
1740 return got_error_from_errno2("open", argv[0]);
1743 if (repo_path == NULL) {
1744 error = get_repo_path(&repo_path);
1748 error = got_repo_pack_fds_open(&pack_fds);
1751 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1755 error = apply_unveil(got_repo_get_path_git_dir(repo), 0);
1759 memset(&ppa, 0, sizeof(ppa));
1761 ppa.verbosity = verbosity;
1763 error = got_repo_load(in, &available_refs, repo, list_refs_only, noop,
1764 load_progress, &ppa, check_cancelled, NULL);
1765 if (verbosity >= 0 && !list_refs_only)
1770 if (list_refs_only) {
1771 TAILQ_FOREACH(pe, &available_refs, entry) {
1772 const char *refname = pe->path;
1773 struct got_object_id *id = pe->data;
1776 error = got_object_id_str(&idstr, id);
1780 printf("%s: %s\n", refname, idstr);
1789 /* Update references */
1790 TAILQ_FOREACH(pe, &available_refs, entry) {
1791 const struct got_error *unlock_err;
1792 struct got_reference *ref;
1793 const char *refname = pe->path;
1794 struct got_object_id *id = pe->data;
1796 if (!is_wanted_ref(&include_args, pe->path))
1799 error = got_ref_open(&ref, repo, refname, 1);
1801 if (error->code != GOT_ERR_NOT_REF)
1803 error = create_ref(refname, id, verbosity, repo);
1807 /* XXX: check advances only and add -f to force? */
1808 error = update_ref(ref, id, 1, verbosity, repo);
1809 unlock_err = got_ref_unlock(ref);
1810 if (unlock_err && error == NULL)
1819 if (in != stdin && fclose(in) == EOF && error == NULL)
1820 error = got_error_from_errno("fclose");
1823 got_repo_close(repo);
1826 const struct got_error *pack_err;
1828 pack_err = got_repo_pack_fds_close(pack_fds);
1833 got_pathlist_free(&include_args, GOT_PATHLIST_FREE_NONE);
1834 got_pathlist_free(&available_refs, GOT_PATHLIST_FREE_ALL);